2021-07-17 02:37:09 +02:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2021-02-01 10:19:43 -06:00
|
|
|
|
using System.Linq;
|
2021-06-09 22:19:39 +02:00
|
|
|
|
using Content.Server.NodeContainer;
|
|
|
|
|
|
using Content.Server.NodeContainer.NodeGroups;
|
2020-06-28 09:23:26 -06:00
|
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
|
namespace Content.Server.Power.Components
|
2020-06-28 09:23:26 -06:00
|
|
|
|
{
|
2023-09-20 00:44:49 +12:00
|
|
|
|
// TODO find a way to just remove this or turn it into one component.
|
|
|
|
|
|
// Component interface queries require enumerating over ALL of an entities components.
|
|
|
|
|
|
// So BaseNetConnectorNodeGroup<TNetType> is slow as shit.
|
2021-10-25 16:21:56 +02:00
|
|
|
|
public interface IBaseNetConnectorComponent<in TNetType>
|
|
|
|
|
|
{
|
|
|
|
|
|
public TNetType? Net { set; }
|
|
|
|
|
|
public Voltage Voltage { get; }
|
|
|
|
|
|
public string? NodeId { get; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-22 18:14:33 -07:00
|
|
|
|
public abstract partial class BaseNetConnectorComponent<TNetType> : Component, IBaseNetConnectorComponent<TNetType>
|
2023-08-25 20:40:42 +02:00
|
|
|
|
where TNetType : class
|
2020-06-28 09:23:26 -06:00
|
|
|
|
{
|
2021-12-08 17:32:32 +01:00
|
|
|
|
[Dependency] private readonly IEntityManager _entMan = default!;
|
|
|
|
|
|
|
2020-06-28 09:23:26 -06:00
|
|
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
|
|
|
|
|
public Voltage Voltage { get => _voltage; set => SetVoltage(value); }
|
2021-03-05 01:08:38 +01:00
|
|
|
|
[DataField("voltage")]
|
|
|
|
|
|
private Voltage _voltage = Voltage.High;
|
2020-06-28 09:23:26 -06:00
|
|
|
|
|
|
|
|
|
|
[ViewVariables]
|
2021-07-04 18:11:52 +02:00
|
|
|
|
public TNetType? Net { get => _net; set => SetNet(value); }
|
|
|
|
|
|
private TNetType? _net;
|
2020-06-28 09:23:26 -06:00
|
|
|
|
|
2023-07-26 22:37:52 +10:00
|
|
|
|
[ViewVariables] public bool NeedsNet => _net != null;
|
2020-06-28 09:23:26 -06:00
|
|
|
|
|
2022-11-16 20:22:11 +01:00
|
|
|
|
[DataField("node")] public string? NodeId { get; set; }
|
2020-06-28 09:23:26 -06:00
|
|
|
|
|
|
|
|
|
|
public void TryFindAndSetNet()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (TryFindNet(out var net))
|
|
|
|
|
|
{
|
|
|
|
|
|
Net = net;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ClearNet()
|
|
|
|
|
|
{
|
2021-07-04 18:11:52 +02:00
|
|
|
|
if (_net != null)
|
2023-08-25 20:40:42 +02:00
|
|
|
|
{
|
2021-07-04 18:11:52 +02:00
|
|
|
|
RemoveSelfFromNet(_net);
|
2023-08-25 20:40:42 +02:00
|
|
|
|
_net = null;
|
|
|
|
|
|
}
|
2020-06-28 09:23:26 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected abstract void AddSelfToNet(TNetType net);
|
|
|
|
|
|
|
|
|
|
|
|
protected abstract void RemoveSelfFromNet(TNetType net);
|
|
|
|
|
|
|
2021-02-02 05:20:24 -06:00
|
|
|
|
private bool TryFindNet([NotNullWhen(true)] out TNetType? foundNet)
|
2020-06-28 09:23:26 -06:00
|
|
|
|
{
|
2023-08-24 03:10:55 -07:00
|
|
|
|
if (_entMan.TryGetComponent(Owner, out NodeContainerComponent? container))
|
2020-06-28 09:23:26 -06:00
|
|
|
|
{
|
2021-04-09 20:47:31 +02:00
|
|
|
|
var compatibleNet = container.Nodes.Values
|
2021-07-04 18:11:52 +02:00
|
|
|
|
.Where(node => (NodeId == null || NodeId == node.Name) && node.NodeGroupID == (NodeGroupID) Voltage)
|
2020-06-28 09:23:26 -06:00
|
|
|
|
.Select(node => node.NodeGroup)
|
|
|
|
|
|
.OfType<TNetType>()
|
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
|
|
if (compatibleNet != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
foundNet = compatibleNet;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
foundNet = default;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-04 18:11:52 +02:00
|
|
|
|
private void SetNet(TNetType? newNet)
|
2020-06-28 09:23:26 -06:00
|
|
|
|
{
|
2021-07-04 18:11:52 +02:00
|
|
|
|
if (_net != null)
|
|
|
|
|
|
RemoveSelfFromNet(_net);
|
|
|
|
|
|
|
|
|
|
|
|
if (newNet != null)
|
|
|
|
|
|
AddSelfToNet(newNet);
|
|
|
|
|
|
|
2020-06-28 09:23:26 -06:00
|
|
|
|
_net = newNet;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void SetVoltage(Voltage newVoltage)
|
|
|
|
|
|
{
|
|
|
|
|
|
ClearNet();
|
|
|
|
|
|
_voltage = newVoltage;
|
|
|
|
|
|
TryFindAndSetNet();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public enum Voltage
|
|
|
|
|
|
{
|
|
|
|
|
|
High = NodeGroupID.HVPower,
|
|
|
|
|
|
Medium = NodeGroupID.MVPower,
|
|
|
|
|
|
Apc = NodeGroupID.Apc,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|