]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Several Ninja Suit power cell upgrade fixes. (#32902)
authorBramvanZijp <56019239+BramvanZijp@users.noreply.github.com>
Tue, 22 Oct 2024 23:36:51 +0000 (01:36 +0200)
committerGitHub <noreply@github.com>
Tue, 22 Oct 2024 23:36:51 +0000 (10:36 +1100)
* Fix several jank issues with space ninja cell upgrades.

* Rework the code to comply with maintainer request.

* Fix some naming convention & formatting errors.

* Change from a custom check to an item whitelist to avoid power cages from fitting.

* Make the EntityUid of GetCellScore non nullable.

* Remove a line from a previous solution to the above problem I forgot to remove.

* Fix the magic number issue.

Content.Server/Ninja/Systems/NinjaSuitSystem.cs
Resources/Locale/en-US/ninja/ninja-actions.ftl
Resources/Prototypes/Entities/Clothing/OuterClothing/suits.yml

index 244b7adf0362bb97a3cb331ef444092506cd71f2..20674dda8799f7d601fdb779a4a63c98ff030ab9 100644 (file)
@@ -22,6 +22,9 @@ public sealed class NinjaSuitSystem : SharedNinjaSuitSystem
     [Dependency] private readonly PowerCellSystem _powerCell = default!;
     [Dependency] private readonly SharedTransformSystem _transform = default!;
 
+    // How much the cell score should be increased per 1 AutoRechargeRate.
+    private const int AutoRechargeValue = 100;
+
     public override void Initialize()
     {
         base.Initialize();
@@ -59,15 +62,26 @@ public sealed class NinjaSuitSystem : SharedNinjaSuitSystem
             return;
 
         // no power cell for some reason??? allow it
-        if (!_powerCell.TryGetBatteryFromSlot(uid, out var battery))
+        if (!_powerCell.TryGetBatteryFromSlot(uid, out var batteryUid, out var battery))
+            return;
+
+        if (!TryComp<BatteryComponent>(args.EntityUid, out var inserting))
+        {
+            args.Cancel();
             return;
+        }
+
+        var user = Transform(uid).ParentUid;
 
         // can only upgrade power cell, not swap to recharge instantly otherwise ninja could just swap batteries with flashlights in maints for easy power
-        if (!TryComp<BatteryComponent>(args.EntityUid, out var inserting) || inserting.MaxCharge <= battery.MaxCharge)
+        if (GetCellScore(inserting.Owner, inserting) <= GetCellScore(battery.Owner, battery))
+        {
             args.Cancel();
+            Popup.PopupEntity(Loc.GetString("ninja-cell-downgrade"), user, user);
+            return;
+        }
 
         // tell ninja abilities that use battery to update it so they don't use charge from the old one
-        var user = Transform(uid).ParentUid;
         if (!_ninja.IsNinja(user))
             return;
 
@@ -76,6 +90,16 @@ public sealed class NinjaSuitSystem : SharedNinjaSuitSystem
         RaiseLocalEvent(user, ref ev);
     }
 
+    // this function assigns a score to a power cell depending on the capacity, to be used when comparing which cell is better.
+    private float GetCellScore(EntityUid uid, BatteryComponent battcomp)
+    {
+        // if a cell is able to automatically recharge, boost the score drastically depending on the recharge rate,
+        // this is to ensure a ninja can still upgrade to a micro reactor cell even if they already have a medium or high.
+        if (TryComp<BatterySelfRechargerComponent>(uid, out var selfcomp) && selfcomp.AutoRecharge)
+            return battcomp.MaxCharge + (selfcomp.AutoRechargeRate*AutoRechargeValue);
+        return battcomp.MaxCharge;
+    }
+
     private void OnEmpAttempt(EntityUid uid, NinjaSuitComponent comp, EmpAttemptEvent args)
     {
         // ninja suit (battery) is immune to emp
index f01f02a60e57fa69dca41c01cd86017f207f7b82..b3e295b7a29a13c1f50a6dabdd3313c455e29f0e 100644 (file)
@@ -1,6 +1,8 @@
 ninja-no-power = Not enough charge in suit battery!
 ninja-revealed = You have been revealed!
 ninja-suit-cooldown = The suit needs time to recuperate from the last attack.
+ninja-cell-downgrade = The suit will only accept a new power cell that is better than the current one!
+ninja-cell-too-large = This power source does not fit in the ninja suit!
 
 ninja-research-steal-fail = No new research nodes were stolen...
 ninja-research-steal-success = Stole {$count} new nodes from {THE($server)}.
index c90e0c98f5b2b03ee72680faf72e7070af27a4f1..a58c2a3fdd4f85869b6ad820b809b8f862260c2f 100644 (file)
         name: power-cell-slot-component-slot-name-default
         startingItem: PowerCellSmall
         disableEject: true
+        whitelist:
+          tags:
+            - PowerCell
 
 - type: entity
   parent: ClothingOuterBase