Debian Codegraph @

prboom2/src/dsda.c

raw
1 //
2 // Copyright(C) 2020 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 Tools
16 //
17
18 #include <math.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21
22 #include "doomstat.h"
23 #include "p_inter.h"
24 #include "p_tick.h"
25 #include "g_game.h"
26 #include "sounds.h"
27 #include "s_sound.h"
28 #include "am_map.h"
29
30 #include "dsda/analysis.h"
31 #include "dsda/args.h"
32 #include "dsda/build.h"
33 #include "dsda/demo.h"
34 #include "dsda/exhud.h"
35 #include "dsda/features.h"
36 #include "dsda/ghost.h"
37 #include "dsda/key_frame.h"
38 #include "dsda/mouse.h"
39 #include "dsda/settings.h"
40 #include "dsda/split_tracker.h"
41 #include "dsda/tracker.h"
42 #include "dsda/wad_stats.h"
43 #include "dsda.h"
44
45 #define TELEFRAG_DAMAGE 10000
46
47 // command-line toggles
48 int dsda_track_pacifist;
49 int dsda_track_100k;
50 int dsda_track_reality;
51
52 int dsda_last_leveltime;
53 int dsda_last_gamemap;
54 int dsda_startmap;
55 int dsda_movie_target;
56 dboolean dsda_any_map_completed;
57
58 // other
59 int dsda_max_kill_requirement;
60 static int dsda_session_attempts = 1;
61
62 static int turbo_scale;
63 static int start_in_build_mode;
64
65 static int line_activation[2][LINE_ACTIVATION_INDEX_MAX + 1];
66 static int line_activation_frame;
67 static int line_activation_index;
68
69 static int dsda_time_keys;
70 static int dsda_time_use;
71 static int dsda_time_secrets;
72
73 dboolean dsda_IsWeapon(mobj_t* thing);
74 void dsda_DisplayNotification(const char* msg);
75 void dsda_ResetMapVariables(void);
76
77 dboolean dsda_ILComplete(void) {
78 return dsda_any_map_completed && dsda_last_gamemap == dsda_startmap && !dsda_movie_target;
79 }
80
81 dboolean dsda_MovieComplete(void) {
82 return dsda_any_map_completed && dsda_last_gamemap == dsda_movie_target && dsda_movie_target;
83 }
84
85 void dsda_WatchLineActivation(line_t* line, mobj_t* mo) {
86 if (mo && mo->player) {
87 if (line_activation_index < LINE_ACTIVATION_INDEX_MAX) {
88 line_activation[line_activation_frame][line_activation_index] = line->iLineID;
89 ++line_activation_index;
90 line_activation[line_activation_frame][line_activation_index] = -1;
91 }
92
93 ++line->player_activations;
94 }
95 }
96
97 int* dsda_PlayerActivatedLines(void) {
98 return line_activation[!line_activation_frame];
99 }
100
101 static void dsda_FlipLineActivationTracker(void) {
102 line_activation_frame = !line_activation_frame;
103 line_activation_index = 0;
104
105 line_activation[line_activation_frame][line_activation_index] = -1;
106 }
107
108 static void dsda_ResetLineActivationTracker(void) {
109 line_activation[0][0] = -1;
110 line_activation[1][0] = -1;
111 }
112
113 static void dsda_HandleTurbo(void) {
114 dsda_arg_t* arg;
115
116 arg = dsda_Arg(dsda_arg_turbo);
117
118 if (arg->found)
119 turbo_scale = arg->value.v_int;
120
121 if (turbo_scale > 100)
122 dsda_ToggleBuildTurbo();
123 }
124
125 int dsda_TurboScale(void) {
126 return turbo_scale;
127 }
128
129 static dboolean frozen_mode;
130
131 dboolean dsda_FrozenMode(void) {
132 return frozen_mode;
133 }
134
135 void dsda_ToggleFrozenMode(void) {
136 if (demorecording || demoplayback)
137 return;
138
139 frozen_mode = !frozen_mode;
140 }
141
142 static void dsda_HandleBuild(void) {
143 start_in_build_mode = dsda_Flag(dsda_arg_build);
144 }
145
146 int dsda_StartInBuildMode(void) {
147 return start_in_build_mode;
148 }
149
150 void dsda_ReadCommandLine(void) {
151 dsda_arg_t* arg;
152 int dsda_time_all;
153
154 dsda_track_pacifist = dsda_Flag(dsda_arg_track_pacifist);
155 dsda_track_100k = dsda_Flag(dsda_arg_track_100k);
156 dsda_track_reality = dsda_Flag(dsda_arg_track_reality);
157 dsda_analysis = dsda_Flag(dsda_arg_analysis);
158 dsda_time_keys = dsda_SimpleIntArg(dsda_arg_time_keys);
159 dsda_time_use = dsda_SimpleIntArg(dsda_arg_time_use);
160 dsda_time_secrets = dsda_SimpleIntArg(dsda_arg_time_secrets);
161 dsda_time_all = dsda_SimpleIntArg(dsda_arg_time_all);
162
163 if ((arg = dsda_Arg(dsda_arg_movie))->found)
164 dsda_movie_target = arg->value.v_int;
165
166 if (dsda_time_all) {
167 dsda_time_keys = dsda_time_all;
168 dsda_time_use = dsda_time_all;
169 dsda_time_secrets = dsda_time_all;
170 }
171
172 arg = dsda_Arg(dsda_arg_export_ghost);
173 if (arg->found)
174 dsda_InitGhostExport(arg->value.v_string);
175
176 dsda_HandleTurbo();
177 dsda_HandleBuild();
178
179 arg = dsda_Arg(dsda_arg_import_ghost);
180 if (arg->found)
181 dsda_InitGhostImport(arg->value.v_string_array, arg->count);
182
183 dsda_InitAutoKeyFrames();
184 dsda_InitCommandHistory();
185 }
186
187 static int dsda_shown_attempt = 0;
188
189 int dsda_SessionAttempts(void) {
190 return dsda_session_attempts;
191 }
192
193 void dsda_DisplayNotifications(void) {
194 if (dsda_ShowDemoAttempts() && dsda_session_attempts > dsda_shown_attempt) {
195 doom_printf("Attempt %d / %d", dsda_session_attempts, dsda_DemoAttempts());
196
197 dsda_shown_attempt = dsda_session_attempts;
198 }
199
200 if (!dsda_pacifist && dsda_track_pacifist && !dsda_pacifist_note_shown) {
201 dsda_pacifist_note_shown = true;
202 dsda_DisplayNotification("Not pacifist!");
203 }
204
205 if (dsda_100k_on_map && dsda_track_100k && !dsda_100k_note_shown) {
206 dsda_TrackFeature(uf_100k);
207
208 dsda_100k_note_shown = true;
209 dsda_DisplayNotification("100K achieved!");
210 }
211
212 if (!dsda_reality && dsda_track_reality && !dsda_almost_reality_note_shown) {
213 if (!dsda_almost_reality && !dsda_almost_reality_note_shown) {
214 dsda_almost_reality_note_shown = true;
215 dsda_DisplayNotification("Not reality / almost reality!");
216 }
217 else if (!dsda_reality_note_shown) {
218 dsda_reality_note_shown = true;
219 dsda_DisplayNotification("Almost reality!");
220 }
221 }
222 }
223
224 void dsda_DecomposeILTime(dsda_level_time_t* level_time) {
225 level_time->m = dsda_last_leveltime / 35 / 60;
226 level_time->s = (dsda_last_leveltime % (60 * 35)) / 35;
227 level_time->t = round(100.f * (dsda_last_leveltime % 35) / 35);
228 }
229
230 void dsda_DecomposeMovieTime(dsda_movie_time_t* total_time) {
231 extern int totalleveltimes;
232
233 total_time->h = totalleveltimes / 35 / 60 / 60;
234 total_time->m = (totalleveltimes % (60 * 60 * 35)) / 35 / 60;
235 total_time->s = (totalleveltimes % (60 * 35)) / 35;
236 }
237
238 void dsda_DisplayNotification(const char* msg) {
239 S_StartVoidSound(gamemode == commercial ? sfx_radio : sfx_itmbk);
240 doom_printf("%s", msg);
241 }
242
243 void dsda_WatchReborn(int playernum) {
244 dsda_reborn = true;
245 }
246
247 void dsda_WatchCard(card_t card) {
248 if (dsda_time_keys)
249 switch (card) {
250 case it_bluecard:
251 case it_blueskull:
252 dsda_AddSplit(DSDA_SPLIT_BLUE_KEY, dsda_time_keys);
253 break;
254 case it_yellowcard:
255 case it_yellowskull:
256 dsda_AddSplit(DSDA_SPLIT_YELLOW_KEY, dsda_time_keys);
257 break;
258 case it_redcard:
259 case it_redskull:
260 dsda_AddSplit(DSDA_SPLIT_RED_KEY, dsda_time_keys);
261 break;
262 default:
263 break;
264 }
265 }
266
267 static int player_damage_leveltime;
268 int player_damage_last_tic;
269
270 void dsda_WatchDamage(mobj_t* target, mobj_t* inflictor, mobj_t* source, int damage) {
271 if (
272 ((source && source->player) || (inflictor && inflictor->intflags & MIF_PLAYER_DAMAGED_BARREL)) \
273 && damage != TELEFRAG_DAMAGE
274 ) {
275 if (!target->player) {
276 if (leveltime != player_damage_leveltime) {
277 player_damage_leveltime = leveltime;
278 player_damage_last_tic = 0;
279 }
280
281 player_damage_last_tic += damage;
282 }
283
284 if (target->type == MT_BARREL || (heretic && target->type == HERETIC_MT_POD))
285 target->intflags |= MIF_PLAYER_DAMAGED_BARREL;
286 else if (!target->player)
287 dsda_pacifist = false;
288 }
289
290 if (target->player) {
291 dsda_reality = false;
292
293 // "almost reality" means allowing nukage damage
294 // we cannot differentiate between crushers and nukage in this scope
295 // we account for crushers in dsda_WatchCrush instead
296 if (inflictor) dsda_almost_reality = false;
297 }
298 }
299
300 void dsda_WatchDeath(mobj_t* thing) {
301 if (thing->flags & MF_COUNTKILL) {
302 ++dsda_kills_on_map;
303
304 if (dsda_kills_on_map >= totalkills) dsda_100k_on_map = true;
305 }
306 }
307
308 void dsda_WatchKill(player_t* player, mobj_t* target) {
309 player->killcount++;
310 if (target->intflags & MIF_SPAWNED_BY_ICON) player->maxkilldiscount++;
311 dsda_WadStatsKill();
312 }
313
314 void dsda_WatchResurrection(mobj_t* target, mobj_t* raiser) {
315 int i;
316
317 if (raiser && raiser->intflags & MIF_SPAWNED_BY_ICON)
318 target->intflags |= MIF_SPAWNED_BY_ICON;
319
320 if (
321 (
322 (target->flags ^ MF_COUNTKILL) &
323 (MF_FRIEND | MF_COUNTKILL)
324 ) || target->intflags & MIF_SPAWNED_BY_ICON
325 ) return;
326
327 for (i = 0; i < g_maxplayers; ++i) {
328 if (!playeringame[i] || players[i].killcount == 0) continue;
329
330 if (players[i].killcount > 0) {
331 players[i].maxkilldiscount++;
332 return;
333 }
334 }
335 }
336
337 void dsda_WatchCrush(mobj_t* thing, int damage) {
338 player_t *player;
339
340 player = thing->player;
341 if (!player) return;
342
343 // invincible
344 if (
345 (damage < 1000 || (!comp[comp_god] && (player->cheats&CF_GODMODE))) \
346 && (player->cheats&CF_GODMODE || player->powers[pw_invulnerability])
347 ) return;
348
349 dsda_almost_reality = false;
350 }
351
352 void dsda_WatchSpawn(mobj_t* spawned) {
353 if (
354 (spawned->flags & MF_COUNTKILL) \
355 || spawned->type == MT_SKULL \
356 || spawned->type == MT_BOSSBRAIN
357 ) dsda_any_monsters = true;
358
359 if (!dsda_any_weapons) dsda_any_weapons = dsda_IsWeapon(spawned);
360
361 if (!((spawned->flags ^ MF_COUNTKILL) & (MF_FRIEND | MF_COUNTKILL)))
362 ++dsda_max_kill_requirement;
363 }
364
365 void dsda_WatchFailedSpawn(mobj_t* spawned) {
366 // Fix count from dsda_WatchSpawn
367 if (!((spawned->flags ^ MF_COUNTKILL) & (MF_FRIEND | MF_COUNTKILL)))
368 --dsda_max_kill_requirement;
369 }
370
371 void dsda_WatchMorph(mobj_t* morphed) {
372 // Fix count from dsda_WatchSpawn
373 if (!((morphed->flags ^ MF_COUNTKILL) & (MF_FRIEND | MF_COUNTKILL)))
374 --dsda_max_kill_requirement;
375 }
376
377 void dsda_WatchUnMorph(mobj_t* morphed) {
378 // Fix count from dsda_WatchSpawn
379 if (!((morphed->flags ^ MF_COUNTKILL) & (MF_FRIEND | MF_COUNTKILL)))
380 --dsda_max_kill_requirement;
381 }
382
383 void dsda_WatchIconSpawn(mobj_t* spawned) {
384 spawned->intflags |= MIF_SPAWNED_BY_ICON;
385
386 // Fix count from dsda_WatchSpawn
387 // We can't know inside P_SpawnMobj what the source is
388 // This is less invasive than introducing a spawn source concept
389 if (!((spawned->flags ^ MF_COUNTKILL) & (MF_FRIEND | MF_COUNTKILL)))
390 --dsda_max_kill_requirement;
391 }
392
393 int dsda_MaxKillRequirement() {
394 return dsda_max_kill_requirement;
395 }
396
397 void dsda_WatchPTickCompleted(void) {
398 dsda_FlipLineActivationTracker();
399 }
400
401 void dsda_WatchCommand(void) {
402 int i;
403 ticcmd_t* cmd;
404 dsda_pclass_t *player_class;
405
406 for (i = 0; i < g_maxplayers; ++i) {
407 if (!playeringame[i]) continue;
408
409 cmd = &players[i].cmd;
410 player_class = &pclass[players[i].pclass];
411
412 if (cmd->buttons & BT_USE && dsda_time_use)
413 dsda_AddSplit(DSDA_SPLIT_USE, dsda_time_use);
414
415 if (cmd->sidemove != 0 || abs(cmd->forwardmove) > player_class->stroller_threshold)
416 dsda_stroller = false;
417
418 if (
419 abs(cmd->sidemove) > player_class->turbo_threshold ||
420 abs(cmd->forwardmove) > player_class->turbo_threshold
421 )
422 dsda_turbo = true;
423 }
424
425 dsda_AddCommandToCommandDisplay(&players[displayplayer].cmd);
426
427 dsda_ExportGhostFrame();
428 }
429
430 void dsda_WatchLedgeImpact(mobj_t* thing, int target_z) {
431 static int old_gametic;
432
433 if (
434 thing->player &&
435 gametic - old_gametic > 17 &&
436 thing->z > thing->floorz &&
437 target_z - thing->z < 32 * FRACUNIT
438 ) {
439 old_gametic = gametic;
440
441 if (dsda_CoordinateDisplay())
442 doom_printf(
443 "Missed ledge by %d\n",
444 ((target_z - thing->z) >> FRACBITS) - 24
445 );
446 }
447 }
448
449 void dsda_WatchBeforeLevelSetup(void) {
450 dsda_100k_on_map = false;
451 dsda_kills_on_map = 0;
452 dsda_100k_note_shown = false;
453 dsda_max_kill_requirement = 0;
454 }
455
456 void dsda_WatchAfterLevelSetup(void) {
457 dsda_SpawnGhost();
458 dsda_ResetTrackers();
459 dsda_ResetLineActivationTracker();
460 dsda_WadStatsEnterMap();
461 player_damage_last_tic = 0;
462 player_damage_leveltime = 0;
463 }
464
465 void dsda_WatchNewLevel(void) {
466 dsda_ResetAutoKeyFrameTimeout();
467 }
468
469 void dsda_WatchLevelCompletion(void) {
470 thinker_t *th;
471 mobj_t *mobj;
472 int i;
473 int secret_count = 0;
474 int kill_count = 0;
475 int missed_monsters = 0;
476
477 for (th = thinkercap.next; th != &thinkercap; th = th->next) {
478 if (th->function != P_MobjThinker) continue;
479
480 mobj = (mobj_t *)th;
481
482 // max rules: everything dead that affects kill counter except icon spawns
483 if (
484 !((mobj->flags ^ MF_COUNTKILL) & (MF_FRIEND | MF_COUNTKILL)) \
485 && !(mobj->intflags & MIF_SPAWNED_BY_ICON) \
486 && mobj->health > 0
487 ) {
488 ++missed_monsters;
489 }
490
491 if (dsda_IsWeapon(mobj)) {
492 ++dsda_missed_weapons;
493 dsda_weapon_collector = false;
494 }
495 }
496
497 dsda_missed_monsters += missed_monsters;
498
499 for (i = 0; i < g_maxplayers; ++i) {
500 if (!playeringame[i]) continue;
501
502 kill_count += players[i].killcount;
503 secret_count += players[i].secretcount;
504 }
505
506 dsda_missed_secrets += (totalsecret - secret_count);
507
508 if (kill_count < totalkills) dsda_100k = false;
509 if (secret_count < totalsecret) dsda_100s = false;
510 if (totalkills > 0) dsda_any_counted_monsters = true;
511 if (totalsecret > 0) dsda_any_secrets = true;
512
513 dsda_last_leveltime = leveltime;
514 dsda_last_gamemap = gamemap;
515 dsda_any_map_completed = true;
516
517 dsda_RecordSplit();
518 dsda_WadStatsExitMap(missed_monsters);
519 }
520
521 dboolean dsda_IsWeapon(mobj_t* thing) {
522 switch (thing->sprite) {
523 case SPR_BFUG:
524 case SPR_MGUN:
525 case SPR_CSAW:
526 case SPR_LAUN:
527 case SPR_PLAS:
528 case SPR_SHOT:
529 case SPR_SGN2:
530 return true;
531 default:
532 return false;
533 }
534 }
535
536 void dsda_WatchWeaponFire(weapontype_t weapon) {
537 if (weapon == wp_fist || weapon == wp_pistol || weapon == wp_chainsaw) return;
538
539 dsda_tyson_weapons = false;
540 }
541
542 void dsda_WatchSecret(void) {
543 if (dsda_time_secrets)
544 dsda_AddSplit(DSDA_SPLIT_SECRET, dsda_time_secrets);
545 }
546
547 static void dsda_ResetTracking(void) {
548 dsda_ResetAnalysis();
549
550 dsda_pacifist_note_shown = false;
551 dsda_reality_note_shown = false;
552 dsda_almost_reality_note_shown = false;
553 }
554
555 void dsda_WatchDeferredInitNew(int skill, int episode, int map) {
556 dsda_ResetTracking();
557
558 if (!demorecording) return;
559
560 ++dsda_session_attempts;
561
562 dsda_QueueQuickstart();
563
564 dsda_ResetRevealMap();
565 G_CheckDemoStatus();
566
567 dsda_last_gamemap = 0;
568 dsda_last_leveltime = 0;
569 dsda_any_map_completed = false;
570
571 dsda_InitDemoRecording();
572
573 boom_basetic = gametic;
574 true_basetic = gametic;
575 }
576
577 void dsda_WatchNewGame(void) {
578 if (!demorecording) return;
579
580 G_BeginRecording();
581 }
582
583 void dsda_WatchLevelReload(int* reloaded) {
584 extern int startmap;
585
586 if (!demorecording || *reloaded) return;
587
588 G_DeferedInitNew(gameskill, gameepisode, startmap);
589 *reloaded = 1;
590 }