2021-01-28 20:21:08 -06:00
|
|
|
#nullable enable
|
2021-01-04 22:32:59 -06:00
|
|
|
using System.Collections.Generic;
|
2020-06-28 09:23:26 -06:00
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
2020-08-13 14:40:27 +02:00
|
|
|
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.GameObjects;
|
2020-08-13 14:40:27 +02:00
|
|
|
using Robust.Shared.IoC;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2021-03-08 04:09:59 +11:00
|
|
|
using Robust.Shared.Physics;
|
|
|
|
|
using Robust.Shared.Serialization;
|
2020-08-13 14:40:27 +02:00
|
|
|
using Robust.Shared.ViewVariables;
|
2020-06-28 09:23:26 -06:00
|
|
|
|
|
|
|
|
namespace Content.Server.GameObjects.Components.NodeContainer.Nodes
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Organizes themselves into distinct <see cref="INodeGroup"/>s with other <see cref="Node"/>s
|
|
|
|
|
/// that they can "reach" and have the same <see cref="Node.NodeGroupID"/>.
|
|
|
|
|
/// </summary>
|
2021-03-05 01:08:38 +01:00
|
|
|
[ImplicitDataDefinitionForInheritors]
|
|
|
|
|
public abstract class Node
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// An ID used as a criteria for combining into groups. Determines which <see cref="INodeGroup"/>
|
|
|
|
|
/// implementation is used as a group, detailed in <see cref="INodeGroupFactory"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ViewVariables]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("nodeGroupID")]
|
|
|
|
|
public NodeGroupID NodeGroupID { get; private set; } = NodeGroupID.Default;
|
2020-06-28 09:23:26 -06:00
|
|
|
|
|
|
|
|
[ViewVariables]
|
|
|
|
|
public INodeGroup NodeGroup { get => _nodeGroup; set => SetNodeGroup(value); }
|
|
|
|
|
private INodeGroup _nodeGroup = BaseNodeGroup.NullGroup;
|
|
|
|
|
|
|
|
|
|
[ViewVariables]
|
2021-01-28 20:21:08 -06:00
|
|
|
public IEntity Owner { get; private set; } = default!;
|
2020-06-28 09:23:26 -06:00
|
|
|
|
2020-07-02 04:02:29 -06:00
|
|
|
[ViewVariables]
|
2020-06-28 09:23:26 -06:00
|
|
|
private bool _needsGroup = true;
|
|
|
|
|
|
2020-07-02 04:02:29 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// If this node should be considered for connection by other nodes.
|
|
|
|
|
/// </summary>
|
2021-02-22 19:18:30 -07:00
|
|
|
public bool Connectable => !_deleting && Anchored;
|
2020-07-02 04:02:29 -06:00
|
|
|
|
2021-03-08 04:09:59 +11:00
|
|
|
private bool Anchored => !Owner.TryGetComponent<IPhysBody>(out var physics) || physics.BodyType == BodyType.Static;
|
2020-07-02 04:02:29 -06:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Prevents a node from being used by other nodes while midway through removal.
|
|
|
|
|
/// </summary>
|
2021-03-05 01:08:38 +01:00
|
|
|
private bool _deleting;
|
2020-08-06 18:47:54 -06:00
|
|
|
|
2020-08-27 09:45:27 -06:00
|
|
|
public virtual void Initialize(IEntity owner)
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
|
|
|
|
Owner = owner;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-22 19:18:30 -07:00
|
|
|
public virtual void OnContainerStartup()
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
|
|
|
|
TryAssignGroupIfNeeded();
|
|
|
|
|
CombineGroupWithReachable();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 22:32:59 -06:00
|
|
|
public void AnchorUpdate()
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
2021-01-04 22:32:59 -06:00
|
|
|
if (Anchored)
|
|
|
|
|
{
|
2021-02-28 03:59:10 -07:00
|
|
|
TryAssignGroupIfNeeded();
|
|
|
|
|
CombineGroupWithReachable();
|
2021-01-04 22:32:59 -06:00
|
|
|
}
|
|
|
|
|
else
|
2020-07-02 04:02:29 -06:00
|
|
|
{
|
2021-02-28 03:59:10 -07:00
|
|
|
RemoveSelfFromGroup();
|
2020-07-02 04:02:29 -06:00
|
|
|
}
|
2021-01-04 22:32:59 -06:00
|
|
|
}
|
|
|
|
|
|
2021-03-02 11:58:19 -07:00
|
|
|
public virtual void OnContainerShutdown()
|
2021-01-04 22:32:59 -06:00
|
|
|
{
|
|
|
|
|
_deleting = true;
|
2020-06-28 09:23:26 -06:00
|
|
|
NodeGroup.RemoveNode(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TryAssignGroupIfNeeded()
|
|
|
|
|
{
|
2021-02-28 03:59:10 -07:00
|
|
|
if (!_needsGroup || !Connectable)
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
NodeGroup = GetReachableCompatibleGroups().FirstOrDefault() ?? MakeNewGroup();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SpreadGroup()
|
|
|
|
|
{
|
|
|
|
|
Debug.Assert(!_needsGroup);
|
2021-02-28 03:59:10 -07:00
|
|
|
foreach (var node in GetReachableCompatibleNodes())
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
2021-02-28 03:59:10 -07:00
|
|
|
if (node._needsGroup)
|
|
|
|
|
{
|
|
|
|
|
node.NodeGroup = NodeGroup;
|
|
|
|
|
node.SpreadGroup();
|
|
|
|
|
}
|
2020-06-28 09:23:26 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ClearNodeGroup()
|
|
|
|
|
{
|
|
|
|
|
_nodeGroup = BaseNodeGroup.NullGroup;
|
|
|
|
|
_needsGroup = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-08 09:53:56 -06:00
|
|
|
protected void RefreshNodeGroup()
|
|
|
|
|
{
|
2021-02-28 03:59:10 -07:00
|
|
|
RemoveSelfFromGroup();
|
2020-10-08 09:53:56 -06:00
|
|
|
TryAssignGroupIfNeeded();
|
|
|
|
|
CombineGroupWithReachable();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-28 09:23:26 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// How this node will attempt to find other reachable <see cref="Node"/>s to group with.
|
2020-07-02 14:00:45 +02:00
|
|
|
/// Returns a set of <see cref="Node"/>s to consider grouping with. Should not return this current <see cref="Node"/>.
|
2020-06-28 09:23:26 -06:00
|
|
|
/// </summary>
|
|
|
|
|
protected abstract IEnumerable<Node> GetReachableNodes();
|
|
|
|
|
|
|
|
|
|
private IEnumerable<Node> GetReachableCompatibleNodes()
|
|
|
|
|
{
|
2021-02-28 03:59:10 -07:00
|
|
|
foreach (var node in GetReachableNodes())
|
|
|
|
|
{
|
|
|
|
|
if (node.NodeGroupID == NodeGroupID && node.Connectable)
|
|
|
|
|
{
|
|
|
|
|
yield return node;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-28 09:23:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<INodeGroup> GetReachableCompatibleGroups()
|
|
|
|
|
{
|
2021-02-28 03:59:10 -07:00
|
|
|
foreach (var node in GetReachableCompatibleNodes())
|
|
|
|
|
{
|
|
|
|
|
if (!node._needsGroup)
|
|
|
|
|
{
|
|
|
|
|
var group = node.NodeGroup;
|
|
|
|
|
if (group != NodeGroup)
|
|
|
|
|
{
|
|
|
|
|
yield return group;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-28 09:23:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CombineGroupWithReachable()
|
|
|
|
|
{
|
2021-02-28 03:59:10 -07:00
|
|
|
if (_needsGroup || !Connectable)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-06-28 09:23:26 -06:00
|
|
|
foreach (var group in GetReachableCompatibleGroups())
|
|
|
|
|
{
|
|
|
|
|
NodeGroup.CombineGroup(group);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetNodeGroup(INodeGroup newGroup)
|
|
|
|
|
{
|
|
|
|
|
_nodeGroup = newGroup;
|
|
|
|
|
NodeGroup.AddNode(this);
|
|
|
|
|
_needsGroup = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private INodeGroup MakeNewGroup()
|
|
|
|
|
{
|
2021-01-28 20:21:08 -06:00
|
|
|
return IoCManager.Resolve<INodeGroupFactory>().MakeNodeGroup(this);
|
2020-06-28 09:23:26 -06:00
|
|
|
}
|
2021-02-28 03:59:10 -07:00
|
|
|
|
|
|
|
|
private void RemoveSelfFromGroup()
|
|
|
|
|
{
|
|
|
|
|
NodeGroup.RemoveNode(this);
|
|
|
|
|
ClearNodeGroup();
|
|
|
|
|
}
|
2020-06-28 09:23:26 -06:00
|
|
|
}
|
|
|
|
|
}
|