From 25534aad459ae225446cf49af2a59de71829f7cc Mon Sep 17 00:00:00 2001 From: nexusmrsep Date: Sun, 25 Aug 2019 20:18:01 +0200 Subject: [PATCH 1/7] charge bionics from UPS via cable --- data/json/items/tools.json | 6 ++-- doc/JSON_FLAGS.md | 1 + src/bionics.cpp | 24 +++++++++++++ src/item.cpp | 50 ++++++++++++++++++++++++++ src/item.h | 1 + src/iuse.cpp | 73 +++++++++++++++++++++++++++++++------- src/player.cpp | 8 ----- 7 files changed, 141 insertions(+), 22 deletions(-) diff --git a/data/json/items/tools.json b/data/json/items/tools.json index 5aa7bea7580be..61c59469c2471 100644 --- a/data/json/items/tools.json +++ b/data/json/items/tools.json @@ -121,7 +121,8 @@ "magazines": [ [ "battery", [ "heavy_plus_battery_cell", "heavy_battery_cell", "heavy_atomic_battery_cell", "heavy_disposable_cell" ] ] ], - "magazine_well": 4 + "magazine_well": 4, + "flags": [ "IS_UPS" ] }, { "id": "acidbomb", @@ -155,7 +156,8 @@ "symbol": ";", "color": "light_green", "ammo": "plutonium", - "max_charges": 2500 + "max_charges": 2500, + "flags": [ "IS_UPS" ] }, { "id": "advanced_ecig", diff --git a/doc/JSON_FLAGS.md b/doc/JSON_FLAGS.md index 793825579cb15..ff6f546eee553 100644 --- a/doc/JSON_FLAGS.md +++ b/doc/JSON_FLAGS.md @@ -1189,6 +1189,7 @@ Melee flags are fully compatible with tool flags, and vice versa. - ```FISH_GOOD``` When used for fishing, it's a good tool (requires that the matching use_action has been set). - ```FISH_POOR``` When used for fishing, it's a poor tool (requires that the matching use_action has been set). - ```HAS_RECIPE``` Used by the E-Ink tablet to indicates it's currently showing a recipe. +- ```IS_UPS``` Item is Unified Power Supply. Used in active item processing - ```LIGHT_[X]``` Illuminates the area with light intensity `[X]` where `[X]` is an intensity value. (e.x. `LIGHT_4` or `LIGHT_100`). - ```MC_MOBILE```, ```MC_RANDOM_STUFF```, ```MC_SCIENCE_STUFF```, ```MC_USED```, ```MC_HAS_DATA``` Memory card related flags, see `iuse.cpp` - ```NO_DROP``` Item should never exist on map tile as a discrete item (must be contained by another item) diff --git a/src/bionics.cpp b/src/bionics.cpp index 3de6376b6b150..0c3f92d883f14 100644 --- a/src/bionics.cpp +++ b/src/bionics.cpp @@ -918,6 +918,30 @@ void player::process_bionic( int b ) for( const item *cable : cables ) { const cata::optional target = cable->get_cable_target( this, pos() ); if( !target ) { + if( g->m.is_outside( pos() ) && !is_night( calendar::turn ) && cable->get_var( "state" ) == "solar_pack_link" ) { + double modifier = g->natural_light_level( pos().z ) / default_daylight_level(); + // basic solar panel produces 50W = 1 charge/20_seconds = 180 charges/hour(3600) + if( is_wearing( "solarpack_on" ) && x_in_y( 180 * modifier, 3600 ) ) { + add_msg( m_bad, "MOD: %f", modifier * 180 ); + charge_power( 1 ); + } + // quantum solar backpack = solar panel x6 + if( is_wearing( "q_solarpack_on" ) && x_in_y( 6 * 180 * modifier, 3600 ) ) { + charge_power( 1 ); + } + } + if( cable->get_var( "state" ) == "UPS_link" ) { + static const item_filter used_ups = [&]( const item & itm ) { + return itm.get_var( "cable" ) == "plugged_in"; + }; + if( has_charges( "UPS_off", 1, used_ups ) ) { + use_charges( "UPS_off", 1, used_ups ); + charge_power( 1 ); + } else if( has_charges( "adv_UPS_off", 1, used_ups ) ) { + use_charges( "adv_UPS_off", roll_remainder( 0.6 ), used_ups ); + charge_power( 1 ); + } + } continue; } const optional_vpart_position vp = g->m.veh_at( *target ); diff --git a/src/item.cpp b/src/item.cpp index ecd28828c58a0..432cf5292ba3e 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -3450,6 +3450,8 @@ std::string item::tname( unsigned int quantity, bool with_prefix, unsigned int t } if( active && ( has_flag( "WATER_EXTINGUISH" ) || has_flag( "LITCIG" ) ) ) { ret << _( " (lit)" ); + } else if( has_flag( "IS_UPS" ) && get_var( "cable" ) == "plugged_in" ) { + ret << _( " (plugged in)" ); } else if( active && !is_food() && !is_corpse() && ( typeId().length() < 3 || typeId().compare( typeId().length() - 3, 3, "_on" ) != 0 ) ) { // Usually the items whose ids end in "_on" have the "active" or "on" string already contained @@ -8129,6 +8131,33 @@ cata::optional item::get_cable_target( player *p, const tripoint &pos bool item::process_cable( player *p, const tripoint &pos ) { + if( p == nullptr ) { + reset_cable( p ); + } + std::string state = get_var( "state" ); + if( state == "solar_pack_link" ) { + if( !p->has_item( *this ) || ( !p->is_wearing( "solarpack_on" ) || + !p->is_wearing( "solarpack_on" ) ) ) { + p->add_msg_if_player( m_bad, _( "You notice the cable has come loose!" ) ); + reset_cable( p ); + return false; + } + } + + static const item_filter used_ups = [&]( const item & itm ) { + return itm.get_var( "cable" ) == "plugged_in"; + }; + + if( state == "UPS" ) { + if( !p->has_item( *this ) || ( !p->has_item_with( used_ups ) ) ) { + p->add_msg_if_player( m_bad, _( "You notice the cable has come loose!" ) ); + for( item *used : p->items_with( used_ups ) ) { + used->erase_var( "cable" ); + } + reset_cable( p ); + return false; + } + } const cata::optional source = get_cable_target( p, pos ); if( !source ) { return false; @@ -8173,6 +8202,23 @@ void item::reset_cable( player *p ) } } +bool item::process_UPS( player *p, const tripoint & /*pos*/ ) +{ + if( p == nullptr ) { + erase_var( "cable" ); + active = false; + return false; + } + bool has_connected_cable = p->has_item_with( []( const item & it ) { + return it.active && it.has_flag( "CABLE_SPOOL" ) && ( it.get_var( "state" ) == "UPS_link" || it.get_var( "state" ) == " UPS " ); + } ); + if ( !has_connected_cable ) { + erase_var( "cable" ); + active = false; + } + return false; +} + bool item::process_wet( player * /*carrier*/, const tripoint & /*pos*/ ) { if( item_counter == 0 ) { @@ -8341,6 +8387,10 @@ bool item::process( player *carrier, const tripoint &pos, bool activate, // DO NOT process this as a tool! It really isn't! return process_cable( carrier, pos ); } + if( has_flag( "IS_UPS" ) ) { + // DO NOT process this as a tool! It really isn't! + return process_UPS( carrier, pos ); + } if( is_tool() ) { return process_tool( carrier, pos ); } diff --git a/src/item.h b/src/item.h index 10969c2dc2f20..da45065c260ee 100644 --- a/src/item.h +++ b/src/item.h @@ -2011,6 +2011,7 @@ class item : public visitable bool process_fake_smoke( player *carrier, const tripoint &pos ); bool process_fake_mill( player *carrier, const tripoint &pos ); bool process_cable( player *carrier, const tripoint &pos ); + bool process_UPS( player *carrier, const tripoint &pos ); bool process_blackpowder_fouling( player *carrier ); bool process_tool( player *carrier, const tripoint &pos ); diff --git a/src/iuse.cpp b/src/iuse.cpp index 6573c63abced5..4ce898d9ba77d 100644 --- a/src/iuse.cpp +++ b/src/iuse.cpp @@ -8436,6 +8436,7 @@ int iuse::cable_attach( player *p, item *it, bool, const tripoint & ) const bool has_solar_pack = p->is_wearing( "solarpack" ) || p->is_wearing( "q_solarpack" ); const bool has_solar_pack_on = p->is_wearing( "solarpack_on" ) || p->is_wearing( "q_solarpack_on" ); const bool wearing_solar_pack = has_solar_pack || has_solar_pack_on; + const bool has_ups = p->has_charges( "UPS_off", 1 ) || p->has_charges( "adv_UPS_off", 1 ); const auto set_cable_active = []( player * p, item * it, const std::string & state ) { it->set_var( "state", state ); @@ -8452,6 +8453,9 @@ int iuse::cable_attach( player *p, item *it, bool, const tripoint & ) if( wearing_solar_pack ) { kmenu.addentry( 2, has_solar_pack_on, -1, _( "Attach cable to solar pack" ) ); } + if( has_ups ) { + kmenu.addentry( 3, true, -1, _( "Attach cable to UPS" ) ); + } kmenu.query(); int choice = kmenu.ret; @@ -8459,9 +8463,24 @@ int iuse::cable_attach( player *p, item *it, bool, const tripoint & ) return 0; // we did nothing. } else if( choice == 1 ) { set_cable_active( p, it, "cable_charger" ); + p->add_msg_if_player( m_info, _( "You attach the cable to your Cable Charger System." ) ); return 0; } else if( choice == 2 ) { set_cable_active( p, it, "solar_pack" ); + p->add_msg_if_player( m_info, _( "You attach the cable to the solar pack." ) ); + return 0; + } else if( choice == 3 ) { + int pos = g->inv_for_filter( _( "Choose UPS:" ), [&]( const item & itm ) { + return itm.has_flag( "IS_UPS" ); }, _( "You don't have any UPS." ) ); + if( pos == INT_MIN ) { + add_msg( _( "Never mind" ) ); + return 0; + } + item &chosen = p->i_at( pos ); + chosen.set_var( "cable", "plugged_in" ); + chosen.activate(); + set_cable_active( p, it, "UPS" ); + p->add_msg_if_player( m_info, _( "You attach the cable to the UPS." ) ); return 0; } // fall through for attaching to a vehicle @@ -8503,16 +8522,19 @@ int iuse::cable_attach( player *p, item *it, bool, const tripoint & ) const bool paying_out = initial_state == "pay_out_cable"; const bool cable_cbm = initial_state == "cable_charger"; const bool solar_pack = initial_state == "solar_pack"; - bool loose_ends = paying_out || cable_cbm || solar_pack; + const bool UPS = initial_state == "UPS"; + bool loose_ends = paying_out || cable_cbm || solar_pack || UPS; uilist kmenu; kmenu.text = _( "Using cable:" ); - kmenu.addentry( 0, paying_out || cable_cbm, -1, _( "Attach loose end of the cable" ) ); - kmenu.addentry( 1, true, -1, _( "Detach and re-spool the cable" ) ); + kmenu.addentry( 0, true, -1, _( "Detach and re-spool the cable" ) ); if( has_bio_cable && loose_ends ) { - kmenu.addentry( 2, !cable_cbm, -1, _( "Attach cable to self" ) ); - // can't attach solar backpacks to cars - if( wearing_solar_pack && cable_cbm ) { - kmenu.addentry( 3, has_solar_pack_on, -1, _( "Attach cable to solar pack" ) ); + kmenu.addentry( 1, paying_out && !solar_pack && !UPS, -1, _( "Attach loose end to vehicle" ) ); + kmenu.addentry( 2, !cable_cbm, -1, _( "Attach loose end to self" ) ); + if( wearing_solar_pack ) { + kmenu.addentry( 3, !solar_pack && !paying_out && !UPS, -1, _( "Attach loose end to solar pack" ) ); + } + if( has_ups ){ + kmenu.addentry( 4, !UPS && !solar_pack && !paying_out, -1, _( "Attach loose end to UPS" ) ); } } kmenu.query(); @@ -8520,26 +8542,52 @@ int iuse::cable_attach( player *p, item *it, bool, const tripoint & ) if( choice < 0 ) { return 0; // we did nothing. - } else if( choice == 1 ) { + } else if( choice == 0 ) { // unconnect & respool it->reset_cable( p ); return 0; - } else if( choice == 2 ) { - // connecting self to backpack or car + } else if( choice == 2 ) { // connect self while other end already connected + p->add_msg_if_player( m_info, _( "You attach the cable to the Cable Charger System." ) ); + // connecting self, solar backpack connected if( solar_pack ) { set_cable_active( p, it, "solar_pack_link" ); + p->add_msg_if_player( m_good, _( "You will now charge power from the solar backpack." ) ); return 0; } + // connecting self, UPS connected + if( UPS ) { + set_cable_active( p, it, "UPS_link" ); + p->add_msg_if_player( m_good, _( "You will now charge power from the UPS." ) ); + return 0; + } + // connecting self, vehicle connected const optional_vpart_position source_vp = confirm_source_vehicle( p, it, true ); if( veh_pointer_or_null( source_vp ) != nullptr ) { set_cable_active( p, it, "cable_charger_link" ); + p->add_msg_if_player( m_good, _( "You will now charge power from the vehicle." ) ); } return 0; } else if( choice == 3 ) { - // connecting self to backpack + // connecting self to solar backpack set_cable_active( p, it, "solar_pack_link" ); + p->add_msg_if_player( m_good, _( "You will now charge power from the solar backpack." ) ); + return 0; + } else if( choice == 4 ){ + // connecting self to UPS + int pos = g->inv_for_filter( _( "Choose UPS:" ), [&]( const item & itm ) { + return itm.has_flag( "IS_UPS" ); }, _( "You don't have any UPS." ) ); + if( pos == INT_MIN ) { + add_msg( _( "Never mind" ) ); + return 0; + } + item &chosen = p->i_at( pos ); + chosen.set_var( "cable", "plugged_in" ); + chosen.activate(); + set_cable_active( p, it, "UPS_link" ); + p->add_msg_if_player( m_info, _( "You attach the cable to the UPS." ) ); + p->add_msg_if_player( m_good, _( "You will now charge power from the UPS." ) ); return 0; } - + // connecting self to vehicle const optional_vpart_position source_vp = confirm_source_vehicle( p, it, paying_out ); vehicle *const source_veh = veh_pointer_or_null( source_vp ); if( source_veh == nullptr && paying_out ) { @@ -8562,6 +8610,7 @@ int iuse::cable_attach( player *p, item *it, bool, const tripoint & ) it->set_var( "source_y", abspos.y ); it->set_var( "source_z", g->get_levz() ); set_cable_active( p, it, "cable_charger_link" ); + p->add_msg_if_player( m_good, _( "You will now charge power from the vehicle." ) ); return 0; } else { vehicle *const target_veh = &target_vp->vehicle(); diff --git a/src/player.cpp b/src/player.cpp index cb63f52019466..a3945f788eb25 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -4245,14 +4245,6 @@ void player::update_needs( int rate_multiplier ) if( has_bionic( bn_bio_solar ) ) { charge_power( rate_multiplier * 25 ); } - if( has_active_bionic( bionic_id( "bio_cable" ) ) ) { - if( is_wearing( "solarpack_on" ) ) { - charge_power( rate_multiplier * 25 ); - } - if( is_wearing( "q_solarpack_on" ) ) { - charge_power( rate_multiplier * 50 ); - } - } } // Huge folks take penalties for cramming themselves in vehicles From 0c03f36d4dcfa6bc7bb975ee22d37adbaeb9f96c Mon Sep 17 00:00:00 2001 From: nexusmrsep Date: Mon, 26 Aug 2019 18:57:49 +0200 Subject: [PATCH 2/7] descriptions, fixes, astyle, cleanup --- data/json/bionics.json | 2 +- data/json/items/tools.json | 2 +- data/json/items/vehicle/cables.json | 4 +- src/bionics.cpp | 63 +++++++++++++++++++---------- src/item.cpp | 11 ++--- src/iuse.cpp | 26 ++++++------ 6 files changed, 66 insertions(+), 42 deletions(-) diff --git a/data/json/bionics.json b/data/json/bionics.json index 6103a74050fbe..2b30534fe74f1 100644 --- a/data/json/bionics.json +++ b/data/json/bionics.json @@ -137,7 +137,7 @@ "type": "bionic", "name": "Cable Charger System", "capacity": 10, - "description": "You have a complex port surgically mounted above your hip. While active, it will recharge bionic power when connected to a battery via jumper cable.", + "description": "You have a complex port surgically mounted above your hip. While active, it will recharge bionic power when connected to a power source via jumper cable.", "occupied_bodyparts": [ [ "TORSO", 10 ] ], "flags": [ "BIONIC_POWER_SOURCE", "BIONIC_SHOCKPROOF", "BIONIC_TOGGLED" ] }, diff --git a/data/json/items/tools.json b/data/json/items/tools.json index 5df9b8a45c75a..ce1a066099a76 100644 --- a/data/json/items/tools.json +++ b/data/json/items/tools.json @@ -108,7 +108,7 @@ "type": "TOOL", "name": "UPS", "name_plural": "UPS's", - "description": "This is a unified power supply, or UPS. It is a device developed jointly by military and scientific interests for use in combat and the field. The UPS is designed to power armor and some guns, but drains batteries quickly.", + "description": "This is a unified power supply, or UPS. It is a device developed jointly by military and scientific interests for use in combat and the field. The UPS is designed to power bionics, armor and some guns, but drains batteries quickly.", "weight": 680, "volume": "2500 ml", "price": 280000, diff --git a/data/json/items/vehicle/cables.json b/data/json/items/vehicle/cables.json index 258fb9cde7389..47a6ab8ba8539 100644 --- a/data/json/items/vehicle/cables.json +++ b/data/json/items/vehicle/cables.json @@ -3,7 +3,7 @@ "type": "TOOL", "id": "jumper_cable", "name": "jumper cable", - "description": "A jumper cable, like you've seen many times before: it's a short multi-stranded copper cable with power leads on either end, whose purpose is to share power between vehicles.", + "description": "A jumper cable, like you've seen many times before: it's a short multi-stranded copper cable with power leads on either end, whose main purpose is to share power between vehicles, but can also link other electrical systems.", "to_hit": 1, "color": "light_blue", "symbol": "&", @@ -22,7 +22,7 @@ "type": "TOOL", "id": "jumper_cable_heavy", "name": "heavy-duty cable", - "description": "A long, thick, heavy-duty cable with power leads on either end. It looks like you could use it to hook up two vehicles to each other, though you expect the power loss would be noticeable.", + "description": "A long, thick, heavy-duty cable with power leads on either end. It looks like you could use it to hook up two vehicles to each other, though you expect the power loss would be noticeable. Can also link other electrical systems.", "volume": "1500 ml", "weight": 750, "max_charges": 20, diff --git a/src/bionics.cpp b/src/bionics.cpp index d3c623a98ee6e..865bd017aa91c 100644 --- a/src/bionics.cpp +++ b/src/bionics.cpp @@ -670,28 +670,49 @@ bool player::activate_bionic( int b, bool eff_only ) reactor_plut = 0; } } else if( bio.id == "bio_cable" ) { - bool has_cable = has_item_with( []( const item & it ) { - return it.active && it.has_flag( "CABLE_SPOOL" ); - } ); - bool has_connected_cable = has_item_with( []( const item & it ) { - return it.active && it.has_flag( "CABLE_SPOOL" ) && it.get_var( "state" ) == "solar_pack_link"; + std::vector cables = items_with( []( const item & it ) { + return it.has_flag( "CABLE_SPOOL" ); } ); - + bool has_cable = cables.size() > 0; + bool free_cable = false; if( !has_cable ) { add_msg_if_player( m_info, - _( "You need a jumper cable connected to a vehicle to drain power from it." ) ); - } - if( is_wearing( "solarpack_on" ) || is_wearing( "q_solarpack_on" ) ) { - if( has_connected_cable ) { - add_msg_if_player( m_info, _( "Your plugged-in solar pack is now able to charge" - " your system." ) ); - } else { - add_msg_if_player( m_info, _( "You need to connect the cable to yourself and the solar pack" - " before your solar pack can charge your system." ) ); + _( "You need a jumper cable connected to a power source to drain power from it." ) ); + } else { + for( item *cable : cables ) { + const std::string state = cable->get_var( "state" ); + if( state == "cable_charger" ) { + add_msg_if_player( m_info, + _( "Cable is plugged-in to the CBM but it has to be also connected to the power source." ) ); + } + if( state == "cable_charger_link" ) { + add_msg_if_player( m_info, + _( "You are plugged to the vehicle. It will charge you if it has some juice in it." ) ); + } + if( state == "solar_pack_link" ) { + add_msg_if_player( m_info, + _( "You are plugged to a solar pack. It will charge you if it's unfolded and in sunlight." ) ); + } + if( state == "UPS_link" ) { + add_msg_if_player( m_info, + _( "You are plugged to a UPS. It will charge you if it has some juice in it." ) ); + } + if( state == "solar_pack" || state == "UPS" ) { + add_msg_if_player( m_info, + _( "You have a cable plugged to a portable power source, but you need to plug it in to the CBM." ) ); + } + if( state == "pay_oyt_cable" ) { + add_msg_if_player( m_info, + _( "You have a cable plugged to a vehicle, but you need to plug it in to the CBM." ) ); + } + if( state == "attach_first" ) { + free_cable = true; + } } - } else if( is_wearing( "solarpack" ) || is_wearing( "q_solarpack" ) ) { - add_msg_if_player( m_info, _( "You might plug in your solar pack to the cable charging" - " system, if you unfold it." ) ); + } + if( free_cable ) { + add_msg_if_player( m_info, + _( "You have at least one free cable in your inventory that you could use to plug yourself in." ) ); } } @@ -973,11 +994,11 @@ void player::process_bionic( int b ) for( const item *cable : cables ) { const cata::optional target = cable->get_cable_target( this, pos() ); if( !target ) { - if( g->m.is_outside( pos() ) && !is_night( calendar::turn ) && cable->get_var( "state" ) == "solar_pack_link" ) { + if( g->m.is_outside( pos() ) && !is_night( calendar::turn ) && + cable->get_var( "state" ) == "solar_pack_link" ) { double modifier = g->natural_light_level( pos().z ) / default_daylight_level(); // basic solar panel produces 50W = 1 charge/20_seconds = 180 charges/hour(3600) if( is_wearing( "solarpack_on" ) && x_in_y( 180 * modifier, 3600 ) ) { - add_msg( m_bad, "MOD: %f", modifier * 180 ); charge_power( 1 ); } // quantum solar backpack = solar panel x6 @@ -987,7 +1008,7 @@ void player::process_bionic( int b ) } if( cable->get_var( "state" ) == "UPS_link" ) { static const item_filter used_ups = [&]( const item & itm ) { - return itm.get_var( "cable" ) == "plugged_in"; + return itm.get_var( "cable" ) == "plugged_in"; }; if( has_charges( "UPS_off", 1, used_ups ) ) { use_charges( "UPS_off", 1, used_ups ); diff --git a/src/item.cpp b/src/item.cpp index 1cb81d363b88d..e8689c74fd664 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8209,7 +8209,7 @@ bool item::process_cable( player *p, const tripoint &pos ) reset_cable( p ); } std::string state = get_var( "state" ); - if( state == "solar_pack_link" ) { + if( state == "solar_pack_link" || state == "solar_pack" ) { if( !p->has_item( *this ) || ( !p->is_wearing( "solarpack_on" ) || !p->is_wearing( "solarpack_on" ) ) ) { p->add_msg_if_player( m_bad, _( "You notice the cable has come loose!" ) ); @@ -8219,8 +8219,8 @@ bool item::process_cable( player *p, const tripoint &pos ) } static const item_filter used_ups = [&]( const item & itm ) { - return itm.get_var( "cable" ) == "plugged_in"; - }; + return itm.get_var( "cable" ) == "plugged_in"; + }; if( state == "UPS" ) { if( !p->has_item( *this ) || ( !p->has_item_with( used_ups ) ) ) { @@ -8284,9 +8284,10 @@ bool item::process_UPS( player *p, const tripoint & /*pos*/ ) return false; } bool has_connected_cable = p->has_item_with( []( const item & it ) { - return it.active && it.has_flag( "CABLE_SPOOL" ) && ( it.get_var( "state" ) == "UPS_link" || it.get_var( "state" ) == " UPS " ); + return it.active && it.has_flag( "CABLE_SPOOL" ) && ( it.get_var( "state" ) == "UPS_link" || + it.get_var( "state" ) == " UPS " ); } ); - if ( !has_connected_cable ) { + if( !has_connected_cable ) { erase_var( "cable" ); active = false; } diff --git a/src/iuse.cpp b/src/iuse.cpp index 13b2a53e23c43..d66fec11cc67a 100644 --- a/src/iuse.cpp +++ b/src/iuse.cpp @@ -8483,7 +8483,8 @@ int iuse::cable_attach( player *p, item *it, bool, const tripoint & ) return 0; } else if( choice == 3 ) { int pos = g->inv_for_filter( _( "Choose UPS:" ), [&]( const item & itm ) { - return itm.has_flag( "IS_UPS" ); }, _( "You don't have any UPS." ) ); + return itm.has_flag( "IS_UPS" ); + }, _( "You don't have any UPS." ) ); if( pos == INT_MIN ) { add_msg( _( "Never mind" ) ); return 0; @@ -8540,12 +8541,13 @@ int iuse::cable_attach( player *p, item *it, bool, const tripoint & ) kmenu.text = _( "Using cable:" ); kmenu.addentry( 0, true, -1, _( "Detach and re-spool the cable" ) ); if( has_bio_cable && loose_ends ) { - kmenu.addentry( 1, paying_out && !solar_pack && !UPS, -1, _( "Attach loose end to vehicle" ) ); + kmenu.addentry( 1, ( paying_out || cable_cbm ) && !solar_pack && + !UPS, -1, _( "Attach loose end to vehicle" ) ); kmenu.addentry( 2, !cable_cbm, -1, _( "Attach loose end to self" ) ); if( wearing_solar_pack ) { kmenu.addentry( 3, !solar_pack && !paying_out && !UPS, -1, _( "Attach loose end to solar pack" ) ); } - if( has_ups ){ + if( has_ups ) { kmenu.addentry( 4, !UPS && !solar_pack && !paying_out, -1, _( "Attach loose end to UPS" ) ); } } @@ -8562,31 +8564,32 @@ int iuse::cable_attach( player *p, item *it, bool, const tripoint & ) // connecting self, solar backpack connected if( solar_pack ) { set_cable_active( p, it, "solar_pack_link" ); - p->add_msg_if_player( m_good, _( "You will now charge power from the solar backpack." ) ); + p->add_msg_if_player( m_good, _( "You are now plugged to the solar backpack." ) ); return 0; } // connecting self, UPS connected if( UPS ) { set_cable_active( p, it, "UPS_link" ); - p->add_msg_if_player( m_good, _( "You will now charge power from the UPS." ) ); + p->add_msg_if_player( m_good, _( "You are now plugged to the UPS." ) ); return 0; } // connecting self, vehicle connected const optional_vpart_position source_vp = confirm_source_vehicle( p, it, true ); if( veh_pointer_or_null( source_vp ) != nullptr ) { set_cable_active( p, it, "cable_charger_link" ); - p->add_msg_if_player( m_good, _( "You will now charge power from the vehicle." ) ); + p->add_msg_if_player( m_good, _( "You are now plugged to the vehicle." ) ); } return 0; } else if( choice == 3 ) { // connecting self to solar backpack set_cable_active( p, it, "solar_pack_link" ); - p->add_msg_if_player( m_good, _( "You will now charge power from the solar backpack." ) ); + p->add_msg_if_player( m_good, _( "You are now plugged to the solar backpack." ) ); return 0; - } else if( choice == 4 ){ + } else if( choice == 4 ) { // connecting self to UPS int pos = g->inv_for_filter( _( "Choose UPS:" ), [&]( const item & itm ) { - return itm.has_flag( "IS_UPS" ); }, _( "You don't have any UPS." ) ); + return itm.has_flag( "IS_UPS" ); + }, _( "You don't have any UPS." ) ); if( pos == INT_MIN ) { add_msg( _( "Never mind" ) ); return 0; @@ -8595,8 +8598,7 @@ int iuse::cable_attach( player *p, item *it, bool, const tripoint & ) chosen.set_var( "cable", "plugged_in" ); chosen.activate(); set_cable_active( p, it, "UPS_link" ); - p->add_msg_if_player( m_info, _( "You attach the cable to the UPS." ) ); - p->add_msg_if_player( m_good, _( "You will now charge power from the UPS." ) ); + p->add_msg_if_player( m_good, _( "You are now plugged to the UPS." ) ); return 0; } // connecting self to vehicle @@ -8622,7 +8624,7 @@ int iuse::cable_attach( player *p, item *it, bool, const tripoint & ) it->set_var( "source_y", abspos.y ); it->set_var( "source_z", g->get_levz() ); set_cable_active( p, it, "cable_charger_link" ); - p->add_msg_if_player( m_good, _( "You will now charge power from the vehicle." ) ); + p->add_msg_if_player( m_good, _( "You are now plugged to the vehicle." ) ); return 0; } else { vehicle *const target_veh = &target_vp->vehicle(); From 45e9d00826df44349f5494fdb97bb859dfb29479 Mon Sep 17 00:00:00 2001 From: nexusmrsep Date: Mon, 26 Aug 2019 19:05:03 +0200 Subject: [PATCH 3/7] p==nullptr check in cable processing --- src/item.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/item.cpp b/src/item.cpp index e8689c74fd664..0167e92385e9f 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8207,6 +8207,7 @@ bool item::process_cable( player *p, const tripoint &pos ) { if( p == nullptr ) { reset_cable( p ); + return false; } std::string state = get_var( "state" ); if( state == "solar_pack_link" || state == "solar_pack" ) { From d8e832f5b0cc89f56ca6e2df8214ef2be4948ef7 Mon Sep 17 00:00:00 2001 From: nexusmrsep <39925111+nexusmrsep@users.noreply.github.com> Date: Mon, 26 Aug 2019 20:41:08 +0200 Subject: [PATCH 4/7] Update src/item.cpp Co-Authored-By: BevapDin --- src/item.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/item.cpp b/src/item.cpp index 0167e92385e9f..ead590f3b59c0 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8286,7 +8286,7 @@ bool item::process_UPS( player *p, const tripoint & /*pos*/ ) } bool has_connected_cable = p->has_item_with( []( const item & it ) { return it.active && it.has_flag( "CABLE_SPOOL" ) && ( it.get_var( "state" ) == "UPS_link" || - it.get_var( "state" ) == " UPS " ); + it.get_var( "state" ) == "UPS" ); } ); if( !has_connected_cable ) { erase_var( "cable" ); From 8a6c63f102e70f95dd44f0a417734b16871511c4 Mon Sep 17 00:00:00 2001 From: nexusmrsep Date: Mon, 26 Aug 2019 22:25:48 +0200 Subject: [PATCH 5/7] var naming convention unification, etc. for clang-tidy --- src/bionics.cpp | 2 +- src/item.cpp | 42 +++++++++++++++++++++--------------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/bionics.cpp b/src/bionics.cpp index 865bd017aa91c..9cf13c6dbda66 100644 --- a/src/bionics.cpp +++ b/src/bionics.cpp @@ -673,7 +673,7 @@ bool player::activate_bionic( int b, bool eff_only ) std::vector cables = items_with( []( const item & it ) { return it.has_flag( "CABLE_SPOOL" ); } ); - bool has_cable = cables.size() > 0; + bool has_cable = cables.empty(); bool free_cable = false; if( !has_cable ) { add_msg_if_player( m_info, diff --git a/src/item.cpp b/src/item.cpp index ead590f3b59c0..0c8a01c1116f0 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8203,18 +8203,18 @@ cata::optional item::get_cable_target( player *p, const tripoint &pos return g->m.getlocal( source ); } -bool item::process_cable( player *p, const tripoint &pos ) +bool item::process_cable( player *carrier, const tripoint &pos ) { - if( p == nullptr ) { - reset_cable( p ); + if( carrier == nullptr ) { + reset_cable( carrier ); return false; } std::string state = get_var( "state" ); if( state == "solar_pack_link" || state == "solar_pack" ) { - if( !p->has_item( *this ) || ( !p->is_wearing( "solarpack_on" ) || - !p->is_wearing( "solarpack_on" ) ) ) { - p->add_msg_if_player( m_bad, _( "You notice the cable has come loose!" ) ); - reset_cable( p ); + if( !carrier->has_item( *this ) || ( !carrier->is_wearing( "solarpack_on" ) || + !carrier->is_wearing( "solarpack_on" ) ) ) { + carrier->add_msg_if_player( m_bad, _( "You notice the cable has come loose!" ) ); + reset_cable( carrier ); return false; } } @@ -8224,25 +8224,25 @@ bool item::process_cable( player *p, const tripoint &pos ) }; if( state == "UPS" ) { - if( !p->has_item( *this ) || ( !p->has_item_with( used_ups ) ) ) { - p->add_msg_if_player( m_bad, _( "You notice the cable has come loose!" ) ); - for( item *used : p->items_with( used_ups ) ) { + if( !carrier->has_item( *this ) || ( !carrier->has_item_with( used_ups ) ) ) { + carrier->add_msg_if_player( m_bad, _( "You notice the cable has come loose!" ) ); + for( item *used : carrier->items_with( used_ups ) ) { used->erase_var( "cable" ); } - reset_cable( p ); + reset_cable( carrier ); return false; } } - const cata::optional source = get_cable_target( p, pos ); + const cata::optional source = get_cable_target( carrier, pos ); if( !source ) { return false; } if( !g->m.veh_at( *source ) || ( source->z != g->get_levz() && !g->m.has_zlevels() ) ) { - if( p != nullptr && p->has_item( *this ) ) { - p->add_msg_if_player( m_bad, _( "You notice the cable has come loose!" ) ); + if( carrier != nullptr && carrier->has_item( *this ) ) { + carrier->add_msg_if_player( m_bad, _( "You notice the cable has come loose!" ) ); } - reset_cable( p ); + reset_cable( carrier ); return false; } @@ -8251,10 +8251,10 @@ bool item::process_cable( player *p, const tripoint &pos ) charges = max_charges - distance; if( charges < 1 ) { - if( p != nullptr && p->has_item( *this ) ) { - p->add_msg_if_player( m_bad, _( "The over-extended cable breaks loose!" ) ); + if( carrier != nullptr && carrier->has_item( *this ) ) { + carrier->add_msg_if_player( m_bad, _( "The over-extended cable breaks loose!" ) ); } - reset_cable( p ); + reset_cable( carrier ); } return false; @@ -8277,14 +8277,14 @@ void item::reset_cable( player *p ) } } -bool item::process_UPS( player *p, const tripoint & /*pos*/ ) +bool item::process_UPS( player *carrier, const tripoint & /*pos*/ ) { - if( p == nullptr ) { + if( carrier == nullptr ) { erase_var( "cable" ); active = false; return false; } - bool has_connected_cable = p->has_item_with( []( const item & it ) { + bool has_connected_cable = carrier->has_item_with( []( const item & it ) { return it.active && it.has_flag( "CABLE_SPOOL" ) && ( it.get_var( "state" ) == "UPS_link" || it.get_var( "state" ) == "UPS" ); } ); From 9d7080c3c5b5934bafd023865616100bdcda6ce2 Mon Sep 17 00:00:00 2001 From: nexusmrsep <39925111+nexusmrsep@users.noreply.github.com> Date: Tue, 27 Aug 2019 17:03:01 +0200 Subject: [PATCH 6/7] Apply suggestions from code review Co-Authored-By: BevapDin --- src/bionics.cpp | 2 +- src/item.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bionics.cpp b/src/bionics.cpp index 9cf13c6dbda66..a477542793547 100644 --- a/src/bionics.cpp +++ b/src/bionics.cpp @@ -673,7 +673,7 @@ bool player::activate_bionic( int b, bool eff_only ) std::vector cables = items_with( []( const item & it ) { return it.has_flag( "CABLE_SPOOL" ); } ); - bool has_cable = cables.empty(); + bool has_cable = !cables.empty(); bool free_cable = false; if( !has_cable ) { add_msg_if_player( m_info, diff --git a/src/item.cpp b/src/item.cpp index 0c8a01c1116f0..9834af39b823f 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8224,7 +8224,7 @@ bool item::process_cable( player *carrier, const tripoint &pos ) }; if( state == "UPS" ) { - if( !carrier->has_item( *this ) || ( !carrier->has_item_with( used_ups ) ) ) { + if( !carrier->has_item( *this ) || !carrier->has_item_with( used_ups ) ) { carrier->add_msg_if_player( m_bad, _( "You notice the cable has come loose!" ) ); for( item *used : carrier->items_with( used_ups ) ) { used->erase_var( "cable" ); From 73058ec395ea80bffef1015d2f01f2cc46087315 Mon Sep 17 00:00:00 2001 From: nexusmrsep Date: Tue, 27 Aug 2019 17:05:36 +0200 Subject: [PATCH 7/7] typo fix --- src/item.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/item.cpp b/src/item.cpp index 0c8a01c1116f0..8a2af85a0e764 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8212,7 +8212,7 @@ bool item::process_cable( player *carrier, const tripoint &pos ) std::string state = get_var( "state" ); if( state == "solar_pack_link" || state == "solar_pack" ) { if( !carrier->has_item( *this ) || ( !carrier->is_wearing( "solarpack_on" ) || - !carrier->is_wearing( "solarpack_on" ) ) ) { + !carrier->is_wearing( "q_solarpack_on" ) ) ) { carrier->add_msg_if_player( m_bad, _( "You notice the cable has come loose!" ) ); reset_cable( carrier ); return false;