Pow3r: stage 1 (#4208)

Co-authored-by: 20kdc <asdd2808@gmail.com>
This commit is contained in:
Pieter-Jan Briers
2021-07-04 18:11:52 +02:00
committed by GitHub
parent ea60a81fdf
commit 103bc19508
212 changed files with 8584 additions and 4426 deletions

View File

@@ -1,5 +1,6 @@
#nullable enable
using System.Collections.Generic;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Serialization.Manager.Attributes;
@@ -12,29 +13,19 @@ namespace Content.Server.NodeContainer.Nodes
[DataDefinition]
public class AdjacentNode : Node
{
protected override IEnumerable<Node> GetReachableNodes()
public override IEnumerable<Node> GetReachableNodes()
{
if (!Owner.Transform.Anchored)
yield break;
var compMgr = IoCManager.Resolve<IComponentManager>();
var grid = IoCManager.Resolve<IMapManager>().GetGrid(Owner.Transform.GridID);
var coords = Owner.Transform.Coordinates;
foreach (var cell in grid.GetCardinalNeighborCells(coords))
var gridIndex = grid.TileIndicesFor(Owner.Transform.Coordinates);
foreach (var (_, node) in NodeHelpers.GetCardinalNeighborNodes(compMgr, grid, gridIndex))
{
foreach (var entity in grid.GetLocal(Owner.EntityManager.GetEntity(cell).Transform.Coordinates))
{
if (!Owner.EntityManager.GetEntity(entity).TryGetComponent<NodeContainerComponent>(out var container))
continue;
foreach (var node in container.Nodes.Values)
{
if (node != null && node != this)
{
yield return node;
}
}
}
if (node != this)
yield return node;
}
}
}

View File

@@ -1,11 +1,8 @@
#nullable enable
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Content.Server.NodeContainer.EntitySystems;
using Content.Server.NodeContainer.NodeGroups;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Physics;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
@@ -26,20 +23,14 @@ namespace Content.Server.NodeContainer.Nodes
[DataField("nodeGroupID")]
public NodeGroupID NodeGroupID { get; private set; } = NodeGroupID.Default;
[ViewVariables]
public INodeGroup NodeGroup { get => _nodeGroup; set => SetNodeGroup(value); }
private INodeGroup _nodeGroup = BaseNodeGroup.NullGroup;
[ViewVariables] public INodeGroup? NodeGroup;
[ViewVariables]
public IEntity Owner { get; private set; } = default!;
[ViewVariables]
private bool _needsGroup = true;
[ViewVariables] public IEntity Owner { get; private set; } = default!;
/// <summary>
/// If this node should be considered for connection by other nodes.
/// </summary>
public bool Connectable => !_deleting && Anchored;
public bool Connectable => !Deleting && Anchored;
protected bool Anchored => !NeedAnchored || Owner.Transform.Anchored;
@@ -50,7 +41,15 @@ namespace Content.Server.NodeContainer.Nodes
/// <summary>
/// Prevents a node from being used by other nodes while midway through removal.
/// </summary>
private bool _deleting;
public bool Deleting;
public readonly HashSet<Node> ReachableNodes = new();
internal int FloodGen;
internal int UndirectGen;
internal bool FlaggedForFlood;
internal int NetId;
public string Name = default!;
public virtual void Initialize(IEntity owner)
{
@@ -59,20 +58,23 @@ namespace Content.Server.NodeContainer.Nodes
public virtual void OnContainerStartup()
{
TryAssignGroupIfNeeded();
CombineGroupWithReachable();
EntitySystem.Get<NodeGroupSystem>().QueueReflood(this);
}
public void CreateSingleNetImmediate()
{
EntitySystem.Get<NodeGroupSystem>().CreateSingleNetImmediate(this);
}
public void AnchorUpdate()
{
if (Anchored)
{
TryAssignGroupIfNeeded();
CombineGroupWithReachable();
EntitySystem.Get<NodeGroupSystem>().QueueReflood(this);
}
else
{
RemoveSelfFromGroup();
EntitySystem.Get<NodeGroupSystem>().QueueNodeRemove(this);
}
}
@@ -80,107 +82,21 @@ namespace Content.Server.NodeContainer.Nodes
{
}
public virtual void OnPostRebuild()
{
}
public virtual void OnContainerShutdown()
{
_deleting = true;
NodeGroup.RemoveNode(this);
}
public bool TryAssignGroupIfNeeded()
{
if (!_needsGroup || !Connectable)
{
return false;
}
NodeGroup = GetReachableCompatibleGroups().FirstOrDefault() ?? MakeNewGroup();
return true;
}
public void SpreadGroup()
{
Debug.Assert(!_needsGroup);
foreach (var node in GetReachableCompatibleNodes())
{
if (node._needsGroup)
{
node.NodeGroup = NodeGroup;
node.SpreadGroup();
}
}
}
public void ClearNodeGroup()
{
_nodeGroup = BaseNodeGroup.NullGroup;
_needsGroup = true;
}
protected void RefreshNodeGroup()
{
RemoveSelfFromGroup();
TryAssignGroupIfNeeded();
CombineGroupWithReachable();
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"/>.
/// </summary>
protected abstract IEnumerable<Node> GetReachableNodes();
private IEnumerable<Node> GetReachableCompatibleNodes()
{
foreach (var node in GetReachableNodes())
{
if (node.NodeGroupID == NodeGroupID && node.Connectable)
{
yield return node;
}
}
}
private IEnumerable<INodeGroup> GetReachableCompatibleGroups()
{
foreach (var node in GetReachableCompatibleNodes())
{
if (!node._needsGroup)
{
var group = node.NodeGroup;
if (group != NodeGroup)
{
yield return group;
}
}
}
}
private void CombineGroupWithReachable()
{
if (_needsGroup || !Connectable)
return;
foreach (var group in GetReachableCompatibleGroups())
{
NodeGroup.CombineGroup(group);
}
}
private void SetNodeGroup(INodeGroup newGroup)
{
_nodeGroup = newGroup;
NodeGroup.AddNode(this);
_needsGroup = false;
}
private INodeGroup MakeNewGroup()
{
return IoCManager.Resolve<INodeGroupFactory>().MakeNodeGroup(this);
}
private void RemoveSelfFromGroup()
{
NodeGroup.RemoveNode(this);
ClearNodeGroup();
}
public abstract IEnumerable<Node> GetReachableNodes();
}
}

View File

@@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Maths;
namespace Content.Server.NodeContainer.Nodes
{
/// <summary>
/// Helper utilities for implementing <see cref="Node"/>.
/// </summary>
public static class NodeHelpers
{
public static IEnumerable<Node> GetNodesInTile(IComponentManager compMgr, IMapGrid grid, Vector2i coords)
{
foreach (var entityUid in grid.GetAnchoredEntities(coords))
{
if (!compMgr.TryGetComponent(entityUid, out NodeContainerComponent? container))
continue;
foreach (var node in container.Nodes.Values)
{
yield return node;
}
}
}
public static IEnumerable<(Direction dir, Node node)> GetCardinalNeighborNodes(
IComponentManager compMgr,
IMapGrid grid,
Vector2i coords,
bool includeSameTile = true)
{
foreach (var (dir, entityUid) in GetCardinalNeighborCells(grid, coords, includeSameTile))
{
if (!compMgr.TryGetComponent(entityUid, out NodeContainerComponent? container))
continue;
foreach (var node in container.Nodes.Values)
{
yield return (dir, node);
}
}
}
[SuppressMessage("ReSharper", "EnforceForeachStatementBraces")]
public static IEnumerable<(Direction dir, EntityUid entity)> GetCardinalNeighborCells(
IMapGrid grid,
Vector2i coords,
bool includeSameTile = true)
{
if (includeSameTile)
{
foreach (var uid in grid.GetAnchoredEntities(coords))
yield return (Direction.Invalid, uid);
}
foreach (var uid in grid.GetAnchoredEntities(coords + (0, 1)))
yield return (Direction.North, uid);
foreach (var uid in grid.GetAnchoredEntities(coords + (0, -1)))
yield return (Direction.South, uid);
foreach (var uid in grid.GetAnchoredEntities(coords + (1, 0)))
yield return (Direction.East, uid);
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)
};
}
}
}

