Skip to content

Commit

Permalink
Migrate map references part ten
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingranade committed Jul 1, 2020
1 parent 43fc2d0 commit 04971e0
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 168 deletions.
20 changes: 12 additions & 8 deletions src/inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,15 @@ void inventory::update_cache_with_item( item &newit )

char inventory::find_usable_cached_invlet( const itype_id &item_type )
{
Character &player_character = get_player_character();
// Some of our preferred letters might already be used.
for( auto invlet : invlet_cache.invlets_for( item_type ) ) {
// Don't overwrite user assignments.
if( assigned_invlet.count( invlet ) ) {
continue;
}
// Check if anything is using this invlet.
if( g->u.invlet_to_item( invlet ) != nullptr ) {
if( player_character.invlet_to_item( invlet ) != nullptr ) {
continue;
}
return invlet;
Expand All @@ -281,6 +282,7 @@ item &inventory::add_item( item newit, bool keep_invlet, bool assign_invlet, boo
{
binned = false;

Character &player_character = get_player_character();
if( should_stack ) {
// See if we can't stack this item.
for( auto &elem : items ) {
Expand All @@ -302,7 +304,7 @@ item &inventory::add_item( item newit, bool keep_invlet, bool assign_invlet, boo
return elem.back();
} else if( keep_invlet && assign_invlet && it_ref->invlet == newit.invlet ) {
// If keep_invlet is true, we'll be forcing other items out of their current invlet.
assign_empty_invlet( *it_ref, g->u );
assign_empty_invlet( *it_ref, player_character );
}
}
}
Expand Down Expand Up @@ -410,7 +412,7 @@ void inventory::form_from_map( const tripoint &origin, int range, const Characte
bool assign_invlet,
bool clear_path )
{
form_from_map( g->m, origin, range, pl, assign_invlet, clear_path );
form_from_map( get_map(), origin, range, pl, assign_invlet, clear_path );
}

void inventory::form_from_zone( map &m, std::unordered_set<tripoint> &zone_pts, const Character *pl,
Expand Down Expand Up @@ -877,6 +879,8 @@ item *inventory::most_appropriate_painkiller( int pain )

void inventory::rust_iron_items()
{
Character &player_character = get_player_character();
map &here = get_map();
for( auto &elem : items ) {
for( auto &elem_stack_iter : elem ) {
if( elem_stack_iter.made_of( material_id( "iron" ) ) &&
Expand All @@ -894,7 +898,7 @@ void inventory::rust_iron_items()
elem_stack_iter.base_volume().value() ) / 250 ) ) ) ) &&
// ^season length ^14/5*0.75/pi (from volume of sphere)
//Freshwater without oxygen rusts slower than air
g->m.water_from( g->u.pos() ).typeId() == itype_salt_water ) {
here.water_from( player_character.pos() ).typeId() == itype_salt_water ) {
elem_stack_iter.inc_damage( DT_ACID ); // rusting never completely destroys an item
add_msg( m_bad, _( "Your %s is damaged by rust." ), elem_stack_iter.tname() );
}
Expand Down Expand Up @@ -1040,8 +1044,7 @@ void inventory::assign_empty_invlet( item &it, const Character &p, const bool fo
if( cur_inv.count() < inv_chars.size() ) {
// XXX YUCK I don't know how else to get the keybindings
// FIXME: Find a better way to get bound keys
avatar &u = g->u;
inventory_selector selector( u );
inventory_selector selector( get_avatar() );

for( const auto &inv_char : inv_chars ) {
if( assigned_invlet.count( inv_char ) ) {
Expand Down Expand Up @@ -1101,11 +1104,12 @@ void inventory::update_invlet( item &newit, bool assign_invlet )
}
}

Character &player_character = get_player_character();
// Remove letters that have been assigned to other items in the inventory
if( newit.invlet ) {
char tmp_invlet = newit.invlet;
newit.invlet = '\0';
if( g->u.invlet_to_item( tmp_invlet ) == nullptr ) {
if( player_character.invlet_to_item( tmp_invlet ) == nullptr ) {
newit.invlet = tmp_invlet;
}
}
Expand All @@ -1118,7 +1122,7 @@ void inventory::update_invlet( item &newit, bool assign_invlet )

// Give the item an invlet if it has none
if( !newit.invlet ) {
assign_empty_invlet( newit, g->u );
assign_empty_invlet( newit, player_character );
}
}
}
Expand Down
47 changes: 26 additions & 21 deletions src/melee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ bool Character::handle_melee_wear( item &shield, float wear_multiplier )
if( comp.typeId() == big_comp && !is_armed() ) {
wield( comp );
} else {
g->m.add_item_or_charges( pos(), comp );
get_map().add_item_or_charges( pos(), comp );
}
}
} else {
Expand Down Expand Up @@ -454,6 +454,7 @@ void Character::melee_attack( Creature &t, bool allow_special, const matec_id &f

int move_cost = attack_speed( *cur_weapon );

Character &player_character = get_player_character();
if( hit_spread < 0 ) {
int stumble_pen = stumble( *this, *cur_weapon );
sfx::generate_melee_sound( pos(), t.pos(), false, false );
Expand All @@ -476,7 +477,7 @@ void Character::melee_attack( Creature &t, bool allow_special, const matec_id &f
} else {
add_msg( _( "You miss." ) );
}
} else if( g->u.sees( *this ) ) {
} else if( player_character.sees( *this ) ) {
if( stumble_pen >= 60 ) {
add_msg( _( "%s misses and stumbles with the momentum." ), name );
} else if( stumble_pen >= 10 ) {
Expand All @@ -502,7 +503,7 @@ void Character::melee_attack( Creature &t, bool allow_special, const matec_id &f
} else {
melee::melee_stats.hit_count += 1;
// Remember if we see the monster at start - it may change
const bool seen = g->u.sees( t );
const bool seen = player_character.sees( t );
// Start of attacks.
const bool critical_hit = scored_crit( t.dodge_roll(), *cur_weapon );
if( critical_hit ) {
Expand Down Expand Up @@ -594,7 +595,7 @@ void Character::melee_attack( Creature &t, bool allow_special, const matec_id &f
}

// Treat monster as seen if we see it before or after the attack
if( seen || g->u.sees( t ) ) {
if( seen || player_character.sees( t ) ) {
std::string message = melee_message( technique, *this, dealt_dam );
player_hit_message( this, message, t, dam, critical_hit );
} else {
Expand Down Expand Up @@ -664,6 +665,7 @@ void player::reach_attack( const tripoint &p )
int move_cost = attack_speed( weapon );
int skill = std::min( 10, get_skill_level( skill_stabbing ) );
int t = 0;
map &here = get_map();
std::vector<tripoint> path = line_to( pos(), p, t, 0 );
path.pop_back(); // Last point is our critter
for( const tripoint &path_point : path ) {
Expand All @@ -677,13 +679,13 @@ void player::reach_attack( const tripoint &p )
critter = inter;
break;
/** @EFFECT_STABBING increases ability to reach attack through fences */
} else if( g->m.impassable( path_point ) &&
} else if( here.impassable( path_point ) &&
// Fences etc. Spears can stab through those
!( weapon.has_flag( "SPEAR" ) &&
g->m.has_flag( "THIN_OBSTACLE", path_point ) &&
here.has_flag( "THIN_OBSTACLE", path_point ) &&
x_in_y( skill, 10 ) ) ) {
/** @EFFECT_STR increases bash effects when reach attacking past something */
g->m.bash( path_point, str_cur + weapon.damage_melee( DT_BASH ) );
here.bash( path_point, str_cur + weapon.damage_melee( DT_BASH ) );
handle_melee_wear( weapon );
mod_moves( -move_cost );
return;
Expand Down Expand Up @@ -830,7 +832,7 @@ float player::get_dodge() const

if( has_effect( effect_grabbed ) ) {
int zed_number = 0;
for( auto &dest : g->m.points_in_radius( pos(), 1, 0 ) ) {
for( auto &dest : get_map().points_in_radius( pos(), 1, 0 ) ) {
const monster *const mon = g->critter_at<monster>( dest );
if( mon && mon->has_effect( effect_grabbing ) ) {
zed_number++;
Expand Down Expand Up @@ -1170,7 +1172,7 @@ matec_id Character::pick_technique( Creature &t, const item &weap,

bool downed = t.has_effect( effect_downed );
bool stunned = t.has_effect( effect_stunned );
bool wall_adjacent = g->m.is_wall_adjacent( pos() );
bool wall_adjacent = get_map().is_wall_adjacent( pos() );

// first add non-aoe tecs
for( const matec_id &tec_id : all ) {
Expand Down Expand Up @@ -1353,7 +1355,7 @@ bool Character::valid_aoe_technique( Creature &t, const ma_technique &technique,
}

if( targets.empty() && technique.aoe == "spin" ) {
for( const tripoint &tmp : g->m.points_in_radius( pos(), 1 ) ) {
for( const tripoint &tmp : get_map().points_in_radius( pos(), 1 ) ) {
if( tmp == t.pos() ) {
continue;
}
Expand Down Expand Up @@ -1476,6 +1478,7 @@ void Character::perform_technique( const ma_technique &technique, Creature &t, d
t.add_effect( effect_stunned, rng( 1_turns, time_duration::from_turns( technique.stun_dur ) ) );
}

map &here = get_map();
if( technique.knockback_dist ) {
const tripoint prev_pos = t.pos(); // track target startpoint for knockback_follow
const int kb_offset_x = rng( -technique.knockback_spread, technique.knockback_spread );
Expand All @@ -1486,18 +1489,18 @@ void Character::perform_technique( const ma_technique &technique, Creature &t, d
}
// This technique makes the player follow into the tile the target was knocked from
if( technique.knockback_follow ) {
const optional_vpart_position vp0 = g->m.veh_at( pos() );
const optional_vpart_position vp0 = here.veh_at( pos() );
vehicle *const veh0 = veh_pointer_or_null( vp0 );
bool to_swimmable = g->m.has_flag( "SWIMMABLE", prev_pos );
bool to_deepwater = g->m.has_flag( TFLAG_DEEP_WATER, prev_pos );
bool to_swimmable = here.has_flag( "SWIMMABLE", prev_pos );
bool to_deepwater = here.has_flag( TFLAG_DEEP_WATER, prev_pos );

// Check if it's possible to move to the new tile
bool move_issue =
g->is_dangerous_tile( prev_pos ) || // Tile contains fire, etc
( to_swimmable && to_deepwater ) || // Dive into deep water
is_mounted() ||
( veh0 != nullptr && std::abs( veh0->velocity ) > 100 ) || // Diving from moving vehicle
( veh0 != nullptr && veh0->player_in_control( g->u ) ) || // Player is driving
( veh0 != nullptr && veh0->player_in_control( get_avatar() ) ) || // Player is driving
has_effect( effect_amigara );

if( !move_issue ) {
Expand All @@ -1524,7 +1527,7 @@ void Character::perform_technique( const ma_technique &technique, Creature &t, d
}

if( technique.disarms && p != nullptr && p->is_armed() ) {
g->m.add_item_or_charges( p->pos(), p->remove_weapon() );
here.add_item_or_charges( p->pos(), p->remove_weapon() );
if( p->is_player() ) {
add_msg_if_npc( _( "<npcname> disarms you!" ) );
} else {
Expand Down Expand Up @@ -2193,6 +2196,7 @@ void player_hit_message( Character *attacker, const std::string &message,
std::string sSCTmod;
game_message_type gmtSCTcolor = m_good;

Character &player_character = get_player_character();
if( dam <= 0 ) {
if( attacker->is_npc() ) {
//~ NPC hits something but does no damage
Expand All @@ -2202,9 +2206,9 @@ void player_hit_message( Character *attacker, const std::string &message,
msg = string_format( _( "%s but do no damage." ), message );
}
msgtype = m_neutral;
} else if(
crit ) { //Player won't see exact numbers of damage dealt by NPC unless player has DEBUG_NIGHTVISION trait
if( attacker->is_npc() && !g->u.has_trait( trait_DEBUG_NIGHTVISION ) ) {
} else if( crit ) {
//Player won't see exact numbers of damage dealt by NPC unless player has DEBUG_NIGHTVISION trait
if( attacker->is_npc() && !player_character.has_trait( trait_DEBUG_NIGHTVISION ) ) {
//~ NPC hits something (critical)
msg = string_format( _( "%s. Critical!" ), message );
} else {
Expand All @@ -2214,7 +2218,7 @@ void player_hit_message( Character *attacker, const std::string &message,
sSCTmod = _( "Critical!" );
gmtSCTcolor = m_critical;
} else {
if( attacker->is_npc() && !g->u.has_trait( trait_DEBUG_NIGHTVISION ) ) {
if( attacker->is_npc() && !player_character.has_trait( trait_DEBUG_NIGHTVISION ) ) {
//~ NPC hits something
msg = string_format( _( "%s." ), message );
} else {
Expand Down Expand Up @@ -2372,6 +2376,7 @@ void player::disarm( npc &target )
return;
}

map &here = get_map();
// hitspread >= 0, which means we are going to disarm by grabbing target by their weapon
if( !is_armed() ) {
/** @EFFECT_UNARMED increases chance to disarm, bonus when nothing wielded */
Expand All @@ -2389,7 +2394,7 @@ void player::disarm( npc &target )
add_msg( _( "You grab at %s and pull with all your force, but it drops nearby!" ),
it.tname() );
const tripoint tp = target.pos() + tripoint( rng( -1, 1 ), rng( -1, 1 ), 0 );
g->m.add_item_or_charges( tp, target.i_rem( &it ) );
here.add_item_or_charges( tp, target.i_rem( &it ) );
mod_moves( -100 );
} else {
add_msg( _( "You grab at %s and pull with all your force, but in vain!" ), it.tname() );
Expand All @@ -2406,7 +2411,7 @@ void player::disarm( npc &target )
add_msg( _( "You smash %s with all your might forcing their %s to drop down nearby!" ),
target.name, it.tname() );
const tripoint tp = target.pos() + tripoint( rng( -1, 1 ), rng( -1, 1 ), 0 );
g->m.add_item_or_charges( tp, target.i_rem( &it ) );
here.add_item_or_charges( tp, target.i_rem( &it ) );
} else {
add_msg( _( "You smash %s with all your might but %s remains in their hands!" ),
target.name, it.tname() );
Expand Down
Loading

0 comments on commit 04971e0

Please sign in to comment.