2021-06-19 13:25:05 +02:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
using Content.Server.Atmos.Piping.Binary.Components;
|
|
|
|
|
using Content.Server.Atmos.Piping.Unary.Components;
|
|
|
|
|
using Content.Server.NodeContainer;
|
2023-06-28 14:28:38 +03:00
|
|
|
using Content.Server.NodeContainer.EntitySystems;
|
2021-07-04 18:11:52 +02:00
|
|
|
using Content.Server.NodeContainer.Nodes;
|
2021-06-19 13:25:05 +02:00
|
|
|
using Content.Shared.Atmos.Piping.Unary.Components;
|
2022-04-02 16:52:44 +13:00
|
|
|
using Content.Shared.Construction.Components;
|
2021-06-19 13:25:05 +02:00
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
using Robust.Shared.Map;
|
2024-03-22 03:08:40 -04:00
|
|
|
using Robust.Shared.Map.Components;
|
2021-06-19 13:25:05 +02:00
|
|
|
|
|
|
|
|
namespace Content.Server.Atmos.Piping.Unary.EntitySystems
|
|
|
|
|
{
|
|
|
|
|
[UsedImplicitly]
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class GasPortableSystem : EntitySystem
|
2021-06-19 13:25:05 +02:00
|
|
|
{
|
2024-08-24 18:55:02 -07:00
|
|
|
[Dependency] private readonly SharedMapSystem _mapSystem = default!;
|
2023-02-02 17:34:53 +01:00
|
|
|
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
|
2023-06-28 14:28:38 +03:00
|
|
|
[Dependency] private readonly NodeContainerSystem _nodeContainer = default!;
|
2021-06-19 13:25:05 +02:00
|
|
|
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
|
|
|
|
SubscribeLocalEvent<GasPortableComponent, AnchorAttemptEvent>(OnPortableAnchorAttempt);
|
2022-04-24 13:54:25 +10:00
|
|
|
// Shouldn't need re-anchored event.
|
2022-01-13 06:49:28 +13:00
|
|
|
SubscribeLocalEvent<GasPortableComponent, AnchorStateChangedEvent>(OnAnchorChanged);
|
2021-06-19 13:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnPortableAnchorAttempt(EntityUid uid, GasPortableComponent component, AnchorAttemptEvent args)
|
|
|
|
|
{
|
2021-11-08 12:37:32 +01:00
|
|
|
if (!EntityManager.TryGetComponent(uid, out TransformComponent? transform))
|
2021-06-19 13:25:05 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// If we can't find any ports, cancel the anchoring.
|
2024-08-24 18:55:02 -07:00
|
|
|
if (!FindGasPortIn(transform.GridUid, transform.Coordinates, out _))
|
2021-06-19 13:25:05 +02:00
|
|
|
args.Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-13 06:49:28 +13:00
|
|
|
private void OnAnchorChanged(EntityUid uid, GasPortableComponent portable, ref AnchorStateChangedEvent args)
|
2021-06-19 13:25:05 +02:00
|
|
|
{
|
2024-03-30 17:17:53 +13:00
|
|
|
if (!_nodeContainer.TryGetNode(uid, portable.PortName, out PipeNode? portableNode))
|
2021-06-19 13:25:05 +02:00
|
|
|
return;
|
|
|
|
|
|
2022-01-13 06:49:28 +13:00
|
|
|
portableNode.ConnectionsEnabled = args.Anchored;
|
2021-06-19 13:25:05 +02:00
|
|
|
|
2021-09-28 13:35:29 +02:00
|
|
|
if (EntityManager.TryGetComponent(uid, out AppearanceComponent? appearance))
|
2021-06-19 13:25:05 +02:00
|
|
|
{
|
2023-02-02 17:34:53 +01:00
|
|
|
_appearance.SetData(uid, GasPortableVisuals.ConnectedState, args.Anchored, appearance);
|
2021-06-19 13:25:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 08:46:30 -04:00
|
|
|
public bool FindGasPortIn(EntityUid? gridId, EntityCoordinates coordinates, [NotNullWhen(true)] out GasPortComponent? port)
|
2021-06-19 13:25:05 +02:00
|
|
|
{
|
|
|
|
|
port = null;
|
|
|
|
|
|
2024-03-22 03:08:40 -04:00
|
|
|
if (!TryComp<MapGridComponent>(gridId, out var grid))
|
2021-06-19 13:25:05 +02:00
|
|
|
return false;
|
|
|
|
|
|
2024-08-24 18:55:02 -07:00
|
|
|
foreach (var entityUid in _mapSystem.GetLocal(gridId.Value, grid, coordinates))
|
2021-06-19 13:25:05 +02:00
|
|
|
{
|
2023-01-19 03:56:45 +01:00
|
|
|
if (EntityManager.TryGetComponent(entityUid, out port))
|
2021-06-19 13:25:05 +02:00
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|