From 7e65d5919319a4a3c84d4edc8e0b765d8aab8a99 Mon Sep 17 00:00:00 2001 From: Gregor Anders Date: Sat, 21 Dec 2019 11:29:46 +0100 Subject: [PATCH 01/15] Initial code for improvements of the water faucet. * if a water faucet is used it offers all comestible liquids contained in the vehicles tanks * if it is installed in a tile where also a hotplate is present it will heat up liquids that have the appropriate flag * if it is installed in a tile where also a freezer/fridge is present it will cool down liquids that have the appropriate flag * while heating up or cooling down it does consume energy --- src/vehicle.h | 7 ++ src/vehicle_use.cpp | 161 ++++++++++++++++++++++++++++++-- tests/vehicle_interact_test.cpp | 129 +++++++++++++++++++++++++ 3 files changed, 290 insertions(+), 7 deletions(-) diff --git a/src/vehicle.h b/src/vehicle.h index 7b49dd1972845..f4cbe7fa2bf10 100644 --- a/src/vehicle.h +++ b/src/vehicle.h @@ -149,6 +149,8 @@ int cmps_to_vmiph( int cmps ); int vmiph_to_cmps( int vmiph ); static constexpr float accel_g = 9.81f; +using item_count_tuple = std::tuple; + /** * Structure, describing vehicle part (ie, wheel, seat) */ @@ -1582,6 +1584,11 @@ class vehicle void use_bike_rack( int part ); void use_harness( int part, const tripoint &pos ); + std::vector get_comestible_liquids(); + item_count_tuple select_comestible_liquid(const std::vector &comestible_liquids, const std::string &message); + void use_fill_container(const item_count_tuple &comestible_liquid, bool has_hotplate, bool has_freezer); + void use_faucet(const item_count_tuple &comestible_liquid, bool has_hotplate, bool has_freezer); + void interact_with( const tripoint &pos, int interact_part ); std::string disp_name() const; diff --git a/src/vehicle_use.cpp b/src/vehicle_use.cpp index 5fa06a8184dd1..77bbd7291b594 100644 --- a/src/vehicle_use.cpp +++ b/src/vehicle_use.cpp @@ -1744,6 +1744,152 @@ void vehicle::use_harness( int part, const tripoint &pos ) } } +std::vector vehicle::get_comestible_liquids() +{ + std::vector liquids; + std::map liquids_map; + + liquids.clear(); + liquids_map.clear(); + + for ( auto &part : parts) + { + const auto remaining = part.ammo_remaining(); + + if (part.is_tank() && !part.base.contents_made_of( SOLID ) && remaining > 0) + { + const auto food = part.base.get_food(); + + if (food != nullptr && food->is_comestible()) + { + const auto comestible = food->get_comestible(); + + if (comestible != nullptr && "DRINK" == comestible->comesttype) + { + const auto liquid_id = part.ammo_current(); + + if (!liquid_id.empty() && liquid_id != "null" && liquid_id != "water") + { + if (liquids_map.find(food) == liquids_map.end()) + { + liquids_map[food] = static_cast(remaining); + } + else + { + liquids_map[food] += static_cast(remaining); + } + } + } + } + } + } + + for (auto &liquid : liquids_map) + { + liquids.push_back(item_count_tuple(liquid.first, liquid.second)); + } + + std::sort(liquids.begin(), liquids.end(), + [](const item_count_tuple &a, const item_count_tuple &b) + { + return std::get<1>(a) > std::get<1>(b); + }); + + return liquids; +} + +item_count_tuple vehicle::select_comestible_liquid(const std::vector &liquids, const std::string &message) +{ + uilist selectmenu; + int choice = std::numeric_limits::min(); + int idx = 0; + + if (!liquids.empty()) + { + for (auto &liquid : liquids) + { + const item* p_item = std::get<0>(liquid); + + if (p_item != nullptr) + { + selectmenu.addentry( idx++, true, MENU_AUTOASSIGN, p_item->tname( std::get<1>(liquid)) ); + } + } + + if( selectmenu.entries.size() == 1 ) + { + choice = selectmenu.entries.front().retval; + } + else + { + selectmenu.text = message; + selectmenu.query(); + choice = selectmenu.ret; + } + + if (choice >= 0 && choice < idx) + { + return liquids.at(static_cast(choice)); + } + } + + return item_count_tuple(nullptr, 0); +} + +void vehicle::use_fill_container(const item_count_tuple &item_tuple, bool, bool) +{ + item *p_item = std::get<0>(item_tuple); + + if (p_item && p_item->is_comestible()) + { + itype_id liquid_id = p_item->typeId(); + + g->u.siphon( *this, liquid_id ); + } +} + +void vehicle::use_faucet(const item_count_tuple &item_tuple, bool has_hotplate, bool has_freezer) +{ + item *p_item = std::get<0>(item_tuple); + + if (p_item && p_item->is_comestible()) + { + itype_id liquid_id = p_item->typeId(); + item liquid(liquid_id, calendar::turn, 1); + + int battery_fuel = fuel_left( "battery", true ); + + if (p_item->has_flag("EATEN_HOT") && !p_item->has_flag("HOT") + && has_hotplate && p_item->type) + { + int required_charges = p_item->type->charges_to_use(); + + if (required_charges <= battery_fuel) + { + discharge_battery( required_charges ); + liquid.heat_up(); + } + } + else if (p_item->has_flag("EATEN_COLD") && !p_item->has_flag("COLD") + && has_freezer && p_item->type) + { + int required_charges = p_item->type->charges_to_use(); + + if (required_charges <= battery_fuel) + { + discharge_battery( required_charges ); + liquid.cold_up(); + } + } + + if( g->u.eat( liquid ) ) + { + drain( liquid_id, 1 ); + g->u.moves -= 250; + } + } +} + void vehicle::use_bike_rack( int part ) { if( parts[part].is_unavailable() || parts[part].removed ) { @@ -1888,6 +2034,9 @@ void vehicle::interact_with( const tripoint &pos, int interact_part ) avail_part_with_feature( interact_part, "ADVANCED_PLANTER", true ) >= 0; const int workbench_part = avail_part_with_feature( interact_part, "WORKBENCH", true ); const bool has_workbench = workbench_part >= 0; + const std::vector comestible_liquids = get_comestible_liquids(); + const bool has_freezer = avail_part_with_feature( interact_part, "FREEZER", true ) >= 0; + const bool has_fridge = avail_part_with_feature( interact_part, "FRIDGE", true ) >= 0; enum { EXAMINE, TRACK, CONTROL, CONTROL_ELECTRONICS, GET_ITEMS, GET_ITEMS_ON_GROUND, FOLD_VEHICLE, UNLOAD_TURRET, RELOAD_TURRET, @@ -1941,7 +2090,7 @@ void vehicle::interact_with( const tripoint &pos, int interact_part ) if( ( has_kitchen || has_chemlab ) && fuel_left( "battery", true ) > 0 ) { selectmenu.addentry( USE_HOTPLATE, true, 'h', _( "Use the hotplate" ) ); } - if( has_faucet && fuel_left( "water_clean" ) > 0 ) { + if( has_faucet && !comestible_liquids.empty() ) { selectmenu.addentry( FILL_CONTAINER, true, 'c', _( "Fill a container with water" ) ); selectmenu.addentry( DRINK, true, 'd', _( "Have a drink" ) ); } @@ -2041,15 +2190,13 @@ void vehicle::interact_with( const tripoint &pos, int interact_part ) return; } case FILL_CONTAINER: { - g->u.siphon( *this, "water_clean" ); + item_count_tuple item_tuple = select_comestible_liquid(comestible_liquids, _( "Select a liquid" )); + use_fill_container(item_tuple, (has_chemlab || has_kitchen), (has_fridge || has_freezer)); return; } case DRINK: { - item water( "water_clean", 0 ); - if( g->u.eat( water ) ) { - drain( "water_clean", 1 ); - g->u.moves -= 250; - } + item_count_tuple item_tuple = select_comestible_liquid(comestible_liquids, _( "Select a drink" )); + use_faucet(item_tuple, (has_chemlab || has_kitchen), (has_fridge || has_freezer)); return; } case USE_WELDER: { diff --git a/tests/vehicle_interact_test.cpp b/tests/vehicle_interact_test.cpp index d916ba0a68aa9..5fa698e51f831 100644 --- a/tests/vehicle_interact_test.cpp +++ b/tests/vehicle_interact_test.cpp @@ -97,3 +97,132 @@ TEST_CASE( "repair_vehicle_part" ) test_repair( tools, false ); } } + +TEST_CASE("water faucet offers comestible liquids", "[water_faucet]") +{ + const tripoint test_origin( 60, 60, 0 ); + const tripoint vehicle_origin = test_origin + tripoint_north; + int battery_charge = 10000; + + clear_player(); + clear_map(); + + g->place_player( test_origin ); + + vehicle *veh_ptr = g->m.add_vehicle( vproto_id( "none" ), vehicle_origin, 0, 0, 0 ); + REQUIRE( veh_ptr != nullptr ); + + REQUIRE( veh_ptr->install_part( point_zero, vpart_id( "storage_battery" ), true ) >= 0 ); + veh_ptr->charge_battery( battery_charge ); + + REQUIRE( veh_ptr->install_part( point_zero, vpart_id( "tank_medium" ), true ) >= 0 ); + REQUIRE( veh_ptr->install_part( point_zero + point_north, vpart_id( "tank_medium" ), true ) >= 0 ); + REQUIRE( veh_ptr->install_part( point_zero + point_east, vpart_id( "tank_medium" ), true ) >= 0 ); + REQUIRE( veh_ptr->install_part( point_zero + point_west, vpart_id( "tank_medium" ), true ) >= 0 ); + + std::vector parts; + + vehicle_part* battery_ptr = nullptr; + + for( auto &part : veh_ptr->parts ) + { + if (part.is_tank()) + { + parts.push_back(std::addressof(part)); + } + if (part.is_battery()) + { + battery_ptr = std::addressof(part); + } + } + + REQUIRE( battery_ptr != nullptr ); + REQUIRE( parts.size() == 4 ); + + std::vector liquids = veh_ptr->get_comestible_liquids(); + + REQUIRE(liquids.empty()); + + item item_water(itype_id("water"), calendar::turn, 4); + item item_water_clean(itype_id("water_clean"), calendar::turn, 4); + item item_pine_tea(itype_id("pine_tea"), calendar::turn, 4); + + parts[0]->ammo_set(item_water.typeId(), item_water.charges); + parts[1]->ammo_set(item_water_clean.typeId(), item_water_clean.charges); + parts[2]->ammo_set(item_pine_tea.typeId(), item_pine_tea.charges); + + liquids = veh_ptr->get_comestible_liquids(); + REQUIRE(liquids.size() == 2); + + // clean water + REQUIRE(item_water_clean.charges == 4); + + veh_ptr->use_faucet(item_count_tuple(std::addressof(item_water_clean), item_water_clean.charges--), false, false); + + REQUIRE(parts[1]->ammo_remaining() == item_water_clean.charges); + REQUIRE(battery_ptr->ammo_remaining() == battery_charge); + + REQUIRE(item_water_clean.charges == 3); + + veh_ptr->use_faucet(item_count_tuple(std::addressof(item_water_clean), item_water_clean.charges--), true, true); + battery_charge--; + + REQUIRE(parts[1]->ammo_remaining() == item_water_clean.charges); + REQUIRE(battery_ptr->ammo_remaining() == battery_charge); + + REQUIRE(item_water_clean.charges == 2); + + veh_ptr->use_faucet(item_count_tuple(std::addressof(item_water_clean), item_water_clean.charges--), true, false); + + REQUIRE(parts[1]->ammo_remaining() == item_water_clean.charges); + REQUIRE(battery_ptr->ammo_remaining() == battery_charge); + + REQUIRE(item_water_clean.charges == 1); + + veh_ptr->use_faucet(item_count_tuple(std::addressof(item_water_clean), item_water_clean.charges--), false, true); + battery_charge--; + + REQUIRE(parts[1]->ammo_remaining() == item_water_clean.charges); + REQUIRE(battery_ptr->ammo_remaining() == battery_charge); + + REQUIRE(item_water_clean.charges == 0); + + liquids = veh_ptr->get_comestible_liquids(); + REQUIRE(liquids.size() == 1); + + // pine needle tea + REQUIRE(item_pine_tea.charges == 4); + + veh_ptr->use_faucet(item_count_tuple(std::addressof(item_pine_tea), item_pine_tea.charges--), false, false); + + REQUIRE(parts[2]->ammo_remaining() == item_pine_tea.charges); + REQUIRE(battery_ptr->ammo_remaining() == battery_charge); + + REQUIRE(item_pine_tea.charges == 3); + + veh_ptr->use_faucet(item_count_tuple(std::addressof(item_pine_tea), item_pine_tea.charges--), true, true); + battery_charge--; + + REQUIRE(parts[2]->ammo_remaining() == item_pine_tea.charges); + REQUIRE(battery_ptr->ammo_remaining() == battery_charge); + + REQUIRE(item_pine_tea.charges == 2); + + veh_ptr->use_faucet(item_count_tuple(std::addressof(item_pine_tea), item_pine_tea.charges--), true, false); + battery_charge--; + + REQUIRE(parts[2]->ammo_remaining() == item_pine_tea.charges); + REQUIRE(battery_ptr->ammo_remaining() == battery_charge); + + REQUIRE(item_pine_tea.charges == 1); + + veh_ptr->use_faucet(item_count_tuple(std::addressof(item_pine_tea), item_pine_tea.charges--), false, true); + + REQUIRE(parts[2]->ammo_remaining() == item_pine_tea.charges); + REQUIRE(battery_ptr->ammo_remaining() == battery_charge); + + REQUIRE(item_pine_tea.charges == 0); + + liquids = veh_ptr->get_comestible_liquids(); + REQUIRE(liquids.size() == 0); +} From 77fd4f765e670a7d230113c8d9222aa706effa1e Mon Sep 17 00:00:00 2001 From: Gregor Anders Date: Sat, 21 Dec 2019 12:34:00 +0100 Subject: [PATCH 02/15] Changed format and adjusted code according to comments. --- src/vehicle_use.cpp | 113 +++++++++++++------------------- tests/vehicle_interact_test.cpp | 12 ++-- 2 files changed, 50 insertions(+), 75 deletions(-) diff --git a/src/vehicle_use.cpp b/src/vehicle_use.cpp index 77bbd7291b594..ecf8272695641 100644 --- a/src/vehicle_use.cpp +++ b/src/vehicle_use.cpp @@ -1744,91 +1744,80 @@ void vehicle::use_harness( int part, const tripoint &pos ) } } -std::vector vehicle::get_comestible_liquids() -{ +std::vector vehicle::get_comestible_liquids() { std::vector liquids; std::map liquids_map; liquids.clear(); liquids_map.clear(); - for ( auto &part : parts) - { - const auto remaining = part.ammo_remaining(); + for ( auto &part : parts) { + const int remaining = part.ammo_remaining(); - if (part.is_tank() && !part.base.contents_made_of( SOLID ) && remaining > 0) - { - const auto food = part.base.get_food(); + if (!part.is_tank() || part.base.contents_made_of( SOLID ) || remaining <= 0) { + continue; + } - if (food != nullptr && food->is_comestible()) - { - const auto comestible = food->get_comestible(); + item *food_ptr = part.base.get_food(); - if (comestible != nullptr && "DRINK" == comestible->comesttype) - { - const auto liquid_id = part.ammo_current(); + if (food_ptr == nullptr || !food_ptr->is_comestible()) { + continue; + } - if (!liquid_id.empty() && liquid_id != "null" && liquid_id != "water") - { - if (liquids_map.find(food) == liquids_map.end()) - { - liquids_map[food] = static_cast(remaining); - } - else - { - liquids_map[food] += static_cast(remaining); - } - } - } - } + const cata::value_ptr comestible = food_ptr->get_comestible(); + + if (comestible == nullptr || "DRINK" != comestible->comesttype) { + continue; + } + + const itype_id liquid_id = part.ammo_current(); + + if (liquid_id.empty() || liquid_id == "null" || liquid_id == "water") { + continue; + } + + if (liquids_map.find(food_ptr) == liquids_map.end()) { + liquids_map[food_ptr] = static_cast(remaining); + } else { + liquids_map[food_ptr] += static_cast(remaining); } } - for (auto &liquid : liquids_map) - { + for (auto &liquid : liquids_map) { liquids.push_back(item_count_tuple(liquid.first, liquid.second)); } std::sort(liquids.begin(), liquids.end(), - [](const item_count_tuple &a, const item_count_tuple &b) - { + [](const item_count_tuple &a, const item_count_tuple &b) { return std::get<1>(a) > std::get<1>(b); }); return liquids; } -item_count_tuple vehicle::select_comestible_liquid(const std::vector &liquids, const std::string &message) -{ +item_count_tuple vehicle::select_comestible_liquid(const std::vector &liquids, const std::string &message) { uilist selectmenu; int choice = std::numeric_limits::min(); int idx = 0; - if (!liquids.empty()) - { - for (auto &liquid : liquids) - { + if (!liquids.empty()) { + for (auto &liquid : liquids) { const item* p_item = std::get<0>(liquid); - if (p_item != nullptr) - { - selectmenu.addentry( idx++, true, MENU_AUTOASSIGN, p_item->tname( std::get<1>(liquid)) ); + if (p_item != nullptr) { + selectmenu.addentry( idx++, true, MENU_AUTOASSIGN, p_item->tname( std::get<1>(liquid)) ); } } - if( selectmenu.entries.size() == 1 ) - { + if( selectmenu.entries.size() == 1 ) { choice = selectmenu.entries.front().retval; - } - else - { + } else { selectmenu.text = message; selectmenu.query(); choice = selectmenu.ret; } - if (choice >= 0 && choice < idx) - { + if (choice >= 0 && choice < idx) { return liquids.at(static_cast(choice)); } } @@ -1836,54 +1825,44 @@ item_count_tuple vehicle::select_comestible_liquid(const std::vector(item_tuple); - if (p_item && p_item->is_comestible()) - { + if (p_item && p_item->is_comestible()) { itype_id liquid_id = p_item->typeId(); g->u.siphon( *this, liquid_id ); } } -void vehicle::use_faucet(const item_count_tuple &item_tuple, bool has_hotplate, bool has_freezer) -{ +void vehicle::use_faucet(const item_count_tuple &item_tuple, bool has_hotplate, bool has_freezer) { item *p_item = std::get<0>(item_tuple); - if (p_item && p_item->is_comestible()) - { + if (p_item && p_item->is_comestible()) { itype_id liquid_id = p_item->typeId(); item liquid(liquid_id, calendar::turn, 1); int battery_fuel = fuel_left( "battery", true ); if (p_item->has_flag("EATEN_HOT") && !p_item->has_flag("HOT") - && has_hotplate && p_item->type) - { + && has_hotplate && p_item->type) { int required_charges = p_item->type->charges_to_use(); - if (required_charges <= battery_fuel) - { + if (required_charges <= battery_fuel) { discharge_battery( required_charges ); liquid.heat_up(); } - } - else if (p_item->has_flag("EATEN_COLD") && !p_item->has_flag("COLD") - && has_freezer && p_item->type) - { + } else if (p_item->has_flag("EATEN_COLD") && !p_item->has_flag("COLD") + && has_freezer && p_item->type) { int required_charges = p_item->type->charges_to_use(); - if (required_charges <= battery_fuel) - { + if (required_charges <= battery_fuel) { discharge_battery( required_charges ); liquid.cold_up(); } } - if( g->u.eat( liquid ) ) - { + if( g->u.eat( liquid ) ) { drain( liquid_id, 1 ); g->u.moves -= 250; } diff --git a/tests/vehicle_interact_test.cpp b/tests/vehicle_interact_test.cpp index 5fa698e51f831..e476b8fca2cef 100644 --- a/tests/vehicle_interact_test.cpp +++ b/tests/vehicle_interact_test.cpp @@ -98,8 +98,7 @@ TEST_CASE( "repair_vehicle_part" ) } } -TEST_CASE("water faucet offers comestible liquids", "[water_faucet]") -{ +TEST_CASE("water faucet offers comestible liquids", "[water_faucet]") { const tripoint test_origin( 60, 60, 0 ); const tripoint vehicle_origin = test_origin + tripoint_north; int battery_charge = 10000; @@ -124,14 +123,11 @@ TEST_CASE("water faucet offers comestible liquids", "[water_faucet]") vehicle_part* battery_ptr = nullptr; - for( auto &part : veh_ptr->parts ) - { - if (part.is_tank()) - { + for( auto &part : veh_ptr->parts ) { + if (part.is_tank()) { parts.push_back(std::addressof(part)); } - if (part.is_battery()) - { + if (part.is_battery()) { battery_ptr = std::addressof(part); } } From f3aaf3f088cb77b16244ef87743291d6262a93a5 Mon Sep 17 00:00:00 2001 From: Gregor Anders Date: Sat, 21 Dec 2019 13:41:13 +0100 Subject: [PATCH 03/15] Used astyle to correct formatting. --- src/vehicle.h | 10 +- src/vehicle_use.cpp | 190 ++++++++++++++++---------------- tests/vehicle_interact_test.cpp | 115 ++++++++++--------- 3 files changed, 166 insertions(+), 149 deletions(-) diff --git a/src/vehicle.h b/src/vehicle.h index f4cbe7fa2bf10..55faf91b72d87 100644 --- a/src/vehicle.h +++ b/src/vehicle.h @@ -149,7 +149,7 @@ int cmps_to_vmiph( int cmps ); int vmiph_to_cmps( int vmiph ); static constexpr float accel_g = 9.81f; -using item_count_tuple = std::tuple; +using item_count_tuple = std::tuple; /** * Structure, describing vehicle part (ie, wheel, seat) @@ -1585,9 +1585,11 @@ class vehicle void use_harness( int part, const tripoint &pos ); std::vector get_comestible_liquids(); - item_count_tuple select_comestible_liquid(const std::vector &comestible_liquids, const std::string &message); - void use_fill_container(const item_count_tuple &comestible_liquid, bool has_hotplate, bool has_freezer); - void use_faucet(const item_count_tuple &comestible_liquid, bool has_hotplate, bool has_freezer); + item_count_tuple select_comestible_liquid( const std::vector &comestible_liquids, + const std::string &message ); + void use_fill_container( const item_count_tuple &comestible_liquid, bool has_hotplate, + bool has_freezer ); + void use_faucet( const item_count_tuple &comestible_liquid, bool has_hotplate, bool has_freezer ); void interact_with( const tripoint &pos, int interact_part ); diff --git a/src/vehicle_use.cpp b/src/vehicle_use.cpp index ecf8272695641..be31989218093 100644 --- a/src/vehicle_use.cpp +++ b/src/vehicle_use.cpp @@ -1744,129 +1744,134 @@ void vehicle::use_harness( int part, const tripoint &pos ) } } -std::vector vehicle::get_comestible_liquids() { - std::vector liquids; - std::map liquids_map; +std::vector vehicle::get_comestible_liquids() +{ + std::vector liquids; + std::map liquids_map; - liquids.clear(); - liquids_map.clear(); + liquids.clear(); + liquids_map.clear(); - for ( auto &part : parts) { - const int remaining = part.ammo_remaining(); + for( auto &part : parts ) { + const int remaining = part.ammo_remaining(); - if (!part.is_tank() || part.base.contents_made_of( SOLID ) || remaining <= 0) { - continue; - } + if( !part.is_tank() || part.base.contents_made_of( SOLID ) || remaining <= 0 ) { + continue; + } - item *food_ptr = part.base.get_food(); + item *food_ptr = part.base.get_food(); - if (food_ptr == nullptr || !food_ptr->is_comestible()) { - continue; - } + if( food_ptr == nullptr || !food_ptr->is_comestible() ) { + continue; + } - const cata::value_ptr comestible = food_ptr->get_comestible(); + const cata::value_ptr comestible = food_ptr->get_comestible(); - if (comestible == nullptr || "DRINK" != comestible->comesttype) { - continue; - } + if( comestible == nullptr || "DRINK" != comestible->comesttype ) { + continue; + } - const itype_id liquid_id = part.ammo_current(); + const itype_id liquid_id = part.ammo_current(); - if (liquid_id.empty() || liquid_id == "null" || liquid_id == "water") { - continue; - } + if( liquid_id.empty() || liquid_id == "null" || liquid_id == "water" ) { + continue; + } - if (liquids_map.find(food_ptr) == liquids_map.end()) { - liquids_map[food_ptr] = static_cast(remaining); - } else { - liquids_map[food_ptr] += static_cast(remaining); + if( liquids_map.find( food_ptr ) == liquids_map.end() ) { + liquids_map[food_ptr] = static_cast( remaining ); + } else { + liquids_map[food_ptr] += static_cast( remaining ); + } } - } - for (auto &liquid : liquids_map) { - liquids.push_back(item_count_tuple(liquid.first, liquid.second)); - } + for( auto &liquid : liquids_map ) { + liquids.push_back( item_count_tuple( liquid.first, liquid.second ) ); + } - std::sort(liquids.begin(), liquids.end(), - [](const item_count_tuple &a, const item_count_tuple &b) { - return std::get<1>(a) > std::get<1>(b); - }); + std::sort( liquids.begin(), liquids.end(), + []( const item_count_tuple & a, const item_count_tuple & b ) { + return std::get<1>( a ) > std::get<1>( b ); + } ); - return liquids; + return liquids; } -item_count_tuple vehicle::select_comestible_liquid(const std::vector &liquids, const std::string &message) { - uilist selectmenu; - int choice = std::numeric_limits::min(); - int idx = 0; +item_count_tuple vehicle::select_comestible_liquid( const std::vector &liquids, + const std::string &message ) +{ + uilist selectmenu; + int choice = std::numeric_limits::min(); + int idx = 0; - if (!liquids.empty()) { - for (auto &liquid : liquids) { - const item* p_item = std::get<0>(liquid); + if( !liquids.empty() ) { + for( auto &liquid : liquids ) { + const item *p_item = std::get<0>( liquid ); - if (p_item != nullptr) { - selectmenu.addentry( idx++, true, MENU_AUTOASSIGN, p_item->tname( std::get<1>(liquid)) ); - } - } + if( p_item != nullptr ) { + selectmenu.addentry( idx++, true, MENU_AUTOASSIGN, p_item->tname( std::get<1>( liquid ) ) ); + } + } - if( selectmenu.entries.size() == 1 ) { - choice = selectmenu.entries.front().retval; - } else { - selectmenu.text = message; - selectmenu.query(); - choice = selectmenu.ret; - } + if( selectmenu.entries.size() == 1 ) { + choice = selectmenu.entries.front().retval; + } else { + selectmenu.text = message; + selectmenu.query(); + choice = selectmenu.ret; + } - if (choice >= 0 && choice < idx) { - return liquids.at(static_cast(choice)); + if( choice >= 0 && choice < idx ) { + return liquids.at( static_cast( choice ) ); + } } - } - return item_count_tuple(nullptr, 0); + return item_count_tuple( nullptr, 0 ); } -void vehicle::use_fill_container(const item_count_tuple &item_tuple, bool, bool) { - item *p_item = std::get<0>(item_tuple); +void vehicle::use_fill_container( const item_count_tuple &item_tuple, bool, bool ) +{ + item *p_item = std::get<0>( item_tuple ); - if (p_item && p_item->is_comestible()) { - itype_id liquid_id = p_item->typeId(); + if( p_item && p_item->is_comestible() ) { + itype_id liquid_id = p_item->typeId(); - g->u.siphon( *this, liquid_id ); - } + g->u.siphon( *this, liquid_id ); + } } -void vehicle::use_faucet(const item_count_tuple &item_tuple, bool has_hotplate, bool has_freezer) { - item *p_item = std::get<0>(item_tuple); +void vehicle::use_faucet( const item_count_tuple &item_tuple, bool has_hotplate, bool has_freezer ) +{ + item *p_item = std::get<0>( item_tuple ); - if (p_item && p_item->is_comestible()) { - itype_id liquid_id = p_item->typeId(); - item liquid(liquid_id, calendar::turn, 1); + if( p_item && p_item->is_comestible() ) { + itype_id liquid_id = p_item->typeId(); + item liquid( liquid_id, calendar::turn, 1 ); - int battery_fuel = fuel_left( "battery", true ); + int battery_fuel = fuel_left( "battery", true ); - if (p_item->has_flag("EATEN_HOT") && !p_item->has_flag("HOT") - && has_hotplate && p_item->type) { - int required_charges = p_item->type->charges_to_use(); + if( p_item->has_flag( "EATEN_HOT" ) && !p_item->has_flag( "HOT" ) + && has_hotplate && p_item->type ) { + int required_charges = p_item->type->charges_to_use(); - if (required_charges <= battery_fuel) { - discharge_battery( required_charges ); - liquid.heat_up(); - } - } else if (p_item->has_flag("EATEN_COLD") && !p_item->has_flag("COLD") - && has_freezer && p_item->type) { - int required_charges = p_item->type->charges_to_use(); + if( required_charges <= battery_fuel ) { + discharge_battery( required_charges ); + liquid.heat_up(); + } + } else if( p_item->has_flag( "EATEN_COLD" ) && !p_item->has_flag( "COLD" ) + && has_freezer && p_item->type ) { + int required_charges = p_item->type->charges_to_use(); - if (required_charges <= battery_fuel) { - discharge_battery( required_charges ); - liquid.cold_up(); - } - } + if( required_charges <= battery_fuel ) { + discharge_battery( required_charges ); + liquid.cold_up(); + } + } - if( g->u.eat( liquid ) ) { - drain( liquid_id, 1 ); - g->u.moves -= 250; + if( g->u.eat( liquid ) ) { + drain( liquid_id, 1 ); + g->u.moves -= 250; + } } - } } void vehicle::use_bike_rack( int part ) @@ -2169,13 +2174,14 @@ void vehicle::interact_with( const tripoint &pos, int interact_part ) return; } case FILL_CONTAINER: { - item_count_tuple item_tuple = select_comestible_liquid(comestible_liquids, _( "Select a liquid" )); - use_fill_container(item_tuple, (has_chemlab || has_kitchen), (has_fridge || has_freezer)); + item_count_tuple item_tuple = select_comestible_liquid( comestible_liquids, + _( "Select a liquid" ) ); + use_fill_container( item_tuple, ( has_chemlab || has_kitchen ), ( has_fridge || has_freezer ) ); return; } case DRINK: { - item_count_tuple item_tuple = select_comestible_liquid(comestible_liquids, _( "Select a drink" )); - use_faucet(item_tuple, (has_chemlab || has_kitchen), (has_fridge || has_freezer)); + item_count_tuple item_tuple = select_comestible_liquid( comestible_liquids, _( "Select a drink" ) ); + use_faucet( item_tuple, ( has_chemlab || has_kitchen ), ( has_fridge || has_freezer ) ); return; } case USE_WELDER: { diff --git a/tests/vehicle_interact_test.cpp b/tests/vehicle_interact_test.cpp index e476b8fca2cef..6a5177ee0c834 100644 --- a/tests/vehicle_interact_test.cpp +++ b/tests/vehicle_interact_test.cpp @@ -98,7 +98,8 @@ TEST_CASE( "repair_vehicle_part" ) } } -TEST_CASE("water faucet offers comestible liquids", "[water_faucet]") { +TEST_CASE( "water faucet offers comestible liquids", "[water_faucet]" ) +{ const tripoint test_origin( 60, 60, 0 ); const tripoint vehicle_origin = test_origin + tripoint_north; int battery_charge = 10000; @@ -119,17 +120,17 @@ TEST_CASE("water faucet offers comestible liquids", "[water_faucet]") { REQUIRE( veh_ptr->install_part( point_zero + point_east, vpart_id( "tank_medium" ), true ) >= 0 ); REQUIRE( veh_ptr->install_part( point_zero + point_west, vpart_id( "tank_medium" ), true ) >= 0 ); - std::vector parts; + std::vector parts; - vehicle_part* battery_ptr = nullptr; + vehicle_part *battery_ptr = nullptr; for( auto &part : veh_ptr->parts ) { - if (part.is_tank()) { - parts.push_back(std::addressof(part)); - } - if (part.is_battery()) { - battery_ptr = std::addressof(part); - } + if( part.is_tank() ) { + parts.push_back( std::addressof( part ) ); + } + if( part.is_battery() ) { + battery_ptr = std::addressof( part ); + } } REQUIRE( battery_ptr != nullptr ); @@ -137,88 +138,96 @@ TEST_CASE("water faucet offers comestible liquids", "[water_faucet]") { std::vector liquids = veh_ptr->get_comestible_liquids(); - REQUIRE(liquids.empty()); + REQUIRE( liquids.empty() ); - item item_water(itype_id("water"), calendar::turn, 4); - item item_water_clean(itype_id("water_clean"), calendar::turn, 4); - item item_pine_tea(itype_id("pine_tea"), calendar::turn, 4); + item item_water( itype_id( "water" ), calendar::turn, 4 ); + item item_water_clean( itype_id( "water_clean" ), calendar::turn, 4 ); + item item_pine_tea( itype_id( "pine_tea" ), calendar::turn, 4 ); - parts[0]->ammo_set(item_water.typeId(), item_water.charges); - parts[1]->ammo_set(item_water_clean.typeId(), item_water_clean.charges); - parts[2]->ammo_set(item_pine_tea.typeId(), item_pine_tea.charges); + parts[0]->ammo_set( item_water.typeId(), item_water.charges ); + parts[1]->ammo_set( item_water_clean.typeId(), item_water_clean.charges ); + parts[2]->ammo_set( item_pine_tea.typeId(), item_pine_tea.charges ); liquids = veh_ptr->get_comestible_liquids(); - REQUIRE(liquids.size() == 2); + REQUIRE( liquids.size() == 2 ); // clean water - REQUIRE(item_water_clean.charges == 4); + REQUIRE( item_water_clean.charges == 4 ); - veh_ptr->use_faucet(item_count_tuple(std::addressof(item_water_clean), item_water_clean.charges--), false, false); + veh_ptr->use_faucet( item_count_tuple( std::addressof( item_water_clean ), + item_water_clean.charges-- ), false, false ); - REQUIRE(parts[1]->ammo_remaining() == item_water_clean.charges); - REQUIRE(battery_ptr->ammo_remaining() == battery_charge); + REQUIRE( parts[1]->ammo_remaining() == item_water_clean.charges ); + REQUIRE( battery_ptr->ammo_remaining() == battery_charge ); - REQUIRE(item_water_clean.charges == 3); + REQUIRE( item_water_clean.charges == 3 ); - veh_ptr->use_faucet(item_count_tuple(std::addressof(item_water_clean), item_water_clean.charges--), true, true); + veh_ptr->use_faucet( item_count_tuple( std::addressof( item_water_clean ), + item_water_clean.charges-- ), true, true ); battery_charge--; - REQUIRE(parts[1]->ammo_remaining() == item_water_clean.charges); - REQUIRE(battery_ptr->ammo_remaining() == battery_charge); + REQUIRE( parts[1]->ammo_remaining() == item_water_clean.charges ); + REQUIRE( battery_ptr->ammo_remaining() == battery_charge ); - REQUIRE(item_water_clean.charges == 2); + REQUIRE( item_water_clean.charges == 2 ); - veh_ptr->use_faucet(item_count_tuple(std::addressof(item_water_clean), item_water_clean.charges--), true, false); + veh_ptr->use_faucet( item_count_tuple( std::addressof( item_water_clean ), + item_water_clean.charges-- ), true, false ); - REQUIRE(parts[1]->ammo_remaining() == item_water_clean.charges); - REQUIRE(battery_ptr->ammo_remaining() == battery_charge); + REQUIRE( parts[1]->ammo_remaining() == item_water_clean.charges ); + REQUIRE( battery_ptr->ammo_remaining() == battery_charge ); - REQUIRE(item_water_clean.charges == 1); + REQUIRE( item_water_clean.charges == 1 ); - veh_ptr->use_faucet(item_count_tuple(std::addressof(item_water_clean), item_water_clean.charges--), false, true); + veh_ptr->use_faucet( item_count_tuple( std::addressof( item_water_clean ), + item_water_clean.charges-- ), false, true ); battery_charge--; - REQUIRE(parts[1]->ammo_remaining() == item_water_clean.charges); - REQUIRE(battery_ptr->ammo_remaining() == battery_charge); + REQUIRE( parts[1]->ammo_remaining() == item_water_clean.charges ); + REQUIRE( battery_ptr->ammo_remaining() == battery_charge ); - REQUIRE(item_water_clean.charges == 0); + REQUIRE( item_water_clean.charges == 0 ); liquids = veh_ptr->get_comestible_liquids(); - REQUIRE(liquids.size() == 1); + REQUIRE( liquids.size() == 1 ); // pine needle tea - REQUIRE(item_pine_tea.charges == 4); + REQUIRE( item_pine_tea.charges == 4 ); - veh_ptr->use_faucet(item_count_tuple(std::addressof(item_pine_tea), item_pine_tea.charges--), false, false); + veh_ptr->use_faucet( item_count_tuple( std::addressof( item_pine_tea ), item_pine_tea.charges-- ), + false, false ); - REQUIRE(parts[2]->ammo_remaining() == item_pine_tea.charges); - REQUIRE(battery_ptr->ammo_remaining() == battery_charge); + REQUIRE( parts[2]->ammo_remaining() == item_pine_tea.charges ); + REQUIRE( battery_ptr->ammo_remaining() == battery_charge ); - REQUIRE(item_pine_tea.charges == 3); + REQUIRE( item_pine_tea.charges == 3 ); - veh_ptr->use_faucet(item_count_tuple(std::addressof(item_pine_tea), item_pine_tea.charges--), true, true); + veh_ptr->use_faucet( item_count_tuple( std::addressof( item_pine_tea ), item_pine_tea.charges-- ), + true, true ); battery_charge--; - REQUIRE(parts[2]->ammo_remaining() == item_pine_tea.charges); - REQUIRE(battery_ptr->ammo_remaining() == battery_charge); + REQUIRE( parts[2]->ammo_remaining() == item_pine_tea.charges ); + REQUIRE( battery_ptr->ammo_remaining() == battery_charge ); - REQUIRE(item_pine_tea.charges == 2); + REQUIRE( item_pine_tea.charges == 2 ); - veh_ptr->use_faucet(item_count_tuple(std::addressof(item_pine_tea), item_pine_tea.charges--), true, false); + veh_ptr->use_faucet( item_count_tuple( std::addressof( item_pine_tea ), item_pine_tea.charges-- ), + true, false ); battery_charge--; - REQUIRE(parts[2]->ammo_remaining() == item_pine_tea.charges); - REQUIRE(battery_ptr->ammo_remaining() == battery_charge); + REQUIRE( parts[2]->ammo_remaining() == item_pine_tea.charges ); + REQUIRE( battery_ptr->ammo_remaining() == battery_charge ); - REQUIRE(item_pine_tea.charges == 1); + REQUIRE( item_pine_tea.charges == 1 ); - veh_ptr->use_faucet(item_count_tuple(std::addressof(item_pine_tea), item_pine_tea.charges--), false, true); + veh_ptr->use_faucet( item_count_tuple( std::addressof( item_pine_tea ), item_pine_tea.charges-- ), + false, true ); - REQUIRE(parts[2]->ammo_remaining() == item_pine_tea.charges); - REQUIRE(battery_ptr->ammo_remaining() == battery_charge); + REQUIRE( parts[2]->ammo_remaining() == item_pine_tea.charges ); + REQUIRE( battery_ptr->ammo_remaining() == battery_charge ); - REQUIRE(item_pine_tea.charges == 0); + REQUIRE( item_pine_tea.charges == 0 ); liquids = veh_ptr->get_comestible_liquids(); - REQUIRE(liquids.size() == 0); + REQUIRE( liquids.size() == 0 ); } From 33c554c5f553de2c1926ede76f197a226411e5e2 Mon Sep 17 00:00:00 2001 From: Gregor Anders Date: Sat, 21 Dec 2019 13:58:43 +0100 Subject: [PATCH 04/15] Update src/vehicle_use.cpp Co-Authored-By: BevapDin --- src/vehicle_use.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vehicle_use.cpp b/src/vehicle_use.cpp index be31989218093..ecf1ab5fd8292 100644 --- a/src/vehicle_use.cpp +++ b/src/vehicle_use.cpp @@ -1765,7 +1765,7 @@ std::vector vehicle::get_comestible_liquids() continue; } - const cata::value_ptr comestible = food_ptr->get_comestible(); + const cata::value_ptr &comestible = food_ptr->get_comestible(); if( comestible == nullptr || "DRINK" != comestible->comesttype ) { continue; From a70a0529254d7a657bf23ad78c35cf66b0344927 Mon Sep 17 00:00:00 2001 From: Gregor Anders Date: Sat, 21 Dec 2019 14:03:31 +0100 Subject: [PATCH 05/15] Update src/vehicle_use.cpp Co-Authored-By: BevapDin --- src/vehicle_use.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vehicle_use.cpp b/src/vehicle_use.cpp index ecf1ab5fd8292..20ab967c315b8 100644 --- a/src/vehicle_use.cpp +++ b/src/vehicle_use.cpp @@ -1785,7 +1785,7 @@ std::vector vehicle::get_comestible_liquids() } for( auto &liquid : liquids_map ) { - liquids.push_back( item_count_tuple( liquid.first, liquid.second ) ); + liquids.emplace_back( liquid.first, liquid.second ); } std::sort( liquids.begin(), liquids.end(), From 3b745e649b1f06c2e0a1fdc044eb1b00e5909df2 Mon Sep 17 00:00:00 2001 From: Gregor Anders Date: Sat, 21 Dec 2019 14:03:43 +0100 Subject: [PATCH 06/15] Update src/vehicle_use.cpp Co-Authored-By: BevapDin --- src/vehicle_use.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vehicle_use.cpp b/src/vehicle_use.cpp index 20ab967c315b8..63c919d6a8e37 100644 --- a/src/vehicle_use.cpp +++ b/src/vehicle_use.cpp @@ -1803,7 +1803,7 @@ item_count_tuple vehicle::select_comestible_liquid( const std::vector::min(); int idx = 0; - if( !liquids.empty() ) { + if( liquids.empty() ) { return ...; } for( auto &liquid : liquids ) { const item *p_item = std::get<0>( liquid ); From 4fa8e37239f9d6a0b023d9b5938b240301312fca Mon Sep 17 00:00:00 2001 From: Gregor Anders Date: Sat, 21 Dec 2019 14:36:48 +0100 Subject: [PATCH 07/15] Changes according to code review. --- src/vehicle.h | 4 +-- src/vehicle_use.cpp | 64 ++++++++++++++++----------------- tests/vehicle_interact_test.cpp | 32 ++++++++--------- 3 files changed, 50 insertions(+), 50 deletions(-) diff --git a/src/vehicle.h b/src/vehicle.h index 55faf91b72d87..49b75c7e20409 100644 --- a/src/vehicle.h +++ b/src/vehicle.h @@ -1587,9 +1587,9 @@ class vehicle std::vector get_comestible_liquids(); item_count_tuple select_comestible_liquid( const std::vector &comestible_liquids, const std::string &message ); - void use_fill_container( const item_count_tuple &comestible_liquid, bool has_hotplate, + void use_fill_container( const item *comestible_liquid, bool has_hotplate, bool has_freezer ); - void use_faucet( const item_count_tuple &comestible_liquid, bool has_hotplate, bool has_freezer ); + void use_faucet( const item *comestible_liquid, bool has_hotplate, bool has_freezer ); void interact_with( const tripoint &pos, int interact_part ); diff --git a/src/vehicle_use.cpp b/src/vehicle_use.cpp index ecf1ab5fd8292..2f39137e92f3c 100644 --- a/src/vehicle_use.cpp +++ b/src/vehicle_use.cpp @@ -1765,27 +1765,23 @@ std::vector vehicle::get_comestible_liquids() continue; } - const cata::value_ptr &comestible = food_ptr->get_comestible(); + const islot_comestible &comestible = *food_ptr->get_comestible(); - if( comestible == nullptr || "DRINK" != comestible->comesttype ) { + if( "DRINK" != comestible.comesttype ) { continue; } const itype_id liquid_id = part.ammo_current(); - if( liquid_id.empty() || liquid_id == "null" || liquid_id == "water" ) { + if( liquid_id == "null" || liquid_id == "water" ) { continue; } - if( liquids_map.find( food_ptr ) == liquids_map.end() ) { - liquids_map[food_ptr] = static_cast( remaining ); - } else { - liquids_map[food_ptr] += static_cast( remaining ); - } + liquids_map[food_ptr] += static_cast( remaining ); } for( auto &liquid : liquids_map ) { - liquids.push_back( item_count_tuple( liquid.first, liquid.second ) ); + liquids.emplace_back( liquid.first, liquid.second ); } std::sort( liquids.begin(), liquids.end(), @@ -1806,10 +1802,7 @@ item_count_tuple vehicle::select_comestible_liquid( const std::vector( liquid ); - - if( p_item != nullptr ) { - selectmenu.addentry( idx++, true, MENU_AUTOASSIGN, p_item->tname( std::get<1>( liquid ) ) ); - } + selectmenu.addentry( idx++, true, MENU_AUTOASSIGN, p_item->tname( std::get<1>( liquid ) ) ); } if( selectmenu.entries.size() == 1 ) { @@ -1821,45 +1814,45 @@ item_count_tuple vehicle::select_comestible_liquid( const std::vector= 0 && choice < idx ) { - return liquids.at( static_cast( choice ) ); + try { + return liquids[static_cast( choice )]; + } catch( ... ) { + debugmsg( "Warning! Invalid comestible liquid index" ); + } } } return item_count_tuple( nullptr, 0 ); } -void vehicle::use_fill_container( const item_count_tuple &item_tuple, bool, bool ) +void vehicle::use_fill_container( const item *comestibale_ptr, bool, bool ) { - item *p_item = std::get<0>( item_tuple ); - - if( p_item && p_item->is_comestible() ) { - itype_id liquid_id = p_item->typeId(); + if( comestibale_ptr && comestibale_ptr->is_comestible() ) { + itype_id liquid_id = comestibale_ptr->typeId(); g->u.siphon( *this, liquid_id ); } } -void vehicle::use_faucet( const item_count_tuple &item_tuple, bool has_hotplate, bool has_freezer ) +void vehicle::use_faucet( const item *comestible_ptr, bool has_hotplate, bool has_freezer ) { - item *p_item = std::get<0>( item_tuple ); - - if( p_item && p_item->is_comestible() ) { - itype_id liquid_id = p_item->typeId(); + if( comestible_ptr && comestible_ptr->is_comestible() ) { + itype_id liquid_id = comestible_ptr->typeId(); item liquid( liquid_id, calendar::turn, 1 ); int battery_fuel = fuel_left( "battery", true ); - if( p_item->has_flag( "EATEN_HOT" ) && !p_item->has_flag( "HOT" ) - && has_hotplate && p_item->type ) { - int required_charges = p_item->type->charges_to_use(); + if( comestible_ptr->has_flag( "EATEN_HOT" ) && !comestible_ptr->has_flag( "HOT" ) + && has_hotplate && comestible_ptr->type ) { + int required_charges = comestible_ptr->type->charges_to_use(); if( required_charges <= battery_fuel ) { discharge_battery( required_charges ); liquid.heat_up(); } - } else if( p_item->has_flag( "EATEN_COLD" ) && !p_item->has_flag( "COLD" ) - && has_freezer && p_item->type ) { - int required_charges = p_item->type->charges_to_use(); + } else if( comestible_ptr->has_flag( "EATEN_COLD" ) && !comestible_ptr->has_flag( "COLD" ) + && has_freezer && comestible_ptr->type ) { + int required_charges = comestible_ptr->type->charges_to_use(); if( required_charges <= battery_fuel ) { discharge_battery( required_charges ); @@ -2176,12 +2169,19 @@ void vehicle::interact_with( const tripoint &pos, int interact_part ) case FILL_CONTAINER: { item_count_tuple item_tuple = select_comestible_liquid( comestible_liquids, _( "Select a liquid" ) ); - use_fill_container( item_tuple, ( has_chemlab || has_kitchen ), ( has_fridge || has_freezer ) ); + const item *comestible_ptr = std::get<0>( item_tuple ); + if( comestible_ptr ) { + use_fill_container( std::get<0>( item_tuple ), ( has_chemlab || has_kitchen ), ( has_fridge || + has_freezer ) ); + } return; } case DRINK: { item_count_tuple item_tuple = select_comestible_liquid( comestible_liquids, _( "Select a drink" ) ); - use_faucet( item_tuple, ( has_chemlab || has_kitchen ), ( has_fridge || has_freezer ) ); + const item *comestible_ptr = std::get<0>( item_tuple ); + if( comestible_ptr ) { + use_faucet( comestible_ptr, ( has_chemlab || has_kitchen ), ( has_fridge || has_freezer ) ); + } return; } case USE_WELDER: { diff --git a/tests/vehicle_interact_test.cpp b/tests/vehicle_interact_test.cpp index 6a5177ee0c834..86510e1243c64 100644 --- a/tests/vehicle_interact_test.cpp +++ b/tests/vehicle_interact_test.cpp @@ -154,16 +154,16 @@ TEST_CASE( "water faucet offers comestible liquids", "[water_faucet]" ) // clean water REQUIRE( item_water_clean.charges == 4 ); - veh_ptr->use_faucet( item_count_tuple( std::addressof( item_water_clean ), - item_water_clean.charges-- ), false, false ); + veh_ptr->use_faucet( std::addressof( item_water_clean ), false, false ); + item_water_clean.charges--; REQUIRE( parts[1]->ammo_remaining() == item_water_clean.charges ); REQUIRE( battery_ptr->ammo_remaining() == battery_charge ); REQUIRE( item_water_clean.charges == 3 ); - veh_ptr->use_faucet( item_count_tuple( std::addressof( item_water_clean ), - item_water_clean.charges-- ), true, true ); + veh_ptr->use_faucet( std::addressof( item_water_clean ), true, true ); + item_water_clean.charges--; battery_charge--; REQUIRE( parts[1]->ammo_remaining() == item_water_clean.charges ); @@ -171,16 +171,16 @@ TEST_CASE( "water faucet offers comestible liquids", "[water_faucet]" ) REQUIRE( item_water_clean.charges == 2 ); - veh_ptr->use_faucet( item_count_tuple( std::addressof( item_water_clean ), - item_water_clean.charges-- ), true, false ); + veh_ptr->use_faucet( std::addressof( item_water_clean ), true, false ); + item_water_clean.charges--; REQUIRE( parts[1]->ammo_remaining() == item_water_clean.charges ); REQUIRE( battery_ptr->ammo_remaining() == battery_charge ); REQUIRE( item_water_clean.charges == 1 ); - veh_ptr->use_faucet( item_count_tuple( std::addressof( item_water_clean ), - item_water_clean.charges-- ), false, true ); + veh_ptr->use_faucet( std::addressof( item_water_clean ), false, true ); + item_water_clean.charges--; battery_charge--; REQUIRE( parts[1]->ammo_remaining() == item_water_clean.charges ); @@ -194,16 +194,16 @@ TEST_CASE( "water faucet offers comestible liquids", "[water_faucet]" ) // pine needle tea REQUIRE( item_pine_tea.charges == 4 ); - veh_ptr->use_faucet( item_count_tuple( std::addressof( item_pine_tea ), item_pine_tea.charges-- ), - false, false ); + veh_ptr->use_faucet( std::addressof( item_pine_tea ), false, false ); + item_pine_tea.charges--; REQUIRE( parts[2]->ammo_remaining() == item_pine_tea.charges ); REQUIRE( battery_ptr->ammo_remaining() == battery_charge ); REQUIRE( item_pine_tea.charges == 3 ); - veh_ptr->use_faucet( item_count_tuple( std::addressof( item_pine_tea ), item_pine_tea.charges-- ), - true, true ); + veh_ptr->use_faucet( std::addressof( item_pine_tea ), true, true ); + item_pine_tea.charges--; battery_charge--; REQUIRE( parts[2]->ammo_remaining() == item_pine_tea.charges ); @@ -211,8 +211,8 @@ TEST_CASE( "water faucet offers comestible liquids", "[water_faucet]" ) REQUIRE( item_pine_tea.charges == 2 ); - veh_ptr->use_faucet( item_count_tuple( std::addressof( item_pine_tea ), item_pine_tea.charges-- ), - true, false ); + veh_ptr->use_faucet( std::addressof( item_pine_tea ), true, false ); + item_pine_tea.charges--; battery_charge--; REQUIRE( parts[2]->ammo_remaining() == item_pine_tea.charges ); @@ -220,8 +220,8 @@ TEST_CASE( "water faucet offers comestible liquids", "[water_faucet]" ) REQUIRE( item_pine_tea.charges == 1 ); - veh_ptr->use_faucet( item_count_tuple( std::addressof( item_pine_tea ), item_pine_tea.charges-- ), - false, true ); + veh_ptr->use_faucet( std::addressof( item_pine_tea ), false, true ); + item_pine_tea.charges--; REQUIRE( parts[2]->ammo_remaining() == item_pine_tea.charges ); REQUIRE( battery_ptr->ammo_remaining() == battery_charge ); From 8d9b6c75e2da5e9f578dc6a0137826da6f71aa51 Mon Sep 17 00:00:00 2001 From: Gregor Anders Date: Sat, 21 Dec 2019 15:04:51 +0100 Subject: [PATCH 08/15] Merge. --- src/vehicle_use.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vehicle_use.cpp b/src/vehicle_use.cpp index 1bdccc2c8a82e..9a2f98ffd8cf3 100644 --- a/src/vehicle_use.cpp +++ b/src/vehicle_use.cpp @@ -1799,7 +1799,7 @@ item_count_tuple vehicle::select_comestible_liquid( const std::vector::min(); int idx = 0; - if( liquids.empty() ) { return ...; } + if( liquids.empty() ) { for( auto &liquid : liquids ) { const item *p_item = std::get<0>( liquid ); selectmenu.addentry( idx++, true, MENU_AUTOASSIGN, p_item->tname( std::get<1>( liquid ) ) ); From 443f68c458ae7ecda603ea98b3ad4438bf6468f2 Mon Sep 17 00:00:00 2001 From: Gregor Anders Date: Sat, 21 Dec 2019 18:39:54 +0100 Subject: [PATCH 09/15] Catch specific exception. --- src/vehicle_use.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vehicle_use.cpp b/src/vehicle_use.cpp index 9a2f98ffd8cf3..b852105b43dd5 100644 --- a/src/vehicle_use.cpp +++ b/src/vehicle_use.cpp @@ -1816,7 +1816,7 @@ item_count_tuple vehicle::select_comestible_liquid( const std::vector= 0 && choice < idx ) { try { return liquids[static_cast( choice )]; - } catch( ... ) { + } catch( const std::out_of_range& ) { debugmsg( "Warning! Invalid comestible liquid index" ); } } From df9ba11d87aacd5b8c332a7c72a3964d3667a20f Mon Sep 17 00:00:00 2001 From: Gregor Anders Date: Sat, 21 Dec 2019 18:43:54 +0100 Subject: [PATCH 10/15] Adjusted format. --- src/vehicle_use.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vehicle_use.cpp b/src/vehicle_use.cpp index b852105b43dd5..3b70ebbf7790e 100644 --- a/src/vehicle_use.cpp +++ b/src/vehicle_use.cpp @@ -1816,7 +1816,7 @@ item_count_tuple vehicle::select_comestible_liquid( const std::vector= 0 && choice < idx ) { try { return liquids[static_cast( choice )]; - } catch( const std::out_of_range& ) { + } catch( const std::out_of_range & ) { debugmsg( "Warning! Invalid comestible liquid index" ); } } From 5219c356144a2664691349646a91816a4465cba4 Mon Sep 17 00:00:00 2001 From: Gregor Anders Date: Sat, 21 Dec 2019 19:51:07 +0100 Subject: [PATCH 11/15] Removed pointless code. --- src/vehicle_use.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/vehicle_use.cpp b/src/vehicle_use.cpp index 3b70ebbf7790e..a8d992d9d4295 100644 --- a/src/vehicle_use.cpp +++ b/src/vehicle_use.cpp @@ -1814,11 +1814,7 @@ item_count_tuple vehicle::select_comestible_liquid( const std::vector= 0 && choice < idx ) { - try { - return liquids[static_cast( choice )]; - } catch( const std::out_of_range & ) { - debugmsg( "Warning! Invalid comestible liquid index" ); - } + return liquids[static_cast( choice )]; } } From c234f676751d15bdc1e7dcf904031daa1dba8805 Mon Sep 17 00:00:00 2001 From: Gregor Anders Date: Sat, 21 Dec 2019 20:14:16 +0100 Subject: [PATCH 12/15] Apply suggestions from code review Co-Authored-By: BevapDin --- src/vehicle_use.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vehicle_use.cpp b/src/vehicle_use.cpp index a8d992d9d4295..5f638aaf8c19c 100644 --- a/src/vehicle_use.cpp +++ b/src/vehicle_use.cpp @@ -2167,7 +2167,7 @@ void vehicle::interact_with( const tripoint &pos, int interact_part ) _( "Select a liquid" ) ); const item *comestible_ptr = std::get<0>( item_tuple ); if( comestible_ptr ) { - use_fill_container( std::get<0>( item_tuple ), ( has_chemlab || has_kitchen ), ( has_fridge || + use_fill_container( std::get<0>( item_tuple ), has_chemlab || has_kitchen, has_fridge || has_freezer ); has_freezer ) ); } return; From cff1066c69e3bd204a5a97912d459703e90407af Mon Sep 17 00:00:00 2001 From: Gregor Anders Date: Sat, 21 Dec 2019 21:33:57 +0100 Subject: [PATCH 13/15] Fixed merge issues. --- src/vehicle_use.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/vehicle_use.cpp b/src/vehicle_use.cpp index 5f638aaf8c19c..0e9330391fd61 100644 --- a/src/vehicle_use.cpp +++ b/src/vehicle_use.cpp @@ -2167,8 +2167,8 @@ void vehicle::interact_with( const tripoint &pos, int interact_part ) _( "Select a liquid" ) ); const item *comestible_ptr = std::get<0>( item_tuple ); if( comestible_ptr ) { - use_fill_container( std::get<0>( item_tuple ), has_chemlab || has_kitchen, has_fridge || has_freezer ); - has_freezer ) ); + use_fill_container( std::get<0>( item_tuple ), has_chemlab || has_kitchen, has_fridge || + has_freezer ); } return; } @@ -2176,7 +2176,7 @@ void vehicle::interact_with( const tripoint &pos, int interact_part ) item_count_tuple item_tuple = select_comestible_liquid( comestible_liquids, _( "Select a drink" ) ); const item *comestible_ptr = std::get<0>( item_tuple ); if( comestible_ptr ) { - use_faucet( comestible_ptr, ( has_chemlab || has_kitchen ), ( has_fridge || has_freezer ) ); + use_faucet( comestible_ptr, has_chemlab || has_kitchen, has_fridge || has_freezer ); } return; } From d8504c7fead282e90729e61bcb80907120dfa010 Mon Sep 17 00:00:00 2001 From: Gregor Anders Date: Sun, 22 Dec 2019 11:01:05 +0100 Subject: [PATCH 14/15] Renamed params and used proper methods for empty check. --- src/vehicle.h | 4 ++-- src/vehicle_use.cpp | 6 +++--- tests/vehicle_interact_test.cpp | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/vehicle.h b/src/vehicle.h index 49b75c7e20409..9de6507e4d0f8 100644 --- a/src/vehicle.h +++ b/src/vehicle.h @@ -1587,9 +1587,9 @@ class vehicle std::vector get_comestible_liquids(); item_count_tuple select_comestible_liquid( const std::vector &comestible_liquids, const std::string &message ); - void use_fill_container( const item *comestible_liquid, bool has_hotplate, + void use_fill_container( const item *comestible_ptr, bool has_hotplate, bool has_freezer ); - void use_faucet( const item *comestible_liquid, bool has_hotplate, bool has_freezer ); + void use_faucet( const item *comestible_ptr, bool has_hotplate, bool has_freezer ); void interact_with( const tripoint &pos, int interact_part ); diff --git a/src/vehicle_use.cpp b/src/vehicle_use.cpp index 0e9330391fd61..c5ba65a8be46b 100644 --- a/src/vehicle_use.cpp +++ b/src/vehicle_use.cpp @@ -1821,10 +1821,10 @@ item_count_tuple vehicle::select_comestible_liquid( const std::vectoris_comestible() ) { - itype_id liquid_id = comestibale_ptr->typeId(); + if( comestible_ptr && comestible_ptr->is_comestible() ) { + itype_id liquid_id = comestible_ptr->typeId(); g->u.siphon( *this, liquid_id ); } diff --git a/tests/vehicle_interact_test.cpp b/tests/vehicle_interact_test.cpp index 86510e1243c64..9ecd8f32aacc0 100644 --- a/tests/vehicle_interact_test.cpp +++ b/tests/vehicle_interact_test.cpp @@ -229,5 +229,5 @@ TEST_CASE( "water faucet offers comestible liquids", "[water_faucet]" ) REQUIRE( item_pine_tea.charges == 0 ); liquids = veh_ptr->get_comestible_liquids(); - REQUIRE( liquids.size() == 0 ); + REQUIRE( liquids.empty() ); } From 26c2365a7761cecbed0731d6f0b6a52a882743e4 Mon Sep 17 00:00:00 2001 From: Gregor Anders Date: Tue, 24 Dec 2019 02:49:15 +0100 Subject: [PATCH 15/15] Make it work again. --- src/vehicle_use.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vehicle_use.cpp b/src/vehicle_use.cpp index c5ba65a8be46b..0b02430da4fc6 100644 --- a/src/vehicle_use.cpp +++ b/src/vehicle_use.cpp @@ -1799,7 +1799,7 @@ item_count_tuple vehicle::select_comestible_liquid( const std::vector::min(); int idx = 0; - if( liquids.empty() ) { + if( !liquids.empty() ) { for( auto &liquid : liquids ) { const item *p_item = std::get<0>( liquid ); selectmenu.addentry( idx++, true, MENU_AUTOASSIGN, p_item->tname( std::get<1>( liquid ) ) );