Skip to content

Commit

Permalink
Replace lifetime_stats with stats_tracker
Browse files Browse the repository at this point in the history
Previously each player object contained some lifetime_stats about tiles
moved, etc.  Replace all that with the newly added stats_tracker
functionality.

One side-effect is that muscle-powered bionics now recharge at random
intervals, rather than regularly, but should have the same average
charge rates.
  • Loading branch information
jbytheway committed Sep 4, 2019
1 parent e69dfec commit 5789f0a
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 66 deletions.
7 changes: 6 additions & 1 deletion src/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ std::string enum_to_string<event_type>( event_type data )
case event_type::activates_mininuke: return "activates_mininuke";
case event_type::administers_mutagen: return "administers_mutagen";
case event_type::angers_amigara_horrors: return "angers_amigara_horrors";
case event_type::avatar_moves: return "avatar_moves";
case event_type::awakes_dark_wyrms: return "awakes_dark_wyrms";
case event_type::becomes_wanted: return "becomes_wanted";
case event_type::broken_bone_mends: return "broken_bone_mends";
case event_type::buries_corpse: return "buries_corpse";
case event_type::causes_resonance_cascade: return "causes_resonance_cascade";
case event_type::character_gains_effect: return "character_gains_effect";
case event_type::character_gets_headshot: return "character_gets_headshot";
case event_type::character_heals_damage: return "character_heals_damage";
case event_type::character_kills_character: return "character_kills_character";
case event_type::character_kills_monster: return "character_kills_monster";
case event_type::character_loses_effect: return "character_loses_effect";
Expand Down Expand Up @@ -88,7 +91,7 @@ constexpr std::array<std::pair<const char *, cata_variant_type>,
constexpr std::array<std::pair<const char *, cata_variant_type>,
event_spec_character::fields.size()> event_spec_character::fields;

