Reduce node resolves (#6435)
This commit is contained in:
@@ -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<int> _visDeletes = new();
|
||||
private readonly List<BaseNodeGroup> _visSends = new();
|
||||
@@ -141,6 +143,9 @@ namespace Content.Server.NodeContainer.EntitySystems
|
||||
|
||||
var sw = Stopwatch.StartNew();
|
||||
|
||||
var xformQuery = EntityManager.GetEntityQuery<TransformComponent>();
|
||||
var nodeQuery = EntityManager.GetEntityQuery<NodeContainerComponent>();
|
||||
|
||||
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<Node> GetCompatibleNodes(Node node)
|
||||
private IEnumerable<Node> GetCompatibleNodes(Node node, EntityQuery<TransformComponent> xformQuery, EntityQuery<NodeContainerComponent> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,16 +12,18 @@ namespace Content.Server.NodeContainer.Nodes
|
||||
[DataDefinition]
|
||||
public class AdjacentNode : Node
|
||||
{
|
||||
public override IEnumerable<Node> GetReachableNodes()
|
||||
public override IEnumerable<Node> GetReachableNodes(TransformComponent xform,
|
||||
EntityQuery<NodeContainerComponent> nodeQuery,
|
||||
EntityQuery<TransformComponent> xformQuery,
|
||||
IMapGrid? grid,
|
||||
IEntityManager entMan)
|
||||
{
|
||||
if (!IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner).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);
|
||||
|
||||
foreach (var (_, node) in NodeHelpers.GetCardinalNeighborNodes(entMan, grid, gridIndex))
|
||||
foreach (var (_, node) in NodeHelpers.GetCardinalNeighborNodes(nodeQuery, grid, gridIndex))
|
||||
{
|
||||
if (node != this)
|
||||
yield return node;
|
||||
|
||||
@@ -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
|
||||
/// <summary>
|
||||
/// If this node should be considered for connection by other nodes.
|
||||
/// </summary>
|
||||
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<TransformComponent>(Owner);
|
||||
return xform.Anchored;
|
||||
}
|
||||
|
||||
protected bool Anchored => !NeedAnchored || IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner).Anchored;
|
||||
|
||||
@@ -145,6 +159,10 @@ namespace Content.Server.NodeContainer.Nodes
|
||||
/// of this asymmetric relation are made to manually update with <see cref="NodeGroupSystem.QueueReflood"/>.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public abstract IEnumerable<Node> GetReachableNodes();
|
||||
public abstract IEnumerable<Node> GetReachableNodes(TransformComponent xform,
|
||||
EntityQuery<NodeContainerComponent> nodeQuery,
|
||||
EntityQuery<TransformComponent> xformQuery,
|
||||
IMapGrid? grid,
|
||||
IEntityManager entMan);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
/// </summary>
|
||||
public static class NodeHelpers
|
||||
{
|
||||
public static IEnumerable<Node> GetNodesInTile(IEntityManager entMan, IMapGrid grid, Vector2i coords)
|
||||
public static IEnumerable<Node> GetNodesInTile(EntityQuery<NodeContainerComponent> 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<NodeContainerComponent> 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)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Node> GetReachableNodes()
|
||||
public override IEnumerable<Node> GetReachableNodes(TransformComponent xform,
|
||||
EntityQuery<NodeContainerComponent> nodeQuery,
|
||||
EntityQuery<TransformComponent> 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<PipeNode>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the pipes that can connect to us from entities on the tile or adjacent in a direction.
|
||||
/// </summary>
|
||||
private IEnumerable<PipeNode> LinkableNodesInDirection(PipeDirection pipeDir)
|
||||
private IEnumerable<PipeNode> LinkableNodesInDirection(Vector2i pos, PipeDirection pipeDir, IMapGrid grid,
|
||||
EntityQuery<NodeContainerComponent> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the pipes from entities on the tile adjacent in a direction.
|
||||
/// </summary>
|
||||
protected IEnumerable<PipeNode> PipesInDirection(PipeDirection pipeDir)
|
||||
protected IEnumerable<PipeNode> PipesInDirection(Vector2i pos, PipeDirection pipeDir, IMapGrid grid,
|
||||
EntityQuery<NodeContainerComponent> nodeQuery)
|
||||
{
|
||||
if (!IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner).Anchored)
|
||||
yield break;
|
||||
var offsetPos = pos.Offset(pipeDir.ToDirection());
|
||||
|
||||
var grid = IoCManager.Resolve<IMapManager>().GetGrid(IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner).GridID);
|
||||
var position = IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner).Coordinates;
|
||||
foreach (var entity in grid.GetInDir(position, pipeDir.ToDirection()))
|
||||
foreach (var entity in grid.GetAnchoredEntities(offsetPos))
|
||||
{
|
||||
if (!IoCManager.Resolve<IEntityManager>().TryGetComponent<NodeContainerComponent>(entity, out var container))
|
||||
continue;
|
||||
|
||||
foreach (var node in container.Nodes.Values)
|
||||
{
|
||||
if (node is PipeNode pipe)
|
||||
yield return pipe;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the pipes from entities on the same tile.
|
||||
/// </summary>
|
||||
protected IEnumerable<PipeNode> PipesInTile()
|
||||
{
|
||||
if (!IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner).Anchored)
|
||||
yield break;
|
||||
|
||||
var grid = IoCManager.Resolve<IMapManager>().GetGrid(IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner).GridID);
|
||||
var position = IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner).Coordinates;
|
||||
foreach (var entity in grid.GetLocal(position))
|
||||
{
|
||||
if (!IoCManager.Resolve<IEntityManager>().TryGetComponent<NodeContainerComponent>(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<IEntityManager>();
|
||||
var xform = entMan.GetComponent<TransformComponent>(Owner);
|
||||
if (!IoCManager.Resolve<IMapManager>().TryGetGrid(xform.GridID, out var grid))
|
||||
return;
|
||||
var pos = grid.WorldToTile(xform.WorldPosition);
|
||||
var query = entMan.GetEntityQuery<NodeContainerComponent>();
|
||||
|
||||
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
|
||||
/// </summary>
|
||||
private void UpdateAdjacentConnectedDirections()
|
||||
{
|
||||
var entMan = IoCManager.Resolve<IEntityManager>();
|
||||
var xform = entMan.GetComponent<TransformComponent>(Owner);
|
||||
if (!IoCManager.Resolve<IMapManager>().TryGetGrid(xform.GridID, out var grid))
|
||||
return;
|
||||
var pos = grid.WorldToTile(xform.WorldPosition);
|
||||
var query = entMan.GetEntityQuery<NodeContainerComponent>();
|
||||
|
||||
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();
|
||||
|
||||
@@ -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<Node> GetReachableNodes()
|
||||
public override IEnumerable<Node> GetReachableNodes(TransformComponent xform,
|
||||
EntityQuery<NodeContainerComponent> nodeQuery,
|
||||
EntityQuery<TransformComponent> 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;
|
||||
}
|
||||
|
||||
@@ -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<Node> GetReachableNodes()
|
||||
public override IEnumerable<Node> GetReachableNodes(TransformComponent xform,
|
||||
EntityQuery<NodeContainerComponent> nodeQuery,
|
||||
EntityQuery<TransformComponent> 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user