Skip to content

Commit

Permalink
Global getters (#41347)
Browse files Browse the repository at this point in the history
* Make Character::stat into a standalone enum class

Enables forward declaration

* Remove player centric overload and unecessary include

* Remove player.h dependency from trap.cpp

* Add singleton-like accessors for avatar, map, weather and event_bus

* use accessors to remove includes of game.h
  • Loading branch information
kevingranade authored Jun 16, 2020
1 parent b10b72a commit 438774d
Show file tree
Hide file tree
Showing 42 changed files with 158 additions and 154 deletions.
7 changes: 3 additions & 4 deletions src/advanced_inv_pane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "advanced_inv_area.h"
#include "advanced_inv_pane.h"
#include "avatar.h"
#include "game.h"
#include "inventory.h"
#include "item.h"
#include "item_contents.h"
Expand Down Expand Up @@ -42,7 +41,7 @@ void advanced_inventory_pane::load_settings( int saved_area_idx,
// determine the square's vehicle/map item presence
bool has_veh_items = square.can_store_in_vehicle() ?
!square.veh->get_items( square.vstor ).empty() : false;
bool has_map_items = !g->m.i_at( square.pos ).empty();
bool has_map_items = !get_map().i_at( square.pos ).empty();
// determine based on map items and settings to show cargo
bool show_vehicle = is_re_enter ?
save_state->in_vehicle : has_veh_items ? true :
Expand Down Expand Up @@ -143,8 +142,8 @@ void advanced_inventory_pane::add_items_from_area( advanced_inv_area &square,
if( !square.canputitems() ) {
return;
}
map &m = g->m;
avatar &u = g->u;
map &m = get_map();
avatar &u = get_avatar();
// Existing items are *not* cleared on purpose, this might be called
// several times in case all surrounding squares are to be shown.
if( square.id == AIM_INVENTORY ) {
Expand Down
27 changes: 16 additions & 11 deletions src/avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ static const std::string flag_FIX_FARSIGHT( "FIX_FARSIGHT" );
class JsonIn;
class JsonOut;

avatar &get_avatar()
{
return g->u;
}

avatar::avatar()
{
show_map_memory = true;
Expand Down Expand Up @@ -1379,7 +1384,7 @@ static int xp_to_next( const avatar &you )
return xp_next;
}

void avatar::upgrade_stat_prompt( const Character::stat &stat )
void avatar::upgrade_stat_prompt( const character_stat &stat )
{
const int free_points = free_upgrade_points();
const int next_lvl_xp = xp_to_next( *this );
Expand All @@ -1391,19 +1396,19 @@ void avatar::upgrade_stat_prompt( const Character::stat &stat )

std::string stat_string;
switch( stat ) {
case STRENGTH:
case character_stat::STRENGTH:
stat_string = _( "strength" );
break;
case DEXTERITY:
case character_stat::DEXTERITY:
stat_string = _( "dexterity" );
break;
case INTELLIGENCE:
case character_stat::INTELLIGENCE:
stat_string = _( "intelligence" );
break;
case PERCEPTION:
case character_stat::PERCEPTION:
stat_string = _( "perception" );
break;
case DUMMY_STAT:
case character_stat::DUMMY_STAT:
stat_string = _( "invalid stat" );
debugmsg( "Tried to use invalid stat" );
break;
Expand All @@ -1414,19 +1419,19 @@ void avatar::upgrade_stat_prompt( const Character::stat &stat )
if( query_yn( _( "Are you sure you want to raise %s? %d points available." ), stat_string,
free_points ) ) {
switch( stat ) {
case STRENGTH:
case character_stat::STRENGTH:
str_upgrade++;
break;
case DEXTERITY:
case character_stat::DEXTERITY:
dex_upgrade++;
break;
case INTELLIGENCE:
case character_stat::INTELLIGENCE:
int_upgrade++;
break;
case PERCEPTION:
case character_stat::PERCEPTION:
per_upgrade++;
break;
case DUMMY_STAT:
case character_stat::DUMMY_STAT:
debugmsg( "Tried to use invalid stat" );
break;
}
Expand Down
4 changes: 3 additions & 1 deletion src/avatar.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class avatar : public player
int get_int_base() const override;
int get_per_base() const override;

void upgrade_stat_prompt( const Character::stat &stat_name );
void upgrade_stat_prompt( const character_stat &stat_name );
// how many points are available to upgrade via STK
int free_upgrade_points() const;
// how much "kill xp" you have
Expand Down Expand Up @@ -265,6 +265,8 @@ class avatar : public player
monster_visible_info mon_visible;
};

avatar &get_avatar();

struct points_left {
int stat_points;
int trait_points;
Expand Down
2 changes: 1 addition & 1 deletion src/bionics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ void bionic_data::load( const JsonObject &jsobj, const std::string & )
// clear data first so that copy-from can override it
stat_bonus.clear();
for( JsonArray ja : jsobj.get_array( "stat_bonus" ) ) {
stat_bonus.emplace( io::string_to_enum<Character::stat>( ja.get_string( 0 ) ),
stat_bonus.emplace( io::string_to_enum<character_stat>( ja.get_string( 0 ) ),
ja.get_int( 1 ) );
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/bionics.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include "bodypart.h"
#include "calendar.h"
#include "character.h"
#include "flat_set.h"
#include "optional.h"
#include "translations.h"
Expand All @@ -21,8 +20,11 @@
class JsonIn;
class JsonObject;
class JsonOut;
class Character;
class player;

enum class character_stat : char;

struct bionic_data {
bionic_data();

Expand Down Expand Up @@ -51,7 +53,7 @@ struct bionic_data {
/**Bonus to weight capacity*/
units::mass weight_capacity_bonus = 0_gram;
/**Map of stats and their corresponding bonuses passively granted by a bionic*/
std::map<Character::stat, int> stat_bonus;
std::map<character_stat, int> stat_bonus;
/**This bionic draws power through a cable*/
bool is_remote_fueled = false;
/**Fuel types that can be used by this bionic*/
Expand Down
38 changes: 22 additions & 16 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,12 @@ std::string enum_to_string<blood_type>( blood_type data )

} // namespace io

Character &get_player_character()
{
return g->u;
}


// *INDENT-OFF*
Character::Character() :

Expand Down Expand Up @@ -2160,7 +2166,7 @@ void Character::update_fuel_storage( const itype_id &fuel )

}

int Character::get_mod_stat_from_bionic( const Character::stat &Stat ) const
int Character::get_mod_stat_from_bionic( const character_stat &Stat ) const
{
int ret = 0;
for( const bionic_id &bid : get_bionics() ) {
Expand Down Expand Up @@ -3537,10 +3543,10 @@ void Character::reset_stats()
mod_str_bonus( 20 );
}

mod_str_bonus( get_mod_stat_from_bionic( STRENGTH ) );
mod_dex_bonus( get_mod_stat_from_bionic( DEXTERITY ) );
mod_per_bonus( get_mod_stat_from_bionic( PERCEPTION ) );
mod_int_bonus( get_mod_stat_from_bionic( INTELLIGENCE ) );
mod_str_bonus( get_mod_stat_from_bionic( character_stat::STRENGTH ) );
mod_dex_bonus( get_mod_stat_from_bionic( character_stat::DEXTERITY ) );
mod_per_bonus( get_mod_stat_from_bionic( character_stat::PERCEPTION ) );
mod_int_bonus( get_mod_stat_from_bionic( character_stat::INTELLIGENCE ) );

// Trait / mutation buffs
mod_str_bonus( std::floor( mutation_value( "str_modifier" ) ) );
Expand Down Expand Up @@ -4202,17 +4208,17 @@ void Character::print_health() const
namespace io
{
template<>
std::string enum_to_string<Character::stat>( Character::stat data )
std::string enum_to_string<character_stat>( character_stat data )
{
switch( data ) {
// *INDENT-OFF*
case Character::stat::STRENGTH: return "STR";
case Character::stat::DEXTERITY: return "DEX";
case Character::stat::INTELLIGENCE: return "INT";
case Character::stat::PERCEPTION: return "PER";
case character_stat::STRENGTH: return "STR";
case character_stat::DEXTERITY: return "DEX";
case character_stat::INTELLIGENCE: return "INT";
case character_stat::PERCEPTION: return "PER";

// *INDENT-ON*
case Character::stat::DUMMY_STAT:
case character_stat::DUMMY_STAT:
break;
}
abort();
Expand Down Expand Up @@ -8186,14 +8192,14 @@ void Character::set_fac_id( const std::string &my_fac_id )
fac_id = faction_id( my_fac_id );
}

std::string get_stat_name( Character::stat Stat )
std::string get_stat_name( character_stat Stat )
{
switch( Stat ) {
// *INDENT-OFF*
case Character::stat::STRENGTH: return pgettext( "strength stat", "STR" );
case Character::stat::DEXTERITY: return pgettext( "dexterity stat", "DEX" );
case Character::stat::INTELLIGENCE: return pgettext( "intelligence stat", "INT" );
case Character::stat::PERCEPTION: return pgettext( "perception stat", "PER" );
case character_stat::STRENGTH: return pgettext( "strength stat", "STR" );
case character_stat::DEXTERITY: return pgettext( "dexterity stat", "DEX" );
case character_stat::INTELLIGENCE: return pgettext( "intelligence stat", "INT" );
case character_stat::PERCEPTION: return pgettext( "perception stat", "PER" );
// *INDENT-ON*
default:
break;
Expand Down
26 changes: 14 additions & 12 deletions src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,14 @@ inline social_modifiers operator+( social_modifiers lhs, const social_modifiers
return lhs;
}

enum class character_stat : char {
STRENGTH,
DEXTERITY,
INTELLIGENCE,
PERCEPTION,
DUMMY_STAT
};

class Character : public Creature, public visitable<Character>
{
public:
Expand Down Expand Up @@ -347,14 +355,6 @@ class Character : public Creature, public visitable<Character>
very_comfortable = 10
};

enum stat {
STRENGTH,
DEXTERITY,
INTELLIGENCE,
PERCEPTION,
DUMMY_STAT
};

// Character stats
// TODO: Make those protected
int str_max;
Expand Down Expand Up @@ -1113,7 +1113,7 @@ class Character : public Creature, public visitable<Character>
/**Updates which bionic contain fuel and which is empty*/
void update_fuel_storage( const itype_id &fuel );
/**Get stat bonus from bionic*/
int get_mod_stat_from_bionic( const Character::stat &Stat ) const;
int get_mod_stat_from_bionic( const character_stat &Stat ) const;
// route for overmap-scale traveling
std::vector<tripoint> omt_path;

Expand Down Expand Up @@ -2432,13 +2432,15 @@ class Character : public Creature, public visitable<Character>
bool last_climate_control_ret;
};

Character &get_player_character();

// Little size helper, exposed for use in deserialization code.
creature_size calculate_size( const Character &c );

template<>
struct enum_traits<Character::stat> {
static constexpr Character::stat last = Character::stat::DUMMY_STAT;
struct enum_traits<character_stat> {
static constexpr character_stat last = character_stat::DUMMY_STAT;
};
/**Get translated name of a stat*/
std::string get_stat_name( Character::stat Stat );
std::string get_stat_name( character_stat Stat );
#endif // CATA_SRC_CHARACTER_H
4 changes: 2 additions & 2 deletions src/descriptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void game::extended_description( const tripoint &p )
description_target cur_target = description_target::terrain;
if( seen_critter( *this, p ) != nullptr ) {
cur_target = description_target::creature;
} else if( g->m.has_furn( p ) ) {
} else if( get_map().has_furn( p ) ) {
cur_target = description_target::furniture;
}

Expand Down Expand Up @@ -155,7 +155,7 @@ std::string map_data_common_t::extended_description() const

if( has_any_harvest ) {
ss << "--" << std::endl;
int player_skill = g->u.get_skill_level( skill_survival );
int player_skill = get_avatar().get_skill_level( skill_survival );
ss << _( "You could harvest the following things from it:" ) << std::endl;
// Group them by identical ids to avoid repeating same blocks of data
// First, invert the mapping: season->id to id->seasons
Expand Down
2 changes: 2 additions & 0 deletions src/event_bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ class event_bus
std::vector<event_subscriber *> subscribers;
};

event_bus &get_event_bus();

#endif // CATA_SRC_EVENT_BUS_H
10 changes: 10 additions & 0 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12490,3 +12490,13 @@ void avatar_moves( const tripoint &old_abs_pos, const avatar &u, const map &m )
}
}
} // namespace cata_event_dispatch

void game_ui::init_ui()
{
g->init_ui( true );
}

event_bus &get_event_bus()
{
return g->events();
}
9 changes: 0 additions & 9 deletions src/game_ui.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
#include "game_ui.h"

#include <memory>

#include "game.h"

void game_ui::init_ui()
{
g->init_ui( true );
}

#if !defined(TILES)

void reinitialize_framebuffer()
Expand Down
3 changes: 1 addition & 2 deletions src/item_contents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "character.h"
#include "enums.h"
#include "game.h"
#include "item.h"
#include "iteminfo_query.h"
#include "itype.h"
Expand Down Expand Up @@ -401,7 +400,7 @@ int item_contents::ammo_consume( int qty, const tripoint &pos )
if( mag.has_flag( "MAG_DESTROY" ) ) {
pocket.remove_item( mag );
} else if( mag.has_flag( "MAG_EJECT" ) ) {
g->m.add_item( pos, mag );
get_map().add_item( pos, mag );
pocket.remove_item( mag );
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/kill_tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "character_id.h"
#include "color.h"
#include "event.h"
#include "game.h"
#include "mtype.h"
#include "options.h"
#include "string_formatter.h"
Expand Down Expand Up @@ -101,7 +100,7 @@ std::string kill_tracker::get_kills_text() const
buffer = string_format( _( "KILL COUNT: %d" ), totalkills );
if( get_option<bool>( "STATS_THROUGH_KILLS" ) ) {
buffer += string_format( _( "\nExperience: %d (%d points available)" ), kill_xp(),
g->u.free_upgrade_points() );
get_avatar().free_upgrade_points() );
}
buffer += "\n";
}
Expand All @@ -122,7 +121,7 @@ void kill_tracker::notify( const cata::event &e )
switch( e.type() ) {
case event_type::character_kills_monster: {
character_id killer = e.get<character_id>( "killer" );
if( killer != g->u.getID() ) {
if( killer != get_avatar().getID() ) {
// TODO: add a kill counter for npcs?
break;
}
Expand All @@ -132,7 +131,7 @@ void kill_tracker::notify( const cata::event &e )
}
case event_type::character_kills_character: {
character_id killer = e.get<character_id>( "killer" );
if( killer != g->u.getID() ) {
if( killer != get_avatar().getID() ) {
break;
}
std::string victim_name = e.get<cata_variant_type::string>( "victim_name" );
Expand Down
Loading

0 comments on commit 438774d

Please sign in to comment.