Remove all obsolete BatteryComponent method calls (#25871)

Removed all obsolete, non-ECS method calls to BatteryComponent
This commit is contained in:
Tayrtahn
2024-03-06 00:34:50 -05:00
committed by GitHub
parent 4228305b12
commit 2935e5bd78
12 changed files with 44 additions and 57 deletions

View File

@@ -151,7 +151,7 @@ public sealed class EmergencyLightSystem : SharedEmergencyLightSystem
} }
else else
{ {
battery.CurrentCharge += component.ChargingWattage * frameTime * component.ChargingEfficiency; _battery.SetCharge(uid, battery.CurrentCharge + component.ChargingWattage * frameTime * component.ChargingEfficiency, battery);
if (battery.IsFullyCharged) if (battery.IsFullyCharged)
{ {
if (TryComp<ApcPowerReceiverComponent>(uid, out var receiver)) if (TryComp<ApcPowerReceiverComponent>(uid, out var receiver))

View File

@@ -1,5 +1,6 @@
using Content.Server.Actions; using Content.Server.Actions;
using Content.Server.Popups; using Content.Server.Popups;
using Content.Server.Power.EntitySystems;
using Content.Server.PowerCell; using Content.Server.PowerCell;
using Content.Shared.Actions; using Content.Shared.Actions;
using Content.Shared.Examine; using Content.Shared.Examine;
@@ -25,6 +26,7 @@ namespace Content.Server.Light.EntitySystems
[Dependency] private readonly ActionContainerSystem _actionContainer = default!; [Dependency] private readonly ActionContainerSystem _actionContainer = default!;
[Dependency] private readonly PopupSystem _popup = default!; [Dependency] private readonly PopupSystem _popup = default!;
[Dependency] private readonly PowerCellSystem _powerCell = default!; [Dependency] private readonly PowerCellSystem _powerCell = default!;
[Dependency] private readonly BatterySystem _battery = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!; [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!; [Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedPointLightSystem _lights = default!; [Dependency] private readonly SharedPointLightSystem _lights = default!;
@@ -268,7 +270,7 @@ namespace Content.Server.Light.EntitySystems
_appearance.SetData(uid, HandheldLightVisuals.Power, HandheldLightPowerStates.Dying, appearanceComponent); _appearance.SetData(uid, HandheldLightVisuals.Power, HandheldLightPowerStates.Dying, appearanceComponent);
} }
if (component.Activated && !battery.TryUseCharge(component.Wattage * frameTime)) if (component.Activated && !_battery.TryUseCharge(uid, component.Wattage * frameTime, battery))
TurnOff(uid, false); TurnOff(uid, false);
UpdateLevel(uid); UpdateLevel(uid);

View File

@@ -7,56 +7,34 @@ namespace Content.Server.Power.Components
/// </summary> /// </summary>
[RegisterComponent] [RegisterComponent]
[Virtual] [Virtual]
[Access(typeof(BatterySystem))]
public partial class BatteryComponent : Component public partial class BatteryComponent : Component
{ {
[Dependency] private readonly IEntityManager _entMan = default!;
public string SolutionName = "battery"; public string SolutionName = "battery";
/// <summary> /// <summary>
/// Maximum charge of the battery in joules (ie. watt seconds) /// Maximum charge of the battery in joules (ie. watt seconds)
/// </summary> /// </summary>
[ViewVariables(VVAccess.ReadWrite)] [DataField]
public float MaxCharge public float MaxCharge;
{
get => _maxCharge;
[Obsolete("Use system method")]
set => _entMan.System<BatterySystem>().SetMaxCharge(Owner, value, this);
}
[DataField("maxCharge")]
[Access(typeof(BatterySystem))]
public float _maxCharge;
/// <summary> /// <summary>
/// Current charge of the battery in joules (ie. watt seconds) /// Current charge of the battery in joules (ie. watt seconds)
/// </summary> /// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public float CurrentCharge
{
get => Charge;
[Obsolete("Use system method")]
set => _entMan.System<BatterySystem>().SetCharge(Owner, value, this);
}
[DataField("startingCharge")] [DataField("startingCharge")]
[Access(typeof(BatterySystem))] public float CurrentCharge;
public float Charge;
/// <summary> /// <summary>
/// True if the battery is fully charged. /// True if the battery is fully charged.
/// </summary> /// </summary>
[ViewVariables] public bool IsFullyCharged => MathHelper.CloseToPercent(CurrentCharge, MaxCharge); [ViewVariables]
public bool IsFullyCharged => MathHelper.CloseToPercent(CurrentCharge, MaxCharge);
/// <summary> /// <summary>
/// The price per one joule. Default is 1 credit for 10kJ. /// The price per one joule. Default is 1 credit for 10kJ.
/// </summary> /// </summary>
[DataField("pricePerJoule")] [DataField]
[ViewVariables(VVAccess.ReadWrite)]
public float PricePerJoule = 0.0001f; public float PricePerJoule = 0.0001f;
[Obsolete("Use system method")]
public bool TryUseCharge(float value)
=> _entMan.System<BatterySystem>().TryUseCharge(Owner, value, this);
} }
/// <summary> /// <summary>

View File

@@ -62,9 +62,9 @@ namespace Content.Server.Power.EntitySystems
var enumerator = AllEntityQuery<PowerNetworkBatteryComponent, BatteryComponent>(); var enumerator = AllEntityQuery<PowerNetworkBatteryComponent, BatteryComponent>();
while (enumerator.MoveNext(out var netBat, out var bat)) while (enumerator.MoveNext(out var netBat, out var bat))
{ {
DebugTools.Assert(bat.Charge <= bat.MaxCharge && bat.Charge >= 0); DebugTools.Assert(bat.CurrentCharge <= bat.MaxCharge && bat.CurrentCharge >= 0);
netBat.NetworkBattery.Capacity = bat.MaxCharge; netBat.NetworkBattery.Capacity = bat.MaxCharge;
netBat.NetworkBattery.CurrentStorage = bat.Charge; netBat.NetworkBattery.CurrentStorage = bat.CurrentCharge;
} }
} }
@@ -108,10 +108,10 @@ namespace Content.Server.Power.EntitySystems
if (value <= 0 || !Resolve(uid, ref battery) || battery.CurrentCharge == 0) if (value <= 0 || !Resolve(uid, ref battery) || battery.CurrentCharge == 0)
return 0; return 0;
var newValue = Math.Clamp(0, battery.CurrentCharge - value, battery._maxCharge); var newValue = Math.Clamp(0, battery.CurrentCharge - value, battery.MaxCharge);
var delta = newValue - battery.Charge; var delta = newValue - battery.CurrentCharge;
battery.Charge = newValue; battery.CurrentCharge = newValue;
var ev = new ChargeChangedEvent(battery.CurrentCharge, battery._maxCharge); var ev = new ChargeChangedEvent(battery.CurrentCharge, battery.MaxCharge);
RaiseLocalEvent(uid, ref ev); RaiseLocalEvent(uid, ref ev);
return delta; return delta;
} }
@@ -121,13 +121,13 @@ namespace Content.Server.Power.EntitySystems
if (!Resolve(uid, ref battery)) if (!Resolve(uid, ref battery))
return; return;
var old = battery._maxCharge; var old = battery.MaxCharge;
battery._maxCharge = Math.Max(value, 0); battery.MaxCharge = Math.Max(value, 0);
battery.Charge = Math.Min(battery.Charge, battery._maxCharge); battery.CurrentCharge = Math.Min(battery.CurrentCharge, battery.MaxCharge);
if (MathHelper.CloseTo(battery._maxCharge, old)) if (MathHelper.CloseTo(battery.MaxCharge, old))
return; return;
var ev = new ChargeChangedEvent(battery.CurrentCharge, battery._maxCharge); var ev = new ChargeChangedEvent(battery.CurrentCharge, battery.MaxCharge);
RaiseLocalEvent(uid, ref ev); RaiseLocalEvent(uid, ref ev);
} }
@@ -136,12 +136,12 @@ namespace Content.Server.Power.EntitySystems
if (!Resolve(uid, ref battery)) if (!Resolve(uid, ref battery))
return; return;
var old = battery.Charge; var old = battery.CurrentCharge;
battery.Charge = MathHelper.Clamp(value, 0, battery._maxCharge); battery.CurrentCharge = MathHelper.Clamp(value, 0, battery.MaxCharge);
if (MathHelper.CloseTo(battery.Charge, old)) if (MathHelper.CloseTo(battery.CurrentCharge, old))
return; return;
var ev = new ChargeChangedEvent(battery.CurrentCharge, battery._maxCharge); var ev = new ChargeChangedEvent(battery.CurrentCharge, battery.MaxCharge);
RaiseLocalEvent(uid, ref ev); RaiseLocalEvent(uid, ref ev);
} }
@@ -150,7 +150,7 @@ namespace Content.Server.Power.EntitySystems
/// </summary> /// </summary>
public bool TryUseCharge(EntityUid uid, float value, BatteryComponent? battery = null) public bool TryUseCharge(EntityUid uid, float value, BatteryComponent? battery = null)
{ {
if (!Resolve(uid, ref battery, false) || value > battery.Charge) if (!Resolve(uid, ref battery, false) || value > battery.CurrentCharge)
return false; return false;
UseCharge(uid, value, battery); UseCharge(uid, value, battery);

View File

@@ -17,6 +17,7 @@ internal sealed class ChargerSystem : EntitySystem
{ {
[Dependency] private readonly ContainerSystem _container = default!; [Dependency] private readonly ContainerSystem _container = default!;
[Dependency] private readonly PowerCellSystem _powerCell = default!; [Dependency] private readonly PowerCellSystem _powerCell = default!;
[Dependency] private readonly BatterySystem _battery = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!; [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
public override void Initialize() public override void Initialize()
@@ -201,11 +202,11 @@ internal sealed class ChargerSystem : EntitySystem
if (!SearchForBattery(targetEntity, out var heldBattery)) if (!SearchForBattery(targetEntity, out var heldBattery))
return; return;
heldBattery.CurrentCharge += component.ChargeRate * frameTime; _battery.SetCharge(targetEntity, heldBattery.CurrentCharge + component.ChargeRate * frameTime, heldBattery);
// Just so the sprite won't be set to 99.99999% visibility // Just so the sprite won't be set to 99.99999% visibility
if (heldBattery.MaxCharge - heldBattery.CurrentCharge < 0.01) if (heldBattery.MaxCharge - heldBattery.CurrentCharge < 0.01)
{ {
heldBattery.CurrentCharge = heldBattery.MaxCharge; _battery.SetCharge(targetEntity, heldBattery.MaxCharge, heldBattery);
} }
UpdateStatus(uid, component); UpdateStatus(uid, component);

View File

@@ -1,5 +1,6 @@
using Content.Server.Administration; using Content.Server.Administration;
using Content.Server.Power.Components; using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Shared.Administration; using Content.Shared.Administration;
using Robust.Shared.Console; using Robust.Shared.Console;
@@ -39,7 +40,8 @@ namespace Content.Server.Power
shell.WriteLine($"No battery found with id {id}."); shell.WriteLine($"No battery found with id {id}.");
return; return;
} }
battery.CurrentCharge = (battery.MaxCharge * percent) / 100; var system = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<BatterySystem>();
system.SetCharge(id.Value, battery.MaxCharge * percent / 100, battery);
// Don't acknowledge b/c people WILL forall this // Don't acknowledge b/c people WILL forall this
} }
} }

