Skip to content

Commit

Permalink
Code Cleanup: Puddles and Spreaders (#39)
Browse files Browse the repository at this point in the history
# Description

Ports space-wizards/space-station-14#26102.

Co-authored-by: Tayrtahn <[email protected]>
  • Loading branch information
stellar-novas and Tayrtahn authored Mar 27, 2024
1 parent f1b5baa commit b702cbd
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -55,7 +57,8 @@ public override void Update(float frameTime)

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();
Expand All @@ -69,14 +72,14 @@ public override void Update(float frameTime)
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));
}
Expand Down
17 changes: 9 additions & 8 deletions Content.Server/Fluids/EntitySystems/PuddleSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
Expand Down Expand Up @@ -124,7 +124,7 @@ private void OnPuddleSpread(Entity<PuddleComponent> entity, ref SpreadNeighborsE
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)
Expand Down Expand Up @@ -612,13 +612,14 @@ public bool TrySpillAt(EntityCoordinates coordinates, Solution solution, out Ent
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>
Expand Down Expand Up @@ -657,7 +658,7 @@ public bool TrySpillAt(TileRef tileRef, Solution solution, out EntityUid puddleU

// 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;
Expand All @@ -678,7 +679,7 @@ public bool TrySpillAt(TileRef tileRef, Solution solution, out EntityUid puddleU

// 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>();

Expand All @@ -703,7 +704,7 @@ public bool TrySpillAt(TileRef tileRef, Solution solution, out EntityUid puddleU
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))
Expand Down Expand Up @@ -740,7 +741,7 @@ public bool TryGetPuddle(TileRef tile, out EntityUid puddleUid)
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))
Expand Down
15 changes: 7 additions & 8 deletions Content.Server/Fluids/EntitySystems/SmokeSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
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;
Expand All @@ -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!;
Expand All @@ -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;
Expand Down Expand Up @@ -137,7 +136,7 @@ private void OnSmokeSpread(Entity<SmokeComponent> entity, ref SpreadNeighborsEve
return;
}

if (!args.NeighborFreeTiles.Any())
if (args.NeighborFreeTiles.Count == 0)
return;

TryComp<TimedDespawnComponent>(entity, out var timer);
Expand All @@ -146,10 +145,10 @@ private void OnSmokeSpread(Entity<SmokeComponent> entity, ref SpreadNeighborsEve
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);

Expand Down Expand Up @@ -305,10 +304,10 @@ private void ReactOnTile(EntityUid uid, SmokeComponent? component = null, Transf
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())
{
Expand Down
12 changes: 5 additions & 7 deletions Content.Server/Spreader/KudzuSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!;

Expand All @@ -28,15 +29,12 @@ private void OnDamageChanged(EntityUid uid, KudzuComponent component, DamageChan
{
// 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))
{
Expand Down Expand Up @@ -69,7 +67,7 @@ private void OnKudzuSpread(EntityUid uid, KudzuComponent component, ref SpreadNe

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);
Expand Down
3 changes: 2 additions & 1 deletion Content.Server/Spreader/SpreadNeighborsEvent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Robust.Shared.Collections;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;

namespace Content.Server.Spreader;
Expand All @@ -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>
Expand Down
Loading

0 comments on commit b702cbd

Please sign in to comment.