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