Skip to content

Commit

Permalink
Move game inventory functions into a separate file and astyle it
Browse files Browse the repository at this point in the history
  • Loading branch information
codemime committed Jul 7, 2016
1 parent 1198fba commit bab7bd7
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 198 deletions.
1 change: 1 addition & 0 deletions CataclysmWin.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@
<Unit filename="src/game.cpp" />
<Unit filename="src/game.h" />
<Unit filename="src/game_constants.h" />
<Unit filename="src/game_inventory.cpp" />
<Unit filename="src/gamemode.cpp" />
<Unit filename="src/gamemode.h" />
<Unit filename="src/gates.cpp" />
Expand Down
1 change: 1 addition & 0 deletions astyled_whitelist
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ src/fault.h
src/filesystem.h
src/fire.h
src/game_constants.h
src/game_inventory.h
src/gates.h
src/generic_factory.h
src/get_version.h
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ SET(CATACLYSM_DDA_SOURCES
${CMAKE_SOURCE_DIR}/src/computer.cpp
${CMAKE_SOURCE_DIR}/src/trap.cpp
${CMAKE_SOURCE_DIR}/src/game.cpp
${CMAKE_SOURCE_DIR}/src/game_inventory.cpp
${CMAKE_SOURCE_DIR}/src/gates.cpp
${CMAKE_SOURCE_DIR}/src/monattack.cpp
${CMAKE_SOURCE_DIR}/src/effect.cpp
Expand Down
198 changes: 0 additions & 198 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "coordinate_conversions.h"
#include "rng.h"
#include "input.h"
#include "inventory_ui.h"
#include "output.h"
#include "skill.h"
#include "line.h"
Expand Down Expand Up @@ -1952,203 +1951,6 @@ int game::inventory_item_menu(int pos, int iStartX, int iWidth, const inventory_
return cMenu;
}

void game::interactive_inv()
{
static const std::set<int> allowed_selections = { { ' ', '.', 'q', '=', '\n', KEY_LEFT, KEY_ESCAPE } };

u.inv.restack( &u );
u.inv.sort();

inventory_pick_selector inv_s( u, _( "Inventory:" ) );

int res;
do {
const item_location &location = inv_s.execute();
if( location == item_location::nowhere ) {
break;
}
refresh_all();
res = inventory_item_menu( u.get_item_position( location.get_item() ) );
} while( allowed_selections.count( res ) != 0 );
}

item_location_filter convert_filter( const item_filter &filter )
{
return [ &filter ]( const item_location &loc ) {
return filter( *loc );
};
}

int game::inv_for_filter( const std::string &title, item_filter filter, const std::string &none_message )
{
return inv_for_filter( title, convert_filter( filter ), none_message );
}

int game::inv_for_filter( const std::string &title, item_location_filter filter, const std::string &none_message )
{
return u.get_item_position( inv_map_splice( filter, title, -1, none_message ).get_item() );
}

int game::inv_for_all( const std::string &title, const std::string &none_message )
{
const std::string msg = ( none_message.empty() ) ? _( "Your inventory is empty." ) : none_message;
return inv_for_filter( title, allow_all_items, msg );
}

int game::inv_for_activatables( const player &p, const std::string &title )
{
return inv_for_filter( title, [ &p ]( const item &it ) {
return p.rate_action_use( it ) != HINT_CANT;
}, _( "You don't have any items you can use." ) );
}

int game::inv_for_flag( const std::string &flag, const std::string &title )
{
return inv_for_filter( title, [ &flag ]( const item &it ) {
return it.has_flag( flag );
} );
}

int game::inv_for_id( const itype_id &id, const std::string &title )
{
return inv_for_filter( title, [ &id ]( const item &it ) {
return it.typeId() == id;
}, string_format( _( "You don't have a %s." ), item::nname( id ).c_str() ) );
}

int game::inv_for_tools_powered_by( const ammotype &battery_id, const std::string &title )
{
return inv_for_filter( title, [ &battery_id ]( const item & it ) {
return it.is_tool() && it.ammo_type() == battery_id;
}, string_format( _( "You don't have %s-powered tools." ), ammo_name( battery_id ).c_str() ) );
}

int game::inv_for_equipped( const std::string &title )
{
return inv_for_filter( title, [ this ]( const item &it ) {
return u.is_worn( it );
}, _( "You don't wear anything." ) );
}

int game::inv_for_unequipped( const std::string &title )
{
return inv_for_filter( title, [ this ]( const item &it ) {
return it.is_armor() && !u.is_worn( it );
}, _( "You don't have any items to wear." ) );
}

item_location game::inv_map_splice( item_filter filter, const std::string &title, int radius,
const std::string &none_message )
{
return inv_map_splice( convert_filter( filter ), title, radius, none_message );
}

item_location game::inv_map_splice( item_location_filter filter, const std::string &title, int radius,
const std::string &none_message )
{
u.inv.restack( &u );
u.inv.sort();

inventory_pick_selector inv_s( u, title, filter );

inv_s.add_nearby_items( radius );
if( inv_s.empty() ) {
const std::string msg = ( none_message.empty() ) ? _( "You don't have the necessary item at hand." ) : none_message;
popup( msg, PF_GET_KEY );
return item_location();
}

return std::move( inv_s.execute() );
}

item *game::inv_map_for_liquid(const item &liquid, const std::string &title, int radius)
{
const auto filter = [ this, &liquid ]( const item_location &location ) {
const bool allow_buckets = ( location.where() == item_location::type::character )
? location.get_item() == &u.weapon // allow only held buckets
: location.where() == item_location::type::map;

return location->get_remaining_capacity_for_liquid( liquid, allow_buckets ) > 0;
};

return inv_map_splice( filter, title, radius,
string_format( _( "You don't have a suitable container for carrying %s." ),
liquid.type_name( 1 ).c_str() ) ).get_item();
}

std::list<std::pair<int, int>> game::multidrop()
{
u.inv.restack( &u );
u.inv.sort();

inventory_drop_selector inv_s( u, _( "Multidrop:" ), [ this ]( const item_location &location ) -> bool {
return u.can_unwield( *location, false );
} );
if( inv_s.empty() ) {
popup( std::string( _( "You have nothing to drop." ) ), PF_GET_KEY );
return std::list<std::pair<int, int> >();
}
return inv_s.execute();
}

void game::compare( const tripoint &offset )
{
u.inv.restack(&u);
u.inv.sort();

inventory_compare_selector inv_s( u, _( "Compare:" ) );

if( offset != tripoint_min ) {
inv_s.add_map_items( u.pos() + offset );
inv_s.add_vehicle_items( u.pos() + offset );
} else {
inv_s.add_nearby_items();
}

if( inv_s.empty() ) {
popup( std::string( _( "There are no items to compare." ) ), PF_GET_KEY );
return;
}

do {
const auto to_compare = inv_s.execute();

if( to_compare.first == nullptr || to_compare.second == nullptr ) {
break;
}

std::vector<iteminfo> vItemLastCh, vItemCh;
std::string sItemLastCh, sItemCh, sItemLastTn, sItemTn;

to_compare.first->info( true, vItemCh );
sItemCh = to_compare.first->tname();
sItemTn = to_compare.first->type_name();

to_compare.second->info(true, vItemLastCh);
sItemLastCh = to_compare.second->tname();
sItemLastTn = to_compare.second->type_name();

int iScrollPos = 0;
int iScrollPosLast = 0;
int ch = ( int ) ' ';

do {
draw_item_info( 0, ( TERMX - VIEW_OFFSET_X * 2 ) / 2, 0, TERMY - VIEW_OFFSET_Y * 2,
sItemLastCh, sItemLastTn, vItemLastCh, vItemCh, iScrollPosLast, true ); //without getch(
ch = draw_item_info( ( TERMX - VIEW_OFFSET_X * 2) / 2, (TERMX - VIEW_OFFSET_X * 2 ) / 2,
0, TERMY - VIEW_OFFSET_Y * 2, sItemCh, sItemTn, vItemCh, vItemLastCh, iScrollPos );

if( ch == KEY_PPAGE ) {
iScrollPos--;
iScrollPosLast--;
} else if( ch == KEY_NPAGE ) {
iScrollPos++;
iScrollPosLast++;
}
} while ( ch == KEY_PPAGE || ch == KEY_NPAGE );
} while( true );
}

// Checks input to see if mouse was moved and handles the mouse view box accordingly.
// Returns true if input requires breaking out into a game action.
bool game::handle_mouseview(input_context &ctxt, std::string &action)
Expand Down
Loading

0 comments on commit bab7bd7

Please sign in to comment.