Files
tbd-station-14/Content.Server/Power/Nodes/CableNode.cs
Vera Aguilera Puerto a5b57c8e10 Inline Transform
2021-12-03 14:20:34 +01:00

80 lines
3.0 KiB
C#

using System.Collections.Generic;
using Content.Server.NodeContainer.Nodes;
using Content.Server.Power.EntitySystems;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Power.Nodes
{
[DataDefinition]
public class CableNode : Node
{
public override IEnumerable<Node> GetReachableNodes()
{
if (!Anchored)
yield break;
var entMan = IoCManager.Resolve<IEntityManager>();
var grid = IoCManager.Resolve<IMapManager>().GetGrid(IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner.Uid).GridID);
var gridIndex = grid.TileIndicesFor(IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner.Uid).Coordinates);
// While we go over adjacent nodes, we build a list of blocked directions due to
// incoming or outgoing wire terminals.
var terminalDirs = 0;
List<(Direction, Node)> nodeDirs = new();
foreach (var (dir, node) in NodeHelpers.GetCardinalNeighborNodes(entMan, grid, gridIndex))
{
if (node is CableNode && node != this)
{
nodeDirs.Add((dir, node));
}
if (node is CableDeviceNode && dir == Direction.Invalid)
{
// device on same tile
nodeDirs.Add((Direction.Invalid, node));
}
if (node is CableTerminalNode)
{
if (dir == Direction.Invalid)
{
// On own tile, block direction it faces
terminalDirs |= 1 << (int) IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(node.Owner.Uid).LocalRotation.GetCardinalDir();
}
else
{
var terminalDir = IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(node.Owner.Uid).LocalRotation.GetCardinalDir();
if (terminalDir.GetOpposite() == dir)
{
// Target tile has a terminal towards us, block the direction.
terminalDirs |= 1 << (int) dir;
break;
}
}
}
}
foreach (var (dir, node) in nodeDirs)
{
// If there is a wire terminal connecting across this direction, skip the node.
if (dir != Direction.Invalid && (terminalDirs & (1 << (int) dir)) != 0)
continue;
yield return node;
}
}
public override void OnPostRebuild()
{
base.OnPostRebuild();
EntitySystem.Get<CableVisSystem>().QueueUpdate(Owner.Uid);
}
}
}