Skip to content

Commit

Permalink
Merge pull request #77109 from sparr/reduce_ter_furn_check_duplication
Browse files Browse the repository at this point in the history
Refactor repeated map.ter .furn .*_at calls
  • Loading branch information
Maleclypse authored Nov 3, 2024
2 parents 9f98a02 + 964eb6a commit d14f093
Show file tree
Hide file tree
Showing 48 changed files with 473 additions and 498 deletions.
58 changes: 25 additions & 33 deletions src/activity_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,7 @@ void hacksaw_activity_actor::start( player_activity &act, Character &/*who*/ )
int moves_before_quality;

if( here.has_furn( target ) ) {
const furn_id furn_type = here.furn( target );
const furn_id &furn_type = here.furn( target );
if( !furn_type->hacksaw->valid() ) {
if( !testing ) {
debugmsg( "%s hacksaw is invalid", furn_type.id().str() );
Expand All @@ -1356,8 +1356,7 @@ void hacksaw_activity_actor::start( player_activity &act, Character &/*who*/ )
}

moves_before_quality = to_moves<int>( furn_type->hacksaw->duration() );
} else if( !here.ter( target )->is_null() ) {
const ter_id ter_type = here.ter( target );
} else if( const ter_id &ter_type = here.ter( target ); !ter_type->is_null() ) {
if( !ter_type->hacksaw->valid() ) {
if( !testing ) {
debugmsg( "%s hacksaw is invalid", ter_type.id().str() );
Expand Down Expand Up @@ -1466,7 +1465,7 @@ void hacksaw_activity_actor::finish( player_activity &act, Character &who )
const activity_data_common *data;

if( here.has_furn( target ) ) {
const furn_id furn_type = here.furn( target );
const furn_id &furn_type = here.furn( target );
if( !furn_type->hacksaw->valid() ) {
if( !testing ) {
debugmsg( "%s hacksaw is invalid", furn_type.id().str() );
Expand All @@ -1486,8 +1485,7 @@ void hacksaw_activity_actor::finish( player_activity &act, Character &who )

data = static_cast<const activity_data_common *>( &*furn_type->hacksaw );
here.furn_set( target, new_furn );
} else if( !here.ter( target )->is_null() ) {
const ter_id ter_type = here.ter( target );
} else if( const ter_id &ter_type = here.ter( target ); !ter_type->is_null() ) {
if( !ter_type->hacksaw->valid() ) {
if( !testing ) {
debugmsg( "%s hacksaw is invalid", ter_type.id().str() );
Expand Down Expand Up @@ -2611,7 +2609,7 @@ void boltcutting_activity_actor::start( player_activity &act, Character &/*who*/
const map &here = get_map();

if( here.has_furn( target ) ) {
const furn_id furn_type = here.furn( target );
const furn_id &furn_type = here.furn( target );
if( !furn_type->boltcut->valid() ) {
if( !testing ) {
debugmsg( "%s boltcut is invalid", furn_type.id().str() );
Expand All @@ -2621,8 +2619,7 @@ void boltcutting_activity_actor::start( player_activity &act, Character &/*who*/
}

act.moves_total = to_moves<int>( furn_type->boltcut->duration() );
} else if( !here.ter( target )->is_null() ) {
const ter_id ter_type = here.ter( target );
} else if( const ter_id &ter_type = here.ter( target ); !ter_type->is_null() ) {
if( !ter_type->boltcut->valid() ) {
if( !testing ) {
debugmsg( "%s boltcut is invalid", ter_type.id().str() );
Expand Down Expand Up @@ -2664,7 +2661,7 @@ void boltcutting_activity_actor::finish( player_activity &act, Character &who )
const activity_data_common *data;

if( here.has_furn( target ) ) {
const furn_id furn_type = here.furn( target );
const furn_id &furn_type = here.furn( target );
if( !furn_type->boltcut->valid() ) {
if( !testing ) {
debugmsg( "%s boltcut is invalid", furn_type.id().str() );
Expand All @@ -2684,8 +2681,7 @@ void boltcutting_activity_actor::finish( player_activity &act, Character &who )

data = static_cast<const activity_data_common *>( &*furn_type->boltcut );
here.furn_set( target, new_furn );
} else if( !here.ter( target )->is_null() ) {
const ter_id ter_type = here.ter( target );
} else if( const ter_id &ter_type = here.ter( target ); !ter_type->is_null() ) {
if( !ter_type->boltcut->valid() ) {
if( !testing ) {
debugmsg( "%s boltcut is invalid", ter_type.id().str() );
Expand Down Expand Up @@ -2812,8 +2808,8 @@ void lockpick_activity_actor::finish( player_activity &act, Character &who )

map &here = get_map();
const tripoint_bub_ms target = here.bub_from_abs( this->target );
const ter_id ter_type = here.ter( target );
const furn_id furn_type = here.furn( target );
const ter_id &ter_type = here.ter( target );
const furn_id &furn_type = here.furn( target );
optional_vpart_position const veh = here.veh_at( target );
int locked_part = -1;
ter_id new_ter_type;
Expand Down Expand Up @@ -2981,7 +2977,7 @@ std::optional<tripoint_bub_ms> lockpick_activity_actor::select_location( avatar
return target;
}

const ter_id terr_type = get_map().ter( *target );
const ter_id &terr_type = get_map().ter( *target );
if( *target == you.pos_bub() ) {
you.add_msg_if_player( m_info, _( "You pick your nose and your sinuses swing open." ) );
} else if( get_creature_tracker().creature_at<npc>( *target ) ) {
Expand Down Expand Up @@ -4327,7 +4323,7 @@ void harvest_activity_actor::start( player_activity &act, Character &who )
map &here = get_map();

if( here.has_furn( target ) ) {
const furn_id furn = here.furn( target );
const furn_id &furn = here.furn( target );

if( furn->has_examine( iexamine::harvest_furn ) ) {
// TODO: Should be generified as a harvest field
Expand Down Expand Up @@ -5398,7 +5394,7 @@ void oxytorch_activity_actor::start( player_activity &act, Character &/*who*/ )
const map &here = get_map();

if( here.has_furn( target ) ) {
const furn_id furn_type = here.furn( target );
const furn_id &furn_type = here.furn( target );
if( !furn_type->oxytorch->valid() ) {
if( !testing ) {
debugmsg( "%s oxytorch is invalid", furn_type.id().str() );
Expand All @@ -5408,8 +5404,7 @@ void oxytorch_activity_actor::start( player_activity &act, Character &/*who*/ )
}

act.moves_total = to_moves<int>( furn_type->oxytorch->duration() );
} else if( !here.ter( target )->is_null() ) {
const ter_id ter_type = here.ter( target );
} else if( const ter_id &ter_type = here.ter( target ); !ter_type->is_null() ) {
if( !ter_type->oxytorch->valid() ) {
if( !testing ) {
debugmsg( "%s oxytorch is invalid", ter_type.id().str() );
Expand Down Expand Up @@ -5455,7 +5450,7 @@ void oxytorch_activity_actor::finish( player_activity &act, Character &who )
const activity_data_common *data;

if( here.has_furn( target ) ) {
const furn_id furn_type = here.furn( target );
const furn_id &furn_type = here.furn( target );
if( !furn_type->oxytorch->valid() ) {
if( !testing ) {
debugmsg( "%s oxytorch is invalid", furn_type.id().str() );
Expand All @@ -5475,8 +5470,7 @@ void oxytorch_activity_actor::finish( player_activity &act, Character &who )

data = static_cast<const activity_data_common *>( &*furn_type->oxytorch );
here.furn_set( target, new_furn );
} else if( !here.ter( target )->is_null() ) {
const ter_id ter_type = here.ter( target );
} else if( const ter_id &ter_type = here.ter( target ); !ter_type->is_null() ) {
if( !ter_type->oxytorch->valid() ) {
if( !testing ) {
debugmsg( "%s oxytorch is invalid", ter_type.id().str() );
Expand Down Expand Up @@ -5854,7 +5848,7 @@ void prying_activity_actor::start( player_activity &act, Character &who )
const map &here = get_map();

if( here.has_furn( target ) ) {
const furn_id furn_type = here.furn( target );
const furn_id &furn_type = here.furn( target );
if( !furn_type->prying->valid() ) {
if( !testing ) {
debugmsg( "%s prying is invalid", furn_type.id().str() );
Expand All @@ -5866,8 +5860,7 @@ void prying_activity_actor::start( player_activity &act, Character &who )
prying_nails = furn_type->prying->prying_data().prying_nails;
act.moves_total = to_moves<int>(
prying_time( *furn_type->prying, tool, who ) );
} else if( !here.ter( target )->is_null() ) {
const ter_id ter_type = here.ter( target );
} else if( const ter_id &ter_type = here.ter( target ); !ter_type->is_null() ) {
if( !ter_type->prying->valid() ) {
if( !testing ) {
debugmsg( "%s prying is invalid", ter_type.id().str() );
Expand Down Expand Up @@ -5965,7 +5958,7 @@ void prying_activity_actor::handle_prying( Character &who )
};

if( here.has_furn( target ) ) {
const furn_id furn_type = here.furn( target );
const furn_id &furn_type = here.furn( target );
if( !furn_type->prying->valid() ) {
if( !testing ) {
debugmsg( "%s prying is invalid", furn_type.id().str() );
Expand All @@ -5988,8 +5981,7 @@ void prying_activity_actor::handle_prying( Character &who )
}

here.furn_set( target, new_furn );
} else if( !here.ter( target )->is_null() ) {
const ter_id ter_type = here.ter( target );
} else if( const ter_id &ter_type = here.ter( target ); !ter_type->is_null() ) {
if( !ter_type->prying->valid() ) {
if( !testing ) {
debugmsg( "%s prying is invalid", ter_type.id().str() );
Expand Down Expand Up @@ -6055,7 +6047,7 @@ void prying_activity_actor::handle_prying_nails( Character &who )
const activity_data_common *data;

if( here.has_furn( target ) ) {
const furn_id furn_type = here.furn( target );
const furn_id &furn_type = here.furn( target );
if( !furn_type->prying->valid() ) {
if( !testing ) {
debugmsg( "%s prying is invalid", furn_type.id().str() );
Expand All @@ -6073,8 +6065,7 @@ void prying_activity_actor::handle_prying_nails( Character &who )

data = static_cast<const activity_data_common *>( &*furn_type->prying );
here.furn_set( target, new_furn );
} else if( !here.ter( target )->is_null() ) {
const ter_id ter_type = here.ter( target );
} else if( const ter_id &ter_type = here.ter( target ); !ter_type->is_null() ) {
if( !ter_type->prying->valid() ) {
if( !testing ) {
debugmsg( "%s prying is invalid", ter_type.id().str() );
Expand Down Expand Up @@ -6429,11 +6420,12 @@ void chop_logs_activity_actor::finish( player_activity &act, Character &who )
int log_quan;
int stick_quan;
int splint_quan;
if( here.ter( pos ) == ter_t_trunk ) {
const ter_id &t = here.ter( pos );
if( t == ter_t_trunk ) {
log_quan = rng( 2, 3 );
stick_quan = rng( 0, 3 );
splint_quan = 0;
} else if( here.ter( pos ) == ter_t_stump ) {
} else if( t == ter_t_stump ) {
log_quan = rng( 0, 2 );
stick_quan = 0;
splint_quan = rng( 5, 15 );
Expand Down
8 changes: 4 additions & 4 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1724,23 +1724,23 @@ void activity_handlers::fill_liquid_do_turn( player_activity *act, Character *yo
if( here.ter( source_pos )->has_examine( iexamine::gaspump ) ) {
add_msg( _( "With a clang and a shudder, the %s pump goes silent." ),
liquid.type_name( 1 ) );
} else if( here.furn( source_pos )->has_examine( iexamine::fvat_full ) ) {
} else if( const furn_id &f = here.furn( source_pos ); f->has_examine( iexamine::fvat_full ) ) {
add_msg( _( "You squeeze the last drops of %s from the vat." ),
liquid.type_name( 1 ) );
map_stack items_here = here.i_at( source_pos );
if( items_here.empty() ) {
if( here.furn( source_pos ) == furn_f_fvat_wood_full ) {
if( f == furn_f_fvat_wood_full ) {
here.furn_set( source_pos, furn_f_fvat_wood_empty );
} else {
here.furn_set( source_pos, furn_f_fvat_empty );
}
}
} else if( here.furn( source_pos )->has_examine( iexamine::compost_full ) ) {
} else if( f->has_examine( iexamine::compost_full ) ) {
add_msg( _( "You squeeze the last drops of %s from the tank." ),
liquid.type_name( 1 ) );
map_stack items_here = here.i_at( source_pos );
if( items_here.empty() ) {
if( here.furn( source_pos ) == furn_f_compost_full ) {
if( f == furn_f_compost_full ) {
here.furn_set( source_pos, furn_f_compost_empty );
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/activity_item_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,8 +836,8 @@ construction const *_find_prereq( tripoint_bub_ms const &loc, construction_id co
bool already_done( construction const &build, tripoint_bub_ms const &loc )
{
map &here = get_map();
const furn_id furn = here.furn( loc );
const ter_id ter = here.ter( loc );
const furn_id &furn = here.furn( loc );
const ter_id &ter = here.ter( loc );
return !build.post_terrain.empty() &&
( ( !build.post_is_furniture && ter_id( build.post_terrain ) == ter ) ||
( build.post_is_furniture && furn_id( build.post_terrain ) == furn ) );
Expand Down Expand Up @@ -1179,8 +1179,8 @@ static activity_reason_info can_do_activity_there( const activity_id &act, Chara
}
}
if( act == ACT_MULTIPLE_CHOP_TREES ) {
if( here.has_flag( ter_furn_flag::TFLAG_TREE, src_loc ) || here.ter( src_loc ) == ter_t_trunk ||
here.ter( src_loc ) == ter_t_stump ) {
const ter_id &t = here.ter( src_loc );
if( t == ter_t_trunk || t == ter_t_stump || here.has_flag( ter_furn_flag::TFLAG_TREE, src_loc ) ) {
if( you.has_quality( qual_AXE ) ) {
return activity_reason_info::ok( do_activity_reason::NEEDS_TREE_CHOPPING );
} else {
Expand Down Expand Up @@ -2470,7 +2470,7 @@ static bool chop_tree_activity( Character &you, const tripoint_bub_ms &src_loc )
you.consume_charges( best_qual, best_qual.type->charges_to_use() );
}
map &here = get_map();
const ter_id ter = here.ter( src_loc );
const ter_id &ter = here.ter( src_loc );
if( here.has_flag( ter_furn_flag::TFLAG_TREE, src_loc ) ) {
you.assign_activity( chop_tree_activity_actor( moves, item_location( you, &best_qual ) ) );
you.activity.placement = here.getglobal( src_loc );
Expand Down Expand Up @@ -2666,7 +2666,7 @@ static std::unordered_set<tripoint_abs_ms> generic_multi_activity_locations(
continue;
}
if( act_id == ACT_MULTIPLE_FISH ) {
const ter_id terrain_id = here.ter( set_pt );
const ter_id &terrain_id = here.ter( set_pt );
if( !terrain_id.obj().has_flag( ter_furn_flag::TFLAG_DEEP_WATER ) ) {
it2 = src_set.erase( it2 );
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/avatar_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,10 +561,10 @@ bool avatar_action::move( avatar &you, map &m, const tripoint &d )
if( waste_moves ) {
you.mod_moves( -you.get_speed() );
}
} else if( m.ter( dest_loc ) == ter_t_door_bar_locked ) {
} else if( const ter_id &t = m.ter( dest_loc ); t == ter_t_door_bar_locked ) {
add_msg( _( "You rattle the bars but the door is locked!" ) );
} else if( const std::unordered_set<ter_str_id> locked_doors = { ter_t_door_locked, ter_t_door_locked_peep, ter_t_door_locked_alarm, ter_t_door_locked_interior };
locked_doors.find( m.ter( dest_loc ).id() ) != locked_doors.end() ) {
locked_doors.find( t.id() ) != locked_doors.end() ) {
// Don't drain move points for learning something you could learn just by looking
add_msg( _( "That door is locked!" ) );
}
Expand Down
5 changes: 3 additions & 2 deletions src/bionics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2415,8 +2415,9 @@ float Character::env_surgery_bonus( int radius ) const
float bonus = 1.0f;
map &here = get_map();
for( const tripoint_bub_ms &cell : here.points_in_radius( pos_bub(), radius ) ) {
if( here.furn( cell )->surgery_skill_multiplier ) {
bonus = std::max( bonus, *here.furn( cell )->surgery_skill_multiplier );
const furn_id &f = here.furn( cell );
if( f->surgery_skill_multiplier ) {
bonus = std::max( bonus, *f->surgery_skill_multiplier );
}
}
return bonus;
Expand Down
7 changes: 4 additions & 3 deletions src/cata_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3596,15 +3596,16 @@ bool cata_tiles::draw_field_or_item( const tripoint &p, const lit_level ll, int
const auto fld_override = field_override.find( tripoint_bub_ms( p ) );
const bool fld_overridden = fld_override != field_override.end();
map &here = get_map();
const field &f = here.field_at( p );
const field_type_id &fld = fld_overridden ?
fld_override->second : here.field_at( p ).displayed_field_type();
fld_override->second : f.displayed_field_type();
bool ret_draw_field = false;
bool ret_draw_items = false;
// go through each field and draw it
if( !fld_overridden ) {
const maptile &tile = here.maptile_at( p );

for( const std::pair<const field_type_id, field_entry> &fd_pr : here.field_at( p ) ) {
for( const std::pair<const field_type_id, field_entry> &fd_pr : f ) {
const field_type_id &fld = fd_pr.first;
if( !invisible[0] && fld.obj().display_field ) {
const lit_level lit = ll;
Expand Down Expand Up @@ -4989,7 +4990,7 @@ void cata_tiles::get_terrain_orientation( const tripoint_bub_ms &p, int &rota, i
};

// get terrain at x,y
const ter_id tid = ter( p, invisible[0] );
const ter_id &tid = ter( p, invisible[0] );
if( tid == ter_str_id::NULL_ID() ) {
subtile = 0;
rota = 0;
Expand Down
20 changes: 12 additions & 8 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1421,18 +1421,22 @@ bool Character::sight_impaired() const
bool Character::has_alarm_clock() const
{
map &here = get_map();
return cache_has_item_with_flag( flag_ALARMCLOCK, true ) ||
( here.veh_at( pos_bub() ) &&
!empty( here.veh_at( pos_bub() )->vehicle().get_avail_parts( "ALARMCLOCK" ) ) ) ||
if( cache_has_item_with_flag( flag_ALARMCLOCK, true ) ) {
return true;
}
const optional_vpart_position &vp = here.veh_at( pos_bub() );
return ( vp && !empty( vp->vehicle().get_avail_parts( "ALARMCLOCK" ) ) ) ||
has_flag( json_flag_ALARMCLOCK );
}

bool Character::has_watch() const
{
map &here = get_map();
return cache_has_item_with_flag( flag_WATCH, true ) ||
( here.veh_at( pos_bub() ) &&
!empty( here.veh_at( pos_bub() )->vehicle().get_avail_parts( "WATCH" ) ) ) ||
if( cache_has_item_with_flag( flag_WATCH, true ) ) {
return true;
}
const optional_vpart_position &vp = here.veh_at( pos_bub() );
return ( vp && !empty( vp->vehicle().get_avail_parts( "WATCH" ) ) ) ||
has_flag( json_flag_WATCH );
}

Expand Down Expand Up @@ -9142,8 +9146,8 @@ units::temperature_delta Character::floor_bedding_warmth( const tripoint &pos )
{
map &here = get_map();
const trap &trap_at_pos = here.tr_at( pos );
const ter_id ter_at_pos = here.ter( pos );
const furn_id furn_at_pos = here.furn( pos );
const ter_id &ter_at_pos = here.ter( pos );
const furn_id &furn_at_pos = here.furn( pos );

const optional_vpart_position vp = here.veh_at( pos );
const std::optional<vpart_reference> boardable = vp.part_with_feature( "BOARDABLE", true );
Expand Down
2 changes: 1 addition & 1 deletion src/character_body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ void Character::update_bodytemp()
const int climate_control_heat = climate_control.first;
const int climate_control_chill = climate_control.second;
const bool use_floor_warmth = can_use_floor_warmth();
const furn_id furn_at_pos = here.furn( pos_bub() );
const furn_id &furn_at_pos = here.furn( pos_bub() );
const std::optional<vpart_reference> boardable = vp.part_with_feature( "BOARDABLE", true );
// This means which temperature is comfortable for a naked person
// Ambient normal temperature is lower while asleep
Expand Down
Loading

0 comments on commit d14f093

Please sign in to comment.