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