Files
tbd-station-14/Content.Server/Atmos/EntitySystems/AirtightSystem.cs
Vera Aguilera Puerto aa9281d667 Refactors the AtmosphereSystem public-facing API to allow for multiple atmos backends. (#8134)
* Refactors the entirety of the AtmosphereSystem public-facing API to allow for multiple atmos backends.

* actually compiles

* Remove commented out code

* funny bracket

* Move archived moles, temperature from GasMixture to TileAtmosphere.

* WIP customizable map default mixture
still VERY buggy

* broken mess
aaaaaaaaaaaaa

* Fix lattice, etc not being considered space

* visualization for "IsSpace"

* help

* Update Content.Client/Atmos/Overlays/AtmosDebugOverlay.cs

Co-authored-by: Moony <moonheart08@users.noreply.github.com>

* Holy SHIT it compiles AGAIN

* Fix AtmosDeviceSystem crash at shutdown

* Fix immutable tiles on map blueprints not being fixed by fixgridatmos/revalidate.

* Use space instead of gasmixture immutable for heat capacity calculations

* Remove all LINDA-specific code from GasMixture, move it to TileAtmosphere/AtmosphereSystem instead.

* Fix roundstart tiles not processing

* Update Content.Server/Atmos/Commands/SetTemperatureCommand.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Update Content.Server/Atmos/EntitySystems/AtmosphereSystem.API.cs

Changed Files tab is so large I can't commit both suggestions at once mfw

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

Co-authored-by: Moony <moonheart08@users.noreply.github.com>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2022-07-04 09:51:34 -05:00

160 lines
6.1 KiB
C#

using Content.Server.Atmos.Components;
using Content.Server.Explosion.EntitySystems;
using Content.Shared.Atmos;
using JetBrains.Annotations;
using Robust.Shared.Map;
namespace Content.Server.Atmos.EntitySystems
{
[UsedImplicitly]
public sealed class AirtightSystem : EntitySystem
{
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
[Dependency] private readonly ExplosionSystem _explosionSystem = default!;
public override void Initialize()
{
SubscribeLocalEvent<AirtightComponent, ComponentInit>(OnAirtightInit);
SubscribeLocalEvent<AirtightComponent, ComponentShutdown>(OnAirtightShutdown);
SubscribeLocalEvent<AirtightComponent, AnchorStateChangedEvent>(OnAirtightPositionChanged);
SubscribeLocalEvent<AirtightComponent, ReAnchorEvent>(OnAirtightReAnchor);
SubscribeLocalEvent<AirtightComponent, RotateEvent>(OnAirtightRotated);
}
private void OnAirtightInit(EntityUid uid, AirtightComponent airtight, ComponentInit args)
{
var xform = EntityManager.GetComponent<TransformComponent>(uid);
if (airtight.FixAirBlockedDirectionInitialize)
{
var rotateEvent = new RotateEvent(airtight.Owner, Angle.Zero, xform.LocalRotation, xform);
OnAirtightRotated(uid, airtight, ref rotateEvent);
}
// Adding this component will immediately anchor the entity, because the atmos system
// requires airtight entities to be anchored for performance.
xform.Anchored = true;
UpdatePosition(airtight);
}
private void OnAirtightShutdown(EntityUid uid, AirtightComponent airtight, ComponentShutdown args)
{
var xform = Transform(uid);
// If the grid is deleting no point updating atmos.
if (_mapManager.TryGetGrid(xform.GridUid, out var grid))
{
if (MetaData(grid.GridEntityId).EntityLifeStage > EntityLifeStage.MapInitialized) return;
}
SetAirblocked(airtight, false, xform);
}
private void OnAirtightPositionChanged(EntityUid uid, AirtightComponent airtight, ref AnchorStateChangedEvent args)
{
var xform = Transform(uid);
if (!TryComp(xform.GridUid, out IMapGridComponent? grid))
return;
var gridId = xform.GridUid;
var coords = xform.Coordinates;
var tilePos = grid.Grid.TileIndicesFor(coords);
// Update and invalidate new position.
airtight.LastPosition = (gridId.Value, tilePos);
InvalidatePosition(gridId.Value, tilePos);
}
private void OnAirtightReAnchor(EntityUid uid, AirtightComponent airtight, ref ReAnchorEvent args)
{
foreach (var gridId in new[] { args.OldGrid, args.Grid })
{
// 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)
return;
airtight.CurrentAirBlockedDirection = (int) Rotate((AtmosDirection)airtight.InitialAirBlockedDirection, ev.NewRotation);
UpdatePosition(airtight);
RaiseLocalEvent(uid, new AirtightChanged(airtight), true);
}
public void SetAirblocked(AirtightComponent airtight, bool airblocked, TransformComponent? xform = null)
{
if (!Resolve(airtight.Owner, ref xform)) return;
airtight.AirBlocked = airblocked;
UpdatePosition(airtight, xform);
RaiseLocalEvent(airtight.Owner, new AirtightChanged(airtight), true);
}
public void UpdatePosition(AirtightComponent airtight, TransformComponent? xform = null)
{
if (!Resolve(airtight.Owner, ref xform)) return;
if (!xform.Anchored || !_mapManager.TryGetGrid(xform.GridUid, out var grid))
return;
airtight.LastPosition = (xform.GridUid.Value, grid.TileIndicesFor(xform.Coordinates));
InvalidatePosition(airtight.LastPosition.Item1, airtight.LastPosition.Item2, airtight.FixVacuum && !airtight.AirBlocked);
}
public void InvalidatePosition(EntityUid gridId, Vector2i pos, bool fixVacuum = false)
{
if (!_mapManager.TryGetGrid(gridId, out var grid))
return;
var gridUid = grid.GridEntityId;
var query = EntityManager.GetEntityQuery<AirtightComponent>();
_explosionSystem.UpdateAirtightMap(gridId, pos, query);
// TODO make atmos system use query
_atmosphereSystem.UpdateAdjacent(gridUid, pos);
_atmosphereSystem.InvalidateTile(gridUid, pos);
if(fixVacuum)
_atmosphereSystem.FixTileVacuum(gridUid, pos);
}
private AtmosDirection Rotate(AtmosDirection myDirection, Angle myAngle)
{
var newAirBlockedDirs = AtmosDirection.Invalid;
if (myAngle == Angle.Zero)
return myDirection;
// TODO ATMOS MULTIZ: When we make multiZ atmos, special case this.
for (var i = 0; i < Atmospherics.Directions; i++)
{
var direction = (AtmosDirection) (1 << i);
if (!myDirection.IsFlagSet(direction)) continue;
var angle = direction.ToAngle();
angle += myAngle;
newAirBlockedDirs |= angle.ToAtmosDirectionCardinal();
}
return newAirBlockedDirs;
}
}
public sealed class AirtightChanged : EntityEventArgs
{
public AirtightComponent Airtight;
public AirtightChanged(AirtightComponent airtight)
{
Airtight = airtight;
}
}
}