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