From 8a66b106218434312adef1acf924263ed98cc407 Mon Sep 17 00:00:00 2001 From: KorGgenT Date: Fri, 18 Oct 2019 21:54:49 -0400 Subject: [PATCH] move has_charges to character.cpp --- src/character.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++ src/player.cpp | 59 -------------------------------------------- 2 files changed, 62 insertions(+), 59 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index 9c243146f0b14..a3fbe5fd8ec17 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -67,6 +67,9 @@ static const bionic_id bio_armor_head( "bio_armor_head" ); static const bionic_id bio_armor_legs( "bio_armor_legs" ); static const bionic_id bio_armor_torso( "bio_armor_torso" ); static const bionic_id bio_carbon( "bio_carbon" ); +static const bionic_id bio_laser( "bio_laser" ); +static const bionic_id bio_lighter( "bio_lighter" ); +static const bionic_id bio_tools( "bio_tools" ); const efftype_id effect_adrenaline( "adrenaline" ); const efftype_id effect_alarm_clock( "alarm_clock" ); @@ -5297,3 +5300,62 @@ std::string Character::is_snuggling() const return "nothing"; } + +bool Character::has_item_with_flag( const std::string &flag, bool need_charges ) const +{ + return has_item_with( [&flag, &need_charges]( const item & it ) { + if( it.is_tool() && need_charges ) { + return it.has_flag( flag ) && it.type->tool->max_charges ? it.charges > 0 : it.has_flag( flag ); + } + return it.has_flag( flag ); + } ); +} + +std::vector Character::all_items_with_flag( const std::string &flag ) const +{ + return items_with( [&flag]( const item & it ) { + return it.has_flag( flag ); + } ); +} + +bool Character::has_charges( const itype_id &it, int quantity, + const std::function &filter ) const +{ + if( it == "fire" || it == "apparatus" ) { + return has_fire( quantity ); + } + if( it == "UPS" && is_mounted() && + mounted_creature.get()->has_flag( MF_RIDEABLE_MECH ) ) { + auto mons = mounted_creature.get(); + return quantity <= mons->battery_item->ammo_remaining(); + } + return charges_of( it, quantity, filter ) == quantity; +} + +bool Character::has_fire( const int quantity ) const +{ + // TODO: Replace this with a "tool produces fire" flag. + + if( g->m.has_nearby_fire( pos() ) ) { + return true; + } else if( has_item_with_flag( "FIRE" ) ) { + return true; + } else if( has_item_with_flag( "FIRESTARTER" ) ) { + auto firestarters = all_items_with_flag( "FIRESTARTER" ); + for( auto &i : firestarters ) { + if( has_charges( i->typeId(), quantity ) ) { + return true; + } + } + } else if( has_active_bionic( bio_tools ) && get_power_level() > quantity * 5_kJ ) { + return true; + } else if( has_bionic( bio_lighter ) && get_power_level() > quantity * 5_kJ ) { + return true; + } else if( has_bionic( bio_laser ) && get_power_level() > quantity * 5_kJ ) { + return true; + } else if( is_npc() ) { + // A hack to make NPCs use their Molotovs + return true; + } + return false; +} diff --git a/src/player.cpp b/src/player.cpp index 2f7080586d53c..ca652b45f1d91 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -6446,34 +6446,6 @@ bool player::use_charges_if_avail( const itype_id &it, int quantity ) return false; } -bool Character::has_fire( const int quantity ) const -{ - // TODO: Replace this with a "tool produces fire" flag. - - if( g->m.has_nearby_fire( pos() ) ) { - return true; - } else if( has_item_with_flag( "FIRE" ) ) { - return true; - } else if( has_item_with_flag( "FIRESTARTER" ) ) { - auto firestarters = all_items_with_flag( "FIRESTARTER" ); - for( auto &i : firestarters ) { - if( has_charges( i->typeId(), quantity ) ) { - return true; - } - } - } else if( has_active_bionic( bio_tools ) && get_power_level() > quantity * 5_kJ ) { - return true; - } else if( has_bionic( bio_lighter ) && get_power_level() > quantity * 5_kJ ) { - return true; - } else if( has_bionic( bio_laser ) && get_power_level() > quantity * 5_kJ ) { - return true; - } else if( is_npc() ) { - // A hack to make NPCs use their Molotovs - return true; - } - return false; -} - void player::use_fire( const int quantity ) { //Okay, so checks for nearby fires first, @@ -6617,20 +6589,6 @@ int player::amount_worn( const itype_id &id ) const return amount; } -bool Character::has_charges( const itype_id &it, int quantity, - const std::function &filter ) const -{ - if( it == "fire" || it == "apparatus" ) { - return has_fire( quantity ); - } - if( it == "UPS" && is_mounted() && - mounted_creature.get()->has_flag( MF_RIDEABLE_MECH ) ) { - auto mons = mounted_creature.get(); - return quantity <= mons->battery_item->ammo_remaining(); - } - return charges_of( it, quantity, filter ) == quantity; -} - int player::leak_level( const std::string &flag ) const { int leak_level = 0; @@ -10595,13 +10553,6 @@ float player::speed_rating() const return ret; } -std::vector Character::all_items_with_flag( const std::string &flag ) const -{ - return items_with( [&flag]( const item & it ) { - return it.has_flag( flag ); - } ); -} - item &player::item_with_best_of_quality( const quality_id &qid ) { int maxq = max_quality( qid ); @@ -10644,16 +10595,6 @@ bool player::crush_frozen_liquid( item_location loc ) return false; } -bool Character::has_item_with_flag( const std::string &flag, bool need_charges ) const -{ - return has_item_with( [&flag, &need_charges]( const item & it ) { - if( it.is_tool() && need_charges ) { - return it.has_flag( flag ) && it.type->tool->max_charges ? it.charges > 0 : it.has_flag( flag ); - } - return it.has_flag( flag ); - } ); -} - void player::on_mutation_gain( const trait_id &mid ) { morale->on_mutation_gain( mid );