]> icculus.org git repositories - btb/d2x.git/blob - main/titles.c
gcc 3.2 warnings
[btb/d2x.git] / main / titles.c
1 /* $Id: titles.c,v 1.17 2003-01-11 02:57:39 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                                 {
1069                                         char fname2[15];
1070
1071                                         i=0;
1072                                         while (fname[i]!='.') {
1073                                                 fname2[i] = fname[i];
1074                                                 i++;
1075                                         }
1076 #ifndef SHAREWARE
1077                                         fname2[i++]='b';
1078 #endif
1079                                         fname2[i++]='.';
1080                                         fname2[i++]='p';
1081                                         fname2[i++]='c';
1082                                         fname2[i++]='x';
1083                                         fname2[i++]=0;
1084
1085                                         if ((MenuHires && cfexist(fname2)) || !cfexist(fname))
1086                                                 load_new_briefing_screen (fname2);
1087                                         else
1088                                                 load_new_briefing_screen (fname);
1089                                 }
1090
1091                                 //load_new_briefing_screen (MenuHires?"end01b.pcx":"end01.pcx");
1092
1093                         } else if (ch == 'B') {
1094                                 char        bitmap_name[32];
1095                                 grs_bitmap  guy_bitmap;
1096                                 ubyte       temp_palette[768];
1097                                 int         iff_error;
1098
1099                                 if (Robot_canv != NULL) {
1100                                         d_free(Robot_canv);
1101                                         Robot_canv=NULL;
1102                                 }
1103
1104                                 get_message_name(&message, bitmap_name);
1105                                 strcat(bitmap_name, ".bbm");
1106                                 guy_bitmap.bm_data = NULL;
1107                                 iff_error = iff_read_bitmap(bitmap_name, &guy_bitmap, BM_LINEAR, temp_palette);
1108                                 Assert(iff_error == IFF_NO_ERROR);
1109                                 gr_remap_bitmap_good( &guy_bitmap, temp_palette, -1, -1 );
1110
1111                                 show_briefing_bitmap(&guy_bitmap);
1112                                 d_free(guy_bitmap.bm_data);
1113                                 prev_ch = 10;
1114 //                      } else if (ch==EOF) {
1115 //                              done=1;
1116 //                      } else if (ch == 'B') {
1117 //                              if (Robot_canv != NULL) {
1118 //                                      d_free(Robot_canv);
1119 //                                      Robot_canv=NULL;
1120 //                              }
1121 //
1122 //                              bitmap_num = get_message_num(&message);
1123 //                              if (bitmap_num != -1)
1124 //                                      show_briefing_bitmap(Textures[bitmap_num]);
1125 //                              prev_ch = 10;                           // read to eoln
1126                         } else if (ch == 'S') {
1127                                 int keypress;
1128                                 fix start_time;
1129
1130                                 chattering=0;
1131                                 if (printing_channel>-1)
1132                                         digi_stop_sound( printing_channel );
1133                                 printing_channel=-1;
1134
1135 #ifdef WINDOWS
1136                                 if (!wpage_done) {
1137                                         DDGRRESTORE;
1138                                         wpage_done =1;
1139                                 }
1140 #endif
1141
1142                                 start_time = timer_get_fixed_seconds();
1143                                 while ( (keypress = local_key_inkey()) == 0 ) {         //      Wait for a key
1144 #ifdef WINDOWS
1145                                         if (_RedrawScreen) {
1146                                                 _RedrawScreen = FALSE;
1147                                                 hum_channel  = digi_start_sound( digi_xlat_sound(SOUND_BRIEFING_HUM), F1_0/2, 0xFFFF/2, 1, -1, -1, -1 );
1148                                                 keypress = KEY_ESC;
1149                                                 break;
1150                                         }
1151 #endif
1152
1153                                         while (timer_get_fixed_seconds() < start_time + KEY_DELAY_DEFAULT/2)
1154                                                 ;
1155                                         flash_cursor(flashing_cursor);
1156
1157 #ifdef ROBOT_MOVIES
1158                                         if (RobotPlaying)
1159                                                 RotateRobot ();
1160 #else
1161                                         show_spinning_robot_frame(robot_num);
1162 #endif
1163
1164                                         show_bitmap_frame();
1165                                         start_time += KEY_DELAY_DEFAULT/2;
1166                                 }
1167
1168 #ifndef NDEBUG
1169                                 if (keypress == KEY_BACKSP)
1170                                         Int3();
1171 #endif
1172                                 if (keypress == KEY_ESC)
1173                                         rval = 1;
1174
1175                                 flashing_cursor = 0;
1176                                 done = 1;
1177                                 WIN(wpage_done = 0);
1178                         } else if (ch == 'P') {         //      New page.
1179                                 if (!GotZ) {
1180                                         Int3(); // Hey ryan!!!! You gotta load a screen before you start
1181                                                 // printing to it! You know, $Z !!!
1182                                     load_new_briefing_screen (MenuHires?"end01b.pcx":"end01.pcx");
1183                                 }
1184
1185                                 new_page = 1;
1186                                 while (*message != 10) {
1187                                         message++;      //      drop carriage return after special escape sequence
1188                                 }
1189                                 message++;
1190                                 prev_ch = 10;
1191                                 gr_update();
1192                         }
1193                 } else if (ch == '\t') {                //      Tab
1194                         if (Briefing_text_x - bsp->text_ulx < tab_stop)
1195                                 Briefing_text_x = bsp->text_ulx + tab_stop;
1196                 } else if ((ch == ';') && (prev_ch == 10)) {
1197                         while (*message++ != 10)
1198                                 ;
1199                         prev_ch = 10;
1200                 } else if (ch == '\\') {
1201                         prev_ch = ch;
1202                 } else if (ch == 10) {
1203                         if (prev_ch != '\\') {
1204                                 prev_ch = ch;
1205                                 if (1) //DumbAdjust==0)
1206                                         Briefing_text_y += (8*(MenuHires+1));
1207                                 else
1208                                         DumbAdjust--;
1209                                 Briefing_text_x = bsp->text_ulx;
1210                                 if (Briefing_text_y > bsp->text_uly + bsp->text_height) {
1211                                         load_briefing_screen(screen_num);
1212                                         Briefing_text_x = bsp->text_ulx;
1213                                         Briefing_text_y = bsp->text_uly;
1214                                 }
1215                         } else {
1216                                 if (ch == 13)           //Can this happen? Above says ch==10
1217                                         Int3();
1218                                 prev_ch = ch;
1219                         }
1220
1221                 } else {
1222                         if (!GotZ) {
1223                                 Int3(); // Hey ryan!!!! You gotta load a screen before you start
1224                                         // printing to it! You know, $Z !!!
1225                                 load_new_briefing_screen (MenuHires?"end01b.pcx":"end01.pcx");
1226                         }
1227
1228                         prev_ch = ch;
1229
1230                         if (!chattering) {
1231                                 printing_channel  = digi_start_sound( digi_xlat_sound(SOUND_BRIEFING_PRINTING), F1_0, 0xFFFF/2, 1, -1, -1, -1 );
1232                                 chattering=1;
1233                         }
1234
1235                         WIN(if (GRMODEINFO(emul)) delay_count = 0);
1236
1237                         Briefing_text_x += show_char_delay(ch, delay_count, robot_num, flashing_cursor);
1238
1239                 }
1240
1241                 //      Check for Esc -> abort.
1242                 if(delay_count)
1243                         key_check=local_key_inkey();
1244                 else
1245                         key_check=0;
1246
1247 #ifdef WINDOWS
1248                 if (_RedrawScreen) {
1249                         _RedrawScreen = FALSE;
1250                         hum_channel  = digi_start_sound( digi_xlat_sound(SOUND_BRIEFING_HUM), F1_0/2, 0xFFFF/2, 1, -1, -1, -1 );
1251                         key_check = KEY_ESC;
1252                 }
1253 #endif
1254                 if ( key_check == KEY_ESC ) {
1255                         rval = 1;
1256                         done = 1;
1257                 }
1258
1259                 if ((key_check == KEY_SPACEBAR) || (key_check == KEY_ENTER))
1260                         delay_count = 0;
1261
1262                 if (Briefing_text_x > bsp->text_ulx + bsp->text_width) {
1263                         Briefing_text_x = bsp->text_ulx;
1264                         Briefing_text_y += bsp->text_uly;
1265                 }
1266
1267                 if ((new_page) || (Briefing_text_y > bsp->text_uly + bsp->text_height)) {
1268                         fix     start_time = 0;
1269                         int     keypress;
1270
1271                         new_page = 0;
1272
1273                         if (printing_channel>-1)
1274                                 digi_stop_sound( printing_channel );
1275                         printing_channel=-1;
1276
1277                         chattering=0;
1278
1279 #ifdef WINDOWS
1280                         if (!wpage_done) {
1281                                 DDGRRESTORE;
1282                                 wpage_done =1;
1283                         }
1284 #endif
1285
1286                         start_time = timer_get_fixed_seconds();
1287                         while ( (keypress = local_key_inkey()) == 0 ) {         //      Wait for a key
1288 #ifdef WINDOWS
1289                                 if (_RedrawScreen) {
1290                                         _RedrawScreen = FALSE;
1291                                         hum_channel  = digi_start_sound( digi_xlat_sound(SOUND_BRIEFING_HUM), F1_0/2, 0xFFFF/2, 1, -1, -1, -1 );
1292                                         keypress = KEY_ESC;
1293                                         break;
1294                                 }
1295 #endif
1296
1297                                 while (timer_get_fixed_seconds() < start_time + KEY_DELAY_DEFAULT/2)
1298                                         ;
1299                                 flash_cursor(flashing_cursor);
1300 #ifdef ROBOT_MOVIES
1301                                 if (RobotPlaying)
1302                                         RotateRobot();
1303 #else
1304                                 show_spinning_robot_frame(robot_num);
1305 #endif
1306                                 show_bitmap_frame();
1307                                 start_time += KEY_DELAY_DEFAULT/2;
1308                         }
1309
1310 #ifdef ROBOT_MOVIES
1311                         if (RobotPlaying)
1312                                 DeInitRobotMovie();
1313                         RobotPlaying=0;
1314 #endif
1315                         robot_num = -1;
1316
1317 #ifndef NDEBUG
1318                         if (keypress == KEY_BACKSP)
1319                                 Int3();
1320 #endif
1321                         if (keypress == KEY_ESC) {
1322                                 rval = 1;
1323                                 done = 1;
1324                         }
1325
1326                         load_briefing_screen(screen_num);
1327                         Briefing_text_x = bsp->text_ulx;
1328                         Briefing_text_y = bsp->text_uly;
1329                         delay_count = KEY_DELAY_DEFAULT;
1330
1331                         WIN(wpage_done = 0);
1332                 }
1333         }
1334
1335 #ifdef ROBOT_MOVIES
1336         if (RobotPlaying) {
1337                 DeInitRobotMovie();
1338                 RobotPlaying=0;
1339         }
1340 #endif
1341
1342         if (Robot_canv != NULL)
1343                 {d_free(Robot_canv); Robot_canv=NULL;}
1344
1345         if (hum_channel>-1)
1346                 digi_stop_sound( hum_channel );
1347         if (printing_channel>-1)
1348                 digi_stop_sound( printing_channel );
1349
1350         return rval;
1351 }
1352
1353 //-----------------------------------------------------------------------------
1354 // Return a pointer to the start of text for screen #screen_num.
1355 char * get_briefing_message(int screen_num)
1356 {
1357         char *tptr = Briefing_text;
1358         int     cur_screen=0;
1359         int     ch;
1360
1361         Assert(screen_num >= 0);
1362
1363         while ( (*tptr != 0 ) && (screen_num != cur_screen)) {
1364                 ch = *tptr++;
1365                 if (ch == '$') {
1366                         ch = *tptr++;
1367                         if (ch == 'S')
1368                                 cur_screen = get_message_num(&tptr);
1369                 }
1370         }
1371
1372         if (screen_num!=cur_screen)
1373                 return (NULL);
1374
1375         return tptr;
1376 }
1377
1378 //-----------------------------------------------------------------------------
1379 //      Load Descent briefing text.
1380 int load_screen_text(char *filename, char **buf)
1381 {
1382         CFILE *tfile;
1383         CFILE *ifile;
1384         int     len, i,x;
1385         int     have_binary = 0;
1386
1387         if ((tfile = cfopen(filename,"rb")) == NULL) {
1388                 char nfilename[30], *ptr;
1389
1390                 strcpy(nfilename, filename);
1391                 ptr = strrchr(nfilename, '.');
1392                 *ptr = '\0';
1393                 strcat(nfilename, ".txb");
1394                 if ((ifile = cfopen(nfilename, "rb")) == NULL) {
1395                         mprintf ((0,"can't open %s!\n",nfilename));
1396                         return (0);
1397                                 //Error("Cannot open file %s or %s", filename, nfilename);
1398                 }
1399
1400                 mprintf ((0,"reading...\n"));
1401                 have_binary = 1;
1402
1403                 len = cfilelength(ifile);
1404                 MALLOC(*buf, char, len+500);
1405                 mprintf ((0,"len=%d\n",len));
1406                 for (x=0, i=0; i < len; i++, x++) {
1407                         cfread (*buf+x,1,1,ifile);
1408                         //  mprintf ((0,"%c",*(*buf+x)));
1409                         if (*(*buf+x)==13)
1410                                 x--;
1411                 }
1412
1413                 cfclose(ifile);
1414         } else {
1415                 len = cfilelength(tfile);
1416                 MALLOC(*buf, char, len+500);
1417                 for (x=0, i=0; i < len; i++, x++) {
1418                         cfread (*buf+x,1,1,tfile);
1419                         // mprintf ((0,"%c",*(*buf+x)));
1420                         if (*(*buf+x)==13)
1421                                 x--;
1422                 }
1423
1424
1425                 //cfread(*buf, 1, len, tfile);
1426                 cfclose(tfile);
1427         }
1428
1429         if (have_binary) {
1430                 char *ptr;
1431
1432                 for (i = 0, ptr = *buf; i < len; i++, ptr++) {
1433                         if (*ptr != '\n') {
1434                                 encode_rotate_left(ptr);
1435                                 *ptr = *ptr ^ BITMAP_TBL_XOR;
1436                                 encode_rotate_left(ptr);
1437                         }
1438                 }
1439         }
1440
1441         return (1);
1442 }
1443
1444 //-----------------------------------------------------------------------------
1445 // Return true if message got aborted, else return false.
1446 int show_briefing_text(int screen_num)
1447 {
1448         char    *message_ptr;
1449
1450         message_ptr = get_briefing_message(screen_num);
1451         if (message_ptr==NULL)
1452                 return (0);
1453
1454         DoBriefingColorStuff();
1455
1456         if (Mission_list[Current_mission_num].descent_version == 1)
1457                 return show_d1_briefing_message(screen_num, message_ptr);
1458         else
1459                 return show_briefing_message(screen_num, message_ptr);
1460 }
1461
1462 void DoBriefingColorStuff ()
1463 {
1464         Briefing_foreground_colors[0] = gr_find_closest_color_current( 0, 40, 0);
1465         Briefing_background_colors[0] = gr_find_closest_color_current( 0, 6, 0);
1466
1467         Briefing_foreground_colors[1] = gr_find_closest_color_current( 40, 33, 35);
1468         Briefing_background_colors[1] = gr_find_closest_color_current( 5, 5, 5);
1469
1470         Briefing_foreground_colors[2] = gr_find_closest_color_current( 8, 31, 54);
1471         Briefing_background_colors[2] = gr_find_closest_color_current( 1, 4, 7);
1472
1473         Erase_color = gr_find_closest_color_current(0, 0, 0);
1474 }
1475
1476 //-----------------------------------------------------------------------------
1477 // Return true if screen got aborted by user, else return false.
1478 int show_briefing_screen( int screen_num, int allow_keys)
1479 {
1480         int     rval=0;
1481         ubyte   palette_save[768];
1482
1483         New_pal_254_bash = 0;
1484
1485         if (Skip_briefing_screens) {
1486                 mprintf((0, "Skipping briefing screen [brief03.pcx]\n"));
1487                 return 0;
1488         }
1489
1490 //      briefing_bm.bm_data=NULL;
1491 //   if ((pcx_error=pcx_read_bitmap( "brief03.pcx", &briefing_bm, BM_LINEAR, New_pal ))!=PCX_ERROR_NONE)     {
1492 //        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));
1493 //              Int3();
1494 //              return 0;
1495 //      }
1496
1497         memcpy(palette_save,gr_palette,sizeof(palette_save));
1498         memcpy(New_pal,gr_palette,sizeof(gr_palette));
1499
1500
1501 //      vfx_set_palette_sub( New_pal );
1502 //      gr_palette_clear();
1503 //      gr_bitmap( 0, 0, &briefing_bm );
1504 #ifdef OGL
1505         gr_palette_load(New_pal);
1506 #endif
1507
1508 //      if (gr_palette_fade_in( New_pal, 32, allow_keys ))
1509 //              return 1;
1510 //      memcpy(gr_palette,New_pal,sizeof(gr_palette));
1511
1512         #ifdef MACINTOSH
1513         key_close();            // kill the keyboard handler during briefing screens for movies
1514         #endif
1515         rval = show_briefing_text(screen_num);
1516         #ifdef MACINTOSH
1517         key_init();
1518         #endif
1519
1520         #if defined (MACINTOSH) || defined(WINDOWS)
1521         memcpy(New_pal,gr_palette,sizeof(gr_palette));          // attempt to get fades after briefing screens done correctly.
1522         #endif
1523
1524
1525 #ifndef WINDOWS
1526         if (gr_palette_fade_out( New_pal, 32, allow_keys ))
1527                 return 1;
1528 #else
1529                 DEFINE_SCREEN(NULL);
1530                 WIN(DDGRLOCK(dd_grd_curcanv));
1531                 gr_clear_canvas (0);
1532                 WIN(DDGRUNLOCK(dd_grd_curcanv));
1533                 if (gr_palette_fade_out( New_pal, 32, allow_keys ))
1534                         return 1;
1535 #endif
1536
1537         gr_copy_palette(gr_palette, palette_save, sizeof(palette_save));
1538
1539 //      d_free(briefing_bm.bm_data);
1540
1541         return rval;
1542 }
1543
1544
1545 //-----------------------------------------------------------------------------
1546 void do_briefing_screens(char *filename,int level_num)
1547 {
1548         int     abort_briefing_screens = 0;
1549         int     cur_briefing_screen = 0;
1550
1551         MVEPaletteCalls=0;
1552
1553         if (Skip_briefing_screens) {
1554                 mprintf((0, "Skipping all briefing screens.\n"));
1555                 return;
1556         }
1557
1558         #ifdef APPLE_DEMO
1559         return;                 // no briefing screens at all for demo
1560
1561         #endif
1562
1563         mprintf ((0,"Trying briefing screen <%s>\n",filename));
1564
1565         if (!filename)
1566                 return;
1567
1568         if (!load_screen_text(filename, &Briefing_text))
1569                 return;
1570
1571         #ifdef SHAREWARE
1572         songs_play_song( SONG_BRIEFING, 1 );
1573         #else
1574         songs_stop_all();
1575         #endif
1576
1577         set_screen_mode( SCREEN_MENU );
1578
1579         WINDOS(
1580                 dd_gr_set_current_canvas(NULL),
1581                 gr_set_current_canvas(NULL)
1582         );
1583
1584         mprintf ((0,"Playing briefing screen <%s>, level %d\n",filename,level_num));
1585
1586         key_flush();
1587
1588         if (Mission_list[Current_mission_num].descent_version == 1) {
1589                 if (level_num == 1) {
1590                         while ((!abort_briefing_screens) && (Briefing_screens[cur_briefing_screen].level_num == 0)) {
1591                                 abort_briefing_screens = show_briefing_screen(cur_briefing_screen, 0);
1592                                 cur_briefing_screen++;
1593                         }
1594                 }
1595
1596                 if (!abort_briefing_screens) {
1597                         for (cur_briefing_screen = 0; cur_briefing_screen < MAX_BRIEFING_SCREENS; cur_briefing_screen++)
1598                                 if (Briefing_screens[cur_briefing_screen].level_num == level_num)
1599                                         if (show_briefing_screen(cur_briefing_screen, 0))
1600                                                 break;
1601                 }
1602
1603         } else
1604                 show_briefing_screen(level_num,0);
1605
1606         d_free (Briefing_text);
1607         key_flush();
1608
1609         return;
1610
1611 }
1612
1613 int DefineBriefingBox (char **buf)
1614 {
1615         int n,i=0;
1616         char name[20];
1617
1618         n=get_new_message_num (buf);
1619
1620         Assert(n < MAX_BRIEFING_SCREENS);
1621
1622         while (**buf!=' ') {
1623                 name[i++]=**buf;
1624                 (*buf)++;
1625         }
1626
1627         name[i]='\0';   // slap a delimiter on this guy
1628
1629         strcpy (Briefing_screens[n].bs_name,name);
1630         Briefing_screens[n].level_num=get_new_message_num (buf);
1631         Briefing_screens[n].message_num=get_new_message_num (buf);
1632         Briefing_screens[n].text_ulx=get_new_message_num (buf);
1633         Briefing_screens[n].text_uly=get_new_message_num (buf);
1634         Briefing_screens[n].text_width=get_new_message_num (buf);
1635         Briefing_screens[n].text_height=get_message_num (buf);  // NOTICE!!!
1636
1637         if (MenuHires) {
1638                 Briefing_screens[n].text_ulx*=2;
1639                 Briefing_screens[n].text_uly*=2.4;
1640                 Briefing_screens[n].text_width*=2;
1641                 Briefing_screens[n].text_height*=2.4;
1642         }
1643
1644         return (n);
1645 }
1646
1647 int get_new_message_num(char **message)
1648 {
1649         int     num=0;
1650
1651         while (**message == ' ')
1652                 (*message)++;
1653
1654         while ((**message >= '0') && (**message <= '9')) {
1655                 num = 10*num + **message-'0';
1656                 (*message)++;
1657         }
1658
1659        (*message)++;
1660
1661         return num;
1662 }
1663