From: Tayrtahn Date: Fri, 18 Apr 2025 03:55:09 +0000 (-0400) Subject: Partial cleanup of ExplosionSystem (#36098) X-Git-Url: https://git.smokeofanarchy.ru/gitweb.cgi?a=commitdiff_plain;h=725522c183b232ab59915ba7a4321b29dcc037b2;p=space-station-14.git Partial cleanup of ExplosionSystem (#36098) * Cleanup warnings in ExplosionSystem.TileFill * Formatting in ExplosionSystem.TileFill * Cleanup warning in ExplosionSystem * Formatting for ExplosionSystem * Switch from MapChangedEvent to MapRemovedEvent * Fix 1 warning in ExplosionSystem.Processing * Fix 1 warning in ExplosionSystem.Visuals * Fix 1 warning in ExplosionSystem.GridMap * Fix 2 warnings in ExplosionSystem.Airtight * Clear _grids --- diff --git a/Content.Server/Explosion/EntitySystems/ExplosionSystem.Airtight.cs b/Content.Server/Explosion/EntitySystems/ExplosionSystem.Airtight.cs index 6fa553bc8b..adc2e14f69 100644 --- a/Content.Server/Explosion/EntitySystems/ExplosionSystem.Airtight.cs +++ b/Content.Server/Explosion/EntitySystems/ExplosionSystem.Airtight.cs @@ -69,7 +69,7 @@ public sealed partial class ExplosionSystem query ??= EntityManager.GetEntityQuery(); var damageQuery = EntityManager.GetEntityQuery(); var destructibleQuery = EntityManager.GetEntityQuery(); - var anchoredEnumerator = grid.GetAnchoredEntitiesEnumerator(tile); + var anchoredEnumerator = _mapSystem.GetAnchoredEntitiesEnumerator(gridId, grid, tile); while (anchoredEnumerator.MoveNext(out var uid)) { @@ -105,7 +105,7 @@ public sealed partial class ExplosionSystem if (!TryComp(transform.GridUid, out var grid)) return; - UpdateAirtightMap(transform.GridUid.Value, grid, grid.CoordinatesToTile(transform.Coordinates)); + UpdateAirtightMap(transform.GridUid.Value, grid, _mapSystem.CoordinatesToTile(transform.GridUid.Value, grid, transform.Coordinates)); } /// diff --git a/Content.Server/Explosion/EntitySystems/ExplosionSystem.GridMap.cs b/Content.Server/Explosion/EntitySystems/ExplosionSystem.GridMap.cs index 3be64e1776..3b63750ff6 100644 --- a/Content.Server/Explosion/EntitySystems/ExplosionSystem.GridMap.cs +++ b/Content.Server/Explosion/EntitySystems/ExplosionSystem.GridMap.cs @@ -258,7 +258,7 @@ public sealed partial class ExplosionSystem { var neighbourIndex = tileRef.GridIndices + NeighbourVectors[i]; - if (grid.TryGetTileRef(neighbourIndex, out var neighbourTile) && !neighbourTile.Tile.IsEmpty) + if (_mapSystem.TryGetTileRef(ev.Entity, grid, neighbourIndex, out var neighbourTile) && !neighbourTile.Tile.IsEmpty) { var oppositeDirection = (NeighborFlag) (1 << ((i + 4) % 8)); edges[neighbourIndex] = edges.GetValueOrDefault(neighbourIndex) | oppositeDirection; diff --git a/Content.Server/Explosion/EntitySystems/ExplosionSystem.Processing.cs b/Content.Server/Explosion/EntitySystems/ExplosionSystem.Processing.cs index 8ea2d5dfd9..3f2a0ea2da 100644 --- a/Content.Server/Explosion/EntitySystems/ExplosionSystem.Processing.cs +++ b/Content.Server/Explosion/EntitySystems/ExplosionSystem.Processing.cs @@ -67,13 +67,10 @@ public sealed partial class ExplosionSystem private List _anchored = new(); - private void OnMapChanged(MapChangedEvent ev) + private void OnMapRemoved(MapRemovedEvent ev) { // If a map was deleted, check the explosion currently being processed belongs to that map. - if (ev.Created) - return; - - if (_activeExplosion?.Epicenter.MapId != ev.Map) + if (_activeExplosion?.Epicenter.MapId != ev.MapId) return; QueueDel(_activeExplosion.VisualEnt); @@ -718,7 +715,7 @@ sealed class Explosion if (spaceData != null) { - var mapUid = mapMan.GetMapEntityId(epicenter.MapId); + var mapUid = mapSystem.GetMap(epicenter.MapId); _explosionData.Add(new() { diff --git a/Content.Server/Explosion/EntitySystems/ExplosionSystem.TileFill.cs b/Content.Server/Explosion/EntitySystems/ExplosionSystem.TileFill.cs index 43cc45beac..05608cda40 100644 --- a/Content.Server/Explosion/EntitySystems/ExplosionSystem.TileFill.cs +++ b/Content.Server/Explosion/EntitySystems/ExplosionSystem.TileFill.cs @@ -1,7 +1,6 @@ using System.Linq; using System.Numerics; using Content.Shared.Administration; -using Content.Shared.Explosion; using Content.Shared.Explosion.Components; using Robust.Shared.Map; using Robust.Shared.Map.Components; @@ -15,6 +14,11 @@ namespace Content.Server.Explosion.EntitySystems; public sealed partial class ExplosionSystem { + /// + /// A list of grids to be reused by to avoid allocating twice for each call. + /// + private List> _grids = []; + /// /// This is the main explosion generating function. /// @@ -48,7 +52,7 @@ public sealed partial class ExplosionSystem // get the epicenter tile indices if (_mapManager.TryFindGridAt(epicenter, out var gridUid, out var candidateGrid) && - candidateGrid.TryGetTileRef(candidateGrid.WorldToTile(epicenter.Position), out var tileRef) && + _mapSystem.TryGetTileRef(gridUid, candidateGrid, _mapSystem.WorldToTile(gridUid, candidateGrid, epicenter.Position), out var tileRef) && !tileRef.Tile.IsEmpty) { epicentreGrid = gridUid; @@ -57,14 +61,15 @@ public sealed partial class ExplosionSystem else if (referenceGrid != null) { // reference grid defines coordinate system that the explosion in space will use - initialTile = Comp(referenceGrid.Value).WorldToTile(epicenter.Position); + var gridComp = Comp(referenceGrid.Value); + initialTile = _mapSystem.WorldToTile(referenceGrid.Value, gridComp, epicenter.Position); } else { // this is a space-based explosion that (should) not touch any grids. initialTile = new Vector2i( - (int) Math.Floor(epicenter.Position.X / DefaultTileSize), - (int) Math.Floor(epicenter.Position.Y / DefaultTileSize)); + (int)Math.Floor(epicenter.Position.X / DefaultTileSize), + (int)Math.Floor(epicenter.Position.Y / DefaultTileSize)); } // Main data for the exploding tiles in space and on various grids @@ -88,7 +93,7 @@ public sealed partial class ExplosionSystem var spaceAngle = Angle.Zero; if (referenceGrid != null) { - var xform = Transform(Comp(referenceGrid.Value).Owner); + var xform = Transform(referenceGrid.Value); (_, spaceAngle, spaceMatrix) = _transformSystem.GetWorldPositionRotationMatrix(xform); } @@ -130,7 +135,7 @@ public sealed partial class ExplosionSystem // These variables keep track of the total intensity we have distributed List tilesInIteration = new() { 1 }; - List iterationIntensity = new() {stepSize}; + List iterationIntensity = new() { stepSize }; var totalTiles = 1; var remainingIntensity = totalIntensity - stepSize; @@ -276,7 +281,9 @@ public sealed partial class ExplosionSystem // diameter x diameter sized box, use a smaller box with radius sized sides: var box = Box2.CenteredAround(epicenter.Position, new Vector2(radius, radius)); - foreach (var grid in _mapManager.FindGridsIntersecting(epicenter.MapId, box)) + _grids.Clear(); + _mapManager.FindGridsIntersecting(epicenter.MapId, box, ref _grids); + foreach (var grid in _grids) { if (TryComp(grid.Owner, out PhysicsComponent? physics) && physics.Mass > mass) { @@ -296,14 +303,15 @@ public sealed partial class ExplosionSystem radius *= 4; box = Box2.CenteredAround(epicenter.Position, new Vector2(radius, radius)); - var mapGrids = _mapManager.FindGridsIntersecting(epicenter.MapId, box).ToList(); - var grids = mapGrids.Select(x => x.Owner).ToList(); + _grids.Clear(); + _mapManager.FindGridsIntersecting(epicenter.MapId, box, ref _grids); + var grids = _grids.Select(x => x.Owner).ToList(); if (referenceGrid != null) return (grids, referenceGrid, radius); // We still don't have are reference grid. So lets also look in the enlarged region - foreach (var grid in mapGrids) + foreach (var grid in _grids) { if (TryComp(grid.Owner, out PhysicsComponent? physics) && physics.Mass > mass) { diff --git a/Content.Server/Explosion/EntitySystems/ExplosionSystem.Visuals.cs b/Content.Server/Explosion/EntitySystems/ExplosionSystem.Visuals.cs index 57323e4de7..eb789eb0c6 100644 --- a/Content.Server/Explosion/EntitySystems/ExplosionSystem.Visuals.cs +++ b/Content.Server/Explosion/EntitySystems/ExplosionSystem.Visuals.cs @@ -57,7 +57,7 @@ public sealed partial class ExplosionSystem // Light, sound & visuals may extend well beyond normal PVS range. In principle, this should probably still be // restricted to something like the same map, but whatever. - _pvsSys.AddGlobalOverride(GetNetEntity(explosionEntity)); + _pvsSys.AddGlobalOverride(explosionEntity); var appearance = AddComp(explosionEntity); _appearance.SetData(explosionEntity, ExplosionAppearanceData.Progress, 1, appearance); diff --git a/Content.Server/Explosion/EntitySystems/ExplosionSystem.cs b/Content.Server/Explosion/EntitySystems/ExplosionSystem.cs index 6bf237f46f..2aeefeb46c 100644 --- a/Content.Server/Explosion/EntitySystems/ExplosionSystem.cs +++ b/Content.Server/Explosion/EntitySystems/ExplosionSystem.cs @@ -94,7 +94,7 @@ public sealed partial class ExplosionSystem : SharedExplosionSystem SubscribeLocalEvent(OnReset); // Handled by ExplosionSystem.Processing.cs - SubscribeLocalEvent(OnMapChanged); + SubscribeLocalEvent(OnMapRemoved); // handled in ExplosionSystemAirtight.cs SubscribeLocalEvent(OnAirtightDamaged); @@ -155,12 +155,12 @@ public sealed partial class ExplosionSystem : SharedExplosionSystem // Override the explosion intensity if optional arguments were provided. if (radius != null) - totalIntensity ??= RadiusToIntensity((float) radius, explosive.IntensitySlope, explosive.MaxIntensity); + totalIntensity ??= RadiusToIntensity((float)radius, explosive.IntensitySlope, explosive.MaxIntensity); totalIntensity ??= explosive.TotalIntensity; QueueExplosion(uid, explosive.ExplosionType, - (float) totalIntensity, + (float)totalIntensity, explosive.IntensitySlope, explosive.MaxIntensity, explosive.TileBreakScale, @@ -332,7 +332,7 @@ public sealed partial class ExplosionSystem : SharedExplosionSystem private Explosion? SpawnExplosion(QueuedExplosion queued) { var pos = queued.Epicenter; - if (!_mapManager.MapExists(pos.MapId)) + if (!_mapSystem.MapExists(pos.MapId)) return null; var results = GetExplosionTiles(pos, queued.Proto.ID, queued.TotalIntensity, queued.Slope, queued.MaxTileIntensity);