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