Optimize pipe net appearance updating. (#6469)

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
Leon Friedrich
2022-02-09 16:10:55 +13:00
committed by GitHub
parent 544a892348
commit 21e0cd4256
9 changed files with 193 additions and 278 deletions

View File

@@ -9,8 +9,8 @@ namespace Content.Server.NodeContainer.Nodes
public interface IRotatableNode
{
/// <summary>
/// Rotates this <see cref="Node"/>.
/// Rotates this <see cref="Node"/>. Returns true if the node's connections need to be updated.
/// </summary>
void RotateEvent(ref RotateEvent ev);
bool RotateEvent(ref RotateEvent ev);
}
}

View File

@@ -56,7 +56,7 @@ namespace Content.Server.NodeContainer.Nodes
[ViewVariables(VVAccess.ReadWrite)]
[DataField("needAnchored")]
private bool NeedAnchored { get; } = true;
public bool NeedAnchored { get; } = true;
/// <summary>
/// Prevents a node from being used by other nodes while midway through removal.
@@ -83,70 +83,11 @@ namespace Content.Server.NodeContainer.Nodes
/// Invoked when the owning <see cref="NodeContainerComponent"/> is initialized.
/// </summary>
/// <param name="owner">The owning entity.</param>
public virtual void Initialize(EntityUid owner)
public virtual void Initialize(EntityUid owner, IEntityManager entMan)
{
Owner = owner;
}
/// <summary>
/// Invoked when the owning <see cref="NodeContainerComponent"/> is started.
/// </summary>
public virtual void OnContainerStartup()
{
EntitySystem.Get<NodeGroupSystem>().QueueReflood(this);
}
/// <summary>
/// Immediately create a single-node node group for this node if it does not have one yet.
/// </summary>
/// <remarks>
/// This can be useful for nodes like pipes
/// that need immediate access to their node group to set parameters like node volume.
/// The node group created by this function (if necessary) will still update and form new,
/// merged groups later if necessary.
/// Set parameters like pipe net volume should then be transferred/merged there.
/// </remarks>
public void CreateSingleNetImmediate()
{
EntitySystem.Get<NodeGroupSystem>().CreateSingleNetImmediate(this);
}
public void AnchorUpdate()
{
if (Anchored)
{
EntitySystem.Get<NodeGroupSystem>().QueueReflood(this);
}
else
{
EntitySystem.Get<NodeGroupSystem>().QueueNodeRemove(this);
}
}
/// <summary>
/// Called when the anchored state of the owning entity changes.
/// </summary>
public virtual void AnchorStateChanged()
{
}
/// <summary>
/// Called after the parent node group has been rebuilt.
/// </summary>
public virtual void OnPostRebuild()
{
}
/// <summary>
/// Called when the owning <see cref="NodeContainerComponent"/> is shut down.
/// </summary>
public virtual void OnContainerShutdown()
{
Deleting = true;
EntitySystem.Get<NodeGroupSystem>().QueueNodeRemove(this);
}
/// <summary>
/// How this node will attempt to find other reachable <see cref="Node"/>s to group with.
/// Returns a set of <see cref="Node"/>s to consider grouping with. Should not return this current <see cref="Node"/>.

View File

