Re-organize all projects (#4166)

This commit is contained in:
DrSmugleaf
2021-06-09 22:19:39 +02:00
committed by GitHub
parent 9f50e4061b
commit ff1a2d97ea
1773 changed files with 5258 additions and 5508 deletions

View File

@@ -0,0 +1,37 @@
#nullable enable
using System.Collections.Generic;
using System.Linq;
using Content.Server.NodeContainer.Nodes;
using Content.Server.Power.Components;
namespace Content.Server.NodeContainer.NodeGroups
{
public abstract class BaseNetConnectorNodeGroup<TNetConnector, TNetType> : BaseNodeGroup where TNetConnector : BaseNetConnectorComponent<TNetType>
{
private readonly Dictionary<Node, List<TNetConnector>> _netConnectorComponents = new();
protected override void OnAddNode(Node node)
{
var newNetConnectorComponents = node.Owner
.GetAllComponents<TNetConnector>()
.Where(powerComp => (NodeGroupID) powerComp.Voltage == node.NodeGroupID)
.ToList();
_netConnectorComponents[node] = newNetConnectorComponents;
foreach (var netConnectorComponent in newNetConnectorComponents)
{
SetNetConnectorNet(netConnectorComponent);
}
}
protected abstract void SetNetConnectorNet(TNetConnector netConnectorComponent);
protected override void OnRemoveNode(Node node)
{
foreach (var netConnectorComponent in _netConnectorComponents[node])
{
netConnectorComponent.ClearNet();
}
_netConnectorComponents.Remove(node);
}
}
}

View File

@@ -0,0 +1,125 @@
#nullable enable
using System.Collections.Generic;
using Content.Server.NodeContainer.EntitySystems;
using Content.Server.NodeContainer.Nodes;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.ViewVariables;
namespace Content.Server.NodeContainer.NodeGroups
{
/// <summary>
/// Maintains a collection of <see cref="Node"/>s, and performs operations requiring a list of
/// all connected <see cref="Node"/>s.
/// </summary>
public interface INodeGroup
{
IReadOnlyList<Node> Nodes { get; }
void Initialize(Node sourceNode);
void AddNode(Node node);
void RemoveNode(Node node);
void CombineGroup(INodeGroup newGroup);
void RemakeGroup();
}
[NodeGroup(NodeGroupID.Default, NodeGroupID.WireNet)]
public class BaseNodeGroup : INodeGroup
{
[ViewVariables]
public IReadOnlyList<Node> Nodes => _nodes;
private readonly List<Node> _nodes = new();
[ViewVariables]
public int NodeCount => Nodes.Count;
/// <summary>
/// Debug variable to indicate that this NodeGroup should not be being used by anything.
/// </summary>
[ViewVariables]
public bool Removed { get; private set; } = false;
public static readonly INodeGroup NullGroup = new NullNodeGroup();
protected GridId GridId { get; private set;}
public virtual void Initialize(Node sourceNode)
{
GridId = sourceNode.Owner.Transform.GridID;
}
public void AddNode(Node node)
{
_nodes.Add(node);
OnAddNode(node);
}
public void RemoveNode(Node node)
{
_nodes.Remove(node);
OnRemoveNode(node);
EntitySystem.Get<NodeGroupSystem>().AddDirtyNodeGroup(this);
}
public void CombineGroup(INodeGroup newGroup)
{
if (newGroup.Nodes.Count < Nodes.Count)
{
newGroup.CombineGroup(this);
return;
}
OnGivingNodesForCombine(newGroup);
foreach (var node in Nodes)
{
node.NodeGroup = newGroup;
}
Removed = true;
}
/// <summary>
/// Causes all <see cref="Node"/>s to remake their groups. Called when a <see cref="Node"/> is removed
/// and may have split a group in two, so multiple new groups may need to be formed.
/// </summary>
public void RemakeGroup()
{
foreach (var node in Nodes)
{
node.ClearNodeGroup();
}
var newGroups = new List<INodeGroup>();
foreach (var node in Nodes)
{
if (node.TryAssignGroupIfNeeded())
{
node.SpreadGroup();
newGroups.Add(node.NodeGroup);
}
}
AfterRemake(newGroups);
Removed = true;
}
protected virtual void OnAddNode(Node node) { }
protected virtual void OnRemoveNode(Node node) { }
protected virtual void OnGivingNodesForCombine(INodeGroup newGroup) { }
protected virtual void AfterRemake(IEnumerable<INodeGroup> newGroups) { }
private class NullNodeGroup : INodeGroup
{
public IReadOnlyList<Node> Nodes => _nodes;
private readonly List<Node> _nodes = new();
public void Initialize(Node sourceNode) { }
public void AddNode(Node node) { }
public void CombineGroup(INodeGroup newGroup) { }
public void RemoveNode(Node node) { }
public void RemakeGroup() { }
}
}
}

View File

@@ -0,0 +1,102 @@
#nullable enable
using System.Collections.Generic;
using Content.Server.Atmos;
using Content.Server.GameObjects.EntitySystems;
using Content.Server.Interfaces;
using Content.Server.NodeContainer.Nodes;
using Robust.Shared.GameObjects;
using Robust.Shared.ViewVariables;
namespace Content.Server.NodeContainer.NodeGroups
{
public interface IPipeNet : IGasMixtureHolder
{
/// <summary>
/// Causes gas in the PipeNet to react.
/// </summary>
void Update();
}
[NodeGroup(NodeGroupID.Pipe)]
public class PipeNet : BaseNodeGroup, IPipeNet
{
[ViewVariables]
public GasMixture Air { get; set; } = new();
public static readonly IPipeNet NullNet = new NullPipeNet();
[ViewVariables]
private readonly List<PipeNode> _pipes = new();
[ViewVariables] private AtmosphereSystem? _atmosphereSystem;
[ViewVariables] private IGridAtmosphereComponent? GridAtmos => _atmosphereSystem?.GetGridAtmosphere(GridId);
public override void Initialize(Node sourceNode)
{
base.Initialize(sourceNode);
_atmosphereSystem = EntitySystem.Get<AtmosphereSystem>();
GridAtmos?.AddPipeNet(this);
}
public void Update()
{
Air.React(this);
}
protected override void OnAddNode(Node node)
{
if (node is not PipeNode pipeNode)
return;
_pipes.Add(pipeNode);
pipeNode.JoinPipeNet(this);
Air.Volume += pipeNode.Volume;
Air.Merge(pipeNode.LocalAir);
pipeNode.LocalAir.Clear();
}
protected override void OnRemoveNode(Node node)
{
RemoveFromGridAtmos();
if (node is not PipeNode pipeNode)
return;
var pipeAir = pipeNode.LocalAir;
pipeAir.Merge(Air);
pipeAir.Multiply(pipeNode.Volume / Air.Volume);
_pipes.Remove(pipeNode);
}
protected override void OnGivingNodesForCombine(INodeGroup newGroup)
{
if (newGroup is not IPipeNet newPipeNet)
return;
newPipeNet.Air.Merge(Air);
Air.Clear();
}
protected override void AfterRemake(IEnumerable<INodeGroup> newGroups)
{
foreach (var newGroup in newGroups)
{
if (newGroup is not IPipeNet newPipeNet)
continue;
newPipeNet.Air.Merge(Air);
var newPipeNetGas = newPipeNet.Air;
newPipeNetGas.Multiply(newPipeNetGas.Volume / Air.Volume);
}
RemoveFromGridAtmos();
}
private void RemoveFromGridAtmos()
{
GridAtmos?.RemovePipeNet(this);
}
private class NullPipeNet : IPipeNet
{
GasMixture IGasMixtureHolder.Air { get; set; } = new();
public void Update() { }
}
}
}

View File

@@ -0,0 +1,21 @@
#nullable enable
using System;
namespace Content.Server.NodeContainer.NodeGroups
{
/// <summary>
/// Associates a <see cref="INodeGroup"/> implementation with a <see cref="NodeGroupID"/>.
/// This is used to gurantee all <see cref="INode"/>s of the same <see cref="INode.NodeGroupID"/>
/// have the same type of <see cref="INodeGroup"/>. Used by <see cref="INodeGroupFactory"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class NodeGroupAttribute : Attribute
{
public NodeGroupID[] NodeGroupIDs { get; }
public NodeGroupAttribute(params NodeGroupID[] nodeGroupTypes)
{
NodeGroupIDs = nodeGroupTypes;
}
}
}

View File

@@ -0,0 +1,70 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Reflection;
using Content.Server.NodeContainer.Nodes;
using Robust.Shared.IoC;
using Robust.Shared.Reflection;
namespace Content.Server.NodeContainer.NodeGroups
{
public interface INodeGroupFactory
{
/// <summary>
/// Performs reflection to associate <see cref="INodeGroup"/> implementations with the
/// string specified in their <see cref="NodeGroupAttribute"/>.
/// </summary>
void Initialize();
/// <summary>
/// Returns a new <see cref="INodeGroup"/> instance.
/// </summary>
INodeGroup MakeNodeGroup(Node sourceNode);
}
public class NodeGroupFactory : INodeGroupFactory
{
[Dependency] private readonly IReflectionManager _reflectionManager = default!;
[Dependency] private readonly IDynamicTypeFactory _typeFactory = default!;
private readonly Dictionary<NodeGroupID, Type> _groupTypes = new();
public void Initialize()
{
var nodeGroupTypes = _reflectionManager.GetAllChildren<INodeGroup>();
foreach (var nodeGroupType in nodeGroupTypes)
{
var att = nodeGroupType.GetCustomAttribute<NodeGroupAttribute>();
if (att != null)
{
foreach (var groupID in att.NodeGroupIDs)
{
_groupTypes.Add(groupID, nodeGroupType);
}
}
}
}
public INodeGroup MakeNodeGroup(Node sourceNode)
{
if (_groupTypes.TryGetValue(sourceNode.NodeGroupID, out var type))
{
var nodeGroup = _typeFactory.CreateInstance<INodeGroup>(type);
nodeGroup.Initialize(sourceNode);
return nodeGroup;
}
throw new ArgumentException($"{sourceNode.NodeGroupID} did not have an associated {nameof(INodeGroup)}.");
}
}
public enum NodeGroupID
{
Default,
HVPower,
MVPower,
Apc,
AMEngine,
Pipe,
WireNet
}
}

View File

@@ -0,0 +1,164 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Content.Server.Power.Components;
using Robust.Shared.IoC;
using Robust.Shared.ViewVariables;
namespace Content.Server.NodeContainer.NodeGroups
{
public interface IPowerNet
{
void AddSupplier(PowerSupplierComponent supplier);
void RemoveSupplier(PowerSupplierComponent supplier);
void UpdateSupplierSupply(PowerSupplierComponent supplier, int oldSupplyRate, int newSupplyRate);
void AddConsumer(PowerConsumerComponent consumer);
void RemoveConsumer(PowerConsumerComponent consumer);
void UpdateConsumerDraw(PowerConsumerComponent consumer, int oldDrawRate, int newDrawRate);
void UpdateConsumerPriority(PowerConsumerComponent consumer, Priority oldPriority, Priority newPriority);
void UpdateConsumerReceivedPower();
}
[NodeGroup(NodeGroupID.HVPower, NodeGroupID.MVPower)]
public class PowerNetNodeGroup : BaseNetConnectorNodeGroup<BasePowerNetComponent, IPowerNet>, IPowerNet
{
private static readonly Priority[] CachedPriorities = (Priority[]) Enum.GetValues(typeof(Priority));
[Dependency] private readonly IPowerNetManager _powerNetManager = default!;
[ViewVariables]
private readonly List<PowerSupplierComponent> _suppliers = new();
[ViewVariables]
private int _totalSupply = 0;
[ViewVariables]
private readonly Dictionary<Priority, List<PowerConsumerComponent>> _consumersByPriority = new();
[ViewVariables]
private readonly Dictionary<Priority, int> _drawByPriority = new();
public static readonly IPowerNet NullNet = new NullPowerNet();
public PowerNetNodeGroup()
{
foreach (Priority priority in Enum.GetValues(typeof(Priority)))
{
_consumersByPriority.Add(priority, new List<PowerConsumerComponent>());
_drawByPriority.Add(priority, 0);
}
}
protected override void SetNetConnectorNet(BasePowerNetComponent netConnectorComponent)
{
netConnectorComponent.Net = this;
}
#region IPowerNet Methods
public void AddSupplier(PowerSupplierComponent supplier)
{
_suppliers.Add(supplier);
_totalSupply += supplier.SupplyRate;
_powerNetManager.AddDirtyPowerNet(this);
}
public void RemoveSupplier(PowerSupplierComponent supplier)
{
Debug.Assert(_suppliers.Contains(supplier));
_suppliers.Remove(supplier);
_totalSupply -= supplier.SupplyRate;
_powerNetManager.AddDirtyPowerNet(this);
}
public void UpdateSupplierSupply(PowerSupplierComponent supplier, int oldSupplyRate, int newSupplyRate)
{
Debug.Assert(_suppliers.Contains(supplier));
_totalSupply -= oldSupplyRate;
_totalSupply += newSupplyRate;
_powerNetManager.AddDirtyPowerNet(this);
}
public void AddConsumer(PowerConsumerComponent consumer)
{
_consumersByPriority[consumer.Priority].Add(consumer);
_drawByPriority[consumer.Priority] += consumer.DrawRate;
_powerNetManager.AddDirtyPowerNet(this);
}
public void RemoveConsumer(PowerConsumerComponent consumer)
{
Debug.Assert(_consumersByPriority[consumer.Priority].Contains(consumer));
consumer.ReceivedPower = 0;
_consumersByPriority[consumer.Priority].Remove(consumer);
_drawByPriority[consumer.Priority] -= consumer.DrawRate;
_powerNetManager.AddDirtyPowerNet(this);
}
public void UpdateConsumerDraw(PowerConsumerComponent consumer, int oldDrawRate, int newDrawRate)
{
Debug.Assert(_consumersByPriority[consumer.Priority].Contains(consumer));
_drawByPriority[consumer.Priority] -= oldDrawRate;
_drawByPriority[consumer.Priority] += newDrawRate;
_powerNetManager.AddDirtyPowerNet(this);
}
public void UpdateConsumerPriority(PowerConsumerComponent consumer, Priority oldPriority, Priority newPriority)
{
Debug.Assert(_consumersByPriority[oldPriority].Contains(consumer));
_consumersByPriority[oldPriority].Remove(consumer);
_drawByPriority[oldPriority] -= consumer.DrawRate;
_consumersByPriority[newPriority].Add(consumer);
_drawByPriority[newPriority] += consumer.DrawRate;
_powerNetManager.AddDirtyPowerNet(this);
}
public void UpdateConsumerReceivedPower()
{
var remainingSupply = _totalSupply;
foreach (var priority in CachedPriorities)
{
var categoryPowerDemand = _drawByPriority[priority];
if (remainingSupply >= categoryPowerDemand) //can fully power all in category
{
remainingSupply -= categoryPowerDemand;
foreach (var consumer in _consumersByPriority[priority])
{
consumer.ReceivedPower = consumer.DrawRate;
}
}
else //cannot fully power all, split power
{
var availiablePowerFraction = (float) remainingSupply / categoryPowerDemand;
remainingSupply = 0;
foreach (var consumer in _consumersByPriority[priority])
{
consumer.ReceivedPower = (int) (consumer.DrawRate * availiablePowerFraction); //give each consumer a fraction of what they requested (rounded down to nearest int)
}
}
}
}
#endregion
private class NullPowerNet : IPowerNet
{
public void AddConsumer(PowerConsumerComponent consumer) { }
public void AddSupplier(PowerSupplierComponent supplier) { }
public void UpdateSupplierSupply(PowerSupplierComponent supplier, int oldSupplyRate, int newSupplyRate) { }
public void RemoveConsumer(PowerConsumerComponent consumer) { }
public void RemoveSupplier(PowerSupplierComponent supplier) { }
public void UpdateConsumerDraw(PowerConsumerComponent consumer, int oldDrawRate, int newDrawRate) { }
public void UpdateConsumerPriority(PowerConsumerComponent consumer, Priority oldPriority, Priority newPriority) { }
public void UpdateConsumerReceivedPower() { }
}
}
}