Debian Codegraph @

prboom2/src/dsda/brute_force.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 Brute Force
16 //
17
18 #include <math.h>
19
20 #include "d_player.h"
21 #include "d_ticcmd.h"
22 #include "doomstat.h"
23 #include "lprintf.h"
24 #include "m_random.h"
25 #include "r_state.h"
26
27 #include "dsda/build.h"
28 #include "dsda/demo.h"
29 #include "dsda/features.h"
30 #include "dsda/key_frame.h"
31 #include "dsda/skip.h"
32 #include "dsda/time.h"
33 #include "dsda/utility.h"
34
35 #include "brute_force.h"
36
37 #define MAX_BF_DEPTH 35
38 #define MAX_BF_CONDITIONS 16
39
40 typedef struct {
41 int min;
42 int max;
43 int i;
44 } bf_range_t;
45
46 typedef struct {
47 dsda_key_frame_t key_frame;
48 bf_range_t forwardmove;
49 bf_range_t sidemove;
50 bf_range_t angleturn;
51 byte buttons;
52 } bf_t;
53
54 typedef struct {
55 dsda_bf_attribute_t attribute;
56 dsda_bf_operator_t operator;
57 fixed_t value;
58 fixed_t secondary_value;
59 } bf_condition_t;
60
61 typedef struct {
62 dsda_bf_attribute_t attribute;
63 dsda_bf_limit_t limit;
64 fixed_t value;
65 dboolean enabled;
66 dboolean evaluated;
67 fixed_t best_value;
68 int best_depth;
69 bf_t best_bf[MAX_BF_DEPTH];
70 } bf_target_t;
71
72 static bf_t brute_force[MAX_BF_DEPTH];
73 static int bf_depth;
74 static int bf_logictic;
75 static int bf_condition_count;
76 static bf_condition_t bf_condition[MAX_BF_CONDITIONS];
77 static long long bf_volume;
78 static long long bf_volume_max;
79 static dboolean bf_mode;
80 static dboolean bf_nomonsters;
81 static dsda_key_frame_t nomo_key_frame;
82 static bf_target_t bf_target;
83 static ticcmd_t bf_result[MAX_BF_DEPTH];
84
85 const char* dsda_bf_attribute_names[dsda_bf_attribute_max] = {
86 [dsda_bf_x] = "x",
87 [dsda_bf_y] = "y",
88 [dsda_bf_z] = "z",
89 [dsda_bf_momx] = "vx",
90 [dsda_bf_momy] = "vy",
91 [dsda_bf_speed] = "spd",
92 [dsda_bf_damage] = "dmg",
93 [dsda_bf_rng] = "rng",
94 [dsda_bf_arm] = "arm",
95 [dsda_bf_hp] = "hp",
96 [dsda_bf_ammo_0] = "am0",
97 [dsda_bf_ammo_1] = "am1",
98 [dsda_bf_ammo_2] = "am2",
99 [dsda_bf_ammo_3] = "am3",
100 [dsda_bf_ammo_4] = "am4",
101 [dsda_bf_ammo_5] = "am5",
102 [dsda_bf_bmapwidth] = "bmw",
103 };
104
105 const char* dsda_bf_misc_names[dsda_bf_misc_max] = {
106 "line skip",
107 "line activation",
108 "have item",
109 };
110
111 const char* dsda_bf_operator_names[dsda_bf_operator_max] = {
112 [dsda_bf_less_than] = "<",
113 [dsda_bf_less_than_or_equal_to] = "<=",
114 [dsda_bf_greater_than] = ">",
115 [dsda_bf_greater_than_or_equal_to] = ">=",
116 [dsda_bf_equal_to] = "==",
117 [dsda_bf_not_equal_to] = "!="
118 };
119
120 const char* dsda_bf_limit_names[dsda_bf_limit_max] = {
121 "acap",
122 "max",
123 "min",
124 };
125
126 const char* dsda_bf_item_names[dsda_bf_item_max] = {
127 [dsda_bf_red_key_card] = "rkc",
128 [dsda_bf_yellow_key_card] = "ykc",
129 [dsda_bf_blue_key_card] = "bkc",
130 [dsda_bf_red_skull_key] = "rsk",
131 [dsda_bf_yellow_skull_key] = "ysk",
132 [dsda_bf_blue_skull_key] = "bsk",
133
134 [dsda_bf_fist] = "f",
135 [dsda_bf_pistol] = "p",
136 [dsda_bf_shotgun] = "sg",
137 [dsda_bf_chaingun] = "cg",
138 [dsda_bf_rocket_launcher] = "rl",
139 [dsda_bf_plasma_gun] = "pg",
140 [dsda_bf_bfg] = "bfg",
141 [dsda_bf_chainsaw] = "cs",
142 [dsda_bf_super_shotgun] = "ssg",
143 };
144
145 static dboolean fixed_point_attribute[dsda_bf_attribute_max] = {
146 [dsda_bf_x] = true,
147 [dsda_bf_y] = true,
148 [dsda_bf_z] = true,
149 [dsda_bf_momx] = true,
150 [dsda_bf_momy] = true,
151 [dsda_bf_speed] = true,
152 [dsda_bf_damage] = true,
153 [dsda_bf_rng] = false,
154 [dsda_bf_arm] = false,
155 [dsda_bf_hp] = false,
156 [dsda_bf_ammo_0] = false,
157 [dsda_bf_ammo_1] = false,
158 [dsda_bf_ammo_2] = false,
159 [dsda_bf_ammo_3] = false,
160 [dsda_bf_ammo_4] = false,
161 [dsda_bf_ammo_5] = false,
162 [dsda_bf_bmapwidth] = false,
163 };
164
165 static dboolean dsda_AdvanceBFRange(bf_range_t* range) {
166 ++range->i;
167
168 if (range->i > range->max) {
169 range->i = range->min;
170 return false;
171 }
172
173 return true;
174 }
175
176 static dboolean dsda_AdvanceBruteForceFrame(int frame) {
177 if (!dsda_AdvanceBFRange(&brute_force[frame].angleturn))
178 if (!dsda_AdvanceBFRange(&brute_force[frame].sidemove))
179 if (!dsda_AdvanceBFRange(&brute_force[frame].forwardmove))
180 return false;
181
182 return true;
183 }
184
185 static int dsda_AdvanceBruteForce(void) {
186 int i;
187
188 for (i = bf_depth - 1; i >= 0; --i)
189 if (dsda_AdvanceBruteForceFrame(i))
190 break;
191
192 return i;
193 }
194
195 static void dsda_CopyBFCommandDepth(ticcmd_t* cmd, bf_t* bf) {
196 memset(cmd, 0, sizeof(*cmd));
197
198 cmd->angleturn = bf->angleturn.i << 8;
199 cmd->forwardmove = bf->forwardmove.i;
200 cmd->sidemove = bf->sidemove.i;
201 cmd->buttons = bf->buttons;
202 }
203
204 static void dsda_CopyBFResult(bf_t* bf, int depth) {
205 int i;
206
207 for (i = 0; i < depth; ++i)
208 dsda_CopyBFCommandDepth(&bf_result[i], &bf[i]);
209
210 if (i != MAX_BF_DEPTH)
211 memset(&bf_result[i], 0, sizeof(ticcmd_t) * (MAX_BF_DEPTH - i));
212 }
213
214 static void dsda_RestoreBFKeyFrame(int frame) {
215 dsda_RestoreKeyFrame(&brute_force[frame].key_frame, true);
216 }
217
218 static void dsda_StoreBFKeyFrame(int frame) {
219 dsda_StoreKeyFrame(&brute_force[frame].key_frame, true, false);
220 }
221
222 static void dsda_PrintBFProgress(void) {
223 int percent;
224 unsigned long long elapsed_time;
225
226 percent = 100 * bf_volume / bf_volume_max;
227 elapsed_time = dsda_ElapsedTimeMS(dsda_timer_brute_force);
228
229 lprintf(LO_INFO, " %lld / %lld sequences tested (%d%%) in %.2f seconds!\n",
230 bf_volume, bf_volume_max, percent, (float) elapsed_time / 1000);
231 }
232
233 #define BF_FAILURE 0
234 #define BF_SUCCESS 1
235
236 static const char* bf_result_text[2] = { "FAILURE", "SUCCESS" };
237 static dboolean brute_force_ended;
238
239 dboolean dsda_BruteForceEnded(void) {
240 return brute_force_ended;
241 }
242
243 static void dsda_EndBF(int result) {
244 brute_force_ended = true;
245
246 lprintf(LO_INFO, "Brute force complete (%s)!\n", bf_result_text[result]);
247 dsda_PrintBFProgress();
248
249 if (bf_nomonsters)
250 dsda_RestoreKeyFrame(&nomo_key_frame, true);
251 else
252 dsda_RestoreBFKeyFrame(0);
253
254 bf_mode = false;
255
256 if (result == BF_SUCCESS)
257 dsda_QueueBuildCommands(bf_result, bf_depth);
258 else
259 dsda_ExitSkipMode();
260 }
261
262 static fixed_t dsda_BFAttribute(int attribute) {
263 extern int bmapwidth;
264
265 player_t* player;
266
267 player = &players[displayplayer];
268
269 switch (attribute) {
270 case dsda_bf_x:
271 return player->mo->x;
272 case dsda_bf_y:
273 return player->mo->y;
274 case dsda_bf_z:
275 return player->mo->z;
276 case dsda_bf_momx:
277 return player->mo->momx;
278 case dsda_bf_momy:
279 return player->mo->momy;
280 case dsda_bf_speed:
281 return P_PlayerSpeed(player);
282 case dsda_bf_damage:
283 {
284 extern int player_damage_last_tic;
285
286 return player_damage_last_tic;
287 }
288 case dsda_bf_rng:
289 return rng.rndindex;
290 case dsda_bf_arm:
291 return player->armorpoints[ARMOR_ARMOR];
292 case dsda_bf_hp:
293 return player->health;
294 case dsda_bf_ammo_0:
295 return player->ammo[0];
296 case dsda_bf_ammo_1:
297 return player->ammo[1];
298 case dsda_bf_ammo_2:
299 return player->ammo[2];
300 case dsda_bf_ammo_3:
301 return player->ammo[3];
302 case dsda_bf_ammo_4:
303 return player->ammo[4];
304 case dsda_bf_ammo_5:
305 return player->ammo[5];
306 case dsda_bf_bmapwidth:
307 return bmapwidth;
308 default:
309 return 0;
310 }
311 }
312
313 static dboolean dsda_BFHaveItem(int item) {
314 player_t* player;
315
316 player = &players[displayplayer];
317
318 switch (item) {
319 case dsda_bf_red_key_card:
320 return player->cards[it_redcard];
321 case dsda_bf_yellow_key_card:
322 return player->cards[it_yellowcard];
323 case dsda_bf_blue_key_card:
324 return player->cards[it_bluecard];
325 case dsda_bf_red_skull_key:
326 return player->cards[it_redskull];
327 case dsda_bf_yellow_skull_key:
328 return player->cards[it_yellowskull];
329 case dsda_bf_blue_skull_key:
330 return player->cards[it_blueskull];
331 case dsda_bf_fist:
332 return player->weaponowned[wp_fist];
333 case dsda_bf_pistol:
334 return player->weaponowned[wp_pistol];
335 case dsda_bf_shotgun:
336 return player->weaponowned[wp_shotgun];
337 case dsda_bf_chaingun:
338 return player->weaponowned[wp_chaingun];
339 case dsda_bf_rocket_launcher:
340 return player->weaponowned[wp_missile];
341 case dsda_bf_plasma_gun:
342 return player->weaponowned[wp_plasma];
343 case dsda_bf_bfg:
344 return player->weaponowned[wp_bfg];
345 case dsda_bf_chainsaw:
346 return player->weaponowned[wp_chainsaw];
347 case dsda_bf_super_shotgun:
348 return player->weaponowned[wp_supershotgun];
349 default:
350 return false;
351 }
352 }
353
354 static dboolean dsda_BFMiscConditionReached(int i) {
355 switch (bf_condition[i].attribute) {
356 case dsda_bf_line_skip:
357 return lines[bf_condition[i].value].player_activations == bf_condition[i].secondary_value;
358 case dsda_bf_line_activation:
359 return lines[bf_condition[i].value].player_activations > bf_condition[i].secondary_value;
360 case dsda_bf_have_item:
361 return dsda_BFHaveItem(bf_condition[i].value);
362 case dsda_bf_lack_item:
363 return !dsda_BFHaveItem(bf_condition[i].value);
364 default:
365 return false;
366 }
367 }
368
369 static dboolean dsda_BFConditionReached(int i) {
370 fixed_t value;
371
372 if (bf_condition[i].operator == dsda_bf_operator_misc)
373 return dsda_BFMiscConditionReached(i);
374
375 value = dsda_BFAttribute(bf_condition[i].attribute);
376
377 switch (bf_condition[i].operator) {
378 case dsda_bf_less_than:
379 return value < bf_condition[i].value;
380 case dsda_bf_less_than_or_equal_to:
381 return value <= bf_condition[i].value;
382 case dsda_bf_greater_than:
383 return value > bf_condition[i].value;
384 case dsda_bf_greater_than_or_equal_to:
385 return value >= bf_condition[i].value;
386 case dsda_bf_equal_to:
387 return value == bf_condition[i].value;
388 case dsda_bf_not_equal_to:
389 return value != bf_condition[i].value;
390 default:
391 return false;
392 }
393 }
394
395 static void dsda_BFUpdateBestResult(fixed_t value) {
396 int i;
397 char str[FIXED_STRING_LENGTH];
398 char cmd_str[COMMAND_MOVEMENT_STRING_LENGTH];
399
400 bf_target.evaluated = true;
401 bf_target.best_value = value;
402 bf_target.best_depth = true_logictic - bf_logictic;
403
404 for (i = 0; i < bf_target.best_depth; ++i)
405 bf_target.best_bf[i] = brute_force[i];
406
407 dsda_CopyBFResult(bf_target.best_bf, bf_target.best_depth);
408
409 if (fixed_point_attribute[bf_target.attribute])
410 dsda_FixedToString(str, value);
411 else
412 snprintf(str, FIXED_STRING_LENGTH, "%i", value);
413
414 lprintf(LO_INFO, "New best: %s = %s\n", dsda_bf_attribute_names[bf_target.attribute], str);
415
416 for (i = 0; i < bf_target.best_depth; ++i) {
417 dsda_PrintCommandMovement(cmd_str, &bf_result[i]);
418 lprintf(LO_INFO, " %s\n", cmd_str);
419 }
420
421 lprintf(LO_INFO, "\n");
422 }
423
424 static dboolean dsda_BFNewBestResult(fixed_t value) {
425 if (!bf_target.evaluated)
426 return true;
427
428 switch (bf_target.limit) {
429 case dsda_bf_acap:
430 return abs(value - bf_target.value) < abs(bf_target.best_value - bf_target.value);
431 case dsda_bf_max:
432 return value > bf_target.best_value;
433 case dsda_bf_min:
434 return value < bf_target.best_value;
435 default:
436 return false;
437 }
438 }
439
440 static void dsda_BFEvaluateTarget(void) {
441 fixed_t value;
442
443 value = dsda_BFAttribute(bf_target.attribute);
444
445 if (dsda_BFNewBestResult(value))
446 dsda_BFUpdateBestResult(value);
447 }
448
449 static dboolean dsda_BFConditionsReached(void) {
450 int i, reached;
451
452 reached = 0;
453 for (i = 0; i < bf_condition_count; ++i)
454 reached += dsda_BFConditionReached(i);
455
456 if (reached == bf_condition_count)
457 if (bf_target.enabled) {
458 dsda_BFEvaluateTarget();
459
460 return false;
461 }
462
463 return reached == bf_condition_count;
464 }
465
466 dboolean dsda_BruteForce(void) {
467 return bf_mode;
468 }
469
470 void dsda_ResetBruteForceConditions(void) {
471 bf_condition_count = 0;
472 memset(&bf_target, 0, sizeof(bf_target));
473 }
474
475 void dsda_AddMiscBruteForceCondition(dsda_bf_attribute_t attribute, fixed_t value) {
476 if (bf_condition_count == MAX_BF_CONDITIONS)
477 return;
478
479 bf_condition[bf_condition_count].attribute = attribute;
480 bf_condition[bf_condition_count].operator = dsda_bf_operator_misc;
481 bf_condition[bf_condition_count].value = value;
482
483 switch (attribute) {
484 case dsda_bf_line_skip:
485 case dsda_bf_line_activation:
486 bf_condition[bf_condition_count].secondary_value = lines[value].player_activations;
487 break;
488 default:
489 break;
490 }
491
492 ++bf_condition_count;
493
494 lprintf(LO_INFO, "Added brute force condition: %s %d\n",
495 dsda_bf_misc_names[attribute],
496 value);
497 }
498
499 void dsda_AddBruteForceCondition(dsda_bf_attribute_t attribute,
500 dsda_bf_operator_t operator, fixed_t value) {
501 if (bf_condition_count == MAX_BF_CONDITIONS)
502 return;
503
504 bf_condition[bf_condition_count].attribute = attribute;
505 bf_condition[bf_condition_count].operator = operator;
506 bf_condition[bf_condition_count].value = value;
507
508 if (fixed_point_attribute[attribute])
509 bf_condition[bf_condition_count].value <<= FRACBITS;
510
511 ++bf_condition_count;
512
513 lprintf(LO_INFO, "Added brute force condition: %s %s %d\n",
514 dsda_bf_attribute_names[attribute],
515 dsda_bf_operator_names[operator],
516 value);
517 }
518
519 void dsda_SetBruteForceTarget(dsda_bf_attribute_t attribute,
520 dsda_bf_limit_t limit, fixed_t value, dboolean has_value) {
521 bf_target.attribute = attribute;
522 bf_target.limit = limit;
523 bf_target.value = value;
524 bf_target.best_value = dsda_BFAttribute(attribute);
525 bf_target.enabled = true;
526
527 if (has_value) {
528 lprintf(LO_INFO, "Set brute force target: %s %s %d\n",
529 dsda_bf_attribute_names[attribute],
530 dsda_bf_limit_names[limit],
531 value);
532
533 if (fixed_point_attribute[attribute])
534 bf_target.value <<= FRACBITS;
535 }
536 else
537 lprintf(LO_INFO, "Set brute force target: %s %s\n",
538 dsda_bf_attribute_names[attribute],
539 dsda_bf_limit_names[limit]);
540 }
541
542 static void dsda_SortIntPair(int* a, int* b) {
543 if (*a > *b) {
544 int temp;
545
546 temp = *a;
547 *a = *b;
548 *b = temp;
549 }
550 }
551
552 int dsda_KeepBruteForceFrame(int i) {
553 ticcmd_t cmd;
554
555 if (!dsda_CopyPendingCmd(&cmd, i))
556 return false;
557
558 brute_force[i].forwardmove.min = cmd.forwardmove;
559 brute_force[i].forwardmove.max = cmd.forwardmove;
560
561 brute_force[i].sidemove.min = cmd.sidemove;
562 brute_force[i].sidemove.max = cmd.sidemove;
563
564 brute_force[i].angleturn.min = cmd.angleturn >> 8;
565 brute_force[i].angleturn.max = cmd.angleturn >> 8;
566
567 brute_force[i].buttons = cmd.buttons;
568
569 return true;
570 }
571
572 int dsda_AddBruteForceFrame(int i,
573 int forwardmove_min, int forwardmove_max,
574 int sidemove_min, int sidemove_max,
575 int angleturn_min, int angleturn_max,
576 byte buttons) {
577 if (i < 0 || i >= MAX_BF_DEPTH)
578 return false;
579
580 dsda_SortIntPair(&forwardmove_min, &forwardmove_max);
581 dsda_SortIntPair(&sidemove_min, &sidemove_max);
582 dsda_SortIntPair(&angleturn_min, &angleturn_max);
583
584 brute_force[i].forwardmove.min = forwardmove_min;
585 brute_force[i].forwardmove.max = forwardmove_max;
586
587 brute_force[i].sidemove.min = sidemove_min;
588 brute_force[i].sidemove.max = sidemove_max;
589
590 brute_force[i].angleturn.min = angleturn_min;
591 brute_force[i].angleturn.max = angleturn_max;
592
593 brute_force[i].buttons = buttons;
594
595 return true;
596 }
597
598 void dsda_BruteForceWithoutMonsters(void) {
599 bf_nomonsters = true;
600 }
601
602 void dsda_BruteForceWithMonsters(void) {
603 bf_nomonsters = false;
604 }
605
606 dboolean dsda_StartBruteForce(int depth) {
607 int i;
608
609 if (!dsda_BuildMode()) {
610 lprintf(LO_WARN, "You cannot start brute force outside of build mode!\n");
611 return false;
612 }
613
614 if (depth <= 0 || depth > MAX_BF_DEPTH)
615 return false;
616
617 dsda_TrackFeature(uf_bruteforce);
618
619 lprintf(LO_INFO, "Brute force starting:\n");
620
621 bf_depth = depth;
622 bf_logictic = true_logictic;
623 bf_volume = 0;
624 bf_volume_max = 1;
625
626 for (i = 0; i < bf_depth; ++i) {
627 lprintf(LO_INFO, " %d: F %d:%d S %d:%d T %d:%d B %d\n", i,
628 brute_force[i].forwardmove.min, brute_force[i].forwardmove.max,
629 brute_force[i].sidemove.min, brute_force[i].sidemove.max,
630 brute_force[i].angleturn.min, brute_force[i].angleturn.max,
631 brute_force[i].buttons);
632
633 bf_volume_max *= (brute_force[i].forwardmove.max - brute_force[i].forwardmove.min + 1) *
634 (brute_force[i].sidemove.max - brute_force[i].sidemove.min + 1) *
635 (brute_force[i].angleturn.max - brute_force[i].angleturn.min + 1);
636
637 brute_force[i].forwardmove.i = brute_force[i].forwardmove.min;
638 brute_force[i].sidemove.i = brute_force[i].sidemove.min;
639 brute_force[i].angleturn.i = brute_force[i].angleturn.min;
640 }
641
642 lprintf(LO_INFO, "Testing %lld sequences with depth %d\n\n", bf_volume_max, bf_depth);
643
644 bf_mode = true;
645
646 if (bf_nomonsters) {
647 lprintf(LO_INFO, "Warning: ignoring monsters! The result may desync with monsters!\n");
648 dsda_StoreKeyFrame(&nomo_key_frame, true, false);
649 P_RemoveMonsters();
650 }
651
652 dsda_EnterSkipMode();
653
654 dsda_StartTimer(dsda_timer_brute_force);
655
656 return true;
657 }
658
659 void dsda_UpdateBruteForce(void) {
660 int frame;
661
662 frame = true_logictic - bf_logictic;
663
664 if (frame == bf_depth) {
665 if (bf_volume % 10000 == 0)
666 dsda_PrintBFProgress();
667
668 frame = dsda_AdvanceBruteForce();
669
670 if (frame >= 0)
671 dsda_RestoreBFKeyFrame(frame);
672 }
673 else
674 dsda_StoreBFKeyFrame(frame);
675 }
676
677 void dsda_EvaluateBruteForce(void) {
678 if (true_logictic - bf_logictic != bf_depth)
679 return;
680
681 ++bf_volume;
682
683 if (dsda_BFConditionsReached()) {
684 dsda_CopyBFResult(brute_force, bf_depth);
685 dsda_EndBF(BF_SUCCESS);
686 }
687 else if (bf_volume >= bf_volume_max) {
688 if (bf_target.enabled && bf_target.evaluated)
689 dsda_EndBF(BF_SUCCESS);
690 else
691 dsda_EndBF(BF_FAILURE);
692 }
693 }
694
695 void dsda_CopyBruteForceCommand(ticcmd_t* cmd) {
696 int depth;
697
698 depth = true_logictic - bf_logictic;
699
700 if (depth >= bf_depth) {
701 memset(cmd, 0, sizeof(*cmd));
702
703 return;
704 }
705
706 dsda_CopyBFCommandDepth(cmd, &brute_force[depth]);
707 }