]> icculus.org git repositories - btb/d2x.git/blob - main/movie.c
84e4a13598f0a7676be82d0bb742f82eb7ec9b04
[btb/d2x.git] / main / movie.c
1 /* $Id: movie.c,v 1.19 2003-02-19 03:09:38 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.19 2003-02-19 03:09:38 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
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
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 = 0;   //default for now is lores
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
121
122 //filename will actually get modified to be either low-res or high-res
123 //returns status.  see values in movie.h
124 int PlayMovie(const char *filename, int must_have)
125 {
126         char name[FILENAME_LEN],*p;
127         int c, ret;
128
129 #ifndef RELEASE
130         if (FindArg("-nomovies"))
131                 return MOVIE_NOT_PLAYED;
132 #endif
133
134         strcpy(name,filename);
135
136         if ((p=strchr(name,'.')) == NULL)               //add extension, if missing
137                 strcat(name,".mve");
138
139         //check for escape already pressed & abort if so
140         while ((c=key_inkey()) != 0)
141                 if (c == KEY_ESC)
142                         return MOVIE_ABORTED;
143
144         // Stop all digital sounds currently playing.
145         digi_stop_all();
146
147         // Stop all songs
148         songs_stop_all();
149
150         digi_close();
151
152         // Start sound
153         if (!FindArg("-nosound"))
154                 MVE_sndInit(1);
155         else
156                 MVE_sndInit(-1);
157
158         ret = RunMovie(name,MovieHires,must_have,-1,-1);
159
160         if (!FindArg("-nosound"))
161                 digi_init();
162
163         Screen_mode = -1;               //force screen reset
164
165         return ret;
166 }
167
168
169 #if 0
170 typedef struct bkg {
171         short x, y, w, h;           // The location of the menu.
172         grs_bitmap * bmp;               // The background under the menu.
173 } bkg;
174
175 bkg movie_bg = {0,0,0,0,NULL};
176 #endif
177
178 #define BOX_BORDER (MenuHires?40:20)
179
180
181 void show_pause_message(char *msg)
182 {
183         int w,h,aw;
184         int x,y;
185
186         gr_set_current_canvas(NULL);
187         gr_set_curfont( SMALL_FONT );
188
189         gr_get_string_size(msg,&w,&h,&aw);
190
191         x = (grd_curscreen->sc_w-w)/2;
192         y = (grd_curscreen->sc_h-h)/2;
193
194 #if 0
195         if (movie_bg.bmp) {
196                 gr_free_bitmap(movie_bg.bmp);
197                 movie_bg.bmp = NULL;
198         }
199
200         // Save the background of the display
201         movie_bg.x=x; movie_bg.y=y; movie_bg.w=w; movie_bg.h=h;
202
203         movie_bg.bmp = gr_create_bitmap( w+BOX_BORDER, h+BOX_BORDER );
204
205         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 );
206 #endif
207
208         gr_setcolor(0);
209         gr_rect(x-BOX_BORDER/2,y-BOX_BORDER/2,x+w+BOX_BORDER/2-1,y+h+BOX_BORDER/2-1);
210
211         gr_set_fontcolor( 255, -1 );
212
213         gr_ustring( 0x8000, y, msg );
214
215         gr_update();
216 }
217
218 void clear_pause_message()
219 {
220 #if 0
221         if (movie_bg.bmp) {
222
223                 gr_bitmap(movie_bg.x-BOX_BORDER/2, movie_bg.y-BOX_BORDER/2, movie_bg.bmp);
224
225                 gr_free_bitmap(movie_bg.bmp);
226                 movie_bg.bmp = NULL;
227         }
228 #endif
229 }
230
231
232 //returns status.  see movie.h
233 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
234 {
235         int filehndl;
236         int result=1,aborted=0;
237         int track = 0;
238         int frame_num;
239         int key;
240
241         result=1;
242
243         // Open Movie file.  If it doesn't exist, no movie, just return.
244
245         filehndl = open_movie_file(filename,must_have);
246
247         if (filehndl == -1) {
248                 if (must_have)
249                         Warning("movie: RunMovie: Cannot open movie <%s>\n",filename);
250                 return MOVIE_NOT_PLAYED;
251         }
252
253         if (hires_flag) {
254                 gr_set_mode(SM(640,480));
255         } else {
256                 gr_set_mode(SM(320,200));
257         }
258
259         if (MVE_rmPrepMovie(filehndl, dx, dy, track)) {
260                 Int3();
261                 return MOVIE_NOT_PLAYED;
262         }
263
264         frame_num = 0;
265
266         FontHires = FontHiresAvailable && hires_flag;
267
268         while((result = MVE_rmStepMovie()) == 0) {
269
270                 draw_subtitles(frame_num);
271
272                 gr_update();
273
274                 key = key_inkey();
275
276                 // If ESCAPE pressed, then quit movie.
277                 if (key == KEY_ESC) {
278                         result = aborted = 1;
279                         break;
280                 }
281
282                 // If PAUSE pressed, then pause movie
283                 if (key == KEY_PAUSE) {
284                         MVE_rmHoldMovie();
285                         show_pause_message(TXT_PAUSE);
286                         while (!key_inkey()) ;
287                         clear_pause_message();
288                 }
289
290 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
291                 if ((key == KEY_CTRLED+KEY_SHIFTED+KEY_PADENTER) ||
292                         (key == KEY_ALTED+KEY_CTRLED+KEY_PADENTER) ||
293                         (key == KEY_ALTED+KEY_SHIFTED+KEY_PADENTER))
294                         gr_toggle_fullscreen();
295 #endif
296
297                 frame_num++;
298         }
299
300         Assert(aborted || result == MVE_ERR_EOF);        ///movie should be over
301
302     MVE_rmEndMovie();
303
304         close(filehndl);                           // Close Movie File
305
306         // Restore old graphic state
307
308         Screen_mode=-1;  //force reset of screen mode
309
310         return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
311 }
312
313
314 int InitMovieBriefing()
315 {
316         if (MenuHires)
317                 gr_set_mode(SM(640,480));
318         else
319                 gr_set_mode(SM(320,200));
320
321         return 1;
322 }
323
324
325 //returns 1 if frame updated ok
326 int RotateRobot()
327 {
328         int err;
329
330         err = MVE_rmStepMovie();
331
332         if (err == MVE_ERR_EOF)     //end of movie, so reset
333         {
334                 reset_movie_file(RoboFile);
335                 if (MVE_rmPrepMovie(RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
336                         Int3();
337                         return 0;
338                 }
339         }
340         else if (err) {
341                 Int3();
342                 return 0;
343         }
344
345         return 1;
346 }
347
348
349 void DeInitRobotMovie(void)
350 {
351         MVE_rmEndMovie();
352         close(RoboFile);                           // Close Movie File
353 }
354
355
356 int InitRobotMovie(char *filename)
357 {
358         if (FindArg("-nomovies"))
359                 return 0;
360
361         con_printf(DEBUG_LEVEL, "RoboFile=%s\n", filename);
362
363         MVE_sndInit(-1);        //tell movies to play no sound for robots
364
365         RoboFile = open_movie_file(filename, 1);
366
367         if (RoboFile == -1) {
368                 Warning("movie: InitRobotMovie: Cannot open movie file <%s>",filename);
369                 return MOVIE_NOT_PLAYED;
370         }
371
372         Vid_State = VID_PLAY;
373
374         if (MVE_rmPrepMovie(RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
375                 Int3();
376                 return 0;
377         }
378
379         RoboFilePos=lseek (RoboFile,0L,SEEK_CUR);
380
381         con_printf(DEBUG_LEVEL, "RoboFilePos=%d!\n", RoboFilePos);
382
383         return 1;
384 }
385
386
387 /*
388  *              Subtitle system code
389  */
390
391 ubyte *subtitle_raw_data;
392
393
394 //search for next field following whitespace 
395 ubyte *next_field(ubyte *p)
396 {
397         while (*p && !isspace(*p))
398                 p++;
399
400         if (!*p)
401                 return NULL;
402
403         while (*p && isspace(*p))
404                 p++;
405
406         if (!*p)
407                 return NULL;
408
409         return p;
410 }
411
412
413 int init_subtitles(char *filename)
414 {
415         CFILE *ifile;
416         int size,read_count;
417         ubyte *p;
418         int have_binary = 0;
419
420         Num_subtitles = 0;
421
422         if (! FindArg("-subtitles"))
423                 return 0;
424
425         ifile = cfopen(filename,"rb");          //try text version
426
427         if (!ifile) {                                                           //no text version, try binary version
428                 char filename2[FILENAME_LEN];
429                 change_filename_ext(filename2,filename,".TXB");
430                 ifile = cfopen(filename2,"rb");
431                 if (!ifile)
432                         return 0;
433                 have_binary = 1;
434         }
435
436         size = cfilelength(ifile);
437
438         MALLOC (subtitle_raw_data, ubyte, size+1);
439
440         read_count = cfread(subtitle_raw_data, 1, size, ifile);
441
442         cfclose(ifile);
443
444         subtitle_raw_data[size] = 0;
445
446         if (read_count != size) {
447                 d_free(subtitle_raw_data);
448                 return 0;
449         }
450
451         p = subtitle_raw_data;
452
453         while (p && p < subtitle_raw_data+size) {
454                 char *endp;
455
456                 endp = strchr(p,'\n'); 
457                 if (endp) {
458                         if (endp[-1] == '\r')
459                                 endp[-1] = 0;           //handle 0d0a pair
460                         *endp = 0;                      //string termintor
461                 }
462
463                 if (have_binary)
464                         decode_text_line(p);
465
466                 if (*p != ';') {
467                         Subtitles[Num_subtitles].first_frame = atoi(p);
468                         p = next_field(p); if (!p) continue;
469                         Subtitles[Num_subtitles].last_frame = atoi(p);
470                         p = next_field(p); if (!p) continue;
471                         Subtitles[Num_subtitles].msg = p;
472
473                         Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
474                         Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
475
476                         Num_subtitles++;
477                 }
478
479                 p = endp+1;
480
481         }
482
483         return 1;
484 }
485
486
487 void close_subtitles()
488 {
489         if (subtitle_raw_data)
490                 d_free(subtitle_raw_data);
491         subtitle_raw_data = NULL;
492         Num_subtitles = 0;
493 }
494
495
496 //draw the subtitles for this frame
497 void draw_subtitles(int frame_num)
498 {
499         static int active_subtitles[MAX_ACTIVE_SUBTITLES];
500         static int num_active_subtitles,next_subtitle,line_spacing;
501         int t,y;
502         int must_erase=0;
503
504         if (frame_num == 0) {
505                 num_active_subtitles = 0;
506                 next_subtitle = 0;
507                 gr_set_curfont( GAME_FONT );
508                 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
509                 gr_set_fontcolor(255,-1);
510         }
511
512         //get rid of any subtitles that have expired
513         for (t=0;t<num_active_subtitles;)
514                 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
515                         int t2;
516                         for (t2=t;t2<num_active_subtitles-1;t2++)
517                                 active_subtitles[t2] = active_subtitles[t2+1];
518                         num_active_subtitles--;
519                         must_erase = 1;
520                 }
521                 else
522                         t++;
523
524         //get any subtitles new for this frame 
525         while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
526                 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
527                         Error("Too many active subtitles!");
528                 active_subtitles[num_active_subtitles++] = next_subtitle;
529                 next_subtitle++;
530         }
531
532         //find y coordinate for first line of subtitles
533         y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
534
535         //erase old subtitles if necessary
536         if (must_erase) {
537                 gr_setcolor(0);
538                 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
539         }
540
541         //now draw the current subtitles
542         for (t=0;t<num_active_subtitles;t++)
543                 if (active_subtitles[t] != -1) {
544                         gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
545                         y += line_spacing+1;
546                 }
547 }
548
549
550 movielib *init_new_movie_lib(char *filename,FILE *fp)
551 {
552         int nfiles,offset;
553         int i,n;
554         movielib *table;
555
556         //read movie file header
557
558         nfiles = file_read_int(fp);             //get number of files
559
560         //table = d_malloc(sizeof(*table) + sizeof(ml_entry)*nfiles);
561         MALLOC(table, movielib, 1);
562         MALLOC(table->movies, ml_entry, nfiles);
563
564         strcpy(table->name,filename);
565         table->n_movies = nfiles;
566
567         offset = 4+4+nfiles*(13+4);     //id + nfiles + nfiles * (filename + size)
568
569         for (i=0;i<nfiles;i++) {
570                 int len;
571
572                 n = fread( table->movies[i].name, 13, 1, fp );
573                 if ( n != 1 )
574                         break;          //end of file (probably)
575
576                 len = file_read_int(fp);
577
578                 table->movies[i].len = len;
579                 table->movies[i].offset = offset;
580
581                 offset += table->movies[i].len;
582
583         }
584
585         fclose(fp);
586
587         table->flags = 0;
588
589         return table;
590
591 }
592
593
594 movielib *init_old_movie_lib(char *filename,FILE *fp)
595 {
596         int nfiles,size;
597         int i;
598         movielib *table,*table2;
599
600         nfiles = 0;
601
602         //allocate big table
603         table = d_malloc(sizeof(*table) + sizeof(ml_entry)*MAX_MOVIES_PER_LIB);
604
605         while( 1 ) {
606                 int len;
607
608                 i = fread( table->movies[nfiles].name, 13, 1, fp );
609                 if ( i != 1 )
610                         break;          //end of file (probably)
611
612                 i = fread( &len, 4, 1, fp );
613                 if ( i != 1 )
614                         Error("error reading movie library <%s>",filename);
615
616                 table->movies[nfiles].len = INTEL_INT(len);
617                 table->movies[nfiles].offset = ftell( fp );
618
619                 fseek( fp, INTEL_INT(len), SEEK_CUR );          //skip data
620
621                 nfiles++;
622         }
623
624         //allocate correct-sized table
625         size = sizeof(*table) + sizeof(ml_entry)*nfiles;
626         table2 = d_malloc(size);
627         memcpy(table2,table,size);
628         d_free(table);
629         table = table2;
630
631         strcpy(table->name,filename);
632
633         table->n_movies = nfiles;
634
635         fclose(fp);
636
637         table->flags = 0;
638
639         return table;
640
641 }
642
643
644 //find the specified movie library, and read in list of movies in it
645 movielib *init_movie_lib(char *filename)
646 {
647         //note: this based on cfile_init_hogfile()
648
649         char id[4];
650         FILE * fp;
651
652         fp = fopen( filename, "rb" );
653
654         if ((fp == NULL) && (AltHogdir_initialized)) {
655                 char temp[128];
656                 strcpy(temp, AltHogDir);
657                 strcat(temp, "/");
658                 strcat(temp, filename);
659                 fp = fopen(temp, "rb");
660         }
661
662         if ( fp == NULL )
663                 return NULL;
664
665         fread( id, 4, 1, fp );
666         if ( !strncmp( id, "DMVL", 4 ) )
667                 return init_new_movie_lib(filename,fp);
668         else if ( !strncmp( id, "DHF", 3 ) ) {
669                 fseek(fp,-1,SEEK_CUR);          //old file had 3 char id
670                 return init_old_movie_lib(filename,fp);
671         }
672         else {
673                 fclose(fp);
674                 return NULL;
675         }
676 }
677
678
679 void close_movie(int i)
680 {
681         if (movie_libs[i]) {
682                 d_free(movie_libs[i]->movies);
683                 d_free(movie_libs[i]);
684         }
685 }
686
687
688 void close_movies()
689 {
690         int i;
691
692         for (i=0;i<N_MOVIE_LIBS;i++)
693                 close_movie(i);
694 }
695
696
697 //ask user to put the D2 CD in.
698 //returns -1 if ESC pressed, 0 if OK chosen
699 //CD may not have been inserted
700 int request_cd(void)
701 {
702 #if 0
703         ubyte save_pal[256*3];
704         grs_canvas *save_canv,*tcanv;
705         int ret,was_faded=gr_palette_faded_out;
706
707         gr_palette_clear();
708
709         save_canv = grd_curcanv;
710         tcanv = gr_create_canvas(grd_curcanv->cv_w,grd_curcanv->cv_h);
711
712         gr_set_current_canvas(tcanv);
713         gr_ubitmap(0,0,&save_canv->cv_bitmap);
714         gr_set_current_canvas(save_canv);
715
716         gr_clear_canvas(BM_XRGB(0,0,0));
717
718         memcpy(save_pal,gr_palette,sizeof(save_pal));
719
720         memcpy(gr_palette,last_palette_for_color_fonts,sizeof(gr_palette));
721
722  try_again:;
723
724         ret = nm_messagebox( "CD ERROR", 1, "Ok", "Please insert your Descent II CD");
725
726         if (ret == -1) {
727                 int ret2;
728
729                 ret2 = nm_messagebox( "CD ERROR", 2, "Try Again", "Leave Game", "You must insert your\nDescent II CD to Continue");
730
731                 if (ret2 == -1 || ret2 == 0)
732                         goto try_again;
733         }
734
735         force_rb_register = 1;  //disc has changed; force register new CD
736
737         gr_palette_clear();
738
739         memcpy(gr_palette,save_pal,sizeof(save_pal));
740
741         gr_ubitmap(0,0,&tcanv->cv_bitmap);
742
743         if (!was_faded)
744                 gr_palette_load(gr_palette);
745
746         gr_free_canvas(tcanv);
747
748         return ret;
749 #else
750         con_printf(DEBUG_LEVEL, "STUB: movie: request_cd\n");
751         return 0;
752 #endif
753 }
754
755
756 void init_movie(char *filename,int libnum,int is_robots,int required)
757 {
758         int high_res;
759         int try = 0;
760
761 #ifndef RELEASE
762         if (FindArg("-nomovies")) {
763                 movie_libs[libnum] = NULL;
764                 return;
765         }
766 #endif
767
768         //for robots, load highres versions if highres menus set
769         if (is_robots)
770                 high_res = MenuHiresAvailable;
771         else
772                 high_res = MovieHires;
773
774         if (high_res)
775                 strchr(filename,'.')[-1] = 'h';
776
777 try_again:;
778
779         if ((movie_libs[libnum] = init_movie_lib(filename)) == NULL) {
780                 char name2[100];
781
782                 strcpy(name2,CDROM_dir);
783                 strcat(name2,filename);
784                 movie_libs[libnum] = init_movie_lib(name2);
785
786                 if (movie_libs[libnum] != NULL)
787                         movie_libs[libnum]->flags |= MLF_ON_CD;
788                 else {
789                         if (required) {
790                                 Warning("Cannot open movie file <%s>\n",filename);
791                         }
792
793                         if (!try) {                                         // first try
794                                 if (strchr(filename, '.')[-1] == 'h') {         // try again with lowres
795                                         strchr(filename, '.')[-1] = 'l';
796                                         high_res = 0;
797                                         Warning("Trying to open movie file <%s> instead\n", filename);
798                                         try++;
799                                         goto try_again;
800                                 } else if (strchr(filename, '.')[-1] == 'l') {  // try again with highres
801                                         strchr(filename, '.')[-1] = 'h';
802                                         high_res = 1;
803                                         Warning("Trying to open movie file <%s> instead\n", filename);
804                                         try++;
805                                         goto try_again;
806                                 }
807                         }
808                 }
809         }
810
811         if (is_robots && movie_libs[libnum]!=NULL)
812                 robot_movies = high_res?2:1;
813 }
814
815
816 //find and initialize the movie libraries
817 void init_movies()
818 {
819         int i;
820         int is_robots;
821
822         for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
823
824                 if (!strnicmp(movielib_files[i],"robot",5))
825                         is_robots = 1;
826                 else
827                         is_robots = 0;
828
829                 init_movie(movielib_files[i],i,is_robots,1);
830         }
831
832         movie_libs[EXTRA_ROBOT_LIB] = NULL;
833
834         atexit(close_movies);
835 }
836
837
838 void init_extra_robot_movie(char *filename)
839 {
840         close_movie(EXTRA_ROBOT_LIB);
841         init_movie(filename,EXTRA_ROBOT_LIB,1,0);
842 }
843
844
845 int movie_handle,movie_start;
846
847 //looks through a movie library for a movie file
848 //returns filehandle, with fileposition at movie, or -1 if can't find
849 int search_movie_lib(movielib *lib,char *filename,int must_have)
850 {
851         int i;
852         int filehandle;
853
854         if (lib == NULL)
855                 return -1;
856
857         for (i=0;i<lib->n_movies;i++)
858                 if (!stricmp(filename,lib->movies[i].name)) {   //found the movie in a library 
859                         int from_cd;
860
861                         from_cd = (lib->flags & MLF_ON_CD);
862
863                         if (from_cd)
864                                 songs_stop_redbook();           //ready to read from CD
865
866                         do {            //keep trying until we get the file handle
867
868 #ifdef O_BINARY
869                                 movie_handle = filehandle = open(lib->name, O_RDONLY | O_BINARY);
870 #else
871                                 movie_handle = filehandle = open(lib->name, O_RDONLY);
872 #endif
873
874                                 if ((filehandle == -1) && (AltHogdir_initialized)) {
875                                         char temp[128];
876                                         strcpy(temp, AltHogDir);
877                                         strcat(temp, "/");
878                                         strcat(temp, lib->name);
879 #ifdef O_BINARY
880                                         movie_handle = filehandle = open(temp, O_RDONLY | O_BINARY);
881 #else
882                                         movie_handle = filehandle = open(temp, O_RDONLY);
883 #endif
884                                 }
885
886                                 if (must_have && from_cd && filehandle == -1) {         //didn't get file!
887
888                                         if (request_cd() == -1)         //ESC from requester
889                                                 break;                                          //bail from here. will get error later
890                                 }
891
892                         } while (must_have && from_cd && filehandle == -1);
893
894                         if (filehandle != -1)
895                                 lseek(filehandle,(movie_start=lib->movies[i].offset),SEEK_SET);
896
897                         return filehandle;
898                 }
899
900         return -1;
901 }
902
903
904 //returns file handle
905 int open_movie_file(char *filename,int must_have)
906 {
907         int filehandle,i;
908
909         for (i=0;i<N_MOVIE_LIBS;i++) {
910                 if ((filehandle = search_movie_lib(movie_libs[i],filename,must_have)) != -1)
911                         return filehandle;
912         }
913
914         return -1;              //couldn't find it
915 }
916
917 //sets the file position to the start of this already-open file
918 int reset_movie_file(int handle)
919 {
920         Assert(handle == movie_handle);
921
922         lseek(handle,movie_start,SEEK_SET);
923
924         return 0;       //everything is cool
925 }