Sync charger and battery systems with upstream
Some checks failed
Build & Test Debug / build (ubuntu-latest) (pull_request) Failing after 8m55s
Build & Test Debug / Build & Test Debug (pull_request) Has been skipped
Build & Test Map Renderer / build (ubuntu-latest) (pull_request) Failing after 9m11s
Build & Test Map Renderer / Build & Test Debug (pull_request) Has been skipped
CRLF Check / CRLF Check (pull_request) Successful in 47s
RGA schema validator / YAML RGA schema validator (pull_request) Successful in 1m11s
Labels: PR / labeler (pull_request_target) Has been cancelled
YAML Linter / YAML Linter (pull_request) Has been cancelled
Map file schema validator / YAML map schema validator (pull_request) Successful in 10m26s
Check Merge Conflicts / check-conflicts (pull_request_target) Successful in 3s
Labels: Size / size-label (pull_request_target) Successful in 3s
Test Packaging / Test Packaging (pull_request) Successful in 29m15s

This commit is contained in:
Codex
2025-12-22 16:46:09 +01:00
parent d61fdb2ac6
commit cfe3ef8efe
6 changed files with 270 additions and 289 deletions

View File

@@ -1,5 +0,0 @@
using Content.Shared.Power.EntitySystems;
namespace Content.Client.Power.EntitySystems;
public sealed class ChargerSystem : SharedChargerSystem;

View File

@@ -12,6 +12,7 @@ using Content.Shared.Item.ItemToggle.Components;
using Content.Shared.MedicalScanner;
using Content.Shared.Mobs.Components;
using Content.Shared.Popups;
using Content.Shared.PowerCell.Components;
using Content.Shared.Temperature.Components;
using Content.Shared.Traits.Assorted;
using Robust.Server.GameObjects;
@@ -83,7 +84,11 @@ public sealed class HealthAnalyzerSystem : EntitySystem
/// </summary>
private void OnAfterInteract(Entity<HealthAnalyzerComponent> uid, ref AfterInteractEvent args)
{
if (args.Target == null || !args.CanReach || !HasComp<MobStateComponent>(args.Target) || !_cell.HasDrawCharge(uid, user: args.User))
TryComp(uid, out PowerCellDrawComponent? draw);
TryComp(uid, out PowerCellSlotComponent? slot);
if (args.Target == null || !args.CanReach || !HasComp<MobStateComponent>(args.Target) ||
!_cell.HasDrawCharge((uid.Owner, draw, slot), user: args.User))
return;
_audio.PlayPvs(uid.Comp.ScanningBeginSound, uid);
@@ -103,7 +108,11 @@ public sealed class HealthAnalyzerSystem : EntitySystem
private void OnDoAfter(Entity<HealthAnalyzerComponent> uid, ref HealthAnalyzerDoAfterEvent args)
{
if (args.Handled || args.Cancelled || args.Target == null || !_cell.HasDrawCharge(uid, user: args.User))
TryComp(uid, out PowerCellDrawComponent? draw);
TryComp(uid, out PowerCellSlotComponent? slot);
if (args.Handled || args.Cancelled || args.Target == null ||
!_cell.HasDrawCharge((uid.Owner, draw, slot), user: args.User))
return;
if (!uid.Comp.Silent)

View File

@@ -26,7 +26,7 @@ public sealed partial class BatterySystem
TrySetChargeCooldown(ent.Owner);
var ev = new ChargeChangedEvent(ent.Comp.CurrentCharge, ent.Comp.MaxCharge);
var ev = new ChargeChangedEvent(ent.Comp.CurrentCharge, delta, ent.Comp.MaxCharge);
RaiseLocalEvent(ent, ref ev);
return delta;
}
@@ -61,21 +61,23 @@ public sealed partial class BatterySystem
return;
}
var ev = new ChargeChangedEvent(ent.Comp.CurrentCharge, ent.Comp.MaxCharge);
var ev = new ChargeChangedEvent(ent.Comp.CurrentCharge, ent.Comp.CurrentCharge - oldCharge, ent.Comp.MaxCharge);
RaiseLocalEvent(ent, ref ev);
}
public override void SetMaxCharge(Entity<BatteryComponent?> ent, float value)
{
if (!Resolve(ent, ref ent.Comp))
return;
var old = ent.Comp.MaxCharge;
var oldCharge = ent.Comp.CurrentCharge;
ent.Comp.MaxCharge = Math.Max(value, 0);
ent.Comp.CurrentCharge = Math.Min(ent.Comp.CurrentCharge, ent.Comp.MaxCharge);
if (MathHelper.CloseTo(ent.Comp.MaxCharge, old))
return;
var ev = new ChargeChangedEvent(ent.Comp.CurrentCharge, ent.Comp.MaxCharge);
var ev = new ChargeChangedEvent(ent.Comp.CurrentCharge, ent.Comp.CurrentCharge - oldCharge, ent.Comp.MaxCharge);
RaiseLocalEvent(ent, ref ev);
}

