/*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; case IC_GUN: if(it.is_gun()) { return true; } break; } return false; }
virtual void select(int entnum, uimenu *menu) { const int starty = 3; const int startx = menu->w_width - menu->pad_right; itype *ity = item_controller->find_template(standard_itype_ids[entnum]); std::string padding = std::string(menu->pad_right - 1, ' '); for(int i = 0; i < lastlen + starty + 1; i++ ) { mvwprintw(menu->window, 1 + i, startx, "%s", padding.c_str() ); } if ( ity != NULL ) { tmp.make(item_controller->find_template(standard_itype_ids[entnum])); tmp.bday = g->turn; if (tmp.is_tool()) { tmp.charges = dynamic_cast<it_tool *>(tmp.type)->max_charges; } else if (tmp.is_ammo()) { tmp.charges = 100; } else if (tmp.is_gun()) { tmp.charges = 0; } else if (tmp.is_gunmod() && (tmp.has_flag("MODE_AUX") || tmp.typeId() == "spare_mag")) { tmp.charges = 0; } else { tmp.charges = -1; } if( tmp.is_stationary() ) { tmp.note = SNIPPET.assign( (dynamic_cast<it_stationary *>(tmp.type))->category ); } std::vector<std::string> desc = foldstring(tmp.info(true), menu->pad_right - 1); int dsize = desc.size(); if ( dsize > menu->w_height - 5 ) { dsize = menu->w_height - 5; } lastlen = dsize; std::string header = string_format("#%d: %s%s", entnum, standard_itype_ids[entnum].c_str(), ( incontainer ? " (contained)" : "" ) ); mvwprintz(menu->window, 1, startx + ( menu->pad_right - 1 - header.size() ) / 2, c_cyan, "%s", header.c_str() ); for(int i = 0; i < desc.size(); i++ ) { mvwprintw(menu->window, starty + i, startx, "%s", desc[i].c_str() ); } mvwprintz(menu->window, menu->w_height - 3, startx, c_green, "%s", msg.c_str()); msg = padding; mvwprintw(menu->window, menu->w_height - 2, startx, "[/] find, [f] container, [q]uit"); } }
static bool gun_has_item( const item &gun, const item *it ) { if( !gun.is_gun() ) { return false; } if( gun.magazine_current() == it ) { return true; } auto gms = gun.gunmods(); return !gms.empty() && std::find( gms.begin(), gms.end(), it ) != gms.end(); }
bool Character::i_add_or_drop(item& it, int qty) { bool retval = true; bool drop = false; inv.assign_empty_invlet(it); for (int i = 0; i < qty; ++i) { if (!drop && (!can_pickWeight(it.weight(), !OPTIONS["DANGEROUS_PICKUPS"]) || !can_pickVolume(it.volume()))) { drop = true; } if( drop ) { retval &= !g->m.add_item_or_charges( pos(), it ).is_null(); } else if ( !( it.has_flag("IRREMOVEABLE") && !it.is_gun() ) ){ i_add(it); } } return retval; }
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 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 ); } }