Debian Codegraph @

similar/main/titles.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Routines to display title screens...
23 *
24 */
25
26
27 #include "dxxsconf.h"
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #if DXX_USE_OGL
33 #include "ogl_init.h"
34 #endif
35 #include "pstypes.h"
36 #include "timer.h"
37 #include "key.h"
38 #include "gr.h"
39 #include "palette.h"
40 #include "iff.h"
41 #include "pcx.h"
42 #include "physfsx.h"
43 #include "u_mem.h"
44 #include "joy.h"
45 #include "titles.h"
46 #include "gamefont.h"
47 #include "gameseq.h"
48 #include "dxxerror.h"
49 #include "robot.h"
50 #include "textures.h"
51 #include "screens.h"
52 #include "multi.h"
53 #include "digi.h"
54 #include "text.h"
55 #include "piggy.h"
56 #include "songs.h"
57 #if DXX_BUILD_DESCENT == 2
58 #include "movie.h"
59 #include "physfsrwops.h"
60 #endif
61 #include "mission.h"
62 #include "mouse.h"
63 #include "console.h"
64 #include "args.h"
65 #include "strutil.h"
66
67 #include "partial_range.h"
68 #include <memory>
69
70 #if DXX_BUILD_DESCENT == 1
71 constexpr std::true_type EMULATING_D1{};
72 #endif
73
74 #if DXX_BUILD_DESCENT == 2
75 #define SHAREWARE_ENDING_FILENAME "ending.tex"
76 #endif
77 #define DEFAULT_BRIEFING_BKG "brief03.pcx"
78
79 namespace dcx {
80
81 namespace {
82
83 constexpr uint8_t DOOR_DIV_INIT = 6;
84 static std::array<color_t, 7> Briefing_text_colors;
85 static color_t *Current_color;
86 static color_t Erase_color;
87
88 // added by Jan Bobrowski for variable-size menu screen
89 static int rescale_x(const grs_bitmap &cv_bitmap, int x)
90 {
91 return x * cv_bitmap.bm_w / 320;
92 }
93
94 static int rescale_y(const grs_bitmap &cv_bitmap, int y)
95 {
96 return y * cv_bitmap.bm_h / 200;
97 }
98
99 static int get_message_num(const char *&message)
100 {
101 char *p;
102 auto num = strtoul(message, &p, 10);
103 while (*p && *p++ != '\n') // Get and drop eoln
104 ;
105 message = p;
106 return num;
107 }
108
109 enum class title_load_location : bool
110 {
111 anywhere,
112 from_hog_only,
113 };
114
115 struct title_screen : window
116 {
117 grs_main_bitmap title_bm;
118 fix64 timer;
119 title_screen(grs_canvas &canvas) :
120 window(canvas, 0, 0, canvas.cv_bitmap.bm_w, canvas.cv_bitmap.bm_h)
121 {
122 }
123 virtual window_event_result event_handler(const d_event &) override;
124 };
125
126 window_event_result title_screen::event_handler(const d_event &event)
127 {
128 window_event_result result;
129
130 switch (event.type)
131 {
132 case event_type::mouse_button_down:
133 if (event_mouse_get_button(event) != mbtn::left)
134 return window_event_result::ignored;
135 else
136 {
137 return window_event_result::close;
138 }
139 break;
140
141 case event_type::key_command:
142 if ((result = call_default_handler(event)) == window_event_result::ignored)
143 {
144 return window_event_result::close;
145 }
146 return result;
147
148 #if DXX_MAX_BUTTONS_PER_JOYSTICK
149 case event_type::joystick_button_down:
150 return window_event_result::close;
151 #endif
152
153 case event_type::idle:
154 timer_delay2(50);
155
156 if (timer_query() > timer)
157 {
158 return window_event_result::close;
159 }
160 break;
161
162 case event_type::window_draw:
163 show_fullscr(w_canv, title_bm);
164 break;
165
166 case event_type::window_close:
167 break;
168
169 default:
170 break;
171 }
172 return window_event_result::ignored;
173 }
174
175 static void show_title_screen(const char *filename)
176 {
177 char new_filename[PATH_MAX] = "";
178
179 auto ts = window_create<title_screen>(grd_curscreen->sc_canvas);
180
181 strcat(new_filename,filename);
182 filename = new_filename;
183
184 ts->timer = timer_query() + i2f(3);
185 const auto pcx_error = pcx_read_bitmap_or_default(filename, ts->title_bm, gr_palette);
186 if (pcx_error != pcx_result::SUCCESS)
187 {
188 con_printf(CON_URGENT, "%s:%u: error: loading briefing screen <%s> failed: PCX load error: %s (%u)", __FILE__, __LINE__, filename, pcx_errormsg(pcx_error), static_cast<unsigned>(pcx_error));
189 }
190
191 gr_palette_load( gr_palette );
192 event_process_all();
193 }
194
195 static void show_title_screen(const char *const filename, title_load_location /* from_hog_only */)
196 {
197 show_title_screen(filename);
198 }
199
200 static void show_first_found_title_screen(const char *oem, const char *share, const char *macshare)
201 {
202 const char *filename = oem;
203 if ((PHYSFS_exists(filename)) ||
204 (filename = share, PHYSFS_exists(filename)) ||
205 (filename = macshare, PHYSFS_exists(filename))
206 )
207 show_title_screen(filename, title_load_location::from_hog_only);
208 }
209
210 }
211
212 }
213
214 namespace dsx {
215 #if DXX_BUILD_DESCENT == 2
216 int intro_played;
217 namespace {
218 static int DefineBriefingBox(const grs_bitmap &, const char *&buf);
219 }
220 #endif
221
222 void show_titles(void)
223 {
224 #if DXX_BUILD_DESCENT == 1
225 songs_play_song(song_number::title, 1);
226 #endif
227 if (CGameArg.SysNoTitles)
228 return;
229 #if DXX_BUILD_DESCENT == 1
230
231 show_first_found_title_screen(
232 "macplay.pcx", // Mac Shareware
233 "mplaycd.pcx", // Mac Registered
234 "iplogo1.pcx" // PC. Only down here because it's lowres ;-)
235 );
236 const bool resolution_at_least_640_480 = (SWIDTH >= 640 && SHEIGHT >= 480);
237 auto &logo_hires_pcx = "logoh.pcx";
238 auto &descent_hires_pcx = "descenth.pcx";
239 show_title_screen((resolution_at_least_640_480 && PHYSFS_exists(logo_hires_pcx)) ? logo_hires_pcx : "logo.pcx", title_load_location::from_hog_only);
240 show_title_screen((resolution_at_least_640_480 && PHYSFS_exists(descent_hires_pcx)) ? descent_hires_pcx : "descent.pcx", title_load_location::from_hog_only);
241 #elif DXX_BUILD_DESCENT == 2
242 int song_playing{0};
243
244 #define MOVIE_REQUIRED 1 //(!is_D2_OEM && !is_SHAREWARE && !is_MAC_SHARE) // causes segfault
245
246 const auto hiresmode{HIRESMODE};
247 { //show bundler screens
248 const auto played = PlayMovie({}, "pre_i.mve", play_movie_warn_missing::verbose);
249 if (played == movie_play_status::skipped)
250 {
251 char filename[12];
252 strcpy(filename, hiresmode ? "pre_i1b.pcx" : "pre_i1.pcx");
253
254 while (PHYSFS_exists(filename))
255 {
256 show_title_screen(filename, title_load_location::anywhere);
257 filename[5]++;
258 }
259 }
260 }
261
262 auto played = PlayMovie("intro.tex", "intro.mve", play_movie_warn_missing::urgent);
263
264 if (played != movie_play_status::skipped)
265 intro_played = 1;
266 else
267 { //didn't get intro movie, try titles
268
269 played = PlayMovie({}, "titles.mve", play_movie_warn_missing::urgent);
270
271 if (played == movie_play_status::skipped)
272 {
273 con_puts(CON_DEBUG, "Playing title song...");
274 songs_play_song(song_number::title, 1);
275 song_playing = 1;
276 con_puts(CON_DEBUG, "Showing logo screens...");
277
278 show_first_found_title_screen(
279 hiresmode ? "iplogo1b.pcx" : "iplogo1.pcx", // OEM
280 "iplogo1.pcx", // SHAREWARE
281 "mplogo.pcx" // MAC SHAREWARE
282 );
283 show_first_found_title_screen(
284 hiresmode ? "logob.pcx" : "logo.pcx", // OEM
285 "logo.pcx", // SHAREWARE
286 "plogo.pcx" // MAC SHAREWARE
287 );
288 }
289 }
290
291 { //show bundler movie or screens
292 auto played = movie_play_status::skipped; //default is not played
293
294 //check if OEM movie exists, so we don't stop the music if it doesn't
295 if (RAIIPHYSFS_File{PHYSFS_openRead("oem.mve")})
296 {
297 played = PlayMovie({}, "oem.mve", play_movie_warn_missing::verbose);
298 song_playing = 0; //movie will kill sound
299 }
300
301 if (played == movie_play_status::skipped)
302 {
303 char filename[12];
304 strcpy(filename, hiresmode ? "oem1b.pcx" : "oem1.pcx");
305 while (PHYSFS_exists(filename))
306 {
307 show_title_screen(filename, title_load_location::anywhere);
308 filename[3]++;
309 }
310 }
311 }
312
313 if (!song_playing)
314 {
315 con_puts(CON_DEBUG, "Playing title song...");
316 songs_play_song(song_number::title, 1);
317 }
318 con_puts(CON_DEBUG, "Showing logo screen...");
319 const auto filename = hiresmode ? "descentb.pcx" : "descent.pcx";
320 if (PHYSFS_exists(filename))
321 show_title_screen(filename, title_load_location::from_hog_only);
322 #endif
323 }
324
325 }
326
327 namespace dcx {
328 namespace {
329
330 //-----------------------------------------------------------------------------
331 struct briefing_screen {
332 char bs_name[13]; // filename, eg merc01. Assumes .lbm suffix.
333 sbyte level_num;
334 sbyte message_num;
335 short text_ulx, text_uly; // upper left x,y of text window
336 short text_width, text_height; // width and height of text window
337 };
338
339 #define ENDING_LEVEL_NUM_OEMSHARE 0x7f
340 #define ENDING_LEVEL_NUM_REGISTER 0x7e
341
342 static grs_subcanvas_ptr create_spinning_robot_sub_canvas(grs_canvas &canvas)
343 {
344 return gr_create_sub_canvas(canvas, rescale_x(canvas.cv_bitmap, 138), rescale_y(canvas.cv_bitmap, 55), rescale_x(canvas.cv_bitmap, 166), rescale_y(canvas.cv_bitmap, 138));
345 }
346
347 static std::array<char, 32> get_message_name(const char *&message, const char *const trailer)
348 {
349 auto p = message;
350 for (; *p == ' '; ++p)
351 {
352 }
353 std::array<char, 32> result{};
354 const auto e = std::prev(result.end(), sizeof(".bbm"));
355 auto i = result.begin();
356 char c;
357 for (; (c = *p) && c != ' ' && c != '\n'; ++p)
358 {
359 *i++ = c;
360 if (i == e)
361 /* Avoid buffer overflow; this was not present in the
362 * original code.
363 */
364 break;
365 }
366 /* This is inconsistent. If the copy loop terminated on a newline,
367 * then `p` points to the newline and the next loop is skipped. If
368 * the copy loop terminated on a null, the next loop is attempted,
369 * but exits immediately. In both those cases, `p` is unchanged.
370 * If the copy loop terminated on any other character, the next loop
371 * will advance `p` to point to a null (consistent with the copy
372 * loop terminating on a null) or past the newline (inconsistent
373 * with the copy loop).
374 *
375 * This inconsistency was present in the prior version of the code,
376 * and is retained in case there exist briefings which rely on this
377 * inconsistency.
378 */
379 if (c != '\n')
380 while ((c = *p) && (++p, c) != '\n') // Get and drop eoln
381 {
382 }
383 message = p;
384 strcpy(i, trailer);
385 return result;
386 }
387 }
388 }
389
390 namespace dsx {
391 namespace {
392
393 #if DXX_BUILD_DESCENT == 2
394
395 static std::array<briefing_screen, 60> Briefing_screens{{
396 {"brief03.pcx",0,3,8,8,257,177}
397 }}; // default=0!!!
398 #endif
399
400 constexpr briefing_screen D1_Briefing_screens_full[] = {
401 { "brief01.pcx", 0, 1, 13, 140, 290, 59 },
402 { "brief02.pcx", 0, 2, 27, 34, 257, 177 },
403 { "brief03.pcx", 0, 3, 20, 22, 257, 177 },
404 { "brief02.pcx", 0, 4, 27, 34, 257, 177 },
405 { "moon01.pcx", 1, 5, 10, 10, 300, 170 }, // level 1
406 { "moon01.pcx", 2, 6, 10, 10, 300, 170 }, // level 2
407 { "moon01.pcx", 3, 7, 10, 10, 300, 170 }, // level 3
408 { "venus01.pcx", 4, 8, 15, 15, 300, 200 }, // level 4
409 { "venus01.pcx", 5, 9, 15, 15, 300, 200 }, // level 5
410 { "brief03.pcx", 6, 10, 20, 22, 257, 177 },
411 { "merc01.pcx", 6, 11, 10, 15, 300, 200 }, // level 6
412 { "merc01.pcx", 7, 12, 10, 15, 300, 200 }, // level 7
413 { "brief03.pcx", 8, 13, 20, 22, 257, 177 },
414 { "mars01.pcx", 8, 14, 10, 100, 300, 200 }, // level 8
415 { "mars01.pcx", 9, 15, 10, 100, 300, 200 }, // level 9
416 { "brief03.pcx", 10, 16, 20, 22, 257, 177 },
417 { "mars01.pcx", 10, 17, 10, 100, 300, 200 }, // level 10
418 { "jup01.pcx", 11, 18, 10, 40, 300, 200 }, // level 11
419 { "jup01.pcx", 12, 19, 10, 40, 300, 200 }, // level 12
420 { "brief03.pcx", 13, 20, 20, 22, 257, 177 },
421 { "jup01.pcx", 13, 21, 10, 40, 300, 200 }, // level 13
422 { "jup01.pcx", 14, 22, 10, 40, 300, 200 }, // level 14
423 { "saturn01.pcx", 15, 23, 10, 40, 300, 200 }, // level 15
424 { "brief03.pcx", 16, 24, 20, 22, 257, 177 },
425 { "saturn01.pcx", 16, 25, 10, 40, 300, 200 }, // level 16
426 { "brief03.pcx", 17, 26, 20, 22, 257, 177 },
427 { "saturn01.pcx", 17, 27, 10, 40, 300, 200 }, // level 17
428 { "uranus01.pcx", 18, 28, 100, 100, 300, 200 }, // level 18
429 { "uranus01.pcx", 19, 29, 100, 100, 300, 200 }, // level 19
430 { "uranus01.pcx", 20, 30, 100, 100, 300, 200 }, // level 20
431 { "uranus01.pcx", 21, 31, 100, 100, 300, 200 }, // level 21
432 { "neptun01.pcx", 22, 32, 10, 20, 300, 200 }, // level 22
433 { "neptun01.pcx", 23, 33, 10, 20, 300, 200 }, // level 23
434 { "neptun01.pcx", 24, 34, 10, 20, 300, 200 }, // level 24
435 { "pluto01.pcx", 25, 35, 10, 20, 300, 200 }, // level 25
436 { "pluto01.pcx", 26, 36, 10, 20, 300, 200 }, // level 26
437 { "pluto01.pcx", 27, 37, 10, 20, 300, 200 }, // level 27
438 { "aster01.pcx", -1, 38, 10, 90, 300, 200 }, // secret level -1
439 { "aster01.pcx", -2, 39, 10, 90, 300, 200 }, // secret level -2
440 { "aster01.pcx", -3, 40, 10, 90, 300, 200 }, // secret level -3
441 { "end01.pcx", ENDING_LEVEL_NUM_OEMSHARE, 1, 23, 40, 320, 200 }, // OEM and shareware end
442 { "end02.pcx", ENDING_LEVEL_NUM_REGISTER, 1, 5, 5, 300, 200 }, // registered end
443 { "end01.pcx", ENDING_LEVEL_NUM_REGISTER, 2, 23, 40, 320, 200 }, // registered end
444 { "end03.pcx", ENDING_LEVEL_NUM_REGISTER, 3, 5, 5, 300, 200 }, // registered end
445 };
446
447 constexpr briefing_screen D1_Briefing_screens_share[] = {
448 { "brief01.pcx", 0, 1, 13, 140, 290, 59 },
449 { "brief02.pcx", 0, 2, 27, 34, 257, 177 },
450 { "brief03.pcx", 0, 3, 20, 22, 257, 177 },
451 { "brief02.pcx", 0, 4, 27, 34, 257, 177 },
452 { "moon01.pcx", 1, 5, 10, 10, 300, 170 }, // level 1
453 { "moon01.pcx", 2, 6, 10, 10, 300, 170 }, // level 2
454 { "moon01.pcx", 3, 7, 10, 10, 300, 170 }, // level 3
455 { "venus01.pcx", 4, 8, 15, 15, 300, 200 }, // level 4
456 { "venus01.pcx", 5, 9, 15, 15, 300, 200 }, // level 5
457 { "brief03.pcx", 6, 10, 20, 22, 257, 177 },
458 { "merc01.pcx", 6, 10, 10, 15, 300, 200 }, // level 6
459 { "merc01.pcx", 7, 11, 10, 15, 300, 200 }, // level 7
460 { "end01.pcx", ENDING_LEVEL_NUM_OEMSHARE, 1, 23, 40, 320, 200 }, // shareware end
461 };
462
463 struct msgstream
464 {
465 int x;
466 int y;
467 color_t color;
468 std::array<char, 2> ch;
469 };
470
471 constexpr const briefing_screen *get_d1_briefing_screens(const descent_hog_size size)
472 {
473 if (size == descent_hog_size::pc_shareware_v14 || size == descent_hog_size::pc_shareware_v10)
474 return D1_Briefing_screens_share;
475 return D1_Briefing_screens_full;
476 }
477
478 #if DXX_BUILD_DESCENT == 1
479 using briefing_screen_deleter = std::default_delete<briefing_screen>;
480 #elif DXX_BUILD_DESCENT == 2
481 class briefing_screen_deleter : std::default_delete<briefing_screen>
482 {
483 typedef std::default_delete<briefing_screen> base_deleter;
484 public:
485 briefing_screen_deleter() = default;
486 briefing_screen_deleter(base_deleter &&b) : base_deleter(std::move(b)) {}
487 void operator()(briefing_screen *const p) const
488 {
489 if (p >= &Briefing_screens.front() && p <= &Briefing_screens.back())
490 return;
491 this->base_deleter::operator()(p);
492 }
493 };
494 #endif
495
496 struct briefing : window
497 {
498 enum class animating_bitmap_type : bool
499 {
500 loop,
501 reset,
502 };
503 enum class door_direction : int8_t
504 {
505 backward = -1,
506 forward = 1,
507 };
508 briefing(grs_canvas &src) :
509 window(src, 0, 0, src.cv_bitmap.bm_w, src.cv_bitmap.bm_h)
510 {
511 }
512 virtual window_event_result event_handler(const d_event &) override;
513 unsigned streamcount;
514 short level_num;
515 short cur_screen;
516 std::unique_ptr<briefing_screen, briefing_screen_deleter> screen;
517 grs_main_bitmap background;
518 animating_bitmap_type animating_bitmap;
519 uint8_t flashing_cursor;
520 uint8_t new_screen;
521 uint8_t new_page;
522 door_direction door_dir;
523 uint8_t door_div_count;
524 int8_t prev_ch;
525 #if DXX_BUILD_DESCENT == 2
526 uint8_t got_z;
527 uint8_t dumb_adjust;
528 uint8_t chattering;
529 uint8_t line_adjustment;
530 RAIIdigi_sound hum_channel, printing_channel;
531 MVESTREAM_ptr_t pMovie;
532 RWops_ptr RoboFile;
533 #endif
534 std::unique_ptr<char[]> text;
535 const char *message;
536 int text_x, text_y;
537 short tab_stop;
538 fix64 start_time;
539 fix64 delay_count;
540 int robot_num;
541 grs_subcanvas_ptr robot_canv;
542 vms_angvec robot_angles;
543 std::array<char, 32> bitmap_name;
544 grs_main_bitmap guy_bitmap;
545 std::array<char, 16> background_name;
546 std::array<msgstream, 2048> messagestream;
547 };
548
549 static void briefing_init(briefing *br, short level_num)
550 {
551 br->level_num = level_num;
552 if (EMULATING_D1 && (br->level_num == 1))
553 br->level_num = 0; // for start of game stuff
554
555 br->cur_screen = 0;
556 br->background_name.back() = 0;
557 strncpy(br->background_name.data(), DEFAULT_BRIEFING_BKG, br->background_name.size() - 1);
558 #if DXX_BUILD_DESCENT == 2
559 br->RoboFile = {};
560 #endif
561 br->robot_num = 0;
562 br->robot_angles = {};
563 br->bitmap_name[0] = '\0';
564 br->door_dir = briefing::door_direction::forward;
565 br->door_div_count = 0;
566 br->animating_bitmap = briefing::animating_bitmap_type::loop;
567 }
568
569 //-----------------------------------------------------------------------------
570 // Load Descent briefing text.
571 static int load_screen_text(const d_fname &filename, std::unique_ptr<char[]> &buf)
572 {
573 int have_binary{0};
574 auto e = end(filename);
575 auto ext = std::find(begin(filename), e, '.');
576 if (ext == e)
577 return (0);
578 if (!d_stricmp(&*ext, ".txb"))
579 have_binary = 1;
580
581 auto tfile = PHYSFSX_openReadBuffered(filename).first;
582 if (!tfile)
583 return (0);
584
585 const auto len{PHYSFS_fileLength(tfile)};
586 buf = std::make_unique<char[]>(len + 1);
587 PHYSFSX_readBytes(tfile, buf.get(), len);
588 #if DXX_BUILD_DESCENT == 1
589 const auto endbuf = &buf[len];
590 #elif DXX_BUILD_DESCENT == 2
591 const auto endbuf = std::remove(&buf[0], &buf[len], 13);
592 #endif
593 *endbuf = 0;
594
595 if (have_binary)
596 decode_text(std::span(buf.get(), len));
597
598 return (1);
599 }
600
601 #if DXX_BUILD_DESCENT == 2
602 static void set_briefing_fontcolor(struct briefing &br);
603 static int get_new_message_num(const char *&message)
604 {
605 char *p;
606 const auto num = strtoul(message, &p, 10);
607 message = p + 1;
608 return num;
609 }
610 #endif
611
612 // Return a pointer to the start of text for screen #screen_num.
613 static const char * get_briefing_message(const briefing *br, int screen_num)
614 {
615 const char *tptr = br->text.get();
616 int cur_screen{0};
617 int ch;
618
619 Assert(screen_num >= 0);
620
621 while ( (*tptr != 0 ) && (screen_num != cur_screen)) {
622 ch = *tptr++;
623 if (ch == '$') {
624 ch = *tptr++;
625 if (ch == 'S')
626 cur_screen = get_message_num(tptr);
627 }
628 }
629
630 if (screen_num!=cur_screen)
631 return (NULL);
632
633 return tptr;
634 }
635
636 static void init_char_pos(briefing *br, int x, int y)
637 {
638 br->text_x = x;
639 br->text_y = y;
640 }
641
642 // Make sure the text stays on the screen
643 // Return 1 if new page required
644 // 0 otherwise
645 static int check_text_pos(briefing *br)
646 {
647 if (br->text_x > br->screen->text_ulx + br->screen->text_width)
648 {
649 br->text_x = br->screen->text_ulx;
650 br->text_y += br->screen->text_uly;
651 }
652
653 if (br->text_y > br->screen->text_uly + br->screen->text_height)
654 {
655 br->new_page = 1;
656 return 1;
657 }
658
659 return 0;
660 }
661
662 static void put_char_delay(const grs_font &cv_font, briefing *const br, const char ch)
663 {
664 if (br->delay_count && (timer_query() < br->start_time + br->delay_count))
665 {
666 br->message--; // Go back to same character
667 return;
668 }
669
670 if (br->streamcount >= br->messagestream.size())
671 return;
672 auto &m = br->messagestream[br->streamcount++];
673 m.x = br->text_x;
674 m.y = br->text_y;
675 m.color = *Current_color;
676 m.ch[0] = ch;
677 m.ch[1] = 0;
678
679 br->prev_ch = ch;
680 const auto w = gr_get_string_size(cv_font, m.ch.data()).width;
681 br->text_x += w;
682
683 #if DXX_BUILD_DESCENT == 2
684 if (!EMULATING_D1 && !br->chattering) {
685 br->printing_channel.reset(digi_start_sound(digi_xlat_sound(sound_effect::SOUND_BRIEFING_PRINTING), F1_0, sound_pan{0x7fff}, 1, -1, -1, sound_object_none));
686 br->chattering=1;
687 }
688 #endif
689
690 br->start_time = timer_query();
691 }
692
693 static void init_spinning_robot(grs_canvas &canvas, briefing &br);
694 [[nodiscard]]
695 static int load_briefing_screen(grs_canvas &, briefing *br, const char *fname);
696
697 // Process a character for the briefing,
698 // including special characters preceded by a '$'.
699 // Return 1 when page is finished, 0 otherwise
700 static int briefing_process_char(grs_canvas &canvas, briefing *const br)
701 {
702 auto &game_font = *GAME_FONT;
703 char ch = *br->message++;
704 if (ch == '$') {
705 ch = *br->message++;
706 #if DXX_BUILD_DESCENT == 2
707 if (ch=='D') {
708 br->cur_screen = DefineBriefingBox(canvas.cv_bitmap, br->message);
709 br->screen.reset(&Briefing_screens[br->cur_screen]);
710 init_char_pos(br, br->screen->text_ulx, br->screen->text_uly);
711 br->line_adjustment=0;
712 br->prev_ch = 10; // read to eoln
713 } else if (ch=='U') {
714 br->cur_screen = get_message_num(br->message);
715 br->screen.reset(&Briefing_screens[br->cur_screen]);
716 init_char_pos(br, br->screen->text_ulx, br->screen->text_uly);
717 br->prev_ch = 10; // read to eoln
718 } else
719 #endif
720 if (ch == 'C') {
721 auto cc = get_message_num(br->message) - 1;
722 if (cc < 0)
723 cc = 0;
724 else if (cc > Briefing_text_colors.size() - 1)
725 cc = Briefing_text_colors.size() - 1;
726 Current_color = &Briefing_text_colors[cc];
727 br->prev_ch = 10;
728 } else if (ch == 'F') { // toggle flashing cursor
729 br->flashing_cursor = !br->flashing_cursor;
730 br->prev_ch = 10;
731 while (*br->message++ != 10)
732 ;
733 } else if (ch == 'T') {
734 br->tab_stop = get_message_num(br->message);
735 br->prev_ch = 10; // read to eoln
736 } else if (ch == 'R') {
737 br->robot_canv.reset();
738 #if DXX_BUILD_DESCENT == 2
739 if (auto &RoboFile = br->RoboFile) {
740 RoboFile.reset();
741 DeInitRobotMovie(br->pMovie);
742 }
743 #endif
744
745 if (EMULATING_D1) {
746 init_spinning_robot(canvas, *br);
747 br->robot_num = get_message_num(br->message);
748 #if DXX_BUILD_DESCENT == 2
749 while (*br->message++ != 10)
750 ;
751 #endif
752 } else {
753 #if DXX_BUILD_DESCENT == 2
754 std::array<char, 8> robot_name_storage{{"RBA.MVE"}}; // matt don't change this!
755 const char *spinRobotName = robot_name_storage.data();
756 const char robot_movie_selector_character = *br->message++;
757
758 robot_name_storage[2] = robot_movie_selector_character; // ugly but proud
759 if (robot_movie_selector_character == '9')
760 {
761 /* Descent 2: Vertigo level 1 `Deep Kraeg Tunnel System`
762 * references `RB9.MVE` for the robot `Fervid`. However,
763 * there is no such MVE in the movie file.
764 * The high-resolution movie file contains `RB9.mve`.
765 * The low-resolution movie file contains `rb9.mve`.
766 */
767 if (const auto m = Current_mission->extra_robot_movie.get())
768 {
769 if (m->resolution == movie_resolution::high)
770 spinRobotName = "RB9.mve";
771 else
772 spinRobotName = "rb9.mve";
773 }
774 }
775 else if (robot_movie_selector_character == '$')
776 {
777 /* Descent 2: Vertigo level 16 `Fold Zandura` references
778 * `RB$.MVE` for the robot `SPIKE`. However, that MVE is
779 * only present in the high-resolution movie file.
780 * The low-resolution movie file contains `RB$.mve`.
781 */
782 if (const auto m = Current_mission->extra_robot_movie.get())
783 {
784 if (m->resolution == movie_resolution::high)
785 {
786 /* High-resolution movie file contains the correct
787 * capitalization, so no change is needed.
788 */
789 }
790 else
791 spinRobotName = "RB$.mve";
792 }
793 }
794
795 if (InitRobotMovie(spinRobotName, br->pMovie))
796 {
797 RotateRobot(br->pMovie.get());
798 set_briefing_fontcolor(*br);
799 }
800 #endif
801 }
802 br->prev_ch = 10; // read to eoln
803 }
804 else if ((ch == 'N' && (br->animating_bitmap = briefing::animating_bitmap_type::loop, true)) ||
805 (ch == 'O' && (br->animating_bitmap = briefing::animating_bitmap_type::reset, true)))
806 {
807 br->robot_canv.reset();
808 br->prev_ch = 10;
809 br->bitmap_name = get_message_name(br->message, "#0");
810 } else if (ch=='A') {
811 #if DXX_BUILD_DESCENT == 2
812 br->line_adjustment=1-br->line_adjustment;
813 #endif
814 } else if (ch=='Z') {
815 #if DXX_BUILD_DESCENT == 2
816 std::array<char, 15> fname, fnameb;
817 br->got_z=1;
818 br->dumb_adjust=1;
819 auto p = br->message;
820 const auto begin_filename{p};
821 const char *separator = nullptr;
822 for (char c; (c = *p) != 0 && c != '\n'; ++p)
823 {
824 if (c == '.' && !separator)
825 separator = p;
826 }
827 const auto end_filename{p};
828 /* This is inconsistent, but preserves the original game
829 * logic.
830 */
831 if (*p != 10)
832 while (*p++ != 10) // Get and drop eoln
833 ;
834 br->message = p;
835 /* In the original game, if a separator was not found, the
836 * game would have undefined behavior. Define that case to
837 * mean an early return.
838 */
839 if (!separator)
840 return 1;
841 const std::size_t idx_end_filename = std::distance(begin_filename, end_filename);
842 if (idx_end_filename >= fname.size() - 1)
843 return 1;
844 const std::size_t idx_separator = std::distance(begin_filename, separator);
845 if (idx_separator >= fnameb.size() - 6)
846 return 1;
847 fname[idx_end_filename] = 0;
848 std::copy(begin_filename, end_filename, std::begin(fname));
849 std::copy_n(begin_filename, idx_separator, std::begin(fnameb));
850 std::copy_n("b.pcx", 6, std::next(std::begin(fnameb), idx_separator));
851 auto &use_fname = ((HIRESMODE && PHYSFSX_exists_ignorecase(fnameb.data())) || !PHYSFSX_exists_ignorecase(fname.data()))
852 ? fnameb
853 : fname;
854 if (!load_briefing_screen(canvas, br, use_fname.data()))
855 return 1;
856 #endif
857 } else if (ch == 'B') {
858 palette_array_t temp_palette;
859 const auto bitmap_name = get_message_name(br->message, ".bbm");
860 br->robot_canv.reset();
861 br->guy_bitmap.reset();
862 const auto iff_error{iff_read_bitmap(&bitmap_name[0], br->guy_bitmap, &temp_palette)};
863 assert(iff_error == iff_status_code::no_error);
864 #if DXX_BUILD_DESCENT == 2
865 gr_remap_bitmap_good( br->guy_bitmap, temp_palette, -1, -1 );
866 #endif
867 (void)iff_error;
868
869 br->prev_ch = 10;
870 } else if (ch == 'S') {
871 #if DXX_BUILD_DESCENT == 2
872 br->chattering = 0;
873 br->printing_channel.reset();
874 #endif
875
876 br->new_screen = 1;
877 return 1;
878 } else if (ch == 'P') { // New page.
879 #if DXX_BUILD_DESCENT == 2
880 if (!br->got_z) {
881 Int3(); // Hey ryan!!!! You gotta load a screen before you start
882 // printing to it! You know, $Z !!!
883 if (!load_briefing_screen(canvas, br, HIRESMODE ? "end01b.pcx" : "end01.pcx"))
884 return 1;
885 }
886
887 br->chattering = 0;
888 br->printing_channel.reset();
889 #endif
890
891 br->new_page = 1;
892
893 while (*br->message != 10) {
894 br->message++; // drop carriage return after special escape sequence
895 }
896 br->message++;
897 br->prev_ch = 10;
898
899 return 1;
900 }
901 #if DXX_BUILD_DESCENT == 2
902 else if (ch == ':') {
903 br->prev_ch = 10;
904 auto p = br->message;
905 /* Legacy clients do not understand $: and will instead show
906 * the remainder of the line. However, if the next two
907 * characters after $: are $F, legacy clients will treat
908 * the $F as above, toggle the flashing_cursor flag, then
909 * discard the rest of the line. This special case allows
910 * briefing authors to hide the directive from legacy
911 * clients, but get the same state of flashing_cursor for
912 * both legacy and aware clients. Briefing authors will
913 * likely need an additional $F nearby to balance this
914 * toggle, but no additional code here is needed to support
915 * that.
916 *
917 * The trailing colon is cosmetic, so that the compatibility
918 * $F is not directly adjacent to the directive.
919 */
920 if (!strncmp(p, "$F:", 3))
921 {
922 br->flashing_cursor = !br->flashing_cursor;
923 p += 3;
924 }
925 auto &rotate_robot_label = "Rebirth.rotate.robot ";
926 constexpr auto rotate_robot_len = sizeof(rotate_robot_label) - 1;
927 if (!strncmp(p, rotate_robot_label, rotate_robot_len))
928 {
929 char *p2;
930 const auto id = strtoul(p + rotate_robot_len, &p2, 10);
931 if (*p2 == '\n')
932 {
933 p = p2;
934 br->robot_canv.reset();
935 if (auto &RoboFile = br->RoboFile)
936 {
937 RoboFile.reset();
938 DeInitRobotMovie(br->pMovie);
939 }
940 init_spinning_robot(canvas, *br);
941 /* This modifies the appearance of the frame, which
942 * is unfortunate. However, without it, all robots
943 * come out blue shifted.
944 */
945 gr_use_palette_table("groupa.256");
946 br->robot_num = id;
947 }
948 }
949 else
950 {
951 const char *p2 = p;
952 /* Suppress non-printing characters. No need to support
953 * encodings.
954 */
955 for (char c; (c = *p2) >= ' ' && c <= '~'; ++p2)
956 {
957 }
958 con_printf(CON_VERBOSE, "warning: unknown briefing directive \"%.*s\"", DXX_ptrdiff_cast_int(p2 - p), p);
959 }
960 for (char c; (c = *p) && (++p, c) != '\n';)
961 {
962 /* Discard through newline. On break, *p is '\0' or
963 * p[-1] is '\n'.
964 */
965 }
966 br->message = p;
967 }
968 #endif
969 else if (ch == '$' || ch == ';') // Print a $/;
970 put_char_delay(game_font, br, ch);
971 } else if (ch == '\t') { // Tab
972 const auto &&fspacx = FSPACX();
973 if (br->text_x - br->screen->text_ulx < fspacx(br->tab_stop))
974 br->text_x = br->screen->text_ulx + fspacx(br->tab_stop);
975 } else if ((ch == ';') && (br->prev_ch == 10)) {
976 while (*br->message++ != 10)
977 ;
978 br->prev_ch = 10;
979 } else if (ch == '\\') {
980 br->prev_ch = ch;
981 } else if (ch == 10) {
982 if (br->prev_ch != '\\') {
983 br->prev_ch = ch;
984 #if DXX_BUILD_DESCENT == 2
985 if (br->dumb_adjust)
986 br->dumb_adjust--;
987 else
988 #endif
989 br->text_y += FSPACY(5)+FSPACY(5)*3/5;
990 br->text_x = br->screen->text_ulx;
991 if (br->text_y > br->screen->text_uly + br->screen->text_height) {
992 #if DXX_BUILD_DESCENT == 1
993 const descent_hog_size size{PHYSFSX_fsize(descent_hog_basename)};
994 auto &bs = get_d1_briefing_screens(size)[br->cur_screen];
995 #elif DXX_BUILD_DESCENT == 2
996 auto &bs = Briefing_screens[br->cur_screen];
997 #endif
998 if (!load_briefing_screen(canvas, br, bs.bs_name))
999 return 1;
1000 br->text_x = br->screen->text_ulx;
1001 br->text_y = br->screen->text_uly;
1002 }
1003 } else {
1004 if (ch == 13) //Can this happen? Above says ch==10
1005 Int3();
1006 br->prev_ch = ch;
1007 }
1008 } else {
1009 #if DXX_BUILD_DESCENT == 2
1010 if (!br->got_z) {
1011 LevelError("briefing wrote to screen without using $Z to load a screen; loading default.");
1012 //Int3(); // Hey ryan!!!! You gotta load a screen before you start
1013 // printing to it! You know, $Z !!!
1014 if (!load_briefing_screen(canvas, br, HIRESMODE ? "end01b.pcx" : "end01.pcx"))
1015 return 1;
1016 }
1017 #endif
1018 put_char_delay(game_font, br, ch);
1019 }
1020
1021 return 0;
1022 }
1023
1024 #if DXX_BUILD_DESCENT == 1
1025 static void set_briefing_fontcolor()
1026 #elif DXX_BUILD_DESCENT == 2
1027 static void set_briefing_fontcolor(briefing &br)
1028 #endif
1029 {
1030 struct rgb
1031 {
1032 int r, g, b;
1033 };
1034 std::array<rgb, 3> colors;
1035 if (EMULATING_D1) {
1036 //green
1037 colors[0] = {0, 54, 0};
1038 //white
1039 colors[1] = {42, 38, 32};
1040 //Begin D1X addition
1041 //red
1042 colors[2] = {63, 0, 0};
1043 }
1044 else
1045 {
1046 colors[0] = {0, 40, 0};
1047 colors[1] = {40, 33, 35};
1048 colors[2] = {8, 31, 54};
1049 }
1050
1051 #if DXX_BUILD_DESCENT == 2
1052 if (br.RoboFile)
1053 {
1054 colors[0] = {0, 31, 0};
1055 }
1056 #endif
1057 Briefing_text_colors[0] = gr_find_closest_color_current(colors[0].r, colors[0].g, colors[0].b);
1058 Briefing_text_colors[1] = gr_find_closest_color_current(colors[1].r, colors[1].g, colors[1].b);
1059 Briefing_text_colors[2] = gr_find_closest_color_current(colors[2].r, colors[2].g, colors[2].b);
1060
1061 //blue
1062 Briefing_text_colors[3] = gr_find_closest_color_current( 0, 0, 54);
1063 //gray
1064 Briefing_text_colors[4] = gr_find_closest_color_current( 14, 14, 14);
1065 //yellow
1066 Briefing_text_colors[5] = gr_find_closest_color_current( 54, 54, 0);
1067 //purple
1068 Briefing_text_colors[6] = gr_find_closest_color_current( 0, 54, 54);
1069 //End D1X addition
1070
1071 Erase_color = gr_find_closest_color_current(0, 0, 0);
1072 }
1073 }
1074
1075 }
1076
1077 namespace {
1078
1079 static int redraw_messagestream(grs_canvas &canvas, const grs_font &cv_font, const msgstream &stream, const int lastcolor)
1080 {
1081 const auto nextcolor{stream.color};
1082 if (lastcolor != nextcolor)
1083 gr_set_fontcolor(canvas, nextcolor, -1);
1084 gr_string(canvas, cv_font, stream.x + 1, stream.y, stream.ch.data());
1085 return nextcolor;
1086 }
1087
1088 }
1089
1090 namespace dsx {
1091 namespace {
1092 static void flash_cursor(grs_canvas &canvas, const grs_font &cv_font, briefing *const br, const int cursor_flag)
1093 {
1094 if (cursor_flag == 0)
1095 return;
1096 gr_set_fontcolor(canvas, (timer_query() % (F1_0 / 2)) > F1_0 / 4 ? *Current_color : Erase_color, -1);
1097 gr_string(canvas, cv_font, br->text_x, br->text_y, "_");
1098 }
1099
1100 #define EXIT_DOOR_MAX 14
1101 #define OTHER_THING_MAX 10 // Adam: This is the number of frames in your new animating thing.
1102
1103 //-----------------------------------------------------------------------------
1104 static void show_animated_bitmap(grs_canvas &canvas, briefing *br)
1105 {
1106 #if DXX_USE_OGL
1107 float scale = 1.0;
1108
1109 if ((static_cast<float>(SWIDTH)/320) < (static_cast<float>(SHEIGHT)/200))
1110 scale = (static_cast<float>(SWIDTH)/320);
1111 else
1112 scale = (static_cast<float>(SHEIGHT)/200);
1113 #endif
1114
1115 // Only plot every nth frame.
1116 if (br->door_div_count) {
1117 if (br->bitmap_name[0] != 0) {
1118 const auto bi = piggy_find_bitmap(br->bitmap_name);
1119 grs_bitmap *const bitmap_ptr = &GameBitmaps[bi];
1120 PIGGY_PAGE_IN( bi );
1121 #if DXX_USE_OGL
1122 ogl_ubitmapm_cs(canvas, rescale_x(canvas.cv_bitmap, 220), rescale_y(canvas.cv_bitmap, 45), bitmap_ptr->bm_w * scale, bitmap_ptr->bm_h * scale, *bitmap_ptr, 255);
1123 #else
1124 gr_bitmapm(canvas, rescale_x(canvas.cv_bitmap, 220), rescale_y(canvas.cv_bitmap, 45), *bitmap_ptr);
1125 #endif
1126 }
1127 br->door_div_count--;
1128 return;
1129 }
1130
1131 br->door_div_count = DOOR_DIV_INIT;
1132
1133 if (br->bitmap_name[0] != 0) {
1134 char *pound_signp;
1135 int num, dig1, dig2;
1136 grs_subcanvas_ptr bitmap_canv;
1137
1138 switch (br->animating_bitmap)
1139 {
1140 case briefing::animating_bitmap_type::loop:
1141 bitmap_canv = gr_create_sub_canvas(canvas, rescale_x(canvas.cv_bitmap, 220), rescale_y(canvas.cv_bitmap, 45), 64, 64);
1142 break;
1143 case briefing::animating_bitmap_type::reset:
1144 bitmap_canv = gr_create_sub_canvas(canvas, rescale_x(canvas.cv_bitmap, 220), rescale_y(canvas.cv_bitmap, 45), 94, 94);
1145 break; // Adam: Change here for your new animating bitmap thing. 94, 94 are bitmap size.
1146 default: Int3(); // Impossible, illegal value for br->animating_bitmap_type
1147 }
1148
1149 auto &subcanvas = *bitmap_canv.get();
1150
1151 pound_signp = strchr(&br->bitmap_name[0], '#');
1152 Assert(pound_signp != NULL);
1153
1154 dig1 = *(pound_signp+1);
1155 dig2 = *(pound_signp+2);
1156 if (dig2 == 0)
1157 num = dig1-'0';
1158 else
1159 num = (dig1-'0')*10 + (dig2-'0');
1160
1161 switch (br->animating_bitmap)
1162 {
1163 case briefing::animating_bitmap_type::loop:
1164 num += static_cast<int8_t>(br->door_dir);
1165 if (num > EXIT_DOOR_MAX) {
1166 num = EXIT_DOOR_MAX;
1167 br->door_dir = briefing::door_direction::backward;
1168 } else if (num < 0) {
1169 num = 0;
1170 br->door_dir = briefing::door_direction::forward;
1171 }
1172 break;
1173 case briefing::animating_bitmap_type::reset:
1174 num++;
1175 if (num > OTHER_THING_MAX)
1176 num = 0;
1177 break;
1178 }
1179
1180 Assert(num < 100);
1181 if (num >= 10) {
1182 *(pound_signp+1) = (num / 10) + '0';
1183 *(pound_signp+2) = (num % 10) + '0';
1184 *(pound_signp+3) = 0;
1185 } else {
1186 *(pound_signp+1) = (num % 10) + '0';
1187 *(pound_signp+2) = 0;
1188 }
1189
1190 const auto bi = piggy_find_bitmap(br->bitmap_name);
1191 grs_bitmap *const bitmap_ptr = &GameBitmaps[bi];
1192 PIGGY_PAGE_IN( bi );
1193 #if DXX_USE_OGL
1194 ogl_ubitmapm_cs(subcanvas, 0, 0, bitmap_ptr->bm_w*scale, bitmap_ptr->bm_h*scale, *bitmap_ptr, 255);
1195 #else
1196 gr_bitmapm(subcanvas, 0, 0, *bitmap_ptr);
1197 #endif
1198
1199 switch (br->animating_bitmap)
1200 {
1201 case briefing::animating_bitmap_type::loop:
1202 if (num == EXIT_DOOR_MAX) {
1203 br->door_dir = briefing::door_direction::backward;
1204 br->door_div_count = 64;
1205 } else if (num == 0) {
1206 br->door_dir = briefing::door_direction::forward;
1207 br->door_div_count = 64;
1208 }
1209 break;
1210 case briefing::animating_bitmap_type::reset:
1211 break;
1212 }
1213 }
1214 }
1215
1216 }
1217
1218 }
1219
1220 namespace {
1221
1222 //-----------------------------------------------------------------------------
1223 static void show_briefing_bitmap(grs_canvas &canvas, grs_bitmap *bmp)
1224 {
1225 const bool hiresmode = HIRESMODE;
1226 const auto w = static_cast<float>(SWIDTH) / (hiresmode ? 640 : 320);
1227 const auto h = static_cast<float>(SHEIGHT) / (hiresmode ? 480 : 200);
1228 const float scale = (w < h) ? w : h;
1229
1230 auto bitmap_canv = gr_create_sub_canvas(canvas, rescale_x(canvas.cv_bitmap, 220), rescale_y(canvas.cv_bitmap, 55), bmp->bm_w*scale, bmp->bm_h*scale);
1231 show_fullscr(*bitmap_canv, *bmp);
1232 }
1233
1234 }
1235
1236 //-----------------------------------------------------------------------------
1237 namespace dsx {
1238 namespace {
1239 static void init_spinning_robot(grs_canvas &canvas, briefing &br) //(int x,int y,int w,int h)
1240 {
1241 br.robot_canv = create_spinning_robot_sub_canvas(canvas);
1242 }
1243
1244 static void show_spinning_robot_frame(const enumerated_array<polymodel, MAX_POLYGON_MODELS, polygon_model_index> &Polygon_models, const d_robot_info_array &Robot_info, briefing &br, const robot_id robot_num)
1245 {
1246 br.robot_angles.p = br.robot_angles.b = 0;
1247 br.robot_angles.h += 150;
1248
1249 const auto model_num = Robot_info[robot_num].model_num;
1250 assert(model_num != polygon_model_index::None);
1251 draw_model_picture(*br.robot_canv.get(), Polygon_models[model_num], br.robot_angles);
1252 }
1253
1254 //-----------------------------------------------------------------------------
1255 #define KEY_DELAY_DEFAULT ((F1_0*20)/1000)
1256
1257 [[nodiscard]]
1258 static int init_new_page(grs_canvas &canvas, briefing *br)
1259 {
1260 br->new_page = 0;
1261 br->robot_num = -1;
1262
1263 const auto r = load_briefing_screen(canvas, br, br->background_name.data());
1264 if (!r)
1265 return r;
1266 br->text_x = br->screen->text_ulx;
1267 br->text_y = br->screen->text_uly;
1268
1269 br->streamcount=0;
1270 br->guy_bitmap.reset();
1271
1272 #if DXX_BUILD_DESCENT == 2
1273 if (auto &RoboFile = br->RoboFile)
1274 {
1275 RoboFile.reset();
1276 DeInitRobotMovie(br->pMovie);
1277 }
1278 #endif
1279
1280 br->start_time = 0;
1281 br->delay_count = KEY_DELAY_DEFAULT;
1282 return r;
1283 }
1284
1285 #if DXX_BUILD_DESCENT == 2
1286 static int DefineBriefingBox(const grs_bitmap &cv_bitmap, const char *&buf)
1287 {
1288 int i{0};
1289 char name[20];
1290
1291 const auto n = get_new_message_num (buf);
1292
1293 assert(n < Briefing_screens.size());
1294
1295 while (*buf != ' ')
1296 {
1297 name[i++]= *buf;
1298 ++buf;
1299 }
1300
1301 name[i]='\0'; // slap a delimiter on this guy
1302
1303 strcpy (Briefing_screens[n].bs_name,name);
1304 Briefing_screens[n].level_num=get_new_message_num (buf);
1305 Briefing_screens[n].message_num=get_new_message_num (buf);
1306 Briefing_screens[n].text_ulx=get_new_message_num (buf);
1307 Briefing_screens[n].text_uly=get_new_message_num (buf);
1308 Briefing_screens[n].text_width=get_new_message_num (buf);
1309 Briefing_screens[n].text_height = get_message_num (buf); // NOTICE!!!
1310
1311 Briefing_screens[n].text_ulx = rescale_x(cv_bitmap, Briefing_screens[n].text_ulx);
1312 Briefing_screens[n].text_uly = rescale_y(cv_bitmap, Briefing_screens[n].text_uly);
1313 Briefing_screens[n].text_width = rescale_x(cv_bitmap, Briefing_screens[n].text_width);
1314 Briefing_screens[n].text_height = rescale_y(cv_bitmap, Briefing_screens[n].text_height);
1315
1316 return (n);
1317 }
1318 #endif
1319
1320 static void free_briefing_screen(briefing *br);
1321
1322 // -----------------------------------------------------------------------------
1323 // loads a briefing screen
1324 static int load_briefing_screen(grs_canvas &canvas, briefing *const br, const char *const fname)
1325 {
1326 #if DXX_BUILD_DESCENT == 1
1327 const descent_hog_size descent_hog_size{PHYSFSX_fsize(descent_hog_basename)};
1328 char forigin[PATH_MAX];
1329 decltype(br->background_name) fname2a;
1330
1331 free_briefing_screen(br);
1332
1333 snprintf(fname2a.data(), fname2a.size(), "%s", fname);
1334 snprintf(forigin, sizeof(forigin), "%s", PHYSFS_getRealDir(fname));
1335 d_strlwr(forigin);
1336
1337 // check if we have a hires version of this image (not included in PC-version by default)
1338 // Also if this hires image comes via external AddOn pack, only apply if requested image would be loaded from descent.hog - not a seperate mission which might want to show something else.
1339 if (SWIDTH >= 640 && SHEIGHT >= 480 && (strstr(forigin,"descent.hog") != NULL))
1340 {
1341 const auto fb = fname2a.begin();
1342 auto ptr = fb;
1343 for (; const char c = *ptr; ++ptr)
1344 {
1345 if (c == '.')
1346 {
1347 *ptr = 0;
1348 break;
1349 }
1350 }
1351 auto &hires_pcx = "h.pcx";
1352 const size_t len_fname2 = std::distance(fb, ptr);
1353 if (len_fname2 + sizeof(hires_pcx) < fname2a.size())
1354 {
1355 strcpy(ptr, hires_pcx);
1356 if (!PHYSFSX_exists_ignorecase(fname2a.data()))
1357 snprintf(fname2a.data(), fname2a.size(), "%s", fname);
1358 }
1359 }
1360 br->background_name = fname2a;
1361 const auto fname2 = fname2a.data();
1362
1363 pcx_result pcx_error;
1364 if ((!d_stricmp(fname2, "brief02.pcx") || !d_stricmp(fname2, "brief02h.pcx")) && cheats.baldguy &&
1365 bald_guy_load("btexture.xxx", br->background, gr_palette) == pcx_result::SUCCESS)
1366 {
1367 }
1368 else if ((pcx_error = pcx_read_bitmap_or_default(fname2, br->background, gr_palette)) != pcx_result::SUCCESS)
1369 {
1370 con_printf(CON_URGENT, "%s:%u: error: loading briefing screen <%s> failed: PCX load error: %s (%u)", __FILE__, __LINE__, fname2, pcx_errormsg(pcx_error), static_cast<unsigned>(pcx_error));
1371 }
1372
1373 // Hack: Make sure black parts of robot are shown black
1374 if (MacPig && gr_palette[0].r == 63 &&
1375 (!d_stricmp(fname2, "brief03.pcx") || !d_stricmp(fname2, "end01.pcx") ||
1376 !d_stricmp(fname2, "brief03h.pcx") || !d_stricmp(fname2, "end01h.pcx")
1377 ))
1378 {
1379 swap_0_255(br->background);
1380 gr_palette[0].r = gr_palette[0].g = gr_palette[0].b = 0;
1381 gr_palette[255].r = gr_palette[255].g = gr_palette[255].b = 63;
1382 }
1383 show_fullscr(canvas, br->background);
1384 gr_palette_load(gr_palette);
1385
1386 set_briefing_fontcolor();
1387
1388 br->screen = std::make_unique<briefing_screen>(get_d1_briefing_screens(descent_hog_size)[br->cur_screen]);
1389 br->screen->text_ulx = rescale_x(canvas.cv_bitmap, br->screen->text_ulx);
1390 br->screen->text_uly = rescale_y(canvas.cv_bitmap, br->screen->text_uly);
1391 br->screen->text_width = rescale_x(canvas.cv_bitmap, br->screen->text_width);
1392 br->screen->text_height = rescale_y(canvas.cv_bitmap, br->screen->text_height);
1393 init_char_pos(br, br->screen->text_ulx, br->screen->text_uly);
1394 #elif DXX_BUILD_DESCENT == 2
1395 free_briefing_screen(br);
1396 const auto bndata = br->background_name.data();
1397 if (fname != bndata)
1398 {
1399 br->background_name.back() = 0;
1400 strncpy(bndata, fname, br->background_name.size() - 1);
1401 }
1402
1403 pcx_result pcx_error;
1404 if ((pcx_error = pcx_read_bitmap_or_default(fname, br->background, gr_palette)) != pcx_result::SUCCESS)
1405 {
1406 con_printf(CON_URGENT, "%s:%u: error: loading briefing screen <%s> failed: PCX load error: %s (%u)", __FILE__, __LINE__, fname, pcx_errormsg(pcx_error), static_cast<unsigned>(pcx_error));
1407 return 0;
1408 }
1409 show_fullscr(canvas, br->background);
1410 if (EMULATING_D1 && !d_stricmp(fname, "brief03.pcx")) // HACK, FIXME: D1 missions should use their own palette (PALETTE.256), but texture replacements not complete
1411 gr_use_palette_table("groupa.256");
1412
1413 gr_palette_load(gr_palette);
1414
1415 set_briefing_fontcolor(*br);
1416
1417 if (EMULATING_D1)
1418 {
1419 br->got_z = 1;
1420 br->screen = std::make_unique<briefing_screen>(Briefing_screens[br->cur_screen]);
1421 br->screen->text_ulx = rescale_x(canvas.cv_bitmap, br->screen->text_ulx);
1422 br->screen->text_uly = rescale_y(canvas.cv_bitmap, br->screen->text_uly);
1423 br->screen->text_width = rescale_x(canvas.cv_bitmap, br->screen->text_width);
1424 br->screen->text_height = rescale_y(canvas.cv_bitmap, br->screen->text_height);
1425 init_char_pos(br, br->screen->text_ulx, br->screen->text_uly);
1426 }
1427
1428 #endif
1429 return 1;
1430 }
1431
1432 static void free_briefing_screen(briefing *br)
1433 {
1434 br->background.reset();
1435 #if DXX_BUILD_DESCENT == 2
1436 if (auto &RoboFile = br->RoboFile)
1437 {
1438 RoboFile.reset();
1439 DeInitRobotMovie(br->pMovie);
1440 }
1441 #endif
1442 br->robot_canv.reset();
1443 #if DXX_BUILD_DESCENT == 2
1444 br->printing_channel.reset();
1445 #endif
1446 if (EMULATING_D1)
1447 br->screen.reset();
1448 }
1449
1450 [[nodiscard]]
1451 static int new_briefing_screen(grs_canvas &canvas, briefing *br, int first)
1452 {
1453 br->new_screen = 0;
1454 const descent_hog_size descent_hog_size{PHYSFSX_fsize(descent_hog_basename)};
1455 const auto num_d1_briefing_screens = (
1456 (descent_hog_size == descent_hog_size::pc_shareware_v14 || descent_hog_size == descent_hog_size::pc_shareware_v10)
1457 ? std::size(D1_Briefing_screens_share)
1458 : std::size(D1_Briefing_screens_full)
1459 );
1460 #if DXX_BUILD_DESCENT == 1
1461
1462 if (!first)
1463 br->cur_screen++;
1464
1465 auto &&d1_briefing_screens = get_d1_briefing_screens(descent_hog_size);
1466 while (br->cur_screen < num_d1_briefing_screens && d1_briefing_screens[br->cur_screen].level_num != br->level_num)
1467 {
1468 br->cur_screen++;
1469 if (br->cur_screen == num_d1_briefing_screens && br->level_num == 0)
1470 {
1471 // Showed the pre-game briefing, now show level 1 briefing
1472 br->level_num++;
1473 br->cur_screen = 0;
1474 }
1475 }
1476
1477 if (br->cur_screen == num_d1_briefing_screens)
1478 return 0; // finished
1479
1480 if (!load_briefing_screen(canvas, br, d1_briefing_screens[br->cur_screen].bs_name))
1481 return 0;
1482
1483 br->message = get_briefing_message(br, d1_briefing_screens[br->cur_screen].message_num);
1484 #elif DXX_BUILD_DESCENT == 2
1485 br->got_z = 0;
1486
1487 if (EMULATING_D1)
1488 {
1489 auto &&d1_briefing_screens = get_d1_briefing_screens(descent_hog_size);
1490 if (!first)
1491 br->cur_screen++;
1492 else
1493 for (int i = 0; i < num_d1_briefing_screens; i++)
1494 Briefing_screens[i] = d1_briefing_screens[i];
1495
1496 while (br->cur_screen < num_d1_briefing_screens && Briefing_screens[br->cur_screen].level_num != br->level_num)
1497 {
1498 br->cur_screen++;
1499 if (br->cur_screen == num_d1_briefing_screens && br->level_num == 0)
1500 {
1501 // Showed the pre-game briefing, now show level 1 briefing
1502 br->level_num++;
1503 br->cur_screen = 0;
1504 }
1505 }
1506
1507 if (br->cur_screen == num_d1_briefing_screens)
1508 return 0; // finished
1509
1510 if (!load_briefing_screen(canvas, br, Briefing_screens[br->cur_screen].bs_name))
1511 return 0;
1512 }
1513 else if (first)
1514 {
1515 br->cur_screen = br->level_num;
1516 br->screen.reset(Briefing_screens.data());
1517 init_char_pos(br, br->screen->text_ulx, br->screen->text_uly-(8*(1+HIRESMODE)));
1518 }
1519 else
1520 return 0; // finished
1521
1522 br->message = get_briefing_message(br, EMULATING_D1 ? Briefing_screens[br->cur_screen].message_num : br->cur_screen);
1523 br->printing_channel.reset();
1524 br->dumb_adjust = 0;
1525 br->line_adjustment = 1;
1526 br->chattering = 0;
1527 br->RoboFile = {};
1528 #endif
1529
1530 if (br->message==NULL)
1531 return 0;
1532
1533 Current_color = &Briefing_text_colors.front();
1534 br->streamcount = 0;
1535 br->tab_stop = 0;
1536 br->flashing_cursor = 0;
1537 br->new_page = 0;
1538 br->start_time = 0;
1539 br->delay_count = KEY_DELAY_DEFAULT;
1540 br->robot_num = -1;
1541 br->bitmap_name[0] = 0;
1542 br->guy_bitmap.reset();
1543 br->prev_ch = -1;
1544
1545 #if DXX_BUILD_DESCENT == 2
1546 if (songs_is_playing() == song_number::None && !br->hum_channel)
1547 br->hum_channel.reset(digi_start_sound(digi_xlat_sound(sound_effect::SOUND_BRIEFING_HUM), F1_0/2, sound_pan{0x7fff}, 1, -1, -1, sound_object_none));
1548 #endif
1549
1550 return 1;
1551 }
1552
1553 }
1554
1555 //-----------------------------------------------------------------------------
1556 window_event_result briefing::event_handler(const d_event &event)
1557 {
1558 window_event_result result;
1559
1560 switch (event.type)
1561 {
1562 case event_type::window_activated:
1563 case event_type::window_deactivated:
1564 key_flush();
1565 break;
1566
1567 case event_type::mouse_button_down:
1568 if (event_mouse_get_button(event) == mbtn::left)
1569 {
1570 if (this->new_screen)
1571 {
1572 if (!new_briefing_screen(*grd_curcanv, this, 0))
1573 {
1574 return window_event_result::close;
1575 }
1576 }
1577 else if (this->new_page)
1578 {
1579 if (!init_new_page(*grd_curcanv, this))
1580 return window_event_result::close;
1581 }
1582 else
1583 this->delay_count = 0;
1584 return window_event_result::handled;
1585 }
1586 break;
1587
1588 #if DXX_MAX_BUTTONS_PER_JOYSTICK
1589 case event_type::joystick_button_down:
1590 // using joy_translate_menu_key doesn't work here for unclear
1591 // reasons, so we build a reasonable facsimile right here
1592 if (event_joystick_get_button(event) == 1)
1593 return window_event_result::close;
1594 if (this->new_screen)
1595 {
1596 if (!new_briefing_screen(*grd_curcanv, this, 0))
1597 {
1598 return window_event_result::close;
1599 }
1600 }
1601 else if (this->new_page)
1602 {
1603 if (!init_new_page(*grd_curcanv, this))
1604 return window_event_result::close;
1605 }
1606 else
1607 this->delay_count = 0;
1608 return window_event_result::handled;
1609 #endif
1610
1611 case event_type::key_command:
1612 {
1613 int key = event_key_get(event);
1614
1615 switch (key)
1616 {
1617 #if DXX_BUILD_DESCENT == 1
1618 case KEY_ALTED + KEY_B: // B - ALTED... BALT... BALD... get it?
1619 cheats.baldguy = !cheats.baldguy;
1620 break;
1621 #endif
1622 case KEY_ESC:
1623 return window_event_result::close;
1624 case KEY_SPACEBAR:
1625 case KEY_ENTER:
1626 this->delay_count = 0;
1627 [[fallthrough]];
1628 default:
1629 if ((result = call_default_handler(event)) != window_event_result::ignored)
1630 return result;
1631 else if (this->new_screen)
1632 {
1633 if (!new_briefing_screen(*grd_curcanv, this, 0))
1634 {
1635 return window_event_result::close;
1636 }
1637 }
1638 else if (this->new_page)
1639 {
1640 if (!init_new_page(*grd_curcanv, this))
1641 return window_event_result::close;
1642 }
1643 break;
1644 }
1645 break;
1646 }
1647
1648 case event_type::window_draw:
1649 {
1650 auto &canvas = w_canv;
1651
1652 timer_delay2(50);
1653
1654 if (!(this->new_screen || this->new_page))
1655 while (!briefing_process_char(canvas, this) && !this->delay_count)
1656 {
1657 check_text_pos(this);
1658 if (this->new_page)
1659 break;
1660 }
1661 check_text_pos(this);
1662
1663 if (this->background.bm_data)
1664 show_fullscr(canvas, this->background);
1665
1666 if (this->guy_bitmap.bm_data)
1667 show_briefing_bitmap(canvas, &this->guy_bitmap);
1668 if (this->bitmap_name[0] != 0)
1669 show_animated_bitmap(canvas, this);
1670 #if DXX_BUILD_DESCENT == 2
1671 if (auto pMovie = this->pMovie.get())
1672 RotateRobot(pMovie);
1673 #endif
1674 if (this->robot_num != -1)
1675 {
1676 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
1677 show_spinning_robot_frame(LevelSharedPolygonModelState.Polygon_models, Robot_info, *this, static_cast<robot_id>(this->robot_num));
1678 }
1679
1680 auto &game_font = *GAME_FONT;
1681
1682 auto lastcolor = *Current_color;
1683 gr_set_fontcolor(canvas, lastcolor, -1);
1684 for (const auto b : partial_const_range(this->messagestream, this->streamcount))
1685 lastcolor = redraw_messagestream(canvas, game_font, b, lastcolor);
1686
1687 if (this->new_page || this->new_screen)
1688 flash_cursor(canvas, game_font, this, this->flashing_cursor);
1689 else if (this->flashing_cursor)
1690 gr_string(canvas, game_font, this->text_x, this->text_y, "_");
1691 break;
1692 }
1693 case event_type::window_close:
1694 free_briefing_screen(this);
1695 #if DXX_BUILD_DESCENT == 2
1696 this->hum_channel.reset();
1697 #endif
1698 break;
1699
1700 default:
1701 break;
1702 }
1703 return window_event_result::ignored;
1704 }
1705
1706 void do_briefing_screens(const d_fname &filename, int level_num)
1707 {
1708 if (!*static_cast<const char *>(filename))
1709 return;
1710
1711 auto br = window_create<briefing>(grd_curscreen->sc_canvas);
1712 briefing_init(br, level_num);
1713
1714 if (!load_screen_text(filename, br->text))
1715 {
1716 return;
1717 }
1718
1719 #if DXX_BUILD_DESCENT == 2
1720 if (!(EMULATING_D1 || is_SHAREWARE || is_MAC_SHARE || is_D2_OEM || !PLAYING_BUILTIN_MISSION))
1721 songs_stop_all();
1722 else
1723 #endif
1724 {
1725 if (const auto s{songs_is_playing()}; s != song_number::briefing && s != song_number::endgame)
1726 songs_play_song(song_number::briefing, 1);
1727 }
1728
1729 #if DXX_BUILD_DESCENT == 1
1730 set_screen_mode( SCREEN_MENU );
1731 #elif DXX_BUILD_DESCENT == 2
1732 // set screen correctly for robot movies
1733 set_screen_mode( SCREEN_MOVIE );
1734 #endif
1735
1736 gr_set_default_canvas();
1737
1738 if (!new_briefing_screen(*grd_curcanv, br, 1))
1739 {
1740 window_close(br);
1741 }
1742 else
1743 // Stay where we are in the stack frame until briefing done
1744 // Too complicated otherwise
1745 event_process_all();
1746 }
1747
1748 void do_end_briefing_screens(const d_fname &filename)
1749 {
1750 int level_num_screen = Current_level_num;
1751
1752 if (!*static_cast<const char *>(filename))
1753 return; // no filename, no ending
1754
1755 if (EMULATING_D1)
1756 {
1757 song_number song;
1758 if (d_stricmp(filename, BIMD1_ENDING_FILE_OEM) == 0)
1759 {
1760 song = song_number::endgame;
1761 level_num_screen = ENDING_LEVEL_NUM_OEMSHARE;
1762 }
1763 else if (d_stricmp(filename, BIMD1_ENDING_FILE_SHARE) == 0)
1764 {
1765 song = song_number::briefing;
1766 level_num_screen = ENDING_LEVEL_NUM_OEMSHARE;
1767 }
1768 else
1769 {
1770 song = song_number::endgame;
1771 level_num_screen = ENDING_LEVEL_NUM_REGISTER;
1772 }
1773 songs_play_song(song, 1);
1774 }
1775 #if DXX_BUILD_DESCENT == 2
1776 else if (PLAYING_BUILTIN_MISSION)
1777 {
1778 song_number song;
1779 if ((d_stricmp(filename, BIMD2_ENDING_FILE_OEM) == 0 && (song = song_number::title, true)) ||
1780 (d_stricmp(filename, BIMD2_ENDING_FILE_SHARE) == 0 && (song = song_number::endgame, true)))
1781 {
1782 songs_play_song(song, 1);
1783 level_num_screen = 1;
1784 }
1785 }
1786 else
1787 {
1788 songs_play_song(song_number::endgame, 1);
1789 level_num_screen = Current_mission->last_level + 1;
1790 }
1791 #endif
1792
1793 do_briefing_screens(filename, level_num_screen);
1794 }
1795
1796 }