View File

@@ -1,259 +0,0 @@
using Content.Server.Power.Components;
using Content.Shared.Examine;
using Content.Shared.PowerCell;
using Content.Shared.Power;
using Content.Shared.Power.Components;
using Content.Shared.Power.EntitySystems;
using Content.Shared.PowerCell.Components;
using Content.Shared.Emp;
using JetBrains.Annotations;
using Robust.Shared.Containers;
using System.Diagnostics.CodeAnalysis;
using Content.Shared.Storage.Components;
using Robust.Server.Containers;
using Content.Shared.Whitelist;
namespace Content.Server.Power.EntitySystems;
[UsedImplicitly]
public sealed class ChargerSystem : SharedChargerSystem
{
[Dependency] private readonly ContainerSystem _container = default!;
[Dependency] private readonly PowerCellSystem _powerCell = default!;
[Dependency] private readonly BatterySystem _battery = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ChargerComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<ChargerComponent, PowerChangedEvent>(OnPowerChanged);
SubscribeLocalEvent<ChargerComponent, EntInsertedIntoContainerMessage>(OnInserted);
SubscribeLocalEvent<ChargerComponent, EntRemovedFromContainerMessage>(OnRemoved);
SubscribeLocalEvent<ChargerComponent, ContainerIsInsertingAttemptEvent>(OnInsertAttempt);
SubscribeLocalEvent<ChargerComponent, InsertIntoEntityStorageAttemptEvent>(OnEntityStorageInsertAttempt);
SubscribeLocalEvent<ChargerComponent, ExaminedEvent>(OnChargerExamine);
}
private void OnStartup(EntityUid uid, ChargerComponent component, ComponentStartup args)
{
UpdateStatus(uid, component);
}
private void OnChargerExamine(EntityUid uid, ChargerComponent component, ExaminedEvent args)
{
using (args.PushGroup(nameof(ChargerComponent)))
{
// rate at which the charger charges
args.PushMarkup(Loc.GetString("charger-examine", ("color", "yellow"), ("chargeRate", (int)component.ChargeRate)));
// try to get contents of the charger
if (!_container.TryGetContainer(uid, component.SlotId, out var container))
return;
if (HasComp<PowerCellSlotComponent>(uid))
return;
// if charger is empty and not a power cell type charger, add empty message
// power cells have their own empty message by default, for things like flash lights
if (container.ContainedEntities.Count == 0)
{
args.PushMarkup(Loc.GetString("charger-empty"));
}
else
{
// add how much each item is charged it
foreach (var contained in container.ContainedEntities)
{
if (!TryComp<BatteryComponent>(contained, out var battery))
continue;
var chargePercentage = (battery.CurrentCharge / battery.MaxCharge) * 100;
args.PushMarkup(Loc.GetString("charger-content", ("chargePercentage", (int)chargePercentage)));
}
}
}
}
public override void Update(float frameTime)
{
var query = EntityQueryEnumerator<ActiveChargerComponent, ChargerComponent, ContainerManagerComponent>();
while (query.MoveNext(out var uid, out _, out var charger, out var containerComp))
{
if (!_container.TryGetContainer(uid, charger.SlotId, out var container, containerComp))
continue;
if (charger.Status == CellChargerStatus.Empty || charger.Status == CellChargerStatus.Charged || container.ContainedEntities.Count == 0)
continue;
foreach (var contained in container.ContainedEntities)
{
TransferPower(uid, contained, charger, frameTime);
}
}
}
private void OnPowerChanged(EntityUid uid, ChargerComponent component, ref PowerChangedEvent args)
{
UpdateStatus(uid, component);
}
private void OnInserted(EntityUid uid, ChargerComponent component, EntInsertedIntoContainerMessage args)
{
if (!component.Initialized)
return;
if (args.Container.ID != component.SlotId)
return;
UpdateStatus(uid, component);
}
private void OnRemoved(EntityUid uid, ChargerComponent component, EntRemovedFromContainerMessage args)
{
if (args.Container.ID != component.SlotId)
return;
UpdateStatus(uid, component);
}
/// <summary>
/// Verify that the entity being inserted is actually rechargeable.
/// </summary>
private void OnInsertAttempt(EntityUid uid, ChargerComponent component, ContainerIsInsertingAttemptEvent args)
{
if (!component.Initialized)
return;
if (args.Container.ID != component.SlotId)
return;
if (!TryComp<PowerCellSlotComponent>(args.EntityUid, out var cellSlot))
return;
if (!cellSlot.FitsInCharger)
args.Cancel();
}
private void OnEntityStorageInsertAttempt(EntityUid uid, ChargerComponent component, ref InsertIntoEntityStorageAttemptEvent args)
{
if (!component.Initialized || args.Cancelled)
return;
if (!TryComp<PowerCellSlotComponent>(uid, out var cellSlot))
return;
if (!cellSlot.FitsInCharger)
args.Cancelled = true;
}
private void UpdateStatus(EntityUid uid, ChargerComponent component)
{
var status = GetStatus(uid, component);
TryComp(uid, out AppearanceComponent? appearance);
if (!_container.TryGetContainer(uid, component.SlotId, out var container))
return;
_appearance.SetData(uid, CellVisual.Occupied, container.ContainedEntities.Count != 0, appearance);
if (component.Status == status || !TryComp(uid, out ApcPowerReceiverComponent? receiver))
return;
component.Status = status;
if (component.Status == CellChargerStatus.Charging)
{
AddComp<ActiveChargerComponent>(uid);
}
else
{
RemComp<ActiveChargerComponent>(uid);
}
switch (component.Status)
{
case CellChargerStatus.Off:
receiver.Load = 0;
_appearance.SetData(uid, CellVisual.Light, CellChargerStatus.Off, appearance);
break;
case CellChargerStatus.Empty:
receiver.Load = 0;
_appearance.SetData(uid, CellVisual.Light, CellChargerStatus.Empty, appearance);
break;
case CellChargerStatus.Charging:
receiver.Load = component.ChargeRate;
_appearance.SetData(uid, CellVisual.Light, CellChargerStatus.Charging, appearance);
break;
case CellChargerStatus.Charged:
receiver.Load = 0;
_appearance.SetData(uid, CellVisual.Light, CellChargerStatus.Charged, appearance);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
private CellChargerStatus GetStatus(EntityUid uid, ChargerComponent component)
{
if (!component.Portable)
{
if (!TryComp(uid, out TransformComponent? transformComponent) || !transformComponent.Anchored)
return CellChargerStatus.Off;
}
if (!TryComp(uid, out ApcPowerReceiverComponent? apcPowerReceiverComponent))
return CellChargerStatus.Off;
if (!component.Portable && !apcPowerReceiverComponent.Powered)
return CellChargerStatus.Off;
if (HasComp<EmpDisabledComponent>(uid))
return CellChargerStatus.Off;
if (!_container.TryGetContainer(uid, component.SlotId, out var container))
return CellChargerStatus.Off;
if (container.ContainedEntities.Count == 0)
return CellChargerStatus.Empty;
if (!SearchForBattery(container.ContainedEntities[0], out var heldEnt, out var heldBattery))
return CellChargerStatus.Off;
if (_battery.IsFull(heldEnt.Value, heldBattery))
return CellChargerStatus.Charged;
return CellChargerStatus.Charging;
}
private void TransferPower(EntityUid uid, EntityUid targetEntity, ChargerComponent component, float frameTime)
{
if (!TryComp(uid, out ApcPowerReceiverComponent? receiverComponent))
return;
if (!receiverComponent.Powered)
return;
if (_whitelistSystem.IsWhitelistFail(component.Whitelist, targetEntity))
return;
if (!SearchForBattery(targetEntity, out var batteryUid, out var heldBattery))
return;
_battery.SetCharge(batteryUid.Value, heldBattery.CurrentCharge + component.ChargeRate * frameTime, heldBattery);
UpdateStatus(uid, component);
}
private bool SearchForBattery(EntityUid uid, [NotNullWhen(true)] out EntityUid? batteryUid, [NotNullWhen(true)] out BatteryComponent? component)
{
// try get a battery directly on the inserted entity
if (!TryComp(uid, out component))
{
// or by checking for a power cell slot on the inserted entity
return _powerCell.TryGetBatteryFromSlot(uid, out batteryUid, out component);
}
batteryUid = uid;
return true;
}
}

View File

@@ -0,0 +1,254 @@
using System.Diagnostics.CodeAnalysis;
using Content.Shared.Emp;
using Content.Shared.Examine;
using Content.Shared.Power.Components;
using Content.Shared.PowerCell;
using Content.Shared.PowerCell.Components;
using Content.Shared.Storage.Components;
using Content.Shared.Whitelist;
using Robust.Shared.Containers;
using Robust.Shared.Timing;
namespace Content.Shared.Power.EntitySystems;
public sealed class ChargerSystem : EntitySystem
{
[Dependency] private readonly PredictedBatterySystem _battery = default!;
[Dependency] private readonly SharedPowerReceiverSystem _receiver = default!;
[Dependency] private readonly EntityWhitelistSystem _whitelist = default!;
[Dependency] private readonly SharedContainerSystem _container = default!;
[Dependency] private readonly PowerCellSystem _powerCell = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ChargerComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<ChargerComponent, PowerChangedEvent>(OnPowerChanged);
SubscribeLocalEvent<ChargerComponent, EntInsertedIntoContainerMessage>(OnInserted);
SubscribeLocalEvent<ChargerComponent, EntRemovedFromContainerMessage>(OnRemoved);
SubscribeLocalEvent<ChargerComponent, ContainerIsInsertingAttemptEvent>(OnInsertAttempt);
SubscribeLocalEvent<ChargerComponent, InsertIntoEntityStorageAttemptEvent>(OnEntityStorageInsertAttempt);
SubscribeLocalEvent<ChargerComponent, ExaminedEvent>(OnChargerExamine);
SubscribeLocalEvent<ChargerComponent, EmpPulseEvent>(OnEmpPulse);
SubscribeLocalEvent<ChargerComponent, EmpDisabledRemovedEvent>(OnEmpRemoved);
SubscribeLocalEvent<InsideChargerComponent, RefreshChargeRateEvent>(OnRefreshChargeRate);
SubscribeLocalEvent<InsideChargerComponent, PredictedBatteryStateChangedEvent>(OnStatusChanged);
}
private void OnStartup(Entity<ChargerComponent> ent, ref ComponentStartup args)
{
UpdateStatus(ent);
}
private void OnChargerExamine(EntityUid uid, ChargerComponent component, ExaminedEvent args)
{
using (args.PushGroup(nameof(ChargerComponent)))
{
// rate at which the charger charges
args.PushMarkup(Loc.GetString("charger-examine", ("color", "yellow"), ("chargeRate", (int)component.ChargeRate)));
// try to get contents of the charger
if (!_container.TryGetContainer(uid, component.SlotId, out var container))
return;
if (HasComp<PowerCellSlotComponent>(uid))
return;
// if charger is empty and not a power cell type charger, add empty message
// power cells have their own empty message by default, for things like flash lights
if (container.ContainedEntities.Count == 0)
{
args.PushMarkup(Loc.GetString("charger-empty"));
}
else
{
// add how much each item is charged it
foreach (var contained in container.ContainedEntities)
{
if (!_powerCell.TryGetBatteryFromEntityOrSlot(contained, out var battery))
continue;
var chargePercent = _battery.GetChargeLevel(battery.Value.AsNullable()) * 100;
args.PushMarkup(Loc.GetString("charger-content", ("chargePercent", (int)chargePercent)));
}
}
}
}
private void OnPowerChanged(Entity<ChargerComponent> ent, ref PowerChangedEvent args)
{
RefreshAllBatteries(ent);
UpdateStatus(ent);
}
private void OnInserted(Entity<ChargerComponent> ent, ref EntInsertedIntoContainerMessage args)
{
if (_timing.ApplyingState)
return; // Already networked in the same gamestate
if (args.Container.ID != ent.Comp.SlotId)
return;
AddComp<InsideChargerComponent>(args.Entity);
if (_powerCell.TryGetBatteryFromEntityOrSlot(args.Entity, out var battery))
_battery.RefreshChargeRate(battery.Value.AsNullable());
UpdateStatus(ent);
}
private void OnRemoved(Entity<ChargerComponent> ent, ref EntRemovedFromContainerMessage args)
{
if (_timing.ApplyingState)
return; // Already networked in the same gamestate
if (args.Container.ID != ent.Comp.SlotId)
return;
RemComp<InsideChargerComponent>(args.Entity);
if (_powerCell.TryGetBatteryFromEntityOrSlot(args.Entity, out var battery))
_battery.RefreshChargeRate(battery.Value.AsNullable());
UpdateStatus(ent);
}
/// <summary>
/// Verify that the entity being inserted is actually rechargeable.
/// </summary>
private void OnInsertAttempt(EntityUid uid, ChargerComponent component, ContainerIsInsertingAttemptEvent args)
{
if (!component.Initialized)
return;
if (args.Container.ID != component.SlotId)
return;
if (!TryComp<PowerCellSlotComponent>(args.EntityUid, out var cellSlot))
return;
if (!cellSlot.FitsInCharger)
args.Cancel();
}
private void OnEntityStorageInsertAttempt(EntityUid uid, ChargerComponent component, ref InsertIntoEntityStorageAttemptEvent args)
{
if (!component.Initialized || args.Cancelled)
return;
if (args.Container.ID != component.SlotId)
return;
if (!TryComp<PowerCellSlotComponent>(uid, out var cellSlot))
return;
if (!cellSlot.FitsInCharger)
args.Cancelled = true;
}
private void OnEmpPulse(Entity<ChargerComponent> ent, ref EmpPulseEvent args)
{
args.Affected = true;
args.Disabled = true;
RefreshAllBatteries(ent);
UpdateStatus(ent);
}
private void OnEmpRemoved(Entity<ChargerComponent> ent, ref EmpDisabledRemovedEvent args)
{
RefreshAllBatteries(ent);
UpdateStatus(ent);
}
private void OnRefreshChargeRate(Entity<InsideChargerComponent> ent, ref RefreshChargeRateEvent args)
{
var chargerUid = Transform(ent).ParentUid;
if (HasComp<EmpDisabledComponent>(chargerUid))
return;
if (!TryComp<ChargerComponent>(chargerUid, out var chargerComp))
return;
if (!chargerComp.Portable && !_receiver.IsPowered(chargerUid))
return;
if (_whitelist.IsWhitelistFail(chargerComp.Whitelist, ent.Owner))
return;
args.NewChargeRate += chargerComp.ChargeRate;
}
private void OnStatusChanged(Entity<InsideChargerComponent> ent, ref PredictedBatteryStateChangedEvent args)
{
// If the battery is full update the visuals and power draw of the charger.
var chargerUid = Transform(ent).ParentUid;
if (!TryComp<ChargerComponent>(chargerUid, out var chargerComp))
return;
UpdateStatus((chargerUid, chargerComp));
}
private void RefreshAllBatteries(Entity<ChargerComponent> ent)
{
// try to get contents of the charger
if (!_container.TryGetContainer(ent.Owner, ent.Comp.SlotId, out var container))
return;
foreach (var item in container.ContainedEntities)
{
if (_powerCell.TryGetBatteryFromEntityOrSlot(item, out var battery))
_battery.RefreshChargeRate(battery.Value.AsNullable());
}
}
private void UpdateStatus(Entity<ChargerComponent> ent)
{
TryComp<AppearanceComponent>(ent, out var appearance);
if (!_container.TryGetContainer(ent.Owner, ent.Comp.SlotId, out var container))
return;
_appearance.SetData(ent.Owner, CellVisual.Occupied, container.ContainedEntities.Count != 0, appearance);
var status = GetStatus(ent);
switch (status)
{
case CellChargerStatus.Charging:
// TODO: If someone ever adds chargers that can charge multiple batteries at once then set this to the total draw rate.
_receiver.SetLoad(ent.Owner, ent.Comp.ChargeRate);
break;
default:
// Don't set the load to 0 or the charger will be considered as powered even if the LV connection is unpowered.
// TODO: Fix this on an ApcPowerReceiver level.
_receiver.SetLoad(ent.Owner, ent.Comp.PassiveDraw);
break;
}
_appearance.SetData(ent.Owner, CellVisual.Light, status, appearance);
}
private CellChargerStatus GetStatus(Entity<ChargerComponent> ent)
{
if (!ent.Comp.Portable && !Transform(ent).Anchored)
return CellChargerStatus.Off;
if (!ent.Comp.Portable && !_receiver.IsPowered(ent.Owner))
return CellChargerStatus.Off;
if (HasComp<EmpDisabledComponent>(ent))
return CellChargerStatus.Off;
if (!_container.TryGetContainer(ent.Owner, ent.Comp.SlotId, out var container))
return CellChargerStatus.Off;
if (container.ContainedEntities.Count == 0)
return CellChargerStatus.Empty;
// Use the first stored battery for visuals. If someone ever makes a multi-slot charger then this will need to be changed.
if (!_powerCell.TryGetBatteryFromEntityOrSlot(container.ContainedEntities[0], out var battery))
return CellChargerStatus.Off;
if (_battery.IsFull(battery.Value.AsNullable()))
return CellChargerStatus.Charged;
return CellChargerStatus.Charging;
}
}

View File

@@ -1,20 +0,0 @@
using Content.Shared.Emp;
using Content.Shared.Power.Components;
namespace Content.Shared.Power.EntitySystems;
public abstract class SharedChargerSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ChargerComponent, EmpPulseEvent>(OnEmpPulse);
}
private void OnEmpPulse(EntityUid uid, ChargerComponent component, ref EmpPulseEvent args)
{
args.Affected = true;
args.Disabled = true;
}
}