From 1e10314900ef9171aff02f32dc2e983c6b78094b Mon Sep 17 00:00:00 2001 From: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> Date: Mon, 7 Feb 2022 01:10:33 +1300 Subject: [PATCH] Reduce node resolves (#6435) --- .../Electrocution/ElectrocutionNode.cs | 14 ++- .../EntitySystems/NodeGroupSystem.cs | 26 +++- .../NodeContainer/Nodes/AdjacentNode.cs | 14 ++- Content.Server/NodeContainer/Nodes/Node.cs | 22 +++- .../NodeContainer/Nodes/NodeHelpers.cs | 27 +--- .../NodeContainer/Nodes/PipeNode.cs | 117 +++++++++--------- .../NodeContainer/Nodes/PortPipeNode.cs | 17 ++- .../NodeContainer/Nodes/PortablePipeNode.cs | 17 ++- Content.Server/Power/Nodes/CableDeviceNode.cs | 20 ++- Content.Server/Power/Nodes/CableNode.cs | 22 ++-- .../Power/Nodes/CableTerminalNode.cs | 27 ++-- .../Power/Nodes/CableTerminalPortNode.cs | 19 ++- 12 files changed, 195 insertions(+), 147 deletions(-) diff --git a/Content.Server/Electrocution/ElectrocutionNode.cs b/Content.Server/Electrocution/ElectrocutionNode.cs index c84cbfd789..f0872f54fa 100644 --- a/Content.Server/Electrocution/ElectrocutionNode.cs +++ b/Content.Server/Electrocution/ElectrocutionNode.cs @@ -1,9 +1,8 @@ -using System.Collections.Generic; +using System.Collections.Generic; using Content.Server.NodeContainer; using Content.Server.NodeContainer.Nodes; -using Content.Server.Power.Nodes; using Robust.Shared.GameObjects; -using Robust.Shared.IoC; +using Robust.Shared.Map; using Robust.Shared.Serialization.Manager.Attributes; namespace Content.Server.Electrocution @@ -16,10 +15,13 @@ namespace Content.Server.Electrocution [DataField("node")] public string NodeName = default!; - public override IEnumerable GetReachableNodes() + public override IEnumerable GetReachableNodes(TransformComponent xform, + EntityQuery nodeQuery, + EntityQuery xformQuery, + IMapGrid? grid, + IEntityManager entMan) { - var ent = IoCManager.Resolve(); - if (!ent.TryGetComponent(CableEntity, out NodeContainerComponent? nodeContainer)) + if (!nodeQuery.TryGetComponent(CableEntity, out var nodeContainer)) yield break; if (nodeContainer.TryGetNode(NodeName, out Node? node)) diff --git a/Content.Server/NodeContainer/EntitySystems/NodeGroupSystem.cs b/Content.Server/NodeContainer/EntitySystems/NodeGroupSystem.cs index eece6b6a04..c205c6e4b3 100644 --- a/Content.Server/NodeContainer/EntitySystems/NodeGroupSystem.cs +++ b/Content.Server/NodeContainer/EntitySystems/NodeGroupSystem.cs @@ -12,6 +12,7 @@ using Robust.Shared.Enums; using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Log; +using Robust.Shared.Map; using Robust.Shared.Maths; using Robust.Shared.Utility; @@ -28,6 +29,7 @@ namespace Content.Server.NodeContainer.EntitySystems [Dependency] private readonly IAdminManager _adminManager = default!; [Dependency] private readonly INodeGroupFactory _nodeGroupFactory = default!; [Dependency] private readonly ILogManager _logManager = default!; + [Dependency] private readonly IMapManager _mapManager = default!; private readonly List _visDeletes = new(); private readonly List _visSends = new(); @@ -141,6 +143,9 @@ namespace Content.Server.NodeContainer.EntitySystems var sw = Stopwatch.StartNew(); + var xformQuery = EntityManager.GetEntityQuery(); + var nodeQuery = EntityManager.GetEntityQuery(); + foreach (var toRemove in _toRemove) { if (toRemove.NodeGroup == null) @@ -182,7 +187,11 @@ namespace Content.Server.NodeContainer.EntitySystems QueueRemakeGroup((BaseNodeGroup) node.NodeGroup); } - foreach (var compatible in GetCompatibleNodes(node)) + // GetCompatibleNodes will involve getting the transform & grid as most connection requirements are + // based on position & anchored neighbours However, here more than one node could be attached to the + // same parent. So there is probably a better way of doing this. + + foreach (var compatible in GetCompatibleNodes(node, xformQuery, nodeQuery)) { ClearReachableIfNecessary(compatible); @@ -303,14 +312,23 @@ namespace Content.Server.NodeContainer.EntitySystems return allNodes; } - private static IEnumerable GetCompatibleNodes(Node node) + private IEnumerable GetCompatibleNodes(Node node, EntityQuery xformQuery, EntityQuery nodeQuery) { - foreach (var reachable in node.GetReachableNodes()) + var xform = xformQuery.GetComponent(node.Owner); + _mapManager.TryGetGrid(xform.GridID, out var grid); + + if (!node.Connectable(EntityManager, xform)) + yield break; + + foreach (var reachable in node.GetReachableNodes(xform, nodeQuery, xformQuery, grid, EntityManager)) { DebugTools.Assert(reachable != node, "GetReachableNodes() should not include self."); - if (reachable.Connectable && reachable.NodeGroupID == node.NodeGroupID) + if (reachable.NodeGroupID == node.NodeGroupID + && reachable.Connectable(EntityManager, xformQuery.GetComponent(reachable.Owner))) + { yield return reachable; + } } } diff --git a/Content.Server/NodeContainer/Nodes/AdjacentNode.cs b/Content.Server/NodeContainer/Nodes/AdjacentNode.cs index c3a08ccc13..c46b22b987 100644 --- a/Content.Server/NodeContainer/Nodes/AdjacentNode.cs +++ b/Content.Server/NodeContainer/Nodes/AdjacentNode.cs @@ -12,16 +12,18 @@ namespace Content.Server.NodeContainer.Nodes [DataDefinition] public class AdjacentNode : Node { - public override IEnumerable GetReachableNodes() + public override IEnumerable GetReachableNodes(TransformComponent xform, + EntityQuery nodeQuery, + EntityQuery xformQuery, + IMapGrid? grid, + IEntityManager entMan) { - if (!IoCManager.Resolve().GetComponent(Owner).Anchored) + if (!xform.Anchored || grid == null) yield break; - var entMan = IoCManager.Resolve(); - var grid = IoCManager.Resolve().GetGrid(IoCManager.Resolve().GetComponent(Owner).GridID); - var gridIndex = grid.TileIndicesFor(IoCManager.Resolve().GetComponent(Owner).Coordinates); + var gridIndex = grid.TileIndicesFor(xform.Coordinates); - foreach (var (_, node) in NodeHelpers.GetCardinalNeighborNodes(entMan, grid, gridIndex)) + foreach (var (_, node) in NodeHelpers.GetCardinalNeighborNodes(nodeQuery, grid, gridIndex)) { if (node != this) yield return node; diff --git a/Content.Server/NodeContainer/Nodes/Node.cs b/Content.Server/NodeContainer/Nodes/Node.cs index 2de408c15c..06391f693c 100644 --- a/Content.Server/NodeContainer/Nodes/Node.cs +++ b/Content.Server/NodeContainer/Nodes/Node.cs @@ -3,6 +3,7 @@ using Content.Server.NodeContainer.EntitySystems; using Content.Server.NodeContainer.NodeGroups; using Robust.Shared.GameObjects; using Robust.Shared.IoC; +using Robust.Shared.Map; using Robust.Shared.Serialization.Manager.Attributes; using Robust.Shared.ViewVariables; @@ -36,7 +37,20 @@ namespace Content.Server.NodeContainer.Nodes /// /// If this node should be considered for connection by other nodes. /// - public bool Connectable => !Deleting && Anchored; + public virtual bool Connectable(IEntityManager entMan, TransformComponent? xform = null) + { + if (Deleting) + return false; + + if (entMan.IsQueuedForDeletion(Owner)) + return false; + + if (!NeedAnchored) + return true; + + xform ??= entMan.GetComponent(Owner); + return xform.Anchored; + } protected bool Anchored => !NeedAnchored || IoCManager.Resolve().GetComponent(Owner).Anchored; @@ -145,6 +159,10 @@ namespace Content.Server.NodeContainer.Nodes /// of this asymmetric relation are made to manually update with . /// /// - public abstract IEnumerable GetReachableNodes(); + public abstract IEnumerable GetReachableNodes(TransformComponent xform, + EntityQuery nodeQuery, + EntityQuery xformQuery, + IMapGrid? grid, + IEntityManager entMan); } } diff --git a/Content.Server/NodeContainer/Nodes/NodeHelpers.cs b/Content.Server/NodeContainer/Nodes/NodeHelpers.cs index 6475d6fa93..1bc0d0d377 100644 --- a/Content.Server/NodeContainer/Nodes/NodeHelpers.cs +++ b/Content.Server/NodeContainer/Nodes/NodeHelpers.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Robust.Shared.GameObjects; @@ -12,11 +12,11 @@ namespace Content.Server.NodeContainer.Nodes /// public static class NodeHelpers { - public static IEnumerable GetNodesInTile(IEntityManager entMan, IMapGrid grid, Vector2i coords) + public static IEnumerable GetNodesInTile(EntityQuery nodeQuery, IMapGrid grid, Vector2i coords) { foreach (var entityUid in grid.GetAnchoredEntities(coords)) { - if (!entMan.TryGetComponent(entityUid, out NodeContainerComponent? container)) + if (!nodeQuery.TryGetComponent(entityUid, out var container)) continue; foreach (var node in container.Nodes.Values) @@ -27,14 +27,14 @@ namespace Content.Server.NodeContainer.Nodes } public static IEnumerable<(Direction dir, Node node)> GetCardinalNeighborNodes( - IEntityManager entMan, + EntityQuery nodeQuery, IMapGrid grid, Vector2i coords, bool includeSameTile = true) { foreach (var (dir, entityUid) in GetCardinalNeighborCells(grid, coords, includeSameTile)) { - if (!entMan.TryGetComponent(entityUid, out NodeContainerComponent? container)) + if (!nodeQuery.TryGetComponent(entityUid, out var container)) continue; foreach (var node in container.Nodes.Values) @@ -68,22 +68,5 @@ namespace Content.Server.NodeContainer.Nodes foreach (var uid in grid.GetAnchoredEntities(coords + (-1, 0))) yield return (Direction.West, uid); } - - public static Vector2i TileOffsetForDir(Direction dir) - { - return dir switch - { - Direction.Invalid => (0, 0), - Direction.South => (0, -1), - Direction.SouthEast => (1, -1), - Direction.East => (1, 0), - Direction.NorthEast => (1, 1), - Direction.North => (0, 1), - Direction.NorthWest => (-1, 1), - Direction.West => (-1, 0), - Direction.SouthWest => (-1, -1), - _ => throw new ArgumentOutOfRangeException(nameof(dir), dir, null) - }; - } } } diff --git a/Content.Server/NodeContainer/Nodes/PipeNode.cs b/Content.Server/NodeContainer/Nodes/PipeNode.cs index 48cc68fe23..fd3d033e47 100644 --- a/Content.Server/NodeContainer/Nodes/PipeNode.cs +++ b/Content.Server/NodeContainer/Nodes/PipeNode.cs @@ -7,6 +7,7 @@ using Content.Shared.Atmos; using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Map; +using Robust.Shared.Maths; using Robust.Shared.Serialization.Manager.Attributes; using Robust.Shared.Utility; using Robust.Shared.ViewVariables; @@ -89,6 +90,11 @@ namespace Content.Server.NodeContainer.Nodes [DataField("connectionsEnabled")] private bool _connectionsEnabled = true; + public override bool Connectable(IEntityManager entMan, TransformComponent? xform = null) + { + return _connectionsEnabled && base.Connectable(entMan, xform); + } + [DataField("rotationsEnabled")] public bool RotationsEnabled { get; set; } = true; @@ -144,25 +150,16 @@ namespace Content.Server.NodeContainer.Nodes UpdateAppearance(); } - public override IEnumerable GetReachableNodes() + public override IEnumerable GetReachableNodes(TransformComponent xform, + EntityQuery nodeQuery, + EntityQuery xformQuery, + IMapGrid? grid, + IEntityManager entMan) { - for (var i = 0; i < PipeDirectionHelpers.AllPipeDirections; i++) - { - var pipeDir = (PipeDirection) (1 << i); - - if (!CurrentPipeDirection.HasDirection(pipeDir)) - continue; - - foreach (var pipe in LinkableNodesInDirection(pipeDir)) - { - yield return pipe; - } - } - - if(_alwaysReachable != null) + if (_alwaysReachable != null) { var remQ = new RemQueue(); - foreach(var pipe in _alwaysReachable) + foreach (var pipe in _alwaysReachable) { if (pipe.Deleting) { @@ -171,64 +168,58 @@ namespace Content.Server.NodeContainer.Nodes yield return pipe; } - foreach(var pipe in remQ) + foreach (var pipe in remQ) { _alwaysReachable.Remove(pipe); } } + + if (!xform.Anchored || grid == null) + yield break; + + var pos = grid.TileIndicesFor(xform.Coordinates); + + for (var i = 0; i < PipeDirectionHelpers.PipeDirections; i++) + { + var pipeDir = (PipeDirection) (1 << i); + + if (!CurrentPipeDirection.HasDirection(pipeDir)) + continue; + + foreach (var pipe in LinkableNodesInDirection(pos, pipeDir, grid, nodeQuery)) + { + yield return pipe; + } + } } /// /// Gets the pipes that can connect to us from entities on the tile or adjacent in a direction. /// - private IEnumerable LinkableNodesInDirection(PipeDirection pipeDir) + private IEnumerable LinkableNodesInDirection(Vector2i pos, PipeDirection pipeDir, IMapGrid grid, + EntityQuery nodeQuery) { - if (!Anchored) - yield break; - - foreach (var pipe in PipesInDirection(pipeDir)) + foreach (var pipe in PipesInDirection(pos, pipeDir, grid, nodeQuery)) { - if (pipe.ConnectionsEnabled && pipe.CurrentPipeDirection.HasDirection(pipeDir.GetOpposite())) + if (pipe.NodeGroupID == NodeGroupID + && pipe.CurrentPipeDirection.HasDirection(pipeDir.GetOpposite())) + { yield return pipe; + } } } /// /// Gets the pipes from entities on the tile adjacent in a direction. /// - protected IEnumerable PipesInDirection(PipeDirection pipeDir) + protected IEnumerable PipesInDirection(Vector2i pos, PipeDirection pipeDir, IMapGrid grid, + EntityQuery nodeQuery) { - if (!IoCManager.Resolve().GetComponent(Owner).Anchored) - yield break; + var offsetPos = pos.Offset(pipeDir.ToDirection()); - var grid = IoCManager.Resolve().GetGrid(IoCManager.Resolve().GetComponent(Owner).GridID); - var position = IoCManager.Resolve().GetComponent(Owner).Coordinates; - foreach (var entity in grid.GetInDir(position, pipeDir.ToDirection())) + foreach (var entity in grid.GetAnchoredEntities(offsetPos)) { - if (!IoCManager.Resolve().TryGetComponent(entity, out var container)) - continue; - - foreach (var node in container.Nodes.Values) - { - if (node is PipeNode pipe) - yield return pipe; - } - } - } - - /// - /// Gets the pipes from entities on the same tile. - /// - protected IEnumerable PipesInTile() - { - if (!IoCManager.Resolve().GetComponent(Owner).Anchored) - yield break; - - var grid = IoCManager.Resolve().GetGrid(IoCManager.Resolve().GetComponent(Owner).GridID); - var position = IoCManager.Resolve().GetComponent(Owner).Coordinates; - foreach (var entity in grid.GetLocal(position)) - { - if (!IoCManager.Resolve().TryGetComponent(entity, out var container)) + if (!nodeQuery.TryGetComponent(entity, out var container)) continue; foreach (var node in container.Nodes.Values) @@ -265,6 +256,13 @@ namespace Content.Server.NodeContainer.Nodes { ConnectedDirections = PipeDirection.None; + var entMan = IoCManager.Resolve(); + var xform = entMan.GetComponent(Owner); + if (!IoCManager.Resolve().TryGetGrid(xform.GridID, out var grid)) + return; + var pos = grid.WorldToTile(xform.WorldPosition); + var query = entMan.GetEntityQuery(); + for (var i = 0; i < PipeDirectionHelpers.AllPipeDirections; i++) { var pipeDir = (PipeDirection) (1 << i); @@ -272,9 +270,9 @@ namespace Content.Server.NodeContainer.Nodes if (!CurrentPipeDirection.HasDirection(pipeDir)) continue; - foreach (var pipe in LinkableNodesInDirection(pipeDir)) + foreach (var pipe in LinkableNodesInDirection(pos, pipeDir, grid, query)) { - if (pipe.Connectable && pipe.NodeGroupID == NodeGroupID) + if (pipe.Connectable(entMan) && pipe.NodeGroupID == NodeGroupID) { ConnectedDirections |= pipeDir; break; @@ -289,11 +287,18 @@ namespace Content.Server.NodeContainer.Nodes /// private void UpdateAdjacentConnectedDirections() { + var entMan = IoCManager.Resolve(); + var xform = entMan.GetComponent(Owner); + if (!IoCManager.Resolve().TryGetGrid(xform.GridID, out var grid)) + return; + var pos = grid.WorldToTile(xform.WorldPosition); + var query = entMan.GetEntityQuery(); + for (var i = 0; i < PipeDirectionHelpers.PipeDirections; i++) { var pipeDir = (PipeDirection) (1 << i); - foreach (var pipe in LinkableNodesInDirection(pipeDir)) + foreach (var pipe in LinkableNodesInDirection(pos, pipeDir, grid, query)) { pipe.UpdateConnectedDirections(); pipe.UpdateAppearance(); diff --git a/Content.Server/NodeContainer/Nodes/PortPipeNode.cs b/Content.Server/NodeContainer/Nodes/PortPipeNode.cs index 18c65173f8..6a029a4c88 100644 --- a/Content.Server/NodeContainer/Nodes/PortPipeNode.cs +++ b/Content.Server/NodeContainer/Nodes/PortPipeNode.cs @@ -1,4 +1,6 @@ using System.Collections.Generic; +using Robust.Shared.GameObjects; +using Robust.Shared.Map; using Robust.Shared.Serialization.Manager.Attributes; namespace Content.Server.NodeContainer.Nodes @@ -6,15 +8,24 @@ namespace Content.Server.NodeContainer.Nodes [DataDefinition] public class PortPipeNode : PipeNode { - public override IEnumerable GetReachableNodes() + public override IEnumerable GetReachableNodes(TransformComponent xform, + EntityQuery nodeQuery, + EntityQuery xformQuery, + IMapGrid? grid, + IEntityManager entMan) { - foreach (var node in PipesInTile()) + if (!xform.Anchored || grid == null) + yield break; + + var gridIndex = grid.TileIndicesFor(xform.Coordinates); + + foreach (var node in NodeHelpers.GetNodesInTile(nodeQuery, grid, gridIndex)) { if (node is PortablePipeNode) yield return node; } - foreach (var node in base.GetReachableNodes()) + foreach (var node in base.GetReachableNodes(xform, nodeQuery, xformQuery, grid, entMan)) { yield return node; } diff --git a/Content.Server/NodeContainer/Nodes/PortablePipeNode.cs b/Content.Server/NodeContainer/Nodes/PortablePipeNode.cs index 7f645cb518..7b6ef85e34 100644 --- a/Content.Server/NodeContainer/Nodes/PortablePipeNode.cs +++ b/Content.Server/NodeContainer/Nodes/PortablePipeNode.cs @@ -1,4 +1,6 @@ using System.Collections.Generic; +using Robust.Shared.GameObjects; +using Robust.Shared.Map; using Robust.Shared.Serialization.Manager.Attributes; namespace Content.Server.NodeContainer.Nodes @@ -6,15 +8,24 @@ namespace Content.Server.NodeContainer.Nodes [DataDefinition] public class PortablePipeNode : PipeNode { - public override IEnumerable GetReachableNodes() + public override IEnumerable GetReachableNodes(TransformComponent xform, + EntityQuery nodeQuery, + EntityQuery xformQuery, + IMapGrid? grid, + IEntityManager entMan) { - foreach (var node in PipesInTile()) + if (!xform.Anchored || grid == null) + yield break; + + var gridIndex = grid.TileIndicesFor(xform.Coordinates); + + foreach (var node in NodeHelpers.GetNodesInTile(nodeQuery, grid, gridIndex)) { if (node is PortPipeNode) yield return node; } - foreach (var node in base.GetReachableNodes()) + foreach (var node in base.GetReachableNodes(xform, nodeQuery, xformQuery, grid, entMan)) { yield return node; } diff --git a/Content.Server/Power/Nodes/CableDeviceNode.cs b/Content.Server/Power/Nodes/CableDeviceNode.cs index cbefeead6d..e89e15761c 100644 --- a/Content.Server/Power/Nodes/CableDeviceNode.cs +++ b/Content.Server/Power/Nodes/CableDeviceNode.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; +using Content.Server.NodeContainer; using Content.Server.NodeContainer.Nodes; using Robust.Shared.GameObjects; -using Robust.Shared.IoC; using Robust.Shared.Map; using Robust.Shared.Serialization.Manager.Attributes; @@ -13,20 +13,18 @@ namespace Content.Server.Power.Nodes [DataDefinition] public class CableDeviceNode : Node { - public override IEnumerable GetReachableNodes() + public override IEnumerable GetReachableNodes(TransformComponent xform, + EntityQuery nodeQuery, + EntityQuery xformQuery, + IMapGrid? grid, + IEntityManager entMan) { - if (!Anchored) + if (!xform.Anchored || grid == null) yield break; - var entMan = IoCManager.Resolve(); + var gridIndex = grid.TileIndicesFor(xform.Coordinates); - // If we're in an invalid grid, such as grid 0, we cannot connect to anything. - if(!IoCManager.Resolve().TryGetGrid(IoCManager.Resolve().GetComponent(Owner).GridID, out var grid)) - yield break; - - var gridIndex = grid.TileIndicesFor(IoCManager.Resolve().GetComponent(Owner).Coordinates); - - foreach (var node in NodeHelpers.GetNodesInTile(entMan, grid, gridIndex)) + foreach (var node in NodeHelpers.GetNodesInTile(nodeQuery, grid, gridIndex)) { if (node is CableNode) yield return node; diff --git a/Content.Server/Power/Nodes/CableNode.cs b/Content.Server/Power/Nodes/CableNode.cs index 9d1143b2a9..d315f7331c 100644 --- a/Content.Server/Power/Nodes/CableNode.cs +++ b/Content.Server/Power/Nodes/CableNode.cs @@ -1,8 +1,8 @@ -using System.Collections.Generic; +using System.Collections.Generic; +using Content.Server.NodeContainer; 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; @@ -12,21 +12,23 @@ namespace Content.Server.Power.Nodes [DataDefinition] public class CableNode : Node { - public override IEnumerable GetReachableNodes() + public override IEnumerable GetReachableNodes(TransformComponent xform, + EntityQuery nodeQuery, + EntityQuery xformQuery, + IMapGrid? grid, + IEntityManager entMan) { - if (!Anchored) + if (!xform.Anchored || grid == null) yield break; - var entMan = IoCManager.Resolve(); - var grid = IoCManager.Resolve().GetGrid(IoCManager.Resolve().GetComponent(Owner).GridID); - var gridIndex = grid.TileIndicesFor(IoCManager.Resolve().GetComponent(Owner).Coordinates); + var gridIndex = grid.TileIndicesFor(xform.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)) + foreach (var (dir, node) in NodeHelpers.GetCardinalNeighborNodes(nodeQuery, grid, gridIndex)) { if (node is CableNode && node != this) { @@ -44,11 +46,11 @@ namespace Content.Server.Power.Nodes if (dir == Direction.Invalid) { // On own tile, block direction it faces - terminalDirs |= 1 << (int) IoCManager.Resolve().GetComponent(node.Owner).LocalRotation.GetCardinalDir(); + terminalDirs |= 1 << (int) xformQuery.GetComponent(node.Owner).LocalRotation.GetCardinalDir(); } else { - var terminalDir = IoCManager.Resolve().GetComponent(node.Owner).LocalRotation.GetCardinalDir(); + var terminalDir = xformQuery.GetComponent(node.Owner).LocalRotation.GetCardinalDir(); if (terminalDir.GetOpposite() == dir) { // Target tile has a terminal towards us, block the direction. diff --git a/Content.Server/Power/Nodes/CableTerminalNode.cs b/Content.Server/Power/Nodes/CableTerminalNode.cs index 6df4196cb8..1c2d138b5d 100644 --- a/Content.Server/Power/Nodes/CableTerminalNode.cs +++ b/Content.Server/Power/Nodes/CableTerminalNode.cs @@ -1,8 +1,9 @@ using System.Collections.Generic; +using Content.Server.NodeContainer; using Content.Server.NodeContainer.Nodes; 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 @@ -10,29 +11,27 @@ namespace Content.Server.Power.Nodes [DataDefinition] public class CableTerminalNode : CableDeviceNode { - public override IEnumerable GetReachableNodes() + public override IEnumerable GetReachableNodes(TransformComponent xform, + EntityQuery nodeQuery, + EntityQuery xformQuery, + IMapGrid? grid, + IEntityManager entMan) { - if (!Anchored) + if (!xform.Anchored || grid == null) yield break; - if (IoCManager.Resolve().GetComponent(Owner).GridID == GridId.Invalid) - yield break; // No funny nodes in spess. + var gridIndex = grid.TileIndicesFor(xform.Coordinates); + var dir = xform.LocalRotation.GetDir(); + var targetIdx = gridIndex.Offset(dir); - var entMan = IoCManager.Resolve(); - var grid = IoCManager.Resolve().GetGrid(IoCManager.Resolve().GetComponent(Owner).GridID); - var gridIndex = grid.TileIndicesFor(IoCManager.Resolve().GetComponent(Owner).Coordinates); - - var dir = IoCManager.Resolve().GetComponent(Owner).LocalRotation.GetDir(); - var targetIdx = gridIndex + NodeHelpers.TileOffsetForDir(dir); - - foreach (var node in NodeHelpers.GetNodesInTile(entMan, grid, targetIdx)) + foreach (var node in NodeHelpers.GetNodesInTile(nodeQuery, grid, targetIdx)) { if (node is CableTerminalPortNode) yield return node; } - foreach (var node in base.GetReachableNodes()) + foreach (var node in base.GetReachableNodes(xform, nodeQuery, xformQuery, grid, entMan)) { yield return node; } diff --git a/Content.Server/Power/Nodes/CableTerminalPortNode.cs b/Content.Server/Power/Nodes/CableTerminalPortNode.cs index 01f090185e..ea8f44dc12 100644 --- a/Content.Server/Power/Nodes/CableTerminalPortNode.cs +++ b/Content.Server/Power/Nodes/CableTerminalPortNode.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; +using Content.Server.NodeContainer; using Content.Server.NodeContainer.Nodes; using Robust.Shared.GameObjects; -using Robust.Shared.IoC; using Robust.Shared.Map; using Robust.Shared.Serialization.Manager.Attributes; @@ -10,19 +10,18 @@ namespace Content.Server.Power.Nodes [DataDefinition] public class CableTerminalPortNode : Node { - public override IEnumerable GetReachableNodes() + public override IEnumerable GetReachableNodes(TransformComponent xform, + EntityQuery nodeQuery, + EntityQuery xformQuery, + IMapGrid? grid, + IEntityManager entMan) { - if (!Anchored) + if (!xform.Anchored || grid == null) yield break; - if (IoCManager.Resolve().GetComponent(Owner).GridID == GridId.Invalid) - yield break; // No funny nodes in spess. + var gridIndex = grid.TileIndicesFor(xform.Coordinates); - var entMan = IoCManager.Resolve(); - var grid = IoCManager.Resolve().GetGrid(IoCManager.Resolve().GetComponent(Owner).GridID); - var gridIndex = grid.TileIndicesFor(IoCManager.Resolve().GetComponent(Owner).Coordinates); - - var nodes = NodeHelpers.GetCardinalNeighborNodes(entMan, grid, gridIndex, includeSameTile: false); + var nodes = NodeHelpers.GetCardinalNeighborNodes(nodeQuery, grid, gridIndex, includeSameTile: false); foreach (var (_, node) in nodes) { if (node is CableTerminalNode)