Reduce node resolves (#6435)

This commit is contained in:
Leon Friedrich
2022-02-07 01:10:33 +13:00
committed by GitHub
parent b82926e324
commit 1e10314900
12 changed files with 195 additions and 147 deletions

View File

@@ -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<Node> GetReachableNodes()
public override IEnumerable<Node> GetReachableNodes(TransformComponent xform,
EntityQuery<NodeContainerComponent> nodeQuery,
EntityQuery<TransformComponent> xformQuery,
IMapGrid? grid,
IEntityManager entMan)
{
if (!Anchored)
if (!xform.Anchored || grid == null)
yield break;
var entMan = IoCManager.Resolve<IEntityManager>();
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<IMapManager>().TryGetGrid(IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner).GridID, out var grid))
yield break;
var gridIndex = grid.TileIndicesFor(IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(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;

View File

@@ -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<Node> GetReachableNodes()
public override IEnumerable<Node> GetReachableNodes(TransformComponent xform,
EntityQuery<NodeContainerComponent> nodeQuery,
EntityQuery<TransformComponent> xformQuery,
IMapGrid? grid,
IEntityManager entMan)
{
if (!Anchored)
if (!xform.Anchored || grid == null)
yield break;
var entMan = IoCManager.Resolve<IEntityManager>();
var grid = IoCManager.Resolve<IMapManager>().GetGrid(IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner).GridID);
var gridIndex = grid.TileIndicesFor(IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(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<IEntityManager>().GetComponent<TransformComponent>(node.Owner).LocalRotation.GetCardinalDir();
terminalDirs |= 1 << (int) xformQuery.GetComponent(node.Owner).LocalRotation.GetCardinalDir();
}
else
{
var terminalDir = IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(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.

View File

@@ -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<Node> GetReachableNodes()
public override IEnumerable<Node> GetReachableNodes(TransformComponent xform,
EntityQuery<NodeContainerComponent> nodeQuery,
EntityQuery<TransformComponent> xformQuery,
IMapGrid? grid,
IEntityManager entMan)
{
if (!Anchored)
if (!xform.Anchored || grid == null)
yield break;
if (IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(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<IEntityManager>();
var grid = IoCManager.Resolve<IMapManager>().GetGrid(IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner).GridID);
var gridIndex = grid.TileIndicesFor(IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner).Coordinates);
var dir = IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(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;
}

View File

@@ -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<Node> GetReachableNodes()
public override IEnumerable<Node> GetReachableNodes(TransformComponent xform,
EntityQuery<NodeContainerComponent> nodeQuery,
EntityQuery<TransformComponent> xformQuery,
IMapGrid? grid,
IEntityManager entMan)
{
if (!Anchored)
if (!xform.Anchored || grid == null)
yield break;
if (IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner).GridID == GridId.Invalid)
yield break; // No funny nodes in spess.
var gridIndex = grid.TileIndicesFor(xform.Coordinates);
var entMan = IoCManager.Resolve<IEntityManager>();
var grid = IoCManager.Resolve<IMapManager>().GetGrid(IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner).GridID);
var gridIndex = grid.TileIndicesFor(IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(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)