CaptureAreaSystem.cs 4.4 KB

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