/*static*/ bool inventory::has_category(const item& it, item_cat cat, const player& u) { switch (cat) { case IC_COMESTIBLE: // food if (it.is_food(&u) || it.is_food_container(&u)) { return true; } break; case IC_AMMO: // ammo if (it.is_ammo() || it.is_ammo_container()) { return true; } break; case IC_ARMOR: // armour if (it.is_armor()) { return true; } break; case IC_BOOK: // books if (it.is_book()) { return true; } break; case IC_TOOL: // tools if (it.is_tool()) { return true; } break; case IC_CONTAINER: // containers for liquid handling if (it.is_tool() || it.is_gun()) { if (it.ammo_type() == "gasoline") { return true; } } else { if (it.is_container()) { return true; } } break; } return false; }
void Item_modifier::modify(item &new_item) const { if(new_item.is_null()) { return; } new_item.damage = std::min( std::max( (int) rng( damage.first, damage.second ), MIN_ITEM_DAMAGE ), MAX_ITEM_DAMAGE ); long ch = (charges.first == charges.second) ? charges.first : rng(charges.first, charges.second); if(ch != -1) { if( new_item.count_by_charges() || new_item.made_of( LIQUID ) ) { // food, ammo // count_by_charges requires that charges is at least 1. It makes no sense to // spawn a "water (0)" item. new_item.charges = std::max( 1l, ch ); } else if( new_item.is_tool() ) { const auto qty = std::min( ch, new_item.ammo_capacity() ); new_item.charges = qty; if( new_item.ammo_type() != "NULL" && qty > 0 ) { new_item.ammo_set( new_item.ammo_type(), qty ); } } else if( !new_item.is_gun() ) { //not gun, food, ammo or tool. new_item.charges = ch; } } if( new_item.is_gun() && ( ammo.get() != nullptr || ch > 0 ) ) { if( ammo.get() == nullptr ) { // In case there is no explicit ammo item defined, use the default ammo if( new_item.ammo_type() != "NULL" ) { new_item.charges = ch; new_item.set_curammo( new_item.ammo_type() ); } } else { const item am = ammo->create_single( new_item.bday ); new_item.set_curammo( am ); // Prefer explicit charges of the gun, else take the charges of the ammo item, // Gun charges are easier to define: {"item":"gun","charge":10,"ammo-item":"ammo"} if( ch > 0 ) { new_item.charges = ch; } else { new_item.charges = am.charges; } } // Make sure the item is in valid state if( new_item.ammo_data() && new_item.magazine_integral() ) { new_item.charges = std::min( new_item.charges, new_item.ammo_capacity() ); } else { new_item.charges = 0; } } if(container.get() != NULL) { item cont = container->create_single(new_item.bday); if (!cont.is_null()) { if (new_item.made_of(LIQUID)) { long rc = cont.get_remaining_capacity_for_liquid(new_item); if(rc > 0 && (new_item.charges > rc || ch == -1)) { // make sure the container is not over-full. // fill up the container (if using default charges) new_item.charges = rc; } } cont.put_in(new_item); new_item = cont; } } if (contents.get() != NULL) { Item_spawn_data::ItemList contentitems = contents->create(new_item.bday); new_item.contents.insert(new_item.contents.end(), contentitems.begin(), contentitems.end()); } }
void Pickup::pick_one_up( const tripoint &pickup_target, item &newit, vehicle *veh, int cargo_part, int index, int quantity, bool &got_water, bool &offered_swap, PickupMap &mapPickup, bool autopickup ) { player &u = g->u; int moves_taken = 100; bool picked_up = false; pickup_answer option = CANCEL; item leftovers = newit; if( newit.invlet != '\0' && u.invlet_to_position( newit.invlet ) != INT_MIN ) { // Existing invlet is not re-usable, remove it and let the code in player.cpp/inventory.cpp // add a new invlet, otherwise keep the (usable) invlet. newit.invlet = '\0'; } if( quantity != 0 && newit.count_by_charges() ) { // Reinserting leftovers happens after item removal to avoid stacking issues. leftovers.charges = newit.charges - quantity; if( leftovers.charges > 0 ) { newit.charges = quantity; } } else { leftovers.charges = 0; } if( newit.made_of( LIQUID ) ) { got_water = true; } else if( !u.can_pickWeight( newit, false ) ) { add_msg( m_info, _( "The %s is too heavy!" ), newit.display_name().c_str() ); } else if( newit.is_ammo() && ( newit.ammo_type() == ammotype( "arrow" ) || newit.ammo_type() == ammotype( "bolt" ) ) ) { // @todo Make quiver code generic so that ammo pouches can use it too //add ammo to quiver int quivered = handle_quiver_insertion( newit, moves_taken, picked_up ); if( quivered > 0 ) { quantity = quivered; //already picked up some for quiver so use special case handling picked_up = true; option = NUM_ANSWERS; } if( newit.charges > 0 ) { if( !u.can_pickVolume( newit ) ) { if( !autopickup ) { // Silence some messaging if we're doing autopickup. add_msg( m_info, ngettext( "There's no room in your inventory for the %s.", "There's no room in your inventory for the %s.", newit.charges ), newit.tname( newit.charges ).c_str() ); } } else { // Add to inventory instead option = STASH; } } if( option == NUM_ANSWERS ) { //not picking up the rest so //update the charges for the item that gets re-added to the game map leftovers.charges = newit.charges; } } else if( newit.is_bucket() && !newit.is_container_empty() ) { if( !autopickup ) { const std::string &explain = string_format( _( "Can't stash %s while it's not empty" ), newit.display_name().c_str() ); option = handle_problematic_pickup( newit, offered_swap, explain ); } else { option = CANCEL; } } else if( !u.can_pickVolume( newit ) ) { if( !autopickup ) { const std::string &explain = string_format( _( "Not enough capacity to stash %s" ), newit.display_name().c_str() ); option = handle_problematic_pickup( newit, offered_swap, explain ); } else { option = CANCEL; } } else { option = STASH; } switch( option ) { case NUM_ANSWERS: // Some other option break; case CANCEL: picked_up = false; break; case WEAR: picked_up = u.wear_item( newit ); break; case WIELD: picked_up = u.wield( newit ); if( !picked_up ) { break; } if( u.weapon.invlet ) { add_msg( m_info, _( "Wielding %c - %s" ), u.weapon.invlet, u.weapon.display_name().c_str() ); } else { add_msg( m_info, _( "Wielding - %s" ), u.weapon.display_name().c_str() ); } break; case SPILL: if( newit.is_container_empty() ) { debugmsg( "Tried to spill contents from an empty container" ); break; } picked_up = newit.spill_contents( u ); if( !picked_up ) { break; } // Intentional fallthrough case STASH: auto &entry = mapPickup[newit.tname()]; entry.second += newit.count_by_charges() ? newit.charges : 1; entry.first = u.i_add( newit ); picked_up = true; break; } if( picked_up ) { Pickup::remove_from_map_or_vehicle( pickup_target, veh, cargo_part, moves_taken, index ); } if( leftovers.charges > 0 ) { bool to_map = veh == nullptr; if( !to_map ) { to_map = !veh->add_item( cargo_part, leftovers ); } if( to_map ) { g->m.add_item_or_charges( pickup_target, leftovers ); } } }
void Pickup::pick_one_up( const tripoint &pickup_target, item &newit, vehicle *veh, int cargo_part, int index, int quantity, bool &got_water, bool &offered_swap, PickupMap &mapPickup, bool autopickup ) { int moves_taken = 100; bool picked_up = false; item leftovers = newit; if( newit.invlet != '\0' && g->u.invlet_to_position( newit.invlet ) != INT_MIN ) { // Existing invlet is not re-usable, remove it and let the code in player.cpp/inventory.cpp // add a new invlet, otherwise keep the (usable) invlet. newit.invlet = '\0'; } if( quantity != 0 && newit.count_by_charges() ) { // Reinserting leftovers happens after item removal to avoid stacking issues. leftovers.charges = newit.charges - quantity; if( leftovers.charges > 0 ) { newit.charges = quantity; } } else { leftovers.charges = 0; } if( newit.made_of(LIQUID) ) { got_water = true; } else if (!g->u.can_pickWeight(newit.weight(), false)) { add_msg(m_info, _("The %s is too heavy!"), newit.display_name().c_str()); } else if( newit.is_ammo() && (newit.ammo_type() == "arrow" || newit.ammo_type() == "bolt")) { //add ammo to quiver int quivered = handle_quiver_insertion( newit, moves_taken, picked_up); if( newit.charges > 0) { if(!g->u.can_pickVolume( newit.volume())) { if(quivered > 0) { //update the charges for the item that gets re-added to the game map quantity = quivered; leftovers.charges = newit.charges; } if( !autopickup ) { // Silence some messaging if we're doing autopickup. add_msg(m_info, ngettext("There's no room in your inventory for the %s.", "There's no room in your inventory for the %s.", newit.charges), newit.tname(newit.charges).c_str()); } } else { //add to inventory instead item &it = g->u.i_add(newit); picked_up = true; //display output message PickupMap map_pickup; int charges = (newit.count_by_charges()) ? newit.charges : 1; map_pickup.insert(std::pair<std::string, ItemCount>(newit.tname(), ItemCount(it, charges))); show_pickup_message(map_pickup); } } } else if (!g->u.can_pickVolume(newit.volume())) { if( !autopickup ) { // Armor can be instantly worn if (newit.is_armor() && query_yn(_("Put on the %s?"), newit.display_name().c_str())) { if (g->u.wear_item(newit)) { picked_up = true; } } else if (g->u.is_armed()) { if (!g->u.weapon.has_flag("NO_UNWIELD")) { if( !offered_swap ) { offered_swap = true; if ( g->u.weapon.type->id != newit.type->id && query_yn(_("No space for %1$s; wield instead? (drops %2$s)"), newit.display_name().c_str(), g->u.weapon.display_name().c_str()) ) { picked_up = true; g->m.add_item_or_charges( pickup_target, g->u.remove_weapon(), 1 ); g->u.inv.assign_empty_invlet( newit, true ); // force getting an invlet. g->u.wield( &( g->u.i_add(newit) ) ); if (newit.invlet) { add_msg(m_info, _("Wielding %c - %s"), newit.invlet, newit.display_name().c_str()); } else { add_msg(m_info, _("Wielding - %s"), newit.display_name().c_str()); } } } } else { add_msg(m_info, _("There's no room in your inventory for the %s " "and you can't unwield your %s."), newit.display_name().c_str(), g->u.weapon.display_name().c_str()); } } else if( !g->u.is_armed() ) { if (g->u.keep_hands_free) { add_msg(m_info, _("There's no room in your inventory for the %s " "and you have decided to keep your hands free."), newit.display_name().c_str()); } else { g->u.inv.assign_empty_invlet(newit, true); // force getting an invlet. g->u.wield(&(g->u.i_add(newit))); picked_up = true; if (newit.invlet) { add_msg(m_info, _("Wielding %c - %s"), newit.invlet, newit.display_name().c_str()); } else { add_msg(m_info, _("Wielding - %s"), newit.display_name().c_str()); } } } // end of if unarmed } // end of if !autopickup } else { auto &entry = mapPickup[newit.tname()]; entry.second += newit.count_by_charges() ? newit.charges : 1; entry.first = g->u.i_add(newit); picked_up = true; } if(picked_up) { Pickup::remove_from_map_or_vehicle(pickup_target, veh, cargo_part, moves_taken, index); } if( leftovers.charges > 0 ) { bool to_map = veh == nullptr; if( !to_map ) { to_map = !veh->add_item( cargo_part, leftovers ); } if( to_map ) { g->m.add_item_or_charges( pickup_target, leftovers ); } } }
void Item_modifier::modify( item &new_item ) const { if( new_item.is_null() ) { return; } new_item.set_damage( rng( damage.first, damage.second ) ); long ch = ( charges.first == charges.second ) ? charges.first : rng( charges.first, charges.second ); if( ch != -1 ) { if( new_item.count_by_charges() || new_item.made_of( LIQUID ) ) { // food, ammo // count_by_charges requires that charges is at least 1. It makes no sense to // spawn a "water (0)" item. new_item.charges = std::max( 1l, ch ); } else if( new_item.is_tool() ) { const auto qty = std::min( ch, new_item.ammo_capacity() ); new_item.charges = qty; if( new_item.ammo_type() && qty > 0 ) { new_item.ammo_set( new_item.ammo_type()->default_ammotype(), qty ); } } else if( !new_item.is_gun() ) { //not gun, food, ammo or tool. new_item.charges = ch; } } if( ch > 0 && ( new_item.is_gun() || new_item.is_magazine() ) ) { if( ammo == nullptr ) { // In case there is no explicit ammo item defined, use the default ammo if( new_item.ammo_type() ) { new_item.ammo_set( new_item.ammo_type()->default_ammotype(), ch ); } } else { const item am = ammo->create_single( new_item.birthday() ); new_item.ammo_set( am.typeId(), ch ); } // Make sure the item is in valid state if( new_item.ammo_data() && new_item.magazine_integral() ) { new_item.charges = std::min( new_item.charges, new_item.ammo_capacity() ); } else { new_item.charges = 0; } } if( new_item.is_tool() || new_item.is_gun() || new_item.is_magazine() ) { bool spawn_ammo = rng( 0, 99 ) < with_ammo && new_item.ammo_remaining() == 0 && ch == -1 && ( !new_item.is_tool() || new_item.type->tool->rand_charges.empty() ); bool spawn_mag = rng( 0, 99 ) < with_magazine && !new_item.magazine_integral() && !new_item.magazine_current(); if( spawn_mag ) { new_item.contents.emplace_back( new_item.magazine_default(), new_item.birthday() ); } if( spawn_ammo ) { if( ammo ) { const item am = ammo->create_single( new_item.birthday() ); new_item.ammo_set( am.typeId() ); } else { new_item.ammo_set( new_item.ammo_type()->default_ammotype() ); } } } if( container != nullptr ) { item cont = container->create_single( new_item.birthday() ); if( !cont.is_null() ) { if( new_item.made_of( LIQUID ) ) { long rc = cont.get_remaining_capacity_for_liquid( new_item ); if( rc > 0 && ( new_item.charges > rc || ch == -1 ) ) { // make sure the container is not over-full. // fill up the container (if using default charges) new_item.charges = rc; } } cont.put_in( new_item ); new_item = cont; } } if( contents != nullptr ) { Item_spawn_data::ItemList contentitems = contents->create( new_item.birthday() ); new_item.contents.insert( new_item.contents.end(), contentitems.begin(), contentitems.end() ); } for( auto &flag : custom_flags ) { new_item.set_flag( flag ); } }