static_assert( static_cast<int>( event_type::num_event_types ) == 58,
static_assert( static_cast<int>( event_type::num_event_types ) == 61,
"This static_assert is a reminder to add a definition below when you add a new "
"event_type. If your event_spec specialization inherits from another struct for "
"its fields definition then you probably don't need a definition here." );
Expand All @@ -100,9 +103,11 @@ static_assert( static_cast<int>( event_type::num_event_types ) == 58,

DEFINE_EVENT_FIELDS( activates_artifact )
DEFINE_EVENT_FIELDS( administers_mutagen )
DEFINE_EVENT_FIELDS( avatar_moves )
DEFINE_EVENT_FIELDS( broken_bone_mends )
DEFINE_EVENT_FIELDS( buries_corpse )
DEFINE_EVENT_FIELDS( character_gains_effect )
DEFINE_EVENT_FIELDS( character_heals_damage )
DEFINE_EVENT_FIELDS( character_kills_character )
DEFINE_EVENT_FIELDS( character_kills_monster )
DEFINE_EVENT_FIELDS( character_loses_effect )
Expand Down
25 changes: 24 additions & 1 deletion src/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ enum class event_type {
activates_mininuke,
administers_mutagen,
angers_amigara_horrors,
avatar_moves,
awakes_dark_wyrms,
becomes_wanted,
broken_bone_mends,
buries_corpse,
causes_resonance_cascade,
character_gains_effect,
character_gets_headshot,
character_heals_damage,
character_kills_character,
character_kills_monster,
character_loses_effect,
Expand Down Expand Up @@ -118,7 +121,7 @@ struct event_spec_character {
};
};

static_assert( static_cast<int>( event_type::num_event_types ) == 58,
static_assert( static_cast<int>( event_type::num_event_types ) == 61,
"This static_assert is to remind you to add a specialization for your new "
"event_type below" );

Expand Down Expand Up @@ -146,6 +149,14 @@ struct event_spec<event_type::administers_mutagen> {
template<>
struct event_spec<event_type::angers_amigara_horrors> : event_spec_empty {};

template<>
struct event_spec<event_type::avatar_moves> {
static constexpr std::array<std::pair<const char *, cata_variant_type>, 1> fields = {{
{ "mount", cata_variant_type::mtype_id },
}
};
};

template<>
struct event_spec<event_type::awakes_dark_wyrms> : event_spec_empty {};

Expand Down Expand Up @@ -183,6 +194,18 @@ struct event_spec<event_type::character_gains_effect> {
};
};

template<>
struct event_spec<event_type::character_gets_headshot> : event_spec_character {};

template<>
struct event_spec<event_type::character_heals_damage> {
static constexpr std::array<std::pair<const char *, cata_variant_type>, 2> fields = {{
{ "character", cata_variant_type::character_id },
{ "damage", cata_variant_type::int_ },
}
};
};

template<>
struct event_spec<event_type::character_kills_monster> {
static constexpr std::array<std::pair<const char *, cata_variant_type>, 2> fields = {{
Expand Down
12 changes: 8 additions & 4 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9078,8 +9078,12 @@ bool game::walk_move( const tripoint &dest_loc )
add_msg( m_good, _( "You are hiding in the %s." ), m.name( dest_loc ) );
}

if( dest_loc != u.pos() && !u.is_mounted() ) {
u.lifetime_stats.squares_walked++;
if( dest_loc != u.pos() ) {
mtype_id mount_type;
if( u.is_mounted() ) {
mount_type = u.mounted_creature->type->id;
}
g->events().send<event_type::avatar_moves>( mount_type );
}

tripoint oldpos = u.pos();
Expand Down Expand Up @@ -9730,7 +9734,7 @@ void game::on_move_effects()
// TODO: Move this to a character method
if( !u.is_mounted() ) {
const item muscle( "muscle" );
if( u.lifetime_stats.squares_walked % 8 == 0 ) {// active power gen
if( one_in( 8 ) ) {// active power gen
if( u.has_active_bionic( bionic_id( "bio_torsionratchet" ) ) ) {
u.charge_power( 1 );
}
Expand All @@ -9740,7 +9744,7 @@ void game::on_move_effects()
}
}
}
if( u.lifetime_stats.squares_walked % 160 == 0 ) { // passive power gen
if( one_in( 160 ) ) {// passive power gen
if( u.has_bionic( bionic_id( "bio_torsionratchet" ) ) ) {
u.charge_power( 1 );
}
Expand Down
24 changes: 16 additions & 8 deletions src/memorial_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "overmapbuffer.h"
#include "profession.h"
#include "skill.h"
#include "stats_tracker.h"

static const efftype_id effect_adrenaline( "adrenaline" );
static const efftype_id effect_datura( "datura" );
Expand Down Expand Up @@ -328,14 +329,17 @@ void memorial_logger::write( std::ostream &file, const std::string &epitaph ) co

//Lifetime stats
file << _( "Lifetime Stats" ) << eol;
file << indent << string_format( _( "Distance walked: %d squares" ),
u.lifetime_stats.squares_walked ) << eol;
file << indent << string_format( _( "Damage taken: %d damage" ),
u.lifetime_stats.damage_taken ) << eol;
file << indent << string_format( _( "Damage healed: %d damage" ),
u.lifetime_stats.damage_healed ) << eol;
file << indent << string_format( _( "Headshots: %d" ),
u.lifetime_stats.headshots ) << eol;
cata::event::data_type not_mounted = { { "mount", cata_variant( mtype_id() ) } };
int moves = g->stats().count( event_type::avatar_moves, not_mounted );
cata::event::data_type is_u = { { "character", cata_variant( u.getID() ) } };
int damage_taken = g->stats().total( event_type::character_takes_damage, "damage", is_u );
int damage_healed = g->stats().total( event_type::character_heals_damage, "damage", is_u );
int headshots = g->stats().count( event_type::character_gets_headshot, is_u );

file << indent << string_format( _( "Distance walked: %d squares" ), moves ) << eol;
file << indent << string_format( _( "Damage taken: %d damage" ), damage_taken ) << eol;
file << indent << string_format( _( "Damage healed: %d damage" ), damage_healed ) << eol;
file << indent << string_format( _( "Headshots: %d" ), headshots ) << eol;
file << eol;

//History
Expand Down Expand Up @@ -996,6 +1000,10 @@ void memorial_logger::notify( const cata::event &e )
pgettext( "memorial_female", "Set off an alarm." ) );
break;
}
// All the events for which we have no memorial log are here
case event_type::avatar_moves:
case event_type::character_gets_headshot:
case event_type::character_heals_damage:
case event_type::character_takes_damage:
break;
case event_type::num_event_types: {
Expand Down
19 changes: 6 additions & 13 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3228,7 +3228,6 @@ void player::apply_damage( Creature *source, body_part hurt, int dam, const bool
const int dam_to_bodypart = std::min( dam, hp_cur[hurtpart] );

hp_cur[hurtpart] -= dam_to_bodypart;
lifetime_stats.damage_taken += dam_to_bodypart;
g->events().send<event_type::character_takes_damage>( getID(), dam_to_bodypart );

if( hp_cur[hurtpart] <= 0 && ( source == nullptr || !source->is_hallucination() ) ) {
Expand Down Expand Up @@ -3306,12 +3305,9 @@ void player::heal( body_part healed, int dam )
void player::heal( hp_part healed, int dam )
{
if( hp_cur[healed] > 0 ) {
hp_cur[healed] += dam;
if( hp_cur[healed] > hp_max[healed] ) {
lifetime_stats.damage_healed -= hp_cur[healed] - hp_max[healed];
hp_cur[healed] = hp_max[healed];
}
lifetime_stats.damage_healed += dam;
int effective_heal = std::min( dam, hp_max[healed] - hp_cur[healed] );
hp_cur[healed] += effective_heal;
g->events().send<event_type::character_heals_damage>( getID(), effective_heal );
}
}

Expand All @@ -3332,12 +3328,9 @@ void player::hurtall( int dam, Creature *source, bool disturb /*= true*/ )
for( int i = 0; i < num_hp_parts; i++ ) {
const hp_part bp = static_cast<hp_part>( i );
// Don't use apply_damage here or it will annoy the player with 6 queries
hp_cur[bp] -= dam;
lifetime_stats.damage_taken += dam;
if( hp_cur[bp] < 0 ) {
lifetime_stats.damage_taken += hp_cur[bp];
hp_cur[bp] = 0;
}
const int dam_to_bodypart = std::min( dam, hp_cur[bp] );
hp_cur[bp] -= dam_to_bodypart;
g->events().send<event_type::character_takes_damage>( getID(), dam_to_bodypart );
}

// Low pain: damage is spread all over the body, so not as painful as 6 hits in one part
Expand Down
14 changes: 0 additions & 14 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,6 @@ class player_morale;
// This corresponds to the level of accuracy of a "snap" or "hip" shot.
extern const double MAX_RECOIL;

//Don't forget to add new stats counters
//to the save and load functions in savegame_json.cpp
struct stats {
int squares_walked = 0;
int damage_taken = 0;
int damage_healed = 0;
int headshots = 0;

void serialize( JsonOut &json ) const;
void deserialize( JsonIn &jsin );
};

struct stat_mod {
int strength = 0;
int dexterity = 0;
Expand Down Expand Up @@ -1647,8 +1635,6 @@ class player : public Character
std::map<body_part, int> bionic_installation_issues( const bionic_id &bioid );

std::set<character_id> follower_ids;
//Record of player stats, for posterity only
stats lifetime_stats;
void mod_stat( const std::string &stat, float modifier ) override;

bool is_underwater() const override;
Expand Down
6 changes: 4 additions & 2 deletions src/ranged.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "cata_utility.h"
#include "debug.h"
#include "dispersion.h"
#include "event_bus.h"
#include "game.h"
#include "gun_mode.h"
#include "input.h"
Expand Down Expand Up @@ -399,7 +400,8 @@ int player::fire_gun( const tripoint &target, int shots, item &gun )
}

if( shot.missed_by <= .1 ) {
lifetime_stats.headshots++; // TODO: check head existence for headshot
// TODO: check head existence for headshot
g->events().send<event_type::character_gets_headshot>( getID() );
}

if( shot.hit_critter ) {
Expand Down Expand Up @@ -672,7 +674,7 @@ dealt_projectile_attack player::throw_item( const tripoint &target, const item &
if( missed_by <= 0.1 && dealt_attack.hit_critter != nullptr ) {
practice( skill_used, final_xp_mult, MAX_SKILL );
// TODO: Check target for existence of head
lifetime_stats.headshots++;
g->events().send<event_type::character_gets_headshot>( getID() );
} else if( dealt_attack.hit_critter != nullptr && missed_by > 0.0f ) {
practice( skill_used, final_xp_mult / ( 1.0f + missed_by ), MAX_SKILL );
} else {
Expand Down
23 changes: 0 additions & 23 deletions src/savegame_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -899,8 +899,6 @@ void avatar::store( JsonOut &json ) const
json.member( "completed_missions", mission::to_uid_vector( completed_missions ) );
json.member( "failed_missions", mission::to_uid_vector( failed_missions ) );

json.member( "player_stats", lifetime_stats );

json.member( "show_map_memory", show_map_memory );

json.member( "assigned_invlet" );
Expand Down Expand Up @@ -1089,8 +1087,6 @@ void avatar::load( JsonObject &data )
}
}

data.read( "player_stats", lifetime_stats );

//Load from legacy map_memory save location (now in its own file <playername>.mm)
if( data.has_member( "map_memory_tiles" ) || data.has_member( "map_memory_curses" ) ) {
player_map_memory.load( data );
Expand Down Expand Up @@ -3105,25 +3101,6 @@ void addiction::deserialize( JsonIn &jsin )
jo.read( "sated", sated );
}

void stats::serialize( JsonOut &json ) const
{
json.start_object();
json.member( "squares_walked", squares_walked );
json.member( "damage_taken", damage_taken );
json.member( "damage_healed", damage_healed );
json.member( "headshots", headshots );
json.end_object();
}

void stats::deserialize( JsonIn &jsin )
{
JsonObject jo = jsin.get_object();
jo.read( "squares_walked", squares_walked );
jo.read( "damage_taken", damage_taken );
jo.read( "damage_healed", damage_healed );
jo.read( "headshots", headshots );
}

void serialize( const recipe_subset &value, JsonOut &jsout )
{
jsout.start_array();
Expand Down

0 comments on commit 5789f0a

Please sign in to comment.