Debian Codegraph @

prboom2/src/dsda/wad_stats.c

raw
1 //
2 // Copyright(C) 2023 by Ryan Krafnick
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // DESCRIPTION:
15 // DSDA Wad Stats
16 //
17
18 #include <stdio.h>
19
20 #include "doomstat.h"
21 #include "lprintf.h"
22 #include "m_file.h"
23 #include "w_wad.h"
24 #include "z_zone.h"
25
26 #include "dsda/data_organizer.h"
27 #include "dsda/skill_info.h"
28 #include "dsda/utility.h"
29
30 #include "wad_stats.h"
31
32 static const char* filename = "stats.txt";
33 static const int current_version = 1;
34 static map_stats_t* current_map_stats;
35
36 wad_stats_t wad_stats;
37
38 static void dsda_EnsureMapCount(int count) {
39 wad_stats.map_count = count;
40
41 if (wad_stats.maps_size < wad_stats.map_count) {
42 int old_size;
43
44 old_size = wad_stats.maps_size;
45 while (wad_stats.maps_size < wad_stats.map_count)
46 wad_stats.maps_size = wad_stats.maps_size ? wad_stats.maps_size * 2 : 32;
47 wad_stats.maps = Z_Realloc(wad_stats.maps, sizeof(*wad_stats.maps) * wad_stats.maps_size);
48 memset(wad_stats.maps + old_size,
49 0, (wad_stats.maps_size - old_size) * sizeof(*wad_stats.maps));
50 }
51 }
52
53 static const char* dsda_WadStatsPath(void) {
54 static dsda_string_t path;
55
56 if (!path.string)
57 dsda_StringPrintF(&path, "%s/%s", dsda_DataDir(), filename);
58
59 return path.string;
60 }
61
62 static dboolean dsda_MapStatsExist(const char* lump) {
63 int i;
64
65 for (i = 0; i < wad_stats.maps_size && wad_stats.maps[i].lump[0]; ++i)
66 if (!strncasecmp(wad_stats.maps[i].lump, lump, 8))
67 return true;
68
69 return false;
70 }
71
72 static int C_DECL dicmp_map_stats(const void* a, const void* b) {
73 const map_stats_t* m1 = (const map_stats_t *) a;
74 const map_stats_t* m2 = (const map_stats_t *) b;
75
76 if (m1->episode == -1 && m2->episode == -1)
77 return m1->lump[0] - m2->lump[0];
78
79 if (m1->episode == -1)
80 return 1;
81
82 if (m2->episode == -1)
83 return -1;
84
85 return (m1->episode == m2->episode) ?
86 (m1->map == m2->map) ?
87 m1->lump[0] - m2->lump[0] : m1->map - m2->map : m1->episode - m2->episode;
88 }
89
90 static void dsda_CreateWadStats(void) {
91 int i;
92 const char* map_name;
93 int map_count = 0;
94 dboolean any_pwad_map = false;
95
96 for (i = numlumps - 1; i > 0; --i) {
97 if (any_pwad_map && lumpinfo[i].source == source_iwad)
98 break;
99
100 if (lumpinfo[i].source != source_iwad &&
101 lumpinfo[i].source != source_pwad)
102 continue;
103
104 if (strncasecmp(lumpinfo[i].name, "THINGS", 8) &&
105 strncasecmp(lumpinfo[i].name, "TEXTMAP", 8))
106 continue;
107
108 map_name = lumpinfo[i - 1].name;
109 if (dsda_MapStatsExist(map_name))
110 continue;
111
112 if (lumpinfo[i - 1].source == source_pwad)
113 any_pwad_map = true;
114
115 {
116 int episode, map;
117 map_stats_t* ms;
118
119 map_count += 1;
120 dsda_EnsureMapCount(map_count);
121 ms = &wad_stats.maps[map_count - 1];
122 memset(ms, 0, sizeof(*wad_stats.maps));
123
124 strcpy(ms->lump, map_name);
125
126 if (sscanf(map_name, "MAP%d", &map) == 1) {
127 ms->episode = 1;
128 ms->map = map;
129 }
130 else if (sscanf(map_name, "E%dM%d", &episode, &map) == 2) {
131 ms->episode = episode;
132 ms->map = map;
133 }
134 else {
135 ms->episode = -1;
136 ms->map = -1;
137 }
138
139 ms->best_time = -1;
140 ms->best_max_time = -1;
141 ms->best_nm_time = -1;
142 ms->max_kills = -1;
143 ms->max_items = -1;
144 ms->max_secrets = -1;
145 }
146 }
147
148 qsort(wad_stats.maps, wad_stats.map_count, sizeof(*wad_stats.maps), dicmp_map_stats);
149 }
150
151 static void dsda_LoadWadStats(void) {
152 const char* path;
153 char* buffer;
154 char** lines;
155 int version;
156 int map_count = 0;
157 int i;
158
159 path = dsda_WadStatsPath();
160
161 if (M_ReadFileToString(path, &buffer) != -1) {
162 lines = dsda_SplitString(buffer, "\n\r");
163
164 if (lines) {
165 if (!lines[0] || !lines[1])
166 I_Error("Encountered invalid wad stats: %s", path);
167
168 if (sscanf(lines[0], "%d", &version) != 1)
169 I_Error("Encountered invalid wad stats: %s", path);
170
171 if (version > current_version)
172 I_Error("Encountered unsupported wad stats version: %s", path);
173
174 if (sscanf(lines[1], "%d", &wad_stats.total_kills) != 1)
175 I_Error("Encountered invalid wad stats: %s", path);
176
177 for (i = 2; lines[i] && *lines[i]; ++i) {
178 map_stats_t ms;
179
180 if (
181 sscanf(
182 lines[i], "%8s %d %d %d %d %d %d %d %d %d %d %d %d %d %d",
183 ms.lump, &ms.episode, &ms.map,
184 &ms.best_skill, &ms.best_time, &ms.best_max_time, &ms.best_nm_time,
185 &ms.total_exits, &ms.total_kills,
186 &ms.best_kills, &ms.best_items, &ms.best_secrets,
187 &ms.max_kills, &ms.max_items, &ms.max_secrets
188 ) == 15
189 ) {
190 map_count += 1;
191 dsda_EnsureMapCount(map_count);
192 wad_stats.maps[map_count - 1] = ms;
193 }
194 }
195
196 Z_Free(lines);
197 }
198
199 Z_Free(buffer);
200 }
201 }
202
203 static dboolean forget_wad_stats;
204
205 void M_ForgetWadStats(void)
206 {
207 forget_wad_stats = true;
208 }
209
210 void M_RememberWadStats(void)
211 {
212 forget_wad_stats = false;
213 }
214
215 void dsda_SaveWadStats(void) {
216 const char* path;
217 FILE* file;
218 int i;
219
220 if (forget_wad_stats)
221 return;
222
223 if (!wad_stats.map_count)
224 return;
225
226 path = dsda_WadStatsPath();
227
228 file = M_OpenFile(path, "wb");
229 if (!file)
230 {
231 lprintf(LO_WARN, "dsda_SaveWadStats: Failed to save wad stats file \"%s\".\n", path);
232 return;
233 }
234
235 fprintf(file, "%d\n", current_version);
236 fprintf(file, "%d\n", wad_stats.total_kills);
237
238 for (i = 0; i < wad_stats.map_count; ++i) {
239 map_stats_t* ms;
240
241 ms = &wad_stats.maps[i];
242 fprintf(file, "%s %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
243 ms->lump, ms->episode, ms->map,
244 ms->best_skill, ms->best_time, ms->best_max_time, ms->best_nm_time,
245 ms->total_exits, ms->total_kills,
246 ms->best_kills, ms->best_items, ms->best_secrets,
247 ms->max_kills, ms->max_items, ms->max_secrets);
248 }
249
250 fclose(file);
251 }
252
253 static map_stats_t* dsda_MapStats(int episode, int map) {
254 int i;
255
256 for (i = 0; i < wad_stats.map_count; ++i)
257 if (wad_stats.maps[i].episode == episode && wad_stats.maps[i].map == map)
258 return &wad_stats.maps[i];
259
260 return NULL;
261 }
262
263 void dsda_WadStatsEnterMap(void) {
264 current_map_stats = dsda_MapStats(gameepisode, gamemap);
265 }
266
267 void dsda_WadStatsExitMap(int missed_monsters) {
268 int skill;
269 int nightmare_skill;
270 int best_normal_skill;
271
272 if (!current_map_stats || demoplayback)
273 return;
274
275 nightmare_skill = num_skills;
276 best_normal_skill = num_skills - 1;
277
278 if (!nomonsters) {
279 skill = gameskill + 1;
280 if (skill > current_map_stats->best_skill) {
281 if (current_map_stats->best_skill < best_normal_skill) {
282 current_map_stats->best_time = -1;
283 current_map_stats->best_max_time = -1;
284 current_map_stats->best_kills = 0;
285 current_map_stats->best_items = 0;
286 current_map_stats->best_secrets = 0;
287 }
288
289 current_map_stats->best_skill = skill;
290 }
291
292 if (skill >= current_map_stats->best_skill || skill == best_normal_skill) {
293 if (levels_completed == 1)
294 if (current_map_stats->best_time == -1 || current_map_stats->best_time > leveltime)
295 current_map_stats->best_time = leveltime;
296
297 if (levels_completed == 1 && skill == nightmare_skill)
298 if (current_map_stats->best_nm_time == -1 || current_map_stats->best_nm_time > leveltime)
299 current_map_stats->best_nm_time = leveltime;
300
301 current_map_stats->max_kills = totalkills;
302 current_map_stats->max_items = totalitems;
303 current_map_stats->max_secrets = totalsecret;
304
305 if (!skill_info.respawn_time) {
306 if (totalkills - missed_monsters > current_map_stats->best_kills)
307 current_map_stats->best_kills = totalkills - missed_monsters;
308
309 if (levels_completed == 1)
310 if (missed_monsters == 0 && players[consoleplayer].secretcount == totalsecret &&
311 (current_map_stats->best_max_time == -1 || current_map_stats->best_max_time > leveltime))
312 current_map_stats->best_max_time = leveltime;
313 }
314
315 if (players[consoleplayer].itemcount > current_map_stats->best_items)
316 current_map_stats->best_items = players[consoleplayer].itemcount;
317
318 if (players[consoleplayer].secretcount > current_map_stats->best_secrets)
319 current_map_stats->best_secrets = players[consoleplayer].secretcount;
320 }
321 }
322
323 ++current_map_stats->total_exits;
324 }
325
326 void dsda_WadStatsKill(void) {
327 if (!current_map_stats || demoplayback)
328 return;
329
330 ++current_map_stats->total_kills;
331 ++wad_stats.total_kills;
332 }
333
334 void dsda_InitWadStats(void) {
335 dsda_LoadWadStats();
336
337 if (!wad_stats.map_count)
338 dsda_CreateWadStats();
339 }