Skip to content

Commit

Permalink
Merge pull request CleverRaven#75328 from PatrikLundell/inhaler
Browse files Browse the repository at this point in the history
Use inhaler/oxygen bottle with least content left
  • Loading branch information
Maleclypse authored Jul 31, 2024
2 parents 038b497 + 95f4da6 commit 836a1ce
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/npcmove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4369,26 +4369,35 @@ void npc::heal_self()
std::string iusage = "INHALER";

const auto filter_use = [this]( const std::string & filter ) -> std::vector<item *> {
auto inv_filtered = items_with( [&filter]( const item & itm )
std::vector<item *> inv_filtered = items_with( [&filter]( const item & itm )
{
return ( itm.type->get_use( filter ) != nullptr ) && itm.ammo_sufficient( nullptr );
} );
return inv_filtered;
};

const auto inv_inhalers = filter_use( iusage );
if( !inv_inhalers.empty() ) {
treatment = inv_inhalers.front();
} else {
const std::vector<item *> inv_inhalers = filter_use( iusage );

for( item *inhaler : inv_inhalers ) {
if( treatment == nullptr || treatment->ammo_remaining() > inhaler->ammo_remaining() ) {
treatment = inhaler;
}
}

if( treatment == nullptr ) {
iusage = "OXYGEN_BOTTLE";
const auto inv_oxybottles = filter_use( iusage );
if( !inv_oxybottles.empty() ) {
treatment = inv_oxybottles.front();
const std::vector<item *> inv_oxybottles = filter_use( iusage );

for( item *oxy_bottle : inv_oxybottles ) {
if( treatment == nullptr || treatment->ammo_remaining() > oxy_bottle->ammo_remaining() ) {
treatment = oxy_bottle;
}
}
}

if( treatment != nullptr ) {
treatment->get_use( iusage )->call( this, *treatment, pos() );
treatment->ammo_consume( treatment->ammo_required(), pos(), this );
treatment->get_use( iusage )->call( this, *treatment, pos_bub() );
treatment->ammo_consume( treatment->ammo_required(), pos_bub(), this );
return;
}
}
Expand Down

0 comments on commit 836a1ce

Please sign in to comment.