Debian Codegraph @

prboom2/src/dsda/build.c

raw
1 //
2 // Copyright(C) 2022 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 Build Mode
16 //
17
18 #include "doomstat.h"
19 #include "g_game.h"
20
21 #include "dsda/args.h"
22 #include "dsda/brute_force.h"
23 #include "dsda/demo.h"
24 #include "dsda/exhud.h"
25 #include "dsda/features.h"
26 #include "dsda/input.h"
27 #include "dsda/key_frame.h"
28 #include "dsda/pause.h"
29 #include "dsda/playback.h"
30 #include "dsda/settings.h"
31 #include "dsda/skip.h"
32
33 #include "build.h"
34
35 typedef struct {
36 ticcmd_t* cmds;
37 int depth;
38 int original_depth;
39 } build_cmd_queue_t;
40
41 static dboolean allow_turbo;
42 static dboolean build_mode;
43 static dboolean advance_frame;
44 static ticcmd_t build_cmd;
45 static ticcmd_t overwritten_cmd;
46 static int overwritten_logictic;
47 static int build_cmd_tic = -1;
48 static dboolean replace_source = true;
49 static build_cmd_queue_t cmd_queue;
50
51 static signed char forward50(void) {
52 return dsda_Flag(dsda_arg_stroller) ?
53 pclass[players[consoleplayer].pclass].forwardmove[0] :
54 pclass[players[consoleplayer].pclass].forwardmove[1];
55 }
56
57 static signed char strafe40(void) {
58 return pclass[players[consoleplayer].pclass].sidemove[1];
59 }
60
61 static signed char strafe50(void) {
62 return dsda_Flag(dsda_arg_stroller) ? 0 : forward50();
63 }
64
65 static signed short shortTic(void) {
66 return (1 << 8);
67 }
68
69 static signed char maxForward(void) {
70 return allow_turbo ? 127 : forward50();
71 }
72
73 static signed char minBackward(void) {
74 return allow_turbo ? -127 : -forward50();
75 }
76
77 static signed char maxStrafeRight(void) {
78 return allow_turbo ? 127 : strafe50();
79 }
80
81 static signed char minStrafeLeft(void) {
82 return allow_turbo ? -128 : -strafe50();
83 }
84
85 void dsda_ChangeBuildCommand(void) {
86 if (demoplayback)
87 dsda_JoinDemo(NULL);
88
89 replace_source = true;
90 build_cmd_tic = true_logictic - 1;
91 dsda_JumpToLogicTicFrom(true_logictic, true_logictic - 1);
92 }
93
94 dboolean dsda_BuildMF(int x) {
95 if (x < 0 || x > 127)
96 return false;
97
98 build_cmd.forwardmove = x;
99
100 dsda_ChangeBuildCommand();
101
102 return true;
103 }
104
105 dboolean dsda_BuildMB(int x) {
106 if (x < 0 || x > 127)
107 return false;
108
109 build_cmd.forwardmove = -x;
110
111 dsda_ChangeBuildCommand();
112
113 return true;
114 }
115
116 dboolean dsda_BuildSR(int x) {
117 if (x < 0 || x > 127)
118 return false;
119
120 build_cmd.sidemove = x;
121
122 dsda_ChangeBuildCommand();
123
124 return true;
125 }
126
127 dboolean dsda_BuildSL(int x) {
128 if (x < 0 || x > 128)
129 return false;
130
131 build_cmd.sidemove = -x;
132
133 dsda_ChangeBuildCommand();
134
135 return true;
136 }
137
138 dboolean dsda_BuildTR(int x) {
139 if (x < 0 || x > 128)
140 return false;
141
142 build_cmd.angleturn = (-x << 8);
143
144 dsda_ChangeBuildCommand();
145
146 return true;
147 }
148
149 dboolean dsda_BuildTL(int x) {
150 if (x < 0 || x > 127)
151 return false;
152
153 build_cmd.angleturn = (x << 8);
154
155 dsda_ChangeBuildCommand();
156
157 return true;
158 }
159
160 dboolean dsda_BuildFU(int x) {
161 if (x < 0 || x > 7)
162 return false;
163
164 build_cmd.lookfly &= 0x0f;
165 build_cmd.lookfly |= (x << 4);
166
167 dsda_ChangeBuildCommand();
168
169 return true;
170 }
171
172 dboolean dsda_BuildFD(int x) {
173 if (x < 0 || x > 7)
174 return false;
175
176 if (x)
177 x = 16 - x;
178
179 build_cmd.lookfly &= 0x0f;
180 build_cmd.lookfly |= (x << 4);
181
182 dsda_ChangeBuildCommand();
183
184 return true;
185 }
186
187 dboolean dsda_BuildFC(void) {
188 build_cmd.lookfly &= 0x0f;
189 build_cmd.lookfly |= 0x80;
190
191 dsda_ChangeBuildCommand();
192
193 return true;
194 }
195
196 dboolean dsda_BuildLU(int x) {
197 if (x < 0 || x > 7)
198 return false;
199
200 build_cmd.lookfly &= 0xf0;
201 build_cmd.lookfly |= x;
202
203 dsda_ChangeBuildCommand();
204
205 return true;
206 }
207
208 dboolean dsda_BuildLD(int x) {
209 if (x < 0 || x > 7)
210 return false;
211
212 if (x)
213 x = 16 - x;
214
215 build_cmd.lookfly &= 0xf0;
216 build_cmd.lookfly |= x;
217
218 dsda_ChangeBuildCommand();
219
220 return true;
221 }
222
223 dboolean dsda_BuildLC(void) {
224 build_cmd.lookfly &= 0xf0;
225 build_cmd.lookfly |= 0x08;
226
227 dsda_ChangeBuildCommand();
228
229 return true;
230 }
231
232 dboolean dsda_BuildUA(int x) {
233 if (x < 0 || x > (heretic ? 10 : 15))
234 return false;
235
236 build_cmd.arti = x;
237
238 dsda_ChangeBuildCommand();
239
240 return true;
241 }
242
243 static void buildForward(void) {
244 if (allow_turbo) {
245 if (build_cmd.forwardmove == 127)
246 build_cmd.forwardmove = 0;
247 else if (build_cmd.forwardmove == forward50())
248 build_cmd.forwardmove = 127;
249 else
250 build_cmd.forwardmove = forward50();
251 }
252 else {
253 if (build_cmd.forwardmove == forward50())
254 build_cmd.forwardmove = 0;
255 else
256 build_cmd.forwardmove = forward50();
257 }
258
259 dsda_ChangeBuildCommand();
260 }
261
262 static void buildBackward(void) {
263 if (allow_turbo) {
264 if (build_cmd.forwardmove == -127)
265 build_cmd.forwardmove = 0;
266 else if (build_cmd.forwardmove == -forward50())
267 build_cmd.forwardmove = -127;
268 else
269 build_cmd.forwardmove = -forward50();
270 }
271 else {
272 if (build_cmd.forwardmove == -forward50())
273 build_cmd.forwardmove = 0;
274 else
275 build_cmd.forwardmove = -forward50();
276 }
277
278 dsda_ChangeBuildCommand();
279 }
280
281 static void buildFineForward(void) {
282 if (build_cmd.forwardmove < maxForward())
283 ++build_cmd.forwardmove;
284
285 dsda_ChangeBuildCommand();
286 }
287
288 static void buildFineBackward(void) {
289 if (build_cmd.forwardmove > minBackward())
290 --build_cmd.forwardmove;
291
292 dsda_ChangeBuildCommand();
293 }
294
295 static void buildStrafeRight(void) {
296 if (allow_turbo) {
297 if (build_cmd.sidemove == 127)
298 build_cmd.sidemove = 0;
299 else if (build_cmd.sidemove == strafe50())
300 build_cmd.sidemove = 127;
301 else
302 build_cmd.sidemove = strafe50();
303 }
304 else {
305 if (build_cmd.sidemove == strafe50())
306 build_cmd.sidemove = 0;
307 else
308 build_cmd.sidemove = strafe50();
309 }
310
311 dsda_ChangeBuildCommand();
312 }
313
314 static void buildStrafeLeft(void) {
315 if (allow_turbo) {
316 if (build_cmd.sidemove == -128)
317 build_cmd.sidemove = 0;
318 else if (build_cmd.sidemove == -strafe50())
319 build_cmd.sidemove = -128;
320 else
321 build_cmd.sidemove = -strafe50();
322 }
323 else {
324 if (build_cmd.sidemove == -strafe50())
325 build_cmd.sidemove = 0;
326 else
327 build_cmd.sidemove = -strafe50();
328 }
329
330 dsda_ChangeBuildCommand();
331 }
332
333 static void buildFineStrafeRight(void) {
334 if (build_cmd.sidemove < maxStrafeRight())
335 ++build_cmd.sidemove;
336
337 dsda_ChangeBuildCommand();
338 }
339
340 static void buildFineStrafeLeft(void) {
341 if (build_cmd.sidemove > minStrafeLeft())
342 --build_cmd.sidemove;
343
344 dsda_ChangeBuildCommand();
345 }
346
347 static void buildTurnRight(void) {
348 build_cmd.angleturn -= shortTic();
349
350 dsda_ChangeBuildCommand();
351 }
352
353 static void buildTurnLeft(void) {
354 build_cmd.angleturn += shortTic();
355
356 dsda_ChangeBuildCommand();
357 }
358
359 static void buildUse(void) {
360 build_cmd.buttons ^= BT_USE;
361
362 dsda_ChangeBuildCommand();
363 }
364
365 static void buildFire(void) {
366 build_cmd.buttons ^= BT_ATTACK;
367
368 dsda_ChangeBuildCommand();
369 }
370
371 static void buildWeapon(int weapon) {
372 int cmdweapon;
373
374 cmdweapon = weapon << BT_WEAPONSHIFT;
375
376 if (build_cmd.buttons & BT_CHANGE && (build_cmd.buttons & BT_WEAPONMASK) == cmdweapon)
377 build_cmd.buttons &= ~BT_CHANGE;
378 else
379 build_cmd.buttons |= BT_CHANGE;
380
381 build_cmd.buttons &= ~BT_WEAPONMASK;
382 if (build_cmd.buttons & BT_CHANGE)
383 build_cmd.buttons |= cmdweapon;
384
385 dsda_ChangeBuildCommand();
386 }
387
388 static void resetCmd(void) {
389 memset(&build_cmd, 0, sizeof(build_cmd));
390 }
391
392 dboolean dsda_AllowBuilding(void) {
393 return !dsda_StrictMode();
394 }
395
396 dboolean dsda_BuildMode(void) {
397 return build_mode;
398 }
399
400 void dsda_QueueBuildCommands(ticcmd_t* cmds, int depth) {
401 cmd_queue.original_depth = depth;
402 cmd_queue.depth = depth;
403
404 if (cmd_queue.cmds)
405 Z_Free(cmd_queue.cmds);
406
407 cmd_queue.cmds = Z_Malloc(depth * sizeof(*cmds));
408 memcpy(cmd_queue.cmds, cmds, depth * sizeof(*cmds));
409 }
410
411 static void dsda_PopCommandQueue(ticcmd_t* cmd) {
412 *cmd = cmd_queue.cmds[cmd_queue.original_depth - cmd_queue.depth];
413 --cmd_queue.depth;
414
415 if (!cmd_queue.depth)
416 dsda_ExitSkipMode();
417 }
418
419 dboolean dsda_BuildPlayback(void) {
420 return !replace_source;
421 }
422
423 void dsda_CopyBuildCmd(ticcmd_t* cmd) {
424 *cmd = build_cmd;
425 }
426
427 void dsda_ReadBuildCmd(ticcmd_t* cmd) {
428 if (cmd_queue.depth)
429 dsda_PopCommandQueue(cmd);
430 else if (dsda_BruteForce())
431 dsda_CopyBruteForceCommand(cmd);
432 else if (true_logictic == build_cmd_tic) {
433 *cmd = build_cmd;
434 build_cmd_tic = -1;
435 }
436 else
437 dsda_CopyPendingCmd(cmd, 0);
438
439 dsda_JoinDemoCmd(cmd);
440 }
441
442 void dsda_EnterBuildMode(void) {
443 dsda_TrackFeature(uf_build);
444
445 if (!demorecording) {
446 if (!build_mode)
447 dsda_StoreTempKeyFrame();
448
449 advance_frame = true;
450 }
451
452 if (!true_logictic)
453 advance_frame = true;
454
455 build_mode = true;
456 dsda_ApplyPauseMode(PAUSE_BUILDMODE);
457
458 dsda_RefreshExHudCommandDisplay();
459 }
460
461 void dsda_ExitBuildMode(void) {
462 build_mode = false;
463 dsda_RemovePauseMode(PAUSE_BUILDMODE);
464
465 dsda_RefreshExHudCommandDisplay();
466 }
467
468 void dsda_RefreshBuildMode(void) {
469 if (demoplayback)
470 replace_source = false;
471
472 if (!dsda_SkipMode() &&
473 overwritten_logictic != true_logictic - 1 &&
474 build_cmd_tic == -1 &&
475 true_logictic > 0) {
476 dsda_CopyPriorCmd(&overwritten_cmd, 1);
477 build_cmd = overwritten_cmd;
478 overwritten_logictic = true_logictic - 1;
479 replace_source = false;
480 }
481 }
482
483 dboolean dsda_BuildResponder(event_t* ev) {
484 if (!dsda_AllowBuilding())
485 return false;
486
487 if (dsda_InputActivated(dsda_input_build)) {
488 if (dsda_BuildMode())
489 dsda_ExitBuildMode();
490 else
491 dsda_EnterBuildMode();
492
493 return true;
494 }
495
496 if (!build_mode || menuactive)
497 return false;
498
499 if (dsda_InputActivated(dsda_input_build_source)) {
500 replace_source = !replace_source;
501
502 if (!replace_source) {
503 build_cmd = overwritten_cmd;
504
505 dsda_ChangeBuildCommand();
506 replace_source = false;
507 }
508
509 return true;
510 }
511
512 if (dsda_InputActivated(dsda_input_build_advance_frame)) {
513 advance_frame = true;
514 build_cmd_tic = true_logictic;
515
516 build_cmd.angleturn = 0;
517 build_cmd.arti = 0;
518 build_cmd.buttons &= ~BT_USE;
519 if (build_cmd.buttons & BT_CHANGE)
520 build_cmd.buttons &= ~(BT_CHANGE | BT_WEAPONMASK);
521
522 if (dsda_CopyPendingCmd(&overwritten_cmd, 0)) {
523 if (!replace_source)
524 build_cmd = overwritten_cmd;
525 }
526 else {
527 overwritten_cmd = build_cmd;
528 replace_source = true;
529 }
530
531 overwritten_logictic = true_logictic;
532
533 if (!demorecording)
534 dsda_StoreTempKeyFrame();
535
536 return true;
537 }
538
539 if (dsda_InputActivated(dsda_input_build_reverse_frame)) {
540 if (!demorecording) {
541 doom_printf("Cannot reverse outside demo");
542 return true;
543 }
544
545 if (true_logictic > 1) {
546 dsda_CopyPriorCmd(&build_cmd, 2);
547 overwritten_cmd = build_cmd;
548 overwritten_logictic = true_logictic - 2;
549 replace_source = false;
550
551 dsda_JumpToLogicTic(true_logictic - 1);
552 }
553
554 return true;
555 }
556
557 if (dsda_InputActivated(dsda_input_build_reset_command)) {
558 resetCmd();
559
560 return true;
561 }
562
563 if (dsda_InputActivated(dsda_input_build_forward)) {
564 buildForward();
565
566 return true;
567 }
568
569 if (dsda_InputActivated(dsda_input_build_backward)) {
570 buildBackward();
571
572 return true;
573 }
574
575 if (dsda_InputActivated(dsda_input_build_fine_forward)) {
576 buildFineForward();
577
578 return true;
579 }
580
581 if (dsda_InputActivated(dsda_input_build_fine_backward)) {
582 buildFineBackward();
583
584 return true;
585 }
586
587 if (dsda_InputActivated(dsda_input_build_strafe_right)) {
588 buildStrafeRight();
589
590 return true;
591 }
592
593 if (dsda_InputActivated(dsda_input_build_strafe_left)) {
594 buildStrafeLeft();
595
596 return true;
597 }
598
599 if (dsda_InputActivated(dsda_input_build_fine_strafe_right)) {
600 buildFineStrafeRight();
601
602 return true;
603 }
604
605 if (dsda_InputActivated(dsda_input_build_fine_strafe_left)) {
606 buildFineStrafeLeft();
607
608 return true;
609 }
610
611 if (dsda_InputActivated(dsda_input_build_turn_right)) {
612 buildTurnRight();
613
614 return true;
615 }
616
617 if (dsda_InputActivated(dsda_input_build_turn_left)) {
618 buildTurnLeft();
619
620 return true;
621 }
622
623 if (dsda_InputActivated(dsda_input_build_use)) {
624 buildUse();
625
626 return true;
627 }
628
629 if (dsda_InputActivated(dsda_input_build_fire)) {
630 buildFire();
631
632 return true;
633 }
634
635 if (dsda_InputActivated(dsda_input_build_weapon1)) {
636 buildWeapon(0);
637
638 return true;
639 }
640
641 if (dsda_InputActivated(dsda_input_build_weapon2)) {
642 buildWeapon(1);
643
644 return true;
645 }
646
647 if (dsda_InputActivated(dsda_input_build_weapon3)) {
648 buildWeapon(2);
649
650 return true;
651 }
652
653 if (dsda_InputActivated(dsda_input_build_weapon4)) {
654 buildWeapon(3);
655
656 return true;
657 }
658
659 if (dsda_InputActivated(dsda_input_build_weapon5)) {
660 buildWeapon(4);
661
662 return true;
663 }
664
665 if (dsda_InputActivated(dsda_input_build_weapon6)) {
666 buildWeapon(5);
667
668 return true;
669 }
670
671 if (dsda_InputActivated(dsda_input_build_weapon7)) {
672 buildWeapon(6);
673
674 return true;
675 }
676
677 if (dsda_InputActivated(dsda_input_build_weapon8)) {
678 buildWeapon(7);
679
680 return true;
681 }
682
683 if (dsda_InputActivated(dsda_input_build_weapon9)) {
684 if (!demo_compatibility && gamemode == commercial)
685 buildWeapon(8);
686
687 return true;
688 }
689
690 if (dsda_InputActivated(dsda_input_join_demo))
691 dsda_JoinDemo(NULL);
692
693 return false;
694 }
695
696 void dsda_ToggleBuildTurbo(void) {
697 allow_turbo = !allow_turbo;
698
699 if (!allow_turbo) {
700 if (build_cmd.forwardmove > maxForward())
701 build_cmd.forwardmove = maxForward();
702 else if (build_cmd.forwardmove < minBackward())
703 build_cmd.forwardmove = minBackward();
704
705 if (build_cmd.sidemove > maxStrafeRight())
706 build_cmd.sidemove = maxStrafeRight();
707 else if (build_cmd.sidemove < minStrafeLeft())
708 build_cmd.sidemove = minStrafeLeft();
709 }
710 }
711
712 dboolean dsda_AdvanceFrame(void) {
713 dboolean result;
714
715 if (dsda_SkipMode())
716 advance_frame = true;
717
718 result = advance_frame;
719 advance_frame = false;
720
721 return result;
722 }