Debian Codegraph @

prboom2/src/dsda/mapinfo/doom.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 MapInfo Doom
16 //
17
18 #include "doomstat.h"
19 #include "f_finale.h"
20 #include "g_game.h"
21 #include "lprintf.h"
22 #include "p_enemy.h"
23 #include "r_data.h"
24 #include "s_sound.h"
25 #include "sounds.h"
26 #include "v_video.h"
27 #include "w_wad.h"
28
29 #include "dsda/args.h"
30 #include "dsda/episode.h"
31 #include "dsda/map_format.h"
32 #include "dsda/mapinfo.h"
33 #include "dsda/mapinfo/doom/parser.h"
34 #include "dsda/preferences.h"
35
36 #include "doom.h"
37
38 static const doom_mapinfo_map_t* last_map;
39 static const doom_mapinfo_map_t* next_map;
40 static const doom_mapinfo_map_t* current_map;
41 static doom_mapinfo_cluster_t* last_cluster;
42 static doom_mapinfo_cluster_t* next_cluster;
43 static doom_mapinfo_cluster_t* current_cluster;
44 static const doom_mapinfo_map_next_t* end_data;
45
46 static doom_mapinfo_map_next_t default_end_data = {
47 .end = dmi_end_game_cast
48 };
49
50 static doom_mapinfo_cluster_t* dsda_DoomClusterEntry(int id) {
51 int i;
52
53 for (i = 0; i < doom_mapinfo.num_clusters; ++i)
54 if (id == doom_mapinfo.clusters[i].id)
55 return &doom_mapinfo.clusters[i];
56
57 return NULL;
58 }
59
60 static doom_mapinfo_map_t* dsda_DoomMapEntry(int gamemap) {
61 int i;
62
63 for (i = 0; i < doom_mapinfo.num_maps; ++i)
64 if (gamemap == doom_mapinfo.maps[i].level_num)
65 return &doom_mapinfo.maps[i];
66
67 return NULL;
68 }
69
70 static doom_mapinfo_map_t* dsda_DoomMapEntryByName(const char* name) {
71 int i;
72
73 for (i = 0; i < doom_mapinfo.num_maps; ++i)
74 if (!stricmp(doom_mapinfo.maps[i].lump_name, name))
75 return &doom_mapinfo.maps[i];
76
77 return NULL;
78 }
79
80 int dsda_DoomNameToMap(int* found, const char* name, int* episode, int* map) {
81 const doom_mapinfo_map_t* entry;
82
83 if (!doom_mapinfo.loaded)
84 return false;
85
86 entry = dsda_DoomMapEntryByName(name);
87
88 if (!entry)
89 *found = false;
90 else {
91 *found = true;
92
93 *episode = 1;
94 *map = entry->level_num;
95 }
96
97 return true;
98 }
99
100 int dsda_DoomFirstMap(int* episode, int* map) {
101 if (!doom_mapinfo.loaded)
102 return false;
103
104 if (num_episodes)
105 *map = episodes[0].start_map;
106 else
107 *map = 1;
108
109 *episode = 1;
110
111 return true;
112 }
113
114 int dsda_DoomNewGameMap(int* episode, int* map) {
115 return false;
116 }
117
118 int dsda_DoomResolveWarp(int* args, int arg_count, int* episode, int* map) {
119 const doom_mapinfo_map_t* entry;
120
121 if (!doom_mapinfo.loaded || !arg_count)
122 return false;
123
124 entry = dsda_DoomMapEntry(args[0]);
125
126 if (!entry)
127 return false;
128
129 *map = entry->level_num;
130 *episode = 1;
131
132 return true;
133 }
134
135 int dsda_DoomNextMap(int* episode, int* map) {
136 const char* next = NULL;
137 const doom_mapinfo_map_t* entry = NULL;
138
139 if (!current_map)
140 return false;
141
142 if (current_map->secret_next.map)
143 next = current_map->secret_next.map;
144
145 if (!next)
146 next = current_map->next.map;
147
148 if (next)
149 entry = dsda_DoomMapEntryByName(next);
150
151 if (!entry)
152 dsda_DoomFirstMap(episode, map);
153 else {
154 *map = entry->level_num;
155 *episode = 1;
156 }
157
158 return true;
159 }
160
161 int dsda_DoomPrevMap(int* episode, int* map) {
162 int i;
163
164 if (!current_map)
165 return false;
166
167 for (i = 0; i < doom_mapinfo.num_maps; ++i)
168 if (
169 doom_mapinfo.maps[i].secret_next.map &&
170 !stricmp(doom_mapinfo.maps[i].secret_next.map, current_map->lump_name)
171 ) {
172 *map = doom_mapinfo.maps[i].level_num;
173 *episode = 1;
174
175 return true;
176 }
177
178 for (i = 0; i < doom_mapinfo.num_maps; ++i)
179 if (
180 doom_mapinfo.maps[i].next.map &&
181 !stricmp(doom_mapinfo.maps[i].next.map, current_map->lump_name)
182 ) {
183 *map = doom_mapinfo.maps[i].level_num;
184 *episode = 1;
185
186 return true;
187 }
188
189 *map = current_map->level_num;
190 *episode = 1;
191
192 return false;
193 }
194
195 int dsda_DoomShowNextLocBehaviour(int* behaviour) {
196 if (!current_map)
197 return false;
198
199 if (end_data)
200 *behaviour = WI_SHOW_NEXT_DONE;
201 else
202 *behaviour = WI_SHOW_NEXT_LOC | WI_SHOW_NEXT_EPISODAL;
203
204 return true;
205 }
206
207 int dsda_DoomSkipDrawShowNextLoc(int* skip) {
208 if (!current_map)
209 return false;
210
211 *skip = !!end_data;
212
213 return true;
214 }
215
216 void dsda_DoomUpdateMapInfo(void) {
217 current_map = dsda_DoomMapEntry(gamemap);
218
219 if (current_map && current_map->cluster)
220 current_cluster = dsda_DoomClusterEntry(current_map->cluster);
221 else
222 current_cluster = NULL;
223 }
224
225 void dsda_DoomUpdateLastMapInfo(void) {
226 last_map = current_map;
227 last_cluster = current_cluster;
228 next_map = NULL;
229 next_cluster = NULL;
230 }
231
232 void dsda_DoomUpdateNextMapInfo(void) {
233 if (end_data)
234 next_map = NULL;
235 else
236 next_map = dsda_DoomMapEntry(wminfo.next + 1);
237
238 if (next_map && next_map->cluster)
239 next_cluster = dsda_DoomClusterEntry(next_map->cluster);
240 else
241 next_cluster = NULL;
242 }
243
244 int dsda_DoomResolveCLEV(int* clev, int* episode, int* map) {
245 const doom_mapinfo_map_t* entry;
246 int level_num;
247
248 if (!doom_mapinfo.loaded)
249 return false;
250
251 if (gamemode == commercial)
252 level_num = *map;
253 else
254 level_num = *map + *episode * 10;
255
256 entry = dsda_DoomMapEntry(level_num);
257
258 if (!entry || !W_LumpNameExists(entry->lump_name)) {
259 *clev = false;
260
261 doom_printf("IDCLEV target not found: %d", level_num);
262 }
263 else {
264 *clev = true;
265
266 *map = entry->level_num;
267 *episode = 1;
268 }
269
270 return true;
271 }
272
273 int dsda_DoomResolveINIT(int* init) {
274 return false;
275 }
276
277 int dsda_DoomMusicIndexToLumpNum(int* lump, int music_index) {
278 return false;
279 }
280
281 int dsda_DoomMapMusic(int* music_index, int* music_lump, int episode, int map) {
282 int lump;
283 const doom_mapinfo_map_t* entry;
284 int level_num;
285
286 if (gamemode == commercial)
287 level_num = map;
288 else
289 level_num = map + episode * 10;
290
291 entry = dsda_DoomMapEntry(level_num);
292
293 if (!entry || !entry->music)
294 return false;
295
296 lump = W_CheckNumForName(entry->music);
297
298 if (lump == LUMP_NOT_FOUND)
299 return false;
300
301 *music_index = -1;
302 *music_lump = lump;
303
304 return true;
305 }
306
307 int dsda_DoomIntermissionMusic(int* music_index, int* music_lump) {
308 int lump;
309
310 if (!last_map || !last_map->inter_music)
311 return false;
312
313 lump = W_CheckNumForName(last_map->inter_music);
314
315 if (lump == LUMP_NOT_FOUND)
316 return false;
317
318 *music_index = -1;
319 *music_lump = lump;
320
321 return true;
322 }
323
324 int dsda_DoomInterMusic(int* music_index, int* music_lump) {
325 int lump = LUMP_NOT_FOUND;
326
327 if (!current_map)
328 return false;
329
330 if (next_cluster && next_cluster->enter_text) {
331 if (next_cluster->music)
332 lump = W_CheckNumForName(next_cluster->music);
333 }
334 else if (current_cluster && current_cluster->exit_text) {
335 if (current_cluster->music)
336 lump = W_CheckNumForName(current_cluster->music);
337 }
338
339 if (lump == LUMP_NOT_FOUND)
340 return false;
341
342 *music_index = -1;
343 *music_lump = lump;
344
345 return true;
346 }
347
348 extern int finalestage;
349 extern int finalecount;
350 extern const char* finaletext;
351 extern const char* finaleflat;
352 extern const char* finalepatch;
353 extern int acceleratestage;
354 extern int midstage;
355
356 // TODO: use end_game everywhere and collapse all the finale implementations
357
358 int dsda_DoomStartFinale(void) {
359 const doom_mapinfo_cluster_t* finale_cluster;
360
361 if (!current_map)
362 return false;
363
364 if (next_cluster && next_cluster->enter_text) {
365 finale_cluster = next_cluster;
366 finaletext = finale_cluster->enter_text;
367 }
368 else if (current_cluster && current_cluster->exit_text) {
369 finale_cluster = current_cluster;
370 finaletext = finale_cluster->exit_text;
371 }
372 else {
373 finaletext = NULL;
374 finalepatch = NULL;
375 finaleflat = NULL;
376
377 return true;
378 }
379
380 if (finale_cluster->flat)
381 finaleflat = finale_cluster->flat;
382 else
383 finaleflat = "FLOOR4_8";
384
385 if (finale_cluster->pic)
386 finalepatch = finale_cluster->pic;
387 else
388 finalepatch = NULL;
389
390 return true;
391 }
392
393 int dsda_DoomFTicker(void) {
394 void WI_checkForAccelerate(void);
395 float Get_TextSpeed(void);
396
397 int next_level = false;
398 const int TEXTSPEED = 3;
399 const int TEXTWAIT = 250;
400 const int NEWTEXTWAIT = 1000;
401
402 if (!demo_compatibility)
403 WI_checkForAccelerate();
404 else {
405 int i;
406
407 for (i = 0; i < g_maxplayers; i++)
408 if (players[i].cmd.buttons)
409 next_level = true;
410 }
411
412 if (!next_level) {
413 // advance animation
414 finalecount++;
415
416 if (!finalestage) {
417 float speed = demo_compatibility ? TEXTSPEED : Get_TextSpeed();
418
419 if (
420 !finaletext ||
421 finalecount > strlen(finaletext) * speed + (midstage ? NEWTEXTWAIT : TEXTWAIT) ||
422 (midstage && acceleratestage)
423 )
424 next_level = true;
425 }
426 }
427
428 if (next_level) {
429 if (end_data) {
430 if (end_data->end == dmi_end_game_cast) {
431 F_StartCast(end_data->end_pic, end_data->music, end_data->loop_music);
432 return false; // let go of finale ownership
433 }
434 else if (end_data->end == dmi_end_game_scroll) {
435 F_StartScroll(end_data->end_pic, end_data->end_pic_b,
436 end_data->music, end_data->loop_music);
437 return true; // keep finale ownership (legacy has game assumptions)
438 }
439 else {
440 F_StartPostFinale();
441 return true; // keep finale ownership (legacy has game assumptions)
442 }
443 }
444 else
445 gameaction = ga_worlddone; // next level, e.g. MAP07
446 }
447
448 return true; // keep finale ownership
449 }
450
451 void dsda_DoomFDrawer(void) {
452 void F_TextWrite(void);
453 void F_BunnyScroll(void);
454
455 if (!finalestage || !end_data)
456 F_TextWrite();
457 else if (end_data->end == dmi_end_game_scroll)
458 F_BunnyScroll();
459 else {
460 // e6y: wide-res
461 V_ClearBorder();
462 V_DrawNamePatchFS(0, 0, 0, end_data->end_pic, CR_DEFAULT, VPT_STRETCH);
463 }
464 }
465
466 int dsda_DoomBossAction(mobj_t* mo) {
467 int i;
468
469 if (!current_map)
470 return false;
471
472 for (i = 0; i < current_map->num_special_actions; ++i)
473 if (current_map->special_actions[i].monster_type == mo->type)
474 break;
475
476 if (i == current_map->num_special_actions)
477 return true;
478
479 if (!P_CheckBossDeath(mo))
480 return true;
481
482 if (!map_format.zdoom)
483 I_Error("MAPINFO boss actions are incompatible with this map format (use UMAPINFO)");
484
485 for (i = 0; i < current_map->num_special_actions; ++i)
486 if (current_map->special_actions[i].monster_type == mo->type)
487 map_format.execute_line_special(
488 current_map->special_actions[i].action_special,
489 current_map->special_actions[i].special_args,
490 NULL, 0, mo
491 );
492
493 return true;
494 }
495
496 int dsda_DoomMapLumpName(const char** name, int episode, int map) {
497 const doom_mapinfo_map_t* target_map;
498
499 if (!doom_mapinfo.loaded)
500 return false;
501
502 target_map = dsda_DoomMapEntry(map);
503
504 if (!target_map)
505 return false;
506
507 *name = target_map->lump_name;
508
509 return true;
510 }
511
512 int dsda_DoomMapAuthor(const char** author) {
513 if (!current_map)
514 return false;
515
516 *author = current_map->author;
517
518 return true;
519 }
520
521 int dsda_DoomHUTitle(dsda_string_t* str) {
522 if (!current_map)
523 return false;
524
525 dsda_StringPrintF(str, "%s: %s", current_map->lump_name, current_map->nice_name);
526
527 return true;
528 }
529
530 int dsda_DoomSkyTexture(int* sky) {
531 if (!current_map || !current_map->sky1.lump)
532 return false;
533
534 *sky = R_TextureNumForName(current_map->sky1.lump);
535
536 return true;
537 }
538
539 int dsda_DoomPrepareInitNew(void) {
540 return false;
541 }
542
543 int dsda_DoomPrepareIntermission(int* result) {
544 const doom_mapinfo_map_t* map = NULL;
545 const doom_mapinfo_map_next_t* next = NULL;
546
547 if (!current_map)
548 return false;
549
550 if (leave_data.map > 0)
551 map = dsda_DoomMapEntry(leave_data.map);
552 else {
553 if (secretexit)
554 next = &current_map->secret_next;
555
556 if (!next || (!next->map && !next->end))
557 next = &current_map->next;
558
559 if (next->map)
560 map = dsda_DoomMapEntryByName(next->map);
561 }
562
563 if (current_map->par) {
564 wminfo.partime = current_map->par * TICRATE;
565 wminfo.modified_partime = true;
566 }
567
568 if (map) {
569 wminfo.next = map->level_num - 1;
570 wminfo.nextep = 0;
571 }
572 else {
573 wminfo.next = 0;
574 wminfo.nextep = 0;
575 }
576
577 // TODO: this should happen somewhere else
578 if (wminfo.nextep != wminfo.epsd) {
579 int i;
580
581 for (i = 0; i < g_maxplayers; i++)
582 players[i].didsecret = false;
583 }
584
585 wminfo.didsecret = players[consoleplayer].didsecret;
586
587 if (!map) {
588 end_data = next;
589
590 if (!end_data->end_pic && !end_data->end)
591 end_data = &default_end_data;
592 }
593 else
594 end_data = NULL;
595
596 *result = 0;
597
598 return true;
599 }
600
601 int dsda_DoomPrepareFinale(int* result) {
602 if (!current_map)
603 return false;
604
605 if (current_cluster != next_cluster)
606 if ((next_cluster && next_cluster->enter_text) ||
607 (current_cluster && current_cluster->exit_text)) {
608 *result = WD_START_FINALE;
609 return true;
610 }
611
612 if (end_data)
613 *result = WD_VICTORY;
614 else
615 *result = 0;
616
617 return true;
618 }
619
620 void dsda_DoomLoadMapInfo(void) {
621 int p;
622
623 if (dsda_Flag(dsda_arg_nomapinfo) || !dsda_UseMapinfo() || raven)
624 return;
625
626 p = -1;
627 while ((p = W_ListNumFromName("MAPINFO", p)) >= 0) {
628 const unsigned char* lump = (const unsigned char *) W_LumpByNum(p);
629 dsda_ParseDoomMapInfo(lump, W_LumpLength(p), I_Error);
630 }
631 }
632
633 int dsda_DoomExitPic(const char** exit_pic) {
634 if (!current_map || !current_map->exit_pic)
635 return false;
636
637 *exit_pic = current_map->exit_pic;
638
639 return true;
640 }
641
642 int dsda_DoomEnterPic(const char** enter_pic) {
643 if (!current_map || !current_map->enter_pic)
644 return false;
645
646 *enter_pic = current_map->enter_pic;
647
648 return true;
649 }
650
651 int dsda_DoomBorderTexture(const char** border_texture) {
652 if (!current_map || !current_map->border_texture)
653 return false;
654
655 *border_texture = current_map->border_texture;
656
657 return true;
658 }
659
660 int dsda_DoomPrepareEntering(void) {
661 extern const char *el_levelname;
662 extern const char *el_levelpic;
663 extern const char *el_author;
664
665 if (!next_map)
666 return false;
667
668 el_author = (next_map->flags & DMI_SHOW_AUTHOR) ? next_map->author : NULL;
669
670 if (next_map->title_patch) {
671 el_levelname = NULL;
672 el_levelpic = next_map->title_patch;
673 }
674 else {
675 el_levelname = next_map->nice_name;
676 el_levelpic = NULL;
677 }
678
679 return true;
680 }
681
682 int dsda_DoomPrepareFinished(void) {
683 extern const char *lf_levelname;
684 extern const char *lf_levelpic;
685 extern const char *lf_author;
686
687 if (!last_map)
688 return false;
689
690 lf_author = (last_map->flags & DMI_SHOW_AUTHOR) ? last_map->author : NULL;
691
692 if (last_map->title_patch) {
693 lf_levelname = NULL;
694 lf_levelpic = last_map->title_patch;
695 }
696 else {
697 lf_levelname = last_map->nice_name;
698 lf_levelpic = NULL;
699 }
700
701 return true;
702 }
703
704 int dsda_DoomMapLightning(int* lightning) {
705 return false;
706 }
707
708 int dsda_DoomApplyFadeTable(void) {
709 return false;
710 }
711
712 int dsda_DoomMapCluster(int* cluster, int map) {
713 if (!current_map)
714 return false;
715
716 *cluster = current_map->cluster;
717
718 return true;
719 }
720
721 int dsda_DoomSky1Texture(short* texture) {
722 return false;
723 }
724
725 int dsda_DoomSky2Texture(short* texture) {
726 return false;
727 }
728
729 int dsda_DoomGravity(fixed_t* gravity) {
730 if (!current_map || !current_map->gravity)
731 return false;
732
733 *gravity = dsda_StringToFixed(current_map->gravity) / 800;
734
735 return true;
736 }
737
738 int dsda_DoomAirControl(fixed_t* air_control) {
739 if (!current_map || !current_map->air_control)
740 return false;
741
742 *air_control = dsda_StringToFixed(current_map->air_control);
743
744 return true;
745 }
746
747 int dsda_DoomInitSky(void) {
748 return false;
749 }
750
751 int dsda_DoomMapFlags(map_info_flags_t* flags) {
752 if (!current_map)
753 return false;
754
755 *flags = current_map->flags;
756
757 return true;
758 }
759
760 int dsda_DoomMapColorMap(int* colormap) {
761 if (!current_map || !current_map->colormap)
762 return false;
763
764 *colormap = R_ColormapNumForName(current_map->colormap);
765
766 if (*colormap < 0)
767 *colormap = 0;
768
769 return true;
770 }