1
0

RuleGridsSystem.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using Content.Server.Antag;
  2. using Content.Server.GameTicking.Rules.Components;
  3. using Content.Server.Spawners.Components;
  4. using Content.Shared.Whitelist;
  5. using Robust.Server.Physics;
  6. using Robust.Shared.Map;
  7. namespace Content.Server.GameTicking.Rules;
  8. /// <summary>
  9. /// Handles storing grids from <see cref="RuleLoadedGridsEvent"/> and antags spawning on their spawners.
  10. /// </summary>
  11. public sealed class RuleGridsSystem : GameRuleSystem<RuleGridsComponent>
  12. {
  13. [Dependency] private readonly EntityWhitelistSystem _whitelist = default!;
  14. [Dependency] private readonly SharedTransformSystem _transform = default!;
  15. public override void Initialize()
  16. {
  17. base.Initialize();
  18. SubscribeLocalEvent<GridSplitEvent>(OnGridSplit);
  19. SubscribeLocalEvent<RuleGridsComponent, RuleLoadedGridsEvent>(OnLoadedGrids);
  20. SubscribeLocalEvent<RuleGridsComponent, AntagSelectLocationEvent>(OnSelectLocation);
  21. }
  22. private void OnGridSplit(ref GridSplitEvent args)
  23. {
  24. var rule = QueryActiveRules();
  25. while (rule.MoveNext(out _, out var comp, out _))
  26. {
  27. if (!comp.MapGrids.Contains(args.Grid))
  28. continue;
  29. comp.MapGrids.AddRange(args.NewGrids);
  30. break; // only 1 rule can own a grid, not multiple
  31. }
  32. }
  33. private void OnLoadedGrids(Entity<RuleGridsComponent> ent, ref RuleLoadedGridsEvent args)
  34. {
  35. var (uid, comp) = ent;
  36. if (comp.Map != null && args.Map != comp.Map)
  37. {
  38. Log.Warning($"{ToPrettyString(uid):rule} loaded grids on multiple maps {comp.Map} and {args.Map}, the second will be ignored.");
  39. return;
  40. }
  41. comp.Map = args.Map;
  42. comp.MapGrids.AddRange(args.Grids);
  43. }
  44. private void OnSelectLocation(Entity<RuleGridsComponent> ent, ref AntagSelectLocationEvent args)
  45. {
  46. var query = EntityQueryEnumerator<SpawnPointComponent, TransformComponent>();
  47. while (query.MoveNext(out var uid, out _, out var xform))
  48. {
  49. if (xform.MapID != ent.Comp.Map)
  50. continue;
  51. if (xform.GridUid is not {} grid || !ent.Comp.MapGrids.Contains(grid))
  52. continue;
  53. if (_whitelist.IsWhitelistFail(ent.Comp.SpawnerWhitelist, uid))
  54. continue;
  55. args.Coordinates.Add(_transform.GetMapCoordinates(xform));
  56. }
  57. }
  58. }
  59. /// <summary>
  60. /// Raised by another gamerule system to store loaded grids, and have other systems work with it.
  61. /// A single rule can only load grids for a single map, attempts to load more are ignored.
  62. /// </summary>
  63. [ByRefEvent]
  64. public record struct RuleLoadedGridsEvent(MapId Map, IReadOnlyList<EntityUid> Grids);