Skip to content

Commit

Permalink
RM13 premature shutdown fix, the Sequel: I need to learn how to Githu…
Browse files Browse the repository at this point in the history
…b better (#77023)

* Initial attempt at fixing 76926

* Added ignoreExternalSources to energy_remaining, else block to else if

* astyling

* Slightly more descriptive shutdown message

* Dealing with merge conflicts yaaaaaaay
  • Loading branch information
DPavonis authored Oct 16, 2024
1 parent 2610e3e commit 8c9ca7a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
20 changes: 14 additions & 6 deletions src/character_armor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,14 @@ bool Character::armor_absorb( damage_unit &du, item &armor, const bodypart_id &b
}
// if this armor has the flag, try to deduct that much energy from it. If that takes it to 0 energy, turn it off before it absorbs damage.
if( armor.has_flag( flag_USE_POWER_WHEN_HIT ) &&
units::from_kilojoule( du.amount ) > armor.energy_consume( units::from_kilojoule( du.amount ),
pos(), nullptr ) ) {
units::from_kilojoule( du.amount ) > armor.energy_remaining( nullptr, true ) ) {
armor.deactivate( nullptr, false );
add_msg_if_player( _( "Your %s doesn't have enough power and shuts down!" ), armor.tname() );
add_msg_if_player( _( "Your %s doesn't have enough power to absorb the blow and shuts down!" ),
armor.tname() );
} else if( armor.has_flag( flag_USE_POWER_WHEN_HIT ) &&
units::from_kilojoule( du.amount ) < armor.energy_remaining( nullptr, true ) ) {
armor.energy_consume( units::from_kilojoule( du.amount ),
pos(), nullptr );
}
// reduce the damage
// -1 is passed as roll so that each material is rolled individually
Expand All @@ -305,10 +309,14 @@ bool Character::armor_absorb( damage_unit &du, item &armor, const bodypart_id &b
}
// if this armor has the flag, try to deduct that much energy from it. If that takes it to 0 energy, turn it off before it absorbs damage.
if( armor.has_flag( flag_USE_POWER_WHEN_HIT ) &&
units::from_kilojoule( du.amount ) > armor.energy_consume( units::from_kilojoule( du.amount ),
pos(), nullptr ) ) {
units::from_kilojoule( du.amount ) > armor.energy_remaining( nullptr, true ) ) {
armor.deactivate( nullptr, false );
add_msg_if_player( _( "Your %s doesn't have enough power and shuts down!" ), armor.tname() );
add_msg_if_player( _( "Your %s doesn't have enough power to absorb the blow and shuts down!" ),
armor.tname() );
} else if( armor.has_flag( flag_USE_POWER_WHEN_HIT ) &&
units::from_kilojoule( du.amount ) < armor.energy_remaining( nullptr, true ) ) {
armor.energy_consume( units::from_kilojoule( du.amount ),
pos(), nullptr );
}
// reduce the damage
// -1 is passed as roll so that each material is rolled individually
Expand Down
37 changes: 21 additions & 16 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10874,31 +10874,36 @@ bool item::uses_energy() const

units::energy item::energy_remaining( const Character *carrier ) const
{
if( !uses_energy() ) {
return 0_kJ;
}
units::energy ret = 0_kJ;
return energy_remaining( carrier, false );
}

// Future energy based batteries
if( is_vehicle_battery() ) {
ret += energy;
}
units::energy item::energy_remaining( const Character *carrier, bool ignoreExternalSources ) const
{
units::energy ret = 0_kJ;

// Magazine in the item
const item *mag = magazine_current();
if( mag ) {
ret += mag->energy_remaining( carrier );
}

// Power from bionic
if( carrier != nullptr && has_flag( flag_USES_BIONIC_POWER ) ) {
ret += carrier->get_power_level();
}
if( !ignoreExternalSources ) {

// Extra power from UPS
if( carrier != nullptr && has_flag( flag_USE_UPS ) ) {
ret += carrier->available_ups();
}
// Future energy based batteries
if( is_vehicle_battery() ) {
ret += energy;
}

// Power from bionic
if( carrier != nullptr && has_flag( flag_USES_BIONIC_POWER ) ) {
ret += carrier->get_power_level();
}

// Extra power from UPS
if( carrier != nullptr && has_flag( flag_USE_UPS ) ) {
ret += carrier->available_ups();
}
};

// Battery(ammo) contained within
if( is_magazine() ) {
Expand Down
3 changes: 3 additions & 0 deletions src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -2447,8 +2447,11 @@ class item : public visitable
/**
* Energy available from battery/UPS/bionics
* @param carrier is used for UPS and bionic power.
* Set second parameter to true to ignore vehicle batteries, UPS and bionic power when checking
*/

units::energy energy_remaining( const Character *carrier = nullptr ) const;
units::energy energy_remaining( const Character *carrier, bool ignoreExternalSources ) const;

/**
* Quantity of ammunition currently loaded in tool, gun or auxiliary gunmod.
Expand Down

0 comments on commit 8c9ca7a

Please sign in to comment.