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