Several Ninja Suit power cell upgrade fixes. (#32902)

* 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.
This commit is contained in:
BramvanZijp
2024-10-23 01:36:51 +02:00
committed by GitHub
parent 8f52a3448e
commit ee8dedea9c
3 changed files with 33 additions and 4 deletions

View File

@@ -22,6 +22,9 @@ public sealed class NinjaSuitSystem : SharedNinjaSuitSystem
[Dependency] private readonly PowerCellSystem _powerCell = default!; [Dependency] private readonly PowerCellSystem _powerCell = default!;
[Dependency] private readonly SharedTransformSystem _transform = 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() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
@@ -59,15 +62,26 @@ public sealed class NinjaSuitSystem : SharedNinjaSuitSystem
return; return;
// no power cell for some reason??? allow it // 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; return;
// 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))
if (!TryComp<BatteryComponent>(args.EntityUid, out var inserting) || inserting.MaxCharge <= battery.MaxCharge) {
args.Cancel(); 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 (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 // 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)) if (!_ninja.IsNinja(user))
return; return;
@@ -76,6 +90,16 @@ public sealed class NinjaSuitSystem : SharedNinjaSuitSystem
RaiseLocalEvent(user, ref ev); 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) private void OnEmpAttempt(EntityUid uid, NinjaSuitComponent comp, EmpAttemptEvent args)
{ {
// ninja suit (battery) is immune to emp // ninja suit (battery) is immune to emp

View File

@@ -1,6 +1,8 @@
ninja-no-power = Not enough charge in suit battery! ninja-no-power = Not enough charge in suit battery!
ninja-revealed = You have been revealed! ninja-revealed = You have been revealed!
ninja-suit-cooldown = The suit needs time to recuperate from the last attack. 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-fail = No new research nodes were stolen...
ninja-research-steal-success = Stole {$count} new nodes from {THE($server)}. ninja-research-steal-success = Stole {$count} new nodes from {THE($server)}.

View File

@@ -187,6 +187,9 @@
name: power-cell-slot-component-slot-name-default name: power-cell-slot-component-slot-name-default
startingItem: PowerCellSmall startingItem: PowerCellSmall
disableEject: true disableEject: true
whitelist:
tags:
- PowerCell
- type: entity - type: entity
parent: ClothingOuterBase parent: ClothingOuterBase