Skip to content

Commit

Permalink
Remove reference to game::u from code in Character/player/avatar memb…
Browse files Browse the repository at this point in the history
…er functions

It will use the implicit this object instead.

This ensures the state and behavior of NPCs does not depend on the state of the player character.
  • Loading branch information
BevapDin committed Feb 8, 2020
1 parent 4a7753d commit e694f9e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
10 changes: 5 additions & 5 deletions src/avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,8 @@ bool avatar::read( item &it, const bool continuous )
}
if( it.type->use_methods.count( "MA_MANUAL" ) ) {

if( g->u.martial_arts_data.has_martialart( martial_art_learned_from( *it.type ) ) ) {
g->u.add_msg_if_player( m_info, _( "You already know all this book has to teach." ) );
if( martial_arts_data.has_martialart( martial_art_learned_from( *it.type ) ) ) {
add_msg_if_player( m_info, _( "You already know all this book has to teach." ) );
activity.set_to_null();
return false;
}
Expand Down Expand Up @@ -588,7 +588,7 @@ bool avatar::read( item &it, const bool continuous )
// push an indentifier of martial art book to the action handling
if( it.type->use_methods.count( "MA_MANUAL" ) ) {

if( g->u.get_stamina() < g->u.get_stamina_max() / 10 ) {
if( get_stamina() < get_stamina_max() / 10 ) {
add_msg( m_info, _( "You are too exhausted to train martial arts." ) );
return false;
}
Expand Down Expand Up @@ -833,14 +833,14 @@ void avatar::do_read( item &book )
const matype_id style_to_learn = martial_art_learned_from( *book.type );
skill_id skill_used = style_to_learn->primary_skill;
int difficulty = std::max( 1, style_to_learn->learn_difficulty );
difficulty = std::max( 1, 20 + difficulty * 2 - g->u.get_skill_level( skill_used ) * 2 );
difficulty = std::max( 1, 20 + difficulty * 2 - get_skill_level( skill_used ) * 2 );
add_msg( m_debug, _( "Chance to learn one in: %d" ), difficulty );

if( one_in( difficulty ) ) {
m->second.call( *this, book, false, pos() );
continuous = false;
} else {
if( activity.index == g->u.getID().get_value() ) {
if( activity.index == getID().get_value() ) {
continuous = true;
switch( rng( 1, 5 ) ) {
case 1:
Expand Down
8 changes: 4 additions & 4 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ void Character::dismount()
monster *critter = mounted_creature.get();
critter->mounted_player_id = character_id();
if( critter->has_flag( MF_RIDEABLE_MECH ) && !critter->type->mech_weapon.empty() ) {
remove_item( g->u.weapon );
remove_item( weapon );
}
if( is_avatar() && g->u.get_grab_type() != OBJECT_NONE ) {
add_msg( m_warning, _( "You let go of the grabbed object." ) );
Expand Down Expand Up @@ -1421,8 +1421,8 @@ cata::optional<std::list<item>::iterator> Character::wear_item( const item &to_w
}

const bool was_deaf = is_deaf();
const bool supertinymouse = g->u.has_trait( trait_id( "SMALL2" ) ) ||
g->u.has_trait( trait_id( "SMALL_OK" ) );
const bool supertinymouse = has_trait( trait_id( "SMALL2" ) ) ||
has_trait( trait_id( "SMALL_OK" ) );
last_item = to_wear.typeId();

std::list<item>::iterator position = position_to_wear_new_item( to_wear );
Expand Down Expand Up @@ -3454,7 +3454,7 @@ int Character::get_thirst() const
std::pair<std::string, nc_color> Character::get_thirst_description() const
{
// some delay from water in stomach is desired, but there needs to be some visceral response
int thirst = get_thirst() - ( std::max( units::to_milliliter<int>( g->u.stomach.get_water() ) / 10,
int thirst = get_thirst() - ( std::max( units::to_milliliter<int>( stomach.get_water() ) / 10,
0 ) );
std::string hydration_string;
nc_color hydration_color = c_white;
Expand Down
41 changes: 19 additions & 22 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ void player::on_hit( Creature *source, body_part bp_hit,
int player::get_lift_assist() const
{
int result = 0;
const std::vector<npc *> helpers = g->u.get_crafting_helpers();
const std::vector<npc *> helpers = get_crafting_helpers();
for( const npc *np : helpers ) {
result += np->get_str();
}
Expand All @@ -1366,7 +1366,7 @@ int player::get_lift_assist() const

int player::get_num_crafting_helpers( int max ) const
{
std::vector<npc *> helpers = g->u.get_crafting_helpers();
std::vector<npc *> helpers = get_crafting_helpers();
return std::min( max, static_cast<int>( helpers.size() ) );
}

Expand Down Expand Up @@ -2650,7 +2650,7 @@ static bool query_consume_ownership( item &target, player &p )
elem->say( "<witnessed_thievery>", 7 );
}
if( !witnesses.empty() && target.is_owned_by( p, true ) ) {
if( g->u.add_faction_warning( target.get_owner() ) ) {
if( p.add_faction_warning( target.get_owner() ) ) {
for( npc *elem : witnesses ) {
elem->make_angry();
}
Expand Down Expand Up @@ -2832,7 +2832,7 @@ item::reload_option player::select_ammo( const item &base,
e.ammo->ammo_data()->nname( e.ammo->ammo_remaining() ), e.ammo->ammo_remaining() );
}
} else if( e.ammo->is_watertight_container() ||
( e.ammo->is_ammo_container() && g->u.is_worn( *e.ammo ) ) ) {
( e.ammo->is_ammo_container() && is_worn( *e.ammo ) ) ) {
// worn ammo containers should be named by their contents with their location also updated below
return e.ammo->contents.front().display_name();

Expand All @@ -2844,10 +2844,10 @@ item::reload_option player::select_ammo( const item &base,
// Get location descriptions
std::vector<std::string> where;
std::transform( opts.begin(), opts.end(),
std::back_inserter( where ), []( const item::reload_option & e ) {
std::back_inserter( where ), [this]( const item::reload_option & e ) {
bool is_ammo_container = e.ammo->is_ammo_container();
if( is_ammo_container || e.ammo->is_container() ) {
if( is_ammo_container && g->u.is_worn( *e.ammo ) ) {
if( is_ammo_container && is_worn( *e.ammo ) ) {
return e.ammo->type_name();
}
return string_format( _( "%s, %s" ), e.ammo->type_name(), e.ammo.describe( &g->u ) );
Expand Down Expand Up @@ -2931,12 +2931,12 @@ item::reload_option player::select_ammo( const item &base,
*opts[ i ].ammo;

char hotkey = -1;
if( g->u.has_item( ammo ) ) {
if( has_item( ammo ) ) {
// if ammo in player possession and either it or any container has a valid invlet use this
if( ammo.invlet ) {
hotkey = ammo.invlet;
} else {
for( const auto obj : g->u.parents( ammo ) ) {
for( const auto obj : parents( ammo ) ) {
if( obj->invlet ) {
hotkey = obj->invlet;
break;
Expand Down Expand Up @@ -3283,7 +3283,7 @@ bool player::can_reload( const item &it, const itype_id &ammo ) const

void player::mend_item( item_location &&obj, bool interactive )
{
if( g->u.has_trait( trait_DEBUG_HS ) ) {
if( has_trait( trait_DEBUG_HS ) ) {
uilist menu;
menu.text = _( "Toggle which fault?" );
std::vector<std::pair<fault_id, bool>> opts;
Expand Down Expand Up @@ -3730,21 +3730,21 @@ bool player::unload( item &it )

if( target->is_magazine() ) {
player_activity unload_mag_act( activity_id( "ACT_UNLOAD_MAG" ) );
g->u.assign_activity( unload_mag_act );
g->u.activity.targets.emplace_back( item_location( *this, target ) );
assign_activity( unload_mag_act );
activity.targets.emplace_back( item_location( *this, target ) );

// Calculate the time to remove the contained ammo (consuming half as much time as required to load the magazine)
int mv = 0;
for( auto &content : target->contents ) {
mv += this->item_reload_cost( it, content, content.charges ) / 2;
}
g->u.activity.moves_left += mv;
activity.moves_left += mv;

// I think this means if unload is not done on ammo-belt, it takes as long as it takes to reload a mag.
if( !it.is_ammo_belt() ) {
g->u.activity.moves_left += mv;
activity.moves_left += mv;
}
g->u.activity.auto_resume = true;
activity.auto_resume = true;

return true;

Expand Down Expand Up @@ -5553,21 +5553,18 @@ item &player::item_with_best_of_quality( const quality_id &qid )

bool player::crush_frozen_liquid( item_location loc )
{

player &u = g->u;

if( u.has_quality( quality_id( "HAMMER" ) ) ) {
item hammering_item = u.item_with_best_of_quality( quality_id( "HAMMER" ) );
if( has_quality( quality_id( "HAMMER" ) ) ) {
item hammering_item = item_with_best_of_quality( quality_id( "HAMMER" ) );
//~ %1$s: item to be crushed, %2$s: hammer name
if( query_yn( _( "Do you want to crush up %1$s with your %2$s?\n"
"<color_red>Be wary of fragile items nearby!</color>" ),
loc.get_item()->display_name(), hammering_item.tname() ) ) {

//Risk smashing tile with hammering tool, risk is lower with higher dex, damage lower with lower strength
if( one_in( 1 + u.dex_cur / 4 ) ) {
if( one_in( 1 + dex_cur / 4 ) ) {
add_msg_if_player( colorize( _( "You swing your %s wildly!" ), c_red ),
hammering_item.tname() );
int smashskill = u.str_cur + hammering_item.damage_melee( DT_BASH );
int smashskill = str_cur + hammering_item.damage_melee( DT_BASH );
g->m.bash( loc.position(), smashskill );
}
add_msg_if_player( _( "You crush up and gather %s" ), loc.get_item()->display_name() );
Expand Down Expand Up @@ -5633,7 +5630,7 @@ const targeting_data &player::get_targeting_data()
debugmsg( "Tried to get targeting data before setting it" );
tdata.reset( new targeting_data() );
tdata->relevant = nullptr;
g->u.cancel_activity();
cancel_activity();
}

return *tdata;
Expand Down

0 comments on commit e694f9e

Please sign in to comment.