From d89a3c961ed13ff5aabe2cb2d301a05d637b8b5e Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Mon, 19 Apr 2021 21:14:18 +0300 Subject: [PATCH 01/87] convert electrick jackhammer --- data/json/items/tool/workshop.json | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/data/json/items/tool/workshop.json b/data/json/items/tool/workshop.json index a0d8acac22e75..2b3856eb2e3d1 100644 --- a/data/json/items/tool/workshop.json +++ b/data/json/items/tool/workshop.json @@ -325,7 +325,7 @@ "type": "TOOL", "name": { "str": "electric jackhammer" }, "description": "This is a construction tool for drilling through hard rock or other surfaces. It runs on a cell compatible with UPS. Use it to blast a hole in adjacent solid terrain.", - "weight": "40000 g", + "weight": "5000 g", "volume": "5 L", "longest_side": "71 cm", "price": 40000, @@ -337,9 +337,18 @@ "symbol": ";", "color": "light_gray", "ammo": [ "battery" ], - "initial_charges": 3500, - "max_charges": 7000, "charges_per_use": 3500, + "pocket_data": [ + { + "pocket_type": "MAGAZINE_WELL", + "rigid": true, + "max_contains_volume": "20 L", + "max_contains_weight": "40 kg", + "item_restriction": [ + "medium_storage_battery" + ] + } + ], "use_action": [ "JACKHAMMER" ], "flags": [ "STAB", "DIG_TOOL", "POWERED", "USE_UPS", "NO_UNLOAD", "NO_RELOAD", "WATER_BREAK" ] }, From b94fa40bd14682fbc96e09cd626f727c8ac3a047 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Mon, 19 Apr 2021 23:51:43 +0300 Subject: [PATCH 02/87] item pwr drain refactor --- src/activity_handlers.cpp | 1 + src/character.cpp | 44 +++++++++++++++++++++++++++++++++++++++ src/character.h | 11 ++++++++++ src/item.cpp | 33 +++++++++++++++++++++++++++++ src/item.h | 20 ++++++++++++++++++ 5 files changed, 109 insertions(+) diff --git a/src/activity_handlers.cpp b/src/activity_handlers.cpp index e2768289cbc94..4e9e720b9cc31 100644 --- a/src/activity_handlers.cpp +++ b/src/activity_handlers.cpp @@ -3818,6 +3818,7 @@ void activity_handlers::jackhammer_finish( player_activity *act, player *p ) if( !act->targets.empty() ) { item &it = *act->targets.front(); p->consume_charges( it, it.ammo_required() ); + //it.electr_consume( it.ammo_required(), p ); } else { debugmsg( "jackhammer activity targets empty" ); } diff --git a/src/character.cpp b/src/character.cpp index 3a0ced3e8548d..75b368f629783 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -11314,6 +11314,50 @@ bool Character::use_charges_if_avail( const itype_id &it, int quantity ) return false; } +int Character::available_ups() +{ + int available_charges = 0; + if( is_mounted() && mounted_creature.get()->has_flag( MF_RIDEABLE_MECH ) ) { + auto *mons = mounted_creature.get(); + available_charges += mons->battery_item->ammo_remaining(); + } + if( has_power() && has_active_bionic( bio_ups ) ) { + available_charges += units::to_kilojoule( get_power_level() ); + } + available_charges += charges_of( itype_UPS ); + + return available_charges; +} + +void Character::consume_ups( int qty ) +{ + if( qty != 0 && is_mounted() && mounted_creature.get()->has_flag( MF_RIDEABLE_MECH ) && + mounted_creature.get()->battery_item ) { + auto *mons = mounted_creature.get(); + int power_drain = std::min( mons->battery_item->ammo_remaining(), qty ); + mons->use_mech_power( -power_drain ); + qty -= std::min( qty, power_drain ); + } + + if( qty != 0 && has_power() && has_active_bionic( bio_ups ) ) { + int bio = std::min( units::to_kilojoule( get_power_level() ), qty ); + mod_power_level( units::from_kilojoule( -bio ) ); + qty -= std::min( qty, bio ); + } + + inventory inv = crafting_inventory( tripoint_zero, -1 ); + + int adv = inv.charges_of( itype_adv_UPS_off, static_cast( std::ceil( qty * 0.6 ) ) ); + if( qty != 0 && adv > 0 ) { + qty -= std::min( qty, static_cast( adv / 0.6 ) ); + } + + int ups = inv.charges_of( itype_UPS_off, qty ); + if( qty != 0 && ups > 0 ) { + qty -= std::min( qty, ups ); + } +} + std::list Character::use_charges( const itype_id &what, int qty, const int radius, const std::function &filter ) { diff --git a/src/character.h b/src/character.h index 944f6a0ad8dbd..0217cbe1a738d 100644 --- a/src/character.h +++ b/src/character.h @@ -2054,6 +2054,17 @@ class Character : public Creature, public visitable // Uses up charges bool use_charges_if_avail( const itype_id &it, int quantity ); + // Returns the amount of UPS charges available + // Sum of mech, bionic UPS, adv UPS and UPS + // Adv UPS includes efficiency bonus + int available_ups(); + /** + * Consume UPS charges. + * Consume order: mech, Bionic UPS, adv UPS, UPS. + * @param qty Number of charges (kJ) + */ + void consume_ups( int qty ); + /** * Use charges in character inventory. * @param what itype_id of item using charges diff --git a/src/item.cpp b/src/item.cpp index 4c9c72aae1fbe..9c495f40c26c9 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8073,6 +8073,39 @@ int item::ammo_consume( int qty, const tripoint &pos ) return 0; } +int item::electr_available( player *carrier ) +{ + if( has_flag( flag_USES_BIONIC_POWER ) ) { + int power = units::to_kilojoule( get_player_character().get_power_level() ); + return power; + } + + int res = 0; + for( const item *e : contents.all_items_top( item_pocket::pocket_type::MAGAZINE ) ) { + res += e->charges; + } + if( carrier != nullptr && has_flag( flag_USE_UPS ) ) { + res += carrier->available_ups(); + } + return res; +} + +bool item::electr_consume( int qty, player *carrier ) +{ + if( electr_available( carrier ) < qty ) { + return false; + } + + // The tripoint in this call is used only for ejecting casings + // It should be safe to pass whatever to it. + qty -= ammo_consume( qty, tripoint_zero ); + + if( carrier != nullptr && has_flag( flag_USE_UPS ) ) { + carrier->consume_ups( qty ); + } + return true; +} + const itype *item::ammo_data() const { const item *mag = magazine_current(); diff --git a/src/item.h b/src/item.h index b81767ec36d11..a355bb1ff1ae9 100644 --- a/src/item.h +++ b/src/item.h @@ -1884,6 +1884,26 @@ class item : public visitable */ int ammo_consume( int qty, const tripoint &pos ); + /** + * How much electric energy is available. + * If the item has flag_USES_BIONIC_POWER then return only bionic power + * If the tool has battery loaded then that is included + * The check is dumb so don't call it on items that don't load batteries + * If the tool has flag_USE_UPS then UPS sources are included + * @param carrier The current carrier + * @return amount of power (kJ) + */ + int electr_available( player *carrier = nullptr ); + + /** + * Consume electric power (if available). If there is not enough power then none is drained and false is returned + * Consume order: Battery, UPS + * @param qty maximum amount of electric power (kJ) that should be consumed + * @param carrier The current carrier + * @return false if there was not enough powr + */ + bool electr_consume( int qty, player *carrier = nullptr ); + /** Specific ammo data, returns nullptr if item is neither ammo nor loaded with any */ const itype *ammo_data() const; /** Specific ammo type, returns "null" if item is neither ammo nor loaded with any */ From d78c3c43a343a71924590b774c90dfe162a186a8 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 00:38:52 +0300 Subject: [PATCH 03/87] jackhammering works --- src/activity_handlers.cpp | 3 +-- src/item.cpp | 12 ++++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/activity_handlers.cpp b/src/activity_handlers.cpp index 4e9e720b9cc31..e0cbe235859d5 100644 --- a/src/activity_handlers.cpp +++ b/src/activity_handlers.cpp @@ -3817,8 +3817,7 @@ void activity_handlers::jackhammer_finish( player_activity *act, player *p ) act->set_to_null(); if( !act->targets.empty() ) { item &it = *act->targets.front(); - p->consume_charges( it, it.ammo_required() ); - //it.electr_consume( it.ammo_required(), p ); + it.electr_consume( it.ammo_required(), p ); } else { debugmsg( "jackhammer activity targets empty" ); } diff --git a/src/item.cpp b/src/item.cpp index 9c495f40c26c9..79bc75928a3be 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8081,9 +8081,17 @@ int item::electr_available( player *carrier ) } int res = 0; - for( const item *e : contents.all_items_top( item_pocket::pocket_type::MAGAZINE ) ) { - res += e->charges; + const item *mag = magazine_current(); + if( mag ) { + res += mag->ammo_remaining(); } + + if( !ammo_types().empty() ) { + for( const item *e : contents.all_items_top( item_pocket::pocket_type::MAGAZINE ) ) { + res += e->charges; + } + } + if( carrier != nullptr && has_flag( flag_USE_UPS ) ) { res += carrier->available_ups(); } From ae7263da7ca0792ee572def93f44d37aa591ef1c Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 00:58:48 +0300 Subject: [PATCH 04/87] ammo req --- src/item.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/item.cpp b/src/item.cpp index 79bc75928a3be..a005f19bfcacd 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8098,6 +8098,15 @@ int item::electr_available( player *carrier ) return res; } +bool item::has_enough_electr( player *carrier ) +{ + if( !it.is_tool() || !it.ammo_required() ) { + return true; + } + + return electr_available( carrier ) >= ammo_required(); +} + bool item::electr_consume( int qty, player *carrier ) { if( electr_available( carrier ) < qty ) { From 5491d15c108deff4a6e6a13010e91b36fcb00934 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 01:00:48 +0300 Subject: [PATCH 05/87] has_enough_electr --- src/item.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/item.h b/src/item.h index a355bb1ff1ae9..82101a237b357 100644 --- a/src/item.h +++ b/src/item.h @@ -1894,6 +1894,14 @@ class item : public visitable * @return amount of power (kJ) */ int electr_available( player *carrier = nullptr ); + + /** + * Check if the item has enough power available to be activated + * Checks all the appropriate power sources: battery, bionic, UPS + * @param carrier The current carrier + * @return true if there is enough power + */ + bool has_enough_electr( player *carrier = nullptr ) /** * Consume electric power (if available). If there is not enough power then none is drained and false is returned From c4099445cc9ce1aee13ce6bbfdb9f34be578d9ef Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 01:19:58 +0300 Subject: [PATCH 06/87] maybe works now --- src/character.h | 1 + src/item.cpp | 4 ++-- src/item.h | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/character.h b/src/character.h index 0217cbe1a738d..27f2117336981 100644 --- a/src/character.h +++ b/src/character.h @@ -2061,6 +2061,7 @@ class Character : public Creature, public visitable /** * Consume UPS charges. * Consume order: mech, Bionic UPS, adv UPS, UPS. + * Does not check if there is enough power so check available_ups() first * @param qty Number of charges (kJ) */ void consume_ups( int qty ); diff --git a/src/item.cpp b/src/item.cpp index a005f19bfcacd..75fdb2b2dde49 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8098,9 +8098,9 @@ int item::electr_available( player *carrier ) return res; } -bool item::has_enough_electr( player *carrier ) +bool item::has_enough_electr( const player &carrier ) { - if( !it.is_tool() || !it.ammo_required() ) { + if( !is_tool() || !ammo_required() ) { return true; } diff --git a/src/item.h b/src/item.h index 82101a237b357..0c5dd2cab4319 100644 --- a/src/item.h +++ b/src/item.h @@ -1893,7 +1893,7 @@ class item : public visitable * @param carrier The current carrier * @return amount of power (kJ) */ - int electr_available( player *carrier = nullptr ); + int electr_available( player *carrier ); /** * Check if the item has enough power available to be activated @@ -1901,7 +1901,7 @@ class item : public visitable * @param carrier The current carrier * @return true if there is enough power */ - bool has_enough_electr( player *carrier = nullptr ) + bool has_enough_electr( const player &carrier ); /** * Consume electric power (if available). If there is not enough power then none is drained and false is returned @@ -1910,7 +1910,7 @@ class item : public visitable * @param carrier The current carrier * @return false if there was not enough powr */ - bool electr_consume( int qty, player *carrier = nullptr ); + bool electr_consume( int qty, player *carrier ); /** Specific ammo data, returns nullptr if item is neither ammo nor loaded with any */ const itype *ammo_data() const; From 0992ef80253e816fbb041fc6946e2681344e4912 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 11:27:57 +0300 Subject: [PATCH 07/87] it compiles --- src/character.cpp | 2 +- src/character.h | 4 ++-- src/item.cpp | 10 +++++----- src/item.h | 10 +++++----- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index 75b368f629783..c8fa21c5c6e07 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -11314,7 +11314,7 @@ bool Character::use_charges_if_avail( const itype_id &it, int quantity ) return false; } -int Character::available_ups() +int Character::available_ups() const { int available_charges = 0; if( is_mounted() && mounted_creature.get()->has_flag( MF_RIDEABLE_MECH ) ) { diff --git a/src/character.h b/src/character.h index 27f2117336981..75ba502a8639e 100644 --- a/src/character.h +++ b/src/character.h @@ -2057,11 +2057,11 @@ class Character : public Creature, public visitable // Returns the amount of UPS charges available // Sum of mech, bionic UPS, adv UPS and UPS // Adv UPS includes efficiency bonus - int available_ups(); + int available_ups() const; /** * Consume UPS charges. * Consume order: mech, Bionic UPS, adv UPS, UPS. - * Does not check if there is enough power so check available_ups() first + * Does not check if there is enough power so check available_ups() first * @param qty Number of charges (kJ) */ void consume_ups( int qty ); diff --git a/src/item.cpp b/src/item.cpp index 75fdb2b2dde49..974ac0073c9c5 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8073,7 +8073,7 @@ int item::ammo_consume( int qty, const tripoint &pos ) return 0; } -int item::electr_available( player *carrier ) +int item::electr_available( const player *carrier ) const { if( has_flag( flag_USES_BIONIC_POWER ) ) { int power = units::to_kilojoule( get_player_character().get_power_level() ); @@ -8098,13 +8098,13 @@ int item::electr_available( player *carrier ) return res; } -bool item::has_enough_electr( const player &carrier ) +bool item::has_enough_electr( const player *carrier ) const { - if( !is_tool() || !ammo_required() ) { + if( !is_tool() || !ammo_required() ) { return true; } - - return electr_available( carrier ) >= ammo_required(); + + return electr_available( carrier ) >= ammo_required(); } bool item::electr_consume( int qty, player *carrier ) diff --git a/src/item.h b/src/item.h index 0c5dd2cab4319..699eda7bd3fdf 100644 --- a/src/item.h +++ b/src/item.h @@ -1893,15 +1893,15 @@ class item : public visitable * @param carrier The current carrier * @return amount of power (kJ) */ - int electr_available( player *carrier ); - - /** + int electr_available( const player *carrier ) const; + + /** * Check if the item has enough power available to be activated - * Checks all the appropriate power sources: battery, bionic, UPS + * Checks all the appropriate power sources: battery, bionic, UPS * @param carrier The current carrier * @return true if there is enough power */ - bool has_enough_electr( const player &carrier ); + bool has_enough_electr( const player *carrier ) const; /** * Consume electric power (if available). If there is not enough power then none is drained and false is returned From b79085d6ea5b037de72a2971c99438b8d5406fff Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 12:27:40 +0300 Subject: [PATCH 08/87] use has_enough_electr --- src/character.cpp | 2 ++ src/game_inventory.cpp | 3 ++- src/item.h | 4 +++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index c8fa21c5c6e07..5e33b773b2152 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -11349,11 +11349,13 @@ void Character::consume_ups( int qty ) int adv = inv.charges_of( itype_adv_UPS_off, static_cast( std::ceil( qty * 0.6 ) ) ); if( qty != 0 && adv > 0 ) { + use_charges( itype_adv_UPS_off, adv ); qty -= std::min( qty, static_cast( adv / 0.6 ) ); } int ups = inv.charges_of( itype_UPS_off, qty ); if( qty != 0 && ups > 0 ) { + use_charges( itype_UPS_off, ups ); qty -= std::min( qty, ups ); } } diff --git a/src/game_inventory.cpp b/src/game_inventory.cpp index bb79da56fcdb9..e08ccbc3f9b66 100644 --- a/src/game_inventory.cpp +++ b/src/game_inventory.cpp @@ -1143,7 +1143,8 @@ class activatable_inventory_preset : public pickup_inventory_preset return string_format( _( "Your %s was broken and won't turn on." ), it.tname() ); } - if( !p.has_enough_charges( it, false ) ) { + //if( !p.has_enough_charges( it, false ) ) { + if( !it.has_enough_electr( &p ) ) { return string_format( ngettext( "Needs at least %d charge", "Needs at least %d charges", loc->ammo_required() ), diff --git a/src/item.h b/src/item.h index 699eda7bd3fdf..49b1a780197e6 100644 --- a/src/item.h +++ b/src/item.h @@ -1888,7 +1888,7 @@ class item : public visitable * How much electric energy is available. * If the item has flag_USES_BIONIC_POWER then return only bionic power * If the tool has battery loaded then that is included - * The check is dumb so don't call it on items that don't load batteries + * The check is dumb so don't call it on items that use other ammo * If the tool has flag_USE_UPS then UPS sources are included * @param carrier The current carrier * @return amount of power (kJ) @@ -1898,6 +1898,7 @@ class item : public visitable /** * Check if the item has enough power available to be activated * Checks all the appropriate power sources: battery, bionic, UPS + * The check is dumb so don't call it on items that use other ammo * @param carrier The current carrier * @return true if there is enough power */ @@ -1906,6 +1907,7 @@ class item : public visitable /** * Consume electric power (if available). If there is not enough power then none is drained and false is returned * Consume order: Battery, UPS + * The code is dumb so don't call it on items that use other ammo * @param qty maximum amount of electric power (kJ) that should be consumed * @param carrier The current carrier * @return false if there was not enough powr From c92187b54f0844f5b8c1e2df9d2f96e47fd8bb3e Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 12:28:21 +0300 Subject: [PATCH 09/87] lint --- data/json/items/tool/workshop.json | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/data/json/items/tool/workshop.json b/data/json/items/tool/workshop.json index 2b3856eb2e3d1..180caf5da56f7 100644 --- a/data/json/items/tool/workshop.json +++ b/data/json/items/tool/workshop.json @@ -338,15 +338,13 @@ "color": "light_gray", "ammo": [ "battery" ], "charges_per_use": 3500, - "pocket_data": [ + "pocket_data": [ { "pocket_type": "MAGAZINE_WELL", "rigid": true, "max_contains_volume": "20 L", "max_contains_weight": "40 kg", - "item_restriction": [ - "medium_storage_battery" - ] + "item_restriction": [ "medium_storage_battery" ] } ], "use_action": [ "JACKHAMMER" ], From 3dc28fde127fe243e05d54c93dba2b608202e9d4 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 13:04:09 +0300 Subject: [PATCH 10/87] UPS ammo does not exist anymore --- data/json/items/ammo_types.json | 6 ------ src/ammo.cpp | 4 +--- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/data/json/items/ammo_types.json b/data/json/items/ammo_types.json index eb3620ad13cc1..997bcf05db342 100644 --- a/data/json/items/ammo_types.json +++ b/data/json/items/ammo_types.json @@ -467,12 +467,6 @@ "name": "ferrous rail projectile", "default": "rebar_rail" }, - { - "type": "ammunition_type", - "id": "UPS", - "name": "UPS", - "default": "UPS" - }, { "type": "ammunition_type", "id": "thrown", diff --git a/src/ammo.cpp b/src/ammo.cpp index b1148125a5848..6d369cd128e22 100644 --- a/src/ammo.cpp +++ b/src/ammo.cpp @@ -10,8 +10,6 @@ #include "translations.h" #include "type_id.h" -static const itype_id itype_UPS( "UPS" ); - namespace { using ammo_map_t = std::unordered_map; @@ -69,7 +67,7 @@ void ammunition_type::check_consistency() const auto &at = ammo.second.default_ammotype_; // TODO: these ammo types should probably not have default ammo at all. - if( at == itype_UPS || at.str() == "components" || at.str() == "thrown" ) { + if( at.str() == "components" || at.str() == "thrown" ) { continue; } From 29a275e26c9e62d15219af97003cd039a0317423 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 13:39:26 +0300 Subject: [PATCH 11/87] use new method in places --- src/character.cpp | 4 ++-- src/game_inventory.cpp | 2 +- src/item.h | 4 ++-- src/item_action.cpp | 2 +- src/iuse.cpp | 2 +- src/npcmove.cpp | 2 +- src/ranged.cpp | 2 +- src/visitable.cpp | 6 ------ 8 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index 5e33b773b2152..b5ff2ca90c17e 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -11349,13 +11349,13 @@ void Character::consume_ups( int qty ) int adv = inv.charges_of( itype_adv_UPS_off, static_cast( std::ceil( qty * 0.6 ) ) ); if( qty != 0 && adv > 0 ) { - use_charges( itype_adv_UPS_off, adv ); + use_charges( itype_adv_UPS_off, adv ); qty -= std::min( qty, static_cast( adv / 0.6 ) ); } int ups = inv.charges_of( itype_UPS_off, qty ); if( qty != 0 && ups > 0 ) { - use_charges( itype_UPS_off, ups ); + use_charges( itype_UPS_off, ups ); qty -= std::min( qty, ups ); } } diff --git a/src/game_inventory.cpp b/src/game_inventory.cpp index e08ccbc3f9b66..8af4e05959cf3 100644 --- a/src/game_inventory.cpp +++ b/src/game_inventory.cpp @@ -1144,7 +1144,7 @@ class activatable_inventory_preset : public pickup_inventory_preset } //if( !p.has_enough_charges( it, false ) ) { - if( !it.has_enough_electr( &p ) ) { + if( !it.has_enough_electr( &p ) ) { return string_format( ngettext( "Needs at least %d charge", "Needs at least %d charges", loc->ammo_required() ), diff --git a/src/item.h b/src/item.h index 49b1a780197e6..793b306ef8689 100644 --- a/src/item.h +++ b/src/item.h @@ -1898,7 +1898,7 @@ class item : public visitable /** * Check if the item has enough power available to be activated * Checks all the appropriate power sources: battery, bionic, UPS - * The check is dumb so don't call it on items that use other ammo + * The check is dumb so don't call it on items that use other ammo * @param carrier The current carrier * @return true if there is enough power */ @@ -1907,7 +1907,7 @@ class item : public visitable /** * Consume electric power (if available). If there is not enough power then none is drained and false is returned * Consume order: Battery, UPS - * The code is dumb so don't call it on items that use other ammo + * The code is dumb so don't call it on items that use other ammo * @param qty maximum amount of electric power (kJ) that should be consumed * @param carrier The current carrier * @return false if there was not enough powr diff --git a/src/item_action.cpp b/src/item_action.cpp index 2e332be0888de..1518389095cff 100644 --- a/src/item_action.cpp +++ b/src/item_action.cpp @@ -155,7 +155,7 @@ item_action_map item_action_generator::map_actions_to_items( player &p, } if( !actual_item->ammo_sufficient() && ( !actual_item->has_flag( STATIC( flag_id( "USE_UPS" ) ) ) || - p.charges_of( itype_UPS ) < actual_item->ammo_required() ) ) { + p.available_ups() < actual_item->ammo_required() ) ) { continue; } diff --git a/src/iuse.cpp b/src/iuse.cpp index 195649347f303..5f22c5492daf4 100644 --- a/src/iuse.cpp +++ b/src/iuse.cpp @@ -4097,7 +4097,7 @@ cata::optional iuse::tazer( player *p, item *it, bool, const tripoint &pos cata::optional iuse::tazer2( player *p, item *it, bool b, const tripoint &pos ) { if( it->ammo_remaining() >= 100 || ( it->has_flag( flag_USE_UPS ) && - p->charges_of( itype_UPS ) >= 100 ) ) { + p->available_ups() >= 100 ) ) { // Instead of having a ctrl+c+v of the function above, spawn a fake tazer and use it // Ugly, but less so than copied blocks item fake( "tazer", calendar::turn_zero ); diff --git a/src/npcmove.cpp b/src/npcmove.cpp index 55da7e2f6e31b..bbb1587916817 100644 --- a/src/npcmove.cpp +++ b/src/npcmove.cpp @@ -3319,7 +3319,7 @@ bool npc::wield_better_weapon() item *best = &weapon; double best_value = -100.0; - const int ups_charges = charges_of( itype_UPS ); + const int ups_charges = available_ups(); const auto compare_weapon = [this, &best, &best_value, ups_charges, can_use_gun, use_silent]( const item & it ) { diff --git a/src/ranged.cpp b/src/ranged.cpp index ce40ce2c39740..04d07a9003f61 100644 --- a/src/ranged.cpp +++ b/src/ranged.cpp @@ -740,7 +740,7 @@ int player::fire_gun( const tripoint &target, int shots, item &gun ) // cap our maximum burst size by the amount of UPS power left if( !gun.has_flag( flag_VEHICLE ) && gun.get_gun_ups_drain() > 0 ) { - shots = std::min( shots, static_cast( charges_of( itype_UPS ) / gun.get_gun_ups_drain() ) ); + shots = std::min( shots, static_cast( available_ups() / gun.get_gun_ups_drain() ) ); } if( shots <= 0 ) { diff --git a/src/visitable.cpp b/src/visitable.cpp index 2d91f765816ee..eb72a0c763ca8 100644 --- a/src/visitable.cpp +++ b/src/visitable.cpp @@ -838,12 +838,6 @@ int Character::charges_of( const itype_id &what, int limit, if( p && p->has_active_bionic( bio_ups ) ) { qty = sum_no_wrap( qty, units::to_kilojoule( p->get_power_level() ) ); } - if( p && p->is_mounted() ) { - const auto *mons = p->mounted_creature.get(); - if( mons->has_flag( MF_RIDEABLE_MECH ) && mons->battery_item ) { - qty = sum_no_wrap( qty, mons->battery_item->ammo_remaining() ); - } - } return std::min( qty, limit ); } From 2dab75aa8fcaa6d33a547f3abe8435fb892b83cd Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 13:46:53 +0300 Subject: [PATCH 12/87] use new method in ranged --- src/ranged.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/ranged.cpp b/src/ranged.cpp index 04d07a9003f61..653be17123d97 100644 --- a/src/ranged.cpp +++ b/src/ranged.cpp @@ -92,7 +92,6 @@ static const itype_id itype_flammable( "flammable" ); static const itype_id itype_m235( "m235" ); static const itype_id itype_metal_rail( "metal_rail" ); static const itype_id itype_UPS( "UPS" ); -static const itype_id itype_UPS_off( "UPS_off" ); static const trap_str_id tr_practice_target( "tr_practice_target" ); @@ -3517,10 +3516,7 @@ bool gunmode_checks_weapon( avatar &you, const map &m, std::vector } } if( !is_mech_weapon ) { - if( !( you.has_charges( itype_UPS_off, ups_drain ) || - you.has_charges( itype_adv_UPS_off, adv_ups_drain ) || - ( you.has_active_bionic( bio_ups ) && - you.get_power_level() >= units::from_kilojoule( ups_drain ) ) ) ) { + if( you.available_ups() < ups_drain ) { messages.push_back( string_format( _( "You need a UPS with at least %2$d charges or an advanced UPS with at least %3$d charges to fire the %1$s!" ), gmode->tname(), ups_drain, adv_ups_drain ) ); From 822a4c411785d11405cd1030d84d3fa559e0fc30 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 14:04:51 +0300 Subject: [PATCH 13/87] ups in generic_game_turn_handler --- src/activity_handlers.cpp | 6 +++--- src/character.cpp | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/activity_handlers.cpp b/src/activity_handlers.cpp index e0cbe235859d5..cc79893090ae1 100644 --- a/src/activity_handlers.cpp +++ b/src/activity_handlers.cpp @@ -205,7 +205,6 @@ static const itype_id itype_splinter( "splinter" ); static const itype_id itype_stick_long( "stick_long" ); static const itype_id itype_steel_chunk( "steel_chunk" ); static const itype_id itype_steel_plate( "steel_plate" ); -static const itype_id itype_UPS( "UPS" ); static const itype_id itype_wire( "wire" ); static const itype_id itype_chain( "chain" ); @@ -1700,7 +1699,8 @@ void activity_handlers::generic_game_turn_handler( player_activity *act, player const int ammo_required = game_item.ammo_required(); bool fail = false; if( game_item.has_flag( flag_USE_UPS ) ) { - fail = !p->use_charges_if_avail( itype_UPS, ammo_required ); + electr_consume( ammo_required, &p ) + //fail = !p->use_charges_if_avail( itype_UPS, ammo_required ); } else { fail = game_item.ammo_consume( ammo_required, p->pos() ) == 0; } @@ -2571,7 +2571,7 @@ void activity_handlers::repair_item_finish( player_activity *act, player *p ) int ammo_remaining = used_tool->ammo_remaining(); if( used_tool->has_flag( flag_USE_UPS ) ) { - ammo_remaining = p->charges_of( itype_UPS ); + ammo_remaining = p->available_ups(); } std::set valid_entries = actor->get_valid_repair_materials( fix ); diff --git a/src/character.cpp b/src/character.cpp index b5ff2ca90c17e..ede01117f26a2 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -11324,7 +11324,8 @@ int Character::available_ups() const if( has_power() && has_active_bionic( bio_ups ) ) { available_charges += units::to_kilojoule( get_power_level() ); } - available_charges += charges_of( itype_UPS ); + available_charges += charges_of( itype_UPS_off ); + available_charges += charges_of( itype_adv_UPS_off ); return available_charges; } From d7ece5ab98ea1bdd4aefc1b64db166c9dd2c8412 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 14:36:25 +0300 Subject: [PATCH 14/87] new methods in more places --- src/activity_handlers.cpp | 2 +- src/character.cpp | 2 +- src/iexamine.cpp | 5 ++--- src/item.cpp | 13 ++++--------- src/item_action.cpp | 2 -- src/npcmove.cpp | 1 - src/player.cpp | 3 +-- src/ranged.cpp | 5 ++--- 8 files changed, 11 insertions(+), 22 deletions(-) diff --git a/src/activity_handlers.cpp b/src/activity_handlers.cpp index cc79893090ae1..61697a2649fd7 100644 --- a/src/activity_handlers.cpp +++ b/src/activity_handlers.cpp @@ -1699,7 +1699,7 @@ void activity_handlers::generic_game_turn_handler( player_activity *act, player const int ammo_required = game_item.ammo_required(); bool fail = false; if( game_item.has_flag( flag_USE_UPS ) ) { - electr_consume( ammo_required, &p ) + game_item.electr_consume( ammo_required, p ); //fail = !p->use_charges_if_avail( itype_UPS, ammo_required ); } else { fail = game_item.ammo_consume( ammo_required, p->pos() ) == 0; diff --git a/src/character.cpp b/src/character.cpp index ede01117f26a2..21a7c8d2cf0c4 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -11325,7 +11325,7 @@ int Character::available_ups() const available_charges += units::to_kilojoule( get_power_level() ); } available_charges += charges_of( itype_UPS_off ); - available_charges += charges_of( itype_adv_UPS_off ); + available_charges += charges_of( itype_adv_UPS_off ); return available_charges; } diff --git a/src/iexamine.cpp b/src/iexamine.cpp index 90b0736d944fe..ce599e8907138 100644 --- a/src/iexamine.cpp +++ b/src/iexamine.cpp @@ -162,7 +162,6 @@ static const itype_id itype_string_36( "string_36" ); static const itype_id itype_tree_spile( "tree_spile" ); static const itype_id itype_unfinished_cac2( "unfinished_cac2" ); static const itype_id itype_unfinished_charcoal( "unfinished_charcoal" ); -static const itype_id itype_UPS( "UPS" ); static const itype_id itype_water( "water" ); static const skill_id skill_cooking( "cooking" ); @@ -2693,7 +2692,7 @@ void iexamine::arcfurnace_empty( player &p, const tripoint &examp ) return; } //arc furnaces require a huge amount of current, so 1 full storage battery would work as a stand in - if( !p.has_charges( itype_UPS, 1250 ) ) { + if( p.available_ups() < 1250 ) { add_msg( _( "This furnace is ready to be turned on, but you lack a UPS with sufficient power." ) ); return; } else { @@ -2704,7 +2703,7 @@ void iexamine::arcfurnace_empty( player &p, const tripoint &examp ) } } - p.use_charges( itype_UPS, 1250 ); + p.consume_ups( 1250 ); here.i_clear( examp ); here.furn_set( examp, next_arcfurnace_type ); item result( "unfinished_cac2", calendar::turn ); diff --git a/src/item.cpp b/src/item.cpp index 974ac0073c9c5..cc6657f514b7d 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -10452,25 +10452,20 @@ bool item::process_tool( player *carrier, const tripoint &pos ) if( type->tool->turns_per_charge > 0 && to_turn( calendar::turn ) % type->tool->turns_per_charge == 0 ) { energy = std::max( ammo_required(), 1 ); - } else if( type->tool->power_draw > 0 ) { // power_draw in mW / 1000000 to give kJ (battery unit) per second energy = type->tool->power_draw / 1000000; // energy_bat remainder results in chance at additional charge/discharge energy += x_in_y( type->tool->power_draw % 1000000, 1000000 ) ? 1 : 0; } - energy -= ammo_consume( energy, pos ); - // for items in player possession if insufficient charges within tool try UPS - if( carrier && has_flag( flag_USE_UPS ) ) { - if( carrier->use_charges_if_avail( itype_UPS, energy ) ) { - energy = 0; - } - } + int energy_available = electr_available( carrier ); + electr_consume( std::min( energy_available, energy ), carrier ); + avatar &player_character = get_avatar(); // if insufficient available charges shutdown the tool - if( energy > 0 ) { + if( energy_available > energy ) { if( carrier && has_flag( flag_USE_UPS ) ) { carrier->add_msg_if_player( m_info, _( "You need an UPS to run the %s!" ), tname() ); } diff --git a/src/item_action.cpp b/src/item_action.cpp index 1518389095cff..c7f55ce82cdeb 100644 --- a/src/item_action.cpp +++ b/src/item_action.cpp @@ -40,8 +40,6 @@ class Character; static const std::string errstring( "ERROR" ); -static const itype_id itype_UPS( "UPS" ); - struct tripoint; static item_action nullaction; diff --git a/src/npcmove.cpp b/src/npcmove.cpp index bbb1587916817..4d50f555e519e 100644 --- a/src/npcmove.cpp +++ b/src/npcmove.cpp @@ -125,7 +125,6 @@ static const itype_id itype_lsd( "lsd" ); static const itype_id itype_smoxygen_tank( "smoxygen_tank" ); static const itype_id itype_thorazine( "thorazine" ); static const itype_id itype_oxygen_tank( "oxygen_tank" ); -static const itype_id itype_UPS( "UPS" ); static const material_id material_battery( "battery" ); static const material_id material_chem_ethanol( "chem_ethanol" ); diff --git a/src/player.cpp b/src/player.cpp index 67ca9f1e05212..2dcda91d55d43 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -97,7 +97,6 @@ static const itype_id itype_adv_UPS_off( "adv_UPS_off" ); static const itype_id itype_battery( "battery" ); static const itype_id itype_large_repairkit( "large_repairkit" ); static const itype_id itype_small_repairkit( "small_repairkit" ); -static const itype_id itype_UPS( "UPS" ); static const itype_id itype_UPS_off( "UPS_off" ); static const trait_id trait_DEBUG_NODMG( "DEBUG_NODMG" ); @@ -1216,7 +1215,7 @@ void player::process_items() } } if( ch_UPS_used > 0 ) { - use_charges( itype_UPS, ch_UPS_used ); + consume_ups( ch_UPS_used ); } } diff --git a/src/ranged.cpp b/src/ranged.cpp index 653be17123d97..7c38290cd06c3 100644 --- a/src/ranged.cpp +++ b/src/ranged.cpp @@ -91,7 +91,6 @@ static const itype_id itype_bolt( "bolt" ); static const itype_id itype_flammable( "flammable" ); static const itype_id itype_m235( "m235" ); static const itype_id itype_metal_rail( "metal_rail" ); -static const itype_id itype_UPS( "UPS" ); static const trap_str_id tr_practice_target( "tr_practice_target" ); @@ -819,7 +818,7 @@ int player::fire_gun( const tripoint &target, int shots, item &gun ) } if( !gun.has_flag( flag_VEHICLE ) ) { - use_charges( itype_UPS, gun.get_gun_ups_drain() ); + consume_ups( gun.get_gun_ups_drain() ); } if( shot.missed_by <= .1 ) { @@ -3523,7 +3522,7 @@ bool gunmode_checks_weapon( avatar &you, const map &m, std::vector result = false; } } else { - if( !you.has_charges( itype_UPS, ups_drain ) ) { + if( you.available_ups() < ups_drain ) { messages.push_back( string_format( _( "Your mech has an empty battery, its %s will not fire." ), gmode->tname() ) ); result = false; From 448ca88f4633e73390da2db71bec30a73d30031a Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 14:44:05 +0300 Subject: [PATCH 15/87] why is tis 2x I do not know --- src/npc.cpp | 3 +-- src/player.cpp | 18 ++---------------- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/src/npc.cpp b/src/npc.cpp index 903f2f7b61f5d..0605d6daa7f91 100644 --- a/src/npc.cpp +++ b/src/npc.cpp @@ -1533,8 +1533,7 @@ void npc::decide_needs() if( weapon.is_gun() ) { int ups_drain = weapon.get_gun_ups_drain(); if( ups_drain > 0 ) { - int ups_charges = charges_of( itype_UPS_off, ups_drain ) + - charges_of( itype_UPS_off, ups_drain ); + int ups_charges = 2 * available_ups(); needrank[need_ammo] = static_cast( ups_charges ) / ups_drain; } else { const ammotype ammo_type = weapon.ammo_type(); diff --git a/src/player.cpp b/src/player.cpp index 2dcda91d55d43..7f03188b8d075 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -97,7 +97,6 @@ static const itype_id itype_adv_UPS_off( "adv_UPS_off" ); static const itype_id itype_battery( "battery" ); static const itype_id itype_large_repairkit( "large_repairkit" ); static const itype_id itype_small_repairkit( "small_repairkit" ); -static const itype_id itype_UPS_off( "UPS_off" ); static const trait_id trait_DEBUG_NODMG( "DEBUG_NODMG" ); static const trait_id trait_CENOBITE( "CENOBITE" ); @@ -1165,18 +1164,8 @@ void player::process_items() } // Active item processing done, now we're recharging. - int ch_UPS = 0; - const auto inv_is_ups = items_with( []( const item & itm ) { - return itm.has_flag( flag_IS_UPS ); - } ); - for( const auto &it : inv_is_ups ) { - itype_id identifier = it->type->get_id(); - if( identifier == itype_UPS_off ) { - ch_UPS += it->ammo_remaining(); - } else if( identifier == itype_adv_UPS_off ) { - ch_UPS += it->ammo_remaining() / 0.6; - } - } + int ch_UPS = available_ups(); + bool update_required = get_check_encumbrance(); for( item &w : worn ) { if( !update_required && w.encumbrance_update_ ) { @@ -1188,9 +1177,6 @@ void player::process_items() calc_encumbrance(); set_check_encumbrance( false ); } - if( has_active_bionic( bionic_id( "bio_ups" ) ) ) { - ch_UPS += units::to_kilojoule( get_power_level() ); - } int ch_UPS_used = 0; // Load all items that use the UPS to their minimal functional charge, From 39cb825f5486935acb6b604b22fea94db7819ca5 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 14:54:40 +0300 Subject: [PATCH 16/87] fix comparison --- src/activity_handlers.cpp | 10 ++-------- src/bionics.cpp | 2 +- src/item.cpp | 2 +- src/npc.cpp | 2 -- 4 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/activity_handlers.cpp b/src/activity_handlers.cpp index 61697a2649fd7..d2e174f60e2ca 100644 --- a/src/activity_handlers.cpp +++ b/src/activity_handlers.cpp @@ -1697,14 +1697,8 @@ void activity_handlers::generic_game_turn_handler( player_activity *act, player if( !act->targets.empty() ) { item &game_item = *act->targets.front(); const int ammo_required = game_item.ammo_required(); - bool fail = false; - if( game_item.has_flag( flag_USE_UPS ) ) { - game_item.electr_consume( ammo_required, p ); - //fail = !p->use_charges_if_avail( itype_UPS, ammo_required ); - } else { - fail = game_item.ammo_consume( ammo_required, p->pos() ) == 0; - } - if( fail ) { + bool success = game_item.electr_consume( ammo_required, p ); + if( !success ) { act->moves_left = 0; add_msg( m_info, _( "The %s runs out of batteries." ), game_item.tname() ); return; diff --git a/src/bionics.cpp b/src/bionics.cpp index 3b258760ebb01..30c681ef7d29f 100644 --- a/src/bionics.cpp +++ b/src/bionics.cpp @@ -536,7 +536,7 @@ void npc::check_or_use_weapon_cbm( const bionic_id &cbm_id ) return; } - const int ups_charges = charges_of( itype_UPS ); + const int ups_charges = available_ups(); int ammo_count = weapon.ammo_remaining(); const int ups_drain = weapon.get_gun_ups_drain(); if( ups_drain > 0 ) { diff --git a/src/item.cpp b/src/item.cpp index cc6657f514b7d..25f10600b1652 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -10465,7 +10465,7 @@ bool item::process_tool( player *carrier, const tripoint &pos ) avatar &player_character = get_avatar(); // if insufficient available charges shutdown the tool - if( energy_available > energy ) { + if( energy_available < energy ) { if( carrier && has_flag( flag_USE_UPS ) ) { carrier->add_msg_if_player( m_info, _( "You need an UPS to run the %s!" ), tname() ); } diff --git a/src/npc.cpp b/src/npc.cpp index 0605d6daa7f91..3a8bf8af97faa 100644 --- a/src/npc.cpp +++ b/src/npc.cpp @@ -97,8 +97,6 @@ static const efftype_id effect_pkill3( "pkill3" ); static const efftype_id effect_ridden( "ridden" ); static const efftype_id effect_riding( "riding" ); -static const itype_id itype_UPS_off( "UPS_off" ); - static const skill_id skill_archery( "archery" ); static const skill_id skill_bashing( "bashing" ); static const skill_id skill_cutting( "cutting" ); From af501ee469526240eeea62e1605f5ae4d2f4f0d6 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 15:04:58 +0300 Subject: [PATCH 17/87] use new method --- src/item.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/item.cpp b/src/item.cpp index 25f10600b1652..b224fd1af6ac8 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -141,7 +141,6 @@ static const itype_id itype_hand_crossbow( "hand_crossbow" ); static const itype_id itype_joint_roach( "joint_roach" ); static const itype_id itype_rad_badge( "rad_badge" ); static const itype_id itype_tuned_mechanism( "tuned_mechanism" ); -static const itype_id itype_UPS( "UPS" ); static const itype_id itype_waterproof_gunmod( "waterproof_gunmod" ); static const skill_id skill_cooking( "cooking" ); @@ -8570,7 +8569,7 @@ int item::units_remaining( const Character &ch, int limit ) const int res = ammo_remaining(); if( res < limit && has_flag( flag_USE_UPS ) ) { - res += ch.charges_of( itype_UPS, limit - res ); + res += std::min( ch.available_ups(), limit - res ); } return std::min( static_cast( res ), limit ); From 360f6dd74c43ac91aa10929c31352a5df41133fe Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 15:11:10 +0300 Subject: [PATCH 18/87] astyle --- src/item.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/item.cpp b/src/item.cpp index b224fd1af6ac8..50d0243b42e45 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8569,7 +8569,7 @@ int item::units_remaining( const Character &ch, int limit ) const int res = ammo_remaining(); if( res < limit && has_flag( flag_USE_UPS ) ) { - res += std::min( ch.available_ups(), limit - res ); + res += std::min( ch.available_ups(), limit - res ); } return std::min( static_cast( res ), limit ); From d92544c2440df3325d3f0ab287483febd5f7a3d9 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 16:22:21 +0300 Subject: [PATCH 19/87] fix item process --- src/character.cpp | 2 +- src/item.cpp | 12 ++++++++---- src/iuse.cpp | 8 ++++---- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index 21a7c8d2cf0c4..149fac1096079 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -7871,7 +7871,7 @@ int Character::ammo_count_for( const item &gun ) int ups_drain = gun.get_gun_ups_drain(); if( ups_drain > 0 ) { - ret = std::min( ret, charges_of( itype_UPS ) / ups_drain ); + ret = std::min( ret, available_ups() / ups_drain ); } return ret; diff --git a/src/item.cpp b/src/item.cpp index 50d0243b42e45..b9fdf5c0f75bd 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -10458,13 +10458,17 @@ bool item::process_tool( player *carrier, const tripoint &pos ) energy += x_in_y( type->tool->power_draw % 1000000, 1000000 ) ? 1 : 0; } - int energy_available = electr_available( carrier ); - electr_consume( std::min( energy_available, energy ), carrier ); - + if( ammo_types().count( ammo_battery ) ) { + int energy_consume = std::min( electr_available( carrier ), energy ); + energy -= energy_consume; + electr_consume( energy_consume, carrier ); + } else { + energy -= ammo_consume( energy, pos ); + } avatar &player_character = get_avatar(); // if insufficient available charges shutdown the tool - if( energy_available < energy ) { + if( energy > 0 ) { if( carrier && has_flag( flag_USE_UPS ) ) { carrier->add_msg_if_player( m_info, _( "You need an UPS to run the %s!" ), tname() ); } diff --git a/src/iuse.cpp b/src/iuse.cpp index 5f22c5492daf4..12b28d9b6deb3 100644 --- a/src/iuse.cpp +++ b/src/iuse.cpp @@ -8377,7 +8377,7 @@ cata::optional iuse::autoclave( player *p, item *it, bool t, const tripoint static const int power_need = ( ( it->type->tool->power_draw / 1000 ) * to_seconds ( 90_minutes ) ) / 1000 + 100; if( power_need > it->ammo_remaining() && !( it->has_flag( flag_USE_UPS ) && - p->charges_of( itype_UPS ) >= power_need ) ) { + p->available_ups() >= power_need ) ) { popup( _( "The autoclave doesn't have enough battery for one cycle. You need at least %s charges." ), power_need ); return cata::nullopt; @@ -8430,7 +8430,7 @@ cata::optional iuse::multicooker( player *p, item *it, bool t, const tripoi if( t ) { //stop action before power runs out and iuse deletes the cooker if( !( it->ammo_remaining() >= charge_buffer ) && !( it->has_flag( flag_USE_UPS ) && - p->charges_of( itype_UPS ) >= charge_buffer ) ) { + p->available_ups() >= charge_buffer ) ) { it->active = false; it->erase_var( "RECIPE" ); it->convert( itype_multi_cooker ); @@ -8525,7 +8525,7 @@ cata::optional iuse::multicooker( player *p, item *it, bool t, const tripoi } else { if( dish_it == nullptr ) { if( it->ammo_remaining() < charges_to_start && !( it->has_flag( flag_USE_UPS ) && - p->charges_of( itype_UPS ) >= charges_to_start ) ) { + p->available_ups() >= charges_to_start ) ) { p->add_msg_if_player( _( "Batteries are low." ) ); return 0; } @@ -8662,7 +8662,7 @@ cata::optional iuse::multicooker( player *p, item *it, bool t, const tripoi 1000 ) ) / 1000; if( it->ammo_remaining() < all_charges && !( it->has_flag( flag_USE_UPS ) && - p->charges_of( itype_UPS ) >= all_charges ) ) { + p->available_ups() >= all_charges ) ) { p->add_msg_if_player( m_warning, _( "The multi-cooker needs %d charges to cook this dish." ), From 313f58cb04db6e389748b674350aa31cc50777f5 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 16:36:19 +0300 Subject: [PATCH 20/87] probably safe --- src/activity_handlers.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/activity_handlers.cpp b/src/activity_handlers.cpp index d2e174f60e2ca..7f923293ab981 100644 --- a/src/activity_handlers.cpp +++ b/src/activity_handlers.cpp @@ -1697,6 +1697,7 @@ void activity_handlers::generic_game_turn_handler( player_activity *act, player if( !act->targets.empty() ) { item &game_item = *act->targets.front(); const int ammo_required = game_item.ammo_required(); + // I am fairly sure this item is always battery operated bool success = game_item.electr_consume( ammo_required, p ); if( !success ) { act->moves_left = 0; From aa7572f966f44bcf80b6b690f7f8e6c191829747 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 17:14:17 +0300 Subject: [PATCH 21/87] astyle --- src/activity_handlers.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/activity_handlers.cpp b/src/activity_handlers.cpp index 7f923293ab981..a7df2dd881c1d 100644 --- a/src/activity_handlers.cpp +++ b/src/activity_handlers.cpp @@ -1697,7 +1697,7 @@ void activity_handlers::generic_game_turn_handler( player_activity *act, player if( !act->targets.empty() ) { item &game_item = *act->targets.front(); const int ammo_required = game_item.ammo_required(); - // I am fairly sure this item is always battery operated + // I am fairly sure this item is always battery operated bool success = game_item.electr_consume( ammo_required, p ); if( !success ) { act->moves_left = 0; From 6ffa4023a67d45b76dac945d668269de4c2a6f40 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 17:23:32 +0300 Subject: [PATCH 22/87] cleanup --- src/game_inventory.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/game_inventory.cpp b/src/game_inventory.cpp index 8af4e05959cf3..abfc5f2bdb741 100644 --- a/src/game_inventory.cpp +++ b/src/game_inventory.cpp @@ -1143,7 +1143,6 @@ class activatable_inventory_preset : public pickup_inventory_preset return string_format( _( "Your %s was broken and won't turn on." ), it.tname() ); } - //if( !p.has_enough_charges( it, false ) ) { if( !it.has_enough_electr( &p ) ) { return string_format( ngettext( "Needs at least %d charge", From a300df970af24f79cb02793d179659ae2cbfe5fa Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 19:11:44 +0300 Subject: [PATCH 23/87] unused line --- src/character.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/character.cpp b/src/character.cpp index 149fac1096079..418dbd99ae444 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -11357,7 +11357,6 @@ void Character::consume_ups( int qty ) int ups = inv.charges_of( itype_UPS_off, qty ); if( qty != 0 && ups > 0 ) { use_charges( itype_UPS_off, ups ); - qty -= std::min( qty, ups ); } } From 1a01e10491f799f6acb2bbc9fcea472c20025af9 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 22:32:14 +0300 Subject: [PATCH 24/87] reqrite --- src/activity_handlers.cpp | 8 +- src/activity_item_handling.cpp | 8 +- src/avatar_action.cpp | 2 +- src/game_inventory.cpp | 2 +- src/item.cpp | 169 ++++++++++++--------------------- src/item.h | 47 +++------ src/iuse_actor.cpp | 2 +- 7 files changed, 82 insertions(+), 156 deletions(-) diff --git a/src/activity_handlers.cpp b/src/activity_handlers.cpp index a7df2dd881c1d..25ad32ade1477 100644 --- a/src/activity_handlers.cpp +++ b/src/activity_handlers.cpp @@ -1698,8 +1698,8 @@ void activity_handlers::generic_game_turn_handler( player_activity *act, player item &game_item = *act->targets.front(); const int ammo_required = game_item.ammo_required(); // I am fairly sure this item is always battery operated - bool success = game_item.electr_consume( ammo_required, p ); - if( !success ) { + bool fail = game_item.ammo_consume( ammo_required, tripoint_zero, p ) == 0; + if( fail ) { act->moves_left = 0; add_msg( m_info, _( "The %s runs out of batteries." ), game_item.tname() ); return; @@ -2128,7 +2128,7 @@ void activity_handlers::vibe_do_turn( player_activity *act, player *p ) if( calendar::once_every( 1_minutes ) ) { if( vibrator_item.ammo_remaining() > 0 ) { - vibrator_item.ammo_consume( 1, p->pos() ); + vibrator_item.ammo_consume( 1, p->pos(), p ); p->add_morale( MORALE_FEELING_GOOD, 3, 40 ); if( vibrator_item.ammo_remaining() == 0 ) { add_msg( m_info, _( "The %s runs out of batteries." ), vibrator_item.tname() ); @@ -3812,7 +3812,7 @@ void activity_handlers::jackhammer_finish( player_activity *act, player *p ) act->set_to_null(); if( !act->targets.empty() ) { item &it = *act->targets.front(); - it.electr_consume( it.ammo_required(), p ); + it.ammo_consume( it.ammo_required(), tripoint_zero, p ); } else { debugmsg( "jackhammer activity targets empty" ); } diff --git a/src/activity_item_handling.cpp b/src/activity_item_handling.cpp index cbb4a39f40d9b..5d2b88886ebb5 100644 --- a/src/activity_item_handling.cpp +++ b/src/activity_item_handling.cpp @@ -1134,9 +1134,9 @@ static activity_reason_info can_do_activity_there( const activity_id &act, playe if( !here.has_flag( "MINEABLE", src_loc ) ) { return activity_reason_info::fail( do_activity_reason::NO_ZONE ); } - std::vector mining_inv = p.items_with( []( const item & itm ) { + std::vector mining_inv = p.items_with( [&p]( const item & itm ) { return ( itm.has_flag( flag_DIG_TOOL ) && !itm.type->can_use( "JACKHAMMER" ) ) || - ( itm.type->can_use( "JACKHAMMER" ) && itm.ammo_sufficient() ); + ( itm.type->can_use( "JACKHAMMER" ) && itm.ammo_sufficient(&p) ); } ); if( mining_inv.empty() ) { return activity_reason_info::fail( do_activity_reason::NEEDS_MINING ); @@ -2119,9 +2119,9 @@ static int chop_moves( player &p, item *it ) static bool mine_activity( player &p, const tripoint &src_loc ) { - std::vector mining_inv = p.items_with( []( const item & itm ) { + std::vector mining_inv = p.items_with( [&p]( const item & itm ) { return ( itm.has_flag( flag_DIG_TOOL ) && !itm.type->can_use( "JACKHAMMER" ) ) || - ( itm.type->can_use( "JACKHAMMER" ) && itm.ammo_sufficient() ); + ( itm.type->can_use( "JACKHAMMER" ) && itm.ammo_sufficient( &p ) ); } ); map &here = get_map(); if( mining_inv.empty() || p.is_mounted() || p.is_underwater() || here.veh_at( src_loc ) || diff --git a/src/avatar_action.cpp b/src/avatar_action.cpp index f056e7cbb6a2a..daea57c70973f 100644 --- a/src/avatar_action.cpp +++ b/src/avatar_action.cpp @@ -175,7 +175,7 @@ bool avatar_action::move( avatar &you, map &m, const tripoint &d ) !m.veh_at( dest_loc ) && !you.is_underwater() && !you.has_effect( effect_stunned ) && !is_riding && !you.has_effect( effect_incorporeal ) ) { if( you.weapon.has_flag( flag_DIG_TOOL ) ) { - if( you.weapon.type->can_use( "JACKHAMMER" ) && you.weapon.ammo_sufficient() ) { + if( you.weapon.type->can_use( "JACKHAMMER" ) && you.weapon.ammo_sufficient( &dynamic_cast( you ) ) ) { you.invoke_item( &you.weapon, "JACKHAMMER", dest_loc ); // don't move into the tile until done mining you.defer_move( dest_loc ); diff --git a/src/game_inventory.cpp b/src/game_inventory.cpp index abfc5f2bdb741..e299982cf6bc1 100644 --- a/src/game_inventory.cpp +++ b/src/game_inventory.cpp @@ -1143,7 +1143,7 @@ class activatable_inventory_preset : public pickup_inventory_preset return string_format( _( "Your %s was broken and won't turn on." ), it.tname() ); } - if( !it.has_enough_electr( &p ) ) { + if( !it.ammo_sufficient( &p ) ) { return string_format( ngettext( "Needs at least %d charge", "Needs at least %d charges", loc->ammo_required() ), diff --git a/src/item.cpp b/src/item.cpp index b9fdf5c0f75bd..b4c12e0d1ee64 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -7934,52 +7934,48 @@ units::energy item::energy_remaining() const return 0_J; } -int item::ammo_remaining() const +int item::ammo_remaining( const player *carrier) const { + int ret = 0; const item *mag = magazine_current(); if( mag ) { - return mag->ammo_remaining(); + ret += mag->ammo_remaining(); + } + + if(carrier != nullptr && has_flag( flag_USES_BIONIC_POWER ) ) { + ret += units::to_kilojoule( carrier->get_power_level() ); + } + + // Charges contained in the tool itself with "max_charges" + // Remove once no tool does this anymore + if( is_tool() && !ammo_types().empty() ) { + ret += charges; } - if( is_tool() ) { + if( carrier != nullptr && has_flag( flag_USE_UPS ) ) { + ret += carrier->available_ups(); + } - if( ammo_types().empty() || - !contents.has_pocket_type( item_pocket::pocket_type::MAGAZINE ) ) { - // includes auxiliary gunmods - if( has_flag( flag_USES_BIONIC_POWER ) ) { - int power = units::to_kilojoule( get_player_character().get_power_level() ); - return power; - } - return charges; - } else { - int res = 0; - for( const item *e : contents.all_items_top( item_pocket::pocket_type::MAGAZINE ) ) { - res += e->charges; - } - return res; - } - } else if( is_gun() && magazine_integral() && !contents.empty() ) { + + if( is_gun() && magazine_integral() && !contents.empty() ) { return contents.first_ammo().charges; } - if( is_magazine() ) { - int res = 0; + if( is_magazine() ) { for( const item *e : contents.all_items_top( item_pocket::pocket_type::MAGAZINE ) ) { - res += e->charges; + ret += e->charges; } - return res; } - - // Handle non-magazines with ammo_restriction in a CONTAINER type pocket (like quivers) + + // Handle non-magazines with ammo_restriction in a CONTAINER type pocket (like quivers) if( !ammo_types().empty() ) { - int res = 0; for( const item *e : contents.all_items_top( item_pocket::pocket_type::CONTAINER ) ) { - res += e->charges; + ret += e->charges; } - return res; } + + return ret; - return 0; } int item::remaining_ammo_capacity() const @@ -8037,89 +8033,48 @@ int item::ammo_required() const return 0; } -bool item::ammo_sufficient( int qty ) const +bool item::ammo_sufficient(const player* carrier, int qty ) const { - return ammo_remaining() >= ammo_required() * qty; + if( !ammo_required() ) { + return true; + } + return ammo_remaining( carrier ) >= ammo_required() * qty; } -int item::ammo_consume( int qty, const tripoint &pos ) +int item::ammo_consume( int qty, const tripoint &pos, player *carrier ) { if( qty < 0 ) { debugmsg( "Cannot consume negative quantity of ammo for %s", tname() ); return 0; } - + + int consumed = 0; + int temp_cons; + + // Consume charges loaded in the item or its magazines if( is_magazine() || contents.has_pocket_type( item_pocket::pocket_type::MAGAZINE_WELL ) ) { - return contents.ammo_consume( qty, pos ); - - } else if( is_tool() || is_gun() ) { - if( !contents.has_pocket_type( item_pocket::pocket_type::MAGAZINE ) || - ( is_tool() && type->tool->ammo_id.empty() ) ) { - qty = std::min( qty, charges ); - Character &player_character = get_player_character(); - if( has_flag( flag_USES_BIONIC_POWER ) ) { - charges = units::to_kilojoule( player_character.get_power_level() ); - player_character.mod_power_level( units::from_kilojoule( -qty ) ); - } - charges -= qty; - if( charges == 0 ) { - curammo = nullptr; - } - return qty; - } - } - - return 0; -} - -int item::electr_available( const player *carrier ) const -{ - if( has_flag( flag_USES_BIONIC_POWER ) ) { - int power = units::to_kilojoule( get_player_character().get_power_level() ); - return power; - } - - int res = 0; - const item *mag = magazine_current(); - if( mag ) { - res += mag->ammo_remaining(); - } - - if( !ammo_types().empty() ) { - for( const item *e : contents.all_items_top( item_pocket::pocket_type::MAGAZINE ) ) { - res += e->charges; - } - } - - if( carrier != nullptr && has_flag( flag_USE_UPS ) ) { - res += carrier->available_ups(); - } - return res; -} - -bool item::has_enough_electr( const player *carrier ) const -{ - if( !is_tool() || !ammo_required() ) { - return true; - } - - return electr_available( carrier ) >= ammo_required(); -} - -bool item::electr_consume( int qty, player *carrier ) -{ - if( electr_available( carrier ) < qty ) { - return false; - } - - // The tripoint in this call is used only for ejecting casings - // It should be safe to pass whatever to it. - qty -= ammo_consume( qty, tripoint_zero ); - - if( carrier != nullptr && has_flag( flag_USE_UPS ) ) { - carrier->consume_ups( qty ); - } - return true; + temp_cons = contents.ammo_consume( qty, pos ); + consumed += temp_cons; + qty -= temp_cons; + } + + // Consume UPS power from various sources + if( carrier != nullptr && has_flag( flag_USE_UPS ) ) { + int ups_used = std::min( carrier->available_ups(), qty ); + carrier->consume_ups( ups_used ); + consumed += ups_used; + qty -= ups_used; + } + + // Consume bio pwr directly + if( carrier != nullptr && has_flag( flag_USES_BIONIC_POWER ) ){ + int bio_used = std::min(units::to_kilojoule(carrier->get_power_level()), qty); + carrier->mod_power_level( units::from_kilojoule( bio_used ) ); + consumed += bio_used; + qty -= bio_used; + } + + return consumed; } const itype *item::ammo_data() const @@ -10458,13 +10413,7 @@ bool item::process_tool( player *carrier, const tripoint &pos ) energy += x_in_y( type->tool->power_draw % 1000000, 1000000 ) ? 1 : 0; } - if( ammo_types().count( ammo_battery ) ) { - int energy_consume = std::min( electr_available( carrier ), energy ); - energy -= energy_consume; - electr_consume( energy_consume, carrier ); - } else { - energy -= ammo_consume( energy, pos ); - } + energy -= ammo_consume( energy, pos ); avatar &player_character = get_avatar(); // if insufficient available charges shutdown the tool diff --git a/src/item.h b/src/item.h index 793b306ef8689..10e50d4835c19 100644 --- a/src/item.h +++ b/src/item.h @@ -1849,7 +1849,11 @@ class item : public visitable units::energy energy_remaining() const; /** Quantity of ammunition currently loaded in tool, gun or auxiliary gunmod */ - int ammo_remaining() const; + /** + * Quantity of ammunition currently loaded in tool, gun or auxiliary gunmod. Can include UPS and bionic + * @param carrier is used for UPS and bionic power + */ + int ammo_remaining( const player *carrier = nullptr ) const; /** * ammo capacity for a specific ammo */ @@ -1864,55 +1868,28 @@ class item : public visitable /** * Check if sufficient ammo is loaded for given number of uses. - * + * Also checks UPS and bionic if usable * Check if there is enough ammo loaded in a tool for the given number of uses * or given number of gun shots. Using this function for this check is preferred * because we expect to add support for items consuming multiple ammo types in * the future. Users of this function will not need to be refactored when this * happens. * - * @param[in] qty Number of uses + * @param carrier who holds the item. Needed for UPS/bionic only + * @param qty Number of uses * @returns true if ammo sufficient for number of uses is loaded, false otherwise */ - bool ammo_sufficient( int qty = 1 ) const; + bool ammo_sufficient( const player *carrier = nullptr, int qty = 1 ) const; /** * Consume ammo (if available) and return the amount of ammo that was consumed + * Consume order: Item, UPS, bionic * @param qty maximum amount of ammo that should be consumed * @param pos current location of item, used for ejecting magazines and similar effects + * @param carrier holder of the item, used for getting UPS and bionic power * @return amount of ammo consumed which will be between 0 and qty */ - int ammo_consume( int qty, const tripoint &pos ); - - /** - * How much electric energy is available. - * If the item has flag_USES_BIONIC_POWER then return only bionic power - * If the tool has battery loaded then that is included - * The check is dumb so don't call it on items that use other ammo - * If the tool has flag_USE_UPS then UPS sources are included - * @param carrier The current carrier - * @return amount of power (kJ) - */ - int electr_available( const player *carrier ) const; - - /** - * Check if the item has enough power available to be activated - * Checks all the appropriate power sources: battery, bionic, UPS - * The check is dumb so don't call it on items that use other ammo - * @param carrier The current carrier - * @return true if there is enough power - */ - bool has_enough_electr( const player *carrier ) const; - - /** - * Consume electric power (if available). If there is not enough power then none is drained and false is returned - * Consume order: Battery, UPS - * The code is dumb so don't call it on items that use other ammo - * @param qty maximum amount of electric power (kJ) that should be consumed - * @param carrier The current carrier - * @return false if there was not enough powr - */ - bool electr_consume( int qty, player *carrier ); + int ammo_consume( int qty, const tripoint &pos, player *carrier = nullptr ); /** Specific ammo data, returns nullptr if item is neither ammo nor loaded with any */ const itype *ammo_data() const; diff --git a/src/iuse_actor.cpp b/src/iuse_actor.cpp index 885dd6c1b7dd1..1d125942729f1 100644 --- a/src/iuse_actor.cpp +++ b/src/iuse_actor.cpp @@ -4346,7 +4346,7 @@ cata::optional sew_advanced_actor::use( player &p, item &it, bool, const tr return t; }; // Mod not already present, check if modification is possible - if( !it.ammo_sufficient( thread_needed ) ) { + if( !it.ammo_sufficient( &p, thread_needed ) ) { //~ %1$s: modification desc, %2$d: number of thread needed prompt = string_format( _( "Can't %1$s (need %2$d thread loaded)" ), tolower( obj.implement_prompt.translated() ), thread_needed ); From 52ba316240cddc73f57cd8ac25792e3b66869bab Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 22:32:57 +0300 Subject: [PATCH 25/87] astyle --- src/activity_item_handling.cpp | 2 +- src/avatar_action.cpp | 3 +- src/item.cpp | 78 +++++++++++++++++----------------- src/item.h | 8 ++-- 4 files changed, 46 insertions(+), 45 deletions(-) diff --git a/src/activity_item_handling.cpp b/src/activity_item_handling.cpp index 5d2b88886ebb5..a9a99ca7f0360 100644 --- a/src/activity_item_handling.cpp +++ b/src/activity_item_handling.cpp @@ -1136,7 +1136,7 @@ static activity_reason_info can_do_activity_there( const activity_id &act, playe } std::vector mining_inv = p.items_with( [&p]( const item & itm ) { return ( itm.has_flag( flag_DIG_TOOL ) && !itm.type->can_use( "JACKHAMMER" ) ) || - ( itm.type->can_use( "JACKHAMMER" ) && itm.ammo_sufficient(&p) ); + ( itm.type->can_use( "JACKHAMMER" ) && itm.ammo_sufficient( &p ) ); } ); if( mining_inv.empty() ) { return activity_reason_info::fail( do_activity_reason::NEEDS_MINING ); diff --git a/src/avatar_action.cpp b/src/avatar_action.cpp index daea57c70973f..fc1f90e743897 100644 --- a/src/avatar_action.cpp +++ b/src/avatar_action.cpp @@ -175,7 +175,8 @@ bool avatar_action::move( avatar &you, map &m, const tripoint &d ) !m.veh_at( dest_loc ) && !you.is_underwater() && !you.has_effect( effect_stunned ) && !is_riding && !you.has_effect( effect_incorporeal ) ) { if( you.weapon.has_flag( flag_DIG_TOOL ) ) { - if( you.weapon.type->can_use( "JACKHAMMER" ) && you.weapon.ammo_sufficient( &dynamic_cast( you ) ) ) { + if( you.weapon.type->can_use( "JACKHAMMER" ) && + you.weapon.ammo_sufficient( &dynamic_cast( you ) ) ) { you.invoke_item( &you.weapon, "JACKHAMMER", dest_loc ); // don't move into the tile until done mining you.defer_move( dest_loc ); diff --git a/src/item.cpp b/src/item.cpp index b4c12e0d1ee64..3782fdaf5fb4d 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -7934,46 +7934,46 @@ units::energy item::energy_remaining() const return 0_J; } -int item::ammo_remaining( const player *carrier) const +int item::ammo_remaining( const player *carrier ) const { - int ret = 0; + int ret = 0; const item *mag = magazine_current(); if( mag ) { ret += mag->ammo_remaining(); } - - if(carrier != nullptr && has_flag( flag_USES_BIONIC_POWER ) ) { - ret += units::to_kilojoule( carrier->get_power_level() ); + + if( carrier != nullptr && has_flag( flag_USES_BIONIC_POWER ) ) { + ret += units::to_kilojoule( carrier->get_power_level() ); } - - // Charges contained in the tool itself with "max_charges" - // Remove once no tool does this anymore + + // Charges contained in the tool itself with "max_charges" + // Remove once no tool does this anymore if( is_tool() && !ammo_types().empty() ) { - ret += charges; + ret += charges; } if( carrier != nullptr && has_flag( flag_USE_UPS ) ) { ret += carrier->available_ups(); } - + if( is_gun() && magazine_integral() && !contents.empty() ) { return contents.first_ammo().charges; } - if( is_magazine() ) { + if( is_magazine() ) { for( const item *e : contents.all_items_top( item_pocket::pocket_type::MAGAZINE ) ) { ret += e->charges; } } - - // Handle non-magazines with ammo_restriction in a CONTAINER type pocket (like quivers) + + // Handle non-magazines with ammo_restriction in a CONTAINER type pocket (like quivers) if( !ammo_types().empty() ) { for( const item *e : contents.all_items_top( item_pocket::pocket_type::CONTAINER ) ) { ret += e->charges; } } - + return ret; } @@ -8033,9 +8033,9 @@ int item::ammo_required() const return 0; } -bool item::ammo_sufficient(const player* carrier, int qty ) const +bool item::ammo_sufficient( const player *carrier, int qty ) const { - if( !ammo_required() ) { + if( !ammo_required() ) { return true; } return ammo_remaining( carrier ) >= ammo_required() * qty; @@ -8047,33 +8047,33 @@ int item::ammo_consume( int qty, const tripoint &pos, player *carrier ) debugmsg( "Cannot consume negative quantity of ammo for %s", tname() ); return 0; } - - int consumed = 0; + + int consumed = 0; int temp_cons; - - // Consume charges loaded in the item or its magazines + + // Consume charges loaded in the item or its magazines if( is_magazine() || contents.has_pocket_type( item_pocket::pocket_type::MAGAZINE_WELL ) ) { temp_cons = contents.ammo_consume( qty, pos ); - consumed += temp_cons; - qty -= temp_cons; - } - - // Consume UPS power from various sources - if( carrier != nullptr && has_flag( flag_USE_UPS ) ) { + consumed += temp_cons; + qty -= temp_cons; + } + + // Consume UPS power from various sources + if( carrier != nullptr && has_flag( flag_USE_UPS ) ) { int ups_used = std::min( carrier->available_ups(), qty ); - carrier->consume_ups( ups_used ); - consumed += ups_used; - qty -= ups_used; - } - - // Consume bio pwr directly - if( carrier != nullptr && has_flag( flag_USES_BIONIC_POWER ) ){ - int bio_used = std::min(units::to_kilojoule(carrier->get_power_level()), qty); - carrier->mod_power_level( units::from_kilojoule( bio_used ) ); - consumed += bio_used; - qty -= bio_used; - } - + carrier->consume_ups( ups_used ); + consumed += ups_used; + qty -= ups_used; + } + + // Consume bio pwr directly + if( carrier != nullptr && has_flag( flag_USES_BIONIC_POWER ) ) { + int bio_used = std::min( units::to_kilojoule( carrier->get_power_level() ), qty ); + carrier->mod_power_level( units::from_kilojoule( bio_used ) ); + consumed += bio_used; + qty -= bio_used; + } + return consumed; } diff --git a/src/item.h b/src/item.h index 10e50d4835c19..389195b6ffa1f 100644 --- a/src/item.h +++ b/src/item.h @@ -1849,7 +1849,7 @@ class item : public visitable units::energy energy_remaining() const; /** Quantity of ammunition currently loaded in tool, gun or auxiliary gunmod */ - /** + /** * Quantity of ammunition currently loaded in tool, gun or auxiliary gunmod. Can include UPS and bionic * @param carrier is used for UPS and bionic power */ @@ -1875,7 +1875,7 @@ class item : public visitable * the future. Users of this function will not need to be refactored when this * happens. * - * @param carrier who holds the item. Needed for UPS/bionic only + * @param carrier who holds the item. Needed for UPS/bionic only * @param qty Number of uses * @returns true if ammo sufficient for number of uses is loaded, false otherwise */ @@ -1883,10 +1883,10 @@ class item : public visitable /** * Consume ammo (if available) and return the amount of ammo that was consumed - * Consume order: Item, UPS, bionic + * Consume order: Item, UPS, bionic * @param qty maximum amount of ammo that should be consumed * @param pos current location of item, used for ejecting magazines and similar effects - * @param carrier holder of the item, used for getting UPS and bionic power + * @param carrier holder of the item, used for getting UPS and bionic power * @return amount of ammo consumed which will be between 0 and qty */ int ammo_consume( int qty, const tripoint &pos, player *carrier = nullptr ); From 6d2b4ac29277c1c8626e7cc83ec4c14a2055b0fb Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 23:20:35 +0300 Subject: [PATCH 26/87] ammo_consume( --- src/activity_handlers.cpp | 6 +++--- src/character.cpp | 14 +------------- src/item.cpp | 6 +++--- src/item.h | 2 +- src/item_contents.cpp | 2 +- src/iuse.cpp | 15 +++++++-------- src/monster.cpp | 4 ++-- src/npcmove.cpp | 2 +- src/ranged.cpp | 4 ++-- src/vehicle_part.cpp | 2 +- 10 files changed, 22 insertions(+), 35 deletions(-) diff --git a/src/activity_handlers.cpp b/src/activity_handlers.cpp index 25ad32ade1477..18aa2f51046b6 100644 --- a/src/activity_handlers.cpp +++ b/src/activity_handlers.cpp @@ -2231,7 +2231,7 @@ void activity_handlers::oxytorch_do_turn( player_activity *act, player *p ) // act->values[0] is the number of charges yet to be consumed const int charges_used = std::min( act->values[0], it.ammo_required() ); - it.ammo_consume( charges_used, p->pos() ); + it.ammo_consume( charges_used, p->pos(), p ); act->values[0] -= static_cast( charges_used ); sfx::play_activity_sound( "tool", "oxytorch", sfx::get_heard_volume( act->placement ) ); @@ -2248,7 +2248,7 @@ void activity_handlers::oxytorch_finish( player_activity *act, player *p ) const ter_id ter = here.ter( pos ); const furn_id furn = here.furn( pos ); // fast players might still have some charges left to be consumed - act->targets.front()->ammo_consume( act->values[0], p->pos() ); + act->targets.front()->ammo_consume( act->values[0], p->pos(), p ); if( furn == f_rack ) { here.furn_set( pos, f_null ); @@ -2475,7 +2475,7 @@ void activity_handlers::repair_item_finish( player_activity *act, player *p ) const repair_item_actor::attempt_hint attempt = actor->repair( *p, *used_tool, fix_location ); if( attempt != repair_item_actor::AS_CANT ) { if( ploc && ploc->where() == item_location::type::map ) { - used_tool->ammo_consume( used_tool->ammo_required(), ploc->position() ); + used_tool->ammo_consume( used_tool->ammo_required(), ploc->position(), p ); } else { p->consume_charges( *used_tool, used_tool->ammo_required() ); } diff --git a/src/character.cpp b/src/character.cpp index 418dbd99ae444..677d2f43c0d55 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -8966,19 +8966,7 @@ bool Character::consume_charges( item &used, int qty ) return true; } - // USE_UPS never occurs on base items but is instead added by the UPS tool mod - if( used.has_flag( flag_USE_UPS ) ) { - // With the new UPS system, we'll want to use any charges built up in the tool before pulling from the UPS - // The usage of the item was already approved, so drain item if possible, otherwise use UPS - if( used.charges >= qty || ( used.magazine_integral() && - !used.has_flag( flag_id( "USES_BIONIC_POWER" ) ) && used.ammo_remaining() >= qty ) ) { - used.ammo_consume( qty, pos() ); - } else { - use_charges( itype_UPS, qty ); - } - } else { - used.ammo_consume( std::min( qty, used.ammo_remaining() ), pos() ); - } + used.ammo_consume( std::min( qty, used.ammo_remaining() ), pos(), &dynamic_cast( *this ) ); return false; } diff --git a/src/item.cpp b/src/item.cpp index 3782fdaf5fb4d..2e48b0d71a6d3 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8689,7 +8689,7 @@ bool item::reload( Character &u, item_location ammo, int qty ) item ammo_copy( ammo->contents.first_ammo() ); ammo_copy.charges = qty; put_in( ammo_copy, item_pocket::pocket_type::MAGAZINE ); - ammo->ammo_consume( qty, tripoint_zero ); + ammo->ammo_consume( qty, tripoint_zero, &dynamic_cast(u)); } else if( ammo->ammo_type() == ammo_plutonium ) { curammo = ammo->type; ammo->charges -= qty; @@ -9305,7 +9305,7 @@ bool item::use_charges( const itype_id &what, int &qty, std::list &used, item temp( *e ); temp.ammo_set( e->ammo_current(), n ); used.push_back( temp ); - e->ammo_consume( n, pos ); + e->ammo_consume( n, pos, nullptr ); } } return VisitResponse::SKIP; @@ -10413,7 +10413,7 @@ bool item::process_tool( player *carrier, const tripoint &pos ) energy += x_in_y( type->tool->power_draw % 1000000, 1000000 ) ? 1 : 0; } - energy -= ammo_consume( energy, pos ); + energy -= ammo_consume( energy, pos, carrier ); avatar &player_character = get_avatar(); // if insufficient available charges shutdown the tool diff --git a/src/item.h b/src/item.h index 389195b6ffa1f..e8c852088dcf5 100644 --- a/src/item.h +++ b/src/item.h @@ -1889,7 +1889,7 @@ class item : public visitable * @param carrier holder of the item, used for getting UPS and bionic power * @return amount of ammo consumed which will be between 0 and qty */ - int ammo_consume( int qty, const tripoint &pos, player *carrier = nullptr ); + int ammo_consume( int qty, const tripoint &pos, player *carrier ); /** Specific ammo data, returns nullptr if item is neither ammo nor loaded with any */ const itype *ammo_data() const; diff --git a/src/item_contents.cpp b/src/item_contents.cpp index 4cbf0c9102270..9795404e5d558 100644 --- a/src/item_contents.cpp +++ b/src/item_contents.cpp @@ -724,7 +724,7 @@ int item_contents::ammo_consume( int qty, const tripoint &pos ) } // assuming only one mag item &mag = pocket.front(); - const int res = mag.ammo_consume( qty, pos ); + const int res = mag.ammo_consume( qty, pos, nullptr ); if( res && mag.ammo_remaining() == 0 ) { if( mag.has_flag( STATIC( flag_id( "MAG_DESTROY" ) ) ) ) { pocket.remove_item( mag ); diff --git a/src/iuse.cpp b/src/iuse.cpp index 12b28d9b6deb3..3d14ed3b562b6 100644 --- a/src/iuse.cpp +++ b/src/iuse.cpp @@ -1940,7 +1940,7 @@ cata::optional iuse::fish_trap( player *p, item *it, bool t, const tripoint } if( fishes == 0 ) { - it->ammo_consume( it->ammo_remaining(), pos ); + it->ammo_consume( it->ammo_remaining(), pos, p ); p->practice( skill_survival, rng( 5, 15 ) ); return 0; @@ -1981,7 +1981,7 @@ cata::optional iuse::fish_trap( player *p, item *it, bool t, const tripoint } } } - it->ammo_consume( bait_consumed, pos ); + it->ammo_consume( bait_consumed, pos, p ); } return 0; } @@ -3235,7 +3235,7 @@ static int toolweapon_on( player &p, item &it, const bool t, "_off"; if( t ) { // Effects while simply on if( double_charge_cost && it.units_sufficient( p ) ) { - it.ammo_consume( 1, p.pos() ); + it.ammo_consume( 1, p.pos(), &p ); } if( !works_underwater && p.is_underwater() ) { p.add_msg_if_player( _( "Your %s gurgles in the water and stops." ), tname ); @@ -4400,7 +4400,7 @@ cata::optional iuse::gasmask( player *p, item *it, bool t, const tripoint & } } if( it->get_var( "gas_absorbed", 0 ) >= 100 ) { - it->ammo_consume( 1, p->pos() ); + it->ammo_consume( 1, p->pos(), p ); it->set_var( "gas_absorbed", 0 ); } if( it->ammo_remaining() == 0 ) { @@ -6313,8 +6313,7 @@ cata::optional iuse::einktabletpc( player *p, item *it, bool t, const tripo ( it->ammo_remaining() > 0 || ( it->has_flag( flag_USE_UPS ) && p->has_enough_charges( *it, false ) ) ) ) { if( calendar::once_every( 5_minutes ) ) { - //it->ammo_consume( 1, p->pos() ); - p->consume_charges( *it, 1 ); + it->ammo_consume( 1, p->pos(), p ); } //the more varied music, the better max mood. @@ -8435,7 +8434,7 @@ cata::optional iuse::multicooker( player *p, item *it, bool t, const tripoi it->erase_var( "RECIPE" ); it->convert( itype_multi_cooker ); //drain the buffer amount given at activation - it->ammo_consume( charge_buffer, pos ); + it->ammo_consume( charge_buffer, pos, p ); p->add_msg_if_player( m_info, _( "Batteries low, entering standby mode. With a low buzzing sound the multi-cooker shuts down." ) ); return 0; @@ -8690,7 +8689,7 @@ cata::optional iuse::multicooker( player *p, item *it, bool t, const tripoi _( "The screen flashes blue symbols and scales as the multi-cooker begins to shake." ) ); it->convert( itype_multi_cooker_filled ).active = true; - it->ammo_consume( charges_to_start - charge_buffer, pos ); + it->ammo_consume( charges_to_start - charge_buffer, pos, p ); p->practice( skill_cooking, meal->difficulty * 3 ); //little bonus diff --git a/src/monster.cpp b/src/monster.cpp index 381a39eee428a..a48a0938273d7 100644 --- a/src/monster.cpp +++ b/src/monster.cpp @@ -205,7 +205,7 @@ monster::monster( const mtype_id &id ) : monster() const itype &type = *item::find_type( mech_bat ); int max_charge = type.magazine->capacity; item mech_bat_item = item( mech_bat, calendar::turn_zero ); - mech_bat_item.ammo_consume( rng( 0, max_charge ), tripoint_zero ); + mech_bat_item.ammo_consume( rng( 0, max_charge ), tripoint_zero, nullptr ); battery_item = cata::make_value( mech_bat_item ); } } @@ -2501,7 +2501,7 @@ bool monster::use_mech_power( int amt ) return false; } amt = -amt; - battery_item->ammo_consume( amt, pos() ); + battery_item->ammo_consume( amt, pos(), nullptr ); return battery_item->ammo_remaining() > 0; } diff --git a/src/npcmove.cpp b/src/npcmove.cpp index 4d50f555e519e..3bfa1761ae955 100644 --- a/src/npcmove.cpp +++ b/src/npcmove.cpp @@ -3627,7 +3627,7 @@ void npc::heal_self() } if( treatment != nullptr ) { treatment->get_use( iusage )->call( *this, *treatment, treatment->active, pos() ); - treatment->ammo_consume( treatment->ammo_required(), pos() ); + treatment->ammo_consume( treatment->ammo_required(), pos(), this ); return; } } diff --git a/src/ranged.cpp b/src/ranged.cpp index 7c38290cd06c3..b972f9fc2af77 100644 --- a/src/ranged.cpp +++ b/src/ranged.cpp @@ -701,7 +701,7 @@ void npc::pretend_fire( npc *source, int shots, item &gun ) } while( curshot != shots ) { const int required = gun.ammo_required(); - if( gun.ammo_consume( required, pos() ) != required ) { + if( gun.ammo_consume( required, pos(), this ) != required ) { debugmsg( "Unexpected shortage of ammo whilst firing %s", gun.tname().c_str() ); break; } @@ -808,7 +808,7 @@ int player::fire_gun( const tripoint &target, int shots, item &gun ) } const int required = gun.ammo_required(); - if( gun.ammo_consume( required, pos() ) != required ) { + if( gun.ammo_consume( required, pos(), this ) != required ) { debugmsg( "Unexpected shortage of ammo whilst firing %s", gun.tname() ); break; } diff --git a/src/vehicle_part.cpp b/src/vehicle_part.cpp index 9e60d904b71ee..f8269c835b31f 100644 --- a/src/vehicle_part.cpp +++ b/src/vehicle_part.cpp @@ -312,7 +312,7 @@ int vehicle_part::ammo_consume( int qty, const tripoint &pos ) } return res; } - return base.ammo_consume( qty, pos ); + return base.ammo_consume( qty, pos, nullptr ); } double vehicle_part::consume_energy( const itype_id &ftype, double energy_j ) From 34e0065570d0504207b33271b5b4f3b70410edae Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 20 Apr 2021 23:21:09 +0300 Subject: [PATCH 27/87] astyle --- src/character.cpp | 3 ++- src/item.cpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index 677d2f43c0d55..ceb9be9c2b9d1 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -8966,7 +8966,8 @@ bool Character::consume_charges( item &used, int qty ) return true; } - used.ammo_consume( std::min( qty, used.ammo_remaining() ), pos(), &dynamic_cast( *this ) ); + used.ammo_consume( std::min( qty, used.ammo_remaining() ), pos(), + &dynamic_cast( *this ) ); return false; } diff --git a/src/item.cpp b/src/item.cpp index 2e48b0d71a6d3..9517687d0481e 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8689,7 +8689,7 @@ bool item::reload( Character &u, item_location ammo, int qty ) item ammo_copy( ammo->contents.first_ammo() ); ammo_copy.charges = qty; put_in( ammo_copy, item_pocket::pocket_type::MAGAZINE ); - ammo->ammo_consume( qty, tripoint_zero, &dynamic_cast(u)); + ammo->ammo_consume( qty, tripoint_zero, &dynamic_cast( u ) ); } else if( ammo->ammo_type() == ammo_plutonium ) { curammo = ammo->type; ammo->charges -= qty; From b99d6d2c9903fd293f28ce0a5c6d6a340c88181c Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Wed, 21 Apr 2021 00:04:47 +0300 Subject: [PATCH 28/87] ammo_remaining( --- src/activity_actor.cpp | 6 +++--- src/activity_handlers.cpp | 9 +++------ src/activity_item_handling.cpp | 4 ++-- src/bionics.cpp | 2 +- src/character.cpp | 3 +-- src/item.cpp | 8 +++++--- src/iuse.cpp | 15 +++++---------- src/player.cpp | 13 ++++--------- 8 files changed, 24 insertions(+), 36 deletions(-) diff --git a/src/activity_actor.cpp b/src/activity_actor.cpp index d08ed4fa08712..4b3ce81ad13bc 100644 --- a/src/activity_actor.cpp +++ b/src/activity_actor.cpp @@ -164,7 +164,7 @@ void aim_activity_actor::do_turn( player_activity &act, Character &who ) } gun_mode gun = weapon->gun_current_mode(); - if( first_turn && gun->has_flag( flag_RELOAD_AND_SHOOT ) && !gun->ammo_remaining() ) { + if( first_turn && gun->has_flag( flag_RELOAD_AND_SHOOT ) && !gun->ammo_remaining( nullptr ) ) { if( !load_RAS_weapon() ) { aborted = true; act.moves_left = 0; @@ -632,7 +632,7 @@ bool gunmod_remove_activity_actor::gunmod_unload( Character &who, item &gunmod ) } // TODO: unloading gunmods happens instantaneously in some cases, but should take time item_location loc = item_location( who, &gunmod ); - return !( gunmod.ammo_remaining() && !who.as_player()->unload( loc, true ) ); + return !( gunmod.ammo_remaining( nullptr ) && !who.as_player()->unload( loc, true ) ); } void gunmod_remove_activity_actor::gunmod_remove( Character &who, item &gun, item &mod ) @@ -1745,7 +1745,7 @@ void unload_activity_actor::unload( Character &who, item_location &target ) who.add_msg_if_player( _( "You unload your %s." ), it.tname() ); } - if( it.has_flag( flag_MAG_DESTROY ) && it.ammo_remaining() == 0 ) { + if( it.has_flag( flag_MAG_DESTROY ) && it.ammo_remaining( nullptr ) == 0 ) { target.remove_item(); } } diff --git a/src/activity_handlers.cpp b/src/activity_handlers.cpp index 18aa2f51046b6..165f43fd281b8 100644 --- a/src/activity_handlers.cpp +++ b/src/activity_handlers.cpp @@ -2127,10 +2127,10 @@ void activity_handlers::vibe_do_turn( player_activity *act, player *p ) } if( calendar::once_every( 1_minutes ) ) { - if( vibrator_item.ammo_remaining() > 0 ) { + if( vibrator_item.ammo_remaining( p ) > 0 ) { vibrator_item.ammo_consume( 1, p->pos(), p ); p->add_morale( MORALE_FEELING_GOOD, 3, 40 ); - if( vibrator_item.ammo_remaining() == 0 ) { + if( vibrator_item.ammo_remaining( p ) == 0 ) { add_msg( m_info, _( "The %s runs out of batteries." ), vibrator_item.tname() ); } } else { @@ -2564,10 +2564,7 @@ void activity_handlers::repair_item_finish( player_activity *act, player *p ) ammo_name = item::nname( used_tool->ammo_current() ); } - int ammo_remaining = used_tool->ammo_remaining(); - if( used_tool->has_flag( flag_USE_UPS ) ) { - ammo_remaining = p->available_ups(); - } + int ammo_remaining = used_tool->ammo_remaining( p ); std::set valid_entries = actor->get_valid_repair_materials( fix ); const inventory &crafting_inv = p->crafting_inventory(); diff --git a/src/activity_item_handling.cpp b/src/activity_item_handling.cpp index a9a99ca7f0360..f520f93c8651a 100644 --- a/src/activity_item_handling.cpp +++ b/src/activity_item_handling.cpp @@ -1433,8 +1433,8 @@ static std::vector> requirements_map( player if( comp_elem.by_charges() ) { // we don't care if there are 10 welders with 5 charges each // we only want the one welder that has the required charge. - if( stack_elem.ammo_remaining() >= comp_elem.count ) { - temp_map[stack_elem.typeId()] += stack_elem.ammo_remaining(); + if( stack_elem.ammo_remaining( &p ) >= comp_elem.count ) { + temp_map[stack_elem.typeId()] += stack_elem.ammo_remaining( &p ); } } else { temp_map[stack_elem.typeId()] += stack_elem.count(); diff --git a/src/bionics.cpp b/src/bionics.cpp index 30c681ef7d29f..15679ab7d51d9 100644 --- a/src/bionics.cpp +++ b/src/bionics.cpp @@ -537,7 +537,7 @@ void npc::check_or_use_weapon_cbm( const bionic_id &cbm_id ) } const int ups_charges = available_ups(); - int ammo_count = weapon.ammo_remaining(); + int ammo_count = weapon.ammo_remaining( this ); const int ups_drain = weapon.get_gun_ups_drain(); if( ups_drain > 0 ) { ammo_count = std::min( ammo_count, ups_charges / ups_drain ); diff --git a/src/character.cpp b/src/character.cpp index ceb9be9c2b9d1..6da3395c6e00d 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -8966,8 +8966,7 @@ bool Character::consume_charges( item &used, int qty ) return true; } - used.ammo_consume( std::min( qty, used.ammo_remaining() ), pos(), - &dynamic_cast( *this ) ); + used.ammo_consume( std::min( qty, used.ammo_remaining() ), pos(), nullptr ); return false; } diff --git a/src/item.cpp b/src/item.cpp index 9517687d0481e..1e7a8fe4bdd2c 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8035,10 +8035,12 @@ int item::ammo_required() const bool item::ammo_sufficient( const player *carrier, int qty ) const { - if( !ammo_required() ) { - return true; + if( ammo_required() ) { + return ammo_remaining( carrier ) >= ammo_required() * qty; + } else if( get_gun_ups_drain() ) { + return ammo_remaining( carrier ) >= get_gun_ups_drain(); } - return ammo_remaining( carrier ) >= ammo_required() * qty; + } int item::ammo_consume( int qty, const tripoint &pos, player *carrier ) diff --git a/src/iuse.cpp b/src/iuse.cpp index 3d14ed3b562b6..efe832bade0b8 100644 --- a/src/iuse.cpp +++ b/src/iuse.cpp @@ -4096,8 +4096,7 @@ cata::optional iuse::tazer( player *p, item *it, bool, const tripoint &pos cata::optional iuse::tazer2( player *p, item *it, bool b, const tripoint &pos ) { - if( it->ammo_remaining() >= 100 || ( it->has_flag( flag_USE_UPS ) && - p->available_ups() >= 100 ) ) { + if( it->ammo_remaining( p ) >= 100 ) { // Instead of having a ctrl+c+v of the function above, spawn a fake tazer and use it // Ugly, but less so than copied blocks item fake( "tazer", calendar::turn_zero ); @@ -8375,8 +8374,7 @@ cata::optional iuse::autoclave( player *p, item *it, bool t, const tripoint //Using power_draw seem to consume random amount of battery so +100 to be safe static const int power_need = ( ( it->type->tool->power_draw / 1000 ) * to_seconds ( 90_minutes ) ) / 1000 + 100; - if( power_need > it->ammo_remaining() && !( it->has_flag( flag_USE_UPS ) && - p->available_ups() >= power_need ) ) { + if( power_need > it->ammo_remaining( p ) ) { popup( _( "The autoclave doesn't have enough battery for one cycle. You need at least %s charges." ), power_need ); return cata::nullopt; @@ -8428,8 +8426,7 @@ cata::optional iuse::multicooker( player *p, item *it, bool t, const tripoi if( t ) { //stop action before power runs out and iuse deletes the cooker - if( !( it->ammo_remaining() >= charge_buffer ) && !( it->has_flag( flag_USE_UPS ) && - p->available_ups() >= charge_buffer ) ) { + if( it->ammo_remaining( p ) < charge_buffer ) { it->active = false; it->erase_var( "RECIPE" ); it->convert( itype_multi_cooker ); @@ -8523,8 +8520,7 @@ cata::optional iuse::multicooker( player *p, item *it, bool t, const tripoi menu.addentry( mc_stop, true, 's', _( "Stop cooking" ) ); } else { if( dish_it == nullptr ) { - if( it->ammo_remaining() < charges_to_start && !( it->has_flag( flag_USE_UPS ) && - p->available_ups() >= charges_to_start ) ) { + if( it->ammo_remaining( p ) < charges_to_start ) { p->add_msg_if_player( _( "Batteries are low." ) ); return 0; } @@ -8660,8 +8656,7 @@ cata::optional iuse::multicooker( player *p, item *it, bool t, const tripoi const int all_charges = charges_to_start + ( ( mealtime / 100 ) * ( it->type->tool->power_draw / 1000 ) ) / 1000; - if( it->ammo_remaining() < all_charges && !( it->has_flag( flag_USE_UPS ) && - p->available_ups() >= all_charges ) ) { + if( it->ammo_remaining( p ) < all_charges ) { p->add_msg_if_player( m_warning, _( "The multi-cooker needs %d charges to cook this dish." ), diff --git a/src/player.cpp b/src/player.cpp index 7f03188b8d075..84d6c60ca7871 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -1164,7 +1164,6 @@ void player::process_items() } // Active item processing done, now we're recharging. - int ch_UPS = available_ups(); bool update_required = get_check_encumbrance(); for( item &w : worn ) { @@ -1177,6 +1176,7 @@ void player::process_items() calc_encumbrance(); set_check_encumbrance( false ); } + int ch_UPS = available_ups(); int ch_UPS_used = 0; // Load all items that use the UPS to their minimal functional charge, @@ -1190,19 +1190,14 @@ void player::process_items() // Bionic power costs are handled elsewhere continue; //this is for UPS-modded items with no battery well - } else if( it->active && !it->ammo_sufficient() && - ( ch_UPS_used >= ch_UPS || - it->ammo_required() > ch_UPS - ch_UPS_used ) ) { + } else if( it->active && !it->ammo_sufficient( this ) ) { it->deactivate(); - } else if( ch_UPS_used < ch_UPS && + } else if( available_ups() > 0 && it->ammo_remaining() < it->ammo_capacity( ammotype( "battery" ) ) ) { - ch_UPS_used++; + consume_ups( 1 ); it->ammo_set( itype_battery, it->ammo_remaining() + 1 ); } } - if( ch_UPS_used > 0 ) { - consume_ups( ch_UPS_used ); - } } bool player::add_faction_warning( const faction_id &id ) From 58a2a9bf82f5f8aaa0c924962cf6c1ad82c51e92 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Wed, 21 Apr 2021 01:52:51 +0300 Subject: [PATCH 29/87] fix ups weapon --- src/item.cpp | 10 ++++------ src/ranged.cpp | 4 ++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/item.cpp b/src/item.cpp index 1e7a8fe4bdd2c..74d42c3f4da95 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -7946,19 +7946,18 @@ int item::ammo_remaining( const player *carrier ) const ret += units::to_kilojoule( carrier->get_power_level() ); } - // Charges contained in the tool itself with "max_charges" - // Remove once no tool does this anymore + // This may not apply to anything (?) if( is_tool() && !ammo_types().empty() ) { ret += charges; } - if( carrier != nullptr && has_flag( flag_USE_UPS ) ) { + if( carrier != nullptr && ( has_flag( flag_USE_UPS ) || get_gun_ups_drain() ) ) { ret += carrier->available_ups(); } if( is_gun() && magazine_integral() && !contents.empty() ) { - return contents.first_ammo().charges; + ret += contents.first_ammo().charges; } if( is_magazine() ) { @@ -7973,9 +7972,7 @@ int item::ammo_remaining( const player *carrier ) const ret += e->charges; } } - return ret; - } int item::remaining_ammo_capacity() const @@ -8040,6 +8037,7 @@ bool item::ammo_sufficient( const player *carrier, int qty ) const } else if( get_gun_ups_drain() ) { return ammo_remaining( carrier ) >= get_gun_ups_drain(); } + return true; } diff --git a/src/ranged.cpp b/src/ranged.cpp index b972f9fc2af77..f1f8f02aa4374 100644 --- a/src/ranged.cpp +++ b/src/ranged.cpp @@ -3493,8 +3493,8 @@ bool gunmode_checks_weapon( avatar &you, const map &m, std::vector const gun_mode &gmode ) { bool result = true; - - if( !gmode->ammo_sufficient() && !gmode->has_flag( flag_RELOAD_AND_SHOOT ) ) { + if( !gmode->ammo_sufficient( &dynamic_cast( you ) ) && + !gmode->has_flag( flag_RELOAD_AND_SHOOT ) ) { if( !gmode->ammo_remaining() ) { messages.push_back( string_format( _( "Your %s is empty!" ), gmode->tname() ) ); } else { From e0e508624aba212ed725d83249062973369f6fab Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Wed, 21 Apr 2021 10:41:49 +0300 Subject: [PATCH 30/87] unused variables --- src/item.cpp | 1 - src/player.cpp | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/item.cpp b/src/item.cpp index 74d42c3f4da95..9b7edb846d7c1 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8071,7 +8071,6 @@ int item::ammo_consume( int qty, const tripoint &pos, player *carrier ) int bio_used = std::min( units::to_kilojoule( carrier->get_power_level() ), qty ); carrier->mod_power_level( units::from_kilojoule( bio_used ) ); consumed += bio_used; - qty -= bio_used; } return consumed; diff --git a/src/player.cpp b/src/player.cpp index 84d6c60ca7871..ad6896a5e1b4a 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -1176,8 +1176,6 @@ void player::process_items() calc_encumbrance(); set_check_encumbrance( false ); } - int ch_UPS = available_ups(); - int ch_UPS_used = 0; // Load all items that use the UPS to their minimal functional charge, // The tool is not really useful if its charges are below charges_to_use From 0cd8c18bf7d511dab46e729f99866dccdd004fff Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Wed, 21 Apr 2021 11:40:16 +0300 Subject: [PATCH 31/87] fix grenade --- src/item.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/item.cpp b/src/item.cpp index 9b7edb846d7c1..4c64847afc73b 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8071,8 +8071,16 @@ int item::ammo_consume( int qty, const tripoint &pos, player *carrier ) int bio_used = std::min( units::to_kilojoule( carrier->get_power_level() ), qty ); carrier->mod_power_level( units::from_kilojoule( bio_used ) ); consumed += bio_used; + qty -= bio_used; } + // Some weird internal non-item charges (used by grenades) + if( is_tool() && type->tool->ammo_id.empty() ) { + int charg_used = std::min( charges, qty ); + charges -= charg_used; + consumed += charg_used; + + } return consumed; } From f965076a24d830a43fb022fa29e094a5fbd47dfc Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Wed, 21 Apr 2021 11:42:24 +0300 Subject: [PATCH 32/87] wrong statement --- src/item.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/item.cpp b/src/item.cpp index 4c64847afc73b..5644e62c390cf 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -7946,8 +7946,8 @@ int item::ammo_remaining( const player *carrier ) const ret += units::to_kilojoule( carrier->get_power_level() ); } - // This may not apply to anything (?) - if( is_tool() && !ammo_types().empty() ) { + // Weird non-item charges. Not sure if used by anything + if( is_tool() && ammo_types().empty() ) { ret += charges; } @@ -8079,8 +8079,8 @@ int item::ammo_consume( int qty, const tripoint &pos, player *carrier ) int charg_used = std::min( charges, qty ); charges -= charg_used; consumed += charg_used; - } + return consumed; } From 0d04c099b4e4dbc1683e520ad4bc9d7454744148 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Wed, 21 Apr 2021 15:10:32 +0300 Subject: [PATCH 33/87] random fixes --- src/activity_actor.cpp | 6 +++--- src/activity_handlers.cpp | 4 +--- src/bionics.cpp | 3 +-- src/character.cpp | 2 +- src/item.cpp | 2 +- src/npc.cpp | 2 +- 6 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/activity_actor.cpp b/src/activity_actor.cpp index 4b3ce81ad13bc..d08ed4fa08712 100644 --- a/src/activity_actor.cpp +++ b/src/activity_actor.cpp @@ -164,7 +164,7 @@ void aim_activity_actor::do_turn( player_activity &act, Character &who ) } gun_mode gun = weapon->gun_current_mode(); - if( first_turn && gun->has_flag( flag_RELOAD_AND_SHOOT ) && !gun->ammo_remaining( nullptr ) ) { + if( first_turn && gun->has_flag( flag_RELOAD_AND_SHOOT ) && !gun->ammo_remaining() ) { if( !load_RAS_weapon() ) { aborted = true; act.moves_left = 0; @@ -632,7 +632,7 @@ bool gunmod_remove_activity_actor::gunmod_unload( Character &who, item &gunmod ) } // TODO: unloading gunmods happens instantaneously in some cases, but should take time item_location loc = item_location( who, &gunmod ); - return !( gunmod.ammo_remaining( nullptr ) && !who.as_player()->unload( loc, true ) ); + return !( gunmod.ammo_remaining() && !who.as_player()->unload( loc, true ) ); } void gunmod_remove_activity_actor::gunmod_remove( Character &who, item &gun, item &mod ) @@ -1745,7 +1745,7 @@ void unload_activity_actor::unload( Character &who, item_location &target ) who.add_msg_if_player( _( "You unload your %s." ), it.tname() ); } - if( it.has_flag( flag_MAG_DESTROY ) && it.ammo_remaining( nullptr ) == 0 ) { + if( it.has_flag( flag_MAG_DESTROY ) && it.ammo_remaining() == 0 ) { target.remove_item(); } } diff --git a/src/activity_handlers.cpp b/src/activity_handlers.cpp index 165f43fd281b8..c9565e771d901 100644 --- a/src/activity_handlers.cpp +++ b/src/activity_handlers.cpp @@ -1696,9 +1696,7 @@ void activity_handlers::generic_game_turn_handler( player_activity *act, player if( calendar::once_every( 1_minutes ) ) { if( !act->targets.empty() ) { item &game_item = *act->targets.front(); - const int ammo_required = game_item.ammo_required(); - // I am fairly sure this item is always battery operated - bool fail = game_item.ammo_consume( ammo_required, tripoint_zero, p ) == 0; + bool fail = game_item.ammo_consume( game_item.ammo_required(), tripoint_zero, p ) == 0; if( fail ) { act->moves_left = 0; add_msg( m_info, _( "The %s runs out of batteries." ), game_item.tname() ); diff --git a/src/bionics.cpp b/src/bionics.cpp index 15679ab7d51d9..01c3ab72e8e54 100644 --- a/src/bionics.cpp +++ b/src/bionics.cpp @@ -536,11 +536,10 @@ void npc::check_or_use_weapon_cbm( const bionic_id &cbm_id ) return; } - const int ups_charges = available_ups(); int ammo_count = weapon.ammo_remaining( this ); const int ups_drain = weapon.get_gun_ups_drain(); if( ups_drain > 0 ) { - ammo_count = std::min( ammo_count, ups_charges / ups_drain ); + ammo_count = ammo_count / ups_drain; } const int cbm_ammo = free_power / bio.info().power_activate; diff --git a/src/character.cpp b/src/character.cpp index 6da3395c6e00d..7846f5ff7c502 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -8966,7 +8966,7 @@ bool Character::consume_charges( item &used, int qty ) return true; } - used.ammo_consume( std::min( qty, used.ammo_remaining() ), pos(), nullptr ); + used.ammo_consume( qty, pos(), nullptr ); return false; } diff --git a/src/item.cpp b/src/item.cpp index 5644e62c390cf..2f5adf502e2b0 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8035,7 +8035,7 @@ bool item::ammo_sufficient( const player *carrier, int qty ) const if( ammo_required() ) { return ammo_remaining( carrier ) >= ammo_required() * qty; } else if( get_gun_ups_drain() ) { - return ammo_remaining( carrier ) >= get_gun_ups_drain(); + return ammo_remaining( carrier ) >= get_gun_ups_drain() * qty; } return true; diff --git a/src/npc.cpp b/src/npc.cpp index 3a8bf8af97faa..d586ebf2040ad 100644 --- a/src/npc.cpp +++ b/src/npc.cpp @@ -1531,7 +1531,7 @@ void npc::decide_needs() if( weapon.is_gun() ) { int ups_drain = weapon.get_gun_ups_drain(); if( ups_drain > 0 ) { - int ups_charges = 2 * available_ups(); + int ups_charges = available_ups(); needrank[need_ammo] = static_cast( ups_charges ) / ups_drain; } else { const ammotype ammo_type = weapon.ammo_type(); From 21836866df160159481f7f79e61651773cd8eac7 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Wed, 21 Apr 2021 16:07:17 +0300 Subject: [PATCH 34/87] fix ammo count --- src/item.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/item.cpp b/src/item.cpp index 2f5adf502e2b0..4f7f44dc6804f 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -7937,29 +7937,29 @@ units::energy item::energy_remaining() const int item::ammo_remaining( const player *carrier ) const { int ret = 0; + + // Magagzine in the item const item *mag = magazine_current(); if( mag ) { ret += mag->ammo_remaining(); } + // Power from bionic if( carrier != nullptr && has_flag( flag_USES_BIONIC_POWER ) ) { ret += units::to_kilojoule( carrier->get_power_level() ); } - // Weird non-item charges. Not sure if used by anything + // Weird non-item charges. Not sure if used by anything in this context if( is_tool() && ammo_types().empty() ) { ret += charges; } + // Extra power from UPS if( carrier != nullptr && ( has_flag( flag_USE_UPS ) || get_gun_ups_drain() ) ) { ret += carrier->available_ups(); } - - if( is_gun() && magazine_integral() && !contents.empty() ) { - ret += contents.first_ammo().charges; - } - + // Magazines and integral magazines on their own if( is_magazine() ) { for( const item *e : contents.all_items_top( item_pocket::pocket_type::MAGAZINE ) ) { ret += e->charges; From 60b66bfa4d821f1bfea7c65efc0b5e3c41c80c1f Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Wed, 21 Apr 2021 19:20:57 +0300 Subject: [PATCH 35/87] misc adjustments --- src/character.cpp | 8 ++++++-- src/character.h | 14 +++++++++----- src/item.cpp | 29 ++++++++++------------------- src/item.h | 12 +++++++----- 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index 7846f5ff7c502..24b25b486fe8f 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -11309,7 +11309,7 @@ int Character::available_ups() const auto *mons = mounted_creature.get(); available_charges += mons->battery_item->ammo_remaining(); } - if( has_power() && has_active_bionic( bio_ups ) ) { + if( has_active_bionic( bio_ups ) ) { available_charges += units::to_kilojoule( get_power_level() ); } available_charges += charges_of( itype_UPS_off ); @@ -11318,8 +11318,10 @@ int Character::available_ups() const return available_charges; } -void Character::consume_ups( int qty ) +int Character::consume_ups( int qty ) { + const int wanted_qty = qty; + if( qty != 0 && is_mounted() && mounted_creature.get()->has_flag( MF_RIDEABLE_MECH ) && mounted_creature.get()->battery_item ) { auto *mons = mounted_creature.get(); @@ -11345,7 +11347,9 @@ void Character::consume_ups( int qty ) int ups = inv.charges_of( itype_UPS_off, qty ); if( qty != 0 && ups > 0 ) { use_charges( itype_UPS_off, ups ); + qty -= ups; } + return wanted_qty - qty; } std::list Character::use_charges( const itype_id &what, int qty, const int radius, diff --git a/src/character.h b/src/character.h index 75ba502a8639e..16e45509a732c 100644 --- a/src/character.h +++ b/src/character.h @@ -2054,17 +2054,21 @@ class Character : public Creature, public visitable // Uses up charges bool use_charges_if_avail( const itype_id &it, int quantity ); - // Returns the amount of UPS charges available - // Sum of mech, bionic UPS, adv UPS and UPS - // Adv UPS includes efficiency bonus + /** + * Available ups from all sources + * Sum of mech, bionic UPS, adv UPS and UPS + * Adv UPS includes efficiency bonus + * @return amount of UPS available + */ int available_ups() const; + /** * Consume UPS charges. * Consume order: mech, Bionic UPS, adv UPS, UPS. - * Does not check if there is enough power so check available_ups() first * @param qty Number of charges (kJ) + * @return amount of UPS consumed which will be between 0 and qty */ - void consume_ups( int qty ); + int consume_ups( int qty ); /** * Use charges in character inventory. diff --git a/src/item.cpp b/src/item.cpp index 4f7f44dc6804f..710eb92db452f 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8047,41 +8047,32 @@ int item::ammo_consume( int qty, const tripoint &pos, player *carrier ) debugmsg( "Cannot consume negative quantity of ammo for %s", tname() ); return 0; } - - int consumed = 0; - int temp_cons; + const int wanted_qty = qty; // Consume charges loaded in the item or its magazines if( is_magazine() || contents.has_pocket_type( item_pocket::pocket_type::MAGAZINE_WELL ) ) { - temp_cons = contents.ammo_consume( qty, pos ); - consumed += temp_cons; - qty -= temp_cons; + qty -= contents.ammo_consume( qty, pos ); + } + + // Some weird internal non-item charges (used by grenades) + if( is_tool() && type->tool->ammo_id.empty() ) { + int charg_used = std::min( charges, qty ); + charges -= charg_used; } // Consume UPS power from various sources if( carrier != nullptr && has_flag( flag_USE_UPS ) ) { - int ups_used = std::min( carrier->available_ups(), qty ); - carrier->consume_ups( ups_used ); - consumed += ups_used; - qty -= ups_used; + qty -= carrier->consume_ups( qty ); } // Consume bio pwr directly if( carrier != nullptr && has_flag( flag_USES_BIONIC_POWER ) ) { int bio_used = std::min( units::to_kilojoule( carrier->get_power_level() ), qty ); carrier->mod_power_level( units::from_kilojoule( bio_used ) ); - consumed += bio_used; qty -= bio_used; } - // Some weird internal non-item charges (used by grenades) - if( is_tool() && type->tool->ammo_id.empty() ) { - int charg_used = std::min( charges, qty ); - charges -= charg_used; - consumed += charg_used; - } - - return consumed; + return wanted_qty - qty; } const itype *item::ammo_data() const diff --git a/src/item.h b/src/item.h index e8c852088dcf5..8261388a09164 100644 --- a/src/item.h +++ b/src/item.h @@ -1850,8 +1850,9 @@ class item : public visitable /** Quantity of ammunition currently loaded in tool, gun or auxiliary gunmod */ /** - * Quantity of ammunition currently loaded in tool, gun or auxiliary gunmod. Can include UPS and bionic - * @param carrier is used for UPS and bionic power + * Quantity of ammunition currently loaded in tool, gun or auxiliary gunmod. Can include UPS and bionic + * If UPS/bionic power does not matter then the carrier can be nullptr + * @param carrier is used for UPS and bionic power */ int ammo_remaining( const player *carrier = nullptr ) const; /** @@ -1868,9 +1869,10 @@ class item : public visitable /** * Check if sufficient ammo is loaded for given number of uses. - * Also checks UPS and bionic if usable * Check if there is enough ammo loaded in a tool for the given number of uses - * or given number of gun shots. Using this function for this check is preferred + * or given number of gun shots. + * If carrier is provides then UPS and bionic may be also used as ammo + * Using this function for this check is preferred * because we expect to add support for items consuming multiple ammo types in * the future. Users of this function will not need to be refactored when this * happens. @@ -1883,7 +1885,7 @@ class item : public visitable /** * Consume ammo (if available) and return the amount of ammo that was consumed - * Consume order: Item, UPS, bionic + * Consume order: loaded items, UPS, bionic * @param qty maximum amount of ammo that should be consumed * @param pos current location of item, used for ejecting magazines and similar effects * @param carrier holder of the item, used for getting UPS and bionic power From 70d042c97a109b5eeac5242368025500bb8a77bc Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Wed, 21 Apr 2021 19:50:25 +0300 Subject: [PATCH 36/87] fix grenade (again) --- src/item.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/item.cpp b/src/item.cpp index 710eb92db452f..f8310a2a60a21 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8058,6 +8058,7 @@ int item::ammo_consume( int qty, const tripoint &pos, player *carrier ) if( is_tool() && type->tool->ammo_id.empty() ) { int charg_used = std::min( charges, qty ); charges -= charg_used; + qty -= charg_used; } // Consume UPS power from various sources From 0faffab911eca7b3b4fef09a26db3ccaf7f5d497 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Wed, 21 Apr 2021 23:41:08 +0300 Subject: [PATCH 37/87] Use Character --- src/item.cpp | 8 ++++---- src/item.h | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/item.cpp b/src/item.cpp index f8310a2a60a21..31a342753f302 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -7934,7 +7934,7 @@ units::energy item::energy_remaining() const return 0_J; } -int item::ammo_remaining( const player *carrier ) const +int item::ammo_remaining( const Character *carrier ) const { int ret = 0; @@ -8030,7 +8030,7 @@ int item::ammo_required() const return 0; } -bool item::ammo_sufficient( const player *carrier, int qty ) const +bool item::ammo_sufficient( const Character *carrier, int qty ) const { if( ammo_required() ) { return ammo_remaining( carrier ) >= ammo_required() * qty; @@ -8041,7 +8041,7 @@ bool item::ammo_sufficient( const player *carrier, int qty ) const } -int item::ammo_consume( int qty, const tripoint &pos, player *carrier ) +int item::ammo_consume( int qty, const tripoint &pos, Character *carrier ) { if( qty < 0 ) { debugmsg( "Cannot consume negative quantity of ammo for %s", tname() ); @@ -8688,7 +8688,7 @@ bool item::reload( Character &u, item_location ammo, int qty ) item ammo_copy( ammo->contents.first_ammo() ); ammo_copy.charges = qty; put_in( ammo_copy, item_pocket::pocket_type::MAGAZINE ); - ammo->ammo_consume( qty, tripoint_zero, &dynamic_cast( u ) ); + ammo->ammo_consume( qty, tripoint_zero, &u ); } else if( ammo->ammo_type() == ammo_plutonium ) { curammo = ammo->type; ammo->charges -= qty; diff --git a/src/item.h b/src/item.h index 8261388a09164..b907f624567e4 100644 --- a/src/item.h +++ b/src/item.h @@ -1854,7 +1854,7 @@ class item : public visitable * If UPS/bionic power does not matter then the carrier can be nullptr * @param carrier is used for UPS and bionic power */ - int ammo_remaining( const player *carrier = nullptr ) const; + int ammo_remaining( const Character *carrier = nullptr ) const; /** * ammo capacity for a specific ammo */ @@ -1881,7 +1881,7 @@ class item : public visitable * @param qty Number of uses * @returns true if ammo sufficient for number of uses is loaded, false otherwise */ - bool ammo_sufficient( const player *carrier = nullptr, int qty = 1 ) const; + bool ammo_sufficient( const Character *carrier = nullptr, int qty = 1 ) const; /** * Consume ammo (if available) and return the amount of ammo that was consumed @@ -1891,7 +1891,7 @@ class item : public visitable * @param carrier holder of the item, used for getting UPS and bionic power * @return amount of ammo consumed which will be between 0 and qty */ - int ammo_consume( int qty, const tripoint &pos, player *carrier ); + int ammo_consume( int qty, const tripoint &pos, Character *carrier ); /** Specific ammo data, returns nullptr if item is neither ammo nor loaded with any */ const itype *ammo_data() const; From 499b1680ab8df323e051f0a5f9ccb86dae1af3ad Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Wed, 21 Apr 2021 23:54:20 +0300 Subject: [PATCH 38/87] electr firestarter --- src/iuse_actor.cpp | 2 +- src/ranged.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/iuse_actor.cpp b/src/iuse_actor.cpp index 1d125942729f1..e5d1252b2fe52 100644 --- a/src/iuse_actor.cpp +++ b/src/iuse_actor.cpp @@ -1241,7 +1241,7 @@ ret_val firestarter_actor::can_use( const Character &p, const item &it, bo return ret_val::make_failure( _( "You can't do that while underwater." ) ); } - if( it.ammo_remaining() < it.ammo_required() ) { + if( !it.ammo_sufficient( &p ) ) { return ret_val::make_failure( _( "This tool doesn't have enough charges." ) ); } diff --git a/src/ranged.cpp b/src/ranged.cpp index f1f8f02aa4374..a5070ba144ab6 100644 --- a/src/ranged.cpp +++ b/src/ranged.cpp @@ -3493,7 +3493,7 @@ bool gunmode_checks_weapon( avatar &you, const map &m, std::vector const gun_mode &gmode ) { bool result = true; - if( !gmode->ammo_sufficient( &dynamic_cast( you ) ) && + if( !gmode->ammo_sufficient( &you ) && !gmode->has_flag( flag_RELOAD_AND_SHOOT ) ) { if( !gmode->ammo_remaining() ) { messages.push_back( string_format( _( "Your %s is empty!" ), gmode->tname() ) ); From 11fd91a0a8964fcbe16efd90b556d3d37aeb20ee Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Thu, 22 Apr 2021 00:36:56 +0300 Subject: [PATCH 39/87] electr firestarter --- src/activity_handlers.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/activity_handlers.cpp b/src/activity_handlers.cpp index c9565e771d901..4248afeac3a48 100644 --- a/src/activity_handlers.cpp +++ b/src/activity_handlers.cpp @@ -1915,9 +1915,8 @@ void activity_handlers::start_fire_finish( player_activity *act, player *p ) return; } - if( it.type->can_have_charges() ) { - p->consume_charges( it, it.type->charges_to_use() ); - } + it.ammo_consume( it.type->charges_to_use(), tripoint_zero, p ); + p->practice( skill_survival, act->index, 5 ); firestarter_actor::resolve_firestarter_use( *p, act->placement ); From 5d41aafc92a924e41d8bed9070a28e2b59e4f21e Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Thu, 22 Apr 2021 00:51:45 +0300 Subject: [PATCH 40/87] astyle --- src/activity_handlers.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/activity_handlers.cpp b/src/activity_handlers.cpp index 4248afeac3a48..26bbe8ee7e840 100644 --- a/src/activity_handlers.cpp +++ b/src/activity_handlers.cpp @@ -1915,7 +1915,7 @@ void activity_handlers::start_fire_finish( player_activity *act, player *p ) return; } - it.ammo_consume( it.type->charges_to_use(), tripoint_zero, p ); + it.ammo_consume( it.type->charges_to_use(), tripoint_zero, p ); p->practice( skill_survival, act->index, 5 ); From d855061ff76a5670d82341e7c7f413227ad37645 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Thu, 22 Apr 2021 10:24:39 +0300 Subject: [PATCH 41/87] revert jackhammer --- data/json/items/tool/workshop.json | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/data/json/items/tool/workshop.json b/data/json/items/tool/workshop.json index 180caf5da56f7..a0d8acac22e75 100644 --- a/data/json/items/tool/workshop.json +++ b/data/json/items/tool/workshop.json @@ -325,7 +325,7 @@ "type": "TOOL", "name": { "str": "electric jackhammer" }, "description": "This is a construction tool for drilling through hard rock or other surfaces. It runs on a cell compatible with UPS. Use it to blast a hole in adjacent solid terrain.", - "weight": "5000 g", + "weight": "40000 g", "volume": "5 L", "longest_side": "71 cm", "price": 40000, @@ -337,16 +337,9 @@ "symbol": ";", "color": "light_gray", "ammo": [ "battery" ], + "initial_charges": 3500, + "max_charges": 7000, "charges_per_use": 3500, - "pocket_data": [ - { - "pocket_type": "MAGAZINE_WELL", - "rigid": true, - "max_contains_volume": "20 L", - "max_contains_weight": "40 kg", - "item_restriction": [ "medium_storage_battery" ] - } - ], "use_action": [ "JACKHAMMER" ], "flags": [ "STAB", "DIG_TOOL", "POWERED", "USE_UPS", "NO_UNLOAD", "NO_RELOAD", "WATER_BREAK" ] }, From a18673803a6d2f922f804cd62c4c06740239d650 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Thu, 22 Apr 2021 14:27:12 +0300 Subject: [PATCH 42/87] seems like dead code --- src/character.cpp | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index 24b25b486fe8f..1b4b98203b11c 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -11406,16 +11406,6 @@ std::list Character::use_charges( const itype_id &what, int qty, const int std::vector del; - bool has_tool_with_UPS = false; - // Detection of UPS tool - inv.visit_items( [ &what, &qty, &has_tool_with_UPS, &filter]( item * e, item * ) { - if( filter( *e ) && e->typeId() == what && e->has_flag( flag_USE_UPS ) ) { - has_tool_with_UPS = true; - return VisitResponse::ABORT; - } - return qty > 0 ? VisitResponse::NEXT : VisitResponse::ABORT; - } ); - if( radius >= 0 ) { get_map().use_charges( pos(), radius, what, qty, return_true ); } @@ -11432,10 +11422,6 @@ std::list Character::use_charges( const itype_id &what, int qty, const int remove_item( *e ); } - if( has_tool_with_UPS ) { - use_charges( itype_UPS, qty, radius ); - } - return res; } From f0be8678ea663a3fe5c787bc2b8b7fab9abcee91 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Thu, 22 Apr 2021 14:28:05 +0300 Subject: [PATCH 43/87] not used anymore --- src/character.cpp | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index 1b4b98203b11c..597b7224a3354 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -11372,36 +11372,6 @@ std::list Character::use_charges( const itype_id &what, int qty, const int if( what == itype_fire ) { use_fire( qty ); return res; - - } else if( what == itype_UPS ) { - if( is_mounted() && mounted_creature.get()->has_flag( MF_RIDEABLE_MECH ) && - mounted_creature.get()->battery_item ) { - auto *mons = mounted_creature.get(); - int power_drain = std::min( mons->battery_item->ammo_remaining(), qty ); - mons->use_mech_power( -power_drain ); - qty -= std::min( qty, power_drain ); - return res; - } - if( has_power() && has_active_bionic( bio_ups ) ) { - int bio = std::min( units::to_kilojoule( get_power_level() ), qty ); - mod_power_level( units::from_kilojoule( -bio ) ); - qty -= std::min( qty, bio ); - } - - int adv = inv.charges_of( itype_adv_UPS_off, static_cast( std::ceil( qty * 0.6 ) ) ); - if( adv > 0 ) { - std::list found = use_charges( itype_adv_UPS_off, adv, radius ); - res.splice( res.end(), found ); - qty -= std::min( qty, static_cast( adv / 0.6 ) ); - } - - int ups = inv.charges_of( itype_UPS_off, qty ); - if( ups > 0 ) { - std::list found = use_charges( itype_UPS_off, ups, radius ); - res.splice( res.end(), found ); - qty -= std::min( qty, ups ); - } - return res; } std::vector del; From 45dae0f075bd0fd10b7ba2841752516102ed8008 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Thu, 22 Apr 2021 14:41:05 +0300 Subject: [PATCH 44/87] unused ups code --- src/bionics.cpp | 1 - src/character.cpp | 8 ++------ src/iuse.cpp | 1 - 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/bionics.cpp b/src/bionics.cpp index 01c3ab72e8e54..6f781eba23de0 100644 --- a/src/bionics.cpp +++ b/src/bionics.cpp @@ -133,7 +133,6 @@ static const itype_id itype_adv_UPS_off( "adv_UPS_off" ); static const itype_id itype_anesthetic( "anesthetic" ); static const itype_id itype_radiocontrol( "radiocontrol" ); static const itype_id itype_remotevehcontrol( "remotevehcontrol" ); -static const itype_id itype_UPS( "UPS" ); static const itype_id itype_UPS_off( "UPS_off" ); static const itype_id itype_water_clean( "water_clean" ); diff --git a/src/character.cpp b/src/character.cpp index 597b7224a3354..b24413a42b509 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -225,6 +225,7 @@ static const itype_id itype_rm13_armor_on( "rm13_armor_on" ); static const itype_id itype_rope_6( "rope_6" ); static const itype_id itype_snare_trigger( "snare_trigger" ); static const itype_id itype_string_36( "string_36" ); +static const itype_id itype_toolset( "toolset" ); static const itype_id itype_UPS( "UPS" ); static const itype_id itype_UPS_off( "UPS_off" ); @@ -8900,7 +8901,7 @@ bool Character::has_enough_charges( const item &it, bool show_msg ) const return true; } if( it.has_flag( flag_USE_UPS ) ) { - if( has_charges( itype_UPS, it.ammo_required() ) || it.ammo_sufficient() ) { + if( it.ammo_sufficient() ) { return true; } if( show_msg ) { @@ -11262,11 +11263,6 @@ bool Character::has_charges( const itype_id &it, int quantity, if( it == itype_fire || it == itype_apparatus ) { return has_fire( quantity ); } - if( it == itype_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; } diff --git a/src/iuse.cpp b/src/iuse.cpp index efe832bade0b8..1d3457463d1c2 100644 --- a/src/iuse.cpp +++ b/src/iuse.cpp @@ -226,7 +226,6 @@ static const efftype_id effect_webbed( "webbed" ); static const efftype_id effect_weed_high( "weed_high" ); static const itype_id itype_adv_UPS_off( "adv_UPS_off" ); -static const itype_id itype_UPS( "UPS" ); static const itype_id itype_advanced_ecig( "advanced_ecig" ); static const itype_id itype_afs_atomic_smartphone( "afs_atomic_smartphone" ); static const itype_id itype_afs_atomic_smartphone_music( "afs_atomic_smartphone_music" ); From e0f0dc1a6681bfc9c8e5edf279eac78e41870236 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Thu, 22 Apr 2021 14:51:39 +0300 Subject: [PATCH 45/87] simplify check --- src/character.cpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index b24413a42b509..ce6f2fe21f85a 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -8900,20 +8900,17 @@ bool Character::has_enough_charges( const item &it, bool show_msg ) const if( !it.is_tool() || !it.ammo_required() ) { return true; } - if( it.has_flag( flag_USE_UPS ) ) { - if( it.ammo_sufficient() ) { - return true; - } - if( show_msg ) { + + if( it.ammo_sufficient( this ) ) { + return true; + } else { + if( show_msg && it.has_flag( flag_USE_UPS ) ) { add_msg_if_player( m_info, ngettext( "Your %s needs %d charge from some UPS.", "Your %s needs %d charges from some UPS.", it.ammo_required() ), it.tname(), it.ammo_required() ); - } - return false; - } else if( !it.ammo_sufficient() ) { - if( show_msg ) { + } else { add_msg_if_player( m_info, ngettext( "Your %s has %d charge, but needs %d.", "Your %s has %d charges, but needs %d.", @@ -8922,6 +8919,7 @@ bool Character::has_enough_charges( const item &it, bool show_msg ) const } return false; } + return true; } From 9a58010bd904888c6060d6fe478d329bba7d6918 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Thu, 22 Apr 2021 14:54:09 +0300 Subject: [PATCH 46/87] small miss --- src/character.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/character.cpp b/src/character.cpp index ce6f2fe21f85a..66422b08d3cbe 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -8910,7 +8910,7 @@ bool Character::has_enough_charges( const item &it, bool show_msg ) const "Your %s needs %d charges from some UPS.", it.ammo_required() ), it.tname(), it.ammo_required() ); - } else { + } else if( show_msg ) { add_msg_if_player( m_info, ngettext( "Your %s has %d charge, but needs %d.", "Your %s has %d charges, but needs %d.", From d5534f89ffd445255047c77b91228c53caa4d0c3 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Thu, 22 Apr 2021 15:39:08 +0300 Subject: [PATCH 47/87] fix crafting with ups --- src/character.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/character.cpp b/src/character.cpp index 66422b08d3cbe..ac09177dffec4 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -11366,10 +11366,50 @@ std::list Character::use_charges( const itype_id &what, int qty, const int if( what == itype_fire ) { use_fire( qty ); return res; + + } else if( what == itype_UPS ) { + if( is_mounted() && mounted_creature.get()->has_flag( MF_RIDEABLE_MECH ) && + mounted_creature.get()->battery_item ) { + auto *mons = mounted_creature.get(); + int power_drain = std::min( mons->battery_item->ammo_remaining(), qty ); + mons->use_mech_power( -power_drain ); + qty -= std::min( qty, power_drain ); + return res; + } + if( has_power() && has_active_bionic( bio_ups ) ) { + int bio = std::min( units::to_kilojoule( get_power_level() ), qty ); + mod_power_level( units::from_kilojoule( -bio ) ); + qty -= std::min( qty, bio ); + } + + int adv = inv.charges_of( itype_adv_UPS_off, static_cast( std::ceil( qty * 0.6 ) ) ); + if( adv > 0 ) { + std::list found = use_charges( itype_adv_UPS_off, adv, radius ); + res.splice( res.end(), found ); + qty -= std::min( qty, static_cast( adv / 0.6 ) ); + } + + int ups = inv.charges_of( itype_UPS_off, qty ); + if( ups > 0 ) { + std::list found = use_charges( itype_UPS_off, ups, radius ); + res.splice( res.end(), found ); + qty -= std::min( qty, ups ); + } + return res; } std::vector del; + bool has_tool_with_UPS = false; + // Detection of UPS tool + inv.visit_items( [ &what, &qty, &has_tool_with_UPS, &filter]( item * e, item * ) { + if( filter( *e ) && e->typeId() == what && e->has_flag( flag_USE_UPS ) ) { + has_tool_with_UPS = true; + return VisitResponse::ABORT; + } + return qty > 0 ? VisitResponse::NEXT : VisitResponse::ABORT; + } ); + if( radius >= 0 ) { get_map().use_charges( pos(), radius, what, qty, return_true ); } @@ -11386,6 +11426,10 @@ std::list Character::use_charges( const itype_id &what, int qty, const int remove_item( *e ); } + if( has_tool_with_UPS ) { + use_charges( itype_UPS, qty, radius ); + } + return res; } From 75ea210fd66ff9ce6f52cf6db8fb2aba0b480e6a Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Thu, 22 Apr 2021 18:45:11 +0300 Subject: [PATCH 48/87] crafting ups use --- src/character.cpp | 45 +++++++++++++-------------------------------- src/character.h | 2 +- 2 files changed, 14 insertions(+), 33 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index ac09177dffec4..e71974ce6bf24 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -11312,7 +11312,7 @@ int Character::available_ups() const return available_charges; } -int Character::consume_ups( int qty ) +int Character::consume_ups( int qty, const int radius ) { const int wanted_qty = qty; @@ -11330,19 +11330,25 @@ int Character::consume_ups( int qty ) qty -= std::min( qty, bio ); } - inventory inv = crafting_inventory( tripoint_zero, -1 ); + inventory inv = crafting_inventory( pos(), radius, true ); int adv = inv.charges_of( itype_adv_UPS_off, static_cast( std::ceil( qty * 0.6 ) ) ); if( qty != 0 && adv > 0 ) { - use_charges( itype_adv_UPS_off, adv ); + use_charges( itype_adv_UPS_off, adv, radius ); qty -= std::min( qty, static_cast( adv / 0.6 ) ); } int ups = inv.charges_of( itype_UPS_off, qty ); if( qty != 0 && ups > 0 ) { - use_charges( itype_UPS_off, ups ); + use_charges( itype_UPS_off, ups, radius ); qty -= ups; } + + if( radius > 0 ) { + get_map().use_charges( pos(), radius, itype_adv_UPS_off, qty, return_true ); + get_map().use_charges( pos(), radius, itype_UPS_off, qty, return_true ); + } + return wanted_qty - qty; } @@ -11368,33 +11374,8 @@ std::list Character::use_charges( const itype_id &what, int qty, const int return res; } else if( what == itype_UPS ) { - if( is_mounted() && mounted_creature.get()->has_flag( MF_RIDEABLE_MECH ) && - mounted_creature.get()->battery_item ) { - auto *mons = mounted_creature.get(); - int power_drain = std::min( mons->battery_item->ammo_remaining(), qty ); - mons->use_mech_power( -power_drain ); - qty -= std::min( qty, power_drain ); - return res; - } - if( has_power() && has_active_bionic( bio_ups ) ) { - int bio = std::min( units::to_kilojoule( get_power_level() ), qty ); - mod_power_level( units::from_kilojoule( -bio ) ); - qty -= std::min( qty, bio ); - } - - int adv = inv.charges_of( itype_adv_UPS_off, static_cast( std::ceil( qty * 0.6 ) ) ); - if( adv > 0 ) { - std::list found = use_charges( itype_adv_UPS_off, adv, radius ); - res.splice( res.end(), found ); - qty -= std::min( qty, static_cast( adv / 0.6 ) ); - } - - int ups = inv.charges_of( itype_UPS_off, qty ); - if( ups > 0 ) { - std::list found = use_charges( itype_UPS_off, ups, radius ); - res.splice( res.end(), found ); - qty -= std::min( qty, ups ); - } + debugmsg( _( "This UPS use needs updating. Create issue on github." ) ); + consume_ups( qty, radius ); return res; } @@ -11427,7 +11408,7 @@ std::list Character::use_charges( const itype_id &what, int qty, const int } if( has_tool_with_UPS ) { - use_charges( itype_UPS, qty, radius ); + consume_ups( qty, radius ); } return res; diff --git a/src/character.h b/src/character.h index 16e45509a732c..b122f7ba97458 100644 --- a/src/character.h +++ b/src/character.h @@ -2068,7 +2068,7 @@ class Character : public Creature, public visitable * @param qty Number of charges (kJ) * @return amount of UPS consumed which will be between 0 and qty */ - int consume_ups( int qty ); + int consume_ups( int qty, const int radius = -1 ); /** * Use charges in character inventory. From b03ae525fea13e366fca3aa99adaa2a4274db9d7 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Thu, 22 Apr 2021 21:14:17 +0300 Subject: [PATCH 49/87] cleaner --- src/character.cpp | 5 ----- src/character.h | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index e71974ce6bf24..5fd7e31e991cd 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -11344,11 +11344,6 @@ int Character::consume_ups( int qty, const int radius ) qty -= ups; } - if( radius > 0 ) { - get_map().use_charges( pos(), radius, itype_adv_UPS_off, qty, return_true ); - get_map().use_charges( pos(), radius, itype_UPS_off, qty, return_true ); - } - return wanted_qty - qty; } diff --git a/src/character.h b/src/character.h index b122f7ba97458..c114459e70a8f 100644 --- a/src/character.h +++ b/src/character.h @@ -2068,7 +2068,7 @@ class Character : public Creature, public visitable * @param qty Number of charges (kJ) * @return amount of UPS consumed which will be between 0 and qty */ - int consume_ups( int qty, const int radius = -1 ); + int consume_ups( int qty, int radius = -1 ); /** * Use charges in character inventory. From 815bef1d0dd485307913b212a9caae803c0ec30e Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Thu, 22 Apr 2021 22:21:16 +0300 Subject: [PATCH 50/87] minimal improvements --- src/avatar_action.cpp | 2 +- src/character.cpp | 5 +++-- src/visitable.cpp | 2 ++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/avatar_action.cpp b/src/avatar_action.cpp index fc1f90e743897..5bdc43fb33c53 100644 --- a/src/avatar_action.cpp +++ b/src/avatar_action.cpp @@ -176,7 +176,7 @@ bool avatar_action::move( avatar &you, map &m, const tripoint &d ) !is_riding && !you.has_effect( effect_incorporeal ) ) { if( you.weapon.has_flag( flag_DIG_TOOL ) ) { if( you.weapon.type->can_use( "JACKHAMMER" ) && - you.weapon.ammo_sufficient( &dynamic_cast( you ) ) ) { + you.weapon.ammo_sufficient( &you ) ) { you.invoke_item( &you.weapon, "JACKHAMMER", dest_loc ); // don't move into the tile until done mining you.defer_move( dest_loc ); diff --git a/src/character.cpp b/src/character.cpp index 5fd7e31e991cd..ea01326184b68 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -216,7 +216,6 @@ static const efftype_id effect_winded( "winded" ); static const field_type_str_id field_fd_clairvoyant( "fd_clairvoyant" ); -static const itype_id itype_adv_UPS_off( "adv_UPS_off" ); static const itype_id itype_apparatus( "apparatus" ); static const itype_id itype_beartrap( "beartrap" ); static const itype_id itype_e_handcuffs( "e_handcuffs" ); @@ -226,8 +225,9 @@ static const itype_id itype_rope_6( "rope_6" ); static const itype_id itype_snare_trigger( "snare_trigger" ); static const itype_id itype_string_36( "string_36" ); static const itype_id itype_toolset( "toolset" ); -static const itype_id itype_UPS( "UPS" ); static const itype_id itype_UPS_off( "UPS_off" ); +static const itype_id itype_adv_UPS_off( "adv_UPS_off" ); +static const itype_id itype_UPS( "UPS" ); static const skill_id skill_archery( "archery" ); static const skill_id skill_dodge( "dodge" ); @@ -11369,6 +11369,7 @@ std::list Character::use_charges( const itype_id &what, int qty, const int return res; } else if( what == itype_UPS ) { + // Fairly sure that nothing comes here. But handle it anyways. debugmsg( _( "This UPS use needs updating. Create issue on github." ) ); consume_ups( qty, radius ); return res; diff --git a/src/visitable.cpp b/src/visitable.cpp index eb72a0c763ca8..dac50f032ab5e 100644 --- a/src/visitable.cpp +++ b/src/visitable.cpp @@ -796,6 +796,7 @@ int inventory::charges_of( const itype_id &what, int limit, const std::function &visitor ) const { if( what == itype_UPS ) { + // Nothing should come here. But until all the charges_of and charges_of_internal no longer use itype_UPS I don't feel safe removing this int qty = 0; qty = sum_no_wrap( qty, charges_of( itype_UPS_off ) ); qty = sum_no_wrap( qty, static_cast( charges_of( itype_adv_UPS_off ) / 0.6 ) ); @@ -832,6 +833,7 @@ int Character::charges_of( const itype_id &what, int limit, } if( what == itype_UPS ) { + // Nothing should come here. But until all the charges_of and charges_of_internal no longer use itype_UPS I don't feel safe removing this int qty = 0; qty = sum_no_wrap( qty, charges_of( itype_UPS_off ) ); qty = sum_no_wrap( qty, static_cast( charges_of( itype_adv_UPS_off ) / 0.6 ) ); From e8938d642021b0030ded9f391b8a2600f0be7691 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Thu, 22 Apr 2021 22:37:12 +0300 Subject: [PATCH 51/87] code that shouldn't run gives debug msg if it is spotted --- src/player.cpp | 6 +++++- src/visitable.cpp | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/player.cpp b/src/player.cpp index ad6896a5e1b4a..da8a3856c0c15 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -1179,6 +1179,7 @@ void player::process_items() // Load all items that use the UPS to their minimal functional charge, // The tool is not really useful if its charges are below charges_to_use + int ups_used = 0; const auto inv_use_ups = items_with( []( const item & itm ) { return itm.has_flag( flag_USE_UPS ); } ); @@ -1192,10 +1193,13 @@ void player::process_items() it->deactivate(); } else if( available_ups() > 0 && it->ammo_remaining() < it->ammo_capacity( ammotype( "battery" ) ) ) { - consume_ups( 1 ); + ups_used++; it->ammo_set( itype_battery, it->ammo_remaining() + 1 ); } } + if( ups_used > 0 ) { + consume_ups( ups_used ); + } } bool player::add_faction_warning( const faction_id &id ) diff --git a/src/visitable.cpp b/src/visitable.cpp index dac50f032ab5e..25cb33e530b9d 100644 --- a/src/visitable.cpp +++ b/src/visitable.cpp @@ -797,6 +797,7 @@ int inventory::charges_of( const itype_id &what, int limit, { if( what == itype_UPS ) { // Nothing should come here. But until all the charges_of and charges_of_internal no longer use itype_UPS I don't feel safe removing this + debugmsg( _( "Please report how your UPS led you to inventory::charges_of" ) ); int qty = 0; qty = sum_no_wrap( qty, charges_of( itype_UPS_off ) ); qty = sum_no_wrap( qty, static_cast( charges_of( itype_adv_UPS_off ) / 0.6 ) ); @@ -834,6 +835,7 @@ int Character::charges_of( const itype_id &what, int limit, if( what == itype_UPS ) { // Nothing should come here. But until all the charges_of and charges_of_internal no longer use itype_UPS I don't feel safe removing this + debugmsg( _( "Please report how your UPS led you to Character::charges_of" ) ); int qty = 0; qty = sum_no_wrap( qty, charges_of( itype_UPS_off ) ); qty = sum_no_wrap( qty, static_cast( charges_of( itype_adv_UPS_off ) / 0.6 ) ); From 752accf4d26d6c0cfeecefabead6ece457339338 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Fri, 23 Apr 2021 13:07:48 +0300 Subject: [PATCH 52/87] minor comment --- src/character.cpp | 2 +- src/visitable.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/character.cpp b/src/character.cpp index ea01326184b68..9ad817cc60188 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -11370,7 +11370,7 @@ std::list Character::use_charges( const itype_id &what, int qty, const int } else if( what == itype_UPS ) { // Fairly sure that nothing comes here. But handle it anyways. - debugmsg( _( "This UPS use needs updating. Create issue on github." ) ); + debugmsg( _( "This UPS use needs updating. Create issue on github." ) ); consume_ups( qty, radius ); return res; } diff --git a/src/visitable.cpp b/src/visitable.cpp index 25cb33e530b9d..c3b5fcc083714 100644 --- a/src/visitable.cpp +++ b/src/visitable.cpp @@ -780,6 +780,7 @@ int read_only_visitable::charges_of( const itype_id &what, int limit, const std::function &filter, const std::function &visitor ) const { + // Crafting still uses this part. if( what == itype_UPS ) { int qty = 0; qty = sum_no_wrap( qty, charges_of( itype_UPS_off ) ); From a32d59b3110eaecdd5b19a5c60e0dd27ef692832 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Fri, 23 Apr 2021 14:04:25 +0300 Subject: [PATCH 53/87] these parts are still used --- src/visitable.cpp | 6 ++---- tests/crafting_test.cpp | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/visitable.cpp b/src/visitable.cpp index c3b5fcc083714..cbe334d1993eb 100644 --- a/src/visitable.cpp +++ b/src/visitable.cpp @@ -797,8 +797,7 @@ int inventory::charges_of( const itype_id &what, int limit, const std::function &visitor ) const { if( what == itype_UPS ) { - // Nothing should come here. But until all the charges_of and charges_of_internal no longer use itype_UPS I don't feel safe removing this - debugmsg( _( "Please report how your UPS led you to inventory::charges_of" ) ); + // Crafting still uses this part. int qty = 0; qty = sum_no_wrap( qty, charges_of( itype_UPS_off ) ); qty = sum_no_wrap( qty, static_cast( charges_of( itype_adv_UPS_off ) / 0.6 ) ); @@ -835,8 +834,7 @@ int Character::charges_of( const itype_id &what, int limit, } if( what == itype_UPS ) { - // Nothing should come here. But until all the charges_of and charges_of_internal no longer use itype_UPS I don't feel safe removing this - debugmsg( _( "Please report how your UPS led you to Character::charges_of" ) ); + // Crafting still uses this part int qty = 0; qty = sum_no_wrap( qty, charges_of( itype_UPS_off ) ); qty = sum_no_wrap( qty, static_cast( charges_of( itype_adv_UPS_off ) / 0.6 ) ); diff --git a/tests/crafting_test.cpp b/tests/crafting_test.cpp index 5bb9203b9a07c..fb815f4559a61 100644 --- a/tests/crafting_test.cpp +++ b/tests/crafting_test.cpp @@ -408,7 +408,7 @@ TEST_CASE( "UPS shows as a crafting component", "[crafting][ups]" ) REQUIRE( dummy.has_item( ups ) ); REQUIRE( ups.charges == 500 ); REQUIRE( dummy.charges_of( itype_id( "UPS_off" ) ) == 500 ); - REQUIRE( dummy.charges_of( itype_id( "UPS" ) ) == 500 ); + REQUIRE( dummy.available_ups() == 500 ); } TEST_CASE( "tools use charge to craft", "[crafting][charge]" ) From c9d7e75b6534a59471741479549486c518da231b Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Fri, 23 Apr 2021 14:31:57 +0300 Subject: [PATCH 54/87] duplicate check --- tests/crafting_test.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/crafting_test.cpp b/tests/crafting_test.cpp index fb815f4559a61..8b6a97cc10506 100644 --- a/tests/crafting_test.cpp +++ b/tests/crafting_test.cpp @@ -407,7 +407,6 @@ TEST_CASE( "UPS shows as a crafting component", "[crafting][ups]" ) item &ups = dummy.i_add( item( "UPS_off", calendar::turn_zero, 500 ) ); REQUIRE( dummy.has_item( ups ) ); REQUIRE( ups.charges == 500 ); - REQUIRE( dummy.charges_of( itype_id( "UPS_off" ) ) == 500 ); REQUIRE( dummy.available_ups() == 500 ); } From 65a43343e4f6cfb5724d01971942f72b8e60e087 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Sun, 25 Apr 2021 15:25:01 +0300 Subject: [PATCH 55/87] available_ups() --- src/character.cpp | 10 ++++++++-- src/visitable.cpp | 1 - 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index 9ad817cc60188..7e6b10f77da24 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -11306,8 +11306,14 @@ int Character::available_ups() const if( has_active_bionic( bio_ups ) ) { available_charges += units::to_kilojoule( get_power_level() ); } - available_charges += charges_of( itype_UPS_off ); - available_charges += charges_of( itype_adv_UPS_off ); + + for( const item *i : all_items_with_flag( flag_IS_UPS ) ) { + if( i->typeId() == itype_adv_UPS_off ) { + available_charges += i->ammo_remaining() / 0.6; + } else { + available_charges += i->ammo_remaining(); + } + } return available_charges; } diff --git a/src/visitable.cpp b/src/visitable.cpp index cbe334d1993eb..9a7d4d1fc6df5 100644 --- a/src/visitable.cpp +++ b/src/visitable.cpp @@ -834,7 +834,6 @@ int Character::charges_of( const itype_id &what, int limit, } if( what == itype_UPS ) { - // Crafting still uses this part int qty = 0; qty = sum_no_wrap( qty, charges_of( itype_UPS_off ) ); qty = sum_no_wrap( qty, static_cast( charges_of( itype_adv_UPS_off ) / 0.6 ) ); From 7119ffdab0a2f15be5e98a9e96b2fafd7df90c74 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Sun, 25 Apr 2021 15:55:42 +0300 Subject: [PATCH 56/87] consume_ups faster --- src/character.cpp | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index 7e6b10f77da24..a2b50ddca4a41 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -11336,18 +11336,38 @@ int Character::consume_ups( int qty, const int radius ) qty -= std::min( qty, bio ); } - inventory inv = crafting_inventory( pos(), radius, true ); + if( radius <= 0 ) { + // Faster method that only cares about carried items + std::vector ups_items = all_items_with_flag( flag_IS_UPS ); - int adv = inv.charges_of( itype_adv_UPS_off, static_cast( std::ceil( qty * 0.6 ) ) ); - if( qty != 0 && adv > 0 ) { - use_charges( itype_adv_UPS_off, adv, radius ); - qty -= std::min( qty, static_cast( adv / 0.6 ) ); - } + // Consume adv UPS first + for( const item *i : ups_items ) { + if( i->typeId() == itype_adv_UPS_off ) { + qty -= const_cast( i )->ammo_consume( qty * 0.6, tripoint_zero, nullptr ) / 0.6; - int ups = inv.charges_of( itype_UPS_off, qty ); - if( qty != 0 && ups > 0 ) { - use_charges( itype_UPS_off, ups, radius ); - qty -= ups; + } + } + // Consume normal UPS + for( const item *i : ups_items ) { + if( i->typeId() != itype_adv_UPS_off ) { + qty -= const_cast( i )->ammo_consume( qty, tripoint_zero, nullptr ); + } + } + } else { + // Slower method for all nearby items (used while crafting) + inventory inv = crafting_inventory( pos(), radius, true ); + + int adv = inv.charges_of( itype_adv_UPS_off, static_cast( std::ceil( qty * 0.6 ) ) ); + if( qty != 0 && adv > 0 ) { + use_charges( itype_adv_UPS_off, adv, radius ); + qty -= std::min( qty, static_cast( adv / 0.6 ) ); + } + + int ups = inv.charges_of( itype_UPS_off, qty ); + if( qty != 0 && ups > 0 ) { + use_charges( itype_UPS_off, ups, radius ); + qty -= ups; + } } return wanted_qty - qty; From 95d3fb51c8cc8b855c5eee002465516f54c0bc0c Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Sun, 25 Apr 2021 16:23:26 +0300 Subject: [PATCH 57/87] just call available_ups --- src/visitable.cpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/visitable.cpp b/src/visitable.cpp index 9a7d4d1fc6df5..30ec416a94447 100644 --- a/src/visitable.cpp +++ b/src/visitable.cpp @@ -834,13 +834,7 @@ int Character::charges_of( const itype_id &what, int limit, } if( what == itype_UPS ) { - int qty = 0; - qty = sum_no_wrap( qty, charges_of( itype_UPS_off ) ); - qty = sum_no_wrap( qty, static_cast( charges_of( itype_adv_UPS_off ) / 0.6 ) ); - if( p && p->has_active_bionic( bio_ups ) ) { - qty = sum_no_wrap( qty, units::to_kilojoule( p->get_power_level() ) ); - } - return std::min( qty, limit ); + return std::min( available_ups(), limit ); } return charges_of_internal( *this, *this, what, limit, filter, visitor ); From 3ebbbbe428aa72d115a03ee6bf66bceb09637ce9 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Sat, 1 May 2021 15:25:26 +0300 Subject: [PATCH 58/87] prevent overdrain --- src/character.cpp | 1 - src/player.cpp | 35 +++++++++++++++++++---------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index a2b50ddca4a41..3f9a9e726cd69 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -8919,7 +8919,6 @@ bool Character::has_enough_charges( const item &it, bool show_msg ) const } return false; } - return true; } diff --git a/src/player.cpp b/src/player.cpp index da8a3856c0c15..e2396b66295f3 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -1179,26 +1179,29 @@ void player::process_items() // Load all items that use the UPS to their minimal functional charge, // The tool is not really useful if its charges are below charges_to_use - int ups_used = 0; const auto inv_use_ups = items_with( []( const item & itm ) { return itm.has_flag( flag_USE_UPS ); } ); - for( const auto &it : inv_use_ups ) { - // For powered armor, an armor-powering bionic should always be preferred over UPS usage. - if( it->is_power_armor() && can_interface_armor() && has_power() ) { - // Bionic power costs are handled elsewhere - continue; - //this is for UPS-modded items with no battery well - } else if( it->active && !it->ammo_sufficient( this ) ) { - it->deactivate(); - } else if( available_ups() > 0 && - it->ammo_remaining() < it->ammo_capacity( ammotype( "battery" ) ) ) { - ups_used++; - it->ammo_set( itype_battery, it->ammo_remaining() + 1 ); + if( !inv_use_ups.empty() ) { + const int available_charges = available_ups(); + int ups_used = 0; + for( const auto &it : inv_use_ups ) { + // For powered armor, an armor-powering bionic should always be preferred over UPS usage. + if( it->is_power_armor() && can_interface_armor() && has_power() ) { + // Bionic power costs are handled elsewhere + continue; + } else if( it->active && !it->ammo_sufficient( this ) ) { + it->deactivate(); + } else if( ups_used < available_charges && + it->ammo_remaining() < it->ammo_capacity( ammotype( "battery" ) ) ) { + // Charge the battery in the UPS modded tool + ups_used++; + it->ammo_set( itype_battery, it->ammo_remaining() + 1 ); + } + } + if( ups_used > 0 ) { + consume_ups( ups_used ); } - } - if( ups_used > 0 ) { - consume_ups( ups_used ); } } From 70de4d26a98559eb3959c0d5e70ba4cc2167a00d Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Mon, 3 May 2021 12:05:46 +0300 Subject: [PATCH 59/87] large UPS rewrites --- src/bionics.cpp | 35 +++++++++++++---------------------- src/character.cpp | 22 ++++------------------ src/item.h | 5 ++++- src/iuse.cpp | 7 ++----- src/player.cpp | 1 - src/ranged.cpp | 1 - src/visitable.cpp | 40 +++++++++++++++++++++------------------- src/visitable.h | 1 + 8 files changed, 45 insertions(+), 67 deletions(-) diff --git a/src/bionics.cpp b/src/bionics.cpp index 6f781eba23de0..deb66497e1734 100644 --- a/src/bionics.cpp +++ b/src/bionics.cpp @@ -129,11 +129,9 @@ static const material_id fuel_type_muscle( "muscle" ); static const material_id fuel_type_sun_light( "sunlight" ); static const material_id fuel_type_wind( "wind" ); -static const itype_id itype_adv_UPS_off( "adv_UPS_off" ); static const itype_id itype_anesthetic( "anesthetic" ); static const itype_id itype_radiocontrol( "radiocontrol" ); static const itype_id itype_remotevehcontrol( "remotevehcontrol" ); -static const itype_id itype_UPS_off( "UPS_off" ); static const itype_id itype_water_clean( "water_clean" ); static const fault_id fault_bionic_salvaged( "fault_bionic_salvaged" ); @@ -1505,19 +1503,15 @@ material_id Character::find_remote_fuel( bool look_only ) } if( cable->get_var( "state" ) == "UPS_link" ) { - static const item_filter used_ups = [&]( const item & itm ) { - return itm.get_var( "cable" ) == "plugged_in"; - }; if( !look_only ) { - if( has_charges( itype_UPS_off, 1, used_ups ) ) { - set_value( "rem_battery", std::to_string( charges_of( itype_UPS_off, - units::to_kilojoule( max_power_level ), used_ups ) ) ); - } else if( has_charges( itype_adv_UPS_off, 1, used_ups ) ) { - set_value( "rem_battery", std::to_string( charges_of( itype_adv_UPS_off, - units::to_kilojoule( max_power_level ), used_ups ) ) ); - } else { - set_value( "rem_battery", std::to_string( 0 ) ); + int remote_battery = 0; + for( const item *i : all_items_with_flag( flag_IS_UPS ) ) { + if( i->get_var( "cable" ) == "plugged_in" ) { + remote_battery = i->ammo_remaining(); + } } + remote_battery = std::min( remote_battery, units::to_kilojoule( max_power_level ) ); + set_value( "rem_battery", std::to_string( remote_battery ) ); } remote_fuel = fuel_type_battery; } @@ -1557,15 +1551,12 @@ int Character::consume_remote_fuel( int amount ) } if( unconsumed_amount > 0 ) { - static const item_filter used_ups = [&]( const item & itm ) { - return itm.get_var( "cable" ) == "plugged_in"; - }; - if( has_charges( itype_UPS_off, unconsumed_amount, used_ups ) ) { - use_charges( itype_UPS_off, unconsumed_amount, used_ups ); - unconsumed_amount -= 1; - } else if( has_charges( itype_adv_UPS_off, unconsumed_amount, used_ups ) ) { - use_charges( itype_adv_UPS_off, roll_remainder( unconsumed_amount * 0.6 ), used_ups ); - unconsumed_amount -= 1; + for( const item *i : all_items_with_flag( flag_IS_UPS ) ) { + if( i->get_var( "cable" ) == "plugged_in" ) { + unconsumed_amount -= const_cast( i )->ammo_consume( unconsumed_amount, tripoint_zero, + nullptr ); + } + break; } } diff --git a/src/character.cpp b/src/character.cpp index 3f9a9e726cd69..1384d74ad4f70 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -11307,11 +11307,7 @@ int Character::available_ups() const } for( const item *i : all_items_with_flag( flag_IS_UPS ) ) { - if( i->typeId() == itype_adv_UPS_off ) { - available_charges += i->ammo_remaining() / 0.6; - } else { - available_charges += i->ammo_remaining(); - } + available_charges += i->ammo_remaining(); } return available_charges; @@ -11339,27 +11335,17 @@ int Character::consume_ups( int qty, const int radius ) // Faster method that only cares about carried items std::vector ups_items = all_items_with_flag( flag_IS_UPS ); - // Consume adv UPS first for( const item *i : ups_items ) { - if( i->typeId() == itype_adv_UPS_off ) { - qty -= const_cast( i )->ammo_consume( qty * 0.6, tripoint_zero, nullptr ) / 0.6; - - } - } - // Consume normal UPS - for( const item *i : ups_items ) { - if( i->typeId() != itype_adv_UPS_off ) { - qty -= const_cast( i )->ammo_consume( qty, tripoint_zero, nullptr ); - } + qty -= const_cast( i )->ammo_consume( qty, tripoint_zero, nullptr ); } } else { // Slower method for all nearby items (used while crafting) inventory inv = crafting_inventory( pos(), radius, true ); - int adv = inv.charges_of( itype_adv_UPS_off, static_cast( std::ceil( qty * 0.6 ) ) ); + int adv = inv.charges_of( itype_adv_UPS_off, qty ); if( qty != 0 && adv > 0 ) { use_charges( itype_adv_UPS_off, adv, radius ); - qty -= std::min( qty, static_cast( adv / 0.6 ) ); + qty -= std::min( qty, adv ); } int ups = inv.charges_of( itype_UPS_off, qty ); diff --git a/src/item.h b/src/item.h index b907f624567e4..fdb952c58ba25 100644 --- a/src/item.h +++ b/src/item.h @@ -1848,22 +1848,25 @@ class item : public visitable /** Quantity of energy currently loaded in tool or battery */ units::energy energy_remaining() const; - /** Quantity of ammunition currently loaded in tool, gun or auxiliary gunmod */ /** * Quantity of ammunition currently loaded in tool, gun or auxiliary gunmod. Can include UPS and bionic * If UPS/bionic power does not matter then the carrier can be nullptr * @param carrier is used for UPS and bionic power */ + int ammo_remaining( const Character *carrier = nullptr ) const; /** * ammo capacity for a specific ammo */ + int ammo_capacity( const ammotype &ammo ) const; /** * how much more ammo can fit into this item * if this item is not loaded, gives remaining capacity of its default ammo */ + int remaining_ammo_capacity() const; + /** Quantity of ammunition consumed per usage of tool or with each shot of gun */ int ammo_required() const; diff --git a/src/iuse.cpp b/src/iuse.cpp index 1d3457463d1c2..8ca653eab7d3d 100644 --- a/src/iuse.cpp +++ b/src/iuse.cpp @@ -225,7 +225,6 @@ static const efftype_id effect_weak_antibiotic_visible( "weak_antibiotic_visible static const efftype_id effect_webbed( "webbed" ); static const efftype_id effect_weed_high( "weed_high" ); -static const itype_id itype_adv_UPS_off( "adv_UPS_off" ); static const itype_id itype_advanced_ecig( "advanced_ecig" ); static const itype_id itype_afs_atomic_smartphone( "afs_atomic_smartphone" ); static const itype_id itype_afs_atomic_smartphone_music( "afs_atomic_smartphone_music" ); @@ -291,7 +290,6 @@ static const itype_id itype_string_36( "string_36" ); static const itype_id itype_thermometer( "thermometer" ); static const itype_id itype_towel( "towel" ); static const itype_id itype_towel_wet( "towel_wet" ); -static const itype_id itype_UPS_off( "UPS_off" ); static const itype_id itype_water( "water" ); static const itype_id itype_water_clean( "water_clean" ); static const itype_id itype_wax( "wax" ); @@ -5862,8 +5860,7 @@ cata::optional iuse::toolmod_attach( player *p, item *it, bool, const tripo auto filter = [&it]( const item & e ) { // don't allow ups battery mods on a UPS or UPS-powered tools if( it->has_flag( flag_USE_UPS ) && - ( e.typeId() == itype_UPS_off || e.typeId() == itype_adv_UPS_off || - e.has_flag( flag_USE_UPS ) ) ) { + ( e.has_flag( flag_IS_UPS ) || e.has_flag( flag_USE_UPS ) ) ) { return false; } @@ -8899,7 +8896,7 @@ cata::optional iuse::cable_attach( player *p, item *it, bool, const tripoin const bool has_solar_pack = p->worn_with_flag( flag_SOLARPACK ); const bool has_solar_pack_on = p->worn_with_flag( flag_SOLARPACK_ON ); const bool wearing_solar_pack = has_solar_pack || has_solar_pack_on; - const bool has_ups = p->has_charges( itype_UPS_off, 1 ) || p->has_charges( itype_adv_UPS_off, 1 ); + const bool has_ups = !( p->all_items_with_flag( flag_IS_UPS ) ).empty(); item_location loc; avatar *you = p->as_avatar(); diff --git a/src/player.cpp b/src/player.cpp index e2396b66295f3..7fab508633190 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -93,7 +93,6 @@ static const efftype_id effect_onfire( "onfire" ); static const efftype_id effect_sleep( "sleep" ); static const efftype_id effect_stunned( "stunned" ); -static const itype_id itype_adv_UPS_off( "adv_UPS_off" ); static const itype_id itype_battery( "battery" ); static const itype_id itype_large_repairkit( "large_repairkit" ); static const itype_id itype_small_repairkit( "small_repairkit" ); diff --git a/src/ranged.cpp b/src/ranged.cpp index a5070ba144ab6..17c6e9afdbf05 100644 --- a/src/ranged.cpp +++ b/src/ranged.cpp @@ -85,7 +85,6 @@ static const itype_id itype_40x46mm( "40x46mm" ); static const itype_id itype_40x53mm( "40x53mm" ); static const itype_id itype_66mm( "66mm" ); static const itype_id itype_84x246mm( "84x246mm" ); -static const itype_id itype_adv_UPS_off( "adv_UPS_off" ); static const itype_id itype_arrow( "arrow" ); static const itype_id itype_bolt( "bolt" ); static const itype_id itype_flammable( "flammable" ); diff --git a/src/visitable.cpp b/src/visitable.cpp index 30ec416a94447..c4499777ca7f0 100644 --- a/src/visitable.cpp +++ b/src/visitable.cpp @@ -38,7 +38,7 @@ #include "vehicle_selector.h" static const itype_id itype_apparatus( "apparatus" ); -static const itype_id itype_adv_UPS_off( "adv_UPS_off" ); +static const itype_id itype_toolset( "toolset" ); static const itype_id itype_UPS( "UPS" ); static const itype_id itype_UPS_off( "UPS_off" ); @@ -748,6 +748,9 @@ static int charges_of_internal( const T &self, const M &main, const itype_id &id if( e->has_flag( STATIC( flag_id( "USE_UPS" ) ) ) ) { found_tool_with_UPS = true; } + if( e->has_flag( flag_id( "USES_BIONIC_POWER" ) ) ) { + qty = sum_no_wrap( qty, units::to_kilojoule( get_player_character().get_power_level() ) ); + } } if( !e->is_container() ) { return qty < limit ? VisitResponse::SKIP : VisitResponse::ABORT; @@ -765,8 +768,22 @@ static int charges_of_internal( const T &self, const M &main, const itype_id &id return qty < limit ? VisitResponse::NEXT : VisitResponse::ABORT; } ); - if( qty < limit && found_tool_with_UPS ) { - qty += main.charges_of( itype_UPS, limit - qty ); + if( qty < limit && ( found_tool_with_UPS || id == itype_UPS) ) { + self.visit_items( [&]( const item * e, item * ) { + if( filter( *e ) ) { + if( e->has_flag( flag_id( "IS_UPS" ) ) ) { + qty = sum_no_wrap( qty, e->ammo_remaining() ); + + if( !e->has_pockets() ) { + return qty < limit ? VisitResponse::SKIP : VisitResponse::ABORT; + } + + } + } + // recurse through any nested containers + return qty < limit ? VisitResponse::NEXT : VisitResponse::ABORT; + } ); + if( visitor ) { visitor( qty ); } @@ -780,14 +797,6 @@ int read_only_visitable::charges_of( const itype_id &what, int limit, const std::function &filter, const std::function &visitor ) const { - // Crafting still uses this part. - if( what == itype_UPS ) { - int qty = 0; - qty = sum_no_wrap( qty, charges_of( itype_UPS_off ) ); - qty = sum_no_wrap( qty, static_cast( charges_of( itype_adv_UPS_off ) / 0.6 ) ); - return std::min( qty, limit ); - } - return charges_of_internal( *this, *this, what, limit, filter, visitor ); } @@ -796,13 +805,6 @@ int inventory::charges_of( const itype_id &what, int limit, const std::function &filter, const std::function &visitor ) const { - if( what == itype_UPS ) { - // Crafting still uses this part. - int qty = 0; - qty = sum_no_wrap( qty, charges_of( itype_UPS_off ) ); - qty = sum_no_wrap( qty, static_cast( charges_of( itype_adv_UPS_off ) / 0.6 ) ); - return std::min( qty, limit ); - } const auto &binned = get_binned_items(); const auto iter = binned.find( what ); if( iter == binned.end() ) { @@ -834,7 +836,7 @@ int Character::charges_of( const itype_id &what, int limit, } if( what == itype_UPS ) { - return std::min( available_ups(), limit ); + return available_ups(); } return charges_of_internal( *this, *this, what, limit, filter, visitor ); diff --git a/src/visitable.h b/src/visitable.h index 8bb352dbdc921..fc035cba21e95 100644 --- a/src/visitable.h +++ b/src/visitable.h @@ -10,6 +10,7 @@ #include "cata_utility.h" #include "type_id.h" +class Character; class item; enum class VisitResponse : int { From 65a6dbd63e636375b36f7226ac2b09787481df00 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Mon, 3 May 2021 13:43:23 +0300 Subject: [PATCH 60/87] fix things --- src/visitable.cpp | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/visitable.cpp b/src/visitable.cpp index c4499777ca7f0..92b446a9474b1 100644 --- a/src/visitable.cpp +++ b/src/visitable.cpp @@ -742,7 +742,7 @@ static int charges_of_internal( const T &self, const M &main, const itype_id &id self.visit_items( [&]( const item * e, item * ) { if( !e->is_broken() && filter( *e ) ) { if( e->is_tool() ) { - if( e->typeId() == id ) { + if( e->typeId() == id && id != itype_UPS_off ) { // includes charges from any included magazine. qty = sum_no_wrap( qty, e->ammo_remaining() ); if( e->has_flag( STATIC( flag_id( "USE_UPS" ) ) ) ) { @@ -751,6 +751,8 @@ static int charges_of_internal( const T &self, const M &main, const itype_id &id if( e->has_flag( flag_id( "USES_BIONIC_POWER" ) ) ) { qty = sum_no_wrap( qty, units::to_kilojoule( get_player_character().get_power_level() ) ); } + } else if( id == itype_UPS_off && e->has_flag( STATIC( flag_id( "IS_UPS" ) ) ) ) { + qty = sum_no_wrap( qty, e->ammo_remaining() ); } if( !e->is_container() ) { return qty < limit ? VisitResponse::SKIP : VisitResponse::ABORT; @@ -768,22 +770,8 @@ static int charges_of_internal( const T &self, const M &main, const itype_id &id return qty < limit ? VisitResponse::NEXT : VisitResponse::ABORT; } ); - if( qty < limit && ( found_tool_with_UPS || id == itype_UPS) ) { - self.visit_items( [&]( const item * e, item * ) { - if( filter( *e ) ) { - if( e->has_flag( flag_id( "IS_UPS" ) ) ) { - qty = sum_no_wrap( qty, e->ammo_remaining() ); - - if( !e->has_pockets() ) { - return qty < limit ? VisitResponse::SKIP : VisitResponse::ABORT; - } - - } - } - // recurse through any nested containers - return qty < limit ? VisitResponse::NEXT : VisitResponse::ABORT; - } ); - + if( qty < limit && found_tool_with_UPS ) { + qty += main.charges_of( itype_UPS, limit - qty ); if( visitor ) { visitor( qty ); } @@ -797,6 +785,12 @@ int read_only_visitable::charges_of( const itype_id &what, int limit, const std::function &filter, const std::function &visitor ) const { + if( what == itype_UPS ) { + int qty = 0; + qty = sum_no_wrap( qty, charges_of( itype_UPS_off ) ); + return std::min( qty, limit ); + } + return charges_of_internal( *this, *this, what, limit, filter, visitor ); } @@ -805,6 +799,12 @@ int inventory::charges_of( const itype_id &what, int limit, const std::function &filter, const std::function &visitor ) const { + if( what == itype_UPS ) { + int qty = 0; + qty = sum_no_wrap( qty, charges_of( itype_UPS_off ) ); + return std::min( qty, limit ); + } + const auto &binned = get_binned_items(); const auto iter = binned.find( what ); if( iter == binned.end() ) { @@ -836,7 +836,11 @@ int Character::charges_of( const itype_id &what, int limit, } if( what == itype_UPS ) { - return available_ups(); + int qty = std::min( available_ups(), limit ); + if( visitor ) { + visitor( qty ); + } + return qty; } return charges_of_internal( *this, *this, what, limit, filter, visitor ); From b83b005325bb7e7593d13f5cfb3ead8d90fe3e42 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Mon, 3 May 2021 15:09:52 +0300 Subject: [PATCH 61/87] ups map drain --- src/character.cpp | 13 ++----------- src/map.cpp | 24 ++++++++++++++++++++++++ src/map.h | 9 +++++++++ src/visitable.cpp | 11 +++++------ 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index 1384d74ad4f70..3d844f140d2ef 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -225,8 +225,6 @@ static const itype_id itype_rope_6( "rope_6" ); static const itype_id itype_snare_trigger( "snare_trigger" ); static const itype_id itype_string_36( "string_36" ); static const itype_id itype_toolset( "toolset" ); -static const itype_id itype_UPS_off( "UPS_off" ); -static const itype_id itype_adv_UPS_off( "adv_UPS_off" ); static const itype_id itype_UPS( "UPS" ); static const skill_id skill_archery( "archery" ); @@ -11342,16 +11340,9 @@ int Character::consume_ups( int qty, const int radius ) // Slower method for all nearby items (used while crafting) inventory inv = crafting_inventory( pos(), radius, true ); - int adv = inv.charges_of( itype_adv_UPS_off, qty ); - if( qty != 0 && adv > 0 ) { - use_charges( itype_adv_UPS_off, adv, radius ); - qty -= std::min( qty, adv ); - } - - int ups = inv.charges_of( itype_UPS_off, qty ); + int ups = inv.charges_of( itype_UPS, qty ); if( qty != 0 && ups > 0 ) { - use_charges( itype_UPS_off, ups, radius ); - qty -= ups; + qty -= get_map().consume_ups( pos(), radius, ups ); } } diff --git a/src/map.cpp b/src/map.cpp index 45cb4ddd98f48..71da418acdca9 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -5008,6 +5008,30 @@ std::list map::use_charges( const tripoint &origin, const int range, return ret; } +int map::consume_ups( const tripoint &origin, const int range, int qty ) +{ + // populate a grid of spots that can be reached + std::vector reachable_pts; + reachable_flood_steps( reachable_pts, origin, range, 1, 100 ); + + for( const tripoint &p : reachable_pts ) { + if( accessible_items( p ) ) { + + map_stack items = i_at( p ); + for( auto &elem : items ) { + if( elem.has_flag( flag_IS_UPS ) ) { + qty -= elem.ammo_consume( qty, p, nullptr ); + if( qty == 0 ) { + break; + } + } + } + } + } + + return qty; +} + std::list > map::get_rc_items( const tripoint &p ) { std::list > rc_pairs; diff --git a/src/map.h b/src/map.h index 2b65f3f6b5d72..d1e3f4f07b0c8 100644 --- a/src/map.h +++ b/src/map.h @@ -1283,6 +1283,15 @@ class map std::list use_charges( const tripoint &origin, int range, const itype_id &type, int &quantity, const std::function &filter = return_true, basecamp *bcp = nullptr ); + + /** + * Consume UPS from UPS sources from area centered at origin. + * @param origin the position of player + * @param range how far the UPS can be used from + * @return Amount of UPS used which will be between 0 and qty + */ + int consume_ups( const tripoint &origin, const int range, int qty ); + /*@}*/ std::list > get_rc_items( const tripoint &p = { -1, -1, -1 } ); diff --git a/src/visitable.cpp b/src/visitable.cpp index 92b446a9474b1..831a80ce5745e 100644 --- a/src/visitable.cpp +++ b/src/visitable.cpp @@ -740,9 +740,9 @@ static int charges_of_internal( const T &self, const M &main, const itype_id &id bool found_tool_with_UPS = false; self.visit_items( [&]( const item * e, item * ) { - if( !e->is_broken() && filter( *e ) ) { - if( e->is_tool() ) { - if( e->typeId() == id && id != itype_UPS_off ) { + if( filter( *e ) && !e->is_broken() ) { + if( e->is_tool() && id != itype_UPS_off ) { + if( e->typeId() == id ) { // includes charges from any included magazine. qty = sum_no_wrap( qty, e->ammo_remaining() ); if( e->has_flag( STATIC( flag_id( "USE_UPS" ) ) ) ) { @@ -751,13 +751,12 @@ static int charges_of_internal( const T &self, const M &main, const itype_id &id if( e->has_flag( flag_id( "USES_BIONIC_POWER" ) ) ) { qty = sum_no_wrap( qty, units::to_kilojoule( get_player_character().get_power_level() ) ); } - } else if( id == itype_UPS_off && e->has_flag( STATIC( flag_id( "IS_UPS" ) ) ) ) { - qty = sum_no_wrap( qty, e->ammo_remaining() ); } if( !e->is_container() ) { return qty < limit ? VisitResponse::SKIP : VisitResponse::ABORT; } - + } else if( id == itype_UPS_off && e->has_flag( STATIC( flag_id( "IS_UPS" ) ) ) ) { + qty = sum_no_wrap( qty, e->ammo_remaining() ); } else if( e->count_by_charges() ) { if( e->typeId() == id ) { qty = sum_no_wrap( qty, e->charges ); From d073d226ac22f01847035d891bb93e0a95df96a0 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Mon, 3 May 2021 16:39:38 +0300 Subject: [PATCH 62/87] cleaner --- src/character.h | 5 ++--- src/item.h | 5 ++--- src/visitable.h | 1 - 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/character.h b/src/character.h index c114459e70a8f..73868632defab 100644 --- a/src/character.h +++ b/src/character.h @@ -2056,15 +2056,14 @@ class Character : public Creature, public visitable /** * Available ups from all sources - * Sum of mech, bionic UPS, adv UPS and UPS - * Adv UPS includes efficiency bonus + * Sum of mech, bionic UPS and UPS * @return amount of UPS available */ int available_ups() const; /** * Consume UPS charges. - * Consume order: mech, Bionic UPS, adv UPS, UPS. + * Consume order: mech, Bionic UPS, UPS. * @param qty Number of charges (kJ) * @return amount of UPS consumed which will be between 0 and qty */ diff --git a/src/item.h b/src/item.h index fdb952c58ba25..57db2810c0300 100644 --- a/src/item.h +++ b/src/item.h @@ -1853,18 +1853,17 @@ class item : public visitable * If UPS/bionic power does not matter then the carrier can be nullptr * @param carrier is used for UPS and bionic power */ - int ammo_remaining( const Character *carrier = nullptr ) const; + /** * ammo capacity for a specific ammo */ - int ammo_capacity( const ammotype &ammo ) const; + /** * how much more ammo can fit into this item * if this item is not loaded, gives remaining capacity of its default ammo */ - int remaining_ammo_capacity() const; /** Quantity of ammunition consumed per usage of tool or with each shot of gun */ diff --git a/src/visitable.h b/src/visitable.h index fc035cba21e95..8bb352dbdc921 100644 --- a/src/visitable.h +++ b/src/visitable.h @@ -10,7 +10,6 @@ #include "cata_utility.h" #include "type_id.h" -class Character; class item; enum class VisitResponse : int { From f1a1bf971ec76f708a1f67db672c3382c4684010 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Mon, 3 May 2021 16:43:37 +0300 Subject: [PATCH 63/87] astyle --- src/item.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/item.h b/src/item.h index 57db2810c0300..5c6788706d8e8 100644 --- a/src/item.h +++ b/src/item.h @@ -1854,12 +1854,12 @@ class item : public visitable * @param carrier is used for UPS and bionic power */ int ammo_remaining( const Character *carrier = nullptr ) const; - + /** * ammo capacity for a specific ammo */ int ammo_capacity( const ammotype &ammo ) const; - + /** * how much more ammo can fit into this item * if this item is not loaded, gives remaining capacity of its default ammo From 8ac075be3e4c19a0423def8ec453510e872040e4 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Mon, 3 May 2021 18:44:10 +0300 Subject: [PATCH 64/87] unnecessary const --- src/map.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/map.h b/src/map.h index d1e3f4f07b0c8..841b4d195ea5b 100644 --- a/src/map.h +++ b/src/map.h @@ -1290,7 +1290,7 @@ class map * @param range how far the UPS can be used from * @return Amount of UPS used which will be between 0 and qty */ - int consume_ups( const tripoint &origin, const int range, int qty ); + int consume_ups( const tripoint &origin, int range, int qty ); /*@}*/ std::list > get_rc_items( const tripoint &p = { -1, -1, -1 } ); From 1500c7dd132b6def8d4e82c119e20e45ed085712 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Mon, 3 May 2021 21:36:15 +0300 Subject: [PATCH 65/87] fix bionic power for crafting tools --- src/visitable.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/visitable.cpp b/src/visitable.cpp index 831a80ce5745e..0c7cc65d21ae4 100644 --- a/src/visitable.cpp +++ b/src/visitable.cpp @@ -769,6 +769,10 @@ static int charges_of_internal( const T &self, const M &main, const itype_id &id return qty < limit ? VisitResponse::NEXT : VisitResponse::ABORT; } ); + if( id == itype_UPS_off && qty < limit && get_player_character().has_active_bionic( bio_ups ) ) { + qty = sum_no_wrap( qty, units::to_kilojoule( get_player_character().get_power_level() ) ); + } + if( qty < limit && found_tool_with_UPS ) { qty += main.charges_of( itype_UPS, limit - qty ); if( visitor ) { From 465914897d252d1d64c1ba8d996caf8ac90b22f5 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Thu, 6 May 2021 13:14:46 +0300 Subject: [PATCH 66/87] ups from map and inventory --- src/character.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index 3d844f140d2ef..7af102e735a94 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -11315,6 +11315,7 @@ int Character::consume_ups( int qty, const int radius ) { const int wanted_qty = qty; + // UPS from mounted mech if( qty != 0 && is_mounted() && mounted_creature.get()->has_flag( MF_RIDEABLE_MECH ) && mounted_creature.get()->battery_item ) { auto *mons = mounted_creature.get(); @@ -11323,21 +11324,23 @@ int Character::consume_ups( int qty, const int radius ) qty -= std::min( qty, power_drain ); } + // UPS from bionic if( qty != 0 && has_power() && has_active_bionic( bio_ups ) ) { int bio = std::min( units::to_kilojoule( get_power_level() ), qty ); mod_power_level( units::from_kilojoule( -bio ) ); qty -= std::min( qty, bio ); } - if( radius <= 0 ) { - // Faster method that only cares about carried items + // UPS from inventory + if( qty != 0 ) { std::vector ups_items = all_items_with_flag( flag_IS_UPS ); - for( const item *i : ups_items ) { qty -= const_cast( i )->ammo_consume( qty, tripoint_zero, nullptr ); } - } else { - // Slower method for all nearby items (used while crafting) + } + + // UPS from nearby map + if( qty != 0 && radius > 0 ) { inventory inv = crafting_inventory( pos(), radius, true ); int ups = inv.charges_of( itype_UPS, qty ); From c1498af0d8ad5db4f859fc2e4f82e818933aa940 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Thu, 6 May 2021 18:06:41 +0300 Subject: [PATCH 67/87] don't run this twice --- src/visitable.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/visitable.cpp b/src/visitable.cpp index 0c7cc65d21ae4..8de9f7aba0285 100644 --- a/src/visitable.cpp +++ b/src/visitable.cpp @@ -839,11 +839,7 @@ int Character::charges_of( const itype_id &what, int limit, } if( what == itype_UPS ) { - int qty = std::min( available_ups(), limit ); - if( visitor ) { - visitor( qty ); - } - return qty; + return std::min( available_ups(), limit ); } return charges_of_internal( *this, *this, what, limit, filter, visitor ); From a45817a1d08c87e05afc0221fcddef2705821ee6 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Fri, 7 May 2021 17:37:57 +0300 Subject: [PATCH 68/87] mandatory carrier for ammo_sufficient --- src/game.cpp | 2 +- src/item.h | 4 ++-- src/item_action.cpp | 2 +- src/iuse.cpp | 18 +++++++++--------- src/mattack_actors.cpp | 2 +- src/npcmove.cpp | 25 ++++++++++++++++++++++++- src/turret.cpp | 2 +- src/visitable.cpp | 2 +- 8 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index af1b2f02ed708..8e1b6e02fed5e 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2118,7 +2118,7 @@ static hint_rating rate_action_use( const avatar &you, const item &it ) if( it.is_broken() ) { return hint_rating::iffy; } else if( it.is_tool() ) { - return it.ammo_sufficient() ? hint_rating::good : hint_rating::iffy; + return it.ammo_sufficient( &you ) ? hint_rating::good : hint_rating::iffy; } else if( it.is_gunmod() ) { /** @EFFECT_GUN >0 allows rating estimates for gun modifications */ if( you.get_skill_level( skill_gun ) == 0 ) { diff --git a/src/item.h b/src/item.h index 5c6788706d8e8..9dc84deed40dc 100644 --- a/src/item.h +++ b/src/item.h @@ -1879,11 +1879,11 @@ class item : public visitable * the future. Users of this function will not need to be refactored when this * happens. * - * @param carrier who holds the item. Needed for UPS/bionic only + * @param carrier who holds the item. Needed for UPS/bionic * @param qty Number of uses * @returns true if ammo sufficient for number of uses is loaded, false otherwise */ - bool ammo_sufficient( const Character *carrier = nullptr, int qty = 1 ) const; + bool ammo_sufficient( const Character *carrier, int qty = 1 ) const; /** * Consume ammo (if available) and return the amount of ammo that was consumed diff --git a/src/item_action.cpp b/src/item_action.cpp index c7f55ce82cdeb..e70169476ad76 100644 --- a/src/item_action.cpp +++ b/src/item_action.cpp @@ -151,7 +151,7 @@ item_action_map item_action_generator::map_actions_to_items( player &p, func->get_actor_ptr()->can_use( p, *actual_item, false, p.pos() ).success() ) ) { continue; } - if( !actual_item->ammo_sufficient() && + if( !actual_item->ammo_sufficient( &p ) && ( !actual_item->has_flag( STATIC( flag_id( "USE_UPS" ) ) ) || p.available_ups() < actual_item->ammo_required() ) ) { continue; diff --git a/src/iuse.cpp b/src/iuse.cpp index 8ca653eab7d3d..51306be7f4928 100644 --- a/src/iuse.cpp +++ b/src/iuse.cpp @@ -1986,7 +1986,7 @@ cata::optional iuse::fish_trap( player *p, item *it, bool t, const tripoint cata::optional iuse::extinguisher( player *p, item *it, bool, const tripoint & ) { - if( !it->ammo_sufficient() ) { + if( !it->ammo_sufficient( p ) ) { return cata::nullopt; } // If anyone other than the player wants to use one of these, @@ -2042,7 +2042,7 @@ cata::optional iuse::extinguisher( player *p, item *it, bool, const tripoin cata::optional iuse::rm13armor_off( player *p, item *it, bool, const tripoint & ) { // This allows it to turn on for a turn, because ammo_sufficient assumes non-tool non-weapons need zero ammo, for some reason. - if( !it->ammo_sufficient() ) { + if( !it->ammo_sufficient( p ) ) { p->add_msg_if_player( m_info, _( "The RM13 combat armor's fuel cells are dead." ) ); return cata::nullopt; } else { @@ -3593,7 +3593,7 @@ cata::optional iuse::teleport( player *p, item *it, bool, const tripoint & p->add_msg_if_player( m_info, _( "You can't do that while mounted." ) ); return cata::nullopt; } - if( !it->ammo_sufficient() ) { + if( !it->ammo_sufficient( p ) ) { return cata::nullopt; } p->moves -= to_moves( 1_seconds ); @@ -4009,7 +4009,7 @@ cata::optional iuse::mininuke( player *p, item *it, bool, const tripoint & cata::optional iuse::portal( player *p, item *it, bool, const tripoint & ) { - if( !it->ammo_sufficient() ) { + if( !it->ammo_sufficient( p ) ) { return cata::nullopt; } if( p->is_mounted() ) { @@ -5612,7 +5612,7 @@ cata::optional iuse::adrenaline_injector( player *p, item *it, bool, const cata::optional iuse::jet_injector( player *p, item *it, bool, const tripoint & ) { - if( !it->ammo_sufficient() ) { + if( !it->ammo_sufficient( p ) ) { p->add_msg_if_player( m_info, _( "The jet injector is empty." ) ); return cata::nullopt; } else { @@ -5640,7 +5640,7 @@ cata::optional iuse::stimpack( player *p, item *it, bool, const tripoint & return cata::nullopt; } - if( !it->ammo_sufficient() ) { + if( !it->ammo_sufficient( p ) ) { p->add_msg_if_player( m_info, _( "The stimulant delivery system is empty." ) ); return cata::nullopt; } else { @@ -7878,7 +7878,7 @@ cata::optional iuse::radiocar( player *p, item *it, bool, const tripoint & } if( choice == 0 ) { //Turn car ON - if( !it->ammo_sufficient() ) { + if( !it->ammo_sufficient( p ) ) { p->add_msg_if_player( _( "The RC car's batteries seem to be dead." ) ); return cata::nullopt; } @@ -7942,7 +7942,7 @@ cata::optional iuse::radiocaron( player *p, item *it, bool t, const tripoin sounds::sound( pos, 6, sounds::sound_t::movement, _( "buzzz…" ), true, "misc", "rc_car_drives" ); return it->type->charges_to_use(); - } else if( !it->ammo_sufficient() ) { + } else if( !it->ammo_sufficient( p ) ) { // Deactivate since other mode has an iuse too. it->active = false; return 0; @@ -9139,7 +9139,7 @@ cata::optional iuse::shavekit( player *p, item *it, bool, const tripoint & p->add_msg_if_player( m_info, _( "You can't do that while mounted." ) ); return cata::nullopt; } - if( !it->ammo_sufficient() ) { + if( !it->ammo_sufficient( p ) ) { p->add_msg_if_player( _( "You need soap to use this." ) ); } else { p->assign_activity( player_activity( shave_activity_actor() ) ); diff --git a/src/mattack_actors.cpp b/src/mattack_actors.cpp index 8d6caa9eb9245..e2e948a965689 100644 --- a/src/mattack_actors.cpp +++ b/src/mattack_actors.cpp @@ -572,7 +572,7 @@ void gun_actor::shoot( monster &z, Creature &target, const gun_mode_id &mode ) c return; } - if( !gun.ammo_sufficient() ) { + if( !gun.ammo_sufficient( nullptr ) ) { if( !no_ammo_sound.empty() ) { sounds::sound( z.pos(), 10, sounds::sound_t::combat, no_ammo_sound ); } diff --git a/src/npcmove.cpp b/src/npcmove.cpp index 3bfa1761ae955..ca29b1d514cab 100644 --- a/src/npcmove.cpp +++ b/src/npcmove.cpp @@ -1436,8 +1436,31 @@ void npc::evaluate_best_weapon( const Creature *target ) } } } +<<<<<<< HEAD return VisitResponse::NEXT; } ); +======= + } + + if( dist == 1 && same_z ) { + add_msg_debug( debugmode::DF_NPC, "%s is trying a melee attack", disp_name() ); + return npc_melee; + } + + // don't mess with CBM weapons + if( cbm_weapon_index < 0 ) { + // TODO: Add a time check now that wielding takes a lot of time + if( wield_better_weapon() ) { + add_msg_debug( debugmode::DF_NPC, "%s is changing weapons", disp_name() ); + return npc_noop; + } + + if( !weapon.ammo_sufficient( this ) && can_reload_current() ) { + add_msg_debug( debugmode::DF_NPC, "%s is reloading", disp_name() ); + return npc_reload; + } + } +>>>>>>> 18ee21eab5 (mandatory carrier for ammo_sufficient) ai_cache.current_attack = best_attack; ai_cache.current_attack_evaluation = best_evaluated_attack; @@ -3610,7 +3633,7 @@ void npc::heal_self() const auto filter_use = [this]( const std::string & filter ) -> std::vector { const auto inv_filtered = items_with( [&filter]( const item & itm ) { - return ( itm.type->get_use( filter ) != nullptr ) && ( itm.ammo_sufficient() ); + return ( itm.type->get_use( filter ) != nullptr ) && ( itm.ammo_sufficient( nullptr ) ); } ); return inv_filtered; }; diff --git a/src/turret.cpp b/src/turret.cpp index 7e623560be269..85e9e042c2fce 100644 --- a/src/turret.cpp +++ b/src/turret.cpp @@ -245,7 +245,7 @@ turret_data::status turret_data::query() const } } else { - if( !part->base.ammo_sufficient() ) { + if( !part->base.ammo_sufficient( nullptr ) ) { return status::no_ammo; } } diff --git a/src/visitable.cpp b/src/visitable.cpp index 8de9f7aba0285..8e9f81fbc3e9a 100644 --- a/src/visitable.cpp +++ b/src/visitable.cpp @@ -748,7 +748,7 @@ static int charges_of_internal( const T &self, const M &main, const itype_id &id if( e->has_flag( STATIC( flag_id( "USE_UPS" ) ) ) ) { found_tool_with_UPS = true; } - if( e->has_flag( flag_id( "USES_BIONIC_POWER" ) ) ) { + if( e->has_flag( STATIC( flag_id( "USES_BIONIC_POWER" ) ) ) ) { qty = sum_no_wrap( qty, units::to_kilojoule( get_player_character().get_power_level() ) ); } } From d3fb832036d3eddcfd72f022c5f49229ab1bb9a0 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Fri, 7 May 2021 17:48:52 +0300 Subject: [PATCH 69/87] unnecessary double check --- src/item_action.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/item_action.cpp b/src/item_action.cpp index e70169476ad76..33e6a896131d4 100644 --- a/src/item_action.cpp +++ b/src/item_action.cpp @@ -151,9 +151,7 @@ item_action_map item_action_generator::map_actions_to_items( player &p, func->get_actor_ptr()->can_use( p, *actual_item, false, p.pos() ).success() ) ) { continue; } - if( !actual_item->ammo_sufficient( &p ) && - ( !actual_item->has_flag( STATIC( flag_id( "USE_UPS" ) ) ) || - p.available_ups() < actual_item->ammo_required() ) ) { + if( !actual_item->ammo_sufficient( &p ) ) { continue; } From eb49809184418ffa0cbbe3584c81e1e31bb46632 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Fri, 14 May 2021 14:59:10 +0300 Subject: [PATCH 70/87] obsolete adv ups --- .../itemgroups/Clothing_Gear/clothing.json | 9 +++---- data/json/itemgroups/Clothing_Gear/gear.json | 5 ++-- .../Locations_MapExtras/locations.json | 2 +- .../monster_drops_lairs.json | 3 +-- data/json/itemgroups/activities_hobbies.json | 3 +-- data/json/itemgroups/main.json | 2 +- data/json/itemgroups/military.json | 3 +-- data/json/itemgroups/science_and_tech.json | 3 +-- data/json/items/migration.json | 7 ++++- data/json/items/tool/electronics.json | 26 ------------------- data/json/npcs/NC_SOLDIER.json | 3 +-- data/json/npcs/items_generic.json | 3 +-- data/json/obsolete.json | 26 +++++++++++++++++++ data/json/recipes/other/power_supplies.json | 17 ------------ src/ranged.cpp | 2 +- 15 files changed, 46 insertions(+), 68 deletions(-) diff --git a/data/json/itemgroups/Clothing_Gear/clothing.json b/data/json/itemgroups/Clothing_Gear/clothing.json index 467f3896bc397..7e654c3d2630b 100644 --- a/data/json/itemgroups/Clothing_Gear/clothing.json +++ b/data/json/itemgroups/Clothing_Gear/clothing.json @@ -1948,8 +1948,7 @@ [ "optical_cloak", 1 ], [ "holo_cloak", 1 ], [ "backpack", 38 ], - { "item": "UPS_off", "prob": 5, "charges": [ 0, 1000 ] }, - [ "adv_UPS_off", 3 ], + { "item": "UPS_off", "prob": 8, "charges": [ 0, 1000 ] }, [ "tacvest", 10 ], [ "molle_pack", 8 ], [ "duffelbag", 15 ], @@ -1989,8 +1988,7 @@ { "item": "mil_mess_kit", "prob": 1, "charges": [ 0, 500 ] }, [ "backpack", 38 ], [ "briefcase", 10 ], - { "item": "UPS_off", "prob": 5, "charges": [ 0, 1000 ] }, - [ "adv_UPS_off", 3 ], + { "item": "UPS_off", "prob": 8, "charges": [ 0, 1000 ] }, [ "chestguard_hard", 20 ], [ "armguard_hard", 20 ], [ "legguard_hard", 15 ], @@ -2111,8 +2109,7 @@ [ "legpouch_large", 6 ], [ "chestpouch", 9 ], [ "ammo_satchel", 4 ], - { "item": "UPS_off", "prob": 5, "charges": [ 0, 1000 ] }, - [ "adv_UPS_off", 3 ], + { "item": "UPS_off", "prob": 8, "charges": [ 0, 1000 ] }, [ "tacvest", 10 ], [ "molle_pack", 8 ], [ "legrig", 10 ], diff --git a/data/json/itemgroups/Clothing_Gear/gear.json b/data/json/itemgroups/Clothing_Gear/gear.json index b0c222207bca6..f80f72587f7b0 100644 --- a/data/json/itemgroups/Clothing_Gear/gear.json +++ b/data/json/itemgroups/Clothing_Gear/gear.json @@ -126,8 +126,7 @@ [ "knee_pads", 5 ], [ "elbow_pads", 5 ], [ "legrig", 5 ], - { "item": "UPS_off", "prob": 5, "charges": [ 0, 1000 ] }, - [ "adv_UPS_off", 2 ], + { "item": "UPS_off", "prob": 7, "charges": [ 0, 1000 ] }, [ "emer_blanket", 5 ], { "group": "ammo_pocket_batteries_full", "prob": 50 }, [ "tool_belt", 5 ], @@ -154,7 +153,7 @@ { "item": "rad_monitor", "prob": 10, "charges": [ 0, 100 ] }, { "item": "oxygen_tank", "prob": 10, "charges-min": 0 }, { "item": "smoxygen_tank", "prob": 15, "charges-min": 0 }, - [ "adv_UPS_off", 2 ], + [ "UPS_off", 2 ], { "item": "prussian_blue", "prob": 25, "charges": [ 1, 10 ] }, { "item": "iodine", "prob": 50, "charges": [ 1, 10 ] } ] diff --git a/data/json/itemgroups/Locations_MapExtras/locations.json b/data/json/itemgroups/Locations_MapExtras/locations.json index 23c25fbb3b480..24899825fc46a 100644 --- a/data/json/itemgroups/Locations_MapExtras/locations.json +++ b/data/json/itemgroups/Locations_MapExtras/locations.json @@ -85,7 +85,7 @@ { "group": "gunmod_energy", "prob": 40 }, { "item": "plut_cell", "prob": 30, "charges": [ 1, 5 ] }, [ "plasma", 30 ], - [ "adv_UPS_off", 20 ] + [ "UPS_off", 20 ] ] }, { diff --git a/data/json/itemgroups/Monsters_Animals_Lairs/monster_drops_lairs.json b/data/json/itemgroups/Monsters_Animals_Lairs/monster_drops_lairs.json index c16c6e60b6232..ac013f2c49083 100644 --- a/data/json/itemgroups/Monsters_Animals_Lairs/monster_drops_lairs.json +++ b/data/json/itemgroups/Monsters_Animals_Lairs/monster_drops_lairs.json @@ -237,8 +237,7 @@ { "item": "bot_grenade_hack", "prob": 1 }, { "item": "bot_flashbang_hack", "prob": 1 }, { "item": "bot_gasbomb_hack", "prob": 1 }, - { "item": "UPS_off", "prob": 5 }, - { "item": "adv_UPS_off", "prob": 3 }, + { "item": "UPS_off", "prob": 8 }, { "item": "bio_power_storage", "prob": 10 }, { "item": "bio_flashlight", "prob": 10 }, { "item": "bio_lighter", "prob": 10 }, diff --git a/data/json/itemgroups/activities_hobbies.json b/data/json/itemgroups/activities_hobbies.json index 5b005686179cc..5a0e221d1b405 100644 --- a/data/json/itemgroups/activities_hobbies.json +++ b/data/json/itemgroups/activities_hobbies.json @@ -249,8 +249,7 @@ { "item": "water_purifier", "prob": 5, "charges": [ 0, 100 ] }, { "item": "radio", "prob": 20, "charges": [ 0, 100 ] }, [ "beartrap", 5 ], - { "item": "UPS_off", "prob": 5, "charges": [ 0, 1000 ] }, - [ "adv_UPS_off", 3 ], + { "item": "UPS_off", "prob": 8, "charges": [ 0, 1000 ] }, [ "string_36", 40 ], [ "longbow", 5 ], [ "compbow", 1 ], diff --git a/data/json/itemgroups/main.json b/data/json/itemgroups/main.json index 46ba7335f28cf..845774908c34b 100644 --- a/data/json/itemgroups/main.json +++ b/data/json/itemgroups/main.json @@ -103,7 +103,7 @@ { "item": "knife_rm42", "prob": 10 }, { "item": "laser_rifle", "prob": 10 }, { "item": "mininuke", "prob": 10 }, - { "item": "adv_UPS_off", "prob": 10 }, + { "item": "UPS_off", "prob": 10 }, { "item": "optical_cloak", "prob": 10 }, { "item": "superalloy_harness_dog", "prob": 10 }, { "item": "holo_cloak", "prob": 10 }, diff --git a/data/json/itemgroups/military.json b/data/json/itemgroups/military.json index e1efa1a9a32c4..4cc677140925a 100644 --- a/data/json/itemgroups/military.json +++ b/data/json/itemgroups/military.json @@ -552,8 +552,7 @@ { "item": "v8_combustion", "prob": 10 }, { "item": "extinguisher", "prob": 20, "charges": 100 }, { "item": "radio", "prob": 20, "charges": [ 0, 100 ] }, - { "item": "UPS_off", "prob": 5, "charges": [ 0, 1000 ] }, - { "item": "adv_UPS_off", "prob": 3 }, + { "item": "UPS_off", "prob": 8, "charges": [ 0, 1000 ] }, { "item": "tacvest", "prob": 10 }, { "item": "molle_pack", "prob": 8 }, { "item": "legrig", "prob": 10 }, diff --git a/data/json/itemgroups/science_and_tech.json b/data/json/itemgroups/science_and_tech.json index 2561b7e6b007a..0962ba2743c4f 100644 --- a/data/json/itemgroups/science_and_tech.json +++ b/data/json/itemgroups/science_and_tech.json @@ -78,8 +78,7 @@ [ "flux_comp_gen", 3 ], [ "portal", 2 ], [ "bot_manhack", 1 ], - { "item": "UPS_off", "prob": 5, "charges": [ 0, 1000 ] }, - [ "adv_UPS_off", 3 ], + { "item": "UPS_off", "prob": 8, "charges": [ 0, 1000 ] }, { "item": "tazer", "prob": 3, "charges": [ 0, 500 ] }, { "item": "plasma", "prob": 8, "charges": [ 1, 25 ] }, [ "usb_drive", 5 ], diff --git a/data/json/items/migration.json b/data/json/items/migration.json index 2c89d4764328c..252efdfcd13fc 100644 --- a/data/json/items/migration.json +++ b/data/json/items/migration.json @@ -77,7 +77,7 @@ { "id": "adv_UPS_on", "type": "MIGRATION", - "replace": "adv_UPS_off" + "replace": "UPS_off" }, { "id": "battery_truck", @@ -1420,5 +1420,10 @@ "id": "gloves_xlsurvivor", "type": "MIGRATION", "replace": "xl_gloves_survivor" + }, + { + "id": "adv_UPS_off", + "type": "MIGRATION", + "replace": "UPS_off" } ] diff --git a/data/json/items/tool/electronics.json b/data/json/items/tool/electronics.json index e9533b04e7c47..209ae95d724a9 100644 --- a/data/json/items/tool/electronics.json +++ b/data/json/items/tool/electronics.json @@ -1,30 +1,4 @@ [ - { - "id": "adv_UPS_off", - "type": "TOOL", - "name": { "str": "advanced UPS", "str_pl": "advanced UPS's" }, - "description": "This is an advanced version of the unified power supply, or UPS. This device has been significantly redesigned to provide better efficiency as well as to consume plutonium fuel batteries rather than regular batteries. Sadly, its plutonium reactor can't be charged in UPS charging station.", - "weight": "453 g", - "volume": "2 L", - "price": 560000, - "price_postapoc": 3000, - "to_hit": -1, - "bashing": 8, - "material": [ "aluminum", "plastic" ], - "symbol": ";", - "color": "light_green", - "ammo": [ "battery" ], - "pocket_data": [ - { - "pocket_type": "MAGAZINE_WELL", - "holster": true, - "max_contains_volume": "20 L", - "max_contains_weight": "20 kg", - "item_restriction": [ "heavy_atomic_battery_cell" ] - } - ], - "flags": [ "IS_UPS" ] - }, { "id": "camera", "type": "TOOL", diff --git a/data/json/npcs/NC_SOLDIER.json b/data/json/npcs/NC_SOLDIER.json index 32254860913cf..26579f0da6481 100644 --- a/data/json/npcs/NC_SOLDIER.json +++ b/data/json/npcs/NC_SOLDIER.json @@ -136,8 +136,7 @@ { "item": "goggles_nv", "prob": 1 }, { "item": "goggles_ir", "prob": 1 }, { "item": "optical_cloak", "prob": 1 }, - { "item": "UPS_off", "prob": 5 }, - { "item": "adv_UPS_off", "prob": 3 }, + { "item": "UPS_off", "prob": 8 }, { "item": "tacvest", "prob": 10 }, { "item": "backpack", "prob": 8 }, { "item": "dump_pouch", "prob": 20 }, diff --git a/data/json/npcs/items_generic.json b/data/json/npcs/items_generic.json index 64a3fe75c951b..a12addde7ba34 100644 --- a/data/json/npcs/items_generic.json +++ b/data/json/npcs/items_generic.json @@ -339,7 +339,6 @@ [ "adderall", 2 ], [ "adjustable_stock", 1 ], [ "adv_chemistry", 1 ], - [ "adv_UPS_off", 1 ], [ "advanced_ecig", 5 ], [ "advanced_electronics", 1 ], [ "airhorn", 3 ], @@ -1082,7 +1081,7 @@ [ "trappers_companion", 2 ], [ "triple_sec", 2 ], [ "two_way_radio", 10 ], - [ "UPS_off", 3 ], + [ "UPS_off", 4 ], [ "umbrella", 3 ], [ "usb_drive", 3 ], [ "V8", 3 ], diff --git a/data/json/obsolete.json b/data/json/obsolete.json index 82c8646ae6964..5c8036d93da7f 100644 --- a/data/json/obsolete.json +++ b/data/json/obsolete.json @@ -1547,5 +1547,31 @@ ], "tools": [ [ [ "tongs", -1 ] ], [ [ "swage", -1 ] ], [ [ "surface_heat", 1, "LIST" ], [ "forge", 1 ], [ "oxy_torch", 1 ] ] ], "components": [ [ [ "copper", 1 ] ], [ [ "duct_tape", 1 ] ] ] + }, + { + "id": "adv_UPS_off", + "type": "TOOL", + "name": { "str": "advanced UPS", "str_pl": "advanced UPS's" }, + "description": "This is an advanced version of the unified power supply, or UPS. This device has been significantly redesigned to provide better efficiency as well as to consume plutonium fuel batteries rather than regular batteries. Sadly, its plutonium reactor can't be charged in UPS charging station.", + "weight": "453 g", + "volume": "2 L", + "price": 560000, + "price_postapoc": 3000, + "to_hit": -1, + "bashing": 8, + "material": [ "aluminum", "plastic" ], + "symbol": ";", + "color": "light_green", + "ammo": [ "battery" ], + "pocket_data": [ + { + "pocket_type": "MAGAZINE_WELL", + "holster": true, + "max_contains_volume": "20 L", + "max_contains_weight": "20 kg", + "item_restriction": [ "heavy_atomic_battery_cell" ] + } + ], + "flags": [ "IS_UPS" ] } ] diff --git a/data/json/recipes/other/power_supplies.json b/data/json/recipes/other/power_supplies.json index 03ada284bede5..f0d57a80e8dee 100644 --- a/data/json/recipes/other/power_supplies.json +++ b/data/json/recipes/other/power_supplies.json @@ -76,23 +76,6 @@ "qualities": [ { "id": "SCREW", "level": 1 } ], "components": [ [ [ "power_supply", 4 ] ], [ [ "amplifier", 3 ] ], [ [ "scrap", 4 ] ], [ [ "cable", 10 ] ] ] }, - { - "type": "recipe", - "activity_level": "LIGHT_EXERCISE", - "result": "adv_UPS_off", - "category": "CC_ELECTRONIC", - "subcategory": "CSC_ELECTRONIC_COMPONENTS", - "skill_used": "electronics", - "skills_required": [ "mechanics", 1 ], - "difficulty": 9, - "time": "1 h 25 m", - "reversible": true, - "decomp_learn": 4, - "book_learn": [ [ "recipe_lab_elec", 7 ] ], - "using": [ [ "soldering_standard", 24 ] ], - "qualities": [ { "id": "SCREW", "level": 1 } ], - "components": [ [ [ "power_supply", 6 ] ], [ [ "amplifier", 5 ] ], [ [ "scrap", 4 ] ], [ [ "cable", 14 ] ], [ [ "plut_cell", 2 ] ] ] - }, { "type": "recipe", "activity_level": "LIGHT_EXERCISE", diff --git a/src/ranged.cpp b/src/ranged.cpp index 17c6e9afdbf05..abb7f979975ed 100644 --- a/src/ranged.cpp +++ b/src/ranged.cpp @@ -3516,7 +3516,7 @@ bool gunmode_checks_weapon( avatar &you, const map &m, std::vector if( !is_mech_weapon ) { if( you.available_ups() < ups_drain ) { messages.push_back( string_format( - _( "You need a UPS with at least %2$d charges or an advanced UPS with at least %3$d charges to fire the %1$s!" ), + _( "You need a UPS with at least %2$d charges to fire the %1$s!" ), gmode->tname(), ups_drain, adv_ups_drain ) ); result = false; } From 38721c4bf1396395bfade29b435f7349862b3bec Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Sat, 19 Jun 2021 17:23:03 +0300 Subject: [PATCH 71/87] ups bionic for crafting --- src/visitable.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/visitable.cpp b/src/visitable.cpp index 8e9f81fbc3e9a..c283a08a77ae7 100644 --- a/src/visitable.cpp +++ b/src/visitable.cpp @@ -769,14 +769,15 @@ static int charges_of_internal( const T &self, const M &main, const itype_id &id return qty < limit ? VisitResponse::NEXT : VisitResponse::ABORT; } ); - if( id == itype_UPS_off && qty < limit && get_player_character().has_active_bionic( bio_ups ) ) { + if( found_tool_with_UPS && qty < limit && get_player_character().has_active_bionic( bio_ups ) ) { qty = sum_no_wrap( qty, units::to_kilojoule( get_player_character().get_power_level() ) ); } if( qty < limit && found_tool_with_UPS ) { - qty += main.charges_of( itype_UPS, limit - qty ); + int used_ups = main.charges_of( itype_UPS, limit - qty ); + qty += used_ups; if( visitor ) { - visitor( qty ); + visitor( used_ups ); } } @@ -808,7 +809,7 @@ int inventory::charges_of( const itype_id &what, int limit, return std::min( qty, limit ); } - const auto &binned = get_binned_items(); + const itype_bin &binned = get_binned_items(); const auto iter = binned.find( what ); if( iter == binned.end() ) { return 0; From d18daef3632027f003aa0c3fdaa8cae368bf5ebe Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Sat, 19 Jun 2021 20:41:49 +0300 Subject: [PATCH 72/87] return pure ups power only --- src/visitable.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/visitable.cpp b/src/visitable.cpp index c283a08a77ae7..f7d96297696c4 100644 --- a/src/visitable.cpp +++ b/src/visitable.cpp @@ -789,6 +789,7 @@ int read_only_visitable::charges_of( const itype_id &what, int limit, const std::function &filter, const std::function &visitor ) const { + // I do not believe this is used anywhere - Hirmuolio if( what == itype_UPS ) { int qty = 0; qty = sum_no_wrap( qty, charges_of( itype_UPS_off ) ); @@ -840,9 +841,12 @@ int Character::charges_of( const itype_id &what, int limit, } if( what == itype_UPS ) { - return std::min( available_ups(), limit ); + int ups_power = available_ups(); + if( has_active_bionic( bio_ups ) ) { + ups_power -= units::to_kilojoule( get_power_level() ); + } + return std::min( ups_power, limit ); } - return charges_of_internal( *this, *this, what, limit, filter, visitor ); } From c8cfbd43f1b45f6cad0b2a273a9ae5dbd9b1e76f Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Sat, 19 Jun 2021 21:15:36 +0300 Subject: [PATCH 73/87] removed comment --- src/visitable.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/visitable.cpp b/src/visitable.cpp index f7d96297696c4..6be2d51743639 100644 --- a/src/visitable.cpp +++ b/src/visitable.cpp @@ -789,7 +789,6 @@ int read_only_visitable::charges_of( const itype_id &what, int limit, const std::function &filter, const std::function &visitor ) const { - // I do not believe this is used anywhere - Hirmuolio if( what == itype_UPS ) { int qty = 0; qty = sum_no_wrap( qty, charges_of( itype_UPS_off ) ); From 3f00f7f2c19758c271d5dad5b74bd3592637453b Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Wed, 30 Jun 2021 19:56:26 +0300 Subject: [PATCH 74/87] unnecessary is_tool check --- src/visitable.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/visitable.cpp b/src/visitable.cpp index 6be2d51743639..8e94cd2b0e462 100644 --- a/src/visitable.cpp +++ b/src/visitable.cpp @@ -740,8 +740,8 @@ static int charges_of_internal( const T &self, const M &main, const itype_id &id bool found_tool_with_UPS = false; self.visit_items( [&]( const item * e, item * ) { - if( filter( *e ) && !e->is_broken() ) { - if( e->is_tool() && id != itype_UPS_off ) { + if( filter( *e && !e->is_broken() ) ) { + if( id != itype_UPS_off ) { if( e->typeId() == id ) { // includes charges from any included magazine. qty = sum_no_wrap( qty, e->ammo_remaining() ); From 6a3e3cbb60c6a9ff61369ff450134cd1ac320a0b Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Wed, 30 Jun 2021 20:48:16 +0300 Subject: [PATCH 75/87] improved charges_of_internal --- src/visitable.cpp | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/src/visitable.cpp b/src/visitable.cpp index 8e94cd2b0e462..730060f9e56fe 100644 --- a/src/visitable.cpp +++ b/src/visitable.cpp @@ -740,33 +740,28 @@ static int charges_of_internal( const T &self, const M &main, const itype_id &id bool found_tool_with_UPS = false; self.visit_items( [&]( const item * e, item * ) { - if( filter( *e && !e->is_broken() ) ) { + if( filter( *e ) && id == e->typeId() && !e->is_broken() ) { if( id != itype_UPS_off ) { - if( e->typeId() == id ) { - // includes charges from any included magazine. + if( e->count_by_charges() ) { + qty = sum_no_wrap( qty, e->charges ); + } else { qty = sum_no_wrap( qty, e->ammo_remaining() ); - if( e->has_flag( STATIC( flag_id( "USE_UPS" ) ) ) ) { - found_tool_with_UPS = true; - } - if( e->has_flag( STATIC( flag_id( "USES_BIONIC_POWER" ) ) ) ) { - qty = sum_no_wrap( qty, units::to_kilojoule( get_player_character().get_power_level() ) ); - } } - if( !e->is_container() ) { - return qty < limit ? VisitResponse::SKIP : VisitResponse::ABORT; + if( e->has_flag( STATIC( flag_id( "USE_UPS" ) ) ) ) { + found_tool_with_UPS = true; + } + if( e->has_flag( STATIC( flag_id( "USES_BIONIC_POWER" ) ) ) ) { + qty = sum_no_wrap( qty, units::to_kilojoule( get_player_character().get_power_level() ) ); } } else if( id == itype_UPS_off && e->has_flag( STATIC( flag_id( "IS_UPS" ) ) ) ) { qty = sum_no_wrap( qty, e->ammo_remaining() ); - } else if( e->count_by_charges() ) { - if( e->typeId() == id ) { - qty = sum_no_wrap( qty, e->charges ); - } - // items counted by charges are not themselves expected to be containers - return qty < limit ? VisitResponse::SKIP : VisitResponse::ABORT; } } - // recurse through any nested containers - return qty < limit ? VisitResponse::NEXT : VisitResponse::ABORT; + if( qty >= limit ) { + return VisitResponse::ABORT; + } + // recurse through nested containers if any + return e->is_container() ? VisitResponse::NEXT : VisitResponse::SKIP; } ); if( found_tool_with_UPS && qty < limit && get_player_character().has_active_bionic( bio_ups ) ) { From 47fb870d12b1f6c2021acebca1e3b9acd56d72b8 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Sun, 4 Jul 2021 22:00:47 +0300 Subject: [PATCH 76/87] force-push improvements --- src/visitable.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/visitable.cpp b/src/visitable.cpp index 730060f9e56fe..6ee764df092a1 100644 --- a/src/visitable.cpp +++ b/src/visitable.cpp @@ -38,7 +38,6 @@ #include "vehicle_selector.h" static const itype_id itype_apparatus( "apparatus" ); -static const itype_id itype_toolset( "toolset" ); static const itype_id itype_UPS( "UPS" ); static const itype_id itype_UPS_off( "UPS_off" ); @@ -830,7 +829,7 @@ int Character::charges_of( const itype_id &what, int limit, for( const auto &bio : *this->my_bionics ) { const bionic_data &bid = bio.info(); if( bid.fake_item == what && ( !bid.activated || bio.powered ) ) { - return std::min( units::to_kilojoule( p->get_power_level() ), limit ); + return std::min( item(bid.fake_item).ammo_remaining( p ), limit ); } } From 5d7fa380bfc1e572214e4b933470b18ad8f1ad47 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Sun, 4 Jul 2021 22:20:29 +0300 Subject: [PATCH 77/87] astyle --- src/visitable.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/visitable.cpp b/src/visitable.cpp index 6ee764df092a1..387d33b285aea 100644 --- a/src/visitable.cpp +++ b/src/visitable.cpp @@ -829,7 +829,7 @@ int Character::charges_of( const itype_id &what, int limit, for( const auto &bio : *this->my_bionics ) { const bionic_data &bid = bio.info(); if( bid.fake_item == what && ( !bid.activated || bio.powered ) ) { - return std::min( item(bid.fake_item).ammo_remaining( p ), limit ); + return std::min( item( bid.fake_item ).ammo_remaining( p ), limit ); } } From 8c15666e7757f7366ec6b235225a54e2ba29b308 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Thu, 8 Jul 2021 01:19:25 +0300 Subject: [PATCH 78/87] fix starting repair --- src/item.cpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/item.cpp b/src/item.cpp index 31a342753f302..c69c15ee4ae59 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8521,12 +8521,7 @@ int item::units_remaining( const Character &ch, int limit ) const return std::min( static_cast( charges ), limit ); } - int res = ammo_remaining(); - if( res < limit && has_flag( flag_USE_UPS ) ) { - res += std::min( ch.available_ups(), limit - res ); - } - - return std::min( static_cast( res ), limit ); + return std::min( ammo_remaining( &ch ), limit ); } bool item::units_sufficient( const Character &ch, int qty ) const From 1185428d961e4cfec9c951031e8e2f29b11b6b28 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Thu, 8 Jul 2021 01:45:17 +0300 Subject: [PATCH 79/87] fix repair bionic consumption --- src/character.cpp | 2 +- src/item.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index 7af102e735a94..8f97c44e91bfc 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -8962,7 +8962,7 @@ bool Character::consume_charges( item &used, int qty ) return true; } - used.ammo_consume( qty, pos(), nullptr ); + used.ammo_consume( qty, pos(), this ); return false; } diff --git a/src/item.cpp b/src/item.cpp index c69c15ee4ae59..7b2dfeb2dc90f 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8069,7 +8069,7 @@ int item::ammo_consume( int qty, const tripoint &pos, Character *carrier ) // Consume bio pwr directly if( carrier != nullptr && has_flag( flag_USES_BIONIC_POWER ) ) { int bio_used = std::min( units::to_kilojoule( carrier->get_power_level() ), qty ); - carrier->mod_power_level( units::from_kilojoule( bio_used ) ); + carrier->mod_power_level( -units::from_kilojoule( bio_used ) ); qty -= bio_used; } From 1bb36f0e71133170eff044b402f98bed39f1035b Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Mon, 12 Jul 2021 23:38:48 +0300 Subject: [PATCH 80/87] rebase fixes --- src/npc_attack.cpp | 4 ++-- src/npcmove.cpp | 25 +------------------------ 2 files changed, 3 insertions(+), 26 deletions(-) diff --git a/src/npc_attack.cpp b/src/npc_attack.cpp index c7edaf31daa69..c618f6cdb2b07 100644 --- a/src/npc_attack.cpp +++ b/src/npc_attack.cpp @@ -189,7 +189,7 @@ npc_attack_rating npc_attack_melee::evaluate_critter( const npc &source, void npc_attack_gun::use( npc &source, const tripoint &location ) const { const item &weapon = *gunmode; - if( !weapon.ammo_sufficient() ) { + if( !weapon.ammo_sufficient( &source) ) { source.do_reload( weapon ); add_msg_debug( debugmode::debug_filter::DF_NPC, "%s is reloading %s", source.disp_name(), weapon.display_name() ); @@ -223,7 +223,7 @@ int npc_attack_gun::base_time_penalty( const npc &source ) const time_penalty += npc_attack_constants::base_time_penalty; } // we want the need to reload a gun cumulative with needing to wield the gun - if( !weapon.ammo_sufficient() ) { + if( !weapon.ammo_sufficient( &source) ) { time_penalty += npc_attack_constants::base_time_penalty; } int recoil_penalty = 0; diff --git a/src/npcmove.cpp b/src/npcmove.cpp index ca29b1d514cab..0c6d4497ad8ca 100644 --- a/src/npcmove.cpp +++ b/src/npcmove.cpp @@ -1417,7 +1417,7 @@ void npc::evaluate_best_weapon( const Creature *target ) // punching things is always available compare( std::make_shared( null_item_reference() ) ); - const int ups_charges = charges_of( itype_UPS ); + const int ups_charges = available_ups(); visit_items( [&compare, &ups_charges, this]( item * it, item * ) { // you can theoretically melee with anything. compare( std::make_shared( *it ) ); @@ -1436,31 +1436,8 @@ void npc::evaluate_best_weapon( const Creature *target ) } } } -<<<<<<< HEAD return VisitResponse::NEXT; } ); -======= - } - - if( dist == 1 && same_z ) { - add_msg_debug( debugmode::DF_NPC, "%s is trying a melee attack", disp_name() ); - return npc_melee; - } - - // don't mess with CBM weapons - if( cbm_weapon_index < 0 ) { - // TODO: Add a time check now that wielding takes a lot of time - if( wield_better_weapon() ) { - add_msg_debug( debugmode::DF_NPC, "%s is changing weapons", disp_name() ); - return npc_noop; - } - - if( !weapon.ammo_sufficient( this ) && can_reload_current() ) { - add_msg_debug( debugmode::DF_NPC, "%s is reloading", disp_name() ); - return npc_reload; - } - } ->>>>>>> 18ee21eab5 (mandatory carrier for ammo_sufficient) ai_cache.current_attack = best_attack; ai_cache.current_attack_evaluation = best_evaluated_attack; From 42ce4c120418ef087c79adc748b206b10d16c5a8 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Mon, 12 Jul 2021 23:45:44 +0300 Subject: [PATCH 81/87] unused itype_toolset --- src/character.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/character.cpp b/src/character.cpp index 8f97c44e91bfc..42af0a9b59964 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -224,7 +224,6 @@ static const itype_id itype_rm13_armor_on( "rm13_armor_on" ); static const itype_id itype_rope_6( "rope_6" ); static const itype_id itype_snare_trigger( "snare_trigger" ); static const itype_id itype_string_36( "string_36" ); -static const itype_id itype_toolset( "toolset" ); static const itype_id itype_UPS( "UPS" ); static const skill_id skill_archery( "archery" ); From 14a87cdfc53027fcc1cd08323051bdcb6d42e327 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 13 Jul 2021 00:28:02 +0300 Subject: [PATCH 82/87] aftershock migration --- data/mods/Aftershock/items/migration.json | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 data/mods/Aftershock/items/migration.json diff --git a/data/mods/Aftershock/items/migration.json b/data/mods/Aftershock/items/migration.json new file mode 100644 index 0000000000000..605638a5e6ad5 --- /dev/null +++ b/data/mods/Aftershock/items/migration.json @@ -0,0 +1,7 @@ +[ + { + "id": "adv_UPS_off", + "type": "MIGRATION", + "replace": "adv_UPS_off" + } +] From 60ffaf557f7aabbab29cb88c98baf386dee56c8b Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 13 Jul 2021 00:35:24 +0300 Subject: [PATCH 83/87] info in json --- data/json/flags.json | 3 ++- data/mods/Aftershock/items/migration.json | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/data/json/flags.json b/data/json/flags.json index be2479e6f193c..e3283e374cf2f 100644 --- a/data/json/flags.json +++ b/data/json/flags.json @@ -1419,7 +1419,8 @@ { "id": "IS_UPS", "type": "json_flag", - "context": [ ] + "context": [ ], + "info": "This item provides power to UPS compatible items." }, { "id": "LEAK_DAM", diff --git a/data/mods/Aftershock/items/migration.json b/data/mods/Aftershock/items/migration.json index 605638a5e6ad5..7d29383915d07 100644 --- a/data/mods/Aftershock/items/migration.json +++ b/data/mods/Aftershock/items/migration.json @@ -2,6 +2,7 @@ { "id": "adv_UPS_off", "type": "MIGRATION", - "replace": "adv_UPS_off" + "replace": "adv_UPS_off", + "//": "Overrides migration from CDDA." } ] From bf0ebce40e22c13658116d0bc84b8ea6396ca067 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 13 Jul 2021 00:35:58 +0300 Subject: [PATCH 84/87] astyle --- src/npc_attack.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/npc_attack.cpp b/src/npc_attack.cpp index c618f6cdb2b07..f08a7644f93b3 100644 --- a/src/npc_attack.cpp +++ b/src/npc_attack.cpp @@ -189,7 +189,7 @@ npc_attack_rating npc_attack_melee::evaluate_critter( const npc &source, void npc_attack_gun::use( npc &source, const tripoint &location ) const { const item &weapon = *gunmode; - if( !weapon.ammo_sufficient( &source) ) { + if( !weapon.ammo_sufficient( &source ) ) { source.do_reload( weapon ); add_msg_debug( debugmode::debug_filter::DF_NPC, "%s is reloading %s", source.disp_name(), weapon.display_name() ); @@ -223,7 +223,7 @@ int npc_attack_gun::base_time_penalty( const npc &source ) const time_penalty += npc_attack_constants::base_time_penalty; } // we want the need to reload a gun cumulative with needing to wield the gun - if( !weapon.ammo_sufficient( &source) ) { + if( !weapon.ammo_sufficient( &source ) ) { time_penalty += npc_attack_constants::base_time_penalty; } int recoil_penalty = 0; From c0f9a4b1e44ced81fa2c03f324c5634f1004639f Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Tue, 13 Jul 2021 10:05:58 +0300 Subject: [PATCH 85/87] unused variable --- src/ranged.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ranged.cpp b/src/ranged.cpp index abb7f979975ed..b0965052efcd8 100644 --- a/src/ranged.cpp +++ b/src/ranged.cpp @@ -104,7 +104,6 @@ static const skill_id skill_launcher( "launcher" ); static const skill_id skill_throw( "throw" ); static const bionic_id bio_railgun( "bio_railgun" ); -static const bionic_id bio_ups( "bio_ups" ); static const std::string flag_MOUNTABLE( "MOUNTABLE" ); From cc0bdb29765fab05a3ebf5a4d8861d93af5e9d3c Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Thu, 15 Jul 2021 00:45:06 +0300 Subject: [PATCH 86/87] adv ups not more efficient --- data/mods/Aftershock/items/tools.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/mods/Aftershock/items/tools.json b/data/mods/Aftershock/items/tools.json index 5d0c6819e3493..8725aed196e06 100644 --- a/data/mods/Aftershock/items/tools.json +++ b/data/mods/Aftershock/items/tools.json @@ -195,7 +195,7 @@ "type": "TOOL_ARMOR", "copy-from": "adv_UPS_off", "name": { "str": "advanced UPS", "str_pl": "advanced UPS's" }, - "description": "This is an advanced version of the unified power supply, or UPS. This device has been significantly redesigned to provide better efficiency as well as to consume plutonium fuel cells rather than batteries, and is both slimmer and lighter to wear. Sadly, its plutonium reactor can't be charged in UPS charging station.", + "description": "This is an advanced version of the unified power supply, or UPS. This device has been significantly redesigned to consume plutonium fuel cells rather than batteries, and is both slimmer and lighter to wear. Sadly, its plutonium reactor can't be charged in UPS charging station.", "sided": true, "flags": [ "WAIST", "FRAGILE", "OVERSIZE", "IS_UPS" ], "armor": [ { "encumbrance": 1, "coverage": 5, "covers": [ "leg_l", "leg_r" ] } ] From cc25bc57f2ab0369edf29faddf4ba586c54f8886 Mon Sep 17 00:00:00 2001 From: Hirmuolio Date: Sat, 17 Jul 2021 00:36:51 +0300 Subject: [PATCH 87/87] remove Character::has_enough_charges --- src/character.cpp | 45 +++++++++++++++++---------------------------- src/character.h | 6 ------ src/item.cpp | 1 - src/iuse.cpp | 17 ++++++----------- 4 files changed, 23 insertions(+), 46 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index 42af0a9b59964..937dedb8b1343 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -8760,7 +8760,23 @@ bool Character::invoke_item( item *used, const std::string &method, const tripoi add_msg_if_player( m_bad, _( "Your %s was broken and won't turn on." ), used->tname() ); return false; } - if( !has_enough_charges( *used, true ) ) { + if( !used->ammo_sufficient( this ) ) { + int ammo_req = used->ammo_required(); + std::string it_name = used->tname(); + if( used->has_flag( flag_USE_UPS ) ) { + add_msg_if_player( m_info, + ngettext( "Your %s needs %d charge from some UPS.", + "Your %s needs %d charges from some UPS.", + ammo_req ), + it_name, ammo_req ); + } else { + int ammo_rem = used->ammo_remaining(); + add_msg_if_player( m_info, + ngettext( "Your %s has %d charge, but needs %d.", + "Your %s has %d charges, but needs %d.", + ammo_rem ), + it_name, ammo_rem, ammo_req ); + } moves = pre_obtain_moves; return false; } @@ -8892,33 +8908,6 @@ bool Character::dispose_item( item_location &&obj, const std::string &prompt ) return false; } -bool Character::has_enough_charges( const item &it, bool show_msg ) const -{ - if( !it.is_tool() || !it.ammo_required() ) { - return true; - } - - if( it.ammo_sufficient( this ) ) { - return true; - } else { - if( show_msg && it.has_flag( flag_USE_UPS ) ) { - add_msg_if_player( m_info, - ngettext( "Your %s needs %d charge from some UPS.", - "Your %s needs %d charges from some UPS.", - it.ammo_required() ), - it.tname(), it.ammo_required() ); - } else if( show_msg ) { - add_msg_if_player( m_info, - ngettext( "Your %s has %d charge, but needs %d.", - "Your %s has %d charges, but needs %d.", - it.ammo_remaining() ), - it.tname(), it.ammo_remaining(), it.ammo_required() ); - } - return false; - } - return true; -} - bool Character::consume_charges( item &used, int qty ) { if( qty < 0 ) { diff --git a/src/character.h b/src/character.h index 73868632defab..e0a81d422eea9 100644 --- a/src/character.h +++ b/src/character.h @@ -1415,12 +1415,6 @@ class Character : public Creature, public visitable */ virtual bool dispose_item( item_location &&obj, const std::string &prompt = std::string() ); - /** - * Has the item enough charges to invoke its use function? - * Also checks if UPS from this player is used instead of item charges. - */ - bool has_enough_charges( const item &it, bool show_msg ) const; - /** Consume charges of a tool or comestible item, potentially destroying it in the process * @param used item consuming the charges * @param qty number of charges to consume which must be non-zero diff --git a/src/item.cpp b/src/item.cpp index 7b2dfeb2dc90f..2d2a42d21c3cc 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8038,7 +8038,6 @@ bool item::ammo_sufficient( const Character *carrier, int qty ) const return ammo_remaining( carrier ) >= get_gun_ups_drain() * qty; } return true; - } int item::ammo_consume( int qty, const tripoint &pos, Character *carrier ) diff --git a/src/iuse.cpp b/src/iuse.cpp index 51306be7f4928..d9fde20b57084 100644 --- a/src/iuse.cpp +++ b/src/iuse.cpp @@ -2299,8 +2299,7 @@ cata::optional iuse::radio_on( player *p, item *it, bool t, const tripoint } } else { // Activated int ch = 1; - if( it->ammo_remaining() > 0 || ( it->has_flag( flag_USE_UPS ) && - p->has_enough_charges( *it, false ) ) ) { + if( it->ammo_sufficient( p ) ) { ch = uilist( _( "Radio:" ), { _( "Scan" ), _( "Turn off" ) } ); @@ -3326,7 +3325,7 @@ cata::optional iuse::jackhammer( player *p, item *it, bool, const tripoint { // use has_enough_charges to check for UPS availability // p is assumed to exist for iuse cases - if( !p->has_enough_charges( *it, false ) ) { + if( !it->ammo_sufficient( p ) ) { return cata::nullopt; } if( p->is_mounted() ) { @@ -4117,8 +4116,7 @@ cata::optional iuse::shocktonfa_off( player *p, item *it, bool t, const tri return iuse::tazer2( p, it, t, pos ); } case 1: { - if( !it->units_sufficient( *p ) && !( it->has_flag( flag_USE_UPS ) && - p->has_enough_charges( *it, false ) ) ) { + if( !it->units_sufficient( *p ) ) { p->add_msg_if_player( m_info, _( "The batteries are dead." ) ); return cata::nullopt; } else { @@ -4136,8 +4134,7 @@ cata::optional iuse::shocktonfa_on( player *p, item *it, bool t, const trip if( t ) { // Effects while simply on } else { - if( !it->units_sufficient( *p ) && !( it->has_flag( flag_USE_UPS ) && - p->has_enough_charges( *it, false ) ) ) { + if( !it->units_sufficient( *p ) ) { p->add_msg_if_player( m_info, _( "Your tactical tonfa is out of power." ) ); it->convert( itype_shocktonfa_off ).active = false; } else { @@ -4162,8 +4159,7 @@ cata::optional iuse::shocktonfa_on( player *p, item *it, bool t, const trip cata::optional iuse::mp3( player *p, item *it, bool, const tripoint & ) { // TODO: avoid item id hardcoding to make this function usable for pure json-defined devices. - if( !it->units_sufficient( *p ) && !( it->has_flag( flag_USE_UPS ) && - p->has_enough_charges( *it, false ) ) ) { + if( !it->units_sufficient( *p ) ) { p->add_msg_if_player( m_info, _( "The device's batteries are dead." ) ); } else if( p->has_active_item( itype_mp3_on ) || p->has_active_item( itype_smartphone_music ) || p->has_active_item( itype_afs_atomic_smartphone_music ) || @@ -6305,8 +6301,7 @@ cata::optional iuse::einktabletpc( player *p, item *it, bool t, const tripo { if( t ) { if( !it->get_var( "EIPC_MUSIC_ON" ).empty() && - ( it->ammo_remaining() > 0 || ( it->has_flag( flag_USE_UPS ) && - p->has_enough_charges( *it, false ) ) ) ) { + it->ammo_sufficient( p ) ) { if( calendar::once_every( 5_minutes ) ) { it->ammo_consume( 1, p->pos(), p ); }