Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ngines into feat/floof-upstream-merge-2024-09-15
  • Loading branch information
Mnemotechnician committed Sep 15, 2024
2 parents a6a4411 + 41a8b1b commit 841a49c
Show file tree
Hide file tree
Showing 16 changed files with 190 additions and 158 deletions.
7 changes: 7 additions & 0 deletions Content.Server/Power/Components/ActiveChargerComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Content.Shared.Containers.ItemSlots;
using Content.Shared.Power;

namespace Content.Server.Power.Components;

[RegisterComponent]
public sealed partial class ActiveChargerComponent : Component { }
19 changes: 0 additions & 19 deletions Content.Server/Power/Components/ChargingComponent.cs

This file was deleted.

16 changes: 0 additions & 16 deletions Content.Server/Power/EntitySystems/BatterySystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public override void Initialize()
SubscribeLocalEvent<BatteryComponent, RejuvenateEvent>(OnBatteryRejuvenate);
SubscribeLocalEvent<BatteryComponent, PriceCalculationEvent>(CalculateBatteryPrice);
SubscribeLocalEvent<BatteryComponent, EmpPulseEvent>(OnEmpPulse);
SubscribeLocalEvent<BatteryComponent, EmpDisabledRemoved>(OnEmpDisabledRemoved);

SubscribeLocalEvent<NetworkBatteryPreSync>(PreSync);
SubscribeLocalEvent<NetworkBatteryPostSync>(PostSync);
Expand Down Expand Up @@ -106,17 +105,6 @@ private void OnEmpPulse(EntityUid uid, BatteryComponent component, ref EmpPulseE
UseCharge(uid, args.EnergyConsumption, component);
}

// if a disabled battery is put into a recharged,
// allow the recharger to start recharging again after the disable ends
private void OnEmpDisabledRemoved(EntityUid uid, BatteryComponent component, ref EmpDisabledRemoved args)
{
if (!TryComp<ChargingComponent>(uid, out var charging))
return;

var ev = new ChargerUpdateStatusEvent();
RaiseLocalEvent<ChargerUpdateStatusEvent>(charging.ChargerUid, ref ev);
}

public float UseCharge(EntityUid uid, float value, BatteryComponent? battery = null)
{
if (value <= 0 || !Resolve(uid, ref battery) || battery.CurrentCharge == 0)
Expand Down Expand Up @@ -191,10 +179,6 @@ public bool IsFull(EntityUid uid, BatteryComponent? battery = null)
if (!Resolve(uid, ref battery))
return false;

// If the battery is full, remove its charging component.
if (TryComp<ChargingComponent>(uid, out _))
RemComp<ChargingComponent>(uid);

return battery.CurrentCharge / battery.MaxCharge >= 0.99f;
}
}
Expand Down
149 changes: 27 additions & 122 deletions Content.Server/Power/EntitySystems/ChargerSystem.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
using Content.Server.Power.Components;
using Content.Server.Emp;
using Content.Server.PowerCell;
using Content.Shared.Examine;
using Content.Shared.Power;
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;

Expand All @@ -31,11 +28,6 @@ public override void Initialize()
SubscribeLocalEvent<ChargerComponent, ContainerIsInsertingAttemptEvent>(OnInsertAttempt);
SubscribeLocalEvent<ChargerComponent, InsertIntoEntityStorageAttemptEvent>(OnEntityStorageInsertAttempt);
SubscribeLocalEvent<ChargerComponent, ExaminedEvent>(OnChargerExamine);

SubscribeLocalEvent<ChargerComponent, ChargerUpdateStatusEvent>(OnUpdateStatus);

SubscribeLocalEvent<ChargerComponent, EmpPulseEvent>(OnEmpPulse);
SubscribeLocalEvent<ChargerComponent, EmpDisabledRemoved>(OnEmpDisabledRemoved);
}

private void OnStartup(EntityUid uid, ChargerComponent component, ComponentStartup args)
Expand All @@ -48,58 +40,21 @@ private void OnChargerExamine(EntityUid uid, ChargerComponent component, Examine
args.PushMarkup(Loc.GetString("charger-examine", ("color", "yellow"), ("chargeRate", (int) component.ChargeRate)));
}

private void StartChargingBattery(EntityUid uid, ChargerComponent component, EntityUid target)
{
bool charge = true;

if (HasComp<EmpDisabledComponent>(uid))
charge = false;
else
if (!TryComp<BatteryComponent>(target, out var battery))
charge = false;
else
if (Math.Abs(battery.MaxCharge - battery.CurrentCharge) < 0.01)
charge = false;

// wrap functionality in an if statement instead of returning...
if (charge)
{
var charging = EnsureComp<ChargingComponent>(target);
charging.ChargerUid = uid;
charging.ChargerComponent = component;
}

// ...so the status always updates (for insertin a power cell)
UpdateStatus(uid, component);
}

