Files
tbd-station-14/Content.Server/Atmos/EntitySystems/AirtightSystem.cs
TemporalOroboros f284b43ff6 Fixes obsolete Transform warnings in Content. (#25256)
* Fix TransformComponent.MapPosition warnings in Content.Client

* Fix TransformComponent.MapPosition warnings in Content.IntegrationTests

* Fix TransformComponent.MapPosition warnings in Content.Shared

* Fix TransformComponent.MapPosition warnings in Content.Server

* Fix TransformComponent.WorldPosition warnings in Content.Shared

* Fix TransformComponent.WorldPosition warnings in Content.Client
Excepts ClickableComponent b/c that needs to be ECS'd entirely later

* Fix TransformComponent.WorldPosition warnings in Content.Server

* Fix TransformComponent.WorldRotation warnings in Content.*

* Fix TransformComponent.MapPosition warnings I missed

* Fix TransformComponent.WorldMatrix warnings in Content.*

* Fix TransformComponent.InvWorldMatrix warnings in Content.*

* Fix TransformComponent.GetWorldPositionRotationMatrixWithInv warnings in Content.*

* Fix TransformComponent.GetWorldPositionRotationMatrix warnings in Content.*

* Fix TransformComponent.GetWorldPositionRotation warnings in Content.*

* Fix TransformComponent.Anchored.set warnings in Content.*

* Fix TransformComponent.Coordinates.set warnings in Content.*

* Fix TransformComponent.LocalPosition.set warnings in Content.*

* Fix TransformComponent.AttachToGridOrMap warnings in Content.*

* Fix TransformComponent.AttachParent warnings in Content.*

* Preempt TransformComponent.LocalRotation.set warnings in Content.Shared

* Preempt TransformComponent.LocalRotation.set warnings in Content.Client

* Preempt TransformComponent.LocalRotation.set warnings in Content.IntegrationTests

* Preempt TransformComponent.LocalRotation.set warnings in Content.Server

* Fix/Preempt the remaining obsolete TransformComponent properties/methods in Content.*

* ECS ClickableComponent

* Fix obsolete SharedTransformSystem methods in Content.*

* Fix ExplosionOverlay `SharedTransformSystem` dependency

* Maybe fix null eye position breaking tests

* MGS requested changes
2024-02-27 12:06:20 +11:00

165 lines
6.3 KiB
C#

using Content.Server.Atmos.Components;
using Content.Server.Explosion.EntitySystems;
using Content.Shared.Atmos;
using JetBrains.Annotations;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
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, MoveEvent>(OnAirtightMoved);
}
private void OnAirtightInit(Entity<AirtightComponent> airtight, ref ComponentInit args)
{
var xform = EntityManager.GetComponent<TransformComponent>(airtight);
if (airtight.Comp.FixAirBlockedDirectionInitialize)
{
var moveEvent = new MoveEvent((airtight, xform, MetaData(airtight)), default, default, Angle.Zero, xform.LocalRotation);
if (AirtightMove(airtight, ref moveEvent))
return;
}
UpdatePosition(airtight);
}
private void OnAirtightShutdown(Entity<AirtightComponent> airtight, ref ComponentShutdown args)
{
var xform = Transform(airtight);
// If the grid is deleting no point updating atmos.
if (HasComp<MapGridComponent>(xform.GridUid) &&
MetaData(xform.GridUid.Value).EntityLifeStage > EntityLifeStage.MapInitialized)
{
return;
}
SetAirblocked(airtight, false, xform);
}
private void OnAirtightPositionChanged(EntityUid uid, AirtightComponent airtight, ref AnchorStateChangedEvent args)
{
var xform = args.Transform;
if (!TryComp(xform.GridUid, out MapGridComponent? grid))
return;
var gridId = xform.GridUid;
var coords = xform.Coordinates;
var tilePos = 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 OnAirtightMoved(Entity<AirtightComponent> airtight, ref MoveEvent ev)
{
AirtightMove(airtight, ref ev);
}
private bool AirtightMove(Entity<AirtightComponent> ent, ref MoveEvent ev)
{
var (owner, airtight) = ent;
if (!airtight.RotateAirBlocked || airtight.InitialAirBlockedDirection == (int)AtmosDirection.Invalid)
return false;
airtight.CurrentAirBlockedDirection = (int) Rotate((AtmosDirection)airtight.InitialAirBlockedDirection, ev.NewRotation);
var pos = airtight.LastPosition;
UpdatePosition(ent, ev.Component);
var airtightEv = new AirtightChanged(owner, airtight, pos);
RaiseLocalEvent(owner, ref airtightEv, true);
return true;
}
public void SetAirblocked(Entity<AirtightComponent> airtight, bool airblocked, TransformComponent? xform = null)
{
if (airtight.Comp.AirBlocked == airblocked)
return;
if (!Resolve(airtight, ref xform))
return;
var pos = airtight.Comp.LastPosition;
airtight.Comp.AirBlocked = airblocked;
UpdatePosition(airtight, xform);
var airtightEv = new AirtightChanged(airtight, airtight, pos);
RaiseLocalEvent(airtight, ref airtightEv, true);
}
public void UpdatePosition(Entity<AirtightComponent> ent, TransformComponent? xform = null)
{
var (owner, airtight) = ent;
if (!Resolve(owner, ref xform))
return;
if (!xform.Anchored || !TryComp(xform.GridUid, out MapGridComponent? 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 (!TryComp(gridId, out MapGridComponent? grid))
return;
var query = EntityManager.GetEntityQuery<AirtightComponent>();
_explosionSystem.UpdateAirtightMap(gridId, pos, grid, query);
// TODO make atmos system use query
_atmosphereSystem.InvalidateTile(gridId, 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;
}
}
[ByRefEvent]
public readonly record struct AirtightChanged(EntityUid Entity, AirtightComponent Airtight,
(EntityUid Grid, Vector2i Tile) Position);
}