View File

@@ -3,9 +3,8 @@ using System.Collections.Generic;
using Content.Server.Atmos;
using Content.Server.Atmos.EntitySystems;
using Content.Server.Interfaces;
using Content.Server.NodeContainer;
using Content.Server.NodeContainer.EntitySystems;
using Content.Server.NodeContainer.NodeGroups;
using Content.Server.NodeContainer.Nodes;
using Content.Shared.Atmos;
using Robust.Server.GameObjects;
using Robust.Shared.Containers;
@@ -13,9 +12,10 @@ using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.NodeContainer.Nodes
namespace Content.Server.NodeContainer.Nodes
{
/// <summary>
/// Connects with other <see cref="PipeNode"/>s whose <see cref="PipeDirection"/>
@@ -63,21 +63,23 @@ namespace Content.Server.GameObjects.Components.NodeContainer.Nodes
set
{
_connectionsEnabled = value;
RefreshNodeGroup();
if (NodeGroup != null)
EntitySystem.Get<NodeGroupSystem>().QueueRemakeGroup((BaseNodeGroup) NodeGroup);
}
}
[DataField("connectionsEnabled")]
private bool _connectionsEnabled = true;
[DataField("rotationsEnabled")]
public bool RotationsEnabled { get; set; } = true;
/// <summary>
/// The <see cref="IPipeNet"/> this pipe is a part of. Set to <see cref="PipeNet.NullNet"/> when not in an <see cref="IPipeNet"/>.
/// The <see cref="IPipeNet"/> this pipe is a part of.
/// </summary>
[ViewVariables]
private IPipeNet _pipeNet = PipeNet.NullNet;
[DataField("connectionsEnabled")]
private bool _connectionsEnabled = true;
private IPipeNet? PipeNet => (IPipeNet?) NodeGroup;
/// <summary>
/// Whether to ignore the pipenet and return the environment's air.
@@ -92,8 +94,12 @@ namespace Content.Server.GameObjects.Components.NodeContainer.Nodes
[ViewVariables]
public GasMixture Air
{
get => !EnvironmentalAir ? _pipeNet.Air : Owner.Transform.Coordinates.GetTileAir() ?? GasMixture.SpaceGas;
set => _pipeNet.Air = value;
get => (!EnvironmentalAir ? PipeNet?.Air : Owner.Transform.Coordinates.GetTileAir()) ?? GasMixture.SpaceGas;
set
{
DebugTools.Assert(PipeNet != null);
PipeNet!.Air = value;
}
}
public void AssumeAir(GasMixture giver)
@@ -105,7 +111,7 @@ namespace Content.Server.GameObjects.Components.NodeContainer.Nodes
return;
}
EntitySystem.Get<AtmosphereSystem>().Merge(_pipeNet.Air, giver);
EntitySystem.Get<AtmosphereSystem>().Merge(PipeNet!.Air, giver);
}
[ViewVariables]
@@ -128,13 +134,6 @@ namespace Content.Server.GameObjects.Components.NodeContainer.Nodes
public void JoinPipeNet(IPipeNet pipeNet)
{
_pipeNet = pipeNet;
OnConnectedDirectionsNeedsUpdating();
}
public void ClearPipeNet()
{
_pipeNet = PipeNet.NullNet;
OnConnectedDirectionsNeedsUpdating();
}
@@ -146,12 +145,11 @@ namespace Content.Server.GameObjects.Components.NodeContainer.Nodes
if (!RotationsEnabled) return;
var diff = ev.NewRotation - ev.OldRotation;
PipeDirection = PipeDirection.RotatePipeDirection(diff);
RefreshNodeGroup();
OnConnectedDirectionsNeedsUpdating();
UpdateAppearance();
}
protected override IEnumerable<Node> GetReachableNodes()
public override IEnumerable<Node> GetReachableNodes()
{
for (var i = 0; i < PipeDirectionHelpers.AllPipeDirections; i++)
{