Skip to content

Commit b702cbd

Browse files
Code Cleanup: Puddles and Spreaders (#39)
# Description Ports space-wizards/space-station-14#26102. Co-authored-by: Tayrtahn <tayrtahn@gmail.com>
1 parent f1b5baa commit b702cbd

6 files changed

Lines changed: 65 additions & 60 deletions

File tree

Content.Server/Fluids/EntitySystems/PuddleDebugDebugOverlaySystem.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ public sealed class PuddleDebugDebugOverlaySystem : SharedPuddleDebugOverlaySyst
1313
[Dependency] private readonly IGameTiming _timing = default!;
1414
[Dependency] private readonly IMapManager _mapManager = default!;
1515
[Dependency] private readonly PuddleSystem _puddle = default!;
16+
[Dependency] private readonly SharedTransformSystem _transform = default!;
17+
[Dependency] private readonly SharedMapSystem _map = default!;
1618

17-
private readonly HashSet<ICommonSession> _playerObservers = new();
18-
private List<Entity<MapGridComponent>> _grids = new();
19+
private readonly HashSet<ICommonSession> _playerObservers = [];
20+
private List<Entity<MapGridComponent>> _grids = [];
1921

2022
public bool ToggleObserver(ICommonSession observer)
2123
{
@@ -55,7 +57,8 @@ public override void Update(float frameTime)
5557

5658
var transform = EntityManager.GetComponent<TransformComponent>(entity);
5759

58-
var worldBounds = Box2.CenteredAround(transform.WorldPosition,
60+
61+
var worldBounds = Box2.CenteredAround(_transform.GetWorldPosition(transform),
5962
new Vector2(LocalViewRange, LocalViewRange));
6063

6164
_grids.Clear();
@@ -69,14 +72,14 @@ public override void Update(float frameTime)
6972
if (!Exists(gridUid))
7073
continue;
7174

72-
foreach (var uid in grid.Comp.GetAnchoredEntities(worldBounds))
75+
foreach (var uid in _map.GetAnchoredEntities(gridUid, grid, worldBounds))
7376
{
7477
PuddleComponent? puddle = null;
7578
TransformComponent? xform = null;
7679
if (!Resolve(uid, ref puddle, ref xform, false))
7780
continue;
7881

79-
var pos = xform.Coordinates.ToVector2i(EntityManager, _mapManager);
82+
var pos = xform.Coordinates.ToVector2i(EntityManager, _mapManager, _transform);
8083
var vol = _puddle.CurrentVolume(uid, puddle);
8184
data.Add(new PuddleDebugOverlayData(pos, vol));
8285
}

Content.Server/Fluids/EntitySystems/PuddleSystem.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public sealed partial class PuddleSystem : SharedPuddleSystem
4141
{
4242
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
4343
[Dependency] private readonly IGameTiming _timing = default!;
44-
[Dependency] private readonly IMapManager _mapManager = default!;
44+
[Dependency] private readonly SharedMapSystem _map = default!;
4545
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
4646
[Dependency] private readonly IRobustRandom _random = default!;
4747
[Dependency] private readonly ITileDefinitionManager _tileDefMan = default!;
@@ -124,7 +124,7 @@ private void OnPuddleSpread(Entity<PuddleComponent> entity, ref SpreadNeighborsE
124124
foreach (var neighbor in args.NeighborFreeTiles)
125125
{
126126
var split = overflow.SplitSolution(spillAmount);
127-
TrySpillAt(neighbor.Grid.GridTileToLocal(neighbor.Tile), split, out _, false);
127+
TrySpillAt(_map.GridTileToLocal(neighbor.Tile.GridUid, neighbor.Grid, neighbor.Tile.GridIndices), split, out _, false);
128128
args.Updates--;
129129

130130
if (args.Updates <= 0)
@@ -612,13 +612,14 @@ public bool TrySpillAt(EntityCoordinates coordinates, Solution solution, out Ent
612612
return false;
613613
}
614614

615-
if (!_mapManager.TryGetGrid(coordinates.GetGridUid(EntityManager), out var mapGrid))
615+
var gridUid = coordinates.GetGridUid(EntityManager);
616+
if (!TryComp<MapGridComponent>(gridUid, out var mapGrid))
616617
{
617618
puddleUid = EntityUid.Invalid;
618619
return false;
619620
}
620621

621-
return TrySpillAt(mapGrid.GetTileRef(coordinates), solution, out puddleUid, sound);
622+
return TrySpillAt(_map.GetTileRef(gridUid.Value, mapGrid, coordinates), solution, out puddleUid, sound);
622623
}
623624

624625
/// <summary>
@@ -657,7 +658,7 @@ public bool TrySpillAt(TileRef tileRef, Solution solution, out EntityUid puddleU
657658

658659
// Let's not spill to invalid grids.
659660
var gridId = tileRef.GridUid;
660-
if (!_mapManager.TryGetGrid(gridId, out var mapGrid))
661+
if (!TryComp<MapGridComponent>(gridId, out var mapGrid))
661662
{
662663
puddleUid = EntityUid.Invalid;
663664
return false;
@@ -678,7 +679,7 @@ public bool TrySpillAt(TileRef tileRef, Solution solution, out EntityUid puddleU
678679

679680
// Get normalized co-ordinate for spill location and spill it in the centre
680681
// TODO: Does SnapGrid or something else already do this?
681-
var anchored = mapGrid.GetAnchoredEntitiesEnumerator(tileRef.GridIndices);
682+
var anchored = _map.GetAnchoredEntitiesEnumerator(gridId, mapGrid, tileRef.GridIndices);
682683
var puddleQuery = GetEntityQuery<PuddleComponent>();
683684
var sparklesQuery = GetEntityQuery<EvaporationSparkleComponent>();
684685

@@ -703,7 +704,7 @@ public bool TrySpillAt(TileRef tileRef, Solution solution, out EntityUid puddleU
703704
return true;
704705
}
705706

706-
var coords = mapGrid.GridTileToLocal(tileRef.GridIndices);
707+
var coords = _map.GridTileToLocal(gridId, mapGrid, tileRef.GridIndices);
707708
puddleUid = EntityManager.SpawnEntity("Puddle", coords);
708709
EnsureComp<PuddleComponent>(puddleUid);
709710
if (TryAddSolution(puddleUid, solution, sound))
@@ -740,7 +741,7 @@ public bool TryGetPuddle(TileRef tile, out EntityUid puddleUid)
740741
if (!TryComp<MapGridComponent>(tile.GridUid, out var grid))
741742
return false;
742743

743-
var anc = grid.GetAnchoredEntitiesEnumerator(tile.GridIndices);
744+
var anc = _map.GetAnchoredEntitiesEnumerator(tile.GridUid, grid, tile.GridIndices);
744745
var puddleQuery = GetEntityQuery<PuddleComponent>();
745746

746747
while (anc.MoveNext(out var ent))

Content.Server/Fluids/EntitySystems/SmokeSystem.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
using Content.Shared.FixedPoint;
1414
using Content.Shared.Smoking;
1515
using Robust.Server.GameObjects;
16-
using Robust.Shared.Map;
16+
using Robust.Shared.Map.Components;
1717
using Robust.Shared.Physics;
1818
using Robust.Shared.Physics.Components;
1919
using Robust.Shared.Physics.Events;
@@ -35,7 +35,7 @@ public sealed class SmokeSystem : EntitySystem
3535
// If I could do it all again this could probably use a lot more of puddles.
3636
[Dependency] private readonly IAdminLogManager _logger = default!;
3737
[Dependency] private readonly IGameTiming _timing = default!;
38-
[Dependency] private readonly IMapManager _mapManager = default!;
38+
[Dependency] private readonly SharedMapSystem _map = default!;
3939
[Dependency] private readonly IPrototypeManager _prototype = default!;
4040
[Dependency] private readonly IRobustRandom _random = default!;
4141
[Dependency] private readonly AppearanceSystem _appearance = default!;
@@ -45,7 +45,6 @@ public sealed class SmokeSystem : EntitySystem
4545
[Dependency] private readonly SharedBroadphaseSystem _broadphase = default!;
4646
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
4747
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
48-
[Dependency] private readonly TransformSystem _transform = default!;
4948

5049
private EntityQuery<SmokeComponent> _smokeQuery;
5150
private EntityQuery<SmokeAffectedComponent> _smokeAffectedQuery;
@@ -137,7 +136,7 @@ private void OnSmokeSpread(Entity<SmokeComponent> entity, ref SpreadNeighborsEve
137136
return;
138137
}
139138

140-
if (!args.NeighborFreeTiles.Any())
139+
if (args.NeighborFreeTiles.Count == 0)
141140
return;
142141

143142
TryComp<TimedDespawnComponent>(entity, out var timer);
@@ -146,10 +145,10 @@ private void OnSmokeSpread(Entity<SmokeComponent> entity, ref SpreadNeighborsEve
146145
var smokePerSpread = entity.Comp.SpreadAmount / Math.Max(1, args.NeighborFreeTiles.Count);
147146
foreach (var neighbor in args.NeighborFreeTiles)
148147
{
149-
var coords = neighbor.Grid.GridTileToLocal(neighbor.Tile);
148+
var coords = _map.GridTileToLocal(neighbor.Tile.GridUid, neighbor.Grid, neighbor.Tile.GridIndices);
150149
var ent = Spawn(prototype.ID, coords);
151150
var spreadAmount = Math.Max(0, smokePerSpread);
152-
entity.Comp.SpreadAmount -= args.NeighborFreeTiles.Count();
151+
entity.Comp.SpreadAmount -= args.NeighborFreeTiles.Count;
153152

154153
StartSmoke(ent, solution.Clone(), timer?.Lifetime ?? entity.Comp.Duration, spreadAmount);
155154

@@ -305,10 +304,10 @@ private void ReactOnTile(EntityUid uid, SmokeComponent? component = null, Transf
305304
if (!_solutionContainerSystem.ResolveSolution(uid, SmokeComponent.SolutionName, ref component.Solution, out var solution) || !solution.Any())
306305
return;
307306

308-
if (!_mapManager.TryGetGrid(xform.GridUid, out var mapGrid))
307+
if (!TryComp<MapGridComponent>(xform.GridUid, out var mapGrid))
309308
return;
310309

311-
var tile = mapGrid.GetTileRef(xform.Coordinates.ToVector2i(EntityManager, _mapManager, _transform));
310+
var tile = _map.GetTileRef(xform.GridUid.Value, mapGrid, xform.Coordinates);
312311

313312
foreach (var reagentQuantity in solution.Contents.ToArray())
314313
{

Content.Server/Spreader/KudzuSystem.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public sealed class KudzuSystem : EntitySystem
1010
{
1111
[Dependency] private readonly IGameTiming _timing = default!;
1212
[Dependency] private readonly IRobustRandom _robustRandom = default!;
13+
[Dependency] private readonly SharedMapSystem _map = default!;
1314
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
1415
[Dependency] private readonly DamageableSystem _damageable = default!;
1516

@@ -28,15 +29,12 @@ private void OnDamageChanged(EntityUid uid, KudzuComponent component, DamageChan
2829
{
2930
// Every time we take any damage, we reduce growth depending on all damage over the growth impact
3031
// So the kudzu gets slower growing the more it is hurt.
31-
int growthDamage = (int) (args.Damageable.TotalDamage / component.GrowthHealth);
32+
var growthDamage = (int) (args.Damageable.TotalDamage / component.GrowthHealth);
3233
if (growthDamage > 0)
3334
{
34-
GrowingKudzuComponent? growing;
35-
if (!TryComp(uid, out growing))
36-
{
37-
growing = AddComp<GrowingKudzuComponent>(uid);
35+
if (!EnsureComp<GrowingKudzuComponent>(uid, out _))
3836
component.GrowthLevel = 3;
39-
}
37+
4038
component.GrowthLevel = Math.Max(1, component.GrowthLevel - growthDamage);
4139
if (EntityManager.TryGetComponent<AppearanceComponent>(uid, out var appearance))
4240
{
@@ -69,7 +67,7 @@ private void OnKudzuSpread(EntityUid uid, KudzuComponent component, ref SpreadNe
6967

7068
foreach (var neighbor in args.NeighborFreeTiles)
7169
{
72-
var neighborUid = Spawn(prototype, neighbor.Grid.GridTileToLocal(neighbor.Tile));
70+
var neighborUid = Spawn(prototype, _map.GridTileToLocal(neighbor.Tile.GridUid, neighbor.Grid, neighbor.Tile.GridIndices));
7371
DebugTools.Assert(HasComp<EdgeSpreaderComponent>(neighborUid));
7472
DebugTools.Assert(HasComp<ActiveEdgeSpreaderComponent>(neighborUid));
7573
DebugTools.Assert(Comp<EdgeSpreaderComponent>(neighborUid).Id == KudzuGroup);

Content.Server/Spreader/SpreadNeighborsEvent.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Robust.Shared.Collections;
2+
using Robust.Shared.Map;
23
using Robust.Shared.Map.Components;
34

45
namespace Content.Server.Spreader;
@@ -10,7 +11,7 @@ namespace Content.Server.Spreader;
1011
[ByRefEvent]
1112
public record struct SpreadNeighborsEvent
1213
{
13-
public ValueList<(MapGridComponent Grid, Vector2i Tile)> NeighborFreeTiles;
14+
public ValueList<(MapGridComponent Grid, TileRef Tile)> NeighborFreeTiles;
1415
public ValueList<EntityUid> Neighbors;
1516

1617
/// <summary>

0 commit comments

Comments
 (0)