std::string describe( const Character *ch ) const override { std::string res = cur.veh.parts[ cur.part ].name(); if( ch ) { res += std::string( " " ) += direction_suffix( ch->pos(), cur.veh.global_part_pos3( cur.part ) ); } return res; }
std::string describe( const Character *ch ) const override { std::string res = g->m.name( cur ); if( ch ) { res += std::string( " " ) += direction_suffix( ch->pos(), cur ); } return res; }
std::string describe( const Character *ch ) const override { vpart_position part_pos( cur.veh, cur.part ); std::string res; if( auto label = part_pos.get_label() ) { res = colorize( *label, c_light_blue ) + " "; } if( auto cargo_part = part_pos.part_with_feature( "CARGO", true ) ) { res += cargo_part->part().name(); } else { debugmsg( "item in vehicle part without cargo storage" ); } if( ch ) { res += " " + direction_suffix( ch->pos(), part_pos.pos() ); } return res; }
void inventory_selector::add_custom_items( const std::list<item>::const_iterator &from, const std::list<item>::const_iterator &to, const std::string &title, const std::function<std::shared_ptr<item_location>( item * )> &locator ) { const auto &stacks = restack_items( from, to ); for( const auto &stack : stacks ) { const auto &location = locator( stack.front() ); if( filter( *location ) ) { const std::string name = trim( string_format( _( "%s %s" ), to_upper_case( title ).c_str(), direction_suffix( u.pos(), location->position() ).c_str() ) ); if( categories.empty() || categories.back().id != name ) { categories.emplace_back( name, name, INT_MIN + int( categories.size() ) ); } if( custom_column == nullptr ) { custom_column.reset( new inventory_column() ); } const long invlet = ( cur_custom_invlet <= max_custom_invlet ) ? cur_custom_invlet++ : '\0'; custom_column->add_entry( inventory_entry( location, stack.size(), &categories.back(), c_unset, invlet ) ); } } }
item_location game::inv_map_splice( item_filter inv_filter, item_filter ground_filter, item_filter vehicle_filter, const std::string &title, int radius ) { inventory_selector inv_s( false, false, title ); // first get matching items from the inventory u.inv.restack( &u ); u.inv.sort(); inv_s.make_item_list( u.inv.slice_filter_by( inv_filter ) ); std::list<item_category> categories; int rank = -1000; // items are stacked per tile considering vehicle and map tiles separately // in the below loops identical items on the same tile are grouped into lists // each element of stacks represents one tile and is a vector of such lists std::vector<std::vector<std::list<item>>> stacks; // an indexed_invslice is created for each map or vehicle tile // each list of items created above for the tile will be added to it std::vector<indexed_invslice> slices; // inv_s.first_item will later contain the chosen item as a pointer to first item // of one of the above lists so use this as the key when storing the item location std::unordered_map<item *, item_location> opts; // the closest 10 items also have their location added to the invlets vector const char min_invlet = '0'; const char max_invlet = '9'; char cur_invlet = min_invlet; std::vector<item_location> invlets; for( const auto &pos : closest_tripoints_first( radius, g->u.pos() ) ) { // second get all matching items on the map within radius if( m.accessible_items( g->u.pos(), pos, radius ) ) { auto items = m.i_at( pos ); // create a new slice and stack for the current map tile stacks.emplace_back(); slices.emplace_back(); // reserve sufficient capacity to ensure reallocation is not required auto ¤t_stack = stacks.back(); current_stack.reserve( items.size() ); for( item &it : items ) { if( ground_filter( it ) ) { auto match = std::find_if( current_stack.begin(), current_stack.end(), [&]( const std::list<item> &e ) { return it.stacks_with( e.back() ); } ); if( match != current_stack.end() ) { match->push_back( it ); } else { // item doesn't stack with any previous so start new list and append to current indexed_invslice current_stack.emplace_back( 1, it ); slices.back().emplace_back( ¤t_stack.back(), INT_MIN ); opts.emplace( ¤t_stack.back().front(), item_location::on_map( pos, &it ) ); if( cur_invlet <= max_invlet ) { current_stack.back().front().invlet = cur_invlet++; invlets.emplace_back( item_location::on_map( pos, &it ) ); } else { current_stack.back().front().invlet = 0; } } } } std::string name = trim( std::string( _( "GROUND" ) ) + " " + direction_suffix( g->u.pos(), pos ) ); categories.emplace_back( name, name, rank-- ); inv_s.make_item_list( slices.back(), &categories.back() ); } // finally get all matching items in vehicle cargo spaces int part = -1; vehicle *veh = m.veh_at( pos, part ); if( veh && part >= 0 ) { part = veh->part_with_feature( part, "CARGO" ); if( part != -1 ) { auto items = veh->get_items( part ); // create a new slice and stack for the current vehicle part stacks.emplace_back(); slices.emplace_back(); // reserve sufficient capacity to ensure reallocation is not required auto ¤t_stack = stacks.back(); current_stack.reserve( items.size() ); for( item &it : items ) { if( vehicle_filter( it ) ) { auto match = std::find_if( current_stack.begin(), current_stack.end(), [&]( const std::list<item> &e ) { return it.stacks_with( e.back() ); } ); if( match != current_stack.end() ) { match->push_back( it ); } else { // item doesn't stack with any previous so start new list and append to current indexed_invslice current_stack.emplace_back( 1, it ); slices.back().emplace_back( ¤t_stack.back(), INT_MIN ); opts.emplace( ¤t_stack.back().front(), item_location::on_vehicle( *veh, veh->parts[part].mount, &it ) ); if( cur_invlet <= max_invlet ) { current_stack.back().front().invlet = cur_invlet++; invlets.emplace_back( item_location::on_vehicle( *veh, veh->parts[part].mount, &it ) ); } else { current_stack.back().front().invlet = 0; } } } } std::string name = trim( std::string( _( "VEHICLE" ) ) + " " + direction_suffix( g->u.pos(), pos ) ); categories.emplace_back( name, name, rank-- ); inv_s.make_item_list( slices.back(), &categories.back() ); } } } inv_s.prepare_paging(); while( true ) { inv_s.display(); const std::string action = inv_s.ctxt.handle_input(); const long ch = inv_s.ctxt.get_raw_input().get_first_input(); const int item_pos = g->u.invlet_to_position( ch ); if( item_pos != INT_MIN ) { // Indexed item in inventory inv_s.set_to_drop( item_pos, 0 ); return item_location::on_character( u, inv_s.first_item ); } else if( ch >= min_invlet && ch <= max_invlet ) { // Indexed item on ground or in vehicle if( (long)invlets.size() > ch - min_invlet ) { return std::move( invlets[ch - min_invlet] ); } } else if( inv_s.handle_movement( action ) ) { // continue with comparison below } else if( action == "QUIT" ) { return item_location::nowhere(); } else if( action == "RIGHT" || action == "CONFIRM" ) { inv_s.set_selected_to_drop( 0 ); // Item in inventory if( inv_s.get_selected_item_position() != INT_MIN ) { return item_location::on_character( u, inv_s.first_item ); } // Item on ground or in vehicle auto it = opts.find( inv_s.first_item ); if( it != opts.end() ) { return std::move( it->second ); } return item_location::nowhere(); } } }
item_location game::inv_map_splice( item_filter inv_filter, item_filter ground_filter, item_filter vehicle_filter, const std::string &title, int radius, const std::string &none_message ) { u.inv.restack( &u ); u.inv.sort(); inventory_selector inv_s( u, inv_filter ); std::list<item_category> categories; int rank = -1000; // items are stacked per tile considering vehicle and map tiles separately // in the below loops identical items on the same tile are grouped into lists // each element of stacks represents one tile and is a vector of such lists std::vector<std::vector<std::list<item>>> stacks; // an indexed_invslice is created for each map or vehicle tile // each list of items created above for the tile will be added to it std::vector<indexed_invslice> slices; // of one of the above lists so use this as the key when storing the item location std::unordered_map<item *, item_location> opts; // the closest 10 items also have their location added to the invlets vector const char min_invlet = '0'; const char max_invlet = '9'; char cur_invlet = min_invlet; for( const auto &pos : closest_tripoints_first( radius, g->u.pos() ) ) { // second get all matching items on the map within radius if( m.accessible_items( g->u.pos(), pos, radius ) ) { auto items = m.i_at( pos ); // create a new slice and stack for the current map tile stacks.emplace_back(); slices.emplace_back(); // reserve sufficient capacity to ensure reallocation is not required auto ¤t_stack = stacks.back(); current_stack.reserve( items.size() ); for( item &it : items ) { if( ground_filter( it ) ) { auto match = std::find_if( current_stack.begin(), current_stack.end(), [&]( const std::list<item> &e ) { return it.stacks_with( e.back() ); } ); if( match != current_stack.end() ) { match->push_back( it ); } else { // item doesn't stack with any previous so start new list and append to current indexed_invslice current_stack.emplace_back( 1, it ); slices.back().emplace_back( ¤t_stack.back(), INT_MIN ); opts.emplace( ¤t_stack.back().front(), item_location( pos, &it ) ); current_stack.back().front().invlet = ( cur_invlet <= max_invlet ) ? cur_invlet++ : 0; } } } std::string name = trim( std::string( _( "GROUND" ) ) + " " + direction_suffix( g->u.pos(), pos ) ); categories.emplace_back( name, name, rank++ ); inv_s.add_entries( slices.back(), &categories.back() ); } // finally get all matching items in vehicle cargo spaces int part = -1; vehicle *veh = m.veh_at( pos, part ); if( veh && part >= 0 ) { part = veh->part_with_feature( part, "CARGO" ); if( part != -1 ) { auto items = veh->get_items( part ); // create a new slice and stack for the current vehicle part stacks.emplace_back(); slices.emplace_back(); // reserve sufficient capacity to ensure reallocation is not required auto ¤t_stack = stacks.back(); current_stack.reserve( items.size() ); for( item &it : items ) { if( vehicle_filter( it ) ) { auto match = std::find_if( current_stack.begin(), current_stack.end(), [&]( const std::list<item> &e ) { return it.stacks_with( e.back() ); } ); if( match != current_stack.end() ) { match->push_back( it ); } else { // item doesn't stack with any previous so start new list and append to current indexed_invslice current_stack.emplace_back( 1, it ); slices.back().emplace_back( ¤t_stack.back(), INT_MIN ); opts.emplace( ¤t_stack.back().front(), item_location( vehicle_cursor( *veh, part ), &it ) ); current_stack.back().front().invlet = ( cur_invlet <= max_invlet ) ? cur_invlet++ : 0; } } } std::string name = trim( std::string( _( "VEHICLE" ) ) + " " + direction_suffix( g->u.pos(), pos ) ); categories.emplace_back( name, name, rank-- ); inv_s.add_entries( slices.back(), &categories.back() ); } } } 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 inv_s.execute_pick_map( title, opts ); }