forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kill_tracker.cpp
165 lines (148 loc) · 4.61 KB
/
kill_tracker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "kill_tracker.h"
#include <string>
#include <tuple>
#include <utility>
#include "avatar.h"
#include "cata_variant.h"
#include "character.h"
#include "character_id.h"
#include "color.h"
#include "event.h"
#include "game.h"
#include "mtype.h"
#include "npc.h"
#include "options.h"
#include "string_formatter.h"
#include "translations.h"
void kill_tracker::reset( const std::map<mtype_id, int> &kills_,
const std::vector<std::string> &npc_kills_ )
{
kills = kills_;
npc_kills = npc_kills_;
}
int kill_tracker::kill_count( const mtype_id &mon ) const
{
auto it = kills.find( mon );
if( it != kills.end() ) {
return it->second;
}
return 0;
}
int kill_tracker::kill_count( const species_id &spec ) const
{
int result = 0;
for( const auto &pair : kills ) {
if( pair.first->in_species( spec ) ) {
result += pair.second;
}
}
return result;
}
int kill_tracker::monster_kill_count() const
{
int result = 0;
for( const auto &pair : kills ) {
result += pair.second;
}
return result;
}
int kill_tracker::npc_kill_count() const
{
return npc_kills.size();
}
int kill_tracker::legacy_kill_xp() const
{
int ret = 0;
for( const std::pair<const mtype_id, int> &pair : kills ) {
ret += ( pair.first->difficulty + pair.first->difficulty_base ) * pair.second;
}
ret += npc_kills.size() * 10;
return ret;
}
std::string kill_tracker::get_kills_text() const
{
std::vector<std::string> data;
int totalkills = 0;
std::map<std::tuple<std::string, std::string, nc_color>, int> kill_counts;
// map <name, sym, color> to kill count
for( const std::pair<const mtype_id, int> &elem : kills ) {
const mtype &m = elem.first.obj();
auto key = std::make_tuple( m.nname(), m.sym, m.color );
kill_counts[key] += elem.second;
totalkills += elem.second;
}
for( const auto &entry : kill_counts ) {
const int num_kills = entry.second;
const std::string &mname = std::get<0>( entry.first );
const std::string &symbol = std::get<1>( entry.first );
const nc_color color = std::get<2>( entry.first );
data.push_back( string_format( "%4d ", num_kills ) + colorize( symbol,
color ) + " " + colorize( mname, c_light_gray ) );
}
for( const auto &npc_name : npc_kills ) {
totalkills += 1;
data.push_back( string_format( "%4d ", 1 ) + colorize( "@ " + npc_name, c_magenta ) );
}
std::string buffer;
if( data.empty() ) {
buffer = _( "You haven't killed any monsters yet!" );
} else {
buffer = string_format( _( "KILL COUNT: %d" ), totalkills );
if( get_option<bool>( "STATS_THROUGH_KILLS" ) ) {
buffer += string_format( _( "\nExperience: %d (%d points available)" ), get_avatar().kill_xp,
get_avatar().free_upgrade_points() );
}
buffer += "\n";
}
for( const std::string &line : data ) {
buffer += "\n" + line;
}
return buffer;
}
void kill_tracker::clear()
{
kills.clear();
npc_kills.clear();
}
static Character *get_avatar_or_follower( const character_id &id )
{
Character &player = get_player_character();
if( player.getID() == id ) {
return &player;
}
if( g->get_follower_list().count( id ) ) {
return g->find_npc( id );
}
return nullptr;
}
static int compute_kill_xp( const mtype_id &mon_type )
{
return mon_type->difficulty + mon_type->difficulty_base;
}
static constexpr int npc_kill_xp = 10;
void kill_tracker::notify( const cata::event &e )
{
switch( e.type() ) {
case event_type::character_kills_monster: {
const character_id killer_id = e.get<character_id>( "killer" );
if( Character *killer = get_avatar_or_follower( killer_id ) ) {
const mtype_id victim_type = e.get<mtype_id>( "victim_type" );
kills[victim_type]++;
killer->kill_xp += compute_kill_xp( victim_type );
victim_type.obj().families.practice_kill( *killer );
}
break;
}
case event_type::character_kills_character: {
const character_id killer_id = e.get<character_id>( "killer" );
if( Character *killer = get_avatar_or_follower( killer_id ) ) {
const std::string victim_name = e.get<cata_variant_type::string>( "victim_name" );
npc_kills.push_back( victim_name );
killer->kill_xp += npc_kill_xp;
}
break;
}
default:
break;
}
}