View File

@@ -179,7 +179,7 @@ public sealed partial class PowerCellSystem : SharedPowerCellSystem
return false; return false;
} }
_sharedAppearanceSystem.SetData(uid, PowerCellSlotVisuals.Enabled, battery.Charge > 0); _sharedAppearanceSystem.SetData(uid, PowerCellSlotVisuals.Enabled, battery.CurrentCharge > 0);
return true; return true;
} }

View File

@@ -7,6 +7,7 @@ using Content.Server.Station.Systems;
using Robust.Shared.Timing; using Robust.Shared.Timing;
using Robust.Shared.Audio; using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems; using Robust.Shared.Audio.Systems;
using Content.Server.Power.EntitySystems;
namespace Content.Server.PowerSink namespace Content.Server.PowerSink
{ {
@@ -31,6 +32,7 @@ namespace Content.Server.PowerSink
[Dependency] private readonly ExplosionSystem _explosionSystem = default!; [Dependency] private readonly ExplosionSystem _explosionSystem = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!; [Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly StationSystem _station = default!; [Dependency] private readonly StationSystem _station = default!;
[Dependency] private readonly BatterySystem _battery = default!;
public override void Initialize() public override void Initialize()
{ {
@@ -64,7 +66,7 @@ namespace Content.Server.PowerSink
if (!transform.Anchored) if (!transform.Anchored)
continue; continue;
battery.CurrentCharge += networkLoad.NetworkLoad.ReceivingPower / 1000; _battery.SetCharge(entity, battery.CurrentCharge + networkLoad.NetworkLoad.ReceivingPower / 1000, battery);
var currentBatteryThreshold = battery.CurrentCharge / battery.MaxCharge; var currentBatteryThreshold = battery.CurrentCharge / battery.MaxCharge;

View File

@@ -1,4 +1,5 @@
using Content.Server.Popups; using Content.Server.Popups;
using Content.Server.Power.EntitySystems;
using Content.Server.PowerCell; using Content.Server.PowerCell;
using Content.Server.Radio.Components; using Content.Server.Radio.Components;
using Content.Shared.Examine; using Content.Shared.Examine;
@@ -10,6 +11,7 @@ namespace Content.Server.Radio.EntitySystems;
public sealed class JammerSystem : EntitySystem public sealed class JammerSystem : EntitySystem
{ {
[Dependency] private readonly PowerCellSystem _powerCell = default!; [Dependency] private readonly PowerCellSystem _powerCell = default!;
[Dependency] private readonly BatterySystem _battery = default!;
[Dependency] private readonly PopupSystem _popup = default!; [Dependency] private readonly PopupSystem _popup = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!; [Dependency] private readonly SharedTransformSystem _transform = default!;
@@ -29,7 +31,7 @@ public sealed class JammerSystem : EntitySystem
while (query.MoveNext(out var uid, out var _, out var jam)) while (query.MoveNext(out var uid, out var _, out var jam))
{ {
if (_powerCell.TryGetBatteryFromSlot(uid, out var battery) && if (_powerCell.TryGetBatteryFromSlot(uid, out var battery) &&
!battery.TryUseCharge(jam.Wattage * frameTime)) !_battery.TryUseCharge(uid, jam.Wattage * frameTime, battery))
{ {
RemComp<ActiveRadioJammerComponent>(uid); RemComp<ActiveRadioJammerComponent>(uid);
} }
@@ -38,7 +40,7 @@ public sealed class JammerSystem : EntitySystem
private void OnActivate(EntityUid uid, RadioJammerComponent comp, ActivateInWorldEvent args) private void OnActivate(EntityUid uid, RadioJammerComponent comp, ActivateInWorldEvent args)
{ {
var activated = !HasComp<ActiveRadioJammerComponent>(uid) && var activated = !HasComp<ActiveRadioJammerComponent>(uid) &&
_powerCell.TryGetBatteryFromSlot(uid, out var battery) && _powerCell.TryGetBatteryFromSlot(uid, out var battery) &&
battery.CurrentCharge > comp.Wattage; battery.CurrentCharge > comp.Wattage;
if (activated) if (activated)

View File

@@ -31,7 +31,7 @@ public sealed class BatterySensorSystem : EntitySystem
{ {
[DeviceNetworkConstants.Command] = DeviceNetworkCommandSyncData, [DeviceNetworkConstants.Command] = DeviceNetworkCommandSyncData,
[DeviceNetworkCommandSyncData] = new BatterySensorData( [DeviceNetworkCommandSyncData] = new BatterySensorData(
battery.Charge, battery.CurrentCharge,
battery.MaxCharge, battery.MaxCharge,
netBattery.CurrentReceiving, netBattery.CurrentReceiving,
netBattery.MaxChargeRate, netBattery.MaxChargeRate,

View File

@@ -107,7 +107,7 @@ public sealed partial class BorgSystem
if (_powerCell.TryGetBatteryFromSlot(uid, out var battery)) if (_powerCell.TryGetBatteryFromSlot(uid, out var battery))
{ {
hasBattery = true; hasBattery = true;
chargePercent = battery.Charge / battery.MaxCharge; chargePercent = battery.CurrentCharge / battery.MaxCharge;
} }
var state = new BorgBuiState(chargePercent, hasBattery); var state = new BorgBuiState(chargePercent, hasBattery);

View File

@@ -41,7 +41,7 @@ public sealed partial class GunSystem
if (!TryComp<BatteryComponent>(uid, out var battery)) if (!TryComp<BatteryComponent>(uid, out var battery))
return; return;
UpdateShots(uid, component, battery.Charge, battery.MaxCharge); UpdateShots(uid, component, battery.CurrentCharge, battery.MaxCharge);
} }
private void UpdateShots(EntityUid uid, BatteryAmmoProviderComponent component, float charge, float maxCharge) private void UpdateShots(EntityUid uid, BatteryAmmoProviderComponent component, float charge, float maxCharge)