Skip to content

Commit

Permalink
Move refueling CBM to its own menu (CleverRaven#48750)
Browse files Browse the repository at this point in the history
* Refueling menu first pass

* Get refueling menu to actually refuel

* Desentangle fueling and eating

* Add refueling to consume menu

* fix error?

* Remove unused static

* remove unused variable
  • Loading branch information
Fris0uman authored Jul 9, 2021
1 parent 0cb3978 commit ca4066a
Show file tree
Hide file tree
Showing 15 changed files with 310 additions and 71 deletions.
9 changes: 9 additions & 0 deletions data/json/player_activities.json
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,15 @@
"based_on": "neither",
"no_resume": true
},
{
"id": "ACT_CONSUME_FUEL_MENU",
"type": "activity_type",
"activity_level": "NO_EXERCISE",
"verb": "consuming fuel",
"suspendable": false,
"based_on": "neither",
"no_resume": true
},
{
"id": "ACT_MIND_SPLICER",
"type": "activity_type",
Expand Down
7 changes: 7 additions & 0 deletions data/raw/keybindings.json
Original file line number Diff line number Diff line change
Expand Up @@ -1686,6 +1686,13 @@
"name": "Toggle safe fuel mod",
"bindings": [ { "input_method": "keyboard_char", "key": "S" }, { "input_method": "keyboard_code", "key": "s", "mod": [ "shift" ] } ]
},
{
"type": "keybinding",
"id": "REFUEL",
"category": "BIONICS",
"name": "Open refuel menu",
"bindings": [ { "input_method": "keyboard_char", "key": "R" }, { "input_method": "keyboard_code", "key": "r", "mod": [ "shift" ] } ]
},
{
"type": "keybinding",
"id": "TOGGLE_AUTO_START",
Expand Down
19 changes: 15 additions & 4 deletions src/activity_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1466,7 +1466,13 @@ void consume_activity_actor::start( player_activity &act, Character &guy )
int moves;
Character &player_character = get_player_character();
if( consume_location ) {
const ret_val<edible_rating> ret = player_character.will_eat( *consume_location, true );
ret_val<edible_rating> ret = ret_val<edible_rating>::make_success();
if( refuel ) {
ret = player_character.can_consume_fuel( *consume_location );
} else {
ret = player_character.will_eat( *consume_location, true );
}

if( !ret.success() ) {
canceled = true;
consume_menu_selections = std::vector<int>();
Expand All @@ -1476,7 +1482,12 @@ void consume_activity_actor::start( player_activity &act, Character &guy )
}
moves = to_moves<int>( guy.get_consume_time( *consume_location ) );
} else if( !consume_item.is_null() ) {
const ret_val<edible_rating> ret = player_character.will_eat( consume_item, true );
ret_val<edible_rating> ret = ret_val<edible_rating>::make_success();
if( refuel ) {
ret = player_character.can_consume_fuel( consume_item );
} else {
ret = player_character.will_eat( consume_item, true );
}
if( !ret.success() ) {
canceled = true;
consume_menu_selections = std::vector<int>();
Expand Down Expand Up @@ -1513,9 +1524,9 @@ void consume_activity_actor::finish( player_activity &act, Character & )
avatar &player_character = get_avatar();
if( !canceled ) {
if( consume_loc ) {
player_character.consume( consume_loc, /*force=*/true );
player_character.consume( consume_loc, /*force=*/true, refuel );
} else if( !consume_item.is_null() ) {
player_character.consume( consume_item, /*force=*/true );
player_character.consume( consume_item, /*force=*/true, refuel );
} else {
debugmsg( "Item location/name to be consumed should not be null." );
}
Expand Down
5 changes: 3 additions & 2 deletions src/activity_actor_definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ class consume_activity_actor : public activity_actor
std::string consume_menu_filter;
bool canceled = false;
activity_id type;
bool refuel = false;
/**
* @pre @p other is a consume_activity_actor
*/
Expand All @@ -595,11 +596,11 @@ class consume_activity_actor : public activity_actor
consume_activity_actor( const item_location &consume_location,
std::vector<int> consume_menu_selections,
const std::vector<item_location> &consume_menu_selected_items,
const std::string &consume_menu_filter, activity_id type ) :
const std::string &consume_menu_filter, activity_id type, bool refuel = false ) :
consume_location( consume_location ), consume_menu_selections( consume_menu_selections ),
consume_menu_selected_items( consume_menu_selected_items ),
consume_menu_filter( consume_menu_filter ),
type( type ) {}
type( type ), refuel( refuel ) {}

explicit consume_activity_actor( const item_location &consume_location ) :
consume_location( consume_location ), consume_menu_selections( std::vector<int>() ) {}
Expand Down
9 changes: 9 additions & 0 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ static const activity_id ACT_CLEAR_RUBBLE( "ACT_CLEAR_RUBBLE" );
static const activity_id ACT_CONSUME_DRINK_MENU( "ACT_CONSUME_DRINK_MENU" );
static const activity_id ACT_CONSUME_FOOD_MENU( "ACT_CONSUME_FOOD_MENU" );
static const activity_id ACT_CONSUME_MEDS_MENU( "ACT_CONSUME_MEDS_MENU" );
static const activity_id ACT_CONSUME_FUEL_MENU( "ACT_CONSUME_FUEL_MENU" );
static const activity_id ACT_CRACKING( "ACT_CRACKING" );
static const activity_id ACT_DISMEMBER( "ACT_DISMEMBER" );
static const activity_id ACT_DISSECT( "ACT_DISSECT" );
Expand Down Expand Up @@ -272,6 +273,7 @@ activity_handlers::do_turn_functions = {
{ ACT_CONSUME_FOOD_MENU, consume_food_menu_do_turn },
{ ACT_CONSUME_DRINK_MENU, consume_drink_menu_do_turn },
{ ACT_CONSUME_MEDS_MENU, consume_meds_menu_do_turn },
{ ACT_CONSUME_FUEL_MENU, consume_fuel_menu_do_turn },
{ ACT_MOVE_LOOT, move_loot_do_turn },
{ ACT_ADV_INVENTORY, adv_inventory_do_turn },
{ ACT_ARMOR_LAYERS, armor_layers_do_turn },
Expand Down Expand Up @@ -351,6 +353,7 @@ activity_handlers::finish_functions = {
{ ACT_CONSUME_FOOD_MENU, eat_menu_finish },
{ ACT_CONSUME_DRINK_MENU, eat_menu_finish },
{ ACT_CONSUME_MEDS_MENU, eat_menu_finish },
{ ACT_CONSUME_FUEL_MENU, eat_menu_finish },
{ ACT_WASH, washing_finish },
{ ACT_HACKSAW, hacksaw_finish },
{ ACT_PRY_NAILS, pry_nails_finish },
Expand Down Expand Up @@ -2843,6 +2846,12 @@ void activity_handlers::consume_meds_menu_do_turn( player_activity *, player * )
avatar_action::eat( player_character, game_menus::inv::consume_meds( player_character ) );
}

void activity_handlers::consume_fuel_menu_do_turn( player_activity *, player * )
{
avatar &player_character = get_avatar();
avatar_action::eat( player_character, game_menus::inv::consume_fuel( player_character ), true );
}

void activity_handlers::move_loot_do_turn( player_activity *act, player *p )
{
activity_on_turn_move_loot( *act, *p );
Expand Down
1 change: 1 addition & 0 deletions src/activity_handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ void eat_menu_do_turn( player_activity *act, player *p );
void consume_food_menu_do_turn( player_activity *act, player *p );
void consume_drink_menu_do_turn( player_activity *act, player *p );
void consume_meds_menu_do_turn( player_activity *act, player *p );
void consume_fuel_menu_do_turn( player_activity *act, player *p );
void move_items_do_turn( player_activity *act, player *p );
void multiple_farm_do_turn( player_activity *act, player *p );
void multiple_fish_do_turn( player_activity *act, player *p );
Expand Down
8 changes: 4 additions & 4 deletions src/avatar_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,29 +830,29 @@ bool avatar_action::eat_here( avatar &you )
return false;
}

void avatar_action::eat( avatar &you, const item_location &loc )
void avatar_action::eat( avatar &you, const item_location &loc, bool refuel )
{
std::string filter;
if( !you.activity.str_values.empty() ) {
filter = you.activity.str_values.back();
}
avatar_action::eat( you, loc, you.activity.values, you.activity.targets, filter,
you.activity.id() );
you.activity.id(), refuel );
}

void avatar_action::eat( avatar &you, const item_location &loc,
const std::vector<int> &consume_menu_selections,
const std::vector<item_location> &consume_menu_selected_items,
const std::string &consume_menu_filter,
activity_id type )
activity_id type, bool refuel )
{
if( !loc ) {
you.cancel_activity();
add_msg( _( "Never mind." ) );
return;
}
you.assign_activity( player_activity( consume_activity_actor( loc, consume_menu_selections,
consume_menu_selected_items, consume_menu_filter, type ) ) );
consume_menu_selected_items, consume_menu_filter, type, refuel ) ) );
}

