File: stats.cpp

package info (click to toggle)
freespace2 25.0.0~rc11%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 47,232 kB
  • sloc: cpp: 657,500; ansic: 22,305; sh: 293; python: 200; makefile: 198; xml: 181
file content (291 lines) | stat: -rw-r--r-- 7,233 bytes parent folder | download
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * Copyright (C) Volition, Inc. 1999.  All rights reserved.
 *
 * All source code herein is the property of Volition, Inc. You may not sell 
 * or otherwise commercially exploit the source or things you created based on the 
 * source.
 *
*/




#include "globalincs/systemvars.h"
#include "hud/hud.h"
#include "network/multi.h"
#include "pilotfile/pilotfile.h"
#include "playerman/player.h"
#include "stats/stats.h"

static const scoring_struct *Player_stats;
static scoring_struct All_time_ever_stats;

void show_stats_init()
{
	if (Game_mode & GM_MULTIPLAYER) {				
		set_player_stats(MY_NET_PLAYER_NUM);
	} else {
		Player_stats = &Player->stats;
	}	

	// multi only has current stats and all-time-ever stats, so this is only needed in single-player
	Pilot.export_stats(&All_time_ever_stats);
}

// write out the label for each stat
void show_stats_label(StatsType type, int sx, int sy, int dy)
{
	char text[NAME_LENGTH];

	if (type == StatsType::MISSION_STATS)
		gr_printf_menu(sx, sy, "%s", XSTR("Mission Stats", 114));
	else
		gr_printf_menu(sx, sy, "%s", XSTR("All Time Stats", 128));
	sy += 2 * dy;


	gr_printf_menu(sx, sy, "%s", XSTR("Total kills", 115));
	sy += 2 * dy;


	gr_printf_menu(sx, sy, "%s", XSTR("Primary weapon shots", 116));
	sy += dy;

	gr_printf_menu(sx, sy, "%s", XSTR("Primary weapon hits", 117));
	sy += dy;

	gr_printf_menu(sx, sy, "%s", XSTR("Primary friendly hits", 118));
	sy += dy;

	strcpy_s(text, XSTR("Primary hit %", 119));
	replace_one(text, "%%", "%", NAME_LENGTH - 1);
	gr_printf_menu(sx, sy, "%s", text);
	sy += dy;

	strcpy_s(text, XSTR("Primary friendly hit %", 120));
	replace_one(text, "%%", "%", NAME_LENGTH - 1);
	gr_printf_menu(sx, sy, "%s", text);
	sy += 2 * dy;


	gr_printf_menu(sx, sy, "%s", XSTR("Secondary weapon shots", 121));
	sy += dy;

	gr_printf_menu(sx, sy, "%s", XSTR("Secondary weapon hits", 122));
	sy += dy;

	gr_printf_menu(sx, sy, "%s", XSTR("Secondary friendly hits", 123));
	sy += dy;

	strcpy_s(text, XSTR("Secondary hit %", 124));
	replace_one(text, "%%", "%", NAME_LENGTH - 1);
	gr_printf_menu(sx, sy, "%s", text);
	sy += dy;

	strcpy_s(text, XSTR("Secondary friendly hit %", 125));
	replace_one(text, "%%", "%", NAME_LENGTH - 1);
	gr_printf_menu(sx, sy, "%s", text);
	sy += 2 * dy;


	gr_printf_menu(sx, sy, "%s", XSTR("Assists", 126));
	sy += 2 * dy;


	if (Game_mode & GM_MULTIPLAYER)
	{
		if (type == StatsType::MISSION_STATS)
		{
			gr_printf_menu(sx, sy, "%s", XSTR("Player Deaths", 127));
			sy += 2 * dy;
		}

		if (type == StatsType::MISSION_STATS)
			gr_printf_menu(sx, sy, "%s", XSTR("Mission score", 1526));
		else
			gr_printf_menu(sx, sy, "%s", XSTR("Score", 1527));
	}
}

void show_stats_numbers(StatsType type, int sx, int sy, int dy)
{
	switch (type)
	{
		case StatsType::MISSION_STATS:
			show_stats_numbers(*Player_stats, true, sx, sy, dy);
			break;

		case StatsType::ALL_TIME_CAMPAIGN_STATS:
			Assertion(!(Game_mode & GM_MULTIPLAYER), "ALL_TIME_CAMPAIGN_STATS are not stored for multiplayer!");
			show_stats_numbers(*Player_stats, false, sx, sy, dy);
			break;

		case StatsType::ALL_TIME_EVER_STATS:
			if (Game_mode & GM_MULTIPLAYER)
				show_stats_numbers(*Player_stats, false, sx, sy, dy);		// in multiplayer, the all-time-ever stats are stored in the player
			else
				show_stats_numbers(All_time_ever_stats, false, sx, sy, dy);
			break;

		default:
			UNREACHABLE("Unhandled StatsType %d in show_stats_numbers()", static_cast<int>(type));
	}
}