private void StopChargingBattery(EntityUid uid, ChargerComponent component, EntityUid target)
{
if (HasComp<ChargingComponent>(target))
RemComp<ChargingComponent>(target);
UpdateStatus(uid, component);
}

public override void Update(float frameTime)
{
var query = EntityQueryEnumerator<ChargingComponent>();
while (query.MoveNext(out var uid, out var charging))
var query = EntityQueryEnumerator<ActiveChargerComponent, ChargerComponent, ContainerManagerComponent>();
while (query.MoveNext(out var uid, out _, out var charger, out var containerComp))
{
if (!TryComp<ChargerComponent>(charging.ChargerUid, out var chargerComponent))
if (!_container.TryGetContainer(uid, charger.SlotId, out var container, containerComp))
continue;

if (charging.ChargerComponent.Status == CellChargerStatus.Off || charging.ChargerComponent.Status == CellChargerStatus.Empty)
if (charger.Status == CellChargerStatus.Empty || charger.Status == CellChargerStatus.Charged || container.ContainedEntities.Count == 0)
continue;

if (HasComp<EmpDisabledComponent>(charging.ChargerUid))
continue;

if (!TryComp<BatteryComponent>(uid, out var battery))
continue;

if (Math.Abs(battery.MaxCharge - battery.CurrentCharge) < 0.01)
StopChargingBattery(charging.ChargerUid, charging.ChargerComponent, uid);
TransferPower(charging.ChargerUid, uid, charging.ChargerComponent, frameTime);
foreach (var contained in container.ContainedEntities)
{
TransferPower(uid, contained, charger, frameTime);
}
}
}

Expand All @@ -116,15 +71,15 @@ private void OnInserted(EntityUid uid, ChargerComponent component, EntInsertedIn
if (args.Container.ID != component.SlotId)
return;

StartChargingBattery(uid, component, args.Entity);
UpdateStatus(uid, component);
}

private void OnRemoved(EntityUid uid, ChargerComponent component, EntRemovedFromContainerMessage args)
{
if (args.Container.ID != component.SlotId)
return;

StopChargingBattery(uid, component, args.Entity);
UpdateStatus(uid, component);
}

/// <summary>
Expand Down Expand Up @@ -157,11 +112,6 @@ private void OnEntityStorageInsertAttempt(EntityUid uid, ChargerComponent compon
args.Cancelled = true;
}

private void OnUpdateStatus(EntityUid uid, ChargerComponent component, ref ChargerUpdateStatusEvent args)
{
UpdateStatus(uid, component);
}

private void UpdateStatus(EntityUid uid, ChargerComponent component)
{
var status = GetStatus(uid, component);
Expand All @@ -176,6 +126,15 @@ private void UpdateStatus(EntityUid uid, ChargerComponent component)

component.Status = status;

if (component.Status == CellChargerStatus.Charging)
{
AddComp<ActiveChargerComponent>(uid);
}
else
{
RemComp<ActiveChargerComponent>(uid);
}

switch (component.Status)
{
case CellChargerStatus.Off:
Expand All @@ -187,7 +146,7 @@ private void UpdateStatus(EntityUid uid, ChargerComponent component)
_appearance.SetData(uid, CellVisual.Light, CellChargerStatus.Empty, appearance);
break;
case CellChargerStatus.Charging:
receiver.Load = component.ChargeRate; //does not scale with multiple slotted batteries
receiver.Load = component.ChargeRate;
_appearance.SetData(uid, CellVisual.Light, CellChargerStatus.Charging, appearance);
break;
case CellChargerStatus.Charged:
Expand All @@ -198,42 +157,6 @@ private void UpdateStatus(EntityUid uid, ChargerComponent component)
throw new ArgumentOutOfRangeException();
}
}

private void OnEmpPulse(EntityUid uid, ChargerComponent component, ref EmpPulseEvent args)
{
// we don't care if we haven't been disabled
if (!args.Disabled)
return;

// if the recharger is hit by an emp pulse,
// stop recharging contained batteries to save resources
if (!_container.TryGetContainer(uid, component.SlotId, out var container))
return;

foreach (var containedEntity in container.ContainedEntities)
{
if (!SearchForBattery(containedEntity, out _, out _))
continue;

StopChargingBattery(uid, component, containedEntity);
}
}

