Power Rework (#863)

Co-authored-by: py01 <pyronetics01@gmail.com>
This commit is contained in:
py01
2020-06-28 09:23:26 -06:00
committed by GitHub
parent ffe25de723
commit 23cc6b1d4e
154 changed files with 11253 additions and 3913 deletions

View File

@@ -0,0 +1,25 @@
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Robust.Shared.GameObjects.Components.Transform;
using System.Collections.Generic;
using System.Linq;
namespace Content.Server.GameObjects.Components.NodeContainer.Nodes
{
/// <summary>
/// A <see cref="Node"/> that can reach other <see cref="AdjacentNode"/>s that are directly adjacent to it.
/// </summary>
[Node("AdjacentNode")]
public class AdjacentNode : Node
{
protected override IEnumerable<Node> GetReachableNodes()
{
return Owner.GetComponent<SnapGridComponent>()
.GetCardinalNeighborCells()
.SelectMany(sgc => sgc.GetLocal())
.Select(entity => entity.TryGetComponent<NodeContainerComponent>(out var container) ? container : null)
.Where(container => container != null)
.SelectMany(container => container.Nodes)
.Where(node => node != null && node != this);
}
}
}

View File

@@ -0,0 +1,131 @@
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.ViewVariables;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
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>
public abstract class Node
{
/// <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]
public NodeGroupID NodeGroupID { get; private set; }
[ViewVariables]
public INodeGroup NodeGroup { get => _nodeGroup; set => SetNodeGroup(value); }
private INodeGroup _nodeGroup = BaseNodeGroup.NullGroup;
[ViewVariables]
public IEntity Owner { get; private set; }
private bool _needsGroup = true;
private bool _deleting = false;
#pragma warning disable 649
[Dependency] private readonly INodeGroupFactory _nodeGroupFactory;
#pragma warning restore 649
public void Initialize(NodeGroupID nodeGroupID, IEntity owner)
{
NodeGroupID = nodeGroupID;
Owner = owner;
}
public void OnContainerInitialize()
{
TryAssignGroupIfNeeded();
CombineGroupWithReachable();
}
public void OnContainerRemove()
{
_deleting = true;
NodeGroup.RemoveNode(this);
}
public bool TryAssignGroupIfNeeded()
{
if (!_needsGroup)
{
return false;
}
NodeGroup = GetReachableCompatibleGroups().FirstOrDefault() ?? MakeNewGroup();
return true;
}
public void StartSpreadingGroup()
{
NodeGroup.BeforeRemakeSpread();
SpreadGroup();
NodeGroup.AfterRemakeSpread();
}
public void SpreadGroup()
{
Debug.Assert(!_needsGroup);
foreach (var node in GetReachableCompatibleNodes().Where(node => node._needsGroup == true))
{
node.NodeGroup = NodeGroup;
node.SpreadGroup();
}
}
public void ClearNodeGroup()
{
_nodeGroup = BaseNodeGroup.NullGroup;
_needsGroup = true;
}
/// <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()
{
return GetReachableNodes().Where(node => node.NodeGroupID == NodeGroupID)
.Where(node => node._deleting == false);
}
private IEnumerable<INodeGroup> GetReachableCompatibleGroups()
{
return GetReachableCompatibleNodes().Where(node => node._needsGroup == false)
.Select(node => node.NodeGroup)
.Where(group => group != NodeGroup);
}
private void CombineGroupWithReachable()
{
Debug.Assert(!_needsGroup);
foreach (var group in GetReachableCompatibleGroups())
{
NodeGroup.CombineGroup(group);
}
}
private void SetNodeGroup(INodeGroup newGroup)
{
_nodeGroup = newGroup;
NodeGroup.AddNode(this);
_needsGroup = false;
}
private INodeGroup MakeNewGroup()
{
return _nodeGroupFactory.MakeNodeGroup(NodeGroupID);
}
}
}

View File

@@ -0,0 +1,20 @@
using System;
namespace Content.Server.GameObjects.Components.NodeContainer.Nodes
{
/// <summary>
/// Associates a <see cref="Node"/> implementation with a string. This is used
/// to specify an <see cref="Node"/>'s strategy in yaml. Used by <see cref="INodeFactory"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class
NodeAttribute : Attribute
{
public string Name { get; }
public NodeAttribute(string name)
{
Name = name;
}
}
}

View File

@@ -0,0 +1,58 @@
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Reflection;
using Robust.Shared.IoC;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Content.Server.GameObjects.Components.NodeContainer.Nodes
{
public interface INodeFactory
{
/// <summary>
/// Performs reflection to associate <see cref="Node"/> implementations with the
/// string specified in their <see cref="NodeAttribute"/>.
/// </summary>
void Initialize();
/// <summary>
/// Returns a new <see cref="Node"/> instance.
/// </summary>
Node MakeNode(string nodeName, NodeGroupID groupID, IEntity owner);
}
public class NodeFactory : INodeFactory
{
private readonly Dictionary<string, Type> _groupTypes = new Dictionary<string, Type>();
#pragma warning disable 649
[Dependency] private readonly IReflectionManager _reflectionManager;
[Dependency] private readonly IDynamicTypeFactory _typeFactory;
#pragma warning restore 649
public void Initialize()
{
var nodeTypes = _reflectionManager.GetAllChildren<Node>();
foreach (var nodeType in nodeTypes)
{
var att = nodeType.GetCustomAttribute<NodeAttribute>();
if (att != null)
{
_groupTypes.Add(att.Name, nodeType);
}
}
}
public Node MakeNode(string nodeName, NodeGroupID groupID, IEntity owner)
{
if (_groupTypes.TryGetValue(nodeName, out var type))
{
var newNode = _typeFactory.CreateInstance<Node>(type);
newNode.Initialize(groupID, owner);
return newNode;
}
throw new ArgumentException($"{nodeName} did not have an associated {nameof(Node)}.");
}
}
}