]> icculus.org git repositories - btb/d2x.git/blob - main/movie.c
comments/whitespace
[btb/d2x.git] / main / movie.c
1 /* $Id: movie.c,v 1.29 2003-06-22 23:04:54 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.29 2003-06-22 23:04:54 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 "screens.h"
51
52 extern int MenuHiresAvailable;
53 extern char CDROM_dir[];
54
55 #define VID_PLAY 0
56 #define VID_PAUSE 1
57
58 int Vid_State;
59
60
61 // Subtitle data
62 typedef struct {
63         short first_frame,last_frame;
64         char *msg;
65 } subtitle;
66
67 #define MAX_SUBTITLES 500
68 #define MAX_ACTIVE_SUBTITLES 3
69 subtitle Subtitles[MAX_SUBTITLES];
70 int Num_subtitles;
71
72 // Movielib data
73 typedef struct {
74         char name[FILENAME_LEN];
75         int offset,len;
76 } ml_entry;
77
78 #define MLF_ON_CD    1
79 #define MAX_MOVIES_PER_LIB    50    //determines size of malloc
80
81 typedef struct {
82         char     name[100]; //[FILENAME_LEN];
83         int      n_movies;
84         ubyte    flags,pad[3];
85         ml_entry *movies;
86 } movielib;
87
88 #ifdef D2_OEM
89 char movielib_files[][FILENAME_LEN] = {"intro-l.mvl","other-l.mvl","robots-l.mvl","oem-l.mvl"};
90 #else
91 char movielib_files[][FILENAME_LEN] = {"intro-l.mvl","other-l.mvl","robots-l.mvl"};
92 #endif
93
94 #define N_BUILTIN_MOVIE_LIBS (sizeof(movielib_files)/sizeof(*movielib_files))
95 #define N_MOVIE_LIBS (N_BUILTIN_MOVIE_LIBS+1)
96 #define EXTRA_ROBOT_LIB N_BUILTIN_MOVIE_LIBS
97 movielib *movie_libs[N_MOVIE_LIBS];
98
99
100 //do we have the robot movies available
101 int robot_movies = 0; //0 means none, 1 means lowres, 2 means hires
102
103 int MovieHires = 1;   //default is highres
104
105 CFILE *RoboFile = NULL;
106 int RoboFilePos = 0;
107
108 // Function Prototypes
109 int RunMovie(char *filename, int highres_flag, int allow_abort,int dx,int dy);
110
111 CFILE *open_movie_file(char *filename, int must_have);
112 int reset_movie_file(CFILE *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 = cfread(buf, 1, count, (CFILE *)handle);
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         CFILE *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 == NULL)
319         {
320                 if (must_have)
321                         con_printf(CON_NORMAL, "movie: RunMovie: Cannot open movie <%s>\n",filename);
322                 return MOVIE_NOT_PLAYED;
323         }
324
325         MVE_memCallbacks(MPlayAlloc, MPlayFree);
326         MVE_ioCallbacks(FileRead);
327
328         if (hires_flag) {
329                 gr_set_mode(SM(640,480));
330         } else {
331                 gr_set_mode(SM(320,200));
332         }
333 #ifdef OGL
334         set_screen_mode(SCREEN_MENU);
335         gr_copy_palette(pal_save, gr_palette, 768);
336         memset(gr_palette, 0, 768);
337         gr_palette_load(gr_palette);
338 #endif
339
340 #if !defined(POLY_ACC)
341         MVE_sfCallbacks(MovieShowFrame);
342         MVE_palCallbacks(MovieSetPalette);
343 #endif
344
345         if (MVE_rmPrepMovie((void *)filehndl, dx, dy, track)) {
346                 Int3();
347                 return MOVIE_NOT_PLAYED;
348         }
349
350         frame_num = 0;
351
352         FontHires = FontHiresAvailable && hires_flag;
353
354         while((result = MVE_rmStepMovie()) == 0) {
355
356                 draw_subtitles(frame_num);
357
358                 gr_palette_load(gr_palette); // moved this here because of flashing
359
360                 gr_update();
361
362                 key = key_inkey();
363
364                 // If ESCAPE pressed, then quit movie.
365                 if (key == KEY_ESC) {
366                         result = aborted = 1;
367                         break;
368                 }
369
370                 // If PAUSE pressed, then pause movie
371                 if (key == KEY_PAUSE) {
372                         MVE_rmHoldMovie();
373                         show_pause_message(TXT_PAUSE);
374                         while (!key_inkey()) ;
375                         clear_pause_message();
376                 }
377
378 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
379                 if ((key == KEY_CTRLED+KEY_SHIFTED+KEY_PADENTER) ||
380                         (key == KEY_ALTED+KEY_CTRLED+KEY_PADENTER) ||
381                         (key == KEY_ALTED+KEY_SHIFTED+KEY_PADENTER))
382                         gr_toggle_fullscreen();
383 #endif
384
385                 frame_num++;
386         }
387
388         Assert(aborted || result == MVE_ERR_EOF);        ///movie should be over
389
390     MVE_rmEndMovie();
391
392         cfclose(filehndl);                           // Close Movie File
393
394         // Restore old graphic state
395
396         Screen_mode=-1;  //force reset of screen mode
397 #ifdef OGL
398         gr_copy_palette(gr_palette, pal_save, 768);
399         gr_palette_load(pal_save);
400 #endif
401
402         return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
403 }
404
405
406 int InitMovieBriefing()
407 {
408 #if 0
409         if (MenuHires)
410                 gr_set_mode(SM(640,480));
411         else
412                 gr_set_mode(SM(320,200));
413
414         gr_init_sub_canvas( &VR_screen_pages[0], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
415         gr_init_sub_canvas( &VR_screen_pages[1], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
416 #endif
417
418         return 1;
419 }
420
421
422 //returns 1 if frame updated ok
423 int RotateRobot()
424 {
425         int err;
426
427         err = MVE_rmStepMovie();
428
429         gr_palette_load(gr_palette);
430
431         if (err == MVE_ERR_EOF)     //end of movie, so reset
432         {
433                 reset_movie_file(RoboFile);
434                 if (MVE_rmPrepMovie((void *)RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
435                         Int3();
436                         return 0;
437                 }
438         }
439         else if (err) {
440                 Int3();
441                 return 0;
442         }
443
444         return 1;
445 }
446
447
448 void DeInitRobotMovie(void)
449 {
450         MVE_rmEndMovie();
451         cfclose(RoboFile);                           // Close Movie File
452 }
453
454
455 int InitRobotMovie(char *filename)
456 {
457         if (FindArg("-nomovies"))
458                 return 0;
459
460         con_printf(DEBUG_LEVEL, "RoboFile=%s\n", filename);
461
462         MVE_sndInit(-1);        //tell movies to play no sound for robots
463
464         RoboFile = open_movie_file(filename, 1);
465
466         if (RoboFile == NULL)
467         {
468                 Warning("movie: InitRobotMovie: Cannot open movie file <%s>",filename);
469                 return MOVIE_NOT_PLAYED;
470         }
471
472         Vid_State = VID_PLAY;
473
474         if (MVE_rmPrepMovie((void *)RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
475                 Int3();
476                 return 0;
477         }
478
479         RoboFilePos = cfseek(RoboFile, 0L, SEEK_CUR);
480
481         con_printf(DEBUG_LEVEL, "RoboFilePos=%d!\n", RoboFilePos);
482
483         return 1;
484 }
485
486
487 /*
488  *              Subtitle system code
489  */
490
491 ubyte *subtitle_raw_data;
492
493
494 //search for next field following whitespace 
495 ubyte *next_field(ubyte *p)
496 {
497         while (*p && !isspace(*p))
498                 p++;
499
500         if (!*p)
501                 return NULL;
502
503         while (*p && isspace(*p))
504                 p++;
505
506         if (!*p)
507                 return NULL;
508
509         return p;
510 }
511
512
513 int init_subtitles(char *filename)
514 {
515         CFILE *ifile;
516         int size,read_count;
517         ubyte *p;
518         int have_binary = 0;
519
520         Num_subtitles = 0;
521
522         if (! FindArg("-subtitles"))
523                 return 0;
524
525         ifile = cfopen(filename,"rb");          //try text version
526
527         if (!ifile) {                                                           //no text version, try binary version
528                 char filename2[FILENAME_LEN];
529                 change_filename_ext(filename2,filename,".TXB");
530                 ifile = cfopen(filename2,"rb");
531                 if (!ifile)
532                         return 0;
533                 have_binary = 1;
534         }
535
536         size = cfilelength(ifile);
537
538         MALLOC (subtitle_raw_data, ubyte, size+1);
539
540         read_count = cfread(subtitle_raw_data, 1, size, ifile);
541
542         cfclose(ifile);
543
544         subtitle_raw_data[size] = 0;
545
546         if (read_count != size) {
547                 d_free(subtitle_raw_data);
548                 return 0;
549         }
550
551         p = subtitle_raw_data;
552
553         while (p && p < subtitle_raw_data+size) {
554                 char *endp;
555
556                 endp = strchr(p,'\n'); 
557                 if (endp) {
558                         if (endp[-1] == '\r')
559                                 endp[-1] = 0;           //handle 0d0a pair
560                         *endp = 0;                      //string termintor
561                 }
562
563                 if (have_binary)
564                         decode_text_line(p);
565
566                 if (*p != ';') {
567                         Subtitles[Num_subtitles].first_frame = atoi(p);
568                         p = next_field(p); if (!p) continue;
569                         Subtitles[Num_subtitles].last_frame = atoi(p);
570                         p = next_field(p); if (!p) continue;
571                         Subtitles[Num_subtitles].msg = p;
572
573                         Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
574                         Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
575
576                         Num_subtitles++;
577                 }
578
579                 p = endp+1;
580
581         }
582
583         return 1;
584 }
585
586
587 void close_subtitles()
588 {
589         if (subtitle_raw_data)
590                 d_free(subtitle_raw_data);
591         subtitle_raw_data = NULL;
592         Num_subtitles = 0;
593 }
594
595
596 //draw the subtitles for this frame
597 void draw_subtitles(int frame_num)
598 {
599         static int active_subtitles[MAX_ACTIVE_SUBTITLES];
600         static int num_active_subtitles,next_subtitle,line_spacing;
601         int t,y;
602         int must_erase=0;
603
604         if (frame_num == 0) {
605                 num_active_subtitles = 0;
606                 next_subtitle = 0;
607                 gr_set_curfont( GAME_FONT );
608                 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
609                 gr_set_fontcolor(255,-1);
610         }
611
612         //get rid of any subtitles that have expired
613         for (t=0;t<num_active_subtitles;)
614                 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
615                         int t2;
616                         for (t2=t;t2<num_active_subtitles-1;t2++)
617                                 active_subtitles[t2] = active_subtitles[t2+1];
618                         num_active_subtitles--;
619                         must_erase = 1;
620                 }
621                 else
622                         t++;
623
624         //get any subtitles new for this frame 
625         while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
626                 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
627                         Error("Too many active subtitles!");
628                 active_subtitles[num_active_subtitles++] = next_subtitle;
629                 next_subtitle++;
630         }
631
632         //find y coordinate for first line of subtitles
633         y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
634
635         //erase old subtitles if necessary
636         if (must_erase) {
637                 gr_setcolor(0);
638                 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
639         }
640
641         //now draw the current subtitles
642         for (t=0;t<num_active_subtitles;t++)
643                 if (active_subtitles[t] != -1) {
644                         gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
645                         y += line_spacing+1;
646                 }
647 }
648
649
650 movielib *init_new_movie_lib(char *filename, CFILE *fp)
651 {
652         int nfiles,offset;
653         int i,n;
654         movielib *table;
655
656         //read movie file header
657
658         nfiles = cfile_read_int(fp);        //get number of files
659
660         //table = d_malloc(sizeof(*table) + sizeof(ml_entry)*nfiles);
661         MALLOC(table, movielib, 1);
662         MALLOC(table->movies, ml_entry, nfiles);
663
664         strcpy(table->name,filename);
665         table->n_movies = nfiles;
666
667         offset = 4+4+nfiles*(13+4);     //id + nfiles + nfiles * (filename + size)
668
669         for (i=0;i<nfiles;i++) {
670                 int len;
671
672                 n = cfread(table->movies[i].name, 13, 1, fp);
673                 if ( n != 1 )
674                         break;          //end of file (probably)
675
676                 len = cfile_read_int(fp);
677
678                 table->movies[i].len = len;
679                 table->movies[i].offset = offset;
680
681                 offset += table->movies[i].len;
682
683         }
684
685         cfclose(fp);
686
687         table->flags = 0;
688
689         return table;
690
691 }
692
693
694 movielib *init_old_movie_lib(char *filename, CFILE *fp)
695 {
696         int nfiles,size;
697         int i;
698         movielib *table,*table2;
699
700         nfiles = 0;
701
702         //allocate big table
703         table = d_malloc(sizeof(*table) + sizeof(ml_entry)*MAX_MOVIES_PER_LIB);
704
705         while( 1 ) {
706                 int len;
707
708                 i = cfread(table->movies[nfiles].name, 13, 1, fp);
709                 if ( i != 1 )
710                         break;          //end of file (probably)
711
712                 i = cfread(&len, 4, 1, fp);
713                 if ( i != 1 )
714                         Error("error reading movie library <%s>",filename);
715
716                 table->movies[nfiles].len = INTEL_INT(len);
717                 table->movies[nfiles].offset = cftell(fp);
718
719                 cfseek(fp, INTEL_INT(len), SEEK_CUR);       //skip data
720
721                 nfiles++;
722         }
723
724         //allocate correct-sized table
725         size = sizeof(*table) + sizeof(ml_entry)*nfiles;
726         table2 = d_malloc(size);
727         memcpy(table2,table,size);
728         d_free(table);
729         table = table2;
730
731         strcpy(table->name,filename);
732
733         table->n_movies = nfiles;
734
735         cfclose(fp);
736
737         table->flags = 0;
738
739         return table;
740
741 }
742
743
744 //find the specified movie library, and read in list of movies in it
745 movielib *init_movie_lib(char *filename)
746 {
747         //note: this based on cfile_init_hogfile()
748
749         char id[4];
750         CFILE *fp;
751
752         fp = cfopen(filename, "rb");
753
754         if ( fp == NULL )
755                 return NULL;
756
757         cfread(id, 4, 1, fp);
758         if ( !strncmp( id, "DMVL", 4 ) )
759                 return init_new_movie_lib(filename,fp);
760         else if ( !strncmp( id, "DHF", 3 ) ) {
761                 cfseek(fp,-1,SEEK_CUR);         //old file had 3 char id
762                 return init_old_movie_lib(filename,fp);
763         }
764         else {
765                 cfclose(fp);
766                 return NULL;
767         }
768 }
769
770
771 void close_movie(int i)
772 {
773         if (movie_libs[i]) {
774                 d_free(movie_libs[i]->movies);
775                 d_free(movie_libs[i]);
776         }
777 }
778
779
780 void close_movies()
781 {
782         int i;
783
784         for (i=0;i<N_MOVIE_LIBS;i++)
785                 close_movie(i);
786 }
787
788
789 //ask user to put the D2 CD in.
790 //returns -1 if ESC pressed, 0 if OK chosen
791 //CD may not have been inserted
792 int request_cd(void)
793 {
794 #if 0
795         ubyte save_pal[256*3];
796         grs_canvas *save_canv,*tcanv;
797         int ret,was_faded=gr_palette_faded_out;
798
799         gr_palette_clear();
800
801         save_canv = grd_curcanv;
802         tcanv = gr_create_canvas(grd_curcanv->cv_w,grd_curcanv->cv_h);
803
804         gr_set_current_canvas(tcanv);
805         gr_ubitmap(0,0,&save_canv->cv_bitmap);
806         gr_set_current_canvas(save_canv);
807
808         gr_clear_canvas(BM_XRGB(0,0,0));
809
810         memcpy(save_pal,gr_palette,sizeof(save_pal));
811
812         memcpy(gr_palette,last_palette_for_color_fonts,sizeof(gr_palette));
813
814  try_again:;
815
816         ret = nm_messagebox( "CD ERROR", 1, "Ok", "Please insert your Descent II CD");
817
818         if (ret == -1) {
819                 int ret2;
820
821                 ret2 = nm_messagebox( "CD ERROR", 2, "Try Again", "Leave Game", "You must insert your\nDescent II CD to Continue");
822
823                 if (ret2 == -1 || ret2 == 0)
824                         goto try_again;
825         }
826
827         force_rb_register = 1;  //disc has changed; force register new CD
828
829         gr_palette_clear();
830
831         memcpy(gr_palette,save_pal,sizeof(save_pal));
832
833         gr_ubitmap(0,0,&tcanv->cv_bitmap);
834
835         if (!was_faded)
836                 gr_palette_load(gr_palette);
837
838         gr_free_canvas(tcanv);
839
840         return ret;
841 #else
842         con_printf(DEBUG_LEVEL, "STUB: movie: request_cd\n");
843         return 0;
844 #endif
845 }
846
847
848 void init_movie(char *filename,int libnum,int is_robots,int required)
849 {
850         int high_res;
851         int try = 0;
852
853 #ifndef RELEASE
854         if (FindArg("-nomovies")) {
855                 movie_libs[libnum] = NULL;
856                 return;
857         }
858 #endif
859
860         //for robots, load highres versions if highres menus set
861         if (is_robots)
862                 high_res = MenuHiresAvailable;
863         else
864                 high_res = MovieHires;
865
866         if (high_res)
867                 strchr(filename,'.')[-1] = 'h';
868
869 try_again:;
870
871         if ((movie_libs[libnum] = init_movie_lib(filename)) == NULL) {
872                 char name2[100];
873
874                 strcpy(name2,CDROM_dir);
875                 strcat(name2,filename);
876                 movie_libs[libnum] = init_movie_lib(name2);
877
878                 if (movie_libs[libnum] != NULL)
879                         movie_libs[libnum]->flags |= MLF_ON_CD;
880                 else {
881                         if (required) {
882                                 Warning("Cannot open movie file <%s>\n",filename);
883                         }
884
885                         if (!try) {                                         // first try
886                                 if (strchr(filename, '.')[-1] == 'h') {         // try again with lowres
887                                         strchr(filename, '.')[-1] = 'l';
888                                         high_res = 0;
889                                         Warning("Trying to open movie file <%s> instead\n", filename);
890                                         try++;
891                                         goto try_again;
892                                 } else if (strchr(filename, '.')[-1] == 'l') {  // try again with highres
893                                         strchr(filename, '.')[-1] = 'h';
894                                         high_res = 1;
895                                         Warning("Trying to open movie file <%s> instead\n", filename);
896                                         try++;
897                                         goto try_again;
898                                 }
899                         }
900                 }
901         }
902
903         if (is_robots && movie_libs[libnum]!=NULL)
904                 robot_movies = high_res?2:1;
905 }
906
907
908 //find and initialize the movie libraries
909 void init_movies()
910 {
911         int i;
912         int is_robots;
913
914         for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
915
916                 if (!strnicmp(movielib_files[i],"robot",5))
917                         is_robots = 1;
918                 else
919                         is_robots = 0;
920
921                 init_movie(movielib_files[i],i,is_robots,1);
922         }
923
924         movie_libs[EXTRA_ROBOT_LIB] = NULL;
925
926         atexit(close_movies);
927 }
928
929
930 void init_extra_robot_movie(char *filename)
931 {
932         close_movie(EXTRA_ROBOT_LIB);
933         init_movie(filename,EXTRA_ROBOT_LIB,1,0);
934 }
935
936
937 CFILE *movie_handle;
938 int movie_start;
939
940 //looks through a movie library for a movie file
941 //returns filehandle, with fileposition at movie, or -1 if can't find
942 CFILE *search_movie_lib(movielib *lib, char *filename, int must_have)
943 {
944         int i;
945         CFILE *filehandle;
946
947         if (lib == NULL)
948                 return NULL;
949
950         for (i=0;i<lib->n_movies;i++)
951                 if (!stricmp(filename,lib->movies[i].name)) {   //found the movie in a library 
952                         int from_cd;
953
954                         from_cd = (lib->flags & MLF_ON_CD);
955
956                         if (from_cd)
957                                 songs_stop_redbook();           //ready to read from CD
958
959                         do {            //keep trying until we get the file handle
960
961                                 movie_handle = filehandle = cfopen(lib->name, "rb");
962
963                                 if (must_have && from_cd && filehandle == NULL)
964                                 {   //didn't get file!
965
966                                         if (request_cd() == -1)         //ESC from requester
967                                                 break;                                          //bail from here. will get error later
968                                 }
969
970                         } while (must_have && from_cd && filehandle == NULL);
971
972                         if (filehandle)
973                                 cfseek(filehandle, (movie_start = lib->movies[i].offset), SEEK_SET);
974
975                         return filehandle;
976                 }
977
978         return NULL;
979 }
980
981
982 //returns file handle
983 CFILE *open_movie_file(char *filename, int must_have)
984 {
985         CFILE *filehandle;
986         int i;
987
988         for (i=0;i<N_MOVIE_LIBS;i++) {
989                 if ((filehandle = search_movie_lib(movie_libs[i], filename, must_have)) != NULL)
990                         return filehandle;
991         }
992
993         return NULL;    //couldn't find it
994 }
995
996 //sets the file position to the start of this already-open file
997 int reset_movie_file(CFILE *handle)
998 {
999         Assert(handle == movie_handle);
1000
1001         cfseek(handle, movie_start, SEEK_SET);
1002
1003         return 0;       //everything is cool
1004 }