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
This commit is contained in:
Tayrtahn
2025-04-17 23:55:09 -04:00
committed by GitHub
parent 0da27d3371
commit 725522c183
6 changed files with 30 additions and 25 deletions

View File

@@ -69,7 +69,7 @@ public sealed partial class ExplosionSystem
query ??= EntityManager.GetEntityQuery<AirtightComponent>();
var damageQuery = EntityManager.GetEntityQuery<DamageableComponent>();
var destructibleQuery = EntityManager.GetEntityQuery<DestructibleComponent>();
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<MapGridComponent>(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));
}
/// <summary>

View File

@@ -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;

View File

@@ -67,13 +67,10 @@ public sealed partial class ExplosionSystem
private List<EntityUid> _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()
{

View File

@@ -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
{
/// <summary>
/// A list of grids to be reused by <see cref="GetLocalGrids"/> to avoid allocating twice for each call.
/// </summary>
private List<Entity<MapGridComponent>> _grids = [];
/// <summary>
/// This is the main explosion generating function.
/// </summary>
@@ -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<MapGridComponent>(referenceGrid.Value).WorldToTile(epicenter.Position);
var gridComp = Comp<MapGridComponent>(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<MapGridComponent>(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<int> tilesInIteration = new() { 1 };
List<float> iterationIntensity = new() {stepSize};
List<float> 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)
{

View File

@@ -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<AppearanceComponent>(explosionEntity);
_appearance.SetData(explosionEntity, ExplosionAppearanceData.Progress, 1, appearance);

View File

@@ -94,7 +94,7 @@ public sealed partial class ExplosionSystem : SharedExplosionSystem
SubscribeLocalEvent<RoundRestartCleanupEvent>(OnReset);
// Handled by ExplosionSystem.Processing.cs
SubscribeLocalEvent<MapChangedEvent>(OnMapChanged);
SubscribeLocalEvent<MapRemovedEvent>(OnMapRemoved);
// handled in ExplosionSystemAirtight.cs
SubscribeLocalEvent<AirtightComponent, DamageChangedEvent>(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);