2021-07-04 18:11:52 +02:00
|
|
|
using Content.Server.Power.NodeGroups;
|
|
|
|
|
using Content.Server.Power.Pow3r;
|
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
|
|
|
{
|
|
|
|
|
/// <summary>
|
2021-07-04 18:11:52 +02:00
|
|
|
/// Attempts to link with a nearby <see cref="ApcPowerProviderComponent"/>s
|
|
|
|
|
/// so that it can receive power from a <see cref="IApcNet"/>.
|
2020-06-28 09:23:26 -06:00
|
|
|
/// </summary>
|
|
|
|
|
[RegisterComponent]
|
2023-08-22 18:14:33 -07:00
|
|
|
public sealed partial class ApcPowerReceiverComponent : Component
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
|
|
|
|
[ViewVariables]
|
2021-09-29 20:07:01 +10:00
|
|
|
public bool Powered => (MathHelper.CloseToPercent(NetworkLoad.ReceivingPower, Load) || !NeedsPower) && !PowerDisabled;
|
2020-06-28 09:23:26 -06:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Amount of charge this needs from an APC per second to function.
|
|
|
|
|
/// </summary>
|
2020-07-26 04:14:03 -06:00
|
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("powerLoad")]
|
2021-07-04 18:11:52 +02:00
|
|
|
public float Load { get => NetworkLoad.DesiredPower; set => NetworkLoad.DesiredPower = value; }
|
2020-06-28 09:23:26 -06:00
|
|
|
|
2021-10-25 21:55:00 +02:00
|
|
|
public ApcPowerProviderComponent? Provider = null;
|
|
|
|
|
|
2020-06-28 09:23:26 -06:00
|
|
|
/// <summary>
|
2020-09-13 06:08:23 -06:00
|
|
|
/// When false, causes this to appear powered even if not receiving power from an Apc.
|
2020-06-28 09:23:26 -06:00
|
|
|
/// </summary>
|
|
|
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
2021-07-04 18:11:52 +02:00
|
|
|
public bool NeedsPower
|
|
|
|
|
{
|
|
|
|
|
get => _needsPower;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_needsPower = value;
|
|
|
|
|
// Reset this so next tick will do a power update.
|
2022-04-14 01:50:42 +12:00
|
|
|
PoweredLastUpdate = null;
|
2021-07-04 18:11:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("needsPower")]
|
|
|
|
|
private bool _needsPower = true;
|
2020-06-28 09:23:26 -06:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// When true, causes this to never appear powered.
|
|
|
|
|
/// </summary>
|
2020-09-13 06:08:23 -06:00
|
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("powerDisabled")]
|
2022-05-05 19:35:06 -07:00
|
|
|
public bool PowerDisabled {
|
|
|
|
|
get => !NetworkLoad.Enabled;
|
|
|
|
|
set => NetworkLoad.Enabled = !value;
|
|
|
|
|
}
|
2021-07-04 18:11:52 +02:00
|
|
|
|
2022-04-14 01:50:42 +12:00
|
|
|
public bool? PoweredLastUpdate;
|
2023-08-22 18:29:10 -07:00
|
|
|
public float LastPowerReceived;
|
2021-07-04 18:11:52 +02:00
|
|
|
|
|
|
|
|
[ViewVariables]
|
|
|
|
|
public PowerState.Load NetworkLoad { get; } = new PowerState.Load
|
|
|
|
|
{
|
|
|
|
|
DesiredPower = 5
|
|
|
|
|
};
|
2020-06-28 09:23:26 -06:00
|
|
|
|
2021-11-02 01:12:55 +01:00
|
|
|
public float PowerReceived => NetworkLoad.ReceivingPower;
|
|
|
|
|
|
2021-10-25 21:55:00 +02:00
|
|
|
protected override void OnRemove()
|
|
|
|
|
{
|
|
|
|
|
Provider?.RemoveReceiver(this);
|
|
|
|
|
|
|
|
|
|
base.OnRemove();
|
|
|
|
|
}
|
2020-06-28 09:23:26 -06:00
|
|
|
}
|
|
|
|
|
|
2021-07-16 12:41:01 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Raised whenever an ApcPowerReceiver becomes powered / unpowered.
|
2023-02-13 07:20:39 -05:00
|
|
|
/// Does nothing on the client.
|
2021-07-16 12:41:01 +10:00
|
|
|
/// </summary>
|
2022-10-15 15:08:15 +11:00
|
|
|
[ByRefEvent]
|
|
|
|
|
public readonly record struct PowerChangedEvent(bool Powered, float ReceivingPower)
|
2021-07-16 12:41:01 +10:00
|
|
|
{
|
2022-10-15 15:08:15 +11:00
|
|
|
public readonly bool Powered = Powered;
|
|
|
|
|
public readonly float ReceivingPower = ReceivingPower;
|
2021-07-16 12:41:01 +10:00
|
|
|
}
|
2023-02-13 07:20:39 -05:00
|
|
|
|
2020-06-28 09:23:26 -06:00
|
|
|
}
|