Skip to content

Commit

Permalink
Map reference migration part 2 (cataclysmbnteam#1733)
Browse files Browse the repository at this point in the history
(cherry picked from commit ace8231a0922b36581f5061053bdc275515724b5)

Co-authored-by: Kevin Granade <kevin.granade@gmail.com>
  • Loading branch information
olanti-p and kevingranade authored Jul 21, 2022
1 parent 5904154 commit 044554a
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 132 deletions.
9 changes: 5 additions & 4 deletions src/advanced_inv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ void advanced_inventory::print_items( const advanced_inventory_pane &pane, bool
} else if( pane.in_vehicle() ) {
maxvolume = s.veh->max_volume( s.vstor );
} else {
maxvolume = g->m.max_volume( s.pos );
maxvolume = get_map().max_volume( s.pos );
}
formatted_head = string_format( "%3.1f %s %s/%s %s",
convert_weight( s.weight ),
Expand Down Expand Up @@ -838,6 +838,7 @@ bool advanced_inventory::move_all_items( bool nested_call )
do_return_entry();
}

map &here = get_map();
if( spane.get_area() == AIM_INVENTORY || spane.get_area() == AIM_WORN ) {
drop_locations dropped;
// keep a list of favorites separated, only drop non-fav first if they exist
Expand Down Expand Up @@ -912,7 +913,7 @@ bool advanced_inventory::move_all_items( bool nested_call )
stack_begin = targets.begin();
stack_end = targets.end();
} else {
map_stack targets = g->m.i_at( sarea.pos );
map_stack targets = here.i_at( sarea.pos );
stack_begin = targets.begin();
stack_end = targets.end();
}
Expand Down Expand Up @@ -1097,7 +1098,7 @@ void advanced_inventory::change_square( const aim_location changeSquare,
} else {
// check item stacks in vehicle and map at said square
auto sq = squares[changeSquare];
auto map_stack = g->m.i_at( sq.pos );
auto map_stack = get_map().i_at( sq.pos );
auto veh_stack = sq.veh->get_items( sq.vstor );
// auto switch to vehicle storage if vehicle items are there, or neither are there
if( !veh_stack.empty() || map_stack.empty() ) {
Expand Down Expand Up @@ -1830,7 +1831,7 @@ void advanced_inventory::draw_minimap()
// get the center of the window
tripoint pc = {getmaxx( minimap ) / 2, getmaxy( minimap ) / 2, 0};
// draw the 3x3 tiles centered around player
g->m.draw( minimap, g->u.pos() );
get_map().draw( minimap, g->u.pos() );
for( auto s : sides ) {
char sym = get_minimap_sym( s );
if( sym == '\0' ) {
Expand Down
27 changes: 14 additions & 13 deletions src/advanced_inv_area.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ int advanced_inv_area::get_item_count() const
} else if( id == AIM_DRAGGED ) {
return can_store_in_vehicle() ? veh->get_items( vstor ).size() : 0;
} else {
return g->m.i_at( pos ).size();
return get_map().i_at( pos ).size();
}
}

Expand All @@ -67,6 +67,7 @@ void advanced_inv_area::init()
volume = 0_ml;
// must update in main function
weight = 0_gram;
map &here = get_map();
switch( id ) {
case AIM_INVENTORY:
case AIM_WORN:
Expand All @@ -82,7 +83,7 @@ void advanced_inv_area::init()
off = g->u.grab_point;
// Reset position because offset changed
pos = g->u.pos() + off;
if( const cata::optional<vpart_reference> vp = g->m.veh_at( pos ).part_with_feature( "CARGO",
if( const cata::optional<vpart_reference> vp = here.veh_at( pos ).part_with_feature( "CARGO",
false ) ) {
veh = &vp->vehicle();
vstor = vp->part_index();
Expand Down Expand Up @@ -131,23 +132,23 @@ void advanced_inv_area::init()
case AIM_NORTH:
case AIM_NORTHEAST: {
const cata::optional<vpart_reference> vp =
g->m.veh_at( pos ).part_with_feature( "CARGO", false );
here.veh_at( pos ).part_with_feature( "CARGO", false );
if( vp ) {
veh = &vp->vehicle();
vstor = vp->part_index();
} else {
veh = nullptr;
vstor = -1;
}
canputitemsloc = can_store_in_vehicle() || g->m.can_put_items_ter_furn( pos );
canputitemsloc = can_store_in_vehicle() || here.can_put_items_ter_furn( pos );
max_size = MAX_ITEM_IN_SQUARE;
if( can_store_in_vehicle() ) {
std::string part_name = vp->info().name();
desc[1] = vp->get_label().value_or( part_name );
}
// get graffiti or terrain name
desc[0] = g->m.has_graffiti_at( pos ) ?
g->m.graffiti_at( pos ) : g->m.name( pos );
desc[0] = here.has_graffiti_at( pos ) ?
here.graffiti_at( pos ) : here.name( pos );
}
default:
break;
Expand All @@ -156,7 +157,7 @@ void advanced_inv_area::init()
/* assemble a list of interesting traits of the target square */
// fields? with a special case for fire
bool danger_field = false;
const field &tmpfld = g->m.field_at( pos );
const field &tmpfld = here.field_at( pos );
for( auto &fld : tmpfld ) {
const field_entry &cur = fld.second;
if( fld.first.obj().has_fire ) {
Expand All @@ -172,7 +173,7 @@ void advanced_inv_area::init()
}

// trap?
const trap &tr = g->m.tr_at( pos );
const trap &tr = here.tr_at( pos );
if( tr.can_see( pos, g->u ) && !tr.is_benign() ) {
flags.append( _( " TRAP" ) );
}
Expand All @@ -183,7 +184,7 @@ void advanced_inv_area::init()
};
auto ter_check = [this]
( const ter_id & id ) {
return g->m.ter( this->pos ) == id;
return get_map().ter( this->pos ) == id;
};
if( std::any_of( ter_water.begin(), ter_water.end(), ter_check ) ) {
flags.append( _( " WATER" ) );
Expand All @@ -202,7 +203,7 @@ units::volume advanced_inv_area::free_volume( bool in_vehicle ) const
if( id == AIM_INVENTORY || id == AIM_WORN ) {
return g->u.volume_capacity() - g->u.volume_carried();
}
return in_vehicle ? veh->free_volume( vstor ) : g->m.free_volume( pos );
return in_vehicle ? veh->free_volume( vstor ) : get_map().free_volume( pos );
}

bool advanced_inv_area::is_same( const advanced_inv_area &other ) const
Expand Down Expand Up @@ -295,13 +296,13 @@ item *advanced_inv_area::get_container( bool in_vehicle )
}
}
} else {
map &m = g->m;
map &here = get_map();
bool is_in_vehicle = veh &&
( uistate.adv_inv_container_in_vehicle || ( can_store_in_vehicle() && in_vehicle ) );

const itemstack &stacks = is_in_vehicle ?
i_stacked( veh->get_items( vstor ) ) :
i_stacked( m.i_at( pos ) );
i_stacked( here.i_at( pos ) );

// check index first
if( stacks.size() > static_cast<size_t>( uistate.adv_inv_container_index ) ) {
Expand Down Expand Up @@ -411,7 +412,7 @@ void advanced_inv_area::set_container_position()
// update the absolute position
pos = g->u.pos() + off;
// update vehicle information
if( const cata::optional<vpart_reference> vp = g->m.veh_at( pos ).part_with_feature( "CARGO",
if( const cata::optional<vpart_reference> vp = get_map().veh_at( pos ).part_with_feature( "CARGO",
false ) ) {
veh = &vp->vehicle();
vstor = vp->part_index();
Expand Down
2 changes: 1 addition & 1 deletion src/avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ const player *avatar::get_book_reader( const item &book, std::vector<std::string
}

// Check for conditions that immediately disqualify the player from reading:
const optional_vpart_position vp = g->m.veh_at( pos() );
const optional_vpart_position vp = get_map().veh_at( pos() );
if( vp && vp->vehicle().player_in_control( *this ) ) {
reasons.emplace_back( _( "It's a bad idea to read while driving!" ) );
return nullptr;
Expand Down
34 changes: 18 additions & 16 deletions src/avatar_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,23 +761,24 @@ void avatar_action::mend( avatar &you, item_location loc )

bool avatar_action::eat_here( avatar &you )
{
map &here = get_map();
if( ( you.has_active_mutation( trait_RUMINANT ) || you.has_active_mutation( trait_GRAZER ) ) &&
( g->m.ter( you.pos() ) == t_underbrush || g->m.ter( you.pos() ) == t_shrub ) ) {
( here.ter( you.pos() ) == t_underbrush || here.ter( you.pos() ) == t_shrub ) ) {
item food( itype_underbrush, calendar::turn, 1 );
if( you.get_stored_kcal() > you.max_stored_kcal() -
food.get_comestible()->default_nutrition.kcal ) {
add_msg( _( "You're too full to eat the leaves from the %s." ), g->m.ter( you.pos() )->name() );
add_msg( _( "You're too full to eat the leaves from the %s." ), here.ter( you.pos() )->name() );
return true;
} else {
you.moves -= 400;
g->m.ter_set( you.pos(), t_grass );
here.ter_set( you.pos(), t_grass );
add_msg( _( "You eat the underbrush." ) );
you.eat( food );
return true;
}
}
if( you.has_active_mutation( trait_GRAZER ) && ( g->m.ter( you.pos() ) == t_grass ||
g->m.ter( you.pos() ) == t_grass_long || g->m.ter( you.pos() ) == t_grass_tall ) ) {
if( you.has_active_mutation( trait_GRAZER ) && ( here.ter( you.pos() ) == t_grass ||
here.ter( you.pos() ) == t_grass_long || here.ter( you.pos() ) == t_grass_tall ) ) {
item food( item( itype_grass, calendar::turn, 1 ) );
if( you.get_stored_kcal() > you.max_stored_kcal() -
food.get_comestible()->default_nutrition.kcal ) {
Expand All @@ -787,24 +788,24 @@ bool avatar_action::eat_here( avatar &you )
you.moves -= 400;
add_msg( _( "You eat the grass." ) );
you.eat( food );
if( g->m.ter( you.pos() ) == t_grass_tall ) {
g->m.ter_set( you.pos(), t_grass_long );
} else if( g->m.ter( you.pos() ) == t_grass_long ) {
g->m.ter_set( you.pos(), t_grass );
if( here.ter( you.pos() ) == t_grass_tall ) {
here.ter_set( you.pos(), t_grass_long );
} else if( here.ter( you.pos() ) == t_grass_long ) {
here.ter_set( you.pos(), t_grass );
} else {
g->m.ter_set( you.pos(), t_dirt );
here.ter_set( you.pos(), t_dirt );
}
return true;
}
}
if( you.has_active_mutation( trait_GRAZER ) ) {
if( g->m.ter( you.pos() ) == t_grass_golf ) {
if( here.ter( you.pos() ) == t_grass_golf ) {
add_msg( _( "This grass is too short to graze." ) );
return true;
} else if( g->m.ter( you.pos() ) == t_grass_dead ) {
} else if( here.ter( you.pos() ) == t_grass_dead ) {
add_msg( _( "This grass is dead and too mangled for you to graze." ) );
return true;
} else if( g->m.ter( you.pos() ) == t_grass_white ) {
} else if( here.ter( you.pos() ) == t_grass_white ) {
add_msg( _( "This grass is tainted with paint and thus inedible." ) );
return true;
}
Expand Down Expand Up @@ -959,12 +960,13 @@ void avatar_action::plthrow( avatar &you, item_location loc,

static void make_active( item_location loc )
{
map &here = get_map();
switch( loc.where() ) {
case item_location::type::map:
g->m.make_active( loc );
here.make_active( loc );
break;
case item_location::type::vehicle:
g->m.veh_at( loc.position() )->vehicle().make_active( loc );
here.veh_at( loc.position() )->vehicle().make_active( loc );
break;
default:
break;
Expand All @@ -975,7 +977,7 @@ static void update_lum( item_location loc, bool add )
{
switch( loc.where() ) {
case item_location::type::map:
g->m.update_lum( loc, add );
get_map().update_lum( loc, add );
break;
default:
break;
Expand Down
33 changes: 17 additions & 16 deletions src/ballistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,18 @@ static void drop_or_embed_projectile( const dealt_projectile_attack &attack )
do_drop = !dropped_item.process( nullptr, attack.end_point, true );
}

map &here = get_map();
if( do_drop ) {
g->m.add_item_or_charges( attack.end_point, dropped_item );
here.add_item_or_charges( attack.end_point, dropped_item );
}

if( proj.has_effect( ammo_effect_HEAVY_HIT ) ) {
if( g->m.has_flag( flag_LIQUID, pt ) ) {
if( here.has_flag( flag_LIQUID, pt ) ) {
sounds::sound( pt, 10, sounds::sound_t::combat, _( "splash!" ), false, "bullet_hit", "hit_water" );
} else {
sounds::sound( pt, 8, sounds::sound_t::combat, _( "thud." ), false, "bullet_hit", "hit_wall" );
}
const trap &tr = g->m.tr_at( pt );
const trap &tr = here.tr_at( pt );
if( tr.triggered_by_item( dropped_item ) ) {
tr.trigger( pt, nullptr, &dropped_item );
}
Expand Down Expand Up @@ -207,9 +208,10 @@ dealt_projectile_attack projectile_attack( const projectile &proj_arg, const tri
double range = rl_dist( source, target_arg );

Creature *target_critter = g->critter_at( target_arg );
map &here = get_map();
double target_size = target_critter != nullptr ?
target_critter->ranged_target_size() :
g->m.ranged_target_size( target_arg );
here.ranged_target_size( target_arg );
projectile_attack_aim aim = projectile_attack_roll( dispersion, range, target_size );

// TODO: move to-hit roll back in here
Expand Down Expand Up @@ -240,7 +242,7 @@ dealt_projectile_attack projectile_attack( const projectile &proj_arg, const tri
// If we were targetting a tile rather than a monster, don't overshoot
// Unless the target was a wall, then we are aiming high enough to overshoot
const bool no_overshoot = proj.has_effect( ammo_effect_NO_OVERSHOOT ) ||
( g->critter_at( target_arg ) == nullptr && g->m.passable( target_arg ) );
( g->critter_at( target_arg ) == nullptr && here.passable( target_arg ) );

double extend_to_range = no_overshoot ? range : proj_arg.range;

Expand Down Expand Up @@ -283,7 +285,7 @@ dealt_projectile_attack projectile_attack( const projectile &proj_arg, const tri
trajectory = line_to( source, target );
} else {
// Go around obstacles a little if we're on target.
trajectory = g->m.find_clear_path( source, target );
trajectory = here.find_clear_path( source, target );
}

add_msg( m_debug, "missed_by_tiles: %.2f; missed_by: %.2f; target (orig/hit): %d,%d,%d/%d,%d,%d",
Expand All @@ -300,7 +302,7 @@ dealt_projectile_attack projectile_attack( const projectile &proj_arg, const tri

static emit_id muzzle_smoke( "emit_smaller_smoke_plume" );
if( proj.has_effect( ammo_effect_MUZZLE_SMOKE ) ) {
g->m.emit_field( trajectory.front(), muzzle_smoke );
here.emit_field( trajectory.front(), muzzle_smoke );
}

if( !no_overshoot && range < extend_to_range ) {
Expand All @@ -322,7 +324,6 @@ dealt_projectile_attack projectile_attack( const projectile &proj_arg, const tri
int projectile_skip_calculation = range * projectile_skip_multiplier;
int projectile_skip_current_frame = rng( 0, projectile_skip_calculation );
bool has_momentum = true;
map &here = get_map();
for( size_t i = 1; i < traj_len && ( has_momentum || stream ); ++i ) {
prev_point = tp;
tp = trajectory[i];
Expand Down Expand Up @@ -363,7 +364,7 @@ dealt_projectile_attack projectile_attack( const projectile &proj_arg, const tri
}

if( in_veh != nullptr ) {
const optional_vpart_position other = g->m.veh_at( tp );
const optional_vpart_position other = here.veh_at( tp );
if( in_veh == veh_pointer_or_null( other ) && other->is_inside() ) {
// Turret is on the roof and can't hit anything inside
continue;
Expand Down Expand Up @@ -403,7 +404,7 @@ dealt_projectile_attack projectile_attack( const projectile &proj_arg, const tri
}

if( critter != nullptr && cur_missed_by < 1.0 ) {
if( in_veh != nullptr && veh_pointer_or_null( g->m.veh_at( tp ) ) == in_veh &&
if( in_veh != nullptr && veh_pointer_or_null( here.veh_at( tp ) ) == in_veh &&
critter->is_player() ) {
// Turret either was aimed by the player (who is now ducking) and shoots from above
// Or was just IFFing, giving lots of warnings and time to get out of the line of fire
Expand All @@ -417,21 +418,21 @@ dealt_projectile_attack projectile_attack( const projectile &proj_arg, const tri
const size_t bt_len = blood_trail_len( attack.dealt_dam.total_damage() );
if( bt_len > 0 ) {
const tripoint &dest = move_along_line( tp, trajectory, bt_len );
g->m.add_splatter_trail( critter->bloodType(), tp, dest );
here.add_splatter_trail( critter->bloodType(), tp, dest );
}
sfx::do_projectile_hit( *attack.hit_critter );
has_momentum = false;
} else {
attack.missed_by = aim.missed_by;
}
} else if( in_veh != nullptr && veh_pointer_or_null( g->m.veh_at( tp ) ) == in_veh ) {
} else if( in_veh != nullptr && veh_pointer_or_null( here.veh_at( tp ) ) == in_veh ) {
// Don't do anything, especially don't call map::shoot as this would damage the vehicle
} else {
g->m.shoot( tp, proj, !no_item_damage && tp == target );
here.shoot( tp, proj, !no_item_damage && tp == target );
has_momentum = proj.impact.total_damage() > 0;
}

if( ( !has_momentum || !is_bullet ) && g->m.impassable( tp ) ) {
if( ( !has_momentum || !is_bullet ) && here.impassable( tp ) ) {
// Don't let flamethrowers go through walls
// TODO: Let them go through bars
traj_len = i;
Expand All @@ -446,7 +447,7 @@ dealt_projectile_attack projectile_attack( const projectile &proj_arg, const tri
g->draw_bullet( tp, static_cast<int>( traj_len-- ), trajectory, bullet );
}

if( g->m.impassable( tp ) ) {
if( here.impassable( tp ) ) {
tp = prev_point;
}

Expand All @@ -467,7 +468,7 @@ dealt_projectile_attack projectile_attack( const projectile &proj_arg, const tri
Creature *mon_ptr = g->get_creature_if( [&]( const Creature & z ) {
// search for creatures in radius 4 around impact site
if( rl_dist( z.pos(), tp ) <= 4 &&
g->m.sees( z.pos(), tp, -1 ) ) {
here.sees( z.pos(), tp, -1 ) ) {
// don't hit targets that have already been hit
if( !z.has_effect( effect_bounced ) ) {
return true;
Expand Down
Loading

0 comments on commit 044554a

Please sign in to comment.