Atmos device performance improvements (#26493)

* Atmos device performance improvements

* AtmosDirection perf improvements

* Fix errors

* Add GasTileOverlayComponent arguments

* Make excite no longer invalidate a tile
This commit is contained in:
Leon Friedrich
2024-03-30 17:17:53 +13:00
committed by GitHub
parent aa96baeb5f
commit 888a3bda51
51 changed files with 373 additions and 324 deletions

View File

@@ -4,6 +4,7 @@ using Content.Server.Atmos.Piping.Components;
using Content.Server.Atmos.Reactions;
using Content.Server.NodeContainer.NodeGroups;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Components;
using Robust.Shared.Map.Components;
using Robust.Shared.Utility;
@@ -11,41 +12,39 @@ namespace Content.Server.Atmos.EntitySystems;
public partial class AtmosphereSystem
{
public GasMixture? GetContainingMixture(EntityUid uid, bool ignoreExposed = false, bool excite = false, TransformComponent? transform = null)
public GasMixture? GetContainingMixture(Entity<TransformComponent?> ent, bool ignoreExposed = false, bool excite = false)
{
if (!ignoreExposed)
if (!Resolve(ent, ref ent.Comp))
return null;
return GetContainingMixture(ent, ent.Comp.GridUid, ent.Comp.MapUid, ignoreExposed, excite);
}
public GasMixture? GetContainingMixture(
Entity<TransformComponent?> ent,
Entity<GridAtmosphereComponent?, GasTileOverlayComponent?>? grid,
Entity<MapAtmosphereComponent?>? map,
bool ignoreExposed = false,
bool excite = false)
{
if (!Resolve(ent, ref ent.Comp))
return null;
if (!ignoreExposed && !ent.Comp.Anchored)
{
// Used for things like disposals/cryo to change which air people are exposed to.
var ev = new AtmosExposedGetAirEvent(uid, excite);
// Give the entity itself a chance to handle this.
RaiseLocalEvent(uid, ref ev, false);
var ev = new AtmosExposedGetAirEvent((ent, ent.Comp), excite);
RaiseLocalEvent(ent, ref ev);
if (ev.Handled)
return ev.Gas;
// We need to get the parent now, so we need the transform... If the parent is invalid, we can't do much else.
if(!Resolve(uid, ref transform) || !transform.ParentUid.IsValid() || transform.MapUid == null)
return GetTileMixture(null, null, Vector2i.Zero, excite);
// Give the parent entity a chance to handle the event...
RaiseLocalEvent(transform.ParentUid, ref ev, false);
if (ev.Handled)
return ev.Gas;
}
// Oops, we did a little bit of code duplication...
else if(!Resolve(uid, ref transform))
{
return GetTileMixture(null, null, Vector2i.Zero, excite);
// TODO ATMOS: recursively iterate up through parents
// This really needs recursive InContainer metadata flag for performance
// And ideally some fast way to get the innermost airtight container.
}
var gridUid = transform.GridUid;
var mapUid = transform.MapUid;
var position = _transformSystem.GetGridOrMapTilePosition(uid, transform);
return GetTileMixture(gridUid, mapUid, position, excite);
var position = _transformSystem.GetGridTilePositionOrDefault((ent, ent.Comp));
return GetTileMixture(grid, map, position, excite);
}
public bool HasAtmosphere(EntityUid gridUid) => _atmosQuery.HasComponent(gridUid);
@@ -84,21 +83,28 @@ public partial class AtmosphereSystem
entity.Comp.InvalidatedCoords.Add(tile);
}
public GasMixture?[]? GetTileMixtures(Entity<GridAtmosphereComponent?>? grid, Entity<MapAtmosphereComponent?>? map, List<Vector2i> tiles, bool excite = false)
public GasMixture?[]? GetTileMixtures(
Entity<GridAtmosphereComponent?, GasTileOverlayComponent?>? grid,
Entity<MapAtmosphereComponent?>? map,
List<Vector2i> tiles,
bool excite = false)
{
GasMixture?[]? mixtures = null;
var handled = false;
// If we've been passed a grid, try to let it handle it.
if (grid is {} gridEnt && Resolve(gridEnt, ref gridEnt.Comp))
if (grid is {} gridEnt && Resolve(gridEnt, ref gridEnt.Comp1))
{
if (excite)
Resolve(gridEnt, ref gridEnt.Comp2);
handled = true;
mixtures = new GasMixture?[tiles.Count];
for (var i = 0; i < tiles.Count; i++)
{
var tile = tiles[i];
if (!gridEnt.Comp.Tiles.TryGetValue(tile, out var atmosTile))
if (!gridEnt.Comp1.Tiles.TryGetValue(tile, out var atmosTile))
{
// need to get map atmosphere
handled = false;
@@ -108,7 +114,10 @@ public partial class AtmosphereSystem
mixtures[i] = atmosTile.Air;
if (excite)
gridEnt.Comp.InvalidatedCoords.Add(tile);
{
AddActiveTile(gridEnt.Comp1, atmosTile);
InvalidateVisuals((gridEnt.Owner, gridEnt.Comp2), tile);
}
}
}
@@ -146,15 +155,22 @@ public partial class AtmosphereSystem
return GetTileMixture(entity.Comp.GridUid, entity.Comp.MapUid, indices, excite);
}
public GasMixture? GetTileMixture(Entity<GridAtmosphereComponent?>? grid, Entity<MapAtmosphereComponent?>? map, Vector2i gridTile, bool excite = false)
public GasMixture? GetTileMixture(
Entity<GridAtmosphereComponent?, GasTileOverlayComponent?>? grid,
Entity<MapAtmosphereComponent?>? map,
Vector2i gridTile,
bool excite = false)
{
// If we've been passed a grid, try to let it handle it.
if (grid is {} gridEnt
&& Resolve(gridEnt, ref gridEnt.Comp, false)
&& gridEnt.Comp.Tiles.TryGetValue(gridTile, out var tile))
&& Resolve(gridEnt, ref gridEnt.Comp1, false)
&& gridEnt.Comp1.Tiles.TryGetValue(gridTile, out var tile))
{
if (excite)
gridEnt.Comp.InvalidatedCoords.Add(gridTile);
{
AddActiveTile(gridEnt.Comp1, tile);
InvalidateVisuals((grid.Value.Owner, grid.Value.Comp2), gridTile);
}
return tile.Air;
}