formatting
[btb/d2x.git] / main / titles.c
1 /* $Id: titles.c,v 1.14 2002-08-30 08:04:44 btb Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include <conf.h>
17 #endif
18
19 #define ROBOT_MOVIES
20
21 #ifdef WINDOWS
22 #include "desw.h"
23 #endif
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #ifdef MACINTOSH
30 #include <Events.h>
31 #endif
32
33 #include "pa_enabl.h"                   //$$POLY_ACC
34 #include "pstypes.h"
35 #include "timer.h"
36 #include "key.h"
37 #include "gr.h"
38 #include "palette.h"
39 #include "iff.h"
40 #include "pcx.h"
41 #include "u_mem.h"
42 #include "joy.h"
43 #include "mono.h"
44 #include "gamefont.h"
45 #include "cfile.h"
46 #include "error.h"
47 #include "polyobj.h"
48 #include "textures.h"
49 #include "screens.h"
50 #include "multi.h"
51 #include "player.h"
52 #include "digi.h"
53 #include "compbit.h"
54 #include "text.h"
55 #include "kmatrix.h"
56 #include "piggy.h"
57 #include "songs.h"
58 #include "newmenu.h"
59 #include "state.h"
60 #ifdef ROBOT_MOVIES
61 #include "movie.h"
62 #endif
63 #include "menu.h"
64
65 #if defined(POLY_ACC)
66 #include "poly_acc.h"
67 #endif
68
69 #ifdef ROBOT_MOVIES
70 extern void RotateRobot();
71 #endif
72
73 void DoBriefingColorStuff ();
74 int get_new_message_num(char **message);
75 int DefineBriefingBox (char **buf);
76
77 extern unsigned RobSX,RobSY,RobDX,RobDY; // Robot movie coords
78
79 extern int MVEPaletteCalls;
80
81 ubyte New_pal[768];
82 int     New_pal_254_bash;
83
84 char CurBriefScreenName[15]="brief03.pcx";
85 char    * Briefing_text;
86 #ifdef ROBOT_MOVIES
87 char RobotPlaying=0;
88 #endif
89
90 #define MAX_BRIEFING_COLORS     3
91
92 // Descent 1 briefings
93 char Ending_text_filename[13] = "\0";
94 char Briefing_text_filename[13] = "\0";
95
96 #define SHAREWARE_ENDING_FILENAME       "ending.tex"
97
98 //      Can be set by -noscreens command line option.  Causes bypassing of all briefing screens.
99 int     Skip_briefing_screens=0;
100 int     Briefing_foreground_colors[MAX_BRIEFING_COLORS], Briefing_background_colors[MAX_BRIEFING_COLORS];
101 int     Current_color = 0;
102 int     Erase_color;
103
104 extern int check_button_press();
105
106 #ifdef MACINTOSH
107 extern void macintosh_quit(void);
108 #endif
109
110 static int rescale_x(int x)
111 {
112         return x * GWIDTH / 320;
113 }
114
115 static int rescale_y(int y)
116 {
117         return y * GHEIGHT / 200;
118 }
119
120 #ifndef MACINTOSH
121 int local_key_inkey(void)
122 {
123         int     rval;
124
125 #ifdef WINDOWS
126         MSG msg;
127
128         DoMessageStuff(&msg);
129 #endif
130
131         rval = key_inkey();
132
133         if (rval == KEY_PRINT_SCREEN) {
134                 #ifdef POLY_ACC
135 #ifdef ROBOT_MOVIES
136                 if (RobotPlaying) {
137                         gr_palette_read(gr_palette);
138                         gr_copy_palette(gr_palette,gr_palette,0);       //reset color lookup cache
139                 }
140 #endif
141                 #endif
142                 save_screen_shot(0);
143                 return 0;                               //say no key pressed
144         }
145
146         if (check_button_press())               //joystick or mouse button pressed?
147                 rval = KEY_SPACEBAR;
148
149         #ifdef MACINTOSH
150         if ( rval == KEY_Q+KEY_COMMAND )
151                 macintosh_quit();
152         #endif
153
154         return rval;
155 }
156 #else
157 int local_key_inkey(void)
158 {
159         EventRecord event;
160         int     rval;
161
162         if (!GetOSEvent(everyEvent, &event))
163                 return 0;
164
165         if (event.what != keyDown)
166                 return 0;
167
168         rval = (int)((event.message & keyCodeMask) >> 8);
169
170         if (rval == KEY_PRINT_SCREEN) {
171                 save_screen_shot(0);
172                 return 0;                               //say no key pressed
173         }
174
175         if (check_button_press())               //joystick or mouse button pressed?
176                 rval = KEY_SPACEBAR;
177
178         #ifdef MACINTOSH
179         if ( rval == KEY_Q+KEY_COMMAND )
180                 macintosh_quit();
181         #endif
182
183         return rval;
184 }
185 #endif
186
187 int show_title_screen( char * filename, int allow_keys, int from_hog_only )
188 {
189         fix timer;
190         int pcx_error;
191         grs_bitmap title_bm;
192         ubyte   palette_save[768];
193         char new_filename[FILENAME_LEN+1] = "";
194
195         #ifdef RELEASE
196         if (from_hog_only)
197                 strcpy(new_filename,"\x01");    //only read from hog file
198         #endif
199
200         strcat(new_filename,filename);
201         filename = new_filename;
202
203         title_bm.bm_data=NULL;
204         if ((pcx_error=pcx_read_bitmap( filename, &title_bm, BM_LINEAR, New_pal ))!=PCX_ERROR_NONE)     {
205                 printf( "File '%s', PCX load error: %s (%i)\n  (No big deal, just no title screen.)\n",filename, pcx_errormsg(pcx_error), pcx_error);
206                 mprintf((0, "File '%s', PCX load error: %s (%i)\n  (No big deal, just no title screen.)\n",filename, pcx_errormsg(pcx_error), pcx_error));
207                 Error( "Error loading briefing screen <%s>, PCX load error: %s (%i)\n",filename, pcx_errormsg(pcx_error), pcx_error);
208         }
209
210         memcpy(palette_save,gr_palette,sizeof(palette_save));
211
212 #if defined(POLY_ACC)
213     pa_save_clut();
214     pa_update_clut(New_pal, 0, 256, 0);
215 #endif
216
217         //vfx_set_palette_sub( New_pal );
218 #ifdef OGL
219         gr_palette_load( New_pal );
220 #else
221         gr_palette_clear();
222 #endif
223
224         WINDOS(
225                 dd_gr_set_current_canvas(NULL),
226                 gr_set_current_canvas( NULL )
227         );
228         WIN(DDGRLOCK(dd_grd_curcanv));
229         show_fullscr(&title_bm);
230         WIN(DDGRUNLOCK(dd_grd_curcanv));
231
232         WIN(DDGRRESTORE);
233
234 #if defined(POLY_ACC)
235     pa_restore_clut();
236 #endif
237
238         if (gr_palette_fade_in( New_pal, 32, allow_keys ))
239                 return 1;
240         gr_copy_palette(gr_palette, New_pal, sizeof(gr_palette));
241
242         gr_palette_load( New_pal );
243         timer   = timer_get_fixed_seconds() + i2f(3);
244         while (1) {
245                 if ( local_key_inkey() && allow_keys ) break;
246                 if ( timer_get_fixed_seconds() > timer ) break;
247         }
248         if (gr_palette_fade_out( New_pal, 32, allow_keys ))
249                 return 1;
250         gr_copy_palette(gr_palette, palette_save, sizeof(palette_save));
251         d_free(title_bm.bm_data);
252         return 0;
253 }
254
255 typedef struct {
256         char    bs_name[14];                //  filename, eg merc01.  Assumes .lbm suffix.
257         byte    level_num;
258         byte    message_num;
259         short   text_ulx, text_uly;         //  upper left x,y of text window
260         short   text_width, text_height;    //  width and height of text window
261 } briefing_screen;
262
263 #define BRIEFING_SECRET_NUM 31          //  This must correspond to the first secret level which must come at the end of the list.
264 #define BRIEFING_OFFSET_NUM 4           // This must correspond to the first level screen (ie, past the bald guy briefing screens)
265
266 #define SHAREWARE_ENDING_LEVEL_NUM  0x7f
267 #define REGISTERED_ENDING_LEVEL_NUM 0x7e
268
269 #ifdef SHAREWARE
270 #define ENDING_LEVEL_NUM        SHAREWARE_ENDING_LEVEL_NUM
271 #else
272 #define ENDING_LEVEL_NUM        REGISTERED_ENDING_LEVEL_NUM
273 #endif
274
275 #define MAX_BRIEFING_SCREENS 60
276
277 briefing_screen Briefing_screens[MAX_BRIEFING_SCREENS]=
278  {{"brief03.pcx",0,3,8,8,257,177}}; // default=0!!!
279
280 int     Briefing_text_x, Briefing_text_y;
281
282 void init_char_pos(int x, int y)
283 {
284         Briefing_text_x = x;
285         Briefing_text_y = y;
286         mprintf ((0,"Setting init x=%d y=%d\n",x,y));
287 }
288
289 grs_canvas      *Robot_canv = NULL;
290 vms_angvec      Robot_angles;
291
292 char    Bitmap_name[32] = "";
293 #define EXIT_DOOR_MAX   14
294 #define OTHER_THING_MAX 10      //  Adam: This is the number of frames in your new animating thing.
295 #define DOOR_DIV_INIT   6
296 byte    Door_dir=1, Door_div_count=0, Animating_bitmap_type=0;
297
298 //-----------------------------------------------------------------------------
299 void show_bitmap_frame(void)
300 {
301 #ifdef WINDOWS
302         dd_grs_canvas *curcanv_save, *bitmap_canv=0;
303 #else
304         grs_canvas *curcanv_save, *bitmap_canv=0;
305 #endif
306
307         grs_bitmap *bitmap_ptr;
308
309         //      Only plot every nth frame.
310         if (Door_div_count) {
311                 Door_div_count--;
312                 return;
313         }
314
315         Door_div_count = DOOR_DIV_INIT;
316
317         if (Bitmap_name[0] != 0) {
318                 char            *pound_signp;
319                 int             num, dig1, dig2;
320
321                 //      Set supertransparency color to black
322                 if (!New_pal_254_bash) {
323                         New_pal_254_bash = 1;
324                         New_pal[254*3] = 0;
325                         New_pal[254*3+1] = 0;
326                         New_pal[254*3+2] = 0;
327                         gr_palette_load( New_pal );
328                 }
329
330                 switch (Animating_bitmap_type) {
331                 case 0:
332                         WINDOS(
333                                 bitmap_canv = dd_gr_create_sub_canvas(dd_grd_curcanv, 220, 45, 64, 64); break,
334                                 bitmap_canv = gr_create_sub_canvas(grd_curcanv, 220, 45, 64, 64);       break
335                         );
336                 case 1:
337                         WINDOS(
338                                 bitmap_canv = dd_gr_create_sub_canvas(dd_grd_curcanv, 220, 45, 94, 94); break,
339                                 bitmap_canv = gr_create_sub_canvas(grd_curcanv, 220, 45, 94, 94);       break
340                         );
341
342                         // Adam: Change here for your new animating bitmap thing. 94, 94 are bitmap size.
343                         default:
344                                 Int3(); // Impossible, illegal value for Animating_bitmap_type
345                 }
346
347                 WINDOS(
348                         curcanv_save = dd_grd_curcanv; dd_grd_curcanv = bitmap_canv,
349                         curcanv_save = grd_curcanv; grd_curcanv = bitmap_canv
350                 );
351
352                 pound_signp = strchr(Bitmap_name, '#');
353                 Assert(pound_signp != NULL);
354
355                 dig1 = *(pound_signp+1);
356                 dig2 = *(pound_signp+2);
357                 if (dig2 == 0)
358                         num = dig1-'0';
359                 else
360                         num = (dig1-'0')*10 + (dig2-'0');
361
362                 switch (Animating_bitmap_type) {
363                 case 0:
364                         num += Door_dir;
365                         if (num > EXIT_DOOR_MAX) {
366                                 num = EXIT_DOOR_MAX;
367                                 Door_dir = -1;
368                         } else if (num < 0) {
369                                 num = 0;
370                                 Door_dir = 1;
371                         }
372                         break;
373                 case 1:
374                         num++;
375                         if (num > OTHER_THING_MAX)
376                                 num = 0;
377                         break;
378                 }
379
380                 Assert(num < 100);
381                 if (num >= 10) {
382                         *(pound_signp+1) = (num / 10) + '0';
383                         *(pound_signp+2) = (num % 10) + '0';
384                         *(pound_signp+3) = 0;
385                 } else {
386                         *(pound_signp+1) = (num % 10) + '0';
387                         *(pound_signp+2) = 0;
388                 }
389
390                 {
391                         bitmap_index bi;
392                         bi = piggy_find_bitmap(Bitmap_name);
393                         bitmap_ptr = &GameBitmaps[bi.index];
394                         PIGGY_PAGE_IN( bi );
395                 }
396
397                 WIN(DDGRLOCK(dd_grd_curcanv));
398                 gr_bitmapm(0, 0, bitmap_ptr);
399                 WIN(DDGRUNLOCK(dd_grd_curcanv));
400
401                 WINDOS(
402                         dd_grd_curcanv = curcanv_save,
403                         grd_curcanv = curcanv_save
404                 );
405                 d_free(bitmap_canv);
406
407                 switch (Animating_bitmap_type) {
408                 case 0:
409                         if (num == EXIT_DOOR_MAX) {
410                                 Door_dir = -1;
411                                 Door_div_count = 64;
412                         } else if (num == 0) {
413                                 Door_dir = 1;
414                                 Door_div_count = 64;
415                         }
416                         break;
417                 case 1:
418                         break;
419                 }
420         }
421
422 }
423
424 //-----------------------------------------------------------------------------
425 void show_briefing_bitmap(grs_bitmap *bmp)
426 {
427 #ifdef WINDOWS
428         dd_grs_canvas *bitmap_canv, *curcanv_save;
429
430         bitmap_canv = dd_gr_create_sub_canvas(dd_grd_curcanv, 220, 45, bmp->bm_w, bmp->bm_h);
431         curcanv_save = dd_grd_curcanv;
432         dd_gr_set_current_canvas(bitmap_canv);
433         DDGRLOCK(dd_grd_curcanv);
434         gr_bitmapm(0,0,bmp);
435         DDGRUNLOCK(dd_grd_curcanv);
436         dd_gr_set_current_canvas(curcanv_save);
437 #else
438         grs_canvas      *curcanv_save, *bitmap_canv;
439
440         bitmap_canv = gr_create_sub_canvas(grd_curcanv, 220, 45, bmp->bm_w, bmp->bm_h);
441         curcanv_save = grd_curcanv;
442         gr_set_current_canvas(bitmap_canv);
443         gr_bitmapm(0, 0, bmp);
444         gr_set_current_canvas(curcanv_save);
445 #endif
446
447         d_free(bitmap_canv);
448 }
449
450 #ifndef WINDOWS
451 //-----------------------------------------------------------------------------
452 void show_spinning_robot_frame(int robot_num)
453 {
454         grs_canvas      *curcanv_save;
455
456         if (robot_num != -1) {
457                 Robot_angles.h += 150;
458
459                 curcanv_save = grd_curcanv;
460                 grd_curcanv = Robot_canv;
461                 Assert(Robot_info[robot_num].model_num != -1);
462                 draw_model_picture(Robot_info[robot_num].model_num, &Robot_angles);
463                 grd_curcanv = curcanv_save;
464         }
465
466 }
467
468 //-----------------------------------------------------------------------------
469 void init_spinning_robot(void) //(int x,int y,int w,int h)
470 {
471 #if 0
472         Robot_angles.p += 0;
473         Robot_angles.b += 0;
474         Robot_angles.h += 0;
475
476 #else
477         int x = rescale_x(138);
478         int y = rescale_y(55);
479         int w = rescale_x(166);
480         int h = rescale_y(138);
481 #endif
482
483         Robot_canv = gr_create_sub_canvas(grd_curcanv, x, y, w, h);
484         // 138, 55, 166, 138
485 }
486 #endif
487
488 //---------------------------------------------------------------------------
489 // Returns char width.
490 // If show_robot_flag set, then show a frame of the spinning robot.
491 int show_char_delay(char the_char, int delay, int robot_num, int cursor_flag)
492 {
493         int w, h, aw;
494         char message[2];
495         static fix      start_time=0;
496
497         robot_num=0;
498         message[0] = the_char;
499         message[1] = 0;
500
501         if (start_time==0 && timer_get_fixed_seconds()<0)
502                 start_time=timer_get_fixed_seconds();
503
504         gr_get_string_size(message, &w, &h, &aw );
505
506         Assert((Current_color >= 0) && (Current_color < MAX_BRIEFING_COLORS));
507
508         //      Draw cursor if there is some delay and caller says to draw cursor
509         if (cursor_flag && delay) {
510                 WIN(DDGRLOCK(dd_grd_curcanv));
511                 gr_set_fontcolor(Briefing_foreground_colors[Current_color], -1);
512                 gr_printf(Briefing_text_x+1, Briefing_text_y, "_" );
513                 WIN(DDGRUNLOCK(dd_grd_curcanv));
514         }
515
516         if (delay)
517                 delay=fixdiv (F1_0,i2f(15));
518
519         if (delay != 0)
520                 show_bitmap_frame();
521
522 #ifdef ROBOT_MOVIES
523         if (RobotPlaying && (delay != 0))
524                 RotateRobot();
525
526         while (timer_get_fixed_seconds() < (start_time + delay)) {
527                 if (RobotPlaying && delay != 0)
528                         RotateRobot();
529         }
530 #else
531         if (robot_num != -1)
532                 show_spinning_robot_frame(robot_num);
533 #endif
534
535         start_time = timer_get_fixed_seconds();
536
537         WIN(DDGRLOCK(dd_grd_curcanv));
538         //      Erase cursor
539         if (cursor_flag && delay) {
540                 gr_set_fontcolor(Erase_color, -1);
541                 gr_printf(Briefing_text_x+1, Briefing_text_y, "_" );
542         }
543
544         //      Draw the character
545         gr_set_fontcolor(Briefing_background_colors[Current_color], -1);
546         gr_printf(Briefing_text_x, Briefing_text_y, message );
547
548         gr_set_fontcolor(Briefing_foreground_colors[Current_color], -1);
549         gr_printf(Briefing_text_x+1, Briefing_text_y, message );
550         WIN(DDGRUNLOCK(dd_grd_curcanv));
551
552         if (delay) gr_update();
553
554 //      if (the_char != ' ')
555 //              if (!digi_is_sound_playing(SOUND_MARKER_HIT))
556 //                      digi_play_sample( SOUND_MARKER_HIT, F1_0 );
557
558         return w;
559 }
560
561 //-----------------------------------------------------------------------------
562 int load_briefing_screen( int screen_num )
563 {
564         int     pcx_error;
565
566         WIN(DDGRLOCK(dd_grd_curcanv));
567         if ((pcx_error=pcx_read_fullscr( CurBriefScreenName, New_pal ))!=PCX_ERROR_NONE) {
568                 printf( "File '%s', PCX load error: %s\n  (It's a briefing screen.  Does this cause you pain?)\n",Briefing_screens[screen_num].bs_name, pcx_errormsg(pcx_error));
569                 printf( "File '%s', PCX load error: %s (%i)\n  (It's a briefing screen.  Does this cause you pain?)\n",Briefing_screens[screen_num].bs_name, pcx_errormsg(pcx_error), pcx_error);
570                 WIN(DDGRUNLOCK(dd_grd_curcanv));
571                 Error( "Error loading briefing screen <%s>, PCX load error: %s (%i)\n",CurBriefScreenName, pcx_errormsg(pcx_error), pcx_error);
572         }
573         WIN(DDGRUNLOCK(dd_grd_curcanv));
574
575         WIN(DDGRRESTORE);
576
577         return 0;
578 }
579
580 int load_new_briefing_screen( char *fname )
581 {
582         int pcx_error;
583
584         mprintf ((0,"Loading new briefing %s!\n",fname));
585         strcpy (CurBriefScreenName,fname);
586
587         //WIN(DEFINE_SCREEN(CurBriefScreenName));
588
589         if (gr_palette_fade_out( New_pal, 32, 0 ))
590                 return 0;
591
592         WIN(DDGRLOCK(dd_grd_curcanv));
593         if ((pcx_error=pcx_read_fullscr( fname, New_pal ))!=PCX_ERROR_NONE)     {
594         //if ((pcx_error=pcx_read_bitmap( fname, &grd_curcanv->cv_bitmap, grd_curcanv->cv_bitmap.bm_type, New_pal ))!=PCX_ERROR_NONE)     {
595                 printf( "File '%s', PCX load error: %s (%i)\n  (It's a briefing screen.  Does this cause you pain?)\n",fname, pcx_errormsg(pcx_error), pcx_error);
596                 WIN(DDGRUNLOCK(dd_grd_curcanv));
597                 Error( "Error loading briefing screen <%s>, PCX load error: %s (%i)\n",fname, pcx_errormsg(pcx_error), pcx_error);
598         }
599         WIN(DDGRUNLOCK(dd_grd_curcanv));
600
601         WIN(DDGRRESTORE);
602
603         gr_copy_palette(gr_palette, New_pal, sizeof(gr_palette));
604
605         if (gr_palette_fade_in( New_pal, 32, 0 ))
606                 return 0;
607         DoBriefingColorStuff();
608
609         return 1;
610 }
611
612
613
614 #define KEY_DELAY_DEFAULT       ((F1_0*20)/1000)
615
616 //-----------------------------------------------------------------------------
617 int get_message_num(char **message)
618 {
619         int     num=0;
620
621         while (**message == ' ')
622                 (*message)++;
623
624         while ((**message >= '0') && (**message <= '9')) {
625                 num = 10*num + **message-'0';
626                 (*message)++;
627         }
628
629         while (*(*message)++ != 10)             //      Get and drop eoln
630                 ;
631
632         return num;
633 }
634
635 //-----------------------------------------------------------------------------
636 void get_message_name(char **message, char *result)
637 {
638         while (**message == ' ')
639                 (*message)++;
640
641         while ((**message != ' ') && (**message != 10)) {
642                 if (**message != '\n')
643                         *result++ = **message;
644                 (*message)++;
645         }
646
647         if (**message != 10)
648                 while (*(*message)++ != 10)             //      Get and drop eoln
649                         ;
650
651         *result = 0;
652 }
653
654 //-----------------------------------------------------------------------------
655 void flash_cursor(int cursor_flag)
656 {
657         if (cursor_flag == 0)
658                 return;
659
660 WIN(DDGRLOCK(dd_grd_curcanv));
661         if ((timer_get_fixed_seconds() % (F1_0/2) ) > (F1_0/4))
662                 gr_set_fontcolor(Briefing_foreground_colors[Current_color], -1);
663         else
664                 gr_set_fontcolor(Erase_color, -1);
665
666         gr_printf(Briefing_text_x+1, Briefing_text_y, "_" );
667 WIN(DDGRUNLOCK(dd_grd_curcanv));
668 }
669
670 extern int InitMovieBriefing();
671
672 //-----------------------------------------------------------------------------
673 // Return true if message got aborted by user (pressed ESC), else return false.
674 int show_d1_briefing_message(int screen_num, char *message)
675 {
676         int     prev_ch=-1;
677         int     ch, done=0;
678         briefing_screen *bsp = &Briefing_screens[screen_num];
679         int     delay_count = KEY_DELAY_DEFAULT;
680         int     key_check;
681         int     robot_num=-1;
682         int     rval=0;
683         int     tab_stop=0;
684         int     flashing_cursor=0;
685         int     new_page=0;
686         int text_ulx = rescale_x(bsp->text_ulx);
687         int text_uly = rescale_y(bsp->text_uly);
688
689         Bitmap_name[0] = 0;
690
691         Current_color = 0;
692
693         // mprintf((0, "Going to print message [%s] at x=%i, y=%i\n", message, x, y));
694         gr_set_curfont( GAME_FONT );
695
696         init_char_pos(text_ulx, text_uly);
697
698         while (!done) {
699                 ch = *message++;
700                 if (ch == '$') {
701                         ch = *message++;
702                         if (ch == 'C') {
703                                 Current_color = get_message_num(&message)-1;
704                                 Assert((Current_color >= 0) && (Current_color < MAX_BRIEFING_COLORS));
705                                 prev_ch = 10;
706                         } else if (ch == 'F') {         //      toggle flashing cursor
707                                 flashing_cursor = !flashing_cursor;
708                                 prev_ch = 10;
709                                 while (*message++ != 10)
710                                         ;
711                         } else if (ch == 'T') {
712                                 tab_stop = get_message_num(&message);
713                                 prev_ch = 10;                                                   //      read to eoln
714                         } else if (ch == 'R') {
715                                 if (Robot_canv != NULL)
716                                         {free(Robot_canv); Robot_canv=NULL;}
717
718                                 init_spinning_robot();
719                                 robot_num = get_message_num(&message);
720                                 prev_ch = 10;                                                   //      read to eoln
721                         } else if (ch == 'N') {
722                                 //--grs_bitmap  *bitmap_ptr;
723                                 if (Robot_canv != NULL)
724                                         {free(Robot_canv); Robot_canv=NULL;}
725
726                                 get_message_name(&message, Bitmap_name);
727                                 strcat(Bitmap_name, "#0");
728                                 Animating_bitmap_type = 0;
729                                 prev_ch = 10;
730                         } else if (ch == 'O') {
731                                 if (Robot_canv != NULL)
732                                         {free(Robot_canv); Robot_canv=NULL;}
733
734                                 get_message_name(&message, Bitmap_name);
735                                 strcat(Bitmap_name, "#0");
736                                 Animating_bitmap_type = 1;
737                                 prev_ch = 10;
738                         } else if (ch == 'B') {
739                                 char                    bitmap_name[32];
740                                 grs_bitmap      guy_bitmap;
741                                 ubyte                   temp_palette[768];
742                                 int                     iff_error;
743
744                                 if (Robot_canv != NULL)
745                                         {free(Robot_canv); Robot_canv=NULL;}
746
747                                 get_message_name(&message, bitmap_name);
748                                 strcat(bitmap_name, ".bbm");
749                                 gr_init_bitmap_data (&guy_bitmap);
750                                 iff_error = iff_read_bitmap(bitmap_name, &guy_bitmap, BM_LINEAR, temp_palette);
751                                 Assert(iff_error == IFF_NO_ERROR);
752
753                                 show_briefing_bitmap(&guy_bitmap);
754                                 gr_free_bitmap_data (&guy_bitmap);
755                                 prev_ch = 10;
756 //                      } else if (ch == 'B') {
757 //                              if (Robot_canv != NULL)
758 //                                      {free(Robot_canv); Robot_canv=NULL;}
759 //
760 //                              bitmap_num = get_message_num(&message);
761 //                              if (bitmap_num != -1)
762 //                                      show_briefing_bitmap(Textures[bitmap_num]);
763 //                              prev_ch = 10;                                                   //      read to eoln
764                         } else if (ch == 'S') {
765                                 int     keypress;
766                                 fix     start_time;
767                                 fix time_out_value;
768
769                                 start_time = timer_get_fixed_seconds();
770                                 start_time = timer_get_approx_seconds();
771                                 time_out_value = start_time + i2f(60*5);        // Wait 1 minute...
772
773                                 //added on 9/13/98 by adb to make arch's requiring updates work
774                                 gr_update();
775                                 //end changes by adb
776
777                                 while ( (keypress = local_key_inkey()) == 0 ) { //      Wait for a key
778                                         if ( timer_get_approx_seconds() > time_out_value ) {
779                                                 keypress = 0;
780                                                 break;                                  // Time out after 1 minute..
781                                         }
782                                         while (timer_get_fixed_seconds() < start_time + KEY_DELAY_DEFAULT/2)
783                                                 ;
784                                         flash_cursor(flashing_cursor);
785                                         show_spinning_robot_frame(robot_num);
786                                         show_bitmap_frame();
787
788                                         //added on 9/13/98 by adb to make arch's requiring updates work
789                                         gr_update();
790                                         //end changes by adb
791
792                                         start_time += KEY_DELAY_DEFAULT/2;
793                                 }
794
795 #ifndef NDEBUG
796                                 if (keypress == KEY_BACKSP)
797                                         Int3();
798 #endif
799                                 if (keypress == KEY_ESC)
800                                         rval = 1;
801
802                                 flashing_cursor = 0;
803                                 done = 1;
804                         } else if (ch == 'P') {         //      New page.
805                                 new_page = 1;
806                                 while (*message != 10) {
807                                         message++;      //      drop carriage return after special escape sequence
808                                 }
809                                 message++;
810                                 prev_ch = 10;
811 //Begin D1X addition
812                         } else if (ch == '$' || ch == ';') {         // Print a $/;
813                                 prev_ch = ch;
814                                 Briefing_text_x += show_char_delay(ch, delay_count, robot_num, flashing_cursor);
815
816 //End D1X addition
817                         }
818                 } else if (ch == '\t') {                //      Tab
819                         if (Briefing_text_x - text_ulx < tab_stop)
820                                 Briefing_text_x = text_ulx + tab_stop;
821                 } else if ((ch == ';') && (prev_ch == 10)) {
822                         while (*message++ != 10)
823                                 ;
824                         prev_ch = 10;
825                 } else if (ch == '\\') {
826                         prev_ch = ch;
827                 } else if (ch == 10) {
828                         if (prev_ch != '\\') {
829                                 prev_ch = ch;
830                                 Briefing_text_y += GAME_FONT->ft_h+GAME_FONT->ft_h*3/5;
831                                 Briefing_text_x = text_ulx;
832                                 if (Briefing_text_y > text_uly + rescale_y(bsp->text_height)) {
833                                         load_briefing_screen(screen_num);
834                                         Briefing_text_x = text_ulx;
835                                         Briefing_text_y = text_uly;
836                                 }
837                         } else {
838                                 if (ch == 13)
839                                         Int3();
840                                 prev_ch = ch;
841                         }
842                 } else {
843                         prev_ch = ch;
844                         Briefing_text_x += show_char_delay(ch, delay_count, robot_num, flashing_cursor);
845                 }
846
847                 //added/changed on 9/13/98 by adb to speed up briefings after pressing a key with SDL
848                 //      Check for Esc -> abort.
849                 if(delay_count)
850                         key_check=local_key_inkey();
851                 else
852                         key_check=0;
853                 //end change - adb
854                 if ( key_check == KEY_ESC ) {
855                         rval = 1;
856                         done = 1;
857                 }
858
859 #if 0
860                 if ( key_check == KEY_ALTED+KEY_F2 )
861                         title_save_game();
862 #endif
863
864                 if ((key_check == KEY_SPACEBAR) || (key_check == KEY_ENTER))
865                         delay_count = 0;
866
867                 if (Briefing_text_x > text_ulx + rescale_x(bsp->text_width)) {
868                         Briefing_text_x = text_ulx;
869                         Briefing_text_y += GAME_FONT->ft_h+GAME_FONT->ft_h*3/5;
870                 }
871
872                 if ((new_page) || (Briefing_text_y > text_uly + rescale_y(bsp->text_height))) {
873                         fix     start_time = 0;
874                         fix     time_out_value = 0;
875                         int     keypress;
876
877                         new_page = 0;
878                         start_time = timer_get_approx_seconds();
879                         time_out_value = start_time + i2f(60*5);                // Wait 1 minute...
880
881                         //added on 9/13/98 by adb to make arch's requiring updates work
882                         gr_update();
883                         //end changes by adb
884
885                         while ( (keypress = local_key_inkey()) == 0 ) {         //      Wait for a key
886                                 if ( timer_get_approx_seconds() > time_out_value ) {
887                                         keypress = 0;
888                                         break;                                  // Time out after 1 minute..
889                                 }
890                                 while (timer_get_approx_seconds() < start_time + KEY_DELAY_DEFAULT/2)
891                                         ;
892                                 flash_cursor(flashing_cursor);
893                                 show_spinning_robot_frame(robot_num);
894                                 show_bitmap_frame();
895
896                                 //added on 9/13/98 by adb to make arch's requiring updates work
897                                 gr_update();
898                                 //end changes by adb
899
900                                 start_time += KEY_DELAY_DEFAULT/2;
901                         }
902
903                         robot_num = -1;
904
905 #ifndef NDEBUG
906                         if (keypress == KEY_BACKSP)
907                                 Int3();
908 #endif
909                         if (keypress == KEY_ESC) {
910                                 rval = 1;
911                                 done = 1;
912                         }
913
914                         load_briefing_screen(screen_num);
915                         Briefing_text_x = text_ulx;
916                         Briefing_text_y = text_uly;
917                         delay_count = KEY_DELAY_DEFAULT;
918                 }
919         }
920
921         if (Robot_canv != NULL)
922                 {free(Robot_canv); Robot_canv=NULL;}
923
924         return rval;
925 }
926
927 //-----------------------------------------------------------------------------
928 // Return true if message got aborted by user (pressed ESC), else return false.
929 int show_briefing_message(int screen_num, char *message)
930 {
931         int     prev_ch=-1;
932         int     ch, done=0,i;
933         briefing_screen *bsp = &Briefing_screens[screen_num];
934         int     delay_count = KEY_DELAY_DEFAULT;
935         int     key_check;
936         int     robot_num=-1;
937         int     rval=0;
938         static int tab_stop=0;
939         int     flashing_cursor=0;
940         int     new_page=0,GotZ=0;
941 #ifdef ROBOT_MOVIES
942         char spinRobotName[]="rba.mve",kludge;  // matt don't change this!
943 #endif
944         char fname[15];
945         char DumbAdjust=0;
946         char chattering=0;
947         int hum_channel=-1,printing_channel=-1;
948         int LineAdjustment=1;
949         WIN(int wpage_done=0);
950
951         Bitmap_name[0] = 0;
952         Current_color = 0;
953 #ifdef ROBOT_MOVIES
954         RobotPlaying=0;
955
956         InitMovieBriefing();
957 #endif
958
959         #ifndef SHAREWARE
960         hum_channel  = digi_start_sound( digi_xlat_sound(SOUND_BRIEFING_HUM), F1_0/2, 0xFFFF/2, 1, -1, -1, -1 );
961         #endif
962
963         // mprintf((0, "Going to print message [%s] at x=%i, y=%i\n", message, x, y));
964         gr_set_curfont( GAME_FONT );
965
966         bsp=&Briefing_screens[0];
967         init_char_pos(bsp->text_ulx, bsp->text_uly-(8*(1+MenuHires)));
968
969         while (!done) {
970                 ch = *message++;
971                 if (ch == '$') {
972                         ch = *message++;
973                         if (ch=='D') {
974                                 screen_num=DefineBriefingBox (&message);
975                         //load_new_briefing_screen (Briefing_screens[screen_num].bs_name);
976
977                                 bsp = &Briefing_screens[screen_num];
978                                 init_char_pos(bsp->text_ulx, bsp->text_uly);
979                                 //LineAdjustment=0;
980                                 prev_ch = 10;                                   // read to eoln
981                         } else if (ch=='U') {
982                                 screen_num=get_message_num(&message);
983                                 bsp = &Briefing_screens[screen_num];
984                                 init_char_pos(bsp->text_ulx, bsp->text_uly);
985                                 prev_ch = 10;                                   // read to eoln
986                         } else if (ch == 'C') {
987                                 Current_color = get_message_num(&message)-1;
988                                 Assert((Current_color >= 0) && (Current_color < MAX_BRIEFING_COLORS));
989                                 prev_ch = 10;
990                         } else if (ch == 'F') {     // toggle flashing cursor
991                                 flashing_cursor = !flashing_cursor;
992                                 prev_ch = 10;
993                                 while (*message++ != 10)
994                                         ;
995                         } else if (ch == 'T') {
996                                 tab_stop = get_message_num(&message);
997                                 tab_stop*=(1+MenuHires);
998                                 prev_ch = 10;                                                   //      read to eoln
999                         } else if (ch == 'R') {
1000                                 if (Robot_canv != NULL) {
1001                                         d_free(Robot_canv);
1002                                         Robot_canv=NULL;
1003                                 }
1004 #ifdef ROBOT_MOVIES
1005                                 if (RobotPlaying) {
1006                                         DeInitRobotMovie();
1007                                         RobotPlaying=0;
1008                                 }
1009
1010                                 kludge=*message++;
1011                                 spinRobotName[2]=kludge; // ugly but proud
1012
1013                                 RobotPlaying=InitRobotMovie(spinRobotName);
1014
1015                                 // gr_remap_bitmap_good( &grd_curcanv->cv_bitmap, pal, -1, -1 );
1016
1017                                 if (RobotPlaying) {
1018                                         DoBriefingColorStuff ();
1019                                         mprintf ((0,"Robot playing is %d!!!",RobotPlaying));
1020                                 }
1021 #else
1022                                 init_spinning_robot();
1023                                 robot_num = get_message_num(&message);
1024 #endif
1025                                 prev_ch = 10;                           // read to eoln
1026                         } else if (ch == 'N') {
1027                                 //--grs_bitmap *bitmap_ptr;
1028                                 if (Robot_canv != NULL) {
1029                                         d_free(Robot_canv);
1030                                         Robot_canv=NULL;
1031                                 }
1032
1033                                 get_message_name(&message, Bitmap_name);
1034                                 strcat(Bitmap_name, "#0");
1035                                 Animating_bitmap_type = 0;
1036                                 prev_ch = 10;
1037                         } else if (ch == 'O') {
1038                                 if (Robot_canv != NULL) {
1039                                                 d_free(Robot_canv);
1040                                                 Robot_canv=NULL;
1041                                 }
1042
1043                                 get_message_name(&message, Bitmap_name);
1044                                 strcat(Bitmap_name, "#0");
1045                                 Animating_bitmap_type = 1;
1046                                 prev_ch = 10;
1047                         } else if (ch=='A') {
1048                                 LineAdjustment=1-LineAdjustment;
1049                         } else if (ch=='Z') {
1050                                 //mprintf ((0,"Got a Z!\n"));
1051                                 GotZ=1;
1052 #if defined (D2_OEM) || defined(COMPILATION) || (defined(MACINTOSH) && defined(SHAREWARE))
1053                                 DumbAdjust=1;
1054 #else
1055                                 if (LineAdjustment==1)
1056                                         DumbAdjust=1;
1057                                 else
1058                                         DumbAdjust=2;
1059 #endif
1060
1061                                 i=0;
1062                                 while ((fname[i]=*message) != '\n') {
1063                                         i++;
1064                                         message++;
1065                                 }
1066                                 fname[i]=0;
1067
1068                                 if (MenuHires) {
1069                                         char fname2[15];
1070
1071                                         i=0;
1072                                         while (fname[i]!='.')
1073                                                 fname2[i] = fname[i++];
1074 #ifndef SHAREWARE
1075                                         fname2[i++]='b';
1076 #endif
1077                                         fname2[i++]='.';
1078                                         fname2[i++]='p';
1079                                         fname2[i++]='c';
1080                                         fname2[i++]='x';
1081                                         fname2[i++]=0;
1082
1083                                         load_new_briefing_screen (cfexist(fname2)?fname2:fname);
1084                                 } else
1085                                         load_new_briefing_screen (fname);
1086
1087                                 //load_new_briefing_screen (MenuHires?"end01b.pcx":"end01.pcx");
1088
1089                         } else if (ch == 'B') {
1090                                 char        bitmap_name[32];
1091                                 grs_bitmap  guy_bitmap;
1092                                 ubyte       temp_palette[768];
1093                                 int         iff_error;
1094
1095                                 if (Robot_canv != NULL) {
1096                                         d_free(Robot_canv);
1097                                         Robot_canv=NULL;
1098                                 }
1099
1100                                 get_message_name(&message, bitmap_name);
1101                                 strcat(bitmap_name, ".bbm");
1102                                 guy_bitmap.bm_data = NULL;
1103                                 iff_error = iff_read_bitmap(bitmap_name, &guy_bitmap, BM_LINEAR, temp_palette);
1104                                 Assert(iff_error == IFF_NO_ERROR);
1105                                 gr_remap_bitmap_good( &guy_bitmap, temp_palette, -1, -1 );
1106
1107                                 show_briefing_bitmap(&guy_bitmap);
1108                                 d_free(guy_bitmap.bm_data);
1109                                 prev_ch = 10;
1110 //                      } else if (ch==EOF) {
1111 //                              done=1;
1112 //                      } else if (ch == 'B') {
1113 //                              if (Robot_canv != NULL) {
1114 //                                      d_free(Robot_canv);
1115 //                                      Robot_canv=NULL;
1116 //                              }
1117 //
1118 //                              bitmap_num = get_message_num(&message);
1119 //                              if (bitmap_num != -1)
1120 //                                      show_briefing_bitmap(Textures[bitmap_num]);
1121 //                              prev_ch = 10;                           // read to eoln
1122                         } else if (ch == 'S') {
1123                                 int keypress;
1124                                 fix start_time;
1125
1126                                 chattering=0;
1127                                 if (printing_channel>-1)
1128                                         digi_stop_sound( printing_channel );
1129                                 printing_channel=-1;
1130
1131 #ifdef WINDOWS
1132                                 if (!wpage_done) {
1133                                         DDGRRESTORE;
1134                                         wpage_done =1;
1135                                 }
1136 #endif
1137
1138                                 start_time = timer_get_fixed_seconds();
1139                                 while ( (keypress = local_key_inkey()) == 0 ) {         //      Wait for a key
1140 #ifdef WINDOWS
1141                                         if (_RedrawScreen) {
1142                                                 _RedrawScreen = FALSE;
1143                                                 hum_channel  = digi_start_sound( digi_xlat_sound(SOUND_BRIEFING_HUM), F1_0/2, 0xFFFF/2, 1, -1, -1, -1 );
1144                                                 keypress = KEY_ESC;
1145                                                 break;
1146                                         }
1147 #endif
1148
1149                                         while (timer_get_fixed_seconds() < start_time + KEY_DELAY_DEFAULT/2)
1150                                                 ;
1151                                         flash_cursor(flashing_cursor);
1152
1153 #ifdef ROBOT_MOVIES
1154                                         if (RobotPlaying)
1155                                                 RotateRobot ();
1156 #else
1157                                         show_spinning_robot_frame(robot_num);
1158 #endif
1159
1160                                         show_bitmap_frame();
1161                                         start_time += KEY_DELAY_DEFAULT/2;
1162                                 }
1163
1164 #ifndef NDEBUG
1165                                 if (keypress == KEY_BACKSP)
1166                                         Int3();
1167 #endif
1168                                 if (keypress == KEY_ESC)
1169                                         rval = 1;
1170
1171                                 flashing_cursor = 0;
1172                                 done = 1;
1173                                 WIN(wpage_done = 0);
1174                         } else if (ch == 'P') {         //      New page.
1175                                 if (!GotZ) {
1176                                         Int3(); // Hey ryan!!!! You gotta load a screen before you start
1177                                                 // printing to it! You know, $Z !!!
1178                                     load_new_briefing_screen (MenuHires?"end01b.pcx":"end01.pcx");
1179                                 }
1180
1181                                 new_page = 1;
1182                                 while (*message != 10) {
1183                                         message++;      //      drop carriage return after special escape sequence
1184                                 }
1185                                 message++;
1186                                 prev_ch = 10;
1187                                 gr_update();
1188                         }
1189                 } else if (ch == '\t') {                //      Tab
1190                         if (Briefing_text_x - bsp->text_ulx < tab_stop)
1191                                 Briefing_text_x = bsp->text_ulx + tab_stop;
1192                 } else if ((ch == ';') && (prev_ch == 10)) {
1193                         while (*message++ != 10)
1194                                 ;
1195                         prev_ch = 10;
1196                 } else if (ch == '\\') {
1197                         prev_ch = ch;
1198                 } else if (ch == 10) {
1199                         if (prev_ch != '\\') {
1200                                 prev_ch = ch;
1201                                 if (1) //DumbAdjust==0)
1202                                         Briefing_text_y += (8*(MenuHires+1));
1203                                 else
1204                                         DumbAdjust--;
1205                                 Briefing_text_x = bsp->text_ulx;
1206                                 if (Briefing_text_y > bsp->text_uly + bsp->text_height) {
1207                                         load_briefing_screen(screen_num);
1208                                         Briefing_text_x = bsp->text_ulx;
1209                                         Briefing_text_y = bsp->text_uly;
1210                                 }
1211                         } else {
1212                                 if (ch == 13)           //Can this happen? Above says ch==10
1213                                         Int3();
1214                                 prev_ch = ch;
1215                         }
1216
1217                 } else {
1218                         if (!GotZ) {
1219                                 Int3(); // Hey ryan!!!! You gotta load a screen before you start
1220                                         // printing to it! You know, $Z !!!
1221                                 load_new_briefing_screen (MenuHires?"end01b.pcx":"end01.pcx");
1222                         }
1223
1224                         prev_ch = ch;
1225
1226                         if (!chattering) {
1227                                 printing_channel  = digi_start_sound( digi_xlat_sound(SOUND_BRIEFING_PRINTING), F1_0, 0xFFFF/2, 1, -1, -1, -1 );
1228                                 chattering=1;
1229                         }
1230
1231                         WIN(if (GRMODEINFO(emul)) delay_count = 0);
1232
1233                         Briefing_text_x += show_char_delay(ch, delay_count, robot_num, flashing_cursor);
1234
1235                 }
1236
1237                 //      Check for Esc -> abort.
1238                 if(delay_count)
1239                         key_check=local_key_inkey();
1240                 else
1241                         key_check=0;
1242
1243 #ifdef WINDOWS
1244                 if (_RedrawScreen) {
1245                         _RedrawScreen = FALSE;
1246                         hum_channel  = digi_start_sound( digi_xlat_sound(SOUND_BRIEFING_HUM), F1_0/2, 0xFFFF/2, 1, -1, -1, -1 );
1247                         key_check = KEY_ESC;
1248                 }
1249 #endif
1250                 if ( key_check == KEY_ESC ) {
1251                         rval = 1;
1252                         done = 1;
1253                 }
1254
1255                 if ((key_check == KEY_SPACEBAR) || (key_check == KEY_ENTER))
1256                         delay_count = 0;
1257
1258                 if (Briefing_text_x > bsp->text_ulx + bsp->text_width) {
1259                         Briefing_text_x = bsp->text_ulx;
1260                         Briefing_text_y += bsp->text_uly;
1261                 }
1262
1263                 if ((new_page) || (Briefing_text_y > bsp->text_uly + bsp->text_height)) {
1264                         fix     start_time = 0;
1265                         int     keypress;
1266
1267                         new_page = 0;
1268
1269                         if (printing_channel>-1)
1270                                 digi_stop_sound( printing_channel );
1271                         printing_channel=-1;
1272
1273                         chattering=0;
1274
1275 #ifdef WINDOWS
1276                         if (!wpage_done) {
1277                                 DDGRRESTORE;
1278                                 wpage_done =1;
1279                         }
1280 #endif
1281
1282                         start_time = timer_get_fixed_seconds();
1283                         while ( (keypress = local_key_inkey()) == 0 ) {         //      Wait for a key
1284 #ifdef WINDOWS
1285                                 if (_RedrawScreen) {
1286                                         _RedrawScreen = FALSE;
1287                                         hum_channel  = digi_start_sound( digi_xlat_sound(SOUND_BRIEFING_HUM), F1_0/2, 0xFFFF/2, 1, -1, -1, -1 );
1288                                         keypress = KEY_ESC;
1289                                         break;
1290                                 }
1291 #endif
1292
1293                                 while (timer_get_fixed_seconds() < start_time + KEY_DELAY_DEFAULT/2)
1294                                         ;
1295                                 flash_cursor(flashing_cursor);
1296 #ifdef ROBOT_MOVIES
1297                                 if (RobotPlaying)
1298                                         RotateRobot();
1299 #else
1300                                 show_spinning_robot_frame(robot_num);
1301 #endif
1302                                 show_bitmap_frame();
1303                                 start_time += KEY_DELAY_DEFAULT/2;
1304                         }
1305
1306 #ifdef ROBOT_MOVIES
1307                         if (RobotPlaying)
1308                                 DeInitRobotMovie();
1309                         RobotPlaying=0;
1310 #endif
1311                         robot_num = -1;
1312
1313 #ifndef NDEBUG
1314                         if (keypress == KEY_BACKSP)
1315                                 Int3();
1316 #endif
1317                         if (keypress == KEY_ESC) {
1318                                 rval = 1;
1319                                 done = 1;
1320                         }
1321
1322                         load_briefing_screen(screen_num);
1323                         Briefing_text_x = bsp->text_ulx;
1324                         Briefing_text_y = bsp->text_uly;
1325                         delay_count = KEY_DELAY_DEFAULT;
1326
1327                         WIN(wpage_done = 0);
1328                 }
1329         }
1330
1331 #ifdef ROBOT_MOVIES
1332         if (RobotPlaying) {
1333                 DeInitRobotMovie();
1334                 RobotPlaying=0;
1335         }
1336 #endif
1337
1338         if (Robot_canv != NULL)
1339                 {d_free(Robot_canv); Robot_canv=NULL;}
1340
1341         if (hum_channel>-1)
1342                 digi_stop_sound( hum_channel );
1343         if (printing_channel>-1)
1344                 digi_stop_sound( printing_channel );
1345
1346         return rval;
1347 }
1348
1349 //-----------------------------------------------------------------------------
1350 // Return a pointer to the start of text for screen #screen_num.
1351 char * get_briefing_message(int screen_num)
1352 {
1353         char *tptr = Briefing_text;
1354         int     cur_screen=0;
1355         int     ch;
1356
1357         Assert(screen_num >= 0);
1358
1359         while ( (*tptr != 0 ) && (screen_num != cur_screen)) {
1360                 ch = *tptr++;
1361                 if (ch == '$') {
1362                         ch = *tptr++;
1363                         if (ch == 'S')
1364                                 cur_screen = get_message_num(&tptr);
1365                 }
1366         }
1367
1368         if (screen_num!=cur_screen)
1369                 return (NULL);
1370
1371         return tptr;
1372 }
1373
1374 //-----------------------------------------------------------------------------
1375 //      Load Descent briefing text.
1376 int load_screen_text(char *filename, char **buf)
1377 {
1378         CFILE *tfile;
1379         CFILE *ifile;
1380         int     len, i,x;
1381         int     have_binary = 0;
1382
1383         if ((tfile = cfopen(filename,"rb")) == NULL) {
1384                 char nfilename[30], *ptr;
1385
1386                 strcpy(nfilename, filename);
1387                 ptr = strrchr(nfilename, '.');
1388                 *ptr = '\0';
1389                 strcat(nfilename, ".txb");
1390                 if ((ifile = cfopen(nfilename, "rb")) == NULL) {
1391                         mprintf ((0,"can't open %s!\n",nfilename));
1392                         return (0);
1393                                 //Error("Cannot open file %s or %s", filename, nfilename);
1394                 }
1395
1396                 mprintf ((0,"reading...\n"));
1397                 have_binary = 1;
1398
1399                 len = cfilelength(ifile);
1400                 MALLOC(*buf, char, len+500);
1401                 mprintf ((0,"len=%d\n",len));
1402                 for (x=0, i=0; i < len; i++, x++) {
1403                         cfread (*buf+x,1,1,ifile);
1404                         //  mprintf ((0,"%c",*(*buf+x)));
1405                         if (*(*buf+x)==13)
1406                                 x--;
1407                 }
1408
1409                 cfclose(ifile);
1410         } else {
1411                 len = cfilelength(tfile);
1412                 MALLOC(*buf, char, len+500);
1413                 for (x=0, i=0; i < len; i++, x++) {
1414                         cfread (*buf+x,1,1,tfile);
1415                         // mprintf ((0,"%c",*(*buf+x)));
1416                         if (*(*buf+x)==13)
1417                                 x--;
1418                 }
1419
1420
1421                 //cfread(*buf, 1, len, tfile);
1422                 cfclose(tfile);
1423         }
1424
1425         if (have_binary) {
1426                 char *ptr;
1427
1428                 for (i = 0, ptr = *buf; i < len; i++, ptr++) {
1429                         if (*ptr != '\n') {
1430                                 encode_rotate_left(ptr);
1431                                 *ptr = *ptr ^ BITMAP_TBL_XOR;
1432                                 encode_rotate_left(ptr);
1433                         }
1434                 }
1435         }
1436
1437         return (1);
1438 }
1439
1440 //-----------------------------------------------------------------------------
1441 // Return true if message got aborted, else return false.
1442 int show_briefing_text(int screen_num)
1443 {
1444         char    *message_ptr;
1445
1446         message_ptr = get_briefing_message(screen_num);
1447         if (message_ptr==NULL)
1448                 return (0);
1449
1450         DoBriefingColorStuff();
1451
1452         if (Mission_list[Current_mission_num].descent_version == 1)
1453                 return show_d1_briefing_message(screen_num, message_ptr);
1454         else
1455                 return show_briefing_message(screen_num, message_ptr);
1456 }
1457
1458 void DoBriefingColorStuff ()
1459 {
1460         Briefing_foreground_colors[0] = gr_find_closest_color_current( 0, 40, 0);
1461         Briefing_background_colors[0] = gr_find_closest_color_current( 0, 6, 0);
1462
1463         Briefing_foreground_colors[1] = gr_find_closest_color_current( 40, 33, 35);
1464         Briefing_background_colors[1] = gr_find_closest_color_current( 5, 5, 5);
1465
1466         Briefing_foreground_colors[2] = gr_find_closest_color_current( 8, 31, 54);
1467         Briefing_background_colors[2] = gr_find_closest_color_current( 1, 4, 7);
1468
1469         Erase_color = gr_find_closest_color_current(0, 0, 0);
1470 }
1471
1472 //-----------------------------------------------------------------------------
1473 // Return true if screen got aborted by user, else return false.
1474 int show_briefing_screen( int screen_num, int allow_keys)
1475 {
1476         int     rval=0;
1477         ubyte   palette_save[768];
1478
1479         New_pal_254_bash = 0;
1480
1481         if (Skip_briefing_screens) {
1482                 mprintf((0, "Skipping briefing screen [brief03.pcx]\n"));
1483                 return 0;
1484         }
1485
1486 //      briefing_bm.bm_data=NULL;
1487 //   if ((pcx_error=pcx_read_bitmap( "brief03.pcx", &briefing_bm, BM_LINEAR, New_pal ))!=PCX_ERROR_NONE)     {
1488 //        mprintf((0, "File '%s', PCX load error: %s (%i)\n  (It's a briefing screen.  Does this cause you pain?)\n","Brief03.pcx", pcx_errormsg(pcx_error), pcx_error));
1489 //              Int3();
1490 //              return 0;
1491 //      }
1492
1493         memcpy(palette_save,gr_palette,sizeof(palette_save));
1494         memcpy(New_pal,gr_palette,sizeof(gr_palette));
1495
1496
1497 //      vfx_set_palette_sub( New_pal );
1498 //      gr_palette_clear();
1499 //      gr_bitmap( 0, 0, &briefing_bm );
1500 #ifdef OGL
1501         gr_palette_load(New_pal);
1502 #endif
1503
1504 //      if (gr_palette_fade_in( New_pal, 32, allow_keys ))
1505 //              return 1;
1506 //      memcpy(gr_palette,New_pal,sizeof(gr_palette));
1507
1508         #ifdef MACINTOSH
1509         key_close();            // kill the keyboard handler during briefing screens for movies
1510         #endif
1511         rval = show_briefing_text(screen_num);
1512         #ifdef MACINTOSH
1513         key_init();
1514         #endif
1515
1516         #if defined (MACINTOSH) || defined(WINDOWS)
1517         memcpy(New_pal,gr_palette,sizeof(gr_palette));          // attempt to get fades after briefing screens done correctly.
1518         #endif
1519
1520
1521 #ifndef WINDOWS
1522         if (gr_palette_fade_out( New_pal, 32, allow_keys ))
1523                 return 1;
1524 #else
1525                 DEFINE_SCREEN(NULL);
1526                 WIN(DDGRLOCK(dd_grd_curcanv));
1527                 gr_clear_canvas (0);
1528                 WIN(DDGRUNLOCK(dd_grd_curcanv));
1529                 if (gr_palette_fade_out( New_pal, 32, allow_keys ))
1530                         return 1;
1531 #endif
1532
1533         gr_copy_palette(gr_palette, palette_save, sizeof(palette_save));
1534
1535 //      d_free(briefing_bm.bm_data);
1536
1537         return rval;
1538 }
1539
1540
1541 //-----------------------------------------------------------------------------
1542 void do_briefing_screens(char *filename,int level_num)
1543 {
1544         int     abort_briefing_screens = 0;
1545         int     cur_briefing_screen = 0;
1546
1547         MVEPaletteCalls=0;
1548
1549         if (Skip_briefing_screens) {
1550                 mprintf((0, "Skipping all briefing screens.\n"));
1551                 return;
1552         }
1553
1554         #ifdef APPLE_DEMO
1555         return;                 // no briefing screens at all for demo
1556
1557         #endif
1558
1559         mprintf ((0,"Trying briefing screen! %s\n",filename));
1560
1561         if (!filename)
1562                 return;
1563
1564         if (!load_screen_text(filename, &Briefing_text))
1565                 return;
1566
1567         #ifdef SHAREWARE
1568         songs_play_song( SONG_BRIEFING, 1 );
1569         #else
1570         songs_stop_all();
1571         #endif
1572
1573         set_screen_mode( SCREEN_MENU );
1574
1575         WINDOS(
1576                 dd_gr_set_current_canvas(NULL),
1577                 gr_set_current_canvas(NULL)
1578         );
1579
1580         mprintf ((0,"Playing briefing screen! %s %d\n",filename,level_num));
1581
1582         key_flush();
1583
1584         if (Mission_list[Current_mission_num].descent_version == 1) {
1585                 if (level_num == 1) {
1586                         while ((!abort_briefing_screens) && (Briefing_screens[cur_briefing_screen].level_num == 0)) {
1587                                 abort_briefing_screens = show_briefing_screen(cur_briefing_screen, 0);
1588                                 cur_briefing_screen++;
1589                         }
1590                 }
1591
1592                 if (!abort_briefing_screens) {
1593                         for (cur_briefing_screen = 0; cur_briefing_screen < MAX_BRIEFING_SCREENS; cur_briefing_screen++)
1594                                 if (Briefing_screens[cur_briefing_screen].level_num == level_num)
1595                                         if (show_briefing_screen(cur_briefing_screen, 0))
1596                                                 break;
1597                 }
1598
1599         } else
1600                 show_briefing_screen(level_num,0);
1601
1602         d_free (Briefing_text);
1603         key_flush();
1604
1605         return;
1606
1607 }
1608
1609 int DefineBriefingBox (char **buf)
1610 {
1611         int n,i=0;
1612         char name[20];
1613
1614         n=get_new_message_num (buf);
1615
1616         Assert(n < MAX_BRIEFING_SCREENS);
1617
1618         while (**buf!=' ') {
1619                 name[i++]=**buf;
1620                 (*buf)++;
1621         }
1622
1623         name[i]='\0';   // slap a delimiter on this guy
1624
1625         strcpy (Briefing_screens[n].bs_name,name);
1626         Briefing_screens[n].level_num=get_new_message_num (buf);
1627         Briefing_screens[n].message_num=get_new_message_num (buf);
1628         Briefing_screens[n].text_ulx=get_new_message_num (buf);
1629         Briefing_screens[n].text_uly=get_new_message_num (buf);
1630         Briefing_screens[n].text_width=get_new_message_num (buf);
1631         Briefing_screens[n].text_height=get_message_num (buf);  // NOTICE!!!
1632
1633         if (MenuHires) {
1634                 Briefing_screens[n].text_ulx*=2;
1635                 Briefing_screens[n].text_uly*=2.4;
1636                 Briefing_screens[n].text_width*=2;
1637                 Briefing_screens[n].text_height*=2.4;
1638         }
1639
1640         return (n);
1641 }
1642
1643 int get_new_message_num(char **message)
1644 {
1645         int     num=0;
1646
1647         while (**message == ' ')
1648                 (*message)++;
1649
1650         while ((**message >= '0') && (**message <= '9')) {
1651                 num = 10*num + **message-'0';
1652                 (*message)++;
1653         }
1654
1655        (*message)++;
1656
1657         return num;
1658 }
1659