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