Re-organize all projects (#4166)
This commit is contained in:
223
Content.Server/APC/ApcNetNodeGroup.cs
Normal file
223
Content.Server/APC/ApcNetNodeGroup.cs
Normal file
@@ -0,0 +1,223 @@
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Server.APC.Components;
|
||||
using Content.Server.Battery.Components;
|
||||
using Content.Server.NodeContainer.NodeGroups;
|
||||
using Content.Server.NodeContainer.Nodes;
|
||||
using Content.Server.Power.Components;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.APC
|
||||
{
|
||||
public interface IApcNet
|
||||
{
|
||||
bool Powered { get; }
|
||||
|
||||
void AddApc(ApcComponent apc);
|
||||
|
||||
void RemoveApc(ApcComponent apc);
|
||||
|
||||
void AddPowerProvider(PowerProviderComponent provider);
|
||||
|
||||
void RemovePowerProvider(PowerProviderComponent provider);
|
||||
|
||||
void UpdatePowerProviderReceivers(PowerProviderComponent provider, int oldLoad, int newLoad);
|
||||
|
||||
void Update(float frameTime);
|
||||
|
||||
GridId? GridId { get; }
|
||||
}
|
||||
|
||||
[NodeGroup(NodeGroupID.Apc)]
|
||||
public class ApcNetNodeGroup : BaseNetConnectorNodeGroup<BaseApcNetComponent, IApcNet>, IApcNet
|
||||
{
|
||||
[ViewVariables]
|
||||
private readonly Dictionary<ApcComponent, BatteryComponent> _apcBatteries = new();
|
||||
|
||||
[ViewVariables]
|
||||
private readonly List<PowerProviderComponent> _providers = new();
|
||||
|
||||
[ViewVariables]
|
||||
public bool Powered { get => _powered; private set => SetPowered(value); }
|
||||
private bool _powered = false;
|
||||
|
||||
//Debug property
|
||||
[ViewVariables]
|
||||
private int TotalReceivers => _providers.SelectMany(provider => provider.LinkedReceivers).Count();
|
||||
|
||||
[ViewVariables]
|
||||
private int TotalPowerReceiverLoad { get => _totalPowerReceiverLoad; set => SetTotalPowerReceiverLoad(value); }
|
||||
|
||||
GridId? IApcNet.GridId => GridId;
|
||||
|
||||
private int _totalPowerReceiverLoad = 0;
|
||||
|
||||
public static readonly IApcNet NullNet = new NullApcNet();
|
||||
|
||||
public override void Initialize(Node sourceNode)
|
||||
{
|
||||
base.Initialize(sourceNode);
|
||||
|
||||
EntitySystem.Get<ApcNetSystem>().AddApcNet(this);
|
||||
}
|
||||
|
||||
protected override void AfterRemake(IEnumerable<INodeGroup> newGroups)
|
||||
{
|
||||
base.AfterRemake(newGroups);
|
||||
|
||||
foreach (var group in newGroups)
|
||||
{
|
||||
if (group is not ApcNetNodeGroup apcNet)
|
||||
continue;
|
||||
|
||||
apcNet.Powered = Powered;
|
||||
}
|
||||
|
||||
StopUpdates();
|
||||
}
|
||||
|
||||
protected override void OnGivingNodesForCombine(INodeGroup newGroup)
|
||||
{
|
||||
base.OnGivingNodesForCombine(newGroup);
|
||||
|
||||
if (newGroup is ApcNetNodeGroup apcNet)
|
||||
{
|
||||
apcNet.Powered = Powered;
|
||||
}
|
||||
|
||||
StopUpdates();
|
||||
}
|
||||
|
||||
private void StopUpdates()
|
||||
{
|
||||
EntitySystem.Get<ApcNetSystem>().RemoveApcNet(this);
|
||||
}
|
||||
|
||||
#region IApcNet Methods
|
||||
|
||||
protected override void SetNetConnectorNet(BaseApcNetComponent netConnectorComponent)
|
||||
{
|
||||
netConnectorComponent.Net = this;
|
||||
}
|
||||
|
||||
public void AddApc(ApcComponent apc)
|
||||
{
|
||||
if (!apc.Owner.TryGetComponent(out BatteryComponent? battery))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_apcBatteries.Add(apc, battery);
|
||||
}
|
||||
|
||||
public void RemoveApc(ApcComponent apc)
|
||||
{
|
||||
_apcBatteries.Remove(apc);
|
||||
}
|
||||
|
||||
public void AddPowerProvider(PowerProviderComponent provider)
|
||||
{
|
||||
_providers.Add(provider);
|
||||
|
||||
foreach (var receiver in provider.LinkedReceivers)
|
||||
{
|
||||
TotalPowerReceiverLoad += receiver.Load;
|
||||
}
|
||||
}
|
||||
|
||||
public void RemovePowerProvider(PowerProviderComponent provider)
|
||||
{
|
||||
_providers.Remove(provider);
|
||||
|
||||
foreach (var receiver in provider.LinkedReceivers)
|
||||
{
|
||||
TotalPowerReceiverLoad -= receiver.Load;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdatePowerProviderReceivers(PowerProviderComponent provider, int oldLoad, int newLoad)
|
||||
{
|
||||
DebugTools.Assert(_providers.Contains(provider));
|
||||
TotalPowerReceiverLoad -= oldLoad;
|
||||
TotalPowerReceiverLoad += newLoad;
|
||||
}
|
||||
|
||||
public void Update(float frameTime)
|
||||
{
|
||||
var remainingPowerNeeded = TotalPowerReceiverLoad * frameTime;
|
||||
|
||||
foreach (var apcBatteryPair in _apcBatteries)
|
||||
{
|
||||
var apc = apcBatteryPair.Key;
|
||||
|
||||
if (!apc.MainBreakerEnabled)
|
||||
continue;
|
||||
|
||||
var battery = apcBatteryPair.Value;
|
||||
|
||||
if (battery.CurrentCharge < remainingPowerNeeded)
|
||||
{
|
||||
remainingPowerNeeded -= battery.CurrentCharge;
|
||||
battery.CurrentCharge = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
battery.UseCharge(remainingPowerNeeded);
|
||||
remainingPowerNeeded = 0;
|
||||
}
|
||||
|
||||
if (remainingPowerNeeded == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
Powered = remainingPowerNeeded == 0;
|
||||
}
|
||||
|
||||
private void SetPowered(bool powered)
|
||||
{
|
||||
if (powered != Powered)
|
||||
{
|
||||
_powered = powered;
|
||||
PoweredChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private void PoweredChanged()
|
||||
{
|
||||
foreach (var provider in _providers)
|
||||
{
|
||||
foreach (var receiver in provider.LinkedReceivers)
|
||||
{
|
||||
receiver.ApcPowerChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SetTotalPowerReceiverLoad(int totalPowerReceiverLoad)
|
||||
{
|
||||
DebugTools.Assert(totalPowerReceiverLoad >= 0, $"Expected load equal to or greater than 0, was {totalPowerReceiverLoad}");
|
||||
_totalPowerReceiverLoad = totalPowerReceiverLoad;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private class NullApcNet : IApcNet
|
||||
{
|
||||
/// <summary>
|
||||
/// It is important that this returns false, so <see cref="PowerProviderComponent"/>s with a <see cref="NullApcNet"/> have no power.
|
||||
/// </summary>
|
||||
public bool Powered => false;
|
||||
public GridId? GridId => default;
|
||||
public void AddApc(ApcComponent apc) { }
|
||||
public void AddPowerProvider(PowerProviderComponent provider) { }
|
||||
public void RemoveApc(ApcComponent apc) { }
|
||||
public void RemovePowerProvider(PowerProviderComponent provider) { }
|
||||
public void UpdatePowerProviderReceivers(PowerProviderComponent provider, int oldLoad, int newLoad) { }
|
||||
public void Update(float frameTime) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
45
Content.Server/APC/ApcNetSystem.cs
Normal file
45
Content.Server/APC/ApcNetSystem.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using Content.Shared.GameTicking;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Server.APC
|
||||
{
|
||||
[UsedImplicitly]
|
||||
internal sealed class ApcNetSystem : EntitySystem, IResettingEntitySystem
|
||||
{
|
||||
[Dependency] private readonly IPauseManager _pauseManager = default!;
|
||||
|
||||
private HashSet<IApcNet> _apcNets = new();
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
foreach (var apcNet in _apcNets)
|
||||
{
|
||||
var gridId = apcNet.GridId;
|
||||
if (gridId != null && !_pauseManager.IsGridPaused(gridId.Value))
|
||||
apcNet.Update(frameTime);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddApcNet(ApcNetNodeGroup apcNet)
|
||||
{
|
||||
_apcNets.Add(apcNet);
|
||||
}
|
||||
|
||||
public void RemoveApcNet(ApcNetNodeGroup apcNet)
|
||||
{
|
||||
_apcNets.Remove(apcNet);
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
// NodeGroupSystem does not remake ApcNets affected during restarting until a frame later,
|
||||
// when their grid is invalid. So, we are clearing them on round restart.
|
||||
_apcNets.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
205
Content.Server/APC/Components/ApcComponent.cs
Normal file
205
Content.Server/APC/Components/ApcComponent.cs
Normal file
@@ -0,0 +1,205 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using Content.Server.Access.Components;
|
||||
using Content.Server.Battery.Components;
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Server.UserInterface;
|
||||
using Content.Shared.APC;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Notification;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.APC.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
public class ApcComponent : BaseApcNetComponent, IActivate
|
||||
{
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
|
||||
public override string Name => "Apc";
|
||||
|
||||
public bool MainBreakerEnabled { get; private set; } = true;
|
||||
|
||||
private ApcChargeState _lastChargeState;
|
||||
|
||||
private TimeSpan _lastChargeStateChange;
|
||||
|
||||
private ApcExternalPowerState _lastExternalPowerState;
|
||||
|
||||
private TimeSpan _lastExternalPowerStateChange;
|
||||
|
||||
private float _lastCharge;
|
||||
|
||||
private TimeSpan _lastChargeChange;
|
||||
|
||||
private bool _uiDirty = true;
|
||||
|
||||
private const float HighPowerThreshold = 0.9f;
|
||||
|
||||
private const int VisualsChangeDelay = 1;
|
||||
|
||||
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(ApcUiKey.Key);
|
||||
|
||||
public BatteryComponent? Battery => Owner.TryGetComponent(out BatteryComponent? batteryComponent) ? batteryComponent : null;
|
||||
|
||||
[ComponentDependency] private AccessReader? _accessReader = null;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
Owner.EnsureComponent<PowerConsumerComponent>();
|
||||
Owner.EnsureComponentWarn<ServerUserInterfaceComponent>();
|
||||
Owner.EnsureComponentWarn<AccessReader>();
|
||||
|
||||
if (UserInterface != null)
|
||||
{
|
||||
UserInterface.OnReceiveMessage += UserInterfaceOnReceiveMessage;
|
||||
}
|
||||
|
||||
Update();
|
||||
}
|
||||
|
||||
protected override void AddSelfToNet(IApcNet apcNet)
|
||||
{
|
||||
apcNet.AddApc(this);
|
||||
}
|
||||
|
||||
protected override void RemoveSelfFromNet(IApcNet apcNet)
|
||||
{
|
||||
apcNet.RemoveApc(this);
|
||||
}
|
||||
|
||||
private void UserInterfaceOnReceiveMessage(ServerBoundUserInterfaceMessage serverMsg)
|
||||
{
|
||||
if (serverMsg.Message is ApcToggleMainBreakerMessage)
|
||||
{
|
||||
var user = serverMsg.Session.AttachedEntity;
|
||||
if(user == null) return;
|
||||
|
||||
if (_accessReader == null || _accessReader.IsAllowed(user))
|
||||
{
|
||||
MainBreakerEnabled = !MainBreakerEnabled;
|
||||
_uiDirty = true;
|
||||
SoundSystem.Play(Filter.Pvs(Owner), "/Audio/Machines/machine_switch.ogg", Owner, AudioParams.Default.WithVolume(-2f));
|
||||
}
|
||||
else
|
||||
{
|
||||
user.PopupMessageCursor(Loc.GetString("Insufficient access!"));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
var newState = CalcChargeState();
|
||||
if (newState != _lastChargeState && _lastChargeStateChange + TimeSpan.FromSeconds(VisualsChangeDelay) < _gameTiming.CurTime)
|
||||
{
|
||||
_lastChargeState = newState;
|
||||
_lastChargeStateChange = _gameTiming.CurTime;
|
||||
|
||||
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
|
||||
{
|
||||
appearance.SetData(ApcVisuals.ChargeState, newState);
|
||||
}
|
||||
}
|
||||
|
||||
Owner.TryGetComponent(out BatteryComponent? battery);
|
||||
|
||||
var newCharge = battery?.CurrentCharge;
|
||||
if (newCharge != null && newCharge != _lastCharge && _lastChargeChange + TimeSpan.FromSeconds(VisualsChangeDelay) < _gameTiming.CurTime)
|
||||
{
|
||||
_lastCharge = newCharge.Value;
|
||||
_lastChargeChange = _gameTiming.CurTime;
|
||||
_uiDirty = true;
|
||||
}
|
||||
|
||||
var extPowerState = CalcExtPowerState();
|
||||
if (extPowerState != _lastExternalPowerState && _lastExternalPowerStateChange + TimeSpan.FromSeconds(VisualsChangeDelay) < _gameTiming.CurTime)
|
||||
{
|
||||
_lastExternalPowerState = extPowerState;
|
||||
_lastExternalPowerStateChange = _gameTiming.CurTime;
|
||||
_uiDirty = true;
|
||||
}
|
||||
|
||||
if (_uiDirty && battery != null && newCharge != null)
|
||||
{
|
||||
UserInterface?.SetState(new ApcBoundInterfaceState(MainBreakerEnabled, extPowerState, newCharge.Value / battery.MaxCharge));
|
||||
_uiDirty = false;
|
||||
}
|
||||
}
|
||||
|
||||
private ApcChargeState CalcChargeState()
|
||||
{
|
||||
if (!Owner.TryGetComponent(out BatteryComponent? battery))
|
||||
{
|
||||
return ApcChargeState.Lack;
|
||||
}
|
||||
|
||||
var chargeFraction = battery.CurrentCharge / battery.MaxCharge;
|
||||
|
||||
if (chargeFraction > HighPowerThreshold)
|
||||
{
|
||||
return ApcChargeState.Full;
|
||||
}
|
||||
|
||||
if (!Owner.TryGetComponent(out PowerConsumerComponent? consumer))
|
||||
{
|
||||
return ApcChargeState.Full;
|
||||
}
|
||||
|
||||
if (consumer.DrawRate == consumer.ReceivedPower)
|
||||
{
|
||||
return ApcChargeState.Charging;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ApcChargeState.Lack;
|
||||
}
|
||||
}
|
||||
|
||||
private ApcExternalPowerState CalcExtPowerState()
|
||||
{
|
||||
if (!Owner.TryGetComponent(out BatteryStorageComponent? batteryStorage))
|
||||
{
|
||||
return ApcExternalPowerState.None;
|
||||
}
|
||||
var consumer = batteryStorage.Consumer;
|
||||
|
||||
if (consumer == null)
|
||||
return ApcExternalPowerState.None;
|
||||
|
||||
if (consumer.ReceivedPower == 0 && consumer.DrawRate != 0)
|
||||
{
|
||||
return ApcExternalPowerState.None;
|
||||
}
|
||||
else if (consumer.ReceivedPower < consumer.DrawRate)
|
||||
{
|
||||
return ApcExternalPowerState.Low;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ApcExternalPowerState.Good;
|
||||
}
|
||||
}
|
||||
|
||||
void IActivate.Activate(ActivateEventArgs eventArgs)
|
||||
{
|
||||
if (!eventArgs.User.TryGetComponent(out ActorComponent? actor))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
UserInterface?.Open(actor.PlayerSession);
|
||||
}
|
||||
}
|
||||
}
|
||||
10
Content.Server/APC/Components/BaseApcNetComponent.cs
Normal file
10
Content.Server/APC/Components/BaseApcNetComponent.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
#nullable enable
|
||||
using Content.Server.Power.Components;
|
||||
|
||||
namespace Content.Server.APC.Components
|
||||
{
|
||||
public abstract class BaseApcNetComponent : BaseNetConnectorComponent<IApcNet>
|
||||
{
|
||||
protected override IApcNet NullNet => ApcNetNodeGroup.NullNet;
|
||||
}
|
||||
}
|
||||
19
Content.Server/APC/PowerApcSystem.cs
Normal file
19
Content.Server/APC/PowerApcSystem.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
#nullable enable
|
||||
using Content.Server.APC.Components;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Server.APC
|
||||
{
|
||||
[UsedImplicitly]
|
||||
internal sealed class PowerApcSystem : EntitySystem
|
||||
{
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
foreach (var apc in ComponentManager.EntityQuery<ApcComponent>(false))
|
||||
{
|
||||
apc.Update();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user