]> icculus.org git repositories - btb/d2x.git/blob - main/movie.c
gr_copy_palette not really a kludge, I think
[btb/d2x.git] / main / movie.c
1 /* $Id: movie.c,v 1.26 2003-06-10 04:46:16 btb Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include <conf.h>
17 #endif
18
19 #ifdef RCS
20 static char rcsid[] = "$Id: movie.c,v 1.26 2003-06-10 04:46:16 btb Exp $";
21 #endif
22
23 #define DEBUG_LEVEL CON_NORMAL
24
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <ctype.h>
31
32 #include "movie.h"
33 #include "console.h"
34 #include "args.h"
35 #include "key.h"
36 #include "digi.h"
37 #include "songs.h"
38 #include "inferno.h"
39 #include "palette.h"
40 #include "strutil.h"
41 #include "error.h"
42 #include "u_mem.h"
43 #include "byteswap.h"
44 #include "gr.h"
45 #include "gamefont.h"
46 #include "cfile.h"
47 #include "menu.h"
48 #include "libmve.h"
49 #include "text.h"
50 #include "fileutil.h"
51 #include "screens.h"
52
53 extern int MenuHiresAvailable;
54 extern char CDROM_dir[];
55
56 #define VID_PLAY 0
57 #define VID_PAUSE 1
58
59 int Vid_State;
60
61
62 // Subtitle data
63 typedef struct {
64         short first_frame,last_frame;
65         char *msg;
66 } subtitle;
67
68 #define MAX_SUBTITLES 500
69 #define MAX_ACTIVE_SUBTITLES 3
70 subtitle Subtitles[MAX_SUBTITLES];
71 int Num_subtitles;
72
73 // Movielib data
74 typedef struct {
75         char name[FILENAME_LEN];
76         int offset,len;
77 } ml_entry;
78
79 #define MLF_ON_CD    1
80 #define MAX_MOVIES_PER_LIB    50    //determines size of malloc
81
82 typedef struct {
83         char     name[100]; //[FILENAME_LEN];
84         int      n_movies;
85         ubyte    flags,pad[3];
86         ml_entry *movies;
87 } movielib;
88
89 #ifdef D2_OEM
90 char movielib_files[][FILENAME_LEN] = {"intro-l.mvl","other-l.mvl","robots-l.mvl","oem-l.mvl"};
91 #else
92 char movielib_files[][FILENAME_LEN] = {"intro-l.mvl","other-l.mvl","robots-l.mvl"};
93 #endif
94
95 #define N_BUILTIN_MOVIE_LIBS (sizeof(movielib_files)/sizeof(*movielib_files))
96 #define N_MOVIE_LIBS (N_BUILTIN_MOVIE_LIBS+1)
97 #define EXTRA_ROBOT_LIB N_BUILTIN_MOVIE_LIBS
98 movielib *movie_libs[N_MOVIE_LIBS];
99
100
101 //do we have the robot movies available
102 int robot_movies = 0; //0 means none, 1 means lowres, 2 means hires
103
104 int MovieHires = 1;   //default is highres
105
106 int RoboFile = 0, RoboFilePos = 0;
107
108 // Function Prototypes
109 int RunMovie(char *filename, int highres_flag, int allow_abort,int dx,int dy);
110
111 int open_movie_file(char *filename,int must_have);
112 int reset_movie_file(int handle);
113
114 void change_filename_ext( char *dest, char *src, char *ext );
115 void decode_text_line(char *p);
116 void draw_subtitles(int frame_num);
117
118
119 // ----------------------------------------------------------------------
120 void* MPlayAlloc(unsigned size)
121 {
122     return d_malloc(size);
123 }
124
125 void MPlayFree(void *p)
126 {
127     d_free(p);
128 }
129
130
131 //-----------------------------------------------------------------------
132
133 unsigned int FileRead(void *handle, void *buf, unsigned int count)
134 {
135     unsigned numread;
136     numread = read((int)handle, buf, count);
137     return (numread == count);
138 }
139
140
141 //-----------------------------------------------------------------------
142
143
144 //filename will actually get modified to be either low-res or high-res
145 //returns status.  see values in movie.h
146 int PlayMovie(const char *filename, int must_have)
147 {
148         char name[FILENAME_LEN],*p;
149         int c, ret;
150
151 #ifndef RELEASE
152         if (FindArg("-nomovies"))
153                 return MOVIE_NOT_PLAYED;
154 #endif
155
156         strcpy(name,filename);
157
158         if ((p=strchr(name,'.')) == NULL)               //add extension, if missing
159                 strcat(name,".mve");
160
161         //check for escape already pressed & abort if so
162         while ((c=key_inkey()) != 0)
163                 if (c == KEY_ESC)
164                         return MOVIE_ABORTED;
165
166         // Stop all digital sounds currently playing.
167         digi_stop_all();
168
169         // Stop all songs
170         songs_stop_all();
171
172         digi_close();
173
174         // Start sound
175         if (!FindArg("-nosound"))
176                 MVE_sndInit(1);
177         else
178                 MVE_sndInit(-1);
179
180         ret = RunMovie(name,MovieHires,must_have,-1,-1);
181
182         if (!FindArg("-nosound"))
183                 digi_init();
184
185         Screen_mode = -1;               //force screen reset
186
187         return ret;
188 }
189
190
191 void MovieShowFrame(ubyte *buf, uint bufw, uint bufh, uint sx, uint sy,
192                                         uint w, uint h, uint dstx, uint dsty)
193 {
194         grs_bitmap source_bm;
195
196         //mprintf((0,"MovieShowFrame %d,%d  %d,%d  %d,%d  %d,%d\n",bufw,bufh,sx,sy,w,h,dstx,dsty));
197
198         Assert(bufw == w && bufh == h);
199
200         source_bm.bm_x = source_bm.bm_y = 0;
201         source_bm.bm_w = source_bm.bm_rowsize = bufw;
202         source_bm.bm_h = bufh;
203         source_bm.bm_type = BM_LINEAR;
204         source_bm.bm_flags = 0;
205         source_bm.bm_data = buf;
206
207         gr_bm_ubitblt(bufw,bufh,dstx,dsty,sx,sy,&source_bm,&grd_curcanv->cv_bitmap);
208 }
209
210 //our routine to set the pallete, called from the movie code
211 void MovieSetPalette(unsigned char *p, unsigned start, unsigned count)
212 {
213         if (count == 0)
214                 return;
215
216         //mprintf((0,"SetPalette p=%x, start=%d, count=%d\n",p,start,count));
217
218         //Color 0 should be black, and we get color 255
219         Assert(start>=1 && start+count-1<=254);
220
221         //Set color 0 to be black
222         gr_palette[0] = gr_palette[1] = gr_palette[2] = 0;
223
224         //Set color 255 to be our subtitle color
225         gr_palette[765] = gr_palette[766] = gr_palette[767] = 50;
226
227         //movie libs palette into our array
228         memcpy(gr_palette+start*3,p+start*3,count*3);
229
230         //finally set the palette in the hardware
231         //gr_palette_load(gr_palette);
232
233         //MVE_SetPalette(p, start, count);
234 }
235
236
237 #if 0
238 typedef struct bkg {
239         short x, y, w, h;           // The location of the menu.
240         grs_bitmap * bmp;               // The background under the menu.
241 } bkg;
242
243 bkg movie_bg = {0,0,0,0,NULL};
244 #endif
245
246 #define BOX_BORDER (MenuHires?40:20)
247
248
249 void show_pause_message(char *msg)
250 {
251         int w,h,aw;
252         int x,y;
253
254         gr_set_current_canvas(NULL);
255         gr_set_curfont( SMALL_FONT );
256
257         gr_get_string_size(msg,&w,&h,&aw);
258
259         x = (grd_curscreen->sc_w-w)/2;
260         y = (grd_curscreen->sc_h-h)/2;
261
262 #if 0
263         if (movie_bg.bmp) {
264                 gr_free_bitmap(movie_bg.bmp);
265                 movie_bg.bmp = NULL;
266         }
267
268         // Save the background of the display
269         movie_bg.x=x; movie_bg.y=y; movie_bg.w=w; movie_bg.h=h;
270
271         movie_bg.bmp = gr_create_bitmap( w+BOX_BORDER, h+BOX_BORDER );
272
273         gr_bm_ubitblt(w+BOX_BORDER, h+BOX_BORDER, 0, 0, x-BOX_BORDER/2, y-BOX_BORDER/2, &(grd_curcanv->cv_bitmap), movie_bg.bmp );
274 #endif
275
276         gr_setcolor(0);
277         gr_rect(x-BOX_BORDER/2,y-BOX_BORDER/2,x+w+BOX_BORDER/2-1,y+h+BOX_BORDER/2-1);
278
279         gr_set_fontcolor( 255, -1 );
280
281         gr_ustring( 0x8000, y, msg );
282
283         gr_update();
284 }
285
286 void clear_pause_message()
287 {
288 #if 0
289         if (movie_bg.bmp) {
290
291                 gr_bitmap(movie_bg.x-BOX_BORDER/2, movie_bg.y-BOX_BORDER/2, movie_bg.bmp);
292
293                 gr_free_bitmap(movie_bg.bmp);
294                 movie_bg.bmp = NULL;
295         }
296 #endif
297 }
298
299
300 //returns status.  see movie.h
301 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
302 {
303         int filehndl;
304         int result=1,aborted=0;
305         int track = 0;
306         int frame_num;
307         int key;
308 #ifdef OGL
309         char pal_save[768];
310 #endif
311
312         result=1;
313
314         // Open Movie file.  If it doesn't exist, no movie, just return.
315
316         filehndl = open_movie_file(filename,must_have);
317
318         if (filehndl == -1) {
319                 if (must_have)
320                         Warning("movie: RunMovie: Cannot open movie <%s>\n",filename);
321                 return MOVIE_NOT_PLAYED;
322         }
323
324         MVE_memCallbacks(MPlayAlloc, MPlayFree);
325         MVE_ioCallbacks(FileRead);
326
327         if (hires_flag) {
328                 gr_set_mode(SM(640,480));
329         } else {
330                 gr_set_mode(SM(320,200));
331         }
332 #ifdef OGL
333         set_screen_mode(SCREEN_MENU);
334         gr_copy_palette(pal_save, gr_palette, 768);
335         memset(gr_palette, 0, 768);
336         gr_palette_load(gr_palette);
337 #endif
338
339 #if !defined(POLY_ACC)
340         MVE_sfCallbacks(MovieShowFrame);
341         MVE_palCallbacks(MovieSetPalette);
342 #endif
343
344         if (MVE_rmPrepMovie((void *)filehndl, dx, dy, track)) {
345                 Int3();
346                 return MOVIE_NOT_PLAYED;
347         }
348
349         frame_num = 0;
350
351         FontHires = FontHiresAvailable && hires_flag;
352
353         while((result = MVE_rmStepMovie()) == 0) {
354
355                 draw_subtitles(frame_num);
356
357                 gr_palette_load(gr_palette); // moved this here because of flashing
358
359                 gr_update();
360
361                 key = key_inkey();
362
363                 // If ESCAPE pressed, then quit movie.
364                 if (key == KEY_ESC) {
365                         result = aborted = 1;
366                         break;
367                 }
368
369                 // If PAUSE pressed, then pause movie
370                 if (key == KEY_PAUSE) {
371                         MVE_rmHoldMovie();
372                         show_pause_message(TXT_PAUSE);
373                         while (!key_inkey()) ;
374                         clear_pause_message();
375                 }
376
377 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
378                 if ((key == KEY_CTRLED+KEY_SHIFTED+KEY_PADENTER) ||
379                         (key == KEY_ALTED+KEY_CTRLED+KEY_PADENTER) ||
380                         (key == KEY_ALTED+KEY_SHIFTED+KEY_PADENTER))
381                         gr_toggle_fullscreen();
382 #endif
383
384                 frame_num++;
385         }
386
387         Assert(aborted || result == MVE_ERR_EOF);        ///movie should be over
388
389     MVE_rmEndMovie();
390
391         close(filehndl);                           // Close Movie File
392
393         // Restore old graphic state
394
395         Screen_mode=-1;  //force reset of screen mode
396 #ifdef OGL
397         gr_copy_palette(gr_palette, pal_save, 768);
398         gr_palette_load(pal_save);
399 #endif
400
401         return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
402 }
403
404
405 int InitMovieBriefing()
406 {
407 #if 0
408         if (MenuHires)
409                 gr_set_mode(SM(640,480));
410         else
411                 gr_set_mode(SM(320,200));
412
413         gr_init_sub_canvas( &VR_screen_pages[0], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
414         gr_init_sub_canvas( &VR_screen_pages[1], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
415 #endif
416
417         return 1;
418 }
419
420
421 //returns 1 if frame updated ok
422 int RotateRobot()
423 {
424         int err;
425
426         err = MVE_rmStepMovie();
427
428         gr_palette_load(gr_palette);
429
430         if (err == MVE_ERR_EOF)     //end of movie, so reset
431         {
432                 reset_movie_file(RoboFile);
433                 if (MVE_rmPrepMovie((void *)RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
434                         Int3();
435                         return 0;
436                 }
437         }
438         else if (err) {
439                 Int3();
440                 return 0;
441         }
442
443         return 1;
444 }
445
446
447 void DeInitRobotMovie(void)
448 {
449         MVE_rmEndMovie();
450         close(RoboFile);                           // Close Movie File
451 }
452
453
454 int InitRobotMovie(char *filename)
455 {
456         if (FindArg("-nomovies"))
457                 return 0;
458
459         con_printf(DEBUG_LEVEL, "RoboFile=%s\n", filename);
460
461         MVE_sndInit(-1);        //tell movies to play no sound for robots
462
463         RoboFile = open_movie_file(filename, 1);
464
465         if (RoboFile == -1) {
466                 Warning("movie: InitRobotMovie: Cannot open movie file <%s>",filename);
467                 return MOVIE_NOT_PLAYED;
468         }
469
470         Vid_State = VID_PLAY;
471
472         if (MVE_rmPrepMovie((void *)RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
473                 Int3();
474                 return 0;
475         }
476
477         RoboFilePos=lseek (RoboFile,0L,SEEK_CUR);
478
479         con_printf(DEBUG_LEVEL, "RoboFilePos=%d!\n", RoboFilePos);
480
481         return 1;
482 }
483
484
485 /*
486  *              Subtitle system code
487  */
488
489 ubyte *subtitle_raw_data;
490
491
492 //search for next field following whitespace 
493 ubyte *next_field(ubyte *p)
494 {
495         while (*p && !isspace(*p))
496                 p++;
497
498         if (!*p)
499                 return NULL;
500
501         while (*p && isspace(*p))
502                 p++;
503
504         if (!*p)
505                 return NULL;
506
507         return p;
508 }
509
510
511 int init_subtitles(char *filename)
512 {
513         CFILE *ifile;
514         int size,read_count;
515         ubyte *p;
516         int have_binary = 0;
517
518         Num_subtitles = 0;
519
520         if (! FindArg("-subtitles"))
521                 return 0;
522
523         ifile = cfopen(filename,"rb");          //try text version
524
525         if (!ifile) {                                                           //no text version, try binary version
526                 char filename2[FILENAME_LEN];
527                 change_filename_ext(filename2,filename,".TXB");
528                 ifile = cfopen(filename2,"rb");
529                 if (!ifile)
530                         return 0;
531                 have_binary = 1;
532         }
533
534         size = cfilelength(ifile);
535
536         MALLOC (subtitle_raw_data, ubyte, size+1);
537
538         read_count = cfread(subtitle_raw_data, 1, size, ifile);
539
540         cfclose(ifile);
541
542         subtitle_raw_data[size] = 0;
543
544         if (read_count != size) {
545                 d_free(subtitle_raw_data);
546                 return 0;
547         }
548
549         p = subtitle_raw_data;
550
551         while (p && p < subtitle_raw_data+size) {
552                 char *endp;
553
554                 endp = strchr(p,'\n'); 
555                 if (endp) {
556                         if (endp[-1] == '\r')
557                                 endp[-1] = 0;           //handle 0d0a pair
558                         *endp = 0;                      //string termintor
559                 }
560
561                 if (have_binary)
562                         decode_text_line(p);
563
564                 if (*p != ';') {
565                         Subtitles[Num_subtitles].first_frame = atoi(p);
566                         p = next_field(p); if (!p) continue;
567                         Subtitles[Num_subtitles].last_frame = atoi(p);
568                         p = next_field(p); if (!p) continue;
569                         Subtitles[Num_subtitles].msg = p;
570
571                         Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
572                         Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
573
574                         Num_subtitles++;
575                 }
576
577                 p = endp+1;
578
579         }
580
581         return 1;
582 }
583
584
585 void close_subtitles()
586 {
587         if (subtitle_raw_data)
588                 d_free(subtitle_raw_data);
589         subtitle_raw_data = NULL;
590         Num_subtitles = 0;
591 }
592
593
594 //draw the subtitles for this frame
595 void draw_subtitles(int frame_num)
596 {
597         static int active_subtitles[MAX_ACTIVE_SUBTITLES];
598         static int num_active_subtitles,next_subtitle,line_spacing;
599         int t,y;
600         int must_erase=0;
601
602         if (frame_num == 0) {
603                 num_active_subtitles = 0;
604                 next_subtitle = 0;
605                 gr_set_curfont( GAME_FONT );
606                 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
607                 gr_set_fontcolor(255,-1);
608         }
609
610         //get rid of any subtitles that have expired
611         for (t=0;t<num_active_subtitles;)
612                 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
613                         int t2;
614                         for (t2=t;t2<num_active_subtitles-1;t2++)
615                                 active_subtitles[t2] = active_subtitles[t2+1];
616                         num_active_subtitles--;
617                         must_erase = 1;
618                 }
619                 else
620                         t++;
621
622         //get any subtitles new for this frame 
623         while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
624                 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
625                         Error("Too many active subtitles!");
626                 active_subtitles[num_active_subtitles++] = next_subtitle;
627                 next_subtitle++;
628         }
629
630         //find y coordinate for first line of subtitles
631         y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
632
633         //erase old subtitles if necessary
634         if (must_erase) {
635                 gr_setcolor(0);
636                 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
637         }
638
639         //now draw the current subtitles
640         for (t=0;t<num_active_subtitles;t++)
641                 if (active_subtitles[t] != -1) {
642                         gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
643                         y += line_spacing+1;
644                 }
645 }
646
647
648 movielib *init_new_movie_lib(char *filename,FILE *fp)
649 {
650         int nfiles,offset;
651         int i,n;
652         movielib *table;
653
654         //read movie file header
655
656         nfiles = file_read_int(fp);             //get number of files
657
658         //table = d_malloc(sizeof(*table) + sizeof(ml_entry)*nfiles);
659         MALLOC(table, movielib, 1);
660         MALLOC(table->movies, ml_entry, nfiles);
661
662         strcpy(table->name,filename);
663         table->n_movies = nfiles;
664
665         offset = 4+4+nfiles*(13+4);     //id + nfiles + nfiles * (filename + size)
666
667         for (i=0;i<nfiles;i++) {
668                 int len;
669
670                 n = fread( table->movies[i].name, 13, 1, fp );
671                 if ( n != 1 )
672                         break;          //end of file (probably)
673
674                 len = file_read_int(fp);
675
676                 table->movies[i].len = len;
677                 table->movies[i].offset = offset;
678
679                 offset += table->movies[i].len;
680
681         }
682
683         fclose(fp);
684
685         table->flags = 0;
686
687         return table;
688
689 }
690
691
692 movielib *init_old_movie_lib(char *filename,FILE *fp)
693 {
694         int nfiles,size;
695         int i;
696         movielib *table,*table2;
697
698         nfiles = 0;
699
700         //allocate big table
701         table = d_malloc(sizeof(*table) + sizeof(ml_entry)*MAX_MOVIES_PER_LIB);
702
703         while( 1 ) {
704                 int len;
705
706                 i = fread( table->movies[nfiles].name, 13, 1, fp );
707                 if ( i != 1 )
708                         break;          //end of file (probably)
709
710                 i = fread( &len, 4, 1, fp );
711                 if ( i != 1 )
712                         Error("error reading movie library <%s>",filename);
713
714                 table->movies[nfiles].len = INTEL_INT(len);
715                 table->movies[nfiles].offset = ftell( fp );
716
717                 fseek( fp, INTEL_INT(len), SEEK_CUR );          //skip data
718
719                 nfiles++;
720         }
721
722         //allocate correct-sized table
723         size = sizeof(*table) + sizeof(ml_entry)*nfiles;
724         table2 = d_malloc(size);
725         memcpy(table2,table,size);
726         d_free(table);
727         table = table2;
728
729         strcpy(table->name,filename);
730
731         table->n_movies = nfiles;
732
733         fclose(fp);
734
735         table->flags = 0;
736
737         return table;
738
739 }
740
741
742 //find the specified movie library, and read in list of movies in it
743 movielib *init_movie_lib(char *filename)
744 {
745         //note: this based on cfile_init_hogfile()
746
747         char id[4];
748         FILE * fp;
749
750         fp = fopen( filename, "rb" );
751
752         if ((fp == NULL) && (AltHogdir_initialized)) {
753                 char temp[128];
754                 strcpy(temp, AltHogDir);
755                 strcat(temp, "/");
756                 strcat(temp, filename);
757                 fp = fopen(temp, "rb");
758         }
759
760         if ( fp == NULL )
761                 return NULL;
762
763         fread( id, 4, 1, fp );
764         if ( !strncmp( id, "DMVL", 4 ) )
765                 return init_new_movie_lib(filename,fp);
766         else if ( !strncmp( id, "DHF", 3 ) ) {
767                 fseek(fp,-1,SEEK_CUR);          //old file had 3 char id
768                 return init_old_movie_lib(filename,fp);
769         }
770         else {
771                 fclose(fp);
772                 return NULL;
773         }
774 }
775
776
777 void close_movie(int i)
778 {
779         if (movie_libs[i]) {
780                 d_free(movie_libs[i]->movies);
781                 d_free(movie_libs[i]);
782         }
783 }
784
785
786 void close_movies()
787 {
788         int i;
789
790         for (i=0;i<N_MOVIE_LIBS;i++)
791                 close_movie(i);
792 }
793
794
795 //ask user to put the D2 CD in.
796 //returns -1 if ESC pressed, 0 if OK chosen
797 //CD may not have been inserted
798 int request_cd(void)
799 {
800 #if 0
801         ubyte save_pal[256*3];
802         grs_canvas *save_canv,*tcanv;
803         int ret,was_faded=gr_palette_faded_out;
804
805         gr_palette_clear();
806
807         save_canv = grd_curcanv;
808         tcanv = gr_create_canvas(grd_curcanv->cv_w,grd_curcanv->cv_h);
809
810         gr_set_current_canvas(tcanv);
811         gr_ubitmap(0,0,&save_canv->cv_bitmap);
812         gr_set_current_canvas(save_canv);
813
814         gr_clear_canvas(BM_XRGB(0,0,0));
815
816         memcpy(save_pal,gr_palette,sizeof(save_pal));
817
818         memcpy(gr_palette,last_palette_for_color_fonts,sizeof(gr_palette));
819
820  try_again:;
821
822         ret = nm_messagebox( "CD ERROR", 1, "Ok", "Please insert your Descent II CD");
823
824         if (ret == -1) {
825                 int ret2;
826
827                 ret2 = nm_messagebox( "CD ERROR", 2, "Try Again", "Leave Game", "You must insert your\nDescent II CD to Continue");
828
829                 if (ret2 == -1 || ret2 == 0)
830                         goto try_again;
831         }
832
833         force_rb_register = 1;  //disc has changed; force register new CD
834
835         gr_palette_clear();
836
837         memcpy(gr_palette,save_pal,sizeof(save_pal));
838
839         gr_ubitmap(0,0,&tcanv->cv_bitmap);
840
841         if (!was_faded)
842                 gr_palette_load(gr_palette);
843
844         gr_free_canvas(tcanv);
845
846         return ret;
847 #else
848         con_printf(DEBUG_LEVEL, "STUB: movie: request_cd\n");
849         return 0;
850 #endif
851 }
852
853
854 void init_movie(char *filename,int libnum,int is_robots,int required)
855 {
856         int high_res;
857         int try = 0;
858
859 #ifndef RELEASE
860         if (FindArg("-nomovies")) {
861                 movie_libs[libnum] = NULL;
862                 return;
863         }
864 #endif
865
866         //for robots, load highres versions if highres menus set
867         if (is_robots)
868                 high_res = MenuHiresAvailable;
869         else
870                 high_res = MovieHires;
871
872         if (high_res)
873                 strchr(filename,'.')[-1] = 'h';
874
875 try_again:;
876
877         if ((movie_libs[libnum] = init_movie_lib(filename)) == NULL) {
878                 char name2[100];
879
880                 strcpy(name2,CDROM_dir);
881                 strcat(name2,filename);
882                 movie_libs[libnum] = init_movie_lib(name2);
883
884                 if (movie_libs[libnum] != NULL)
885                         movie_libs[libnum]->flags |= MLF_ON_CD;
886                 else {
887                         if (required) {
888                                 Warning("Cannot open movie file <%s>\n",filename);
889                         }
890
891                         if (!try) {                                         // first try
892                                 if (strchr(filename, '.')[-1] == 'h') {         // try again with lowres
893                                         strchr(filename, '.')[-1] = 'l';
894                                         high_res = 0;
895                                         Warning("Trying to open movie file <%s> instead\n", filename);
896                                         try++;
897                                         goto try_again;
898                                 } else if (strchr(filename, '.')[-1] == 'l') {  // try again with highres
899                                         strchr(filename, '.')[-1] = 'h';
900                                         high_res = 1;
901                                         Warning("Trying to open movie file <%s> instead\n", filename);
902                                         try++;
903                                         goto try_again;
904                                 }
905                         }
906                 }
907         }
908
909         if (is_robots && movie_libs[libnum]!=NULL)
910                 robot_movies = high_res?2:1;
911 }
912
913
914 //find and initialize the movie libraries
915 void init_movies()
916 {
917         int i;
918         int is_robots;
919
920         for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
921
922                 if (!strnicmp(movielib_files[i],"robot",5))
923                         is_robots = 1;
924                 else
925                         is_robots = 0;
926
927                 init_movie(movielib_files[i],i,is_robots,1);
928         }
929
930         movie_libs[EXTRA_ROBOT_LIB] = NULL;
931
932         atexit(close_movies);
933 }
934
935
936 void init_extra_robot_movie(char *filename)
937 {
938         close_movie(EXTRA_ROBOT_LIB);
939         init_movie(filename,EXTRA_ROBOT_LIB,1,0);
940 }
941
942
943 int movie_handle,movie_start;
944
945 //looks through a movie library for a movie file
946 //returns filehandle, with fileposition at movie, or -1 if can't find
947 int search_movie_lib(movielib *lib,char *filename,int must_have)
948 {
949         int i;
950         int filehandle;
951
952         if (lib == NULL)
953                 return -1;
954
955         for (i=0;i<lib->n_movies;i++)
956                 if (!stricmp(filename,lib->movies[i].name)) {   //found the movie in a library 
957                         int from_cd;
958
959                         from_cd = (lib->flags & MLF_ON_CD);
960
961                         if (from_cd)
962                                 songs_stop_redbook();           //ready to read from CD
963
964                         do {            //keep trying until we get the file handle
965
966 #ifdef O_BINARY
967                                 movie_handle = filehandle = open(lib->name, O_RDONLY | O_BINARY);
968 #else
969                                 movie_handle = filehandle = open(lib->name, O_RDONLY);
970 #endif
971
972                                 if ((filehandle == -1) && (AltHogdir_initialized)) {
973                                         char temp[128];
974                                         strcpy(temp, AltHogDir);
975                                         strcat(temp, "/");
976                                         strcat(temp, lib->name);
977 #ifdef O_BINARY
978                                         movie_handle = filehandle = open(temp, O_RDONLY | O_BINARY);
979 #else
980                                         movie_handle = filehandle = open(temp, O_RDONLY);
981 #endif
982                                 }
983
984                                 if (must_have && from_cd && filehandle == -1) {         //didn't get file!
985
986                                         if (request_cd() == -1)         //ESC from requester
987                                                 break;                                          //bail from here. will get error later
988                                 }
989
990                         } while (must_have && from_cd && filehandle == -1);
991
992                         if (filehandle != -1)
993                                 lseek(filehandle,(movie_start=lib->movies[i].offset),SEEK_SET);
994
995                         return filehandle;
996                 }
997
998         return -1;
999 }
1000
1001
1002 //returns file handle
1003 int open_movie_file(char *filename,int must_have)
1004 {
1005         int filehandle,i;
1006
1007         for (i=0;i<N_MOVIE_LIBS;i++) {
1008                 if ((filehandle = search_movie_lib(movie_libs[i],filename,must_have)) != -1)
1009                         return filehandle;
1010         }
1011
1012         return -1;              //couldn't find it
1013 }
1014
1015 //sets the file position to the start of this already-open file
1016 int reset_movie_file(int handle)
1017 {
1018         Assert(handle == movie_handle);
1019
1020         lseek(handle,movie_start,SEEK_SET);
1021
1022         return 0;       //everything is cool
1023 }