]> icculus.org git repositories - btb/d2x.git/blob - main/titles.c
added TODO file
[btb/d2x.git] / main / titles.c
1 /* $Id: titles.c,v 1.13 2002-08-27 08:03:25 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:        Int3(); //      Impossible, illegal value for Animating_bitmap_type
344                 }
345
346                 WINDOS(
347                         curcanv_save = dd_grd_curcanv; dd_grd_curcanv = bitmap_canv,
348                         curcanv_save = grd_curcanv; grd_curcanv = bitmap_canv
349                 );
350
351                 pound_signp = strchr(Bitmap_name, '#');
352                 Assert(pound_signp != NULL);
353
354                 dig1 = *(pound_signp+1);
355                 dig2 = *(pound_signp+2);
356                 if (dig2 == 0)
357                         num = dig1-'0';
358                 else
359                         num = (dig1-'0')*10 + (dig2-'0');
360
361                 switch (Animating_bitmap_type) {
362                         case 0:
363                                 num += Door_dir;
364                                 if (num > EXIT_DOOR_MAX) {
365                                         num = EXIT_DOOR_MAX;
366                                         Door_dir = -1;
367                                 } else if (num < 0) {
368                                         num = 0;
369                                         Door_dir = 1;
370                                 }
371                                 break;
372                         case 1:
373                                 num++;
374                                 if (num > OTHER_THING_MAX)
375                                         num = 0;
376                                 break;
377                 }
378
379                 Assert(num < 100);
380                 if (num >= 10) {
381                         *(pound_signp+1) = (num / 10) + '0';
382                         *(pound_signp+2) = (num % 10) + '0';
383                         *(pound_signp+3) = 0;
384                 } else {
385                         *(pound_signp+1) = (num % 10) + '0';
386                         *(pound_signp+2) = 0;
387                 }
388
389                 {
390                         bitmap_index bi;
391                         bi = piggy_find_bitmap(Bitmap_name);
392                         bitmap_ptr = &GameBitmaps[bi.index];
393                         PIGGY_PAGE_IN( bi );
394                 }
395
396                 WIN(DDGRLOCK(dd_grd_curcanv));
397                 gr_bitmapm(0, 0, bitmap_ptr);
398                 WIN(DDGRUNLOCK(dd_grd_curcanv));
399
400                 WINDOS(
401                         dd_grd_curcanv = curcanv_save,
402                         grd_curcanv = curcanv_save
403                 );
404                 d_free(bitmap_canv);
405
406                 switch (Animating_bitmap_type) {
407                         case 0:
408                                 if (num == EXIT_DOOR_MAX) {
409                                         Door_dir = -1;
410                                         Door_div_count = 64;
411                                 } else if (num == 0) {
412                                         Door_dir = 1;
413                                         Door_div_count = 64;
414                                 }
415                                 break;
416                         case 1:
417                                 break;
418                 }
419         }
420
421 }
422
423 //      -----------------------------------------------------------------------------
424 void show_briefing_bitmap(grs_bitmap *bmp)
425 {
426 #ifdef WINDOWS
427         dd_grs_canvas *bitmap_canv, *curcanv_save;
428   
429         bitmap_canv = dd_gr_create_sub_canvas(dd_grd_curcanv, 220, 45, bmp->bm_w, bmp->bm_h); 
430         curcanv_save = dd_grd_curcanv;
431         dd_gr_set_current_canvas(bitmap_canv);
432         DDGRLOCK(dd_grd_curcanv);
433         gr_bitmapm(0,0,bmp);
434         DDGRUNLOCK(dd_grd_curcanv);
435         dd_gr_set_current_canvas(curcanv_save);
436 #else
437         grs_canvas      *curcanv_save, *bitmap_canv;
438
439         bitmap_canv = gr_create_sub_canvas(grd_curcanv, 220, 45, bmp->bm_w, bmp->bm_h);
440         curcanv_save = grd_curcanv;
441         gr_set_current_canvas(bitmap_canv);
442         gr_bitmapm(0, 0, bmp);
443         gr_set_current_canvas(curcanv_save);
444 #endif
445
446         d_free(bitmap_canv);
447 }
448
449 #ifndef WINDOWS
450 //      -----------------------------------------------------------------------------
451 void show_spinning_robot_frame(int robot_num)
452 {
453         grs_canvas      *curcanv_save;
454
455         if (robot_num != -1) {
456                 Robot_angles.h += 150;
457
458                 curcanv_save = grd_curcanv;
459                 grd_curcanv = Robot_canv;
460                 Assert(Robot_info[robot_num].model_num != -1);
461                 draw_model_picture(Robot_info[robot_num].model_num, &Robot_angles);
462                 grd_curcanv = curcanv_save;
463         }
464
465 }
466
467 //  -----------------------------------------------------------------------------
468 void init_spinning_robot(void) //(int x,int y,int w,int h)
469  {
470 #if 0
471         Robot_angles.p += 0;
472         Robot_angles.b += 0;
473         Robot_angles.h += 0;
474
475 #else
476         int x = rescale_x(138);
477         int y = rescale_y(55);
478         int w = rescale_x(166);
479         int h = rescale_y(138);
480 #endif
481
482         Robot_canv = gr_create_sub_canvas(grd_curcanv, x, y, w, h);
483         // 138, 55, 166, 138
484  }
485 #endif
486
487 //      ---------------------------------------------------------------------------
488 //      Returns char width.
489 //      If show_robot_flag set, then show a frame of the spinning robot.
490 int show_char_delay(char the_char, int delay, int robot_num, int cursor_flag)
491 {
492         int     w, h, aw;
493         char    message[2];
494         static fix      start_time=0;
495
496         robot_num=0;
497         message[0] = the_char;
498         message[1] = 0;
499
500         if (start_time==0 && timer_get_fixed_seconds()<0)
501                 start_time=timer_get_fixed_seconds();
502
503         gr_get_string_size(message, &w, &h, &aw );
504
505         Assert((Current_color >= 0) && (Current_color < MAX_BRIEFING_COLORS));
506
507         //      Draw cursor if there is some delay and caller says to draw cursor
508         if (cursor_flag && delay) {
509                 WIN(DDGRLOCK(dd_grd_curcanv));
510                 gr_set_fontcolor(Briefing_foreground_colors[Current_color], -1);
511                 gr_printf(Briefing_text_x+1, Briefing_text_y, "_" );
512                 WIN(DDGRUNLOCK(dd_grd_curcanv));
513         }
514
515         if (delay)
516                 delay=fixdiv (F1_0,i2f(15));
517
518         if (delay != 0)
519                 show_bitmap_frame();
520
521 #ifdef ROBOT_MOVIES
522         if (RobotPlaying && (delay != 0))
523                 RotateRobot();
524
525         while (timer_get_fixed_seconds() < (start_time + delay)) {
526                 if (RobotPlaying && delay != 0)
527                         RotateRobot();
528         }
529 #else
530         if (robot_num != -1)
531                 show_spinning_robot_frame(robot_num);
532 #endif
533
534         start_time = timer_get_fixed_seconds();
535
536         WIN(DDGRLOCK(dd_grd_curcanv));
537         //      Erase cursor
538         if (cursor_flag && delay) {
539                 gr_set_fontcolor(Erase_color, -1);
540                 gr_printf(Briefing_text_x+1, Briefing_text_y, "_" );
541         }
542
543         //      Draw the character
544         gr_set_fontcolor(Briefing_background_colors[Current_color], -1);
545         gr_printf(Briefing_text_x, Briefing_text_y, message );
546
547         gr_set_fontcolor(Briefing_foreground_colors[Current_color], -1);
548         gr_printf(Briefing_text_x+1, Briefing_text_y, message );
549         WIN(DDGRUNLOCK(dd_grd_curcanv));
550
551         if (delay) gr_update();
552
553 //      if (the_char != ' ')
554 //              if (!digi_is_sound_playing(SOUND_MARKER_HIT))
555 //                      digi_play_sample( SOUND_MARKER_HIT, F1_0 );
556
557         return w;
558 }
559
560 //      -----------------------------------------------------------------------------
561 int load_briefing_screen( int screen_num )
562 {
563         int     pcx_error;
564
565         WIN(DDGRLOCK(dd_grd_curcanv));
566         if ((pcx_error=pcx_read_fullscr( CurBriefScreenName, New_pal ))!=PCX_ERROR_NONE) {
567                 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));
568                 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);
569                 WIN(DDGRUNLOCK(dd_grd_curcanv));
570                 Error( "Error loading briefing screen <%s>, PCX load error: %s (%i)\n",CurBriefScreenName, pcx_errormsg(pcx_error), pcx_error);
571         }
572         WIN(DDGRUNLOCK(dd_grd_curcanv));
573
574         WIN(DDGRRESTORE);
575
576         return 0;
577 }
578
579 int load_new_briefing_screen( char *fname )
580 {
581         int pcx_error;
582
583         mprintf ((0,"Loading new briefing %s!\n",fname));
584         strcpy (CurBriefScreenName,fname);
585
586         //WIN(DEFINE_SCREEN(CurBriefScreenName));
587
588         if (gr_palette_fade_out( New_pal, 32, 0 ))
589                 return 0;
590
591         WIN(DDGRLOCK(dd_grd_curcanv));
592         if ((pcx_error=pcx_read_fullscr( fname, New_pal ))!=PCX_ERROR_NONE)     {
593         //if ((pcx_error=pcx_read_bitmap( fname, &grd_curcanv->cv_bitmap, grd_curcanv->cv_bitmap.bm_type, New_pal ))!=PCX_ERROR_NONE)     {
594                 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);
595                 WIN(DDGRUNLOCK(dd_grd_curcanv));
596                 Error( "Error loading briefing screen <%s>, PCX load error: %s (%i)\n",fname, pcx_errormsg(pcx_error), pcx_error);
597         }
598         WIN(DDGRUNLOCK(dd_grd_curcanv));
599
600         WIN(DDGRRESTORE);
601
602         gr_copy_palette(gr_palette, New_pal, sizeof(gr_palette));
603
604         if (gr_palette_fade_in( New_pal, 32, 0 ))
605                 return 0;
606         DoBriefingColorStuff();
607
608         return 1;
609 }
610
611
612
613 #define KEY_DELAY_DEFAULT       ((F1_0*20)/1000)
614
615 //      -----------------------------------------------------------------------------
616 int get_message_num(char **message)
617 {
618         int     num=0;
619
620         while (**message == ' ')
621                 (*message)++;
622
623         while ((**message >= '0') && (**message <= '9')) {
624                 num = 10*num + **message-'0';
625                 (*message)++;
626         }
627
628         while (*(*message)++ != 10)             //      Get and drop eoln
629                 ;
630
631         return num;
632 }
633
634 //      -----------------------------------------------------------------------------
635 void get_message_name(char **message, char *result)
636 {
637         while (**message == ' ')
638                 (*message)++;
639
640         while ((**message != ' ') && (**message != 10)) {
641                 if (**message != '\n')
642                         *result++ = **message;
643                 (*message)++;
644         }
645
646         if (**message != 10)
647                 while (*(*message)++ != 10)             //      Get and drop eoln
648                         ;
649
650         *result = 0;
651 }
652
653 //      -----------------------------------------------------------------------------
654 void flash_cursor(int cursor_flag)
655 {
656         if (cursor_flag == 0)
657                 return;
658
659 WIN(DDGRLOCK(dd_grd_curcanv));
660         if ((timer_get_fixed_seconds() % (F1_0/2) ) > (F1_0/4))
661                 gr_set_fontcolor(Briefing_foreground_colors[Current_color], -1);
662         else
663                 gr_set_fontcolor(Erase_color, -1);
664
665         gr_printf(Briefing_text_x+1, Briefing_text_y, "_" );
666 WIN(DDGRUNLOCK(dd_grd_curcanv));
667 }
668
669 extern int InitMovieBriefing();
670
671 //      -----------------------------------------------------------------------------
672 //      Return true if message got aborted by user (pressed ESC), else return false.
673 int show_d1_briefing_message(int screen_num, char *message)
674 {
675         int     prev_ch=-1;
676         int     ch, done=0;
677         briefing_screen *bsp = &Briefing_screens[screen_num];
678         int     delay_count = KEY_DELAY_DEFAULT;
679         int     key_check;
680         int     robot_num=-1;
681         int     rval=0;
682         int     tab_stop=0;
683         int     flashing_cursor=0;
684         int     new_page=0;
685         int text_ulx = rescale_x(bsp->text_ulx);
686         int text_uly = rescale_y(bsp->text_uly);
687
688         Bitmap_name[0] = 0;
689
690         Current_color = 0;
691
692         // mprintf((0, "Going to print message [%s] at x=%i, y=%i\n", message, x, y));
693         gr_set_curfont( GAME_FONT );    
694
695         init_char_pos(text_ulx, text_uly);
696
697         while (!done) {
698                 ch = *message++;
699                 if (ch == '$') {
700                         ch = *message++;
701                         if (ch == 'C') {
702                                 Current_color = get_message_num(&message)-1;
703                                 Assert((Current_color >= 0) && (Current_color < MAX_BRIEFING_COLORS));
704                                 prev_ch = 10;
705                         } else if (ch == 'F') {         //      toggle flashing cursor
706                                 flashing_cursor = !flashing_cursor;
707                                 prev_ch = 10;
708                                 while (*message++ != 10)
709                                         ;
710                         } else if (ch == 'T') {
711                                 tab_stop = get_message_num(&message);
712                                 prev_ch = 10;                                                   //      read to eoln
713                         } else if (ch == 'R') {
714                                 if (Robot_canv != NULL)
715                                         {free(Robot_canv); Robot_canv=NULL;}
716
717                                 init_spinning_robot();
718                                 robot_num = get_message_num(&message);
719                                 prev_ch = 10;                                                   //      read to eoln
720                         } else if (ch == 'N') {
721                                 //--grs_bitmap  *bitmap_ptr;
722                                 if (Robot_canv != NULL)
723                                         {free(Robot_canv); Robot_canv=NULL;}
724
725                                 get_message_name(&message, Bitmap_name);
726                                 strcat(Bitmap_name, "#0");
727                                 Animating_bitmap_type = 0;
728                                 prev_ch = 10;
729                         } else if (ch == 'O') {
730                                 if (Robot_canv != NULL)
731                                         {free(Robot_canv); Robot_canv=NULL;}
732
733                                 get_message_name(&message, Bitmap_name);
734                                 strcat(Bitmap_name, "#0");
735                                 Animating_bitmap_type = 1;
736                                 prev_ch = 10;
737                         } else if (ch == 'B') {
738                                 char                    bitmap_name[32];
739                                 grs_bitmap      guy_bitmap;
740                                 ubyte                   temp_palette[768];
741                                 int                     iff_error;
742
743                                 if (Robot_canv != NULL)
744                                         {free(Robot_canv); Robot_canv=NULL;}
745
746                                 get_message_name(&message, bitmap_name);
747                                 strcat(bitmap_name, ".bbm");
748                                 gr_init_bitmap_data (&guy_bitmap);
749                                 iff_error = iff_read_bitmap(bitmap_name, &guy_bitmap, BM_LINEAR, temp_palette);
750                                 Assert(iff_error == IFF_NO_ERROR);
751
752                                 show_briefing_bitmap(&guy_bitmap);
753                                 gr_free_bitmap_data (&guy_bitmap);
754                                 prev_ch = 10;
755 //                      } else if (ch == 'B') {
756 //                              if (Robot_canv != NULL)
757 //                                      {free(Robot_canv); Robot_canv=NULL;}
758 //
759 //                              bitmap_num = get_message_num(&message);
760 //                              if (bitmap_num != -1)
761 //                                      show_briefing_bitmap(Textures[bitmap_num]);
762 //                              prev_ch = 10;                                                   //      read to eoln
763                         } else if (ch == 'S') {
764                                 int     keypress;
765                                 fix     start_time;
766                                 fix     time_out_value;
767
768                                 start_time = timer_get_fixed_seconds();
769                                 start_time = timer_get_approx_seconds();
770                                 time_out_value = start_time + i2f(60*5);                // Wait 1 minute...
771
772                                 //added on 9/13/98 by adb to make arch's requiring updates work
773                                 gr_update();
774                                 //end changes by adb
775  
776                                 while ( (keypress = local_key_inkey()) == 0 ) {         //      Wait for a key
777                                         if ( timer_get_approx_seconds() > time_out_value ) {
778                                                 keypress = 0;
779                                                 break;                                  // Time out after 1 minute..
780                                         }
781                                         while (timer_get_fixed_seconds() < start_time + KEY_DELAY_DEFAULT/2)
782                                                 ;
783                                         flash_cursor(flashing_cursor);
784                                         show_spinning_robot_frame(robot_num);
785                                         show_bitmap_frame();
786
787                                         //added on 9/13/98 by adb to make arch's requiring updates work
788                                         gr_update();
789                                         //end changes by adb
790
791                                         start_time += KEY_DELAY_DEFAULT/2;
792                                 }
793
794 #ifndef NDEBUG
795                                 if (keypress == KEY_BACKSP)
796                                         Int3();
797 #endif
798                                 if (keypress == KEY_ESC)
799                                         rval = 1;
800
801                                 flashing_cursor = 0;
802                                 done = 1;
803                         } else if (ch == 'P') {         //      New page.
804                                 new_page = 1;
805                                 while (*message != 10) {
806                                         message++;      //      drop carriage return after special escape sequence
807                                 }
808                                 message++;
809                                 prev_ch = 10;
810 //Begin D1X addition
811                         } else if (ch == '$' || ch == ';') {         // Print a $/;
812                                 prev_ch = ch;
813                                 Briefing_text_x += show_char_delay(ch, delay_count, robot_num, flashing_cursor);
814
815 //End D1X addition
816                         }
817                 } else if (ch == '\t') {                //      Tab
818                         if (Briefing_text_x - text_ulx < tab_stop)
819                                 Briefing_text_x = text_ulx + tab_stop;
820                 } else if ((ch == ';') && (prev_ch == 10)) {
821                         while (*message++ != 10)
822                                 ;
823                         prev_ch = 10;
824                 } else if (ch == '\\') {
825                         prev_ch = ch;
826                 } else if (ch == 10) {
827                         if (prev_ch != '\\') {
828                                 prev_ch = ch;
829                                 Briefing_text_y += GAME_FONT->ft_h+GAME_FONT->ft_h*3/5;
830                                 Briefing_text_x = text_ulx;
831                                 if (Briefing_text_y > text_uly + rescale_y(bsp->text_height)) {
832                                         load_briefing_screen(screen_num);
833                                         Briefing_text_x = text_ulx;
834                                         Briefing_text_y = text_uly;
835                                 }
836                         } else {
837                                 if (ch == 13)
838                                         Int3();
839                                 prev_ch = ch;
840                         }
841                 } else {
842                         prev_ch = ch;
843                         Briefing_text_x += show_char_delay(ch, delay_count, robot_num, flashing_cursor);
844                 }
845
846                 //added/changed on 9/13/98 by adb to speed up briefings after pressing a key with SDL
847                 //      Check for Esc -> abort.
848                  if(delay_count)
849                   key_check=local_key_inkey();
850                  else
851                   key_check=0;
852                 //end change - adb
853                 if ( key_check == KEY_ESC ) {
854                         rval = 1;
855                         done = 1;
856                 }
857
858 #if 0
859                 if ( key_check == KEY_ALTED+KEY_F2 )
860                         title_save_game();
861 #endif
862
863                 if ((key_check == KEY_SPACEBAR) || (key_check == KEY_ENTER))
864                         delay_count = 0;
865
866                 if (Briefing_text_x > text_ulx + rescale_x(bsp->text_width)) {
867                         Briefing_text_x = text_ulx;
868                         Briefing_text_y += GAME_FONT->ft_h+GAME_FONT->ft_h*3/5;
869                 }
870
871                 if ((new_page) || (Briefing_text_y > text_uly + rescale_y(bsp->text_height))) {
872                         fix     start_time = 0;
873                         fix     time_out_value = 0;
874                         int     keypress;
875
876                         new_page = 0;
877                         start_time = timer_get_approx_seconds();
878                         time_out_value = start_time + i2f(60*5);                // Wait 1 minute...
879
880                         //added on 9/13/98 by adb to make arch's requiring updates work
881                         gr_update();
882                         //end changes by adb
883         
884                         while ( (keypress = local_key_inkey()) == 0 ) {         //      Wait for a key
885                                 if ( timer_get_approx_seconds() > time_out_value ) {
886                                         keypress = 0;
887                                         break;                                  // Time out after 1 minute..
888                                 }
889                                 while (timer_get_approx_seconds() < start_time + KEY_DELAY_DEFAULT/2)
890                                         ;
891                                 flash_cursor(flashing_cursor);
892                                 show_spinning_robot_frame(robot_num);
893                                 show_bitmap_frame();
894
895                                 //added on 9/13/98 by adb to make arch's requiring updates work
896                                 gr_update();
897                                 //end changes by adb
898
899                                 start_time += KEY_DELAY_DEFAULT/2;
900                         }
901
902                         robot_num = -1;
903
904 #ifndef NDEBUG
905                         if (keypress == KEY_BACKSP)
906                                 Int3();
907 #endif
908                         if (keypress == KEY_ESC) {
909                                 rval = 1;
910                                 done = 1;
911                         }
912
913                         load_briefing_screen(screen_num);
914                         Briefing_text_x = text_ulx;
915                         Briefing_text_y = text_uly;
916                         delay_count = KEY_DELAY_DEFAULT;
917                 }
918         }
919
920         if (Robot_canv != NULL)
921                 {free(Robot_canv); Robot_canv=NULL;}
922
923         return rval;
924 }
925
926 //      -----------------------------------------------------------------------------
927 //      Return true if message got aborted by user (pressed ESC), else return false.
928 int show_briefing_message(int screen_num, char *message)
929 {
930         int     prev_ch=-1;
931         int     ch, done=0,i;
932         briefing_screen *bsp = &Briefing_screens[screen_num];
933         int     delay_count = KEY_DELAY_DEFAULT;
934         int     key_check;
935         int     robot_num=-1;
936         int     rval=0;
937         static int tab_stop=0;
938         int     flashing_cursor=0;
939         int     new_page=0,GotZ=0;
940 #ifdef ROBOT_MOVIES
941         char spinRobotName[]="rba.mve",kludge;  // matt don't change this!
942 #endif
943         char fname[15];
944         char DumbAdjust=0;
945         char chattering=0;
946         int hum_channel=-1,printing_channel=-1;
947         int LineAdjustment=1;
948         WIN(int wpage_done=0);
949
950         Bitmap_name[0] = 0;
951         Current_color = 0;
952 #ifdef ROBOT_MOVIES
953         RobotPlaying=0;
954
955         InitMovieBriefing();
956 #endif
957
958         #ifndef SHAREWARE
959         hum_channel  = digi_start_sound( digi_xlat_sound(SOUND_BRIEFING_HUM), F1_0/2, 0xFFFF/2, 1, -1, -1, -1 );
960         #endif
961  
962         // mprintf((0, "Going to print message [%s] at x=%i, y=%i\n", message, x, y));
963         gr_set_curfont( GAME_FONT );
964
965         bsp=&Briefing_screens[0];
966         init_char_pos(bsp->text_ulx, bsp->text_uly-(8*(1+MenuHires)));
967
968         while (!done) {
969                 ch = *message++;
970                 if (ch == '$') {
971                         ch = *message++;
972                         if (ch=='D') {
973                                 screen_num=DefineBriefingBox (&message);
974                         //load_new_briefing_screen (Briefing_screens[screen_num].bs_name);
975
976                                 bsp = &Briefing_screens[screen_num];
977                                 init_char_pos(bsp->text_ulx, bsp->text_uly);
978                                 //LineAdjustment=0;
979                                 prev_ch = 10;                                   // read to eoln
980                         } else if (ch=='U') {
981                                 screen_num=get_message_num(&message);
982                                 bsp = &Briefing_screens[screen_num];
983                                 init_char_pos(bsp->text_ulx, bsp->text_uly);
984                                 prev_ch = 10;                                   // read to eoln
985                         } else if (ch == 'C') {
986                                 Current_color = get_message_num(&message)-1;
987                                 Assert((Current_color >= 0) && (Current_color < MAX_BRIEFING_COLORS));
988                                 prev_ch = 10;
989                         } else if (ch == 'F') {     // toggle flashing cursor
990                                 flashing_cursor = !flashing_cursor;
991                                 prev_ch = 10;
992                                 while (*message++ != 10)
993                                         ;
994                         } else if (ch == 'T') {
995                                 tab_stop = get_message_num(&message);
996                                 tab_stop*=(1+MenuHires);
997                                 prev_ch = 10;                                                   //      read to eoln
998                         } else if (ch == 'R') {
999                                 if (Robot_canv != NULL) {
1000                                         d_free(Robot_canv);
1001                                         Robot_canv=NULL;
1002                                 }
1003 #ifdef ROBOT_MOVIES
1004                                 if (RobotPlaying) {
1005                                         DeInitRobotMovie();
1006                                         RobotPlaying=0;
1007                                 }
1008
1009                                 kludge=*message++;
1010                                 spinRobotName[2]=kludge; // ugly but proud
1011
1012                                 RobotPlaying=InitRobotMovie(spinRobotName);
1013
1014                                 // gr_remap_bitmap_good( &grd_curcanv->cv_bitmap, pal, -1, -1 );
1015
1016                                 if (RobotPlaying) {
1017                                         DoBriefingColorStuff ();
1018                                         mprintf ((0,"Robot playing is %d!!!",RobotPlaying));
1019                                 }
1020 #else
1021                                 init_spinning_robot();
1022                                 robot_num = get_message_num(&message);
1023 #endif
1024                                 prev_ch = 10;                           // read to eoln
1025                         } else if (ch == 'N') {
1026                                 //--grs_bitmap *bitmap_ptr;
1027                                 if (Robot_canv != NULL) {
1028                                         d_free(Robot_canv);
1029                                         Robot_canv=NULL;
1030                                 }
1031
1032                                 get_message_name(&message, Bitmap_name);
1033                                 strcat(Bitmap_name, "#0");
1034                                 Animating_bitmap_type = 0;
1035                                 prev_ch = 10;
1036                         } else if (ch == 'O') {
1037                                 if (Robot_canv != NULL) {
1038                                                 d_free(Robot_canv);
1039                                                 Robot_canv=NULL;
1040                                 }
1041
1042                                 get_message_name(&message, Bitmap_name);
1043                                 strcat(Bitmap_name, "#0");
1044                                 Animating_bitmap_type = 1;
1045                                 prev_ch = 10;
1046                         } else if (ch=='A') {
1047                                 LineAdjustment=1-LineAdjustment;
1048                         } else if (ch=='Z') {
1049                                 //mprintf ((0,"Got a Z!\n"));
1050                                 GotZ=1;
1051 #if defined (D2_OEM) || defined(COMPILATION) || (defined(MACINTOSH) && defined(SHAREWARE))
1052                                 DumbAdjust=1;
1053 #else
1054                                 if (LineAdjustment==1)
1055                                         DumbAdjust=1;
1056                                 else
1057                                         DumbAdjust=2;
1058 #endif
1059
1060                                 i=0;
1061                                 while ((fname[i]=*message) != '\n') {
1062                                         i++;
1063                                         message++;
1064                                 }
1065                                 fname[i]=0;
1066
1067                                 if (MenuHires) {
1068                                         char fname2[15];
1069
1070                                         i=0;
1071                                         while (fname[i]!='.')
1072                                                 fname2[i] = fname[i++];
1073 #ifndef SHAREWARE
1074                                         fname2[i++]='b';
1075 #endif
1076                                         fname2[i++]='.';
1077                                         fname2[i++]='p';
1078                                         fname2[i++]='c';
1079                                         fname2[i++]='x';
1080                                         fname2[i++]=0;
1081
1082                                         load_new_briefing_screen (cfexist(fname2)?fname2:fname);
1083                                 } else
1084                                         load_new_briefing_screen (fname);
1085
1086                                 //load_new_briefing_screen (MenuHires?"end01b.pcx":"end01.pcx");
1087
1088                         } else if (ch == 'B') {
1089                                 char        bitmap_name[32];
1090                                 grs_bitmap  guy_bitmap;
1091                                 ubyte       temp_palette[768];
1092                                 int         iff_error;
1093
1094                                 if (Robot_canv != NULL) {
1095                                         d_free(Robot_canv);
1096                                         Robot_canv=NULL;
1097                                 }
1098
1099                                 get_message_name(&message, bitmap_name);
1100                                 strcat(bitmap_name, ".bbm");
1101                                 guy_bitmap.bm_data = NULL;
1102                                 iff_error = iff_read_bitmap(bitmap_name, &guy_bitmap, BM_LINEAR, temp_palette);
1103                                 Assert(iff_error == IFF_NO_ERROR);
1104                                 gr_remap_bitmap_good( &guy_bitmap, temp_palette, -1, -1 );
1105
1106                                 show_briefing_bitmap(&guy_bitmap);
1107                                 d_free(guy_bitmap.bm_data);
1108                                 prev_ch = 10;
1109 //                      } else if (ch==EOF) {
1110 //                              done=1;
1111 //                      } else if (ch == 'B') {
1112 //                              if (Robot_canv != NULL) {
1113 //                                      d_free(Robot_canv);
1114 //                                      Robot_canv=NULL;
1115 //                              }
1116 //
1117 //                              bitmap_num = get_message_num(&message);
1118 //                              if (bitmap_num != -1)
1119 //                                      show_briefing_bitmap(Textures[bitmap_num]);
1120 //                              prev_ch = 10;                           // read to eoln
1121                         } else if (ch == 'S') {
1122                                 int keypress;
1123                                 fix start_time;
1124
1125                                 chattering=0;
1126                                 if (printing_channel>-1)
1127                                         digi_stop_sound( printing_channel );
1128                                 printing_channel=-1;
1129
1130 #ifdef WINDOWS
1131                                 if (!wpage_done) {
1132                                         DDGRRESTORE;
1133                                         wpage_done =1;
1134                                 }
1135 #endif
1136
1137                                 start_time = timer_get_fixed_seconds();
1138                                 while ( (keypress = local_key_inkey()) == 0 ) {         //      Wait for a key
1139 #ifdef WINDOWS
1140                                         if (_RedrawScreen) {
1141                                                 _RedrawScreen = FALSE;
1142                                                 hum_channel  = digi_start_sound( digi_xlat_sound(SOUND_BRIEFING_HUM), F1_0/2, 0xFFFF/2, 1, -1, -1, -1 );
1143                                                 keypress = KEY_ESC;
1144                                                 break;
1145                                         }
1146 #endif
1147
1148                                         while (timer_get_fixed_seconds() < start_time + KEY_DELAY_DEFAULT/2)
1149                                                 ;
1150                                         flash_cursor(flashing_cursor);
1151
1152 #ifdef ROBOT_MOVIES
1153                                         if (RobotPlaying)
1154                                                 RotateRobot ();
1155 #else
1156                                         show_spinning_robot_frame(robot_num);
1157 #endif
1158
1159                                         show_bitmap_frame();
1160                                         start_time += KEY_DELAY_DEFAULT/2;
1161                                 }
1162
1163 #ifndef NDEBUG
1164                                 if (keypress == KEY_BACKSP)
1165                                         Int3();
1166 #endif
1167                                 if (keypress == KEY_ESC)
1168                                         rval = 1;
1169
1170                                 flashing_cursor = 0;
1171                                 done = 1;
1172                                 WIN(wpage_done = 0);
1173                         } else if (ch == 'P') {         //      New page.
1174                                 if (!GotZ) {
1175                                         Int3(); // Hey ryan!!!! You gotta load a screen before you start
1176                                                 // printing to it! You know, $Z !!!
1177                                     load_new_briefing_screen (MenuHires?"end01b.pcx":"end01.pcx");
1178                                 }
1179
1180                                 new_page = 1;
1181                                 while (*message != 10) {
1182                                         message++;      //      drop carriage return after special escape sequence
1183                                 }
1184                                 message++;
1185                                 prev_ch = 10;
1186                                 gr_update();
1187                         }
1188                 } else if (ch == '\t') {                //      Tab
1189                         if (Briefing_text_x - bsp->text_ulx < tab_stop)
1190                                 Briefing_text_x = bsp->text_ulx + tab_stop;
1191                 } else if ((ch == ';') && (prev_ch == 10)) {
1192                         while (*message++ != 10)
1193                                 ;
1194                         prev_ch = 10;
1195                 } else if (ch == '\\') {
1196                         prev_ch = ch;
1197                 } else if (ch == 10) {
1198                         if (prev_ch != '\\') {
1199                                 prev_ch = ch;
1200                                 if (1) //DumbAdjust==0)
1201                                         Briefing_text_y += (8*(MenuHires+1));
1202                                 else
1203                                         DumbAdjust--;
1204                                 Briefing_text_x = bsp->text_ulx;
1205                                 if (Briefing_text_y > bsp->text_uly + bsp->text_height) {
1206                                         load_briefing_screen(screen_num);
1207                                         Briefing_text_x = bsp->text_ulx;
1208                                         Briefing_text_y = bsp->text_uly;
1209                                 }
1210                         } else {
1211                                 if (ch == 13)           //Can this happen? Above says ch==10
1212                                         Int3();
1213                                 prev_ch = ch;
1214                         }
1215
1216                 } else {
1217                         if (!GotZ) {
1218                                 Int3(); // Hey ryan!!!! You gotta load a screen before you start
1219                                         // printing to it! You know, $Z !!!
1220                                 load_new_briefing_screen (MenuHires?"end01b.pcx":"end01.pcx");
1221                         }
1222
1223                         prev_ch = ch;
1224
1225                         if (!chattering) {
1226                                 printing_channel  = digi_start_sound( digi_xlat_sound(SOUND_BRIEFING_PRINTING), F1_0, 0xFFFF/2, 1, -1, -1, -1 );
1227                                 chattering=1;
1228                         }
1229
1230                         WIN(if (GRMODEINFO(emul)) delay_count = 0);
1231
1232                         Briefing_text_x += show_char_delay(ch, delay_count, robot_num, flashing_cursor);
1233
1234                 }
1235
1236                 //      Check for Esc -> abort.
1237                 if(delay_count)
1238                         key_check=local_key_inkey();
1239                 else
1240                         key_check=0;
1241
1242 #ifdef WINDOWS
1243                 if (_RedrawScreen) {
1244                         _RedrawScreen = FALSE;
1245                         hum_channel  = digi_start_sound( digi_xlat_sound(SOUND_BRIEFING_HUM), F1_0/2, 0xFFFF/2, 1, -1, -1, -1 );
1246                         key_check = KEY_ESC;
1247                 }
1248 #endif
1249                 if ( key_check == KEY_ESC ) {
1250                         rval = 1;
1251                         done = 1;
1252                 }
1253
1254                 if ((key_check == KEY_SPACEBAR) || (key_check == KEY_ENTER))
1255                         delay_count = 0;
1256
1257                 if (Briefing_text_x > bsp->text_ulx + bsp->text_width) {
1258                         Briefing_text_x = bsp->text_ulx;
1259                         Briefing_text_y += bsp->text_uly;
1260                 }
1261
1262                 if ((new_page) || (Briefing_text_y > bsp->text_uly + bsp->text_height)) {
1263                         fix     start_time = 0;
1264                         int     keypress;
1265
1266                         new_page = 0;
1267
1268                         if (printing_channel>-1)
1269                                 digi_stop_sound( printing_channel );
1270                         printing_channel=-1;
1271
1272                         chattering=0;
1273
1274 #ifdef WINDOWS
1275                         if (!wpage_done) {
1276                                 DDGRRESTORE;
1277                                 wpage_done =1;
1278                         }
1279 #endif
1280
1281                         start_time = timer_get_fixed_seconds();
1282                         while ( (keypress = local_key_inkey()) == 0 ) {         //      Wait for a key
1283 #ifdef WINDOWS
1284                                 if (_RedrawScreen) {
1285                                         _RedrawScreen = FALSE;
1286                                         hum_channel  = digi_start_sound( digi_xlat_sound(SOUND_BRIEFING_HUM), F1_0/2, 0xFFFF/2, 1, -1, -1, -1 );
1287                                         keypress = KEY_ESC;
1288                                         break;
1289                                 }
1290 #endif
1291
1292                                 while (timer_get_fixed_seconds() < start_time + KEY_DELAY_DEFAULT/2)
1293                                         ;
1294                                 flash_cursor(flashing_cursor);
1295 #ifdef ROBOT_MOVIES
1296                                 if (RobotPlaying)
1297                                         RotateRobot();
1298 #else
1299                                 show_spinning_robot_frame(robot_num);
1300 #endif
1301                                 show_bitmap_frame();
1302                                 start_time += KEY_DELAY_DEFAULT/2;
1303                         }
1304
1305 #ifdef ROBOT_MOVIES
1306                         if (RobotPlaying)
1307                                 DeInitRobotMovie();
1308                         RobotPlaying=0;
1309 #endif
1310                         robot_num = -1;
1311
1312 #ifndef NDEBUG
1313                         if (keypress == KEY_BACKSP)
1314                                 Int3();
1315 #endif
1316                         if (keypress == KEY_ESC) {
1317                                 rval = 1;
1318                                 done = 1;
1319                         }
1320
1321                         load_briefing_screen(screen_num);
1322                         Briefing_text_x = bsp->text_ulx;
1323                         Briefing_text_y = bsp->text_uly;
1324                         delay_count = KEY_DELAY_DEFAULT;
1325
1326                         WIN(wpage_done = 0);
1327                 }
1328         }
1329
1330 #ifdef ROBOT_MOVIES
1331         if (RobotPlaying) {
1332                 DeInitRobotMovie();
1333                 RobotPlaying=0;
1334         }
1335 #endif
1336
1337         if (Robot_canv != NULL)
1338                 {d_free(Robot_canv); Robot_canv=NULL;}
1339
1340         if (hum_channel>-1)
1341                 digi_stop_sound( hum_channel );
1342         if (printing_channel>-1)
1343                 digi_stop_sound( printing_channel );
1344
1345         return rval;
1346 }
1347
1348 //-----------------------------------------------------------------------------
1349 // Return a pointer to the start of text for screen #screen_num.
1350 char * get_briefing_message(int screen_num)
1351 {
1352         char    *tptr = Briefing_text;
1353         int     cur_screen=0;
1354         int     ch;
1355
1356         Assert(screen_num >= 0);
1357
1358         while ( (*tptr != 0 ) && (screen_num != cur_screen)) {
1359                 ch = *tptr++;
1360                 if (ch == '$') {
1361                         ch = *tptr++;
1362                         if (ch == 'S')
1363                                 cur_screen = get_message_num(&tptr);
1364                 }
1365         }
1366
1367         if (screen_num!=cur_screen)
1368                 return (NULL);
1369
1370         return tptr;
1371 }
1372
1373 //-----------------------------------------------------------------------------
1374 //      Load Descent briefing text.
1375 int load_screen_text(char *filename, char **buf)
1376 {
1377         CFILE *tfile;
1378         CFILE *ifile;
1379         int     len, i,x;
1380         int     have_binary = 0;
1381
1382         if ((tfile = cfopen(filename,"rb")) == NULL) {
1383                 char nfilename[30], *ptr;
1384
1385                 strcpy(nfilename, filename);
1386                 ptr = strrchr(nfilename, '.');
1387                 *ptr = '\0';
1388                 strcat(nfilename, ".txb");
1389                 if ((ifile = cfopen(nfilename, "rb")) == NULL) {
1390                         mprintf ((0,"can't open %s!\n",nfilename));
1391                         return (0);
1392                                 //Error("Cannot open file %s or %s", filename, nfilename);
1393                 }
1394
1395                 mprintf ((0,"reading...\n"));
1396                 have_binary = 1;
1397
1398                 len = cfilelength(ifile);
1399                 MALLOC(*buf, char, len+500);
1400                 mprintf ((0,"len=%d\n",len));
1401                 for (x=0, i=0; i < len; i++, x++) {
1402                         cfread (*buf+x,1,1,ifile);
1403                         //  mprintf ((0,"%c",*(*buf+x)));
1404                         if (*(*buf+x)==13)
1405                                 x--;
1406                 }
1407
1408                 cfclose(ifile);
1409         } else {
1410                 len = cfilelength(tfile);
1411                 MALLOC(*buf, char, len+500);
1412                 for (x=0, i=0; i < len; i++, x++) {
1413                         cfread (*buf+x,1,1,tfile);
1414                         // mprintf ((0,"%c",*(*buf+x)));
1415                         if (*(*buf+x)==13)
1416                                 x--;
1417                 }
1418
1419
1420                 //cfread(*buf, 1, len, tfile);
1421                 cfclose(tfile);
1422         }
1423
1424         if (have_binary) {
1425                 char *ptr;
1426
1427                 for (i = 0, ptr = *buf; i < len; i++, ptr++) {
1428                         if (*ptr != '\n') {
1429                                 encode_rotate_left(ptr);
1430                                 *ptr = *ptr ^ BITMAP_TBL_XOR;
1431                                 encode_rotate_left(ptr);
1432                         }
1433                 }
1434         }
1435
1436         return (1);
1437 }
1438
1439 //-----------------------------------------------------------------------------
1440 // Return true if message got aborted, else return false.
1441 int show_briefing_text(int screen_num)
1442 {
1443         char    *message_ptr;
1444
1445         message_ptr = get_briefing_message(screen_num);
1446         if (message_ptr==NULL)
1447                 return (0);
1448
1449         DoBriefingColorStuff();
1450
1451         if (Mission_list[Current_mission_num].descent_version == 1)
1452                 return show_d1_briefing_message(screen_num, message_ptr);
1453         else
1454                 return show_briefing_message(screen_num, message_ptr);
1455 }
1456
1457 void DoBriefingColorStuff ()
1458 {
1459         Briefing_foreground_colors[0] = gr_find_closest_color_current( 0, 40, 0);
1460         Briefing_background_colors[0] = gr_find_closest_color_current( 0, 6, 0);
1461
1462         Briefing_foreground_colors[1] = gr_find_closest_color_current( 40, 33, 35);
1463         Briefing_background_colors[1] = gr_find_closest_color_current( 5, 5, 5);
1464
1465         Briefing_foreground_colors[2] = gr_find_closest_color_current( 8, 31, 54);
1466         Briefing_background_colors[2] = gr_find_closest_color_current( 1, 4, 7);
1467
1468         Erase_color = gr_find_closest_color_current(0, 0, 0);
1469 }
1470
1471 //-----------------------------------------------------------------------------
1472 // Return true if screen got aborted by user, else return false.
1473 int show_briefing_screen( int screen_num, int allow_keys)
1474 {
1475         int     rval=0;
1476         ubyte   palette_save[768];
1477
1478         New_pal_254_bash = 0;
1479
1480         if (Skip_briefing_screens) {
1481                 mprintf((0, "Skipping briefing screen [brief03.pcx]\n"));
1482                 return 0;
1483         }
1484
1485 //      briefing_bm.bm_data=NULL;       
1486 //   if ((pcx_error=pcx_read_bitmap( "brief03.pcx", &briefing_bm, BM_LINEAR, New_pal ))!=PCX_ERROR_NONE)     {
1487 //        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));
1488 //              Int3();
1489 //              return 0;
1490 //      }
1491
1492         memcpy(palette_save,gr_palette,sizeof(palette_save));
1493         memcpy(New_pal,gr_palette,sizeof(gr_palette));
1494    
1495
1496 //      vfx_set_palette_sub( New_pal );
1497 //      gr_palette_clear();
1498 //      gr_bitmap( 0, 0, &briefing_bm );
1499 #ifdef OGL
1500         gr_palette_load(New_pal);
1501 #endif
1502
1503 //      if (gr_palette_fade_in( New_pal, 32, allow_keys ))      
1504 //              return 1;
1505 //      memcpy(gr_palette,New_pal,sizeof(gr_palette));
1506
1507         #ifdef MACINTOSH
1508         key_close();            // kill the keyboard handler during briefing screens for movies
1509         #endif
1510         rval = show_briefing_text(screen_num);
1511         #ifdef MACINTOSH
1512         key_init();
1513         #endif
1514         
1515         #if defined (MACINTOSH) || defined(WINDOWS)
1516         memcpy(New_pal,gr_palette,sizeof(gr_palette));          // attempt to get fades after briefing screens done correctly.
1517         #endif
1518
1519
1520         #ifndef WINDOWS 
1521         if (gr_palette_fade_out( New_pal, 32, allow_keys ))
1522                 return 1;
1523    #else
1524                 DEFINE_SCREEN(NULL);
1525                 WIN(DDGRLOCK(dd_grd_curcanv));
1526                 gr_clear_canvas (0);
1527                 WIN(DDGRUNLOCK(dd_grd_curcanv));
1528                 if (gr_palette_fade_out( New_pal, 32, allow_keys ))
1529                         return 1;
1530         #endif
1531
1532         gr_copy_palette(gr_palette, palette_save, sizeof(palette_save));
1533
1534 //      d_free(briefing_bm.bm_data);
1535
1536         return rval;
1537 }
1538
1539
1540 //      -----------------------------------------------------------------------------
1541 void do_briefing_screens(char *filename,int level_num)
1542 {
1543         int     abort_briefing_screens = 0;
1544         int     cur_briefing_screen = 0;
1545
1546         MVEPaletteCalls=0;
1547
1548         if (Skip_briefing_screens) {
1549                 mprintf((0, "Skipping all briefing screens.\n"));
1550                 return;
1551         }
1552         
1553         #ifdef APPLE_DEMO
1554         return;                 // no briefing screens at all for demo
1555
1556         #endif
1557
1558         mprintf ((0,"Trying briefing screen! %s\n",filename));
1559
1560         if (!filename)
1561                 return;
1562
1563         if (!load_screen_text(filename, &Briefing_text))
1564                 return;
1565
1566         #ifdef SHAREWARE
1567         songs_play_song( SONG_BRIEFING, 1 );
1568         #else
1569         songs_stop_all();
1570         #endif
1571
1572         set_screen_mode( SCREEN_MENU );
1573         
1574         WINDOS(
1575                 dd_gr_set_current_canvas(NULL),
1576                 gr_set_current_canvas(NULL)
1577         );
1578
1579         mprintf ((0,"Playing briefing screen! %s %d\n",filename,level_num));
1580
1581         key_flush();
1582
1583         if (Mission_list[Current_mission_num].descent_version == 1) {
1584                 if (level_num == 1) {
1585                         while ((!abort_briefing_screens) && (Briefing_screens[cur_briefing_screen].level_num == 0)) {
1586                                 abort_briefing_screens = show_briefing_screen(cur_briefing_screen, 0);
1587                                 cur_briefing_screen++;
1588                         }
1589                 }
1590
1591                 if (!abort_briefing_screens) {
1592                         for (cur_briefing_screen = 0; cur_briefing_screen < MAX_BRIEFING_SCREENS; cur_briefing_screen++)
1593                                 if (Briefing_screens[cur_briefing_screen].level_num == level_num)
1594                                         if (show_briefing_screen(cur_briefing_screen, 0))
1595                                                 break;
1596                 }
1597
1598         } else
1599                 show_briefing_screen(level_num,0);
1600
1601         d_free (Briefing_text);
1602         key_flush();
1603
1604         return;
1605
1606 }
1607
1608 int DefineBriefingBox (char **buf)
1609 {
1610         int n,i=0;
1611         char name[20];
1612
1613         n=get_new_message_num (buf);
1614
1615         Assert(n < MAX_BRIEFING_SCREENS);
1616
1617         while (**buf!=' ') {
1618                 name[i++]=**buf;
1619                 (*buf)++;
1620         }
1621
1622         name[i]='\0';   // slap a delimiter on this guy
1623
1624         strcpy (Briefing_screens[n].bs_name,name);
1625         Briefing_screens[n].level_num=get_new_message_num (buf);
1626         Briefing_screens[n].message_num=get_new_message_num (buf);
1627         Briefing_screens[n].text_ulx=get_new_message_num (buf);
1628         Briefing_screens[n].text_uly=get_new_message_num (buf);
1629         Briefing_screens[n].text_width=get_new_message_num (buf);
1630         Briefing_screens[n].text_height=get_message_num (buf);  // NOTICE!!!
1631
1632         if (MenuHires) {
1633                 Briefing_screens[n].text_ulx*=2;
1634                 Briefing_screens[n].text_uly*=2.4;
1635                 Briefing_screens[n].text_width*=2;
1636                 Briefing_screens[n].text_height*=2.4;
1637         }
1638
1639         return (n);
1640 }
1641
1642 int get_new_message_num(char **message)
1643 {
1644         int     num=0;
1645
1646         while (**message == ' ')
1647                 (*message)++;
1648
1649         while ((**message >= '0') && (**message <= '9')) {
1650                 num = 10*num + **message-'0';
1651                 (*message)++;
1652         }
1653
1654        (*message)++;
1655
1656         return num;
1657 }
1658