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(); }
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 ); } }