]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Code cleanup: puddles and spreaders (#26102)
authorTayrtahn <tayrtahn@gmail.com>
Fri, 15 Mar 2024 03:41:08 +0000 (23:41 -0400)
committerGitHub <noreply@github.com>
Fri, 15 Mar 2024 03:41:08 +0000 (14:41 +1100)
* Removed obsolete MapManager calls

* More code cleanup

* EnsureComp

* Rerun tests

Content.Server/Fluids/EntitySystems/PuddleDebugDebugOverlaySystem.cs
Content.Server/Fluids/EntitySystems/PuddleSystem.cs
Content.Server/Fluids/EntitySystems/SmokeSystem.cs
Content.Server/Spreader/KudzuSystem.cs
Content.Server/Spreader/SpreadNeighborsEvent.cs
Content.Server/Spreader/SpreaderSystem.cs

index ac967b3c201b9d510c3aad8f13fa004e1b5d4947..859f2d80a671e8ef4962a6ce4eb5f6a95f82e4d6 100644 (file)
@@ -13,9 +13,11 @@ public sealed class PuddleDebugDebugOverlaySystem : SharedPuddleDebugOverlaySyst
     [Dependency] private readonly IGameTiming _timing = default!;
     [Dependency] private readonly IMapManager _mapManager = default!;
     [Dependency] private readonly PuddleSystem _puddle = default!;
+    [Dependency] private readonly SharedTransformSystem _transform = default!;
+    [Dependency] private readonly SharedMapSystem _map = default!;
 
-    private readonly HashSet<ICommonSession> _playerObservers = new();
-    private List<Entity<MapGridComponent>> _grids = new();
+    private readonly HashSet<ICommonSession> _playerObservers = [];
+    private List<Entity<MapGridComponent>> _grids = [];
 
     public bool ToggleObserver(ICommonSession observer)
     {
@@ -55,7 +57,8 @@ public sealed class PuddleDebugDebugOverlaySystem : SharedPuddleDebugOverlaySyst
 
             var transform = EntityManager.GetComponent<TransformComponent>(entity);
 
-            var worldBounds = Box2.CenteredAround(transform.WorldPosition,
+
+            var worldBounds = Box2.CenteredAround(_transform.GetWorldPosition(transform),
                 new Vector2(LocalViewRange, LocalViewRange));
 
             _grids.Clear();
@@ -69,14 +72,14 @@ public sealed class PuddleDebugDebugOverlaySystem : SharedPuddleDebugOverlaySyst
                 if (!Exists(gridUid))
                     continue;
 
-                foreach (var uid in grid.Comp.GetAnchoredEntities(worldBounds))
+                foreach (var uid in _map.GetAnchoredEntities(gridUid, grid, worldBounds))
                 {
                     PuddleComponent? puddle = null;
                     TransformComponent? xform = null;
                     if (!Resolve(uid, ref puddle, ref xform, false))
                         continue;
 
-                    var pos = xform.Coordinates.ToVector2i(EntityManager, _mapManager);
+                    var pos = xform.Coordinates.ToVector2i(EntityManager, _mapManager, _transform);
                     var vol = _puddle.CurrentVolume(uid, puddle);
                     data.Add(new PuddleDebugOverlayData(pos, vol));
                 }
index c0e8847ae98d710e4a5c62abd0c45543ddab8d04..c03e6f593c6d54114460183c8690c3b2549e2acb 100644 (file)
@@ -41,7 +41,7 @@ public sealed partial class PuddleSystem : SharedPuddleSystem
 {
     [Dependency] private readonly IAdminLogManager _adminLogger = default!;
     [Dependency] private readonly IGameTiming _timing = default!;
-    [Dependency] private readonly IMapManager _mapManager = default!;
+    [Dependency] private readonly SharedMapSystem _map = default!;
     [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
     [Dependency] private readonly IRobustRandom _random = default!;
     [Dependency] private readonly ITileDefinitionManager _tileDefMan = default!;
@@ -124,7 +124,7 @@ public sealed partial class PuddleSystem : SharedPuddleSystem
             foreach (var neighbor in args.NeighborFreeTiles)
             {
                 var split = overflow.SplitSolution(spillAmount);
-                TrySpillAt(neighbor.Grid.GridTileToLocal(neighbor.Tile), split, out _, false);
+                TrySpillAt(_map.GridTileToLocal(neighbor.Tile.GridUid, neighbor.Grid, neighbor.Tile.GridIndices), split, out _, false);
                 args.Updates--;
 
                 if (args.Updates <= 0)
@@ -612,13 +612,14 @@ public sealed partial class PuddleSystem : SharedPuddleSystem
             return false;
         }
 
-        if (!_mapManager.TryGetGrid(coordinates.GetGridUid(EntityManager), out var mapGrid))
+        var gridUid = coordinates.GetGridUid(EntityManager);
+        if (!TryComp<MapGridComponent>(gridUid, out var mapGrid))
         {
             puddleUid = EntityUid.Invalid;
             return false;
         }
 
-        return TrySpillAt(mapGrid.GetTileRef(coordinates), solution, out puddleUid, sound);
+        return TrySpillAt(_map.GetTileRef(gridUid.Value, mapGrid, coordinates), solution, out puddleUid, sound);
     }
 
     /// <summary>
@@ -657,7 +658,7 @@ public sealed partial class PuddleSystem : SharedPuddleSystem
 
         // Let's not spill to invalid grids.
         var gridId = tileRef.GridUid;
-        if (!_mapManager.TryGetGrid(gridId, out var mapGrid))
+        if (!TryComp<MapGridComponent>(gridId, out var mapGrid))
         {
             puddleUid = EntityUid.Invalid;
             return false;
@@ -678,7 +679,7 @@ public sealed partial class PuddleSystem : SharedPuddleSystem
 
         // Get normalized co-ordinate for spill location and spill it in the centre
         // TODO: Does SnapGrid or something else already do this?
-        var anchored = mapGrid.GetAnchoredEntitiesEnumerator(tileRef.GridIndices);
+        var anchored = _map.GetAnchoredEntitiesEnumerator(gridId, mapGrid, tileRef.GridIndices);
         var puddleQuery = GetEntityQuery<PuddleComponent>();
         var sparklesQuery = GetEntityQuery<EvaporationSparkleComponent>();
 
@@ -703,7 +704,7 @@ public sealed partial class PuddleSystem : SharedPuddleSystem
             return true;
         }
 
-        var coords = mapGrid.GridTileToLocal(tileRef.GridIndices);
+        var coords = _map.GridTileToLocal(gridId, mapGrid, tileRef.GridIndices);
         puddleUid = EntityManager.SpawnEntity("Puddle", coords);
         EnsureComp<PuddleComponent>(puddleUid);
         if (TryAddSolution(puddleUid, solution, sound))
@@ -740,7 +741,7 @@ public sealed partial class PuddleSystem : SharedPuddleSystem
         if (!TryComp<MapGridComponent>(tile.GridUid, out var grid))
             return false;
 
-        var anc = grid.GetAnchoredEntitiesEnumerator(tile.GridIndices);
+        var anc = _map.GetAnchoredEntitiesEnumerator(tile.GridUid, grid, tile.GridIndices);
         var puddleQuery = GetEntityQuery<PuddleComponent>();
 
         while (anc.MoveNext(out var ent))
index cb6f9c13ef16cef6945c188337049d06a09d6ef5..ae170842a0c45a8f349f94ef7de5e86923d99a3f 100644 (file)
@@ -13,7 +13,7 @@ using Content.Shared.Database;
 using Content.Shared.FixedPoint;
 using Content.Shared.Smoking;
 using Robust.Server.GameObjects;
-using Robust.Shared.Map;
+using Robust.Shared.Map.Components;
 using Robust.Shared.Physics;
 using Robust.Shared.Physics.Components;
 using Robust.Shared.Physics.Events;
@@ -35,7 +35,7 @@ public sealed class SmokeSystem : EntitySystem
     // If I could do it all again this could probably use a lot more of puddles.
     [Dependency] private readonly IAdminLogManager _logger = default!;
     [Dependency] private readonly IGameTiming _timing = default!;
-    [Dependency] private readonly IMapManager _mapManager = default!;
+    [Dependency] private readonly SharedMapSystem _map = default!;
     [Dependency] private readonly IPrototypeManager _prototype = default!;
     [Dependency] private readonly IRobustRandom _random = default!;
     [Dependency] private readonly AppearanceSystem _appearance = default!;
@@ -45,7 +45,6 @@ public sealed class SmokeSystem : EntitySystem
     [Dependency] private readonly SharedBroadphaseSystem _broadphase = default!;
     [Dependency] private readonly SharedPhysicsSystem _physics = default!;
     [Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
-    [Dependency] private readonly TransformSystem _transform = default!;
 
     private EntityQuery<SmokeComponent> _smokeQuery;
     private EntityQuery<SmokeAffectedComponent> _smokeAffectedQuery;
@@ -137,7 +136,7 @@ public sealed class SmokeSystem : EntitySystem
             return;
         }
 
-        if (!args.NeighborFreeTiles.Any())
+        if (args.NeighborFreeTiles.Count == 0)
             return;
 
         TryComp<TimedDespawnComponent>(entity, out var timer);
@@ -146,10 +145,10 @@ public sealed class SmokeSystem : EntitySystem
         var smokePerSpread = entity.Comp.SpreadAmount / Math.Max(1, args.NeighborFreeTiles.Count);
         foreach (var neighbor in args.NeighborFreeTiles)
         {
-            var coords = neighbor.Grid.GridTileToLocal(neighbor.Tile);
+            var coords = _map.GridTileToLocal(neighbor.Tile.GridUid, neighbor.Grid, neighbor.Tile.GridIndices);
             var ent = Spawn(prototype.ID, coords);
             var spreadAmount = Math.Max(0, smokePerSpread);
-            entity.Comp.SpreadAmount -= args.NeighborFreeTiles.Count();
+            entity.Comp.SpreadAmount -= args.NeighborFreeTiles.Count;
 
             StartSmoke(ent, solution.Clone(), timer?.Lifetime ?? entity.Comp.Duration, spreadAmount);
 
@@ -305,10 +304,10 @@ public sealed class SmokeSystem : EntitySystem
         if (!_solutionContainerSystem.ResolveSolution(uid, SmokeComponent.SolutionName, ref component.Solution, out var solution) || !solution.Any())
             return;
 
-        if (!_mapManager.TryGetGrid(xform.GridUid, out var mapGrid))
+        if (!TryComp<MapGridComponent>(xform.GridUid, out var mapGrid))
             return;
 
-        var tile = mapGrid.GetTileRef(xform.Coordinates.ToVector2i(EntityManager, _mapManager, _transform));
+        var tile = _map.GetTileRef(xform.GridUid.Value, mapGrid, xform.Coordinates);
 
         foreach (var reagentQuantity in solution.Contents.ToArray())
         {
index 49698ea6735db1becb707a032e9b3383baa07f81..dc176ebc2a6501a24f6717376a30e3e7c4efb4ac 100644 (file)
@@ -10,6 +10,7 @@ public sealed class KudzuSystem : EntitySystem
 {
     [Dependency] private readonly IGameTiming _timing = default!;
     [Dependency] private readonly IRobustRandom _robustRandom = default!;
+    [Dependency] private readonly SharedMapSystem _map = default!;
     [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
     [Dependency] private readonly DamageableSystem _damageable = default!;
 
@@ -28,15 +29,12 @@ public sealed class KudzuSystem : EntitySystem
     {
         // Every time we take any damage, we reduce growth depending on all damage over the growth impact
         //   So the kudzu gets slower growing the more it is hurt.
-        int growthDamage = (int) (args.Damageable.TotalDamage / component.GrowthHealth);
+        var growthDamage = (int) (args.Damageable.TotalDamage / component.GrowthHealth);
         if (growthDamage > 0)
         {
-            GrowingKudzuComponent? growing;
-            if (!TryComp(uid, out growing))
-            {
-                growing = AddComp<GrowingKudzuComponent>(uid);
+            if (!EnsureComp<GrowingKudzuComponent>(uid, out _))
                 component.GrowthLevel = 3;
-            }
+
             component.GrowthLevel = Math.Max(1, component.GrowthLevel - growthDamage);
             if (EntityManager.TryGetComponent<AppearanceComponent>(uid, out var appearance))
             {
@@ -69,7 +67,7 @@ public sealed class KudzuSystem : EntitySystem
 
         foreach (var neighbor in args.NeighborFreeTiles)
         {
-            var neighborUid = Spawn(prototype, neighbor.Grid.GridTileToLocal(neighbor.Tile));
+            var neighborUid = Spawn(prototype, _map.GridTileToLocal(neighbor.Tile.GridUid, neighbor.Grid, neighbor.Tile.GridIndices));
             DebugTools.Assert(HasComp<EdgeSpreaderComponent>(neighborUid));
             DebugTools.Assert(HasComp<ActiveEdgeSpreaderComponent>(neighborUid));
             DebugTools.Assert(Comp<EdgeSpreaderComponent>(neighborUid).Id == KudzuGroup);
index 0cee55b5880ac3eac9d9883ede4e61a76a07c34a..022ef49f33afa79ec6387d3364395bb99d3051cd 100644 (file)
@@ -1,4 +1,5 @@
 using Robust.Shared.Collections;
+using Robust.Shared.Map;
 using Robust.Shared.Map.Components;
 
 namespace Content.Server.Spreader;
@@ -10,7 +11,7 @@ namespace Content.Server.Spreader;
 [ByRefEvent]
 public record struct SpreadNeighborsEvent
 {
-    public ValueList<(MapGridComponent Grid, Vector2i Tile)> NeighborFreeTiles;
+    public ValueList<(MapGridComponent Grid, TileRef Tile)> NeighborFreeTiles;
     public ValueList<EntityUid> Neighbors;
 
     /// <summary>
index ad3033a14bbd83435f2ffb26a937f03a5c7c0aef..269d35d12ffc531da6d0ada0f2ca7c37d13efef4 100644 (file)
@@ -18,9 +18,9 @@ namespace Content.Server.Spreader;
 /// </summary>
 public sealed class SpreaderSystem : EntitySystem
 {
-    [Dependency] private readonly IMapManager _mapManager = default!;
     [Dependency] private readonly IPrototypeManager _prototype = default!;
     [Dependency] private readonly IRobustRandom _robustRandom = default!;
+    [Dependency] private readonly SharedMapSystem _map = default!;
 
     /// <summary>
     /// Cached maximum number of updates per spreader prototype. This is applied per-grid.
@@ -31,7 +31,7 @@ public sealed class SpreaderSystem : EntitySystem
     /// Remaining number of updates per grid & prototype.
     /// </summary>
     // TODO PERFORMANCE Assign each prototype to an index and convert dictionary to array
-    private Dictionary<EntityUid, Dictionary<string, int>> _gridUpdates = new();
+    private readonly Dictionary<EntityUid, Dictionary<string, int>> _gridUpdates = [];
 
     public const float SpreadCooldownSeconds = 1;
 
@@ -57,7 +57,7 @@ public sealed class SpreaderSystem : EntitySystem
 
     private void SetupPrototypes()
     {
-        _prototypeUpdates = new Dictionary<string, int>();
+        _prototypeUpdates = [];
         foreach (var proto in _prototype.EnumeratePrototypes<EdgeSpreaderPrototype>())
         {
             _prototypeUpdates.Add(proto.ID, proto.UpdatesPerSecond);
@@ -80,9 +80,9 @@ public sealed class SpreaderSystem : EntitySystem
         EnsureComp<SpreaderGridComponent>(ev.EntityUid);
     }
 
-    private void OnTerminating(EntityUid uid, EdgeSpreaderComponent component, ref EntityTerminatingEvent args)
+    private void OnTerminating(Entity<EdgeSpreaderComponent> entity, ref EntityTerminatingEvent args)
     {
-        var neighbors = GetSpreadableNeighbors(uid);
+        var neighbors = GetSpreadableNeighbors(entity);
 
         foreach (var neighbor in neighbors)
         {
@@ -163,7 +163,7 @@ public sealed class SpreaderSystem : EntitySystem
         }
     }
 
-    private void Spread(EntityUid uid, TransformComponent xform, string prototype, ref int updates)
+    private void Spread(EntityUid uid, TransformComponent xform, ProtoId<EdgeSpreaderPrototype> prototype, ref int updates)
     {
         GetNeighbors(uid, xform, prototype, out var freeTiles, out _, out var neighbors);
 
@@ -181,18 +181,18 @@ public sealed class SpreaderSystem : EntitySystem
     /// <summary>
     /// Gets the neighboring node data for the specified entity and the specified node group.
     /// </summary>
-    public void GetNeighbors(EntityUid uid, TransformComponent transform, string prototype, out ValueList<(MapGridComponent Grid, Vector2i Tile)> freeTiles, out ValueList<Vector2i> occupiedTiles, out ValueList<EntityUid> neighbors)
+    public void GetNeighbors(EntityUid uid, TransformComponent comp, ProtoId<EdgeSpreaderPrototype> prototype, out ValueList<(MapGridComponent, TileRef)> freeTiles, out ValueList<Vector2i> occupiedTiles, out ValueList<EntityUid> neighbors)
     {
         // TODO remove occupiedTiles -- its currently unused and just slows this method down.
-        DebugTools.Assert(_prototype.HasIndex<EdgeSpreaderPrototype>(prototype));
-        freeTiles = new ValueList<(MapGridComponent Grid, Vector2i Tile)>();
-        occupiedTiles = new ValueList<Vector2i>();
-        neighbors = new ValueList<EntityUid>();
+        DebugTools.Assert(_prototype.HasIndex(prototype));
+        freeTiles = [];
+        occupiedTiles = [];
+        neighbors = [];
 
-        if (!_mapManager.TryGetGrid(transform.GridUid, out var grid))
+        if (!TryComp<MapGridComponent>(comp.GridUid, out var grid))
             return;
 
-        var tile = grid.TileIndicesFor(transform.Coordinates);
+        var tile = _map.TileIndicesFor(comp.GridUid.Value, grid, comp.Coordinates);
         var spreaderQuery = GetEntityQuery<EdgeSpreaderComponent>();
         var airtightQuery = GetEntityQuery<AirtightComponent>();
         var dockQuery = GetEntityQuery<DockingComponent>();
@@ -201,10 +201,10 @@ public sealed class SpreaderSystem : EntitySystem
         var blockedAtmosDirs = AtmosDirection.Invalid;
 
         // Due to docking ports they may not necessarily be opposite directions.
-        var neighborTiles = new ValueList<(MapGridComponent grid, Vector2i Indices, AtmosDirection OtherDir, AtmosDirection OurDir)>();
+        var neighborTiles = new ValueList<(EntityUid entity, MapGridComponent grid, Vector2i Indices, AtmosDirection OtherDir, AtmosDirection OurDir)>();
 
         // Check if anything on our own tile blocking that direction.
-        var ourEnts = grid.GetAnchoredEntitiesEnumerator(tile);
+        var ourEnts = _map.GetAnchoredEntitiesEnumerator(comp.GridUid.Value, grid, tile);
 
         while (ourEnts.MoveNext(out var ent))
         {
@@ -215,7 +215,7 @@ public sealed class SpreaderSystem : EntitySystem
                 xformQuery.TryGetComponent(dock.DockedWith, out var dockedXform) &&
                 TryComp<MapGridComponent>(dockedXform.GridUid, out var dockedGrid))
             {
-                neighborTiles.Add((dockedGrid, dockedGrid.CoordinatesToTile(dockedXform.Coordinates), xform.LocalRotation.ToAtmosDirection(), dockedXform.LocalRotation.ToAtmosDirection()));
+                neighborTiles.Add((ent.Value, dockedGrid, _map.CoordinatesToTile(ent.Value, dockedGrid, dockedXform.Coordinates), xform.LocalRotation.ToAtmosDirection(), dockedXform.LocalRotation.ToAtmosDirection()));
             }
 
             // If we're on a blocked tile work out which directions we can go.
@@ -225,7 +225,7 @@ public sealed class SpreaderSystem : EntitySystem
                 continue;
             }
 
-            foreach (var value in new[] { AtmosDirection.North, AtmosDirection.East, AtmosDirection.South, AtmosDirection.West})
+            foreach (var value in new[] { AtmosDirection.North, AtmosDirection.East, AtmosDirection.South, AtmosDirection.West })
             {
                 if ((value & airtight.AirBlockedDirection) == 0x0)
                     continue;
@@ -242,20 +242,20 @@ public sealed class SpreaderSystem : EntitySystem
             var direction = (Direction) (i * 2);
             var atmosDir = direction.ToAtmosDirection();
             var neighborPos = SharedMapSystem.GetDirection(tile, direction);
-            neighborTiles.Add((grid, neighborPos, atmosDir, atmosDir.GetOpposite()));
+            neighborTiles.Add((comp.GridUid.Value, grid, neighborPos, atmosDir, atmosDir.GetOpposite()));
         }
 
-        foreach (var (neighborGrid, neighborPos, ourAtmosDir, otherAtmosDir) in neighborTiles)
+        foreach (var (neighborEnt, neighborGrid, neighborPos, ourAtmosDir, otherAtmosDir) in neighborTiles)
         {
             // This tile is blocked to that direction.
             if ((blockedAtmosDirs & ourAtmosDir) != 0x0)
                 continue;
 
-            if (!neighborGrid.TryGetTileRef(neighborPos, out var tileRef) || tileRef.Tile.IsEmpty)
+            if (!_map.TryGetTileRef(neighborEnt, neighborGrid, neighborPos, out var tileRef) || tileRef.Tile.IsEmpty)
                 continue;
 
             var directionEnumerator =
-                neighborGrid.GetAnchoredEntitiesEnumerator(neighborPos);
+                _map.GetAnchoredEntitiesEnumerator(neighborEnt, neighborGrid, neighborPos);
             var occupied = false;
 
             while (directionEnumerator.MoveNext(out var ent))
@@ -278,7 +278,7 @@ public sealed class SpreaderSystem : EntitySystem
 
             var oldCount = occupiedTiles.Count;
             directionEnumerator =
-                neighborGrid.GetAnchoredEntitiesEnumerator(neighborPos);
+                _map.GetAnchoredEntitiesEnumerator(neighborEnt, neighborGrid, neighborPos);
 
             while (directionEnumerator.MoveNext(out var ent))
             {
@@ -294,7 +294,7 @@ public sealed class SpreaderSystem : EntitySystem
             }
 
             if (oldCount == occupiedTiles.Count)
-                freeTiles.Add((neighborGrid, neighborPos));
+                freeTiles.Add((neighborGrid, tileRef));
         }
     }
 
@@ -308,20 +308,23 @@ public sealed class SpreaderSystem : EntitySystem
         var neighbors = new List<EntityUid>();
 
         Vector2i tile;
+        EntityUid ent;
         MapGridComponent? grid;
 
         if (position == null)
         {
             var transform = Transform(uid);
-            if (!_mapManager.TryGetGrid(transform.GridUid, out grid) || TerminatingOrDeleted(transform.GridUid.Value))
+            if (!TryComp(transform.GridUid, out grid) || TerminatingOrDeleted(transform.GridUid.Value))
                 return neighbors;
-            tile = grid.TileIndicesFor(transform.Coordinates);
+            tile = _map.TileIndicesFor(transform.GridUid.Value, grid, transform.Coordinates);
+            ent = transform.GridUid.Value;
         }
         else
         {
-            if (!_mapManager.TryGetGrid(position.Value.Grid, out grid))
+            if (!TryComp(position.Value.Grid, out grid))
                 return neighbors;
             tile = position.Value.Tile;
+            ent = position.Value.Grid;
         }
 
         var spreaderQuery = GetEntityQuery<EdgeSpreaderComponent>();
@@ -333,13 +336,13 @@ public sealed class SpreaderSystem : EntitySystem
                 continue;
 
             var directionEnumerator =
-                grid.GetAnchoredEntitiesEnumerator(SharedMapSystem.GetDirection(tile, direction.ToDirection()));
+                _map.GetAnchoredEntitiesEnumerator(ent, grid, SharedMapSystem.GetDirection(tile, direction.ToDirection()));
 
-            while (directionEnumerator.MoveNext(out var ent))
+            while (directionEnumerator.MoveNext(out var entity))
             {
-                DebugTools.Assert(Transform(ent.Value).Anchored);
-                if (spreaderQuery.HasComponent(ent) && !TerminatingOrDeleted(ent.Value))
-                    neighbors.Add(ent.Value);
+                DebugTools.Assert(Transform(entity.Value).Anchored);
+                if (spreaderQuery.HasComponent(entity) && !TerminatingOrDeleted(entity.Value))
+                    neighbors.Add(entity.Value);
             }
         }