Skip to content

Commit

Permalink
Merge pull request CleverRaven#19532 from codemime/ref_inv_n9
Browse files Browse the repository at this point in the history
Decouple some of the inventory functions from the game class (refactoring)
  • Loading branch information
Rivet-the-Zombie authored Dec 3, 2016
2 parents 8c3bc14 + f9b98e0 commit e1de5da
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 119 deletions.
1 change: 1 addition & 0 deletions astyled_whitelist
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ src/filesystem.h
src/fire.h
src/flag.h
src/game_constants.h
src/game_inventory.h
src/gates.h
src/generic_factory.h
src/get_version.h
Expand Down
3 changes: 0 additions & 3 deletions lua/class_definitions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,8 @@ classes = {
{ name = "get_temperature", rval = "int", args = { } },
{ name = "handle_liquid", rval = "bool", args = { "item" } },
{ name = "increase_kill_count", rval = nil, args = { "mtype_id" } },
{ name = "interactive_inv", rval = nil, args = { } },
{ name = "inv_for_all", rval = "int", args = { "string" } },
{ name = "inv_for_flag", rval = "int", args = { "string", "string" } },
{ name = "inv_map_for_liquid", rval = "item&", args = { "item", "string" } },
{ name = "inv_map_for_liquid", rval = "item&", args = { "item", "string", "int" } },
{ name = "inventory_item_menu", rval = "int", args = { "int" } },
{ name = "inventory_item_menu", rval = "int", args = { "int", "int" } },
{ name = "inventory_item_menu", rval = "int", args = { "int", "int", "int" } },
Expand Down
3 changes: 2 additions & 1 deletion src/armor_layers.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "game.h"
#include "game_inventory.h"
#include "player.h"
#include "catacharset.h" // used for utf8_width()
#include "input.h"
Expand Down Expand Up @@ -507,7 +508,7 @@ void player::sort_armor()
// filter inventory for all items that are armor/clothing
// NOTE: This is from player's inventory, even for NPCs!
// @todo Allow making NPCs equip their own stuff
int pos = g->inv_for_unequipped( _( "Put on" ) );
int pos = game_menus::inv::wear( g->u );
// only equip if something valid selected!
if( pos != INT_MIN ) {
// wear the item
Expand Down
32 changes: 18 additions & 14 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
#include "cata_utility.h"
#include "pathfinding.h"
#include "projectile.h"
#include "game_inventory.h"
#include "gates.h"
#include "item_factory.h"
#include "scent_map.h"
Expand Down Expand Up @@ -2854,12 +2855,11 @@ bool game::handle_action()
break;

case ACTION_INVENTORY:
interactive_inv();
refresh_all();
game_menus::inv::common( u );
break;

case ACTION_COMPARE:
compare();
game_menus::inv::compare( u );
break;

case ACTION_ORGANIZE:
Expand Down Expand Up @@ -7101,7 +7101,7 @@ void game::smash()
void game::use_item(int pos)
{
if (pos == INT_MIN) {
auto loc = inv_for_activatables( _( "Use item" ) );
auto loc = game_menus::inv::use( u );

if( !loc ) {
add_msg( _( "Never mind." ) );
Expand Down Expand Up @@ -7549,7 +7549,7 @@ bool pet_menu(monster *z)
return true;
}

const auto items_to_stash = g->multidrop();
const auto items_to_stash = game_menus::inv::multidrop( g->u );
if( !items_to_stash.empty() ) {
g->u.drop( items_to_stash, z->pos(), true );
z->add_effect( effect_controlled, 5);
Expand Down Expand Up @@ -9228,7 +9228,7 @@ int game::list_items(const int iLastState)
do {
if (!ground_items.empty() || iLastState == 1) {
if (action == "COMPARE") {
compare( active_pos );
game_menus::inv::compare( u, active_pos );
reset = true;
refresh_all();
} else if (action == "FILTER") {
Expand Down Expand Up @@ -9958,7 +9958,7 @@ bool game::handle_liquid( item &liquid, item * const source, const int radius,
}
};

const std::string liquid_name = liquid.has_infinite_charges() ? liquid.tname() : liquid.display_name( liquid.charges );
const std::string liquid_name = liquid.display_name( liquid.charges );

uimenu menu;
menu.return_invalid = true;
Expand All @@ -9981,8 +9981,7 @@ bool game::handle_liquid( item &liquid, item * const source, const int radius,

menu.addentry( -1, true, 'c', _( "Pour into a container" ) );
actions.emplace_back( [&]() {
const std::string text = string_format( _( "Container for %s" ), liquid_name.c_str() );
item * const cont = inv_map_for_liquid( liquid, text, radius );
item * const cont = game_menus::inv::container_for( u, liquid, radius ).get_item();

if( cont == nullptr || cont->is_null() ) {
add_msg( _( "Never mind." ) );
Expand Down Expand Up @@ -10106,7 +10105,7 @@ void game::drop( int pos, const tripoint &where )
if( pos != INT_MIN ) {
u.drop( pos, where );
} else {
u.drop( multidrop(), where );
u.drop( game_menus::inv::multidrop( u ), where );
}
}

Expand Down Expand Up @@ -10671,16 +10670,21 @@ void game::eat(int pos)
void game::wear(int pos)
{
if (pos == INT_MIN) {
pos = inv_for_unequipped( _( "Wear item" ) );
pos = game_menus::inv::wear( u );
}

if( pos == INT_MIN ) {
add_msg( _( "Never mind." ) );
return;
}

u.wear(pos);
u.wear( pos );
}

void game::takeoff(int pos)
{
if (pos == INT_MIN) {
pos = inv_for_equipped( _( "Take off item" ) );
pos = game_menus::inv::take_off( u );
}
if (pos == INT_MIN) {
add_msg(_("Never mind."));
Expand Down Expand Up @@ -11025,7 +11029,7 @@ void game::wield( int pos )
void game::read()
{
// Can read items from inventory or within one tile (including in vehicles)
auto loc = inv_for_books( _( "Read" ) );
auto loc = game_menus::inv::read( u );

if( loc ) {
u.read( loc.obtain( u ) );
Expand Down
32 changes: 1 addition & 31 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ typedef std::vector< const std::list<item>* > const_invslice;
typedef std::vector< std::pair<std::list<item>*, int> > indexed_invslice;

typedef std::function<bool( const item & )> item_filter;
typedef std::function<bool( const item_location & )> item_location_filter;

class game
{
Expand Down Expand Up @@ -461,19 +460,11 @@ class game
std::string list_item_upvote;
std::string list_item_downvote;

item *inv_map_for_liquid(const item &liquid, const std::string &title, int radius = 0);

item_location get_item_from_inventory( player &p, const std::string &title );
void interactive_inv();

// @todo Move these functions to game_menus::inv and isolate them.
int inv_for_filter( const std::string &title, item_filter filter, const std::string &none_message = "" );

int inv_for_all( const std::string &title, const std::string &none_message = "" );
int inv_for_flag( const std::string &flag, const std::string &title );
int inv_for_id( const itype_id &id, const std::string &title );
int inv_for_tools_powered_by( const ammotype &battery_id, const std::string &title );
int inv_for_equipped( const std::string &title );
int inv_for_unequipped( const std::string &title );

enum inventory_item_menu_positon {
RIGHT_TERMINAL_EDGE,
Expand All @@ -483,29 +474,9 @@ class game
};
int inventory_item_menu(int pos, int startx = 0, int width = 50, inventory_item_menu_positon position = RIGHT_OF_INFO);

/**
* @name Customized inventory menus
*
* The functions here execute customized inventory menus for specific game situations.
* Each menu displays only related inventory (or nearby) items along with context dependent information.
* More functions will follow. @todo update all 'inv_for...()' functions to return @ref item_location instead of @ref int and move them here.
* @param title Title of the menu
* @return Either location of the selected item or null location if none was selected.
*/
/*@{*/
/** Custom-filtered menu for inventory items and those that are nearby (within @ref radius). */
item_location inv_map_splice( item_filter filter, const std::string &title, int radius = 0,
const std::string &none_message = "" );
/** Item activation menu. */
item_location inv_for_activatables( const std::string &title );
/** Book reading menu. */
item_location inv_for_books( const std::string &title );
/** Gunmod installation menu. */
item_location inv_for_gunmod( const item &gunmod, const std::string &title );
/*@}*/

// Select items to drop. Returns a list of pairs of position, quantity.
std::list<std::pair<int, int>> multidrop();
faction *list_factions(std::string title = "FACTIONS:");

bool has_gametype() const;
Expand Down Expand Up @@ -797,7 +768,6 @@ class game
void examine();

void grab(); // Establish a grab on something.
void compare( const tripoint &offset = tripoint_min ); // Compare items 'I'
void drop(int pos = INT_MIN, const tripoint &where = tripoint_min ); // Drop an item 'd'
void drop_in_direction(); // Drop w/ direction 'D'

Expand Down
Loading

0 comments on commit e1de5da

Please sign in to comment.