void avatar_action::plthrow( avatar &you, item_location loc,
Expand Down
4 changes: 2 additions & 2 deletions src/avatar_action.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ namespace avatar_action
{

/** Eat food or fuel 'E' (or 'a') */
void eat( avatar &you, const item_location &loc );
void eat( avatar &you, const item_location &loc, bool refuel = false );
void eat( avatar &you, const item_location &loc,
const std::vector<int> &consume_menu_selections,
const std::vector<item_location> &consume_menu_selected_items,
const std::string &consume_menu_filter, activity_id type );
const std::string &consume_menu_filter, activity_id type, bool refuel = false );
// special rules for eating: grazing etc
// returns false if no rules are needed
bool eat_here( avatar &you );
Expand Down
11 changes: 9 additions & 2 deletions src/bionics_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string>

#include "avatar.h"
#include "avatar_action.h"
#include "bionics.h"
#include "bodypart.h"
#include "calendar.h"
Expand All @@ -17,6 +18,7 @@
#include "enums.h"
#include "flat_set.h"
#include "game.h"
#include "game_inventory.h"
#include "input.h"
#include "inventory.h"
#include "material.h"
Expand Down Expand Up @@ -268,9 +270,10 @@ static void draw_bionics_titlebar( const catacurses::window &window, avatar *p,
std::string desc_append = string_format(
_( "[<color_yellow>%s</color>] Reassign, [<color_yellow>%s</color>] Switch tabs, "
"[<color_yellow>%s</color>] Toggle fuel saving mode, "
"[<color_yellow>%s</color>] Toggle auto start mode." ),
"[<color_yellow>%s</color>] Toggle auto start mode, "
"[<color_yellow>%s</color>] Open refueling menu." ),
ctxt.get_desc( "REASSIGN" ), ctxt.get_desc( "NEXT_TAB" ), ctxt.get_desc( "TOGGLE_SAFE_FUEL" ),
ctxt.get_desc( "TOGGLE_AUTO_START" ) );
ctxt.get_desc( "TOGGLE_AUTO_START" ), ctxt.get_desc( "REFUEL" ) );
desc_append += string_format( _( " [<color_yellow>%s</color>] Sort: %s" ), ctxt.get_desc( "SORT" ),
sort_mode_str( uistate.bionic_sort_mode ) );
std::string desc;
Expand Down Expand Up @@ -636,6 +639,7 @@ void avatar::power_bionics()
ctxt.register_action( "QUIT" );
ctxt.register_action( "HELP_KEYBINDINGS" );
ctxt.register_action( "TOGGLE_SAFE_FUEL" );
ctxt.register_action( "REFUEL" );
ctxt.register_action( "TOGGLE_AUTO_START" );
ctxt.register_action( "SORT" );

