Content changes for grid splitting (#7645)

Co-authored-by: Vera Aguilera Puerto <gradientvera@outlook.com>
This commit is contained in:
metalgearsloth
2022-04-24 00:59:38 +10:00
committed by GitHub
parent fd4cdbb2e0
commit 8c7772630c
35 changed files with 290 additions and 149 deletions

View File

@@ -22,6 +22,7 @@ namespace Content.Server.Atmos.EntitySystems
SubscribeLocalEvent<AirtightComponent, ComponentInit>(OnAirtightInit);
SubscribeLocalEvent<AirtightComponent, ComponentShutdown>(OnAirtightShutdown);
SubscribeLocalEvent<AirtightComponent, AnchorStateChangedEvent>(OnAirtightPositionChanged);
SubscribeLocalEvent<AirtightComponent, ReAnchorEvent>(OnAirtightReAnchor);
SubscribeLocalEvent<AirtightComponent, RotateEvent>(OnAirtightRotated);
}
@@ -57,7 +58,7 @@ namespace Content.Server.Atmos.EntitySystems
private void OnAirtightPositionChanged(EntityUid uid, AirtightComponent airtight, ref AnchorStateChangedEvent args)
{
var xform = EntityManager.GetComponent<TransformComponent>(uid);
var xform = Transform(uid);
var gridId = xform.GridID;
var coords = xform.Coordinates;
@@ -70,6 +71,16 @@ namespace Content.Server.Atmos.EntitySystems
InvalidatePosition(gridId, tilePos);
}
private void OnAirtightReAnchor(EntityUid uid, AirtightComponent airtight, ref ReAnchorEvent args)
{
foreach (var gridId in new[] { args.OldGrid, args.GridId })
{
// Update and invalidate new position.
airtight.LastPosition = (gridId, args.TilePos);
InvalidatePosition(gridId, args.TilePos);
}
}
private void OnAirtightRotated(EntityUid uid, AirtightComponent airtight, ref RotateEvent ev)
{
if (!airtight.RotateAirBlocked || airtight.InitialAirBlockedDirection == (int)AtmosDirection.Invalid)

View File

@@ -28,6 +28,7 @@ namespace Content.Server.Atmos.EntitySystems
private void InitializeGrid()
{
SubscribeLocalEvent<GridAtmosphereComponent, ComponentInit>(OnGridAtmosphereInit);
SubscribeLocalEvent<GridAtmosphereComponent, GridSplitEvent>(OnGridSplit);
}
private void OnGridAtmosphereInit(EntityUid uid, GridAtmosphereComponent gridAtmosphere, ComponentInit args)
@@ -60,6 +61,56 @@ namespace Content.Server.Atmos.EntitySystems
GridRepopulateTiles(mapGrid.Grid, gridAtmosphere);
}
private void OnGridSplit(EntityUid uid, GridAtmosphereComponent originalGridAtmos, ref GridSplitEvent args)
{
foreach (var newGrid in args.NewGrids)
{
// Make extra sure this is a valid grid.
if (!_mapManager.TryGetGrid(newGrid, out var mapGrid))
continue;
var entity = mapGrid.GridEntityId;
// If the new split grid has an atmosphere already somehow, use that. Otherwise, add a new one.
if (!TryComp(entity, out GridAtmosphereComponent? newGridAtmos))
newGridAtmos = AddComp<GridAtmosphereComponent>(entity);
// We assume the tiles on the new grid have the same coordinates as they did on the old grid...
var enumerator = mapGrid.GetAllTilesEnumerator();
while (enumerator.MoveNext(out var tile))
{
var indices = tile.Value.GridIndices;
// This split event happens *before* the spaced tiles have been invalidated, therefore we can still
// access their gas data. On the next atmos update tick, these tiles will be spaced. Poof!
if (!originalGridAtmos.Tiles.TryGetValue(indices, out var tileAtmosphere))
continue;
// The new grid atmosphere has been initialized, meaning it has all the needed TileAtmospheres...
if (!newGridAtmos.Tiles.TryGetValue(indices, out var newTileAtmosphere))
// Let's be honest, this is really not gonna happen, but just in case...!
continue;
// Copy a bunch of data over... Not great, maybe put this in TileAtmosphere?
newTileAtmosphere.Air = tileAtmosphere.Air?.Clone() ?? null;
newTileAtmosphere.Hotspot = tileAtmosphere.Hotspot;
newTileAtmosphere.HeatCapacity = tileAtmosphere.HeatCapacity;
newTileAtmosphere.Temperature = tileAtmosphere.Temperature;
newTileAtmosphere.PressureDifference = tileAtmosphere.PressureDifference;
newTileAtmosphere.PressureDirection = tileAtmosphere.PressureDirection;
// TODO ATMOS: Somehow force GasTileOverlaySystem to perform an update *right now, right here.*
// The reason why is that right now, gas will flicker until the next GasTileOverlay update.
// That looks bad, of course. We want to avoid that! Anyway that's a bit more complicated so out of scope.
// Invalidate the tile, it's redundant but redundancy is good! Also HashSet so really, no duplicates.
InvalidateTile(originalGridAtmos, indices);
InvalidateTile(newGridAtmos, indices);
}
}
}
#region Grid Is Simulated
/// <summary>
@@ -217,10 +268,11 @@ namespace Content.Server.Atmos.EntitySystems
private AtmosDirection GetBlockedDirections(IMapGrid mapGrid, Vector2i indices)
{
var value = AtmosDirection.Invalid;
var enumerator = GetObstructingComponentsEnumerator(mapGrid, indices);
foreach (var airtightComponent in GetObstructingComponents(mapGrid, indices))
while (enumerator.MoveNext(out var airtightComponent))
{
if(airtightComponent.AirBlocked)
if (airtightComponent.AirBlocked)
value |= airtightComponent.AirBlockedDirection;
}

View File

@@ -11,7 +11,6 @@ namespace Content.Server.Atmos.EntitySystems;
/// </summary>
public sealed class AutomaticAtmosSystem : EntitySystem
{
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
public override void Initialize()
@@ -20,7 +19,6 @@ public sealed class AutomaticAtmosSystem : EntitySystem
SubscribeLocalEvent<TileChangedEvent>(OnTileChanged);
}
private void OnTileChanged(TileChangedEvent ev)
{
// Only if a atmos-holding tile has been added or removed.
@@ -28,18 +26,18 @@ public sealed class AutomaticAtmosSystem : EntitySystem
// TODO: Make tiledefmanager cache the IsSpace property, and turn this lookup-through-two-interfaces into
// TODO: a simple array lookup, as tile IDs are likely contiguous, and there's at most 2^16 possibilities anyway.
if (!((ev.OldTile.IsSpace(_tileDefinitionManager) && !ev.NewTile.IsSpace(_tileDefinitionManager)) ||
(!ev.OldTile.IsSpace(_tileDefinitionManager) && ev.NewTile.IsSpace(_tileDefinitionManager))))
(!ev.OldTile.IsSpace(_tileDefinitionManager) && ev.NewTile.IsSpace(_tileDefinitionManager))) ||
HasComp<IAtmosphereComponent>(ev.Entity))
return;
var uid = _mapManager.GetGridEuid(ev.NewTile.GridIndex);
if (!TryComp<PhysicsComponent>(uid, out var physics))
if (!TryComp<PhysicsComponent>(ev.Entity, out var physics))
return;
// We can't actually count how many tiles there are efficiently, so instead estimate with the mass.
if (physics.Mass / ShuttleSystem.TileMassMultiplier >= 7.0f && !HasComp<IAtmosphereComponent>(uid))
if (physics.Mass / ShuttleSystem.TileMassMultiplier >= 7.0f)
{
AddComp<GridAtmosphereComponent>(uid);
Logger.InfoS("atmos", $"Giving grid {uid} GridAtmosphereComponent.");
AddComp<GridAtmosphereComponent>(ev.Entity);
Logger.InfoS("atmos", $"Giving grid {ev.Entity} GridAtmosphereComponent.");
}
// It's not super important to remove it should the grid become too small again.
// If explosions ever gain the ability to outright shatter grids, do rethink this.