]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Fix rigged power cells exploding early (#41813)
authorslarticodefast <161409025+slarticodefast@users.noreply.github.com>
Wed, 10 Dec 2025 10:28:28 +0000 (11:28 +0100)
committerGitHub <noreply@github.com>
Wed, 10 Dec 2025 10:28:28 +0000 (10:28 +0000)
* fix riggable

* fix

Content.Server/Power/EntitySystems/BatterySystem.API.cs
Content.Server/Power/EntitySystems/RiggableSystem.cs
Content.Shared/Power/ChargeEvents.cs
Content.Shared/Power/EntitySystems/PredictedBatterySystem.API.cs

index 5683e7c13349a32746d7f1f112923d29b176b7b2..5995758d3daa23fa5c7ddea5db417c0f338e911d 100644 (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);
     }
 
index 3972cf3f864d84cf7ed5dfb17dc6ad767138b035..390e75eeb8c4f249deead6b89c85995cd6b6ee63 100644 (file)
@@ -86,13 +86,10 @@ public sealed class RiggableSystem : EntitySystem
         if (!ent.Comp.IsRigged)
             return;
 
-        if (TryComp<BatteryComponent>(ent, out var batteryComponent))
-        {
-            if (batteryComponent.CurrentCharge == 0f)
-                return;
+        if (args.Charge == 0f)
+            return; // No charge to cause an explosion.
 
-            Explode(ent, batteryComponent.CurrentCharge);
-        }
+        Explode(ent, args.Charge);
     }
 
     // predicted batteries
@@ -101,13 +98,13 @@ public sealed class RiggableSystem : EntitySystem
         if (!ent.Comp.IsRigged)
             return;
 
-        if (TryComp<PredictedBatteryComponent>(ent, out var predictedBatteryComponent))
-        {
-            var charge = _predictedBattery.GetCharge((ent.Owner, predictedBatteryComponent));
-            if (charge == 0f)
-                return;
+        if (args.CurrentCharge == 0f)
+            return; // No charge to cause an explosion.
 
-            Explode(ent, charge);
-        }
+        // Don't explode if we are not using any charge.
+        if (args.CurrentChargeRate == 0f && args.Delta == 0f)
+            return;
+
+        Explode(ent, args.CurrentCharge);
     }
 }
index f84b255f12fc455ec02cdc2e3a2e6a5988e1a914..d6a9c66071f89091e3cc129d0f108257a5d9b986 100644 (file)
@@ -8,7 +8,23 @@ namespace Content.Shared.Power;
 /// Only raised for entities with <see cref="BatteryComponent"/>.
 /// </summary>
 [ByRefEvent]
-public readonly record struct ChargeChangedEvent(float Charge, float MaxCharge);
+public readonly record struct ChargeChangedEvent(float Charge, float Delta, float MaxCharge)
+{
+    /// <summary>
+    /// The new charge of the battery.
+    /// </summary>
+    public readonly float Charge = Charge;
+
+    /// <summary>
+    /// The amount the charge was changed by.
+    /// </summary>
+    public readonly float Delta = Delta;
+
+    /// <summary>
+    /// The maximum charge of the battery.
+    /// </summary>
+    public readonly float MaxCharge = MaxCharge;
+}
 
 /// <summary>
 /// Raised when a predicted battery's charge or capacity changes (capacity affects relative charge percentage).
@@ -18,7 +34,29 @@ public readonly record struct ChargeChangedEvent(float Charge, float MaxCharge);
 /// Only raised for entities with <see cref="PredictedBatteryComponent"/>.
 /// </summary>
 [ByRefEvent]
-public readonly record struct PredictedBatteryChargeChangedEvent(float CurrentCharge, float CurrentChargeRate, TimeSpan CurrentTime, float MaxCharge);
+public readonly record struct PredictedBatteryChargeChangedEvent(float CurrentCharge, float Delta, float CurrentChargeRate, float MaxCharge)
+{
+    /// <summary>
+    /// The new charge of the battery.
+    /// </summary>
+    public readonly float CurrentCharge = CurrentCharge;
+
+    /// <summary>
+    /// The amount the charge was changed by.
+    /// This might be 0 if only the charge rate was modified.
+    /// </summary>
+    public readonly float Delta = Delta;
+
+    /// <summary>
+    /// The new charge rate of the battery.
+    /// </summary>
+    public readonly float CurrentChargeRate = CurrentChargeRate;
+
+    /// <summary>
+    /// The maximum charge of the battery.
+    /// </summary>
+    public readonly float MaxCharge = MaxCharge;
+}
 
 /// <summary>
 /// Raised when a battery changes its state between full, empty, or neither.
index e5b509ca31da71e2ced088dd2cff9ad09d1cc6ca..e1d685daeb8f402e39d691385bd47dc7414433d7 100644 (file)
@@ -36,7 +36,7 @@ public sealed partial class PredictedBatterySystem
 
         TrySetChargeCooldown(ent.Owner);
 
-        var changedEv = new PredictedBatteryChargeChangedEvent(newValue, ent.Comp.ChargeRate, curTime, ent.Comp.MaxCharge);
+        var changedEv = new PredictedBatteryChargeChangedEvent(newValue, delta, ent.Comp.ChargeRate, ent.Comp.MaxCharge);
         RaiseLocalEvent(ent, ref changedEv);
 
         // Raise events if the battery status changed between full, empty, or neither.
@@ -95,7 +95,7 @@ public sealed partial class PredictedBatterySystem
         ent.Comp.LastUpdate = curTime;
         Dirty(ent);
 
-        var ev = new PredictedBatteryChargeChangedEvent(newValue, ent.Comp.ChargeRate, curTime, ent.Comp.MaxCharge);
+        var ev = new PredictedBatteryChargeChangedEvent(newValue, delta, ent.Comp.ChargeRate, ent.Comp.MaxCharge);
         RaiseLocalEvent(ent, ref ev);
 
         // Raise events if the battery status changed between full, empty, or neither.
@@ -115,13 +115,14 @@ public sealed partial class PredictedBatterySystem
         if (value == ent.Comp.MaxCharge)
             return;
 
+        var oldCharge = GetCharge(ent);
         ent.Comp.MaxCharge = Math.Max(value, 0);
         ent.Comp.LastCharge = GetCharge(ent); // This clamps it using the new max.
         var curTime = _timing.CurTime;
         ent.Comp.LastUpdate = curTime;
         Dirty(ent);
 
-        var ev = new PredictedBatteryChargeChangedEvent(ent.Comp.LastCharge, ent.Comp.ChargeRate, curTime, ent.Comp.MaxCharge);
+        var ev = new PredictedBatteryChargeChangedEvent(ent.Comp.LastCharge, ent.Comp.LastCharge - oldCharge, ent.Comp.ChargeRate, ent.Comp.MaxCharge);
         RaiseLocalEvent(ent, ref ev);
 
         // Raise events if the battery status changed between full, empty, or neither.
@@ -240,7 +241,7 @@ public sealed partial class PredictedBatterySystem
         Dirty(ent);
 
         // Inform other systems about the new rate;
-        var changedEv = new PredictedBatteryChargeChangedEvent(ent.Comp.LastCharge, ent.Comp.ChargeRate, curTime, ent.Comp.MaxCharge);
+        var changedEv = new PredictedBatteryChargeChangedEvent(ent.Comp.LastCharge, 0f, ent.Comp.ChargeRate, ent.Comp.MaxCharge);
         RaiseLocalEvent(ent, ref changedEv);
 
         return refreshEv.NewChargeRate;