int stats_get_kills(StatsType type, int ship_class)
{
	switch (type)
	{
		case StatsType::MISSION_STATS:
			return stats_get_kills(*Player_stats, true, ship_class);

		case StatsType::ALL_TIME_CAMPAIGN_STATS:
			return stats_get_kills(*Player_stats, false, ship_class);

		case StatsType::ALL_TIME_EVER_STATS:
			if (Game_mode & GM_MULTIPLAYER)
				return stats_get_kills(*Player_stats, false, ship_class);		// in multiplayer, the all-time-ever stats are stored in the player
			else
				return stats_get_kills(All_time_ever_stats, false, ship_class);

		default:
			UNREACHABLE("Unhandled StatsType %d in stats_get_kills()", static_cast<int>(type));
			return 0;
	}
}

void show_stats_numbers(const scoring_struct &stats, bool use_m_stats, int sx, int sy, int dy)
{
	float		pct;
	int kill_count_ok, p_shots_fired, p_shots_hit, p_bonehead_hits, s_shots_fired, s_shots_hit, s_bonehead_hits, assists, score;

	if (use_m_stats)
	{
		kill_count_ok = stats.m_kill_count_ok;
		p_shots_fired = stats.mp_shots_fired;
		p_shots_hit = stats.mp_shots_hit;
		p_bonehead_hits = stats.mp_bonehead_hits;
		s_shots_fired = stats.ms_shots_fired;
		s_shots_hit = stats.ms_shots_hit;
		s_bonehead_hits = stats.ms_bonehead_hits;
		assists = stats.m_assists;
		score = stats.m_score;
	}
	else
	{
		kill_count_ok = stats.kill_count_ok;
		p_shots_fired = stats.p_shots_fired;
		p_shots_hit = stats.p_shots_hit;
		p_bonehead_hits = stats.p_bonehead_hits;
		s_shots_fired = stats.s_shots_fired;
		s_shots_hit = stats.s_shots_hit;
		s_bonehead_hits = stats.s_bonehead_hits;
		assists = stats.assists;
		score = stats.score;
	}

	sy += 2 * dy;

	// kills stats
	gr_printf_menu(sx, sy, "%d", kill_count_ok);
	sy += 2 * dy;


	// primary weapon stats
	gr_printf_menu(sx, sy, "%u", p_shots_fired);
	sy += dy;

	gr_printf_menu(sx, sy, "%u", p_shots_hit);
	sy += dy;

	gr_printf_menu(sx, sy, "%u", p_bonehead_hits);
	sy += dy;

	if (p_shots_fired > 0)
		pct = 100.0f * i2fl(p_shots_hit) / i2fl(p_shots_fired);
	else
		pct = 0.0f;
	gr_printf_menu(sx, sy, "%d%%", fl2i(pct));
	sy += dy;

	if (p_shots_fired > 0)
		pct = 100.0f * i2fl(p_bonehead_hits) / i2fl(p_shots_fired);
	else
		pct = 0.0f;
	gr_printf_menu(sx, sy, "%d%%", fl2i(pct));
	sy += 2 * dy;


	// secondary weapon stats
	gr_printf_menu(sx, sy, "%u", s_shots_fired);
	sy += dy;

	gr_printf_menu(sx, sy, "%u", s_shots_hit);
	sy += dy;

	gr_printf_menu(sx, sy, "%u", s_bonehead_hits);
	sy += dy;

	if (s_shots_fired > 0)
		pct = 100.0f * i2fl(s_shots_hit) / i2fl(s_shots_fired);
	else
		pct = 0.0f;
	gr_printf_menu(sx, sy, "%d%%", fl2i(pct));
	sy += dy;

	if (s_shots_fired > 0)
		pct = 100.0f * i2fl(s_bonehead_hits) / i2fl(s_shots_fired);
	else
		pct = 0.0f;
	gr_printf_menu(sx, sy, "%d%%", fl2i(pct));
	sy += 2 * dy;


	// assists and player rescues (respawns)
	gr_printf_menu(sx, sy, "%d", assists);
	sy += 2 * dy;


	if (Game_mode & GM_MULTIPLAYER)
	{
		if (use_m_stats)
		{
			gr_printf_menu(sx, sy, "%d", stats.m_player_deaths);
			sy += 2 * dy;
		}

		// score
		gr_printf_menu(sx, sy, "%d", score);
	}
}

int stats_get_kills(const scoring_struct &stats, bool use_m_stats, int ship_class)
{
	Assertion(ship_class >= 0 && ship_class < MAX_SHIP_CLASSES, "ship_class is out of bounds!");

	if (use_m_stats)
		return stats.m_okKills[ship_class];
	else
		return stats.kills[ship_class];
}

void show_stats_close()
{	
}
 
// initialize the statistics portion of the player structure for multiplayer.  Only the host of
// a netgame needs to be doing this (and if fact, only he *should* be doing this)
void init_multiplayer_stats( )
{
	scoring_struct *ptr;

	for (int idx=0; idx < MAX_PLAYERS; idx++) {
		ptr = &Players[idx].stats;
		scoring_level_init( ptr );
	}
}

void set_player_stats(int pid)
{
   Player_stats = &Net_players[pid].m_player->stats;
}