Skip to content

Commit

Permalink
Code cleanup + fix the rapid recharge verb to remove pause.
Browse files Browse the repository at this point in the history
  • Loading branch information
BramvanZijp committed Oct 24, 2024
1 parent 96aac6f commit 51f4459
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ private void AddTricksVerbs(GetVerbsEvent<Verb> args)
var recharger = EnsureComp<BatterySelfRechargerComponent>(args.Target);
recharger.AutoRecharge = true;
recharger.AutoRechargeRate = battery.MaxCharge; // Instant refill.
recharger.AutoRechargePause = false; // No delay.
},
Impact = LogImpact.Medium,
Message = Loc.GetString("admin-trick-infinite-battery-object-description"),
Expand Down Expand Up @@ -603,6 +604,7 @@ private void AddTricksVerbs(GetVerbsEvent<Verb> args)
recharger.AutoRecharge = true;
recharger.AutoRechargeRate = battery.MaxCharge; // Instant refill.
recharger.AutoRechargePause = false; // No delay.
}
},
Impact = LogImpact.Extreme,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

namespace Content.Server.Power.Components
{
/// <summary>
Expand Down Expand Up @@ -27,8 +29,8 @@ public sealed partial class BatterySelfRechargerComponent : Component
[ViewVariables(VVAccess.ReadWrite)] [DataField("autoRechargePauseTime")] public float AutoRechargePauseTime { get; set; } = 0f;

/// <summary>
/// The current cooldown before the entity goes back to automatically recharging.
/// Do not auto recharge if this timestamp has yet to happen, set for the auto recharge pause system.
/// </summary>
[DataField] public float AutoRechargeCooldown { get; set; } = 0f;
[DataField] public TimeSpan NextAutoRecharge { get; set; } = TimeSpan.FromSeconds(0f);
}
}
53 changes: 33 additions & 20 deletions Content.Server/Power/EntitySystems/BatterySystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
using Content.Server.Power.Components;
using Content.Shared.Examine;
using Content.Shared.Rejuvenate;
using Content.Shared.Timing;
using JetBrains.Annotations;
using Robust.Shared.Utility;
using Robust.Shared.Timing;

namespace Content.Server.Power.EntitySystems
{
[UsedImplicitly]
public sealed class BatterySystem : EntitySystem
{
[Dependency] protected readonly IGameTiming Timing = default!;

public override void Initialize()
{
base.Initialize();
Expand Down Expand Up @@ -84,17 +88,13 @@ public override void Update(float frameTime)
while (query.MoveNext(out var uid, out var comp, out var batt))
{
if (!comp.AutoRecharge) continue;
if (batt.IsFullyCharged) {
continue;
}
if (batt.IsFullyCharged) continue;

//
if (comp.AutoRechargePause)
{
if (comp.AutoRechargeCooldown > 0)
{
SetChargeCooldown(uid, comp.AutoRechargeCooldown - frameTime, comp);
if (comp.NextAutoRecharge > Timing.CurTime)
continue;
}
}

SetCharge(uid, batt.CurrentCharge + comp.AutoRechargeRate * frameTime, batt);
}
}
Expand All @@ -112,10 +112,7 @@ private void OnEmpPulse(EntityUid uid, BatteryComponent component, ref EmpPulseE
args.Affected = true;
UseCharge(uid, args.EnergyConsumption, component);
// Apply a cooldown to the entity's self recharge if needed to avoid it immediately self recharging after an EMP.
if (TryComp<BatterySelfRechargerComponent>(uid, out var batterySelfRechargerComponent))
if (batterySelfRechargerComponent.AutoRechargePause && batterySelfRechargerComponent.AutoRechargePauseTime > 0)
if (args.EnergyConsumption > 0 && batterySelfRechargerComponent.AutoRechargePauseTime > batterySelfRechargerComponent.AutoRechargeCooldown)
SetChargeCooldown(uid, batterySelfRechargerComponent.AutoRechargePauseTime, batterySelfRechargerComponent);
TrySetChargeCooldown(uid);
}

public float UseCharge(EntityUid uid, float value, BatteryComponent? battery = null)
Expand All @@ -128,10 +125,7 @@ public float UseCharge(EntityUid uid, float value, BatteryComponent? battery = n
battery.CurrentCharge = newValue;

// Apply a cooldown to the entity's self recharge if needed.
if (TryComp<BatterySelfRechargerComponent>(uid, out var batterySelfRechargerComponent))
if (batterySelfRechargerComponent.AutoRechargePause && batterySelfRechargerComponent.AutoRechargePauseTime > 0)
if (value > 0 && batterySelfRechargerComponent.AutoRechargePauseTime > batterySelfRechargerComponent.AutoRechargeCooldown)
SetChargeCooldown(uid, batterySelfRechargerComponent.AutoRechargePauseTime, batterySelfRechargerComponent);
TrySetChargeCooldown(uid);

var ev = new ChargeChangedEvent(battery.CurrentCharge, battery.MaxCharge);
RaiseLocalEvent(uid, ref ev);
Expand Down Expand Up @@ -167,6 +161,26 @@ public void SetCharge(EntityUid uid, float value, BatteryComponent? battery = nu
var ev = new ChargeChangedEvent(battery.CurrentCharge, battery.MaxCharge);
RaiseLocalEvent(uid, ref ev);
}
/// <summary>
/// Checks if the entity has a self recharge and puts it on cooldown if applicable.
/// </summary>
public void TrySetChargeCooldown(EntityUid uid, float value = -1)
{
if (!TryComp<BatterySelfRechargerComponent>(uid, out var batteryself))
return;

if (!batteryself.AutoRechargePause)
return;

// If no answer or a negative is given for value, use the default from AutoRechargePauseTime.
if (value < 0)
value = batteryself.AutoRechargePauseTime;

if (Timing.CurTime+TimeSpan.FromSeconds(value) <= batteryself.NextAutoRecharge)
return;

SetChargeCooldown(uid, batteryself.AutoRechargePauseTime, batteryself);
}

/// <summary>
/// Puts the entity's self recharge on cooldown for the specified time.
Expand All @@ -176,11 +190,10 @@ public void SetChargeCooldown(EntityUid uid, float value, BatterySelfRechargerCo
if (!Resolve(uid, ref batteryself))
return;

var old = batteryself.AutoRechargeCooldown;
if (value >= 0)
batteryself.AutoRechargeCooldown = value;
batteryself.NextAutoRecharge = Timing.CurTime+TimeSpan.FromSeconds(value);
else
batteryself.AutoRechargeCooldown = 0;
batteryself.NextAutoRecharge = Timing.CurTime;
}

/// <summary>
Expand Down

0 comments on commit 51f4459

Please sign in to comment.