Files

195 lines
7.3 KiB
C#
Raw Permalink Normal View History

2023-08-31 18:32:03 +10:00
using System.Diagnostics.CodeAnalysis;
2022-09-16 11:46:09 -07:00
using Content.Server.Atmos.EntitySystems;
using Content.Server.Body.Systems;
2022-07-13 19:11:59 -04:00
using Content.Server.Construction;
using Content.Server.Construction.Components;
2022-05-03 01:43:25 +03:00
using Content.Server.Storage.Components;
2023-08-31 22:29:11 +10:00
using Content.Shared.Destructible;
2023-11-19 17:44:42 +00:00
using Content.Shared.Explosion;
2023-08-31 22:30:40 -04:00
using Content.Shared.Foldable;
2023-08-31 22:29:11 +10:00
using Content.Shared.Interaction;
using Content.Shared.Lock;
using Content.Shared.Movement.Events;
using Content.Shared.Storage.Components;
using Content.Shared.Storage.EntitySystems;
2023-09-05 00:07:01 +10:00
using Content.Shared.Tools.Systems;
2023-08-31 22:29:11 +10:00
using Content.Shared.Verbs;
using Robust.Server.GameObjects;
2022-07-13 19:11:59 -04:00
using Robust.Shared.Containers;
2023-08-31 22:29:11 +10:00
using Robust.Shared.GameStates;
2022-07-13 19:11:59 -04:00
using Robust.Shared.Map;
2022-05-03 01:43:25 +03:00
namespace Content.Server.Storage.EntitySystems;
public sealed class EntityStorageSystem : SharedEntityStorageSystem
2022-05-03 01:43:25 +03:00
{
2022-07-13 19:11:59 -04:00
[Dependency] private readonly ConstructionSystem _construction = default!;
2022-09-16 11:46:09 -07:00
[Dependency] private readonly AtmosphereSystem _atmos = default!;
[Dependency] private readonly IMapManager _map = default!;
[Dependency] private readonly MapSystem _mapSystem = default!;
2022-05-03 01:43:25 +03:00
public override void Initialize()
{
base.Initialize();
2022-07-13 19:11:59 -04:00
2023-08-31 22:29:11 +10:00
/* CompRef things */
SubscribeLocalEvent<EntityStorageComponent, EntityUnpausedEvent>(OnEntityUnpausedEvent);
2023-08-31 22:29:11 +10:00
SubscribeLocalEvent<EntityStorageComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<EntityStorageComponent, ComponentStartup>(OnComponentStartup);
SubscribeLocalEvent<EntityStorageComponent, ActivateInWorldEvent>(OnInteract, after: new[] { typeof(LockSystem) });
SubscribeLocalEvent<EntityStorageComponent, LockToggleAttemptEvent>(OnLockToggleAttempt);
SubscribeLocalEvent<EntityStorageComponent, DestructionEventArgs>(OnDestruction);
SubscribeLocalEvent<EntityStorageComponent, GetVerbsEvent<InteractionVerb>>(AddToggleOpenVerb);
SubscribeLocalEvent<EntityStorageComponent, ContainerRelayMovementEntityEvent>(OnRelayMovement);
2023-08-31 22:30:40 -04:00
SubscribeLocalEvent<EntityStorageComponent, FoldAttemptEvent>(OnFoldAttempt);
2023-08-31 22:29:11 +10:00
SubscribeLocalEvent<EntityStorageComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<EntityStorageComponent, ComponentHandleState>(OnHandleState);
/* CompRef things */
2023-03-24 21:30:19 -07:00
SubscribeLocalEvent<EntityStorageComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<EntityStorageComponent, WeldableAttemptEvent>(OnWeldableAttempt);
2023-11-19 17:44:42 +00:00
SubscribeLocalEvent<EntityStorageComponent, BeforeExplodeEvent>(OnExploded);
2022-09-16 11:46:09 -07:00
SubscribeLocalEvent<InsideEntityStorageComponent, InhaleLocationEvent>(OnInsideInhale);
SubscribeLocalEvent<InsideEntityStorageComponent, ExhaleLocationEvent>(OnInsideExhale);
SubscribeLocalEvent<InsideEntityStorageComponent, AtmosExposedGetAirEvent>(OnInsideExposed);
SubscribeLocalEvent<InsideEntityStorageComponent, EntGotRemovedFromContainerMessage>(OnRemoved);
2022-05-03 01:43:25 +03:00
}
2023-03-24 21:30:19 -07:00
private void OnMapInit(EntityUid uid, EntityStorageComponent component, MapInitEvent args)
{
if (!component.Open && component.Air.TotalMoles == 0)
{
// If we're closed on spawn and have no air already saved, we need to pull some air into our environment from where we spawned,
// so that we have -something-. For example, if you bought an animal crate or something.
TakeGas(uid, component);
}
}
protected override void OnComponentInit(EntityUid uid, SharedEntityStorageComponent component, ComponentInit args)
2022-07-13 19:11:59 -04:00
{
base.OnComponentInit(uid, component, args);
2022-07-13 19:11:59 -04:00
if (TryComp<ConstructionComponent>(uid, out var construction))
_construction.AddContainer(uid, ContainerName, construction);
2022-07-13 19:11:59 -04:00
}
2023-08-31 18:32:03 +10:00
public override bool ResolveStorage(EntityUid uid, [NotNullWhen(true)] ref SharedEntityStorageComponent? component)
{
2023-08-31 22:29:11 +10:00
if (component != null)
return true;
TryComp<EntityStorageComponent>(uid, out var storage);
component = storage;
return component != null;
2023-08-31 18:32:03 +10:00
}
private void OnWeldableAttempt(EntityUid uid, EntityStorageComponent component, WeldableAttemptEvent args)
{
if (component.Open)
{
args.Cancel();
return;
}
if (component.Contents.Contains(args.User))
{
var msg = Loc.GetString("entity-storage-component-already-contains-user-message");
Popup.PopupEntity(msg, args.User, args.User);
args.Cancel();
}
}
2023-11-19 17:44:42 +00:00
private void OnExploded(Entity<EntityStorageComponent> ent, ref BeforeExplodeEvent args)
{
args.Contents.AddRange(ent.Comp.Contents.ContainedEntities);
}
protected override void TakeGas(EntityUid uid, SharedEntityStorageComponent component)
2022-09-16 11:46:09 -07:00
{
if (!component.Airtight)
return;
var serverComp = (EntityStorageComponent) component;
var tile = GetOffsetTileRef(uid, serverComp);
2022-09-16 11:46:09 -07:00
if (tile != null && _atmos.GetTileMixture(tile.Value.GridUid, null, tile.Value.GridIndices, true) is {} environment)
{
_atmos.Merge(serverComp.Air, environment.RemoveVolume(serverComp.Air.Volume));
2022-09-16 11:46:09 -07:00
}
}
public override void ReleaseGas(EntityUid uid, SharedEntityStorageComponent component)
2022-09-16 11:46:09 -07:00
{
var serverComp = (EntityStorageComponent) component;
if (!serverComp.Airtight)
return;
var tile = GetOffsetTileRef(uid, serverComp);
2022-09-16 11:46:09 -07:00
if (tile != null && _atmos.GetTileMixture(tile.Value.GridUid, null, tile.Value.GridIndices, true) is {} environment)
{
_atmos.Merge(environment, serverComp.Air);
serverComp.Air.Clear();
2022-09-16 11:46:09 -07:00
}
}
private TileRef? GetOffsetTileRef(EntityUid uid, EntityStorageComponent component)
{
var targetCoordinates = new EntityCoordinates(uid, component.EnteringOffset).ToMap(EntityManager, TransformSystem);
2022-09-16 11:46:09 -07:00
if (_map.TryFindGridAt(targetCoordinates, out var gridId, out var grid))
2022-09-16 11:46:09 -07:00
{
return _mapSystem.GetTileRef(gridId, grid, targetCoordinates);
2022-09-16 11:46:09 -07:00
}
return null;
}
private void OnRemoved(EntityUid uid, InsideEntityStorageComponent component, EntGotRemovedFromContainerMessage args)
{
if (args.Container.Owner != component.Storage)
return;
RemComp(uid, component);
}
2022-09-16 11:46:09 -07:00
#region Gas mix event handlers
private void OnInsideInhale(EntityUid uid, InsideEntityStorageComponent component, InhaleLocationEvent args)
{
if (TryComp<EntityStorageComponent>(component.Storage, out var storage) && storage.Airtight)
2022-09-16 11:46:09 -07:00
{
args.Gas = storage.Air;
}
}
private void OnInsideExhale(EntityUid uid, InsideEntityStorageComponent component, ExhaleLocationEvent args)
{
if (TryComp<EntityStorageComponent>(component.Storage, out var storage) && storage.Airtight)
2022-09-16 11:46:09 -07:00
{
args.Gas = storage.Air;
}
}
private void OnInsideExposed(EntityUid uid, InsideEntityStorageComponent component, ref AtmosExposedGetAirEvent args)
{
if (args.Handled)
return;
if (TryComp<EntityStorageComponent>(component.Storage, out var storage))
{
if (!storage.Airtight)
return;
2022-09-16 11:46:09 -07:00
args.Gas = storage.Air;
}
args.Handled = true;
}
#endregion
2022-05-03 01:43:25 +03:00
}