private void OnEmpDisabledRemoved(EntityUid uid, ChargerComponent component, ref EmpDisabledRemoved args)
{
// if an emp disable subsides,
// attempt to start charging all batteries
if (!_container.TryGetContainer(uid, component.SlotId, out var container))
return;

foreach (var containedEntity in container.ContainedEntities)
{
if (!SearchForBattery(containedEntity, out _, out _))
continue;

StartChargingBattery(uid, component, containedEntity);
}
}

private CellChargerStatus GetStatus(EntityUid uid, ChargerComponent component)
{
Expand All @@ -255,28 +178,13 @@ private CellChargerStatus GetStatus(EntityUid uid, ChargerComponent component)
if (container.ContainedEntities.Count == 0)
return CellChargerStatus.Empty;

var statusOut = CellChargerStatus.Off;

foreach (var containedEntity in container.ContainedEntities)
{
// if none of the slotted items are actually batteries, represent the charger as off
if (!SearchForBattery(containedEntity, out _, out _))
continue;

// if all batteries are either EMP'd or fully charged, represent the charger as fully charged
statusOut = CellChargerStatus.Charged;
if (HasComp<EmpDisabledComponent>(containedEntity))
continue;

if (!HasComp<ChargingComponent>(containedEntity))
continue;
if (!SearchForBattery(container.ContainedEntities[0], out _, out var heldBattery))
return CellChargerStatus.Off;

// if we have atleast one battery being charged, represent the charger as charging;
statusOut = CellChargerStatus.Charging;
break;
}
if (Math.Abs(heldBattery.MaxCharge - heldBattery.CurrentCharge) < 0.01)
return CellChargerStatus.Charged;

return statusOut;
return CellChargerStatus.Charging;
}

private void TransferPower(EntityUid uid, EntityUid targetEntity, ChargerComponent component, float frameTime)
Expand All @@ -293,11 +201,11 @@ private void TransferPower(EntityUid uid, EntityUid targetEntity, ChargerCompone
if (!SearchForBattery(targetEntity, out var batteryUid, out var heldBattery))
return;

_battery.TrySetCharge(batteryUid.Value, heldBattery.CurrentCharge + component.ChargeRate * frameTime, heldBattery);
_battery.SetCharge(batteryUid.Value, heldBattery.CurrentCharge + component.ChargeRate * frameTime, heldBattery);
// Just so the sprite won't be set to 99.99999% visibility
if (heldBattery.MaxCharge - heldBattery.CurrentCharge < 0.01)
{
_battery.TrySetCharge(batteryUid.Value, heldBattery.MaxCharge, heldBattery);
_battery.SetCharge(batteryUid.Value, heldBattery.MaxCharge, heldBattery);
}

UpdateStatus(uid, component);
Expand All @@ -315,6 +223,3 @@ private bool SearchForBattery(EntityUid uid, [NotNullWhen(true)] out EntityUid?
return true;
}
}

[ByRefEvent]
public record struct ChargerUpdateStatusEvent();
5 changes: 5 additions & 0 deletions Content.Shared/Chemistry/EntitySystems/RehydratableSystem.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using Content.Shared.Chemistry.Components;
using Content.Shared.FixedPoint;
using Content.Shared.Popups;
using Robust.Shared.Network;
using Robust.Shared.Random;

namespace Content.Shared.Chemistry.EntitySystems;

public sealed class RehydratableSystem : EntitySystem
{
[Dependency] private readonly INetManager _net = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly SharedSolutionContainerSystem _solutions = default!;
Expand All @@ -31,6 +33,9 @@ private void OnSolutionChange(Entity<RehydratableComponent> ent, ref SolutionCon
// Try not to make this public if you can help it.
private void Expand(Entity<RehydratableComponent> ent)
{
if (_net.IsClient)
return; // no

var (uid, comp) = ent;

var randomMob = _random.Pick(comp.PossibleSpawns);
Expand Down
18 changes: 18 additions & 0 deletions Resources/Changelog/Changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6354,3 +6354,21 @@ Entries:
id: 6360
time: '2024-09-14T06:48:55.0000000+00:00'
url: https://github.com/Simple-Station/Einstein-Engines/pull/896
- author: Mnemotechnician
changes:
- type: Fix
message: Cyborg recharging stations finally work again.
- type: Fix
message: >-
Rehydratable entities (such as monkey cubes) no longer spawn a second
client-side entity when rehydrated.
id: 6361
time: '2024-09-14T17:55:41.0000000+00:00'
url: https://github.com/Simple-Station/Einstein-Engines/pull/888
- author: VMSolidus
changes:
- type: Tweak
message: The Uncloneable trait is only worth one point now.
id: 6362
time: '2024-09-14T18:01:21.0000000+00:00'
url: https://github.com/Simple-Station/Einstein-Engines/pull/868
Loading

0 comments on commit 841a49c

Please sign in to comment.