1
0

CaptureAreaSystem.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using Content.Server.GameTicking.Rules.Components;
  2. using Content.Shared.NPC.Components;
  3. using Content.Shared.Physics;
  4. using Robust.Shared.Timing;
  5. using Content.Server.Chat.Systems;
  6. using Content.Server.RoundEnd;
  7. using Content.Shared.Mobs.Components;
  8. using Content.Shared.Mobs;
  9. namespace Content.Server.GameTicking.Rules;
  10. public sealed class CaptureAreaSystem : GameRuleSystem<CaptureAreaRuleComponent>
  11. {
  12. [Dependency] private readonly SharedTransformSystem _transform = default!;
  13. [Dependency] private readonly IEntityManager _entityManager = default!;
  14. [Dependency] private readonly EntityLookupSystem _lookup = default!;
  15. [Dependency] private readonly IGameTiming _timing = default!;
  16. [Dependency] private readonly ChatSystem _chat = default!;
  17. [Dependency] private readonly RoundEndSystem _roundEndSystem = default!;
  18. [Dependency] private readonly GameTicker _gameTicker = default!;
  19. public override void Initialize()
  20. {
  21. base.Initialize();
  22. }
  23. public override void Update(float frameTime)
  24. {
  25. base.Update(frameTime);
  26. var query = EntityQueryEnumerator<CaptureAreaComponent>();
  27. while (query.MoveNext(out var uid, out var area))
  28. {
  29. ProcessArea(uid, area, frameTime);
  30. }
  31. }
  32. private void ProcessArea(EntityUid uid, CaptureAreaComponent area, float frameTime)
  33. {
  34. var areaXform = _transform.GetMapCoordinates(uid);
  35. var factionCounts = new Dictionary<string, int>();
  36. // Initialize counts for all capturable factions to 0
  37. foreach (var faction in area.CapturableFactions)
  38. {
  39. factionCounts[faction] = 0;
  40. }
  41. // Find entities in range and count factions
  42. var entitiesInRange = _lookup.GetEntitiesInRange(areaXform, area.CaptureRadius, LookupFlags.Dynamic | LookupFlags.Sundries); // Include dynamic entities and items/mobs etc.
  43. foreach (var entity in entitiesInRange)
  44. {
  45. if (EntityManager.TryGetComponent<MobStateComponent>(entity, out var mobState))
  46. {
  47. //do not count dead and crit mobs
  48. if (mobState.CurrentState == MobState.Alive)
  49. // Check if the entity has a faction and if it's one we care about
  50. if (_entityManager.TryGetComponent<NpcFactionMemberComponent>(entity, out var factionMember))
  51. {
  52. foreach (var faction in factionMember.Factions)
  53. {
  54. if (area.CapturableFactions.Contains(faction))
  55. factionCounts[faction]++;
  56. }
  57. }
  58. }
  59. }
  60. // Determine the controlling faction
  61. string currentController = "";
  62. int controllerCount = 0;
  63. foreach (var (faction, count) in factionCounts)
  64. {
  65. if (count > 0)
  66. {
  67. currentController = faction;
  68. controllerCount++;
  69. }
  70. }
  71. // If more than one faction is present, it's contested
  72. if (controllerCount > 1)
  73. currentController = ""; // Reset controller if contested
  74. // Update component state
  75. area.Occupied = controllerCount > 0;
  76. if (currentController != area.Controller)
  77. {
  78. // Controller changed (or became contested/empty)
  79. area.Controller = currentController;
  80. area.CaptureTimer = 0f; // Reset timer on change
  81. if (currentController == "")
  82. {
  83. _chat.DispatchGlobalAnnouncement($"{area.PreviousController} has lost control of {area.Name}!", "Objective", false, null, Color.Red);
  84. }
  85. else
  86. {
  87. _chat.DispatchGlobalAnnouncement($"{currentController} has gained control of {area.Name}!", "Objective", false, null, Color.DodgerBlue);
  88. }
  89. }
  90. else if (!string.IsNullOrEmpty(currentController))
  91. {
  92. // Controller remains the same, increment timer
  93. area.CaptureTimer += frameTime;
  94. //Check for capture completion
  95. if (area.CaptureTimer >= area.CaptureDuration)
  96. {
  97. if (_gameTicker.RunLevel == GameRunLevel.InRound)
  98. {
  99. _chat.DispatchGlobalAnnouncement($"{currentController} has captured {area.Name} and is victorious!", "Round", false, null, Color.Green);
  100. _roundEndSystem.EndRound();
  101. }
  102. }
  103. }
  104. else
  105. {
  106. // Area is empty or contested, and wasn't previously controlled by a single faction
  107. area.CaptureTimer = 0f; // Ensure timer is reset/stays reset
  108. }
  109. area.PreviousController = currentController;
  110. }
  111. }