]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Fix autorecharge
authormetalgearsloth <comedian_vs_clown@hotmail.com>
Sat, 26 Apr 2025 14:44:02 +0000 (00:44 +1000)
committerScarKy0 <scarky0@onet.eu>
Mon, 28 Apr 2025 13:53:40 +0000 (15:53 +0200)
Content.Shared/Charges/Systems/SharedChargesSystem.cs

index 4805e5a4410947b34f9e0548891f30b6067ec2d8..e915580baeb0f400a6be10cf4d94303ac5a033f8 100644 (file)
@@ -41,10 +41,9 @@ public abstract class SharedChargesSystem : EntitySystem
         }
 
         // only show the recharging info if it's not full
-        if (charges == comp.MaxCharges || !TryComp<AutoRechargeComponent>(uid, out var recharge))
+        if (charges == comp.MaxCharges || !Resolve(uid, ref rechargeEnt.Comp2, false))
             return;
 
-        rechargeEnt.Comp2 = recharge;
         var timeRemaining = GetNextRechargeTime(rechargeEnt);
         args.PushMarkup(Loc.GetString("limited-charges-recharging", ("seconds", timeRemaining.TotalSeconds.ToString("F1"))));
     }
@@ -95,17 +94,12 @@ public abstract class SharedChargesSystem : EntitySystem
     /// <summary>
     /// Adds the specified charges. Does not reset the accumulator.
     /// </summary>
-    public void AddCharges(Entity<LimitedChargesComponent?> action, int addCharges)
+    public void AddCharges(Entity<LimitedChargesComponent?, AutoRechargeComponent?> action, int addCharges)
     {
         if (addCharges == 0)
             return;
 
-        action.Comp ??= EnsureComp<LimitedChargesComponent>(action.Owner);
-
-        // 1. If we're going FROM max then set lastupdate to now (so it doesn't instantly recharge).
-        // 2. If we're going TO max then also set lastupdate to now.
-        // 3. Otherwise don't modify it.
-        // No idea if we go to 0 but future problem.
+        action.Comp1 ??= EnsureComp<LimitedChargesComponent>(action.Owner);
 
         var lastCharges = GetCurrentCharges(action);
         var charges = lastCharges + addCharges;
@@ -113,13 +107,25 @@ public abstract class SharedChargesSystem : EntitySystem
         if (lastCharges == charges)
             return;
 
-        if (charges == action.Comp.MaxCharges || lastCharges == action.Comp.MaxCharges)
+        // If we were at max then need to reset the timer.
+        if (charges == action.Comp1.MaxCharges || lastCharges == action.Comp1.MaxCharges)
+        {
+            action.Comp1.LastUpdate = _timing.CurTime;
+            action.Comp1.LastCharges = action.Comp1.MaxCharges;
+        }
+        // If it has auto-recharge then make up the difference.
+        else if (Resolve(action.Owner, ref action.Comp2, false))
         {
-            action.Comp.LastUpdate = _timing.CurTime;
+            var duration = action.Comp2.RechargeDuration;
+            var diff = (_timing.CurTime - action.Comp1.LastUpdate);
+            var remainder = (int) (diff / duration);
+
+            action.Comp1.LastCharges += remainder;
+            action.Comp1.LastUpdate += (remainder * duration);
         }
 
-        action.Comp.LastCharges = Math.Clamp(charges, 0, action.Comp.MaxCharges);
-        Dirty(action);
+        action.Comp1.LastCharges = Math.Clamp(action.Comp1.LastCharges + addCharges, 0, action.Comp1.MaxCharges);
+        Dirty(action.Owner, action.Comp1);
     }
 
     public bool TryUseCharge(Entity<LimitedChargesComponent?> entity)