Expand Down Expand Up @@ -835,6 +839,9 @@ void avatar::power_bionics()
popup( _( "You can't toggle fuel saving mode on a non-fueled CBM." ) );
}
}
} else if( action == "REFUEL" ) {
avatar_action::eat( get_avatar(), game_menus::inv::consume_fuel( get_avatar() ), true );
break;
} else if( action == "TOGGLE_AUTO_START" ) {
auto &bio_list = tab_mode == TAB_ACTIVE ? active : passive;
if( !current_bionic_list->empty() ) {
Expand Down
2 changes: 2 additions & 0 deletions src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -2365,6 +2365,8 @@ class Character : public Creature, public visitable
int nutrition_for( const item &comest ) const;
/** Can the food be [theoretically] eaten no matter the consequences? */
ret_val<edible_rating> can_eat( const item &food ) const;
/** Can the fuel be [theoretically] eaten? */
ret_val<edible_rating> can_consume_fuel( const item &fuel ) const;
/**
* Same as @ref can_eat, but takes consequences into account.
* Asks about them if @param interactive is true, refuses otherwise.
Expand Down
68 changes: 33 additions & 35 deletions src/consumption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,22 +635,8 @@ morale_type Character::allergy_type( const item &food ) const

ret_val<edible_rating> Character::can_eat( const item &food ) const
{
bool can_fuel_cbm = can_fuel_bionic_with( food );
if( !food.is_comestible() && !can_fuel_cbm ) {
if( !food.is_comestible() ) {
return ret_val<edible_rating>::make_failure( _( "That doesn't look edible." ) );
} else if( can_fuel_cbm ) {
std::string item_name = food.tname();
material_id mat_type = food.get_base_material().id;
if( food.type->magazine ) {
const item ammo = item( food.ammo_current() );
item_name = ammo.tname();
mat_type = ammo.get_base_material().id;
}
if( get_fuel_capacity( mat_type ) <= 0 ) {
return ret_val<edible_rating>::make_failure( _( "No space to store more %s" ), item_name );
} else {
return ret_val<edible_rating>::make_success();
}
}

const auto &comest = food.get_comestible();
Expand Down Expand Up @@ -768,6 +754,26 @@ ret_val<edible_rating> Character::can_eat( const item &food ) const
return ret_val<edible_rating>::make_success();
}

ret_val<edible_rating> Character::can_consume_fuel( const item &fuel ) const
{
if( !can_fuel_bionic_with( fuel ) ) {
return ret_val<edible_rating>::make_failure( _( "That doesn't look useable as fuel." ) );
} else {
std::string item_name = fuel.tname();
material_id mat_type = fuel.get_base_material().id;
if( fuel.type->magazine ) {
const item ammo = item( fuel.ammo_current() );
item_name = ammo.tname();
mat_type = ammo.get_base_material().id;
}
if( get_fuel_capacity( mat_type ) <= 0 ) {
return ret_val<edible_rating>::make_failure( _( "No space to store more %s" ), item_name );
}

}
return ret_val<edible_rating>::make_success();
}

ret_val<edible_rating> Character::will_eat( const item &food, bool interactive ) const
{
const auto ret = can_eat( food );
Expand All @@ -778,7 +784,7 @@ ret_val<edible_rating> Character::will_eat( const item &food, bool interactive )
return ret;
}

// exit early for cbm fuel as we've already tested everything in can_eat
// exit early as we've already tested everything in can_eat
if( !food.is_comestible() ) {
return ret_val<edible_rating>::make_success();
}
Expand Down Expand Up @@ -1528,7 +1534,7 @@ bool Character::fuel_bionic_with( item &it )
ngettext( "<npcname> load %1$i charge of %2$s in their %3$s.",
"<npcname> load %1$i charges of %2$s in their %3$s.", loadable ), loadable, mat->name(),
bio->name );
mod_moves( -250 );

// Return false for magazines because only their ammo is consumed
return !is_magazine;
}
Expand Down Expand Up @@ -1730,18 +1736,7 @@ static bool consume_med( item &target, player &you )
return true;
}

static bool cbm_is_full( const player &guy, const item &fuel )
{
material_id fuel_mat;
if( fuel.is_magazine() ) {
fuel_mat = item( fuel.ammo_current() ).get_base_material().id;
} else {
fuel_mat = fuel.get_base_material().id;
}
return guy.get_fuel_capacity( fuel_mat ) > 0;
}

trinary player::consume( item &target, bool force )
trinary player::consume( item &target, bool force, bool refuel )
{
if( target.is_null() ) {
add_msg_if_player( m_info, _( "You do not have that item." ) );
Expand All @@ -1762,10 +1757,13 @@ trinary player::consume( item &target, bool force )
if( is_player() && !query_consume_ownership( target, *this ) ) {
return trinary::NONE;
}
if( consume_med( target, *this ) ||
( has_max_power() && get_power_level() < get_max_power_level() &&
cbm_is_full( *this, target ) && fuel_bionic_with( target ) ) ||
eat( target, *this, force ) ) {

if( refuel ) {
fuel_bionic_with( target );
return target.charges <= 0 ? trinary::ALL : trinary::SOME;
}

if( consume_med( target, *this ) || eat( target, *this, force ) ) {

get_event_bus().send<event_type::character_consumes_item>( getID(), target.typeId() );

Expand All @@ -1776,15 +1774,15 @@ trinary player::consume( item &target, bool force )
return trinary::NONE;
}

trinary player::consume( item_location loc, bool force )
trinary player::consume( item_location loc, bool force, bool refuel )
{
if( !loc ) {
debugmsg( "Null loc to consume." );
return trinary::NONE;
}
contents_change_handler handler;
item &target = *loc;
trinary result = consume( target, force );
trinary result = consume( target, force, refuel );
if( result != trinary::NONE ) {
handler.unseal_pocket_containing( loc );
}
Expand Down
Loading

0 comments on commit ca4066a

Please sign in to comment.