Partial atmos refactor (#22521)

* Reduce atmos component queries

* Remove method events

* Cache airtight data

* Make MolesArchived nullable

* Fix airtight cache

* only get tile def once

* Immutable mixtures

* firelock queries

* misc

* misc cleanup

* Trim disconnected tiles

* Fix merge issues and bugs

* Why does the PR keep increasing in scope

* debug overlay

* Fix bugs

* Fix test, remove unused events

* Add setmapatmos command

* Fix overlays

* Add map check

* A

* Resolve conflicts with #26102

* Remove some obsolete methods
This commit is contained in:
Leon Friedrich
2024-03-24 03:34:56 +11:00
committed by GitHub
parent 05f282f5ce
commit 18a35e7e83
43 changed files with 922 additions and 666 deletions

View File

@@ -18,6 +18,7 @@ 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!;
@@ -33,6 +34,8 @@ public sealed class SpreaderSystem : EntitySystem
// TODO PERFORMANCE Assign each prototype to an index and convert dictionary to array
private readonly Dictionary<EntityUid, Dictionary<string, int>> _gridUpdates = [];
private EntityQuery<EdgeSpreaderComponent> _query;
public const float SpreadCooldownSeconds = 1;
[ValidatePrototypeId<TagPrototype>]
@@ -47,6 +50,8 @@ public sealed class SpreaderSystem : EntitySystem
SubscribeLocalEvent<EdgeSpreaderComponent, EntityTerminatingEvent>(OnTerminating);
SetupPrototypes();
_query = GetEntityQuery<EdgeSpreaderComponent>();
}
private void OnPrototypeReload(PrototypesReloadedEventArgs obj)
@@ -66,13 +71,7 @@ public sealed class SpreaderSystem : EntitySystem
private void OnAirtightChanged(ref AirtightChanged ev)
{
var neighbors = GetSpreadableNeighbors(ev.Entity, ev.Airtight, ev.Position);
foreach (var neighbor in neighbors)
{
if (!TerminatingOrDeleted(neighbor))
EnsureComp<ActiveEdgeSpreaderComponent>(neighbor);
}
ActivateSpreadableNeighbors(ev.Entity, ev.Position);
}
private void OnGridInit(GridInitializeEvent ev)
@@ -82,13 +81,7 @@ public sealed class SpreaderSystem : EntitySystem
private void OnTerminating(Entity<EdgeSpreaderComponent> entity, ref EntityTerminatingEvent args)
{
var neighbors = GetSpreadableNeighbors(entity);
foreach (var neighbor in neighbors)
{
if (!TerminatingOrDeleted(neighbor))
EnsureComp<ActiveEdgeSpreaderComponent>(neighbor);
}
ActivateSpreadableNeighbors(entity);
}
/// <inheritdoc/>
@@ -254,8 +247,7 @@ public sealed class SpreaderSystem : EntitySystem
if (!_map.TryGetTileRef(neighborEnt, neighborGrid, neighborPos, out var tileRef) || tileRef.Tile.IsEmpty)
continue;
var directionEnumerator =
_map.GetAnchoredEntitiesEnumerator(neighborEnt, neighborGrid, neighborPos);
var directionEnumerator = _map.GetAnchoredEntitiesEnumerator(neighborEnt, neighborGrid, neighborPos);
var occupied = false;
while (directionEnumerator.MoveNext(out var ent))
@@ -277,8 +269,7 @@ public sealed class SpreaderSystem : EntitySystem
continue;
var oldCount = occupiedTiles.Count;
directionEnumerator =
_map.GetAnchoredEntitiesEnumerator(neighborEnt, neighborGrid, neighborPos);
directionEnumerator = _map.GetAnchoredEntitiesEnumerator(neighborEnt, neighborGrid, neighborPos);
while (directionEnumerator.MoveNext(out var ent))
{
@@ -299,14 +290,11 @@ public sealed class SpreaderSystem : EntitySystem
}
/// <summary>
/// Given an entity, this returns a list of all adjacent entities with a <see cref="EdgeSpreaderComponent"/>.
/// This function activates all spreaders that are adjacent to a given entity. This also activates other spreaders
/// on the same tile as the current entity (for thin airtight entities like windoors).
/// </summary>
public List<EntityUid> GetSpreadableNeighbors(EntityUid uid, AirtightComponent? comp = null,
(EntityUid Grid, Vector2i Tile)? position = null)
public void ActivateSpreadableNeighbors(EntityUid uid, (EntityUid Grid, Vector2i Tile)? position = null)
{
Resolve(uid, ref comp, false);
var neighbors = new List<EntityUid>();
Vector2i tile;
EntityUid ent;
MapGridComponent? grid;
@@ -315,37 +303,40 @@ public sealed class SpreaderSystem : EntitySystem
{
var transform = Transform(uid);
if (!TryComp(transform.GridUid, out grid) || TerminatingOrDeleted(transform.GridUid.Value))
return neighbors;
return;
tile = _map.TileIndicesFor(transform.GridUid.Value, grid, transform.Coordinates);
ent = transform.GridUid.Value;
}
else
{
if (!TryComp(position.Value.Grid, out grid))
return neighbors;
tile = position.Value.Tile;
ent = position.Value.Grid;
return;
(ent, tile) = position.Value;
}
var spreaderQuery = GetEntityQuery<EdgeSpreaderComponent>();
var anchored = _map.GetAnchoredEntitiesEnumerator(ent, grid, tile);
while (anchored.MoveNext(out var entity))
{
if (entity == ent)
continue;
DebugTools.Assert(Transform(entity.Value).Anchored);
if (_query.HasComponent(ent) && !TerminatingOrDeleted(entity.Value))
EnsureComp<ActiveEdgeSpreaderComponent>(entity.Value);
}
for (var i = 0; i < Atmospherics.Directions; i++)
{
var direction = (AtmosDirection) (1 << i);
if (comp != null && !comp.AirBlockedDirection.IsFlagSet(direction))
continue;
var adjacentTile = SharedMapSystem.GetDirection(tile, direction.ToDirection());
anchored = _map.GetAnchoredEntitiesEnumerator(ent, grid, adjacentTile);
var directionEnumerator =
_map.GetAnchoredEntitiesEnumerator(ent, grid, SharedMapSystem.GetDirection(tile, direction.ToDirection()));
while (directionEnumerator.MoveNext(out var entity))
while (anchored.MoveNext(out var entity))
{
DebugTools.Assert(Transform(entity.Value).Anchored);
if (spreaderQuery.HasComponent(entity) && !TerminatingOrDeleted(entity.Value))
neighbors.Add(entity.Value);
if (_query.HasComponent(ent) && !TerminatingOrDeleted(entity.Value))
EnsureComp<ActiveEdgeSpreaderComponent>(entity.Value);
}
}
return neighbors;
}
}