@@ -21,8 +21,6 @@ namespace Content.Server.NodeContainer.Nodes
[DataDefinition]
public class PipeNode : Node, IGasMixtureHolder, IRotatableNode
{
private PipeDirection _connectedDirections;
/// <summary>
/// The directions in which this pipe can connect to other pipes around it.
/// </summary>
@@ -56,21 +54,6 @@ namespace Content.Server.NodeContainer.Nodes
EntitySystem.Get<NodeGroupSystem>().QueueRemakeGroup((BaseNodeGroup) NodeGroup);
}
/// <summary>
/// The directions in which this node is connected to other nodes.
/// Used by <see cref="PipeVisualState"/>.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public PipeDirection ConnectedDirections
{
get => _connectedDirections;
private set
{
_connectedDirections = value;
UpdateAppearance();
}
}
/// <summary>
/// Whether this node can connect to others or not.
/// </summary>
@@ -124,30 +107,37 @@ namespace Content.Server.NodeContainer.Nodes
private const float DefaultVolume = 200f;
public override void OnContainerStartup()
public override void Initialize(EntityUid owner, IEntityManager entMan)
{
base.OnContainerStartup();
OnConnectedDirectionsNeedsUpdating();
base.Initialize(owner, entMan);
if (!RotationsEnabled)
return;
var xform = entMan.GetComponent<TransformComponent>(owner);
CurrentPipeDirection = _originalPipeDirection.RotatePipeDirection(xform.LocalRotation);
}
public override void OnContainerShutdown()
bool IRotatableNode.RotateEvent(ref RotateEvent ev)
{
base.OnContainerShutdown();
UpdateAdjacentConnectedDirections();
}
if (_originalPipeDirection == PipeDirection.Fourway)
return false;
public void JoinPipeNet(IPipeNet pipeNet)
{
OnConnectedDirectionsNeedsUpdating();
}
// update valid pipe direction
if (!RotationsEnabled)
{
if (CurrentPipeDirection == _originalPipeDirection)
return false;
/// <summary>
/// Rotates the <see cref="PipeDirection"/> when the entity is rotated, and re-calculates the <see cref="IPipeNet"/>.
/// </summary>
void IRotatableNode.RotateEvent(ref RotateEvent ev)
{
OnConnectedDirectionsNeedsUpdating();
UpdateAppearance();
CurrentPipeDirection = _originalPipeDirection;
}
else
{
CurrentPipeDirection = _originalPipeDirection.RotatePipeDirection(ev.NewRotation);
}
// node connections need to be updated
return true;
}
public override IEnumerable<Node> GetReachableNodes(TransformComponent xform,
@@ -229,104 +219,5 @@ namespace Content.Server.NodeContainer.Nodes
}
}
}
/// <summary>
/// Updates the <see cref="ConnectedDirections"/> of this and all sorrounding pipes.
/// Also updates CurrentPipeDirection.
/// </summary>
private void OnConnectedDirectionsNeedsUpdating()
{
if (RotationsEnabled)
{
CurrentPipeDirection = _originalPipeDirection.RotatePipeDirection(IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(Owner).LocalRotation);
}
else
{
CurrentPipeDirection = _originalPipeDirection;
}
UpdateConnectedDirections();
UpdateAdjacentConnectedDirections();
UpdateAppearance();
}
/// <summary>
/// Checks what directions there are connectable pipes in, to update <see cref="ConnectedDirections"/>.
/// </summary>
private void UpdateConnectedDirections()
{
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);
if (!CurrentPipeDirection.HasDirection(pipeDir))
continue;
foreach (var pipe in LinkableNodesInDirection(pos, pipeDir, grid, query))
{
if (pipe.Connectable(entMan) && pipe.NodeGroupID == NodeGroupID)
{
ConnectedDirections |= pipeDir;
break;
}
}
}
}
/// <summary>
/// Calls <see cref="UpdateConnectedDirections"/> on all adjacent pipes,
/// to update their <see cref="ConnectedDirections"/> when this pipe is changed.
/// </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(pos, pipeDir, grid, query))
{
pipe.UpdateConnectedDirections();
pipe.UpdateAppearance();
}
}
}
/// <summary>
/// Updates the <see cref="AppearanceComponent"/>.
/// Gets the combined <see cref="ConnectedDirections"/> of every pipe on this entity, so the visualizer on this entity can draw the pipe connections.
/// </summary>
private void UpdateAppearance()
{
if (!IoCManager.Resolve<IEntityManager>().TryGetComponent(Owner, out AppearanceComponent? appearance)
|| !IoCManager.Resolve<IEntityManager>().TryGetComponent(Owner, out NodeContainerComponent? container))
return;
var netConnectedDirections = PipeDirection.None;
foreach (var node in container.Nodes.Values)
{
if (node is PipeNode pipe)
{
netConnectedDirections |= pipe.ConnectedDirections;
}
}
appearance.SetData(PipeVisuals.VisualState, netConnectedDirections);
}
}
}