]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Helper method for get charge level (#41601)
authorPok <113675512+Pok27@users.noreply.github.com>
Thu, 27 Nov 2025 17:52:02 +0000 (19:52 +0200)
committerGitHub <noreply@github.com>
Thu, 27 Nov 2025 17:52:02 +0000 (17:52 +0000)
* get-charge-percent

* review

Content.Server/Light/EntitySystems/HandheldLightSystem.cs
Content.Server/Radio/EntitySystems/JammerSystem.cs
Content.Server/Silicons/Borgs/BorgSystem.Transponder.cs
Content.Server/Silicons/Borgs/BorgSystem.Ui.cs
Content.Server/Speech/EntitySystems/DamagedSiliconAccentSystem.cs
Content.Shared/Power/EntitySystems/ChargerSystem.cs
Content.Shared/Power/EntitySystems/PredictedBatterySystem.API.cs
Content.Shared/PowerCell/PowerCellSystem.cs
Resources/Locale/en-US/power/components/charger.ftl

index 52b6614790db12202de6aed71d8d9a4c728833da..4102e2dca0c848a5a7c0f36b81e7b8ed056f569b 100644 (file)
@@ -238,12 +238,12 @@ namespace Content.Server.Light.EntitySystems
 
             var appearanceComponent = EntityManager.GetComponentOrNull<AppearanceComponent>(uid);
 
-            var fraction = _battery.GetCharge(battery.Value.AsNullable()) / battery.Value.Comp.MaxCharge;
-            if (fraction >= 0.30)
+            var chargeFraction = _battery.GetChargeLevel(battery.Value.AsNullable());
+            if (chargeFraction >= 0.30)
             {
                 _appearance.SetData(uid, HandheldLightVisuals.Power, HandheldLightPowerStates.FullPower, appearanceComponent);
             }
-            else if (fraction >= 0.10)
+            else if (chargeFraction >= 0.10)
             {
                 _appearance.SetData(uid, HandheldLightVisuals.Power, HandheldLightPowerStates.LowPower, appearanceComponent);
             }
index 750010c9ccf7bd5c48343525d8ed3bffaf9ccd99..eaa42d744c8ede2bfed3e2d4b467e2df78b6b629 100644 (file)
@@ -43,8 +43,8 @@ public sealed class JammerSystem : SharedJammerSystem
                 }
                 else
                 {
-                    var percentCharged = _battery.GetCharge(battery.Value.AsNullable()) / battery.Value.Comp.MaxCharge;
-                    var chargeLevel = percentCharged switch
+                    var chargeFraction = _battery.GetChargeLevel(battery.Value.AsNullable());
+                    var chargeLevel = chargeFraction switch
                     {
                         > 0.50f => RadioJammerChargeLevel.High,
                         < 0.15f => RadioJammerChargeLevel.Low,
index 1237919b2032b77f0cc5847f506543da59352a50..cc665c9e62691473e8e9815886354959d0443e80 100644 (file)
@@ -39,9 +39,9 @@ public sealed partial class BorgSystem
             if (now < comp.NextBroadcast)
                 continue;
 
-            var charge = 0f;
+            var chargeFraction = 0f;
             if (_powerCell.TryGetBatteryFromSlot(uid, out var battery))
-                charge = _battery.GetCharge(battery.Value.AsNullable()) / battery.Value.Comp.MaxCharge;
+                chargeFraction = _battery.GetChargeLevel(battery.Value.AsNullable());
 
             var hpPercent = CalcHP(uid);
 
@@ -52,7 +52,7 @@ public sealed partial class BorgSystem
                 comp.Sprite,
                 comp.Name,
                 meta.EntityName,
-                charge,
+                chargeFraction,
                 hpPercent,
                 chassis.ModuleCount,
                 hasBrain,
index d718916abfccd556bc888dd3587353d4dfd2e5c1..1306e5414c525d29dcdbb12faef588ea85158c11 100644 (file)
@@ -114,7 +114,7 @@ public sealed partial class BorgSystem
             return;
         }
 
-        var chargePercent = (short)MathF.Round(_battery.GetCharge(battery.Value.AsNullable()) / battery.Value.Comp.MaxCharge * 10f);
+        var chargePercent = (short)MathF.Round(_battery.GetChargeLevel(battery.Value.AsNullable()) * 10f);
 
         // we make sure 0 only shows if they have absolutely no battery.
         // also account for floating point imprecision
index 99cd0ca9e22f0832313bb2f171a3893b6d0a0d6d..e5db13ca844225aedc360945a76431168a6dd7be 100644 (file)
@@ -36,7 +36,7 @@ public sealed class DamagedSiliconAccentSystem : EntitySystem
             }
             else if (_powerCell.TryGetBatteryFromSlot(uid, out var battery))
             {
-                currentChargeLevel = _battery.GetCharge(battery.Value.AsNullable()) / battery.Value.Comp.MaxCharge;
+                currentChargeLevel = _battery.GetChargeLevel(battery.Value.AsNullable());
             }
             currentChargeLevel = Math.Clamp(currentChargeLevel, 0.0f, 1.0f);
             // Corrupt due to low power (drops characters on longer messages)
