Skip to content

Commit

Permalink
Replace item_stack::front() with item_stack::only_item()
Browse files Browse the repository at this point in the history
"front" no longer makes sense since items in a colony are unordered.

The new function gives a debug message if there is not exactly one item
at the location.

Also fixed a few bugs caused by previous refactoring and caught in
code review
  • Loading branch information
ifreund committed Jun 19, 2019
1 parent 04c1ffd commit cfc8644
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 176 deletions.
24 changes: 12 additions & 12 deletions src/computer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,12 +882,12 @@ PERTINENT FOREMAN LOGS WILL BE PREPENDED TO NOTES" ),
print_error( _( "ERROR: Please place sample in centrifuge." ) );
} else if( items.size() > 1 ) {
print_error( _( "ERROR: Please remove all but one sample from centrifuge." ) );
} else if( items.front().contents.empty() ) {
} else if( items.only_item().contents.empty() ) {
print_error( _( "ERROR: Please only use container with blood sample." ) );
} else if( items.front().contents.front().typeId() != "blood" ) {
} else if( items.only_item().contents.front().typeId() != "blood" ) {
print_error( _( "ERROR: Please only use blood samples." ) );
} else { // Success!
const item &blood = items.front().contents.front();
const item &blood = items.only_item().contents.front();
const mtype *mt = blood.get_mtype();
if( mt == nullptr || mt->id == mtype_id::NULL_ID() ) {
print_line( _( "Result: Human blood, no pathogens found." ) );
Expand Down Expand Up @@ -927,13 +927,13 @@ PERTINENT FOREMAN LOGS WILL BE PREPENDED TO NOTES" ),
print_error( _( "ERROR: Please place memory bank in scan area." ) );
} else if( items.size() > 1 ) {
print_error( _( "ERROR: Please only scan one item at a time." ) );
} else if( items.front().typeId() != "usb_drive" &&
items.front().typeId() != "black_box" ) {
} else if( items.only_item().typeId() != "usb_drive" &&
items.only_item().typeId() != "black_box" ) {
print_error( _( "ERROR: Memory bank destroyed or not present." ) );
} else if( items.front().typeId() == "usb_drive" && items.front().contents.empty() ) {
} else if( items.only_item().typeId() == "usb_drive" && items.only_item().contents.empty() ) {
print_error( _( "ERROR: Memory bank is empty." ) );
} else { // Success!
if( items.front().typeId() == "black_box" ) {
if( items.only_item().typeId() == "black_box" ) {
print_line( _( "Memory Bank: Military Hexron Encryption\nPrinting Transcript\n" ) );
item transcript( "black_box_transcript", calendar::turn );
g->m.add_item_or_charges( g->u.posx(), g->u.posy(), transcript );
Expand Down Expand Up @@ -1624,11 +1624,11 @@ void computer::activate_failure( computer_failure_type fail )
print_error( _( "ERROR: Please place sample in centrifuge." ) );
} else if( items.size() > 1 ) {
print_error( _( "ERROR: Please remove all but one sample from centrifuge." ) );
} else if( items.front().typeId() != "vacutainer" ) {
} else if( items.only_item().typeId() != "vacutainer" ) {
print_error( _( "ERROR: Please use blood-contained samples." ) );
} else if( items.front().contents.empty() ) {
} else if( items.only_item().contents.empty() ) {
print_error( _( "ERROR: Blood draw kit, empty." ) );
} else if( items.front().contents.front().typeId() != "blood" ) {
} else if( items.only_item().contents.front().typeId() != "blood" ) {
print_error( _( "ERROR: Please only use blood samples." ) );
} else {
print_error( _( "ERROR: Blood sample destroyed." ) );
Expand All @@ -1649,9 +1649,9 @@ void computer::activate_failure( computer_failure_type fail )
print_error( _( "ERROR: Please place memory bank in scan area." ) );
} else if( items.size() > 1 ) {
print_error( _( "ERROR: Please only scan one item at a time." ) );
} else if( items.front().typeId() != "usb_drive" ) {
} else if( items.only_item().typeId() != "usb_drive" ) {
print_error( _( "ERROR: Memory bank destroyed or not present." ) );
} else if( items.front().contents.empty() ) {
} else if( items.only_item().contents.empty() ) {
print_error( _( "ERROR: Memory bank is empty." ) );
} else {
print_error( _( "ERROR: Data bank destroyed." ) );
Expand Down
2 changes: 1 addition & 1 deletion src/editmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ void editmap::update_view( bool update_info )
const int target_stack_size = target_stack.size();
if( !g->m.has_flag( "CONTAINER", target ) && target_stack_size > 0 ) {
trim_and_print( w_info, off, 1, getmaxx( w_info ), c_light_gray, _( "There is a %s there." ),
target_stack.front().tname() );
target_stack.begin()->tname() );
off++;
if( target_stack_size > 1 ) {
mvwprintw( w_info, off, 1, ngettext( "There is %d other item there as well.",
Expand Down
14 changes: 9 additions & 5 deletions src/faction_camp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1909,25 +1909,29 @@ static std::pair<size_t, std::string> farm_action( const tripoint &omt_tgt, farm
}
break;
case farm_ops::harvest:
if( farm_map.furn( pos ) == f_plant_harvest && !farm_map.i_at( pos ).empty() ) {
const item &seed = farm_map.i_at( pos ).front();
if( farm_valid_seed( seed ) ) {
if( farm_map.furn( pos ) == f_plant_harvest ) {
// Can't use item_stack::only_item() since there might be fertilizer
map_stack items = farm_map.i_at( pos );
const map_stack::iterator seed = std::find_if( items.begin(), items.end(), []( const item & it ) {
return it.is_seed();
} );
if( seed != items.end() && farm_valid_seed( *seed ) ) {
plots_cnt += 1;
if( comp ) {
int skillLevel = comp->get_skill_level( skill_survival );
///\EFFECT_SURVIVAL increases number of plants harvested from a seed
int plant_cnt = rng( skillLevel / 2, skillLevel );
plant_cnt = std::min( std::max( plant_cnt, 1 ), 9 );
int seed_cnt = std::max( 1, rng( plant_cnt / 4, plant_cnt / 2 ) );
for( auto &i : iexamine::get_harvest_items( *seed.type, plant_cnt,
for( auto &i : iexamine::get_harvest_items( *seed->type, plant_cnt,
seed_cnt, true ) ) {
g->m.add_item_or_charges( g->u.pos(), i );
}
farm_map.i_clear( pos );
farm_map.furn_set( pos, f_null );
farm_map.ter_set( pos, t_dirt );
} else {
plant_names.insert( item::nname( itype_id( seed.type->seed->fruit_id ) ) );
plant_names.insert( item::nname( itype_id( seed->type->seed->fruit_id ) ) );
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/fungal_effects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,16 @@ void fungal_effects::spread_fungus_one_tile( const tripoint &p, const int growth
}
} else if( m.has_flag( "PLANT", p ) ) {
// Replace the (already existing) seed
if( !m.i_at( p ).empty() ) {
m.i_at( p ).front() = item( "fungal_seeds", calendar::turn );
} else {
// Can't use item_stack::only_item() since there might be fertilizer
map_stack items = m.i_at( p );
const map_stack::iterator seed = std::find_if( items.begin(), items.end(), []( const item & it ) {
return it.is_seed();
} );
if( seed == items.end() || !seed->is_seed() ) {
DebugLog( D_ERROR, DC_ALL ) << "No seed item in the PLANT terrain at position " <<
string_format( "%d,%d,%d.", p.x, p.y, p.z );
} else {
*seed = item( "fungal_seeds", calendar::turn );
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5119,23 +5119,23 @@ bool game::forced_door_closing( const tripoint &p, const ter_id &door_type, int

m.ter_set( x, y, door_type );
if( m.has_flag( "NOITEM", x, y ) ) {
auto items = m.i_at( x, y );
while( !items.empty() ) {
if( items.front().made_of( LIQUID ) ) {
m.i_rem( x, y, 0 );
map_stack items = m.i_at( x, y );
for( map_stack::iterator it = items.begin(); it != items.end(); ) {
if( it->made_of( LIQUID ) ) {
it = items.erase( it );
continue;
}
if( items.front().made_of( material_id( "glass" ) ) && one_in( 2 ) ) {
if( it->made_of( material_id( "glass" ) ) && one_in( 2 ) ) {
if( can_see ) {
add_msg( m_warning, _( "A %s shatters!" ), items.front().tname() );
add_msg( m_warning, _( "A %s shatters!" ), it->tname() );
} else {
add_msg( m_warning, _( "Something shatters!" ) );
}
m.i_rem( x, y, 0 );
it = items.erase( it );
continue;
}
m.add_item_or_charges( kbx, kby, items.front() );
m.i_rem( x, y, 0 );
m.add_item_or_charges( kbx, kby, *it );
it = items.erase( it );
}
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/gates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ void doors::close_door( map &m, Character &who, const tripoint &closep )
m.close_door( closep, inside, false );
didit = true;
who.add_msg_if_player( m_info, _( "You push the %s out of the way." ),
items_in_way.size() == 1 ? items_in_way.front().tname() : _( "stuff" ) );
items_in_way.size() == 1 ? items_in_way.only_item().tname() : _( "stuff" ) );
who.mod_moves( -std::min( items_in_way.stored_volume() / ( max_nudge / 50 ), 100 ) );

if( m.has_flag( "NOITEM", closep ) ) {
Expand Down
Loading

0 comments on commit cfc8644

Please sign in to comment.