1
0

RatKingSystem.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System.Numerics;
  2. using Content.Server.Atmos.EntitySystems;
  3. using Content.Server.Chat.Systems;
  4. using Content.Server.NPC;
  5. using Content.Server.NPC.HTN;
  6. using Content.Server.NPC.Systems;
  7. using Content.Server.Popups;
  8. using Content.Shared.Atmos;
  9. using Content.Shared.Dataset;
  10. using Content.Shared.Nutrition.Components;
  11. using Content.Shared.Nutrition.EntitySystems;
  12. using Content.Shared.Pointing;
  13. using Content.Shared.Random.Helpers;
  14. using Content.Shared.RatKing;
  15. using Robust.Shared.Map;
  16. using Robust.Shared.Random;
  17. namespace Content.Server.RatKing
  18. {
  19. /// <inheritdoc/>
  20. public sealed class RatKingSystem : SharedRatKingSystem
  21. {
  22. [Dependency] private readonly AtmosphereSystem _atmos = default!;
  23. [Dependency] private readonly ChatSystem _chat = default!;
  24. [Dependency] private readonly HTNSystem _htn = default!;
  25. [Dependency] private readonly HungerSystem _hunger = default!;
  26. [Dependency] private readonly NPCSystem _npc = default!;
  27. [Dependency] private readonly PopupSystem _popup = default!;
  28. public override void Initialize()
  29. {
  30. base.Initialize();
  31. SubscribeLocalEvent<RatKingComponent, RatKingRaiseArmyActionEvent>(OnRaiseArmy);
  32. SubscribeLocalEvent<RatKingComponent, RatKingDomainActionEvent>(OnDomain);
  33. SubscribeLocalEvent<RatKingComponent, AfterPointedAtEvent>(OnPointedAt);
  34. }
  35. /// <summary>
  36. /// Summons an allied rat servant at the King, costing a small amount of hunger
  37. /// </summary>
  38. private void OnRaiseArmy(EntityUid uid, RatKingComponent component, RatKingRaiseArmyActionEvent args)
  39. {
  40. if (args.Handled)
  41. return;
  42. if (!TryComp<HungerComponent>(uid, out var hunger))
  43. return;
  44. //make sure the hunger doesn't go into the negatives
  45. if (_hunger.GetHunger(hunger) < component.HungerPerArmyUse)
  46. {
  47. _popup.PopupEntity(Loc.GetString("rat-king-too-hungry"), uid, uid);
  48. return;
  49. }
  50. args.Handled = true;
  51. _hunger.ModifyHunger(uid, -component.HungerPerArmyUse, hunger);
  52. var servant = Spawn(component.ArmyMobSpawnId, Transform(uid).Coordinates);
  53. var comp = EnsureComp<RatKingServantComponent>(servant);
  54. comp.King = uid;
  55. Dirty(servant, comp);
  56. component.Servants.Add(servant);
  57. _npc.SetBlackboard(servant, NPCBlackboard.FollowTarget, new EntityCoordinates(uid, Vector2.Zero));
  58. UpdateServantNpc(servant, component.CurrentOrder);
  59. }
  60. /// <summary>
  61. /// uses hunger to release a specific amount of ammonia into the air. This heals the rat king
  62. /// and his servants through a specific metabolism.
  63. /// </summary>
  64. private void OnDomain(EntityUid uid, RatKingComponent component, RatKingDomainActionEvent args)
  65. {
  66. if (args.Handled)
  67. return;
  68. if (!TryComp<HungerComponent>(uid, out var hunger))
  69. return;
  70. //make sure the hunger doesn't go into the negatives
  71. if (_hunger.GetHunger(hunger) < component.HungerPerDomainUse)
  72. {
  73. _popup.PopupEntity(Loc.GetString("rat-king-too-hungry"), uid, uid);
  74. return;
  75. }
  76. args.Handled = true;
  77. _hunger.ModifyHunger(uid, -component.HungerPerDomainUse, hunger);
  78. _popup.PopupEntity(Loc.GetString("rat-king-domain-popup"), uid);
  79. var tileMix = _atmos.GetTileMixture(uid, excite: true);
  80. tileMix?.AdjustMoles(Gas.Ammonia, component.MolesAmmoniaPerDomain);
  81. }
  82. private void OnPointedAt(EntityUid uid, RatKingComponent component, ref AfterPointedAtEvent args)
  83. {
  84. if (component.CurrentOrder != RatKingOrderType.CheeseEm)
  85. return;
  86. foreach (var servant in component.Servants)
  87. {
  88. _npc.SetBlackboard(servant, NPCBlackboard.CurrentOrderedTarget, args.Pointed);
  89. }
  90. }
  91. public override void UpdateServantNpc(EntityUid uid, RatKingOrderType orderType)
  92. {
  93. base.UpdateServantNpc(uid, orderType);
  94. if (!TryComp<HTNComponent>(uid, out var htn))
  95. return;
  96. if (htn.Plan != null)
  97. _htn.ShutdownPlan(htn);
  98. _npc.SetBlackboard(uid, NPCBlackboard.CurrentOrders, orderType);
  99. _htn.Replan(htn);
  100. }
  101. public override void DoCommandCallout(EntityUid uid, RatKingComponent component)
  102. {
  103. base.DoCommandCallout(uid, component);
  104. if (!component.OrderCallouts.TryGetValue(component.CurrentOrder, out var datasetId) ||
  105. !PrototypeManager.TryIndex<LocalizedDatasetPrototype>(datasetId, out var datasetPrototype))
  106. return;
  107. var msg = Random.Pick(datasetPrototype);
  108. _chat.TrySendInGameICMessage(uid, msg, InGameICChatType.Speak, true);
  109. }
  110. }
  111. }