index d22e45bf298622cddc63392de5490e7459b6c568..b561eedcbbf0768744c1d78174e16feb044d13cc 100644 (file)
@@ -71,8 +71,8 @@ public sealed class ChargerSystem : EntitySystem
                     if (!_powerCell.TryGetBatteryFromEntityOrSlot(contained, out var battery))
                         continue;
 
-                    var chargePercentage = _battery.GetCharge(battery.Value.AsNullable()) / battery.Value.Comp.MaxCharge * 100;
-                    args.PushMarkup(Loc.GetString("charger-content", ("chargePercentage", (int)chargePercentage)));
+                    var chargePercent = _battery.GetChargeLevel(battery.Value.AsNullable()) * 100;
+                    args.PushMarkup(Loc.GetString("charger-content", ("chargePercent", (int)chargePercent)));
                 }
             }
         }
index c06b458dc20cc3770eb16f418e65a0c6e661b245..e5b509ca31da71e2ced088dd2cff9ad09d1cc6ca 100644 (file)
@@ -174,6 +174,21 @@ public sealed partial class PredictedBatterySystem
         return charge;
     }
 
+    /// <summary>
+    /// Gets the fraction of charge remaining (0–1).
+    /// </summary>
+    [PublicAPI]
+    public float GetChargeLevel(Entity<PredictedBatteryComponent?> ent)
+    {
+        if (!Resolve(ent, ref ent.Comp, false))
+            return 0f;
+
+        if (ent.Comp.MaxCharge <= 0f)
+            return 0f;
+
+        return GetCharge(ent) / ent.Comp.MaxCharge;
+    }
+
     /// <summary>
     /// Gets number of remaining uses for the given charge cost.
     /// </summary>
index dfc2dfdbe2b96211ea277133f9b5ae878e624925..41d6c094ca16ef633bbbe723485686a1a5940f1b 100644 (file)
@@ -129,8 +129,8 @@ public sealed partial class PowerCellSystem : EntitySystem
 
     private void OnBatteryExamined(Entity<PredictedBatteryComponent> ent, ref ExaminedEvent args)
     {
-        var charge = _battery.GetCharge(ent.AsNullable()) / ent.Comp.MaxCharge * 100;
-        args.PushMarkup(Loc.GetString("power-cell-component-examine-details", ("currentCharge", $"{charge:F0}")));
+        var chargePercent = _battery.GetChargeLevel(ent.AsNullable()) * 100;
+        args.PushMarkup(Loc.GetString("power-cell-component-examine-details", ("currentCharge", $"{chargePercent:F0}")));
     }
 
     private void OnDrawRefreshChargeRate(Entity<PowerCellDrawComponent> ent, ref RefreshChargeRateEvent args)
index 9ac16a2d46e9fe65307f65d60899d765c70f1fc4..01fa658c8c9f90649b3071b6f3cca9cedd72194d 100644 (file)
@@ -1,4 +1,4 @@
 charger-examine = Charges at [color={$color}]{$chargeRate}W[/color].
 charger-component-charge-rate = Charge rate
-charger-content = Current charge is at [color=#5E7C16]{$chargePercentage}[/color]%.
+charger-content = Current charge is at [color=#5E7C16]{$chargePercent}[/color]%.
 charger-empty = There is nothing in the charger.