This commit was manufactured by cvs2svn to create branch
[btb/d2x.git] / main / endlevel.c
1 /* $Id: endlevel.c,v 1.15 2003-04-12 00:11:46 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: endlevel.c,v 1.15 2003-04-12 00:11:46 btb Exp $";
21 #endif
22
23 //#define SLEW_ON 1
24
25 //#define _MARK_ON
26
27 #include <stdlib.h>
28
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <ctype.h> // for isspace
33
34 #include "fix.h"
35 #include "vecmat.h"
36 #include "gr.h"
37 #include "3d.h"
38 #include "error.h"
39 #include "palette.h"
40 #include "iff.h"
41 #include "mono.h"
42 #include "texmap.h"
43 #include "fvi.h"
44 #include "u_mem.h"
45 #include "sounds.h"
46
47 #include "inferno.h"
48 #include "endlevel.h"
49 #include "object.h"
50 #include "game.h"
51 #include "screens.h"
52 #include "gauges.h"
53 #include "wall.h"
54 #include "terrain.h"
55 #include "polyobj.h"
56 #include "bm.h"
57 #include "gameseq.h"
58 #include "newdemo.h"
59 #include "multi.h"
60 #include "vclip.h"
61 #include "fireball.h"
62 #ifdef NETWORK
63 #include "network.h"
64 #endif
65 #include "text.h"
66 #include "digi.h"
67 #include "cfile.h"
68 #include "compbit.h"
69 #include "songs.h"
70 #include "movie.h"
71 #include "render.h"
72
73 typedef struct flythrough_data {
74         object          *obj;
75         vms_angvec      angles;                 //orientation in angles
76         vms_vector      step;                           //how far in a second
77         vms_vector      angstep;                        //rotation per second
78         fix                     speed;                  //how fast object is moving
79         vms_vector      headvec;                        //where we want to be pointing
80         int                     first_time;             //flag for if first time through
81         fix                     offset_frac;    //how far off-center as portion of way
82         fix                     offset_dist;    //how far currently off-center
83 } flythrough_data;
84
85 //endlevel sequence states
86
87 #define EL_OFF                          0               //not in endlevel
88 #define EL_FLYTHROUGH   1               //auto-flythrough in tunnel
89 #define EL_LOOKBACK             2               //looking back at player
90 #define EL_OUTSIDE              3               //flying outside for a while
91 #define EL_STOPPED              4               //stopped, watching explosion
92 #define EL_PANNING              5               //panning around, watching player
93 #define EL_CHASING              6               //chasing player to station
94
95 #define SHORT_SEQUENCE  1               //if defined, end sequnce when panning starts
96 //#define STATION_ENABLED       1               //if defined, load & use space station model
97
98 int Endlevel_sequence = 0;
99
100 extern fix player_speed;
101
102 int transition_segnum,exit_segnum;
103
104 object *endlevel_camera;
105
106 #define FLY_SPEED i2f(50)
107
108 void do_endlevel_flythrough(int n);
109 void draw_stars();
110 int find_exit_side(object *obj);
111 void generate_starfield();
112 void start_endlevel_flythrough(int n,object *obj,fix speed);
113 void start_rendered_endlevel_sequence();
114
115 #ifdef D2_OEM
116 char movie_table[] =    {       'a','a','a','a','d','d','d','d' };
117 #else
118 char movie_table[] =    {       'a','b','c',
119                                                 #ifndef SHAREWARE
120                                                         'a',
121                                                         'd','f','d','f',
122                                                         'g','h','i','g',
123                                                         'j','k','l','j',
124                                                         'm','o','m','o',
125                                                         'p','q','p','q'
126                                                 #endif
127                                         };
128 #endif
129
130 #define N_MOVIES (sizeof(movie_table) / sizeof(*movie_table))
131
132 #ifndef SHAREWARE
133 #ifdef D2_OEM
134 char movie_table_secret[] = {'a','d'};
135 #else
136 char movie_table_secret[] = {'a','d','g','j','m','p'};
137 #endif
138 #define N_MOVIES_SECRET (sizeof(movie_table_secret) / sizeof(*movie_table_secret))
139 #else
140 #define N_MOVIES_SECRET 0
141 #endif
142
143
144 #define FLY_ACCEL i2f(5)
145
146 fix cur_fly_speed,desired_fly_speed;
147
148 extern int matt_find_connect_side(int seg0,int seg1);
149
150 grs_bitmap *satellite_bitmap,*station_bitmap,*terrain_bitmap;   //!!*exit_bitmap,
151 vms_vector satellite_pos,satellite_upvec;
152 //!!grs_bitmap **exit_bitmap_list[1];
153 int station_modelnum,exit_modelnum,destroyed_exit_modelnum;
154
155 vms_vector station_pos = {0xf8c4<<10,0x3c1c<<12,0x372<<10};
156
157 #ifdef STATION_ENABLED
158 grs_bitmap *station_bitmap;
159 grs_bitmap **station_bitmap_list[1];
160 int station_modelnum;
161 #endif
162
163 vms_vector mine_exit_point;
164 vms_vector mine_ground_exit_point;
165 vms_vector mine_side_exit_point;
166 vms_matrix mine_exit_orient;
167
168 int outside_mine;
169
170 grs_bitmap terrain_bm_instance;
171 grs_bitmap satellite_bm_instance;
172
173 //find delta between two angles
174 fixang delta_ang(fixang a,fixang b)
175 {
176         fixang delta0,delta1;
177
178         return (abs(delta0 = a - b) < abs(delta1 = b - a)) ? delta0 : delta1;
179
180 }
181
182 //return though which side of seg0 is seg1
183 int matt_find_connect_side(int seg0,int seg1)
184 {
185         segment *Seg=&Segments[seg0];
186         int i;
187
188         for (i=MAX_SIDES_PER_SEGMENT;i--;) if (Seg->children[i]==seg1) return i;
189
190         return -1;
191 }
192
193 extern int Kmatrix_nomovie_message;
194
195 #if defined(D2_OEM) || defined(COMPILATION)
196 #define MOVIE_REQUIRED 0
197 #else
198 #define MOVIE_REQUIRED 1
199 #endif
200
201 //returns movie played status.  see movie.h
202 int start_endlevel_movie()
203 {
204         char movie_name[] = "esa.mve";
205         int r;
206         ubyte save_pal[768];
207
208         //Assert(Current_mission_num == Builtin_mission_num);   //only play movie for built-in mission
209
210         //Assert(N_MOVIES >= Last_level);
211         //Assert(N_MOVIES_SECRET >= -Last_secret_level);
212
213         #ifndef D2_OEM
214         if (Current_level_num == Last_level)
215                 return 1;       //don't play movie
216         #endif
217
218         if (Current_level_num > 0)
219                 movie_name[2] = movie_table[Current_level_num-1];
220         else {
221                 #ifndef SHAREWARE
222                         return 0;       //no escapes for secret level
223                 #else
224                         Error("Invalid level number <%d>",Current_level_num);
225                 #endif
226         }
227
228         memcpy(save_pal,gr_palette,768);
229
230         #ifndef SHAREWARE
231                 r=PlayMovie(movie_name,(Game_mode & GM_MULTI)?0:MOVIE_REQUIRED);
232         #else
233                 return  0;      // movie not played for shareware
234         #endif
235
236         if (Newdemo_state == ND_STATE_PLAYBACK) {
237                 set_screen_mode(SCREEN_GAME);
238                 memcpy(gr_palette,save_pal,768);
239         }
240
241 #ifdef NETWORK
242    if (r==MOVIE_NOT_PLAYED && (Game_mode & GM_MULTI))
243           Kmatrix_nomovie_message=1;
244         else
245           Kmatrix_nomovie_message=0;
246 #endif
247
248         return (r);
249
250 }
251
252 void
253 free_endlevel_data()
254 {
255         if (terrain_bm_instance.bm_data)
256                 d_free(terrain_bm_instance.bm_data);
257
258         if (satellite_bm_instance.bm_data)
259                 d_free(satellite_bm_instance.bm_data);
260 }
261
262 void init_endlevel()
263 {
264         //##satellite_bitmap = bm_load("earth.bbm");
265         //##terrain_bitmap = bm_load("moon.bbm");
266         //##
267         //##load_terrain("matt5b.bbm");         //load bitmap as height array
268         //##//load_terrain("ttest2.bbm");               //load bitmap as height array
269
270         #ifdef STATION_ENABLED
271         station_bitmap = bm_load("steel3.bbm");
272         station_bitmap_list[0] = &station_bitmap;
273
274         station_modelnum = load_polygon_model("station.pof",1,station_bitmap_list,NULL);
275         #endif
276
277 //!!    exit_bitmap = bm_load("steel1.bbm");
278 //!!    exit_bitmap_list[0] = &exit_bitmap;
279
280 //!!    exit_modelnum = load_polygon_model("exit01.pof",1,exit_bitmap_list,NULL);
281 //!!    destroyed_exit_modelnum = load_polygon_model("exit01d.pof",1,exit_bitmap_list,NULL);
282
283         generate_starfield();
284
285         atexit(free_endlevel_data);
286
287         terrain_bm_instance.bm_data = satellite_bm_instance.bm_data = NULL;
288 }
289
290 object external_explosion;
291 int ext_expl_playing,mine_destroyed;
292
293 extern fix flash_scale;
294
295 vms_angvec exit_angles={-0xa00,0,0};
296
297 vms_matrix surface_orient;
298
299 int endlevel_data_loaded=0;
300 extern char last_palette_loaded[];
301
302 void start_endlevel_sequence()
303 {
304         int     i;
305         int movie_played = MOVIE_NOT_PLAYED;
306         int inited = 0;
307
308         if (!inited) {
309                 if (Piggy_hamfile_version >= 3 &&
310                         (Mission_list[Current_mission_num].descent_version == 1 ||
311                          Current_mission_num == Builtin_mission_num))
312                         inited = load_exit_models();
313                 else
314                         inited = 1;
315         }
316
317         if (Newdemo_state == ND_STATE_RECORDING)                // stop demo recording
318                 Newdemo_state = ND_STATE_PAUSED;
319
320         if (Newdemo_state == ND_STATE_PLAYBACK) {               // don't do this if in playback mode
321                 if (Current_mission_num == Builtin_mission_num) //only play movie for built-in mission
322                         start_endlevel_movie();
323                 strcpy(last_palette_loaded,"");         //force palette load next time
324                 return;
325         }
326
327         if (Player_is_dead || ConsoleObject->flags&OF_SHOULD_BE_DEAD)
328                 return;                         //don't start if dead!
329
330         //      Dematerialize Buddy!
331         for (i=0; i<=Highest_object_index; i++)
332                 if (Objects[i].type == OBJ_ROBOT)
333                         if (Robot_info[Objects[i].id].companion) {
334                                 object_create_explosion(Objects[i].segnum, &Objects[i].pos, F1_0*7/2, VCLIP_POWERUP_DISAPPEARANCE );
335                                 Objects[i].flags |= OF_SHOULD_BE_DEAD;
336                         }
337
338         Players[Player_num].homing_object_dist = -F1_0; // Turn off homing sound.
339
340         reset_rear_view();              //turn off rear view if set
341
342 #ifdef NETWORK
343         if (Game_mode & GM_MULTI) {
344                 multi_send_endlevel_start(0);
345                 network_do_frame(1, 1);
346         }
347 #endif
348
349         if (Current_mission_num == Builtin_mission_num) {
350                 // only play movie for built-in mission
351                 if (!(Game_mode & GM_MULTI))
352                         movie_played = start_endlevel_movie();
353         }
354
355         if (inited && endlevel_data_loaded && (movie_played == MOVIE_NOT_PLAYED)) {
356                 //don't have movie.  Do rendered sequence, if available
357 #ifndef WINDOWS
358                 start_rendered_endlevel_sequence();
359 #endif
360                 return;
361         }
362
363         //don't have movie or rendered sequence, fade out
364         gr_palette_fade_out(gr_palette, 32, 0);
365
366         PlayerFinishedLevel(0);         //done with level
367 }
368
369 static int cockpit_mode_save;
370
371 void start_rendered_endlevel_sequence()
372 {
373         int last_segnum,exit_side,tunnel_length;
374
375         {
376                 int segnum,old_segnum,entry_side,i;
377
378                 //count segments in exit tunnel
379
380                 old_segnum = ConsoleObject->segnum;
381                 exit_side = find_exit_side(ConsoleObject);
382                 segnum = Segments[old_segnum].children[exit_side];
383                 tunnel_length = 0;
384                 do {
385                         entry_side = matt_find_connect_side(segnum,old_segnum);
386                         exit_side = Side_opposite[entry_side];
387                         old_segnum = segnum;
388                         segnum = Segments[segnum].children[exit_side];
389                         tunnel_length++;
390                 } while (segnum >= 0);
391
392                 if (segnum != -2) {
393                         PlayerFinishedLevel(0);         //don't do special sequence
394                         return;
395                 }
396
397                 last_segnum = old_segnum;
398
399                 //now pick transition segnum 1/3 of the way in
400
401                 old_segnum = ConsoleObject->segnum;
402                 exit_side = find_exit_side(ConsoleObject);
403                 segnum = Segments[old_segnum].children[exit_side];
404                 i=tunnel_length/3;
405                 while (i--) {
406
407                         entry_side = matt_find_connect_side(segnum,old_segnum);
408                         exit_side = Side_opposite[entry_side];
409                         old_segnum = segnum;
410                         segnum = Segments[segnum].children[exit_side];
411                 }
412                 transition_segnum = segnum;
413
414         }
415
416         cockpit_mode_save = Cockpit_mode;
417
418         #ifdef NETWORK
419         if (Game_mode & GM_MULTI) {
420                 multi_send_endlevel_start(0);
421                 network_do_frame(1, 1);
422         }
423         #endif
424
425         #ifdef SHAREWARE
426         Assert(last_segnum == exit_segnum);
427         //      songs_play_song( SONG_ENDLEVEL, 0 );    // JTS: Until we get an exit song, just don't worry
428         #endif
429
430         Endlevel_sequence = EL_FLYTHROUGH;
431
432         ConsoleObject->movement_type = MT_NONE;                 //movement handled by flythrough
433         ConsoleObject->control_type = CT_NONE;
434
435         Game_suspended |= SUSP_ROBOTS;          //robots don't move
436
437         cur_fly_speed = desired_fly_speed = FLY_SPEED;
438
439         start_endlevel_flythrough(0,ConsoleObject,cur_fly_speed);               //initialize
440
441         HUD_init_message( TXT_EXIT_SEQUENCE );
442
443         outside_mine = ext_expl_playing = 0;
444
445         flash_scale = f1_0;
446
447         //init_endlevel();
448
449         mine_destroyed=0;
450
451 }
452
453 extern flythrough_data fly_objects[];
454
455 extern object *slew_obj;
456
457 vms_angvec player_angles,player_dest_angles;
458 vms_angvec camera_desired_angles,camera_cur_angles;
459
460 #define CHASE_TURN_RATE (0x4000/4)              //max turn per second
461
462 //returns bitmask of which angles are at dest. bits 0,1,2 = p,b,h
463 int chase_angles(vms_angvec *cur_angles,vms_angvec *desired_angles)
464 {
465         vms_angvec delta_angs,alt_angles,alt_delta_angs;
466         fix total_delta,alt_total_delta;
467         fix frame_turn;
468         int mask=0;
469
470         delta_angs.p = desired_angles->p - cur_angles->p;
471         delta_angs.h = desired_angles->h - cur_angles->h;
472         delta_angs.b = desired_angles->b - cur_angles->b;
473 //delta_angs.b = 0;
474
475 //printf("chasing angles...desired = %x %x %x, cur = %x %x %x   ",desired_angles->p,desired_angles->b,desired_angles->h,cur_angles->p,cur_angles->b,cur_angles->h);
476
477         total_delta = abs(delta_angs.p) + abs(delta_angs.b) + abs(delta_angs.h);
478
479         alt_angles.p = f1_0/2 - cur_angles->p;
480         alt_angles.b = cur_angles->b + f1_0/2;
481         alt_angles.h = cur_angles->h + f1_0/2;
482
483         alt_delta_angs.p = desired_angles->p - alt_angles.p;
484         alt_delta_angs.h = desired_angles->h - alt_angles.h;
485         alt_delta_angs.b = desired_angles->b - alt_angles.b;
486 //alt_delta_angs.b = 0;
487
488         alt_total_delta = abs(alt_delta_angs.p) + abs(alt_delta_angs.b) + abs(alt_delta_angs.h);
489
490 //printf("Total delta = %x, alt total_delta = %x\n",total_delta,alt_total_delta);
491
492         if (alt_total_delta < total_delta) {
493                 //mprintf((0,"FLIPPING ANGLES!\n"));
494                 //printf("FLIPPING ANGLES!\n");
495                 *cur_angles = alt_angles;
496                 delta_angs = alt_delta_angs;
497         }
498
499         frame_turn = fixmul(FrameTime,CHASE_TURN_RATE);
500
501         if (abs(delta_angs.p) < frame_turn) {
502                 cur_angles->p = desired_angles->p;
503                 mask |= 1;
504         }
505         else
506                 if (delta_angs.p > 0)
507                         cur_angles->p += frame_turn;
508                 else
509                         cur_angles->p -= frame_turn;
510
511         if (abs(delta_angs.b) < frame_turn) {
512                 cur_angles->b = desired_angles->b;
513                 mask |= 2;
514         }
515         else
516                 if (delta_angs.b > 0)
517                         cur_angles->b += frame_turn;
518                 else
519                         cur_angles->b -= frame_turn;
520 //cur_angles->b = 0;
521
522         if (abs(delta_angs.h) < frame_turn) {
523                 cur_angles->h = desired_angles->h;
524                 mask |= 4;
525         }
526         else
527                 if (delta_angs.h > 0)
528                         cur_angles->h += frame_turn;
529                 else
530                         cur_angles->h -= frame_turn;
531
532         return mask;
533 }
534
535 void stop_endlevel_sequence()
536 {
537         Interpolation_method = 0;
538
539         gr_palette_fade_out(gr_palette, 32, 0);
540
541         select_cockpit(cockpit_mode_save);
542
543         Endlevel_sequence = EL_OFF;
544
545         PlayerFinishedLevel(0);
546
547 }
548
549 #define VCLIP_BIG_PLAYER_EXPLOSION      58
550
551 //--unused-- vms_vector upvec = {0,f1_0,0};
552
553 //find the angle between the player's heading & the station
554 void get_angs_to_object(vms_angvec *av,vms_vector *targ_pos,vms_vector *cur_pos)
555 {
556         vms_vector tv;
557
558         vm_vec_sub(&tv,targ_pos,cur_pos);
559
560         vm_extract_angles_vector(av,&tv);
561 }
562
563 void do_endlevel_frame()
564 {
565         static fix timer;
566         static fix bank_rate;
567         vms_vector save_last_pos;
568         static fix explosion_wait1=0;
569         static fix explosion_wait2=0;
570         static fix ext_expl_halflife;
571
572         save_last_pos = ConsoleObject->last_pos;        //don't let move code change this
573         object_move_all();
574         ConsoleObject->last_pos = save_last_pos;
575
576         if (ext_expl_playing) {
577
578                 external_explosion.lifeleft -= FrameTime;
579                 do_explosion_sequence(&external_explosion);
580
581                 if (external_explosion.lifeleft < ext_expl_halflife)
582                         mine_destroyed = 1;
583
584                 if (external_explosion.flags & OF_SHOULD_BE_DEAD)
585                         ext_expl_playing = 0;
586         }
587
588         if (cur_fly_speed != desired_fly_speed) {
589                 fix delta = desired_fly_speed - cur_fly_speed;
590                 fix frame_accel = fixmul(FrameTime,FLY_ACCEL);
591
592                 if (abs(delta) < frame_accel)
593                         cur_fly_speed = desired_fly_speed;
594                 else
595                         if (delta > 0)
596                                 cur_fly_speed += frame_accel;
597                         else
598                                 cur_fly_speed -= frame_accel;
599         }
600
601         //do big explosions
602         if (!outside_mine) {
603
604                 if (Endlevel_sequence==EL_OUTSIDE) {
605                         vms_vector tvec;
606
607                         vm_vec_sub(&tvec,&ConsoleObject->pos,&mine_side_exit_point);
608
609                         if (vm_vec_dot(&tvec,&mine_exit_orient.fvec) > 0) {
610                                 object *tobj;
611
612                                 outside_mine = 1;
613
614                                 tobj = object_create_explosion(exit_segnum,&mine_side_exit_point,i2f(50),VCLIP_BIG_PLAYER_EXPLOSION);
615
616                                 if (tobj) {
617                                         external_explosion = *tobj;
618
619                                         tobj->flags |= OF_SHOULD_BE_DEAD;
620
621                                         flash_scale = 0;        //kill lights in mine
622
623                                         ext_expl_halflife = tobj->lifeleft;
624
625                                         ext_expl_playing = 1;
626                                 }
627
628                                 digi_link_sound_to_pos( SOUND_BIG_ENDLEVEL_EXPLOSION, exit_segnum, 0, &mine_side_exit_point, 0, i2f(3)/4 );
629                         }
630                 }
631
632                 //do explosions chasing player
633                 if ((explosion_wait1-=FrameTime) < 0) {
634                         vms_vector tpnt;
635                         int segnum;
636                         object *expl;
637                         static int sound_count;
638
639                         vm_vec_scale_add(&tpnt,&ConsoleObject->pos,&ConsoleObject->orient.fvec,-ConsoleObject->size*5);
640                         vm_vec_scale_add2(&tpnt,&ConsoleObject->orient.rvec,(d_rand()-RAND_MAX/2)*15);
641                         vm_vec_scale_add2(&tpnt,&ConsoleObject->orient.uvec,(d_rand()-RAND_MAX/2)*15);
642
643                         segnum = find_point_seg(&tpnt,ConsoleObject->segnum);
644
645                         if (segnum != -1) {
646                                 expl = object_create_explosion(segnum,&tpnt,i2f(20),VCLIP_BIG_PLAYER_EXPLOSION);
647                                 if (d_rand()<10000 || ++sound_count==7) {               //pseudo-random
648                                         digi_link_sound_to_pos( SOUND_TUNNEL_EXPLOSION, segnum, 0, &tpnt, 0, F1_0 );
649                                         sound_count=0;
650                                 }
651                         }
652
653                         explosion_wait1 = 0x2000 + d_rand()/4;
654
655                 }
656         }
657
658         //do little explosions on walls
659         if (Endlevel_sequence >= EL_FLYTHROUGH && Endlevel_sequence < EL_OUTSIDE)
660                 if ((explosion_wait2-=FrameTime) < 0) {
661                         vms_vector tpnt;
662                         fvi_query fq;
663                         fvi_info hit_data;
664
665                         //create little explosion on wall
666
667                         vm_vec_copy_scale(&tpnt,&ConsoleObject->orient.rvec,(d_rand()-RAND_MAX/2)*100);
668                         vm_vec_scale_add2(&tpnt,&ConsoleObject->orient.uvec,(d_rand()-RAND_MAX/2)*100);
669                         vm_vec_add2(&tpnt,&ConsoleObject->pos);
670
671                         if (Endlevel_sequence == EL_FLYTHROUGH)
672                                 vm_vec_scale_add2(&tpnt,&ConsoleObject->orient.fvec,d_rand()*200);
673                         else
674                                 vm_vec_scale_add2(&tpnt,&ConsoleObject->orient.fvec,d_rand()*60);
675
676                         //find hit point on wall
677
678                         fq.p0 = &ConsoleObject->pos;
679                         fq.p1 = &tpnt;
680                         fq.startseg = ConsoleObject->segnum;
681                         fq.rad = 0;
682                         fq.thisobjnum = 0;
683                         fq.ignore_obj_list = NULL;
684                         fq.flags = 0;
685
686                         find_vector_intersection(&fq,&hit_data);
687
688                         if (hit_data.hit_type==HIT_WALL && hit_data.hit_seg!=-1)
689                                 object_create_explosion(hit_data.hit_seg,&hit_data.hit_pnt,i2f(3)+d_rand()*6,VCLIP_SMALL_EXPLOSION);
690
691                         explosion_wait2 = (0xa00 + d_rand()/8)/2;
692                 }
693
694         switch (Endlevel_sequence) {
695
696                 case EL_OFF: return;
697
698                 case EL_FLYTHROUGH: {
699
700                         do_endlevel_flythrough(0);
701
702                         if (ConsoleObject->segnum == transition_segnum) {
703
704                                 if ((Current_mission_num == Builtin_mission_num) &&
705                                         (start_endlevel_movie() != MOVIE_NOT_PLAYED))
706                                         stop_endlevel_sequence();
707                                 else {
708                                         int objnum;
709
710                                         //songs_play_song( SONG_ENDLEVEL, 0 );
711
712                                         Endlevel_sequence = EL_LOOKBACK;
713
714                                         objnum = obj_create(OBJ_CAMERA, 0,
715                                                             ConsoleObject->segnum,&ConsoleObject->pos,&ConsoleObject->orient,0,
716                                                             CT_NONE,MT_NONE,RT_NONE);
717
718                                         if (objnum == -1) { //can't get object, so abort
719                                                 mprintf((1, "Can't get object for endlevel sequence.  Aborting endlevel sequence.\n"));
720                                                 stop_endlevel_sequence();
721                                                 return;
722                                         }
723
724                                         Viewer = endlevel_camera = &Objects[objnum];
725
726                                         select_cockpit(CM_LETTERBOX);
727
728                                         fly_objects[1] = fly_objects[0];
729                                         fly_objects[1].obj = endlevel_camera;
730                                         fly_objects[1].speed = (5*cur_fly_speed)/4;
731                                         fly_objects[1].offset_frac = 0x4000;
732
733                                         vm_vec_scale_add2(&endlevel_camera->pos,&endlevel_camera->orient.fvec,i2f(7));
734
735                                         timer=0x20000;
736
737                                 }
738                         }
739
740                         break;
741                 }
742
743
744                 case EL_LOOKBACK: {
745
746                         do_endlevel_flythrough(0);
747                         do_endlevel_flythrough(1);
748
749                         if (timer>0) {
750
751                                 timer -= FrameTime;
752
753                                 if (timer < 0)          //reduce speed
754                                         fly_objects[1].speed = fly_objects[0].speed;
755                         }
756
757                         if (endlevel_camera->segnum == exit_segnum) {
758                                 vms_angvec cam_angles,exit_seg_angles;
759
760                                 Endlevel_sequence = EL_OUTSIDE;
761
762                                 timer = i2f(2);
763
764                                 vm_vec_negate(&endlevel_camera->orient.fvec);
765                                 vm_vec_negate(&endlevel_camera->orient.rvec);
766
767                                 vm_extract_angles_matrix(&cam_angles,&endlevel_camera->orient);
768                                 vm_extract_angles_matrix(&exit_seg_angles,&mine_exit_orient);
769                                 bank_rate = (-exit_seg_angles.b - cam_angles.b)/2;
770
771                                 ConsoleObject->control_type = endlevel_camera->control_type = CT_NONE;
772
773 #ifdef SLEW_ON
774  slew_obj = endlevel_camera;
775 #endif
776                         }
777
778                         break;
779                 }
780
781                 case EL_OUTSIDE: {
782                         #ifndef SLEW_ON
783                         vms_angvec cam_angles;
784                         #endif
785
786                         vm_vec_scale_add2(&ConsoleObject->pos,&ConsoleObject->orient.fvec,fixmul(FrameTime,cur_fly_speed));
787 #ifndef SLEW_ON
788                         vm_vec_scale_add2(&endlevel_camera->pos,&endlevel_camera->orient.fvec,fixmul(FrameTime,-2*cur_fly_speed));
789                         vm_vec_scale_add2(&endlevel_camera->pos,&endlevel_camera->orient.uvec,fixmul(FrameTime,-cur_fly_speed/10));
790
791                         vm_extract_angles_matrix(&cam_angles,&endlevel_camera->orient);
792                         cam_angles.b += fixmul(bank_rate,FrameTime);
793                         vm_angles_2_matrix(&endlevel_camera->orient,&cam_angles);
794 #endif
795
796                         timer -= FrameTime;
797
798                         if (timer < 0) {
799
800                                 Endlevel_sequence = EL_STOPPED;
801
802                                 vm_extract_angles_matrix(&player_angles,&ConsoleObject->orient);
803
804                                 timer = i2f(3);
805
806                         }
807
808                         break;
809                 }
810
811                 case EL_STOPPED: {
812
813                         get_angs_to_object(&player_dest_angles,&station_pos,&ConsoleObject->pos);
814                         chase_angles(&player_angles,&player_dest_angles);
815                         vm_angles_2_matrix(&ConsoleObject->orient,&player_angles);
816
817                         vm_vec_scale_add2(&ConsoleObject->pos,&ConsoleObject->orient.fvec,fixmul(FrameTime,cur_fly_speed));
818
819                         timer -= FrameTime;
820
821                         if (timer < 0) {
822
823                                 #ifdef SLEW_ON
824                                 slew_obj = endlevel_camera;
825                                 _do_slew_movement(endlevel_camera,1,1);
826                                 timer += FrameTime;             //make time stop
827                                 break;
828                                 #else
829
830                                 #ifdef SHORT_SEQUENCE
831
832                                 stop_endlevel_sequence();
833
834                                 #else
835                                 Endlevel_sequence = EL_PANNING;
836
837                                 vm_extract_angles_matrix(&camera_cur_angles,&endlevel_camera->orient);
838
839
840                                 timer = i2f(3);
841
842                                 if (Game_mode & GM_MULTI) { // try to skip part of the seq if multiplayer
843                                         stop_endlevel_sequence();
844                                         return;
845                                 }
846
847                                 //mprintf((0,"Switching to pan...\n"));
848                                 #endif          //SHORT_SEQUENCE
849                                 #endif          //SLEW_ON
850
851                         }
852                         break;
853                 }
854
855                 #ifndef SHORT_SEQUENCE
856                 case EL_PANNING: {
857                         #ifndef SLEW_ON
858                         int mask;
859                         #endif
860
861                         get_angs_to_object(&player_dest_angles,&station_pos,&ConsoleObject->pos);
862                         chase_angles(&player_angles,&player_dest_angles);
863                         vm_angles_2_matrix(&ConsoleObject->orient,&player_angles);
864                         vm_vec_scale_add2(&ConsoleObject->pos,&ConsoleObject->orient.fvec,fixmul(FrameTime,cur_fly_speed));
865
866                         #ifdef SLEW_ON
867                         _do_slew_movement(endlevel_camera,1,1);
868                         #else
869
870                         get_angs_to_object(&camera_desired_angles,&ConsoleObject->pos,&endlevel_camera->pos);
871                         mask = chase_angles(&camera_cur_angles,&camera_desired_angles);
872                         vm_angles_2_matrix(&endlevel_camera->orient,&camera_cur_angles);
873
874                         if ((mask&5) == 5) {
875
876                                 vms_vector tvec;
877
878                                 Endlevel_sequence = EL_CHASING;
879
880                                 vm_vec_normalized_dir_quick(&tvec,&station_pos,&ConsoleObject->pos);
881                                 vm_vector_2_matrix(&ConsoleObject->orient,&tvec,&surface_orient.uvec,NULL);
882
883                                 desired_fly_speed *= 2;
884
885                                 //mprintf((0,"Switching to chase...\n"));
886
887                         }
888                         #endif
889
890                         break;
891                 }
892
893                 case EL_CHASING: {
894                         fix d,speed_scale;
895
896                         #ifdef SLEW_ON
897                         _do_slew_movement(endlevel_camera,1,1);
898                         #endif
899
900                         get_angs_to_object(&camera_desired_angles,&ConsoleObject->pos,&endlevel_camera->pos);
901                         chase_angles(&camera_cur_angles,&camera_desired_angles);
902
903                         #ifndef SLEW_ON
904                         vm_angles_2_matrix(&endlevel_camera->orient,&camera_cur_angles);
905                         #endif
906
907                         d = vm_vec_dist_quick(&ConsoleObject->pos,&endlevel_camera->pos);
908
909                         speed_scale = fixdiv(d,i2f(0x20));
910                         if (d<f1_0) d=f1_0;
911
912                         get_angs_to_object(&player_dest_angles,&station_pos,&ConsoleObject->pos);
913                         chase_angles(&player_angles,&player_dest_angles);
914                         vm_angles_2_matrix(&ConsoleObject->orient,&player_angles);
915
916                         vm_vec_scale_add2(&ConsoleObject->pos,&ConsoleObject->orient.fvec,fixmul(FrameTime,cur_fly_speed));
917                         #ifndef SLEW_ON
918                         vm_vec_scale_add2(&endlevel_camera->pos,&endlevel_camera->orient.fvec,fixmul(FrameTime,fixmul(speed_scale,cur_fly_speed)));
919
920                         if (vm_vec_dist(&ConsoleObject->pos,&station_pos) < i2f(10))
921                                 stop_endlevel_sequence();
922                         #endif
923
924                         break;
925
926                 }
927                 #endif          //ifdef SHORT_SEQUENCE
928
929         }
930 }
931
932
933 #define MIN_D 0x100
934
935 //find which side to fly out of
936 int find_exit_side(object *obj)
937 {
938         int i;
939         vms_vector prefvec,segcenter,sidevec;
940         fix best_val=-f2_0;
941         int best_side;
942         segment *pseg = &Segments[obj->segnum];
943
944         //find exit side
945
946         vm_vec_normalized_dir_quick(&prefvec,&obj->pos,&obj->last_pos);
947
948         compute_segment_center(&segcenter,pseg);
949
950         best_side=-1;
951         for (i=MAX_SIDES_PER_SEGMENT;--i >= 0;) {
952                 fix d;
953
954                 if (pseg->children[i]!=-1) {
955
956                         compute_center_point_on_side(&sidevec,pseg,i);
957                         vm_vec_normalized_dir_quick(&sidevec,&sidevec,&segcenter);
958                         d = vm_vec_dotprod(&sidevec,&prefvec);
959
960                         if (labs(d) < MIN_D) d=0;
961
962                         if (d > best_val) {best_val=d; best_side=i;}
963
964                 }
965         }
966
967         Assert(best_side!=-1);
968
969         return best_side;
970 }
971
972 extern fix Render_zoom;                                                 //the player's zoom factor
973
974 extern vms_vector Viewer_eye;   //valid during render
975
976 void draw_exit_model()
977 {
978         vms_vector model_pos;
979         int f=15,u=0;   //21;
980
981         vm_vec_scale_add(&model_pos,&mine_exit_point,&mine_exit_orient.fvec,i2f(f));
982         vm_vec_scale_add2(&model_pos,&mine_exit_orient.uvec,i2f(u));
983
984         draw_polygon_model(&model_pos,&mine_exit_orient,NULL,(mine_destroyed)?destroyed_exit_modelnum:exit_modelnum,0,f1_0,NULL,NULL);
985
986 }
987
988 int exit_point_bmx,exit_point_bmy;
989
990 fix satellite_size = i2f(400);
991
992 #define SATELLITE_DIST          i2f(1024)
993 #define SATELLITE_WIDTH         satellite_size
994 #define SATELLITE_HEIGHT        ((satellite_size*9)/4)          //((satellite_size*5)/2)
995
996 void render_external_scene(fix eye_offset)
997 {
998
999         Viewer_eye = Viewer->pos;
1000
1001         if (eye_offset)
1002                 vm_vec_scale_add2(&Viewer_eye,&Viewer->orient.rvec,eye_offset);
1003
1004         g3_set_view_matrix(&Viewer->pos,&Viewer->orient,Render_zoom);
1005
1006         //g3_draw_horizon(BM_XRGB(0,0,0),BM_XRGB(16,16,16));            //,-1);
1007         gr_clear_canvas(BM_XRGB(0,0,0));
1008
1009         g3_start_instance_matrix(&vmd_zero_vector,&surface_orient);
1010         draw_stars();
1011         g3_done_instance();
1012
1013         {       //draw satellite
1014
1015                 vms_vector delta;
1016                 g3s_point p,top_pnt;
1017
1018                 g3_rotate_point(&p,&satellite_pos);
1019                 g3_rotate_delta_vec(&delta,&satellite_upvec);
1020
1021                 g3_add_delta_vec(&top_pnt,&p,&delta);
1022
1023                 if (! (p.p3_codes & CC_BEHIND)) {
1024                         int save_im = Interpolation_method;
1025                         //p.p3_flags &= ~PF_PROJECTED;
1026                         //g3_project_point(&p);
1027                         if (! (p.p3_flags & PF_OVERFLOW)) {
1028                                 Interpolation_method = 0;
1029                                 //gr_bitmapm(f2i(p.p3_sx)-32,f2i(p.p3_sy)-32,satellite_bitmap);
1030                                 g3_draw_rod_tmap(satellite_bitmap,&p,SATELLITE_WIDTH,&top_pnt,SATELLITE_WIDTH,f1_0);
1031                                 Interpolation_method = save_im;
1032                         }
1033                 }
1034         }
1035
1036         #ifdef STATION_ENABLED
1037         draw_polygon_model(&station_pos,&vmd_identity_matrix,NULL,station_modelnum,0,f1_0,NULL,NULL);
1038         #endif
1039
1040         render_terrain(&mine_ground_exit_point,exit_point_bmx,exit_point_bmy);
1041
1042         draw_exit_model();
1043         if (ext_expl_playing)
1044                 draw_fireball(&external_explosion);
1045
1046         Lighting_on=0;
1047         render_object(ConsoleObject);
1048         Lighting_on=1;
1049 }
1050
1051 #define MAX_STARS 500
1052
1053 vms_vector stars[MAX_STARS];
1054
1055 void generate_starfield()
1056 {
1057         int i;
1058
1059         for (i=0;i<MAX_STARS;i++) {
1060
1061                 stars[i].x = (d_rand() - RAND_MAX/2) << 14;
1062                 stars[i].z = (d_rand() - RAND_MAX/2) << 14;
1063                 stars[i].y = (d_rand()/2) << 14;
1064
1065         }
1066 }
1067
1068 void draw_stars()
1069 {
1070         int i;
1071         int intensity=31;
1072         g3s_point p;
1073
1074         for (i=0;i<MAX_STARS;i++) {
1075
1076                 if ((i&63) == 0) {
1077                         gr_setcolor(BM_XRGB(intensity,intensity,intensity));
1078                         intensity-=3;
1079                 }
1080
1081                 //g3_rotate_point(&p,&stars[i]);
1082                 g3_rotate_delta_vec(&p.p3_vec,&stars[i]);
1083                 g3_code_point(&p);
1084
1085                 if (p.p3_codes == 0) {
1086
1087                         p.p3_flags &= ~PF_PROJECTED;
1088
1089                         g3_project_point(&p);
1090
1091                         gr_pixel(f2i(p.p3_sx),f2i(p.p3_sy));
1092                 }
1093         }
1094
1095 //@@    {
1096 //@@            vms_vector delta;
1097 //@@            g3s_point top_pnt;
1098 //@@
1099 //@@            g3_rotate_point(&p,&satellite_pos);
1100 //@@            g3_rotate_delta_vec(&delta,&satellite_upvec);
1101 //@@
1102 //@@            g3_add_delta_vec(&top_pnt,&p,&delta);
1103 //@@
1104 //@@            if (! (p.p3_codes & CC_BEHIND)) {
1105 //@@                    int save_im = Interpolation_method;
1106 //@@                    Interpolation_method = 0;
1107 //@@                    //p.p3_flags &= ~PF_PROJECTED;
1108 //@@                    g3_project_point(&p);
1109 //@@                    if (! (p.p3_flags & PF_OVERFLOW))
1110 //@@                            //gr_bitmapm(f2i(p.p3_sx)-32,f2i(p.p3_sy)-32,satellite_bitmap);
1111 //@@                            g3_draw_rod_tmap(satellite_bitmap,&p,SATELLITE_WIDTH,&top_pnt,SATELLITE_WIDTH,f1_0);
1112 //@@                    Interpolation_method = save_im;
1113 //@@            }
1114 //@@    }
1115
1116 }
1117
1118 void endlevel_render_mine(fix eye_offset)
1119 {
1120         int start_seg_num;
1121
1122         Viewer_eye = Viewer->pos;
1123
1124         if (Viewer->type == OBJ_PLAYER )
1125                 vm_vec_scale_add2(&Viewer_eye,&Viewer->orient.fvec,(Viewer->size*3)/4);
1126
1127         if (eye_offset)
1128                 vm_vec_scale_add2(&Viewer_eye,&Viewer->orient.rvec,eye_offset);
1129
1130         #ifdef EDITOR
1131         if (Function_mode==FMODE_EDITOR)
1132                 Viewer_eye = Viewer->pos;
1133         #endif
1134
1135         if (Endlevel_sequence >= EL_OUTSIDE) {
1136
1137                 start_seg_num = exit_segnum;
1138         }
1139         else {
1140                 start_seg_num = find_point_seg(&Viewer_eye,Viewer->segnum);
1141
1142                 if (start_seg_num==-1)
1143                         start_seg_num = Viewer->segnum;
1144         }
1145
1146         if (Endlevel_sequence == EL_LOOKBACK) {
1147                 vms_matrix headm,viewm;
1148                 vms_angvec angles = {0,0,0x7fff};
1149
1150                 vm_angles_2_matrix(&headm,&angles);
1151                 vm_matrix_x_matrix(&viewm,&Viewer->orient,&headm);
1152                 g3_set_view_matrix(&Viewer_eye,&viewm,Render_zoom);
1153         }
1154         else
1155                 g3_set_view_matrix(&Viewer_eye,&Viewer->orient,Render_zoom);
1156
1157         render_mine(start_seg_num,eye_offset, 0);
1158 }
1159
1160 void render_endlevel_frame(fix eye_offset)
1161 {
1162
1163         g3_start_frame();
1164
1165         if (Endlevel_sequence < EL_OUTSIDE)
1166                 endlevel_render_mine(eye_offset);
1167         else
1168                 render_external_scene(eye_offset);
1169
1170         g3_end_frame();
1171
1172 }
1173
1174
1175 ///////////////////////// copy of flythrough code for endlevel
1176
1177
1178 #define MAX_FLY_OBJECTS 2
1179
1180 flythrough_data fly_objects[MAX_FLY_OBJECTS];
1181
1182 flythrough_data *flydata;
1183
1184 int matt_find_connect_side(int seg0,int seg1);
1185
1186 void compute_segment_center(vms_vector *vp,segment *sp);
1187
1188 fixang delta_ang(fixang a,fixang b);
1189 fixang interp_angle(fixang dest,fixang src,fixang step);
1190
1191 #define DEFAULT_SPEED i2f(16)
1192
1193 #define MIN_D 0x100
1194
1195 //if speed is zero, use default speed
1196 void start_endlevel_flythrough(int n,object *obj,fix speed)
1197 {
1198         flydata = &fly_objects[n];
1199
1200         flydata->obj = obj;
1201
1202         flydata->first_time = 1;
1203
1204         flydata->speed = speed?speed:DEFAULT_SPEED;
1205
1206         flydata->offset_frac = 0;
1207 }
1208
1209 static vms_angvec *angvec_add2_scale(vms_angvec *dest,vms_vector *src,fix s)
1210 {
1211         dest->p += fixmul(src->x,s);
1212         dest->b  += fixmul(src->z,s);
1213         dest->h  += fixmul(src->y,s);
1214
1215         return dest;
1216 }
1217
1218 #define MAX_ANGSTEP     0x4000          //max turn per second
1219
1220 #define MAX_SLIDE_PER_SEGMENT 0x10000
1221
1222 void do_endlevel_flythrough(int n)
1223 {
1224         object *obj;
1225         segment *pseg;
1226         int old_player_seg;
1227
1228         flydata = &fly_objects[n];
1229         obj = flydata->obj;
1230         
1231         old_player_seg = obj->segnum;
1232
1233         //move the player for this frame
1234
1235         if (!flydata->first_time) {
1236
1237                 vm_vec_scale_add2(&obj->pos,&flydata->step,FrameTime);
1238                 angvec_add2_scale(&flydata->angles,&flydata->angstep,FrameTime);
1239
1240                 vm_angles_2_matrix(&obj->orient,&flydata->angles);
1241         }
1242
1243         //check new player seg
1244
1245         update_object_seg(obj);
1246         pseg = &Segments[obj->segnum];
1247
1248         if (flydata->first_time || obj->segnum != old_player_seg) {             //moved into new seg
1249                 vms_vector curcenter,nextcenter;
1250                 fix step_size,seg_time;
1251                 short entry_side,exit_side = -1;//what sides we entry and leave through
1252                 vms_vector dest_point;          //where we are heading (center of exit_side)
1253                 vms_angvec dest_angles;         //where we want to be pointing
1254                 vms_matrix dest_orient;
1255                 int up_side=0;
1256
1257                 entry_side=0;
1258
1259                 //find new exit side
1260
1261                 if (!flydata->first_time) {
1262
1263                         entry_side = matt_find_connect_side(obj->segnum,old_player_seg);
1264                         exit_side = Side_opposite[entry_side];
1265                 }
1266
1267                 if (flydata->first_time || entry_side==-1 || pseg->children[exit_side]==-1)
1268                         exit_side = find_exit_side(obj);
1269
1270                 {                                                                               //find closest side to align to
1271                         fix d,largest_d=-f1_0;
1272                         int i;
1273
1274                         for (i=0;i<6;i++) {
1275                                 #ifdef COMPACT_SEGS
1276                                 vms_vector v1;
1277                                 get_side_normal(pseg, i, 0, &v1 );
1278                                 d = vm_vec_dot(&v1,&flydata->obj->orient.uvec);
1279                                 #else
1280                                 d = vm_vec_dot(&pseg->sides[i].normals[0],&flydata->obj->orient.uvec);
1281                                 #endif
1282                                 if (d > largest_d) {largest_d = d; up_side=i;}
1283                         }
1284
1285                 }
1286
1287                 //update target point & angles
1288
1289                 compute_center_point_on_side(&dest_point,pseg,exit_side);
1290
1291                 //update target point and movement points
1292
1293                 //offset object sideways
1294                 if (flydata->offset_frac) {
1295                         int s0=-1,s1=0,i;
1296                         vms_vector s0p,s1p;
1297                         fix dist;
1298
1299                         for (i=0;i<6;i++)
1300                                 if (i!=entry_side && i!=exit_side && i!=up_side && i!=Side_opposite[up_side])
1301                                  {
1302                                         if (s0==-1)
1303                                                 s0 = i;
1304                                         else
1305                                                 s1 = i;
1306                                  }
1307
1308                         compute_center_point_on_side(&s0p,pseg,s0);
1309                         compute_center_point_on_side(&s1p,pseg,s1);
1310                         dist = fixmul(vm_vec_dist(&s0p,&s1p),flydata->offset_frac);
1311
1312                         if (dist-flydata->offset_dist > MAX_SLIDE_PER_SEGMENT)
1313                                 dist = flydata->offset_dist + MAX_SLIDE_PER_SEGMENT;
1314
1315                         flydata->offset_dist = dist;
1316
1317                         vm_vec_scale_add2(&dest_point,&obj->orient.rvec,dist);
1318
1319                 }
1320
1321                 vm_vec_sub(&flydata->step,&dest_point,&obj->pos);
1322                 step_size = vm_vec_normalize_quick(&flydata->step);
1323                 vm_vec_scale(&flydata->step,flydata->speed);
1324
1325                 compute_segment_center(&curcenter,pseg);
1326                 compute_segment_center(&nextcenter,&Segments[pseg->children[exit_side]]);
1327                 vm_vec_sub(&flydata->headvec,&nextcenter,&curcenter);
1328
1329                 #ifdef COMPACT_SEGS
1330                 {
1331                         vms_vector _v1;
1332                         get_side_normal(pseg, up_side, 0, &_v1 );
1333                         vm_vector_2_matrix(&dest_orient,&flydata->headvec,&_v1,NULL);
1334                 }
1335                 #else
1336                 vm_vector_2_matrix(&dest_orient,&flydata->headvec,&pseg->sides[up_side].normals[0],NULL);
1337                 #endif
1338                 vm_extract_angles_matrix(&dest_angles,&dest_orient);
1339
1340                 if (flydata->first_time)
1341                         vm_extract_angles_matrix(&flydata->angles,&obj->orient);
1342
1343                 seg_time = fixdiv(step_size,flydata->speed);    //how long through seg
1344
1345                 if (seg_time) {
1346                         flydata->angstep.x = max(-MAX_ANGSTEP,min(MAX_ANGSTEP,fixdiv(delta_ang(flydata->angles.p,dest_angles.p),seg_time)));
1347                         flydata->angstep.z = max(-MAX_ANGSTEP,min(MAX_ANGSTEP,fixdiv(delta_ang(flydata->angles.b,dest_angles.b),seg_time)));
1348                         flydata->angstep.y = max(-MAX_ANGSTEP,min(MAX_ANGSTEP,fixdiv(delta_ang(flydata->angles.h,dest_angles.h),seg_time)));
1349
1350                 }
1351                 else {
1352                         flydata->angles = dest_angles;
1353                         flydata->angstep.x = flydata->angstep.y = flydata->angstep.z = 0;
1354                 }
1355         }
1356
1357         flydata->first_time=0;
1358 }
1359
1360 #define JOY_NULL 15
1361 #define ROT_SPEED 8             //rate of rotation while key held down
1362 #define VEL_SPEED (15)  //rate of acceleration while key held down
1363
1364 extern short old_joy_x,old_joy_y;       //position last time around
1365
1366 #include "key.h"
1367 #include "joy.h"
1368
1369 #ifdef SLEW_ON          //this is a special routine for slewing around external scene
1370 int _do_slew_movement(object *obj, int check_keys, int check_joy )
1371 {
1372         int moved = 0;
1373         vms_vector svel, movement;                              //scaled velocity (per this frame)
1374         vms_matrix rotmat,new_pm;
1375         int joy_x,joy_y,btns;
1376         int joyx_moved,joyy_moved;
1377         vms_angvec rotang;
1378
1379         if (keyd_pressed[KEY_PAD5])
1380                 vm_vec_zero(&obj->phys_info.velocity);
1381
1382         if (check_keys) {
1383                 obj->phys_info.velocity.x += VEL_SPEED * (key_down_time(KEY_PAD9) - key_down_time(KEY_PAD7));
1384                 obj->phys_info.velocity.y += VEL_SPEED * (key_down_time(KEY_PADMINUS) - key_down_time(KEY_PADPLUS));
1385                 obj->phys_info.velocity.z += VEL_SPEED * (key_down_time(KEY_PAD8) - key_down_time(KEY_PAD2));
1386
1387                 rotang.pitch =  (key_down_time(KEY_LBRACKET) - key_down_time(KEY_RBRACKET))/ROT_SPEED;
1388                 rotang.bank  = (key_down_time(KEY_PAD1) - key_down_time(KEY_PAD3))/ROT_SPEED;
1389                 rotang.head  = (key_down_time(KEY_PAD6) - key_down_time(KEY_PAD4))/ROT_SPEED;
1390         }
1391         else
1392                 rotang.pitch = rotang.bank  = rotang.head  = 0;
1393
1394         //check for joystick movement
1395
1396         if (check_joy && joy_present)   {
1397                 joy_get_pos(&joy_x,&joy_y);
1398                 btns=joy_get_btns();
1399
1400                 joyx_moved = (abs(joy_x - old_joy_x)>JOY_NULL);
1401                 joyy_moved = (abs(joy_y - old_joy_y)>JOY_NULL);
1402
1403                 if (abs(joy_x) < JOY_NULL) joy_x = 0;
1404                 if (abs(joy_y) < JOY_NULL) joy_y = 0;
1405
1406                 if (btns)
1407                         if (!rotang.pitch) rotang.pitch = fixmul(-joy_y * 512,FrameTime); else;
1408                 else
1409                         if (joyy_moved) obj->phys_info.velocity.z = -joy_y * 8192;
1410
1411                 if (!rotang.head) rotang.head = fixmul(joy_x * 512,FrameTime);
1412
1413                 if (joyx_moved) old_joy_x = joy_x;
1414                 if (joyy_moved) old_joy_y = joy_y;
1415         }
1416
1417         moved = rotang.pitch | rotang.bank | rotang.head;
1418
1419         vm_angles_2_matrix(&rotmat,&rotang);
1420         vm_matrix_x_matrix(&new_pm,&obj->orient,&rotmat);
1421         obj->orient = new_pm;
1422         vm_transpose_matrix(&new_pm);           //make those columns rows
1423
1424         moved |= obj->phys_info.velocity.x | obj->phys_info.velocity.y | obj->phys_info.velocity.z;
1425
1426         svel = obj->phys_info.velocity;
1427         vm_vec_scale(&svel,FrameTime);          //movement in this frame
1428         vm_vec_rotate(&movement,&svel,&new_pm);
1429
1430         vm_vec_add2(&obj->pos,&movement);
1431
1432         moved |= (movement.x || movement.y || movement.z);
1433
1434         return moved;
1435 }
1436 #endif
1437
1438 #define LINE_LEN        80
1439 #define NUM_VARS        8
1440
1441 #define STATION_DIST    i2f(1024)
1442
1443 int convert_ext( char *dest, char *ext )
1444 {
1445         char *t;
1446
1447         t = strchr(dest,'.');
1448
1449         if (t && (t-dest <= 8)) {
1450                 t[1] = ext[0];                  
1451                 t[2] = ext[1];                  
1452                 t[3] = ext[2];  
1453                 return 1;
1454         }
1455         else
1456                 return 0;
1457 }
1458
1459 //called for each level to load & setup the exit sequence
1460 void load_endlevel_data(int level_num)
1461 {
1462         char filename[13];
1463         char line[LINE_LEN],*p;
1464         CFILE *ifile;
1465         int var,segnum,sidenum;
1466         int exit_side=0, i;
1467         int have_binary = 0;
1468
1469         endlevel_data_loaded = 0;               //not loaded yet
1470
1471 try_again:
1472         ;
1473
1474         if (level_num<0)                //secret level
1475                 strcpy(filename,Secret_level_names[-level_num-1]);
1476         else                                    //normal level
1477                 strcpy(filename,Level_names[level_num-1]);
1478
1479         if (!convert_ext(filename,"END"))
1480                 Error("Error converting filename <%s> for endlevel data\n",filename);
1481
1482         ifile = cfopen(filename,"rb");
1483
1484         if (!ifile) {
1485
1486                 convert_ext(filename,"txb");
1487
1488                 ifile = cfopen(filename,"rb");
1489
1490                 if (!ifile) {
1491                         if (level_num==1) {
1492                                 con_printf(CON_DEBUG, "Cannot load file text of binary version of <%s>\n",filename);
1493                                 endlevel_data_loaded = 0;
1494                                 return;
1495                         }
1496                         else {
1497                                 level_num = 1;
1498                                 goto try_again;
1499                         }
1500                 }
1501
1502                 have_binary = 1;
1503         }
1504
1505         //ok...this parser is pretty simple.  It ignores comments, but
1506         //everything else must be in the right place
1507
1508         var = 0;
1509
1510         while (cfgets(line,LINE_LEN,ifile)) {
1511
1512                 if (have_binary) {
1513                         for (i = 0; i < strlen(line) - 1; i++) {
1514                                 encode_rotate_left(&(line[i]));
1515                                 line[i] = line[i] ^ BITMAP_TBL_XOR;
1516                                 encode_rotate_left(&(line[i]));
1517                         }
1518                         p = line;
1519                 }
1520
1521                 if ((p=strchr(line,';'))!=NULL)
1522                         *p = 0;         //cut off comment
1523
1524                 for (p=line+strlen(line)-1;p>line && isspace(*p);*p--=0);
1525                 for (p=line;isspace(*p);p++);
1526
1527                 if (!*p)                //empty line
1528                         continue;
1529
1530                 switch (var) {
1531
1532                         case 0: {                                               //ground terrain
1533                                 int iff_error;
1534                                 ubyte pal[768];
1535
1536                                 if (terrain_bm_instance.bm_data)
1537                                         d_free(terrain_bm_instance.bm_data);
1538
1539                                 Assert(terrain_bm_instance.bm_data == NULL);
1540
1541                                 iff_error = iff_read_bitmap(p,&terrain_bm_instance,BM_LINEAR,pal);
1542                                 if (iff_error != IFF_NO_ERROR) {
1543                                         Error("File %s - IFF error: %s",p,iff_errormsg(iff_error));
1544                                 }
1545
1546                                 terrain_bitmap = &terrain_bm_instance;
1547
1548                                 gr_remap_bitmap_good( terrain_bitmap, pal, iff_transparent_color, -1);
1549
1550                                 break;
1551                         }
1552
1553                         case 1:                                                 //height map
1554
1555                                 load_terrain(p);
1556                                 break;
1557
1558
1559                         case 2:
1560
1561                                 sscanf(p,"%d,%d",&exit_point_bmx,&exit_point_bmy);
1562                                 break;
1563
1564                         case 3:                                                 //exit heading
1565
1566                                 exit_angles.h = i2f(atoi(p))/360;
1567                                 break;
1568
1569                         case 4: {                                               //planet bitmap
1570                                 int iff_error;
1571                                 ubyte pal[768];
1572
1573                                 if (satellite_bm_instance.bm_data)
1574                                         d_free(satellite_bm_instance.bm_data);
1575
1576                                 iff_error = iff_read_bitmap(p,&satellite_bm_instance,BM_LINEAR,pal);
1577                                 if (iff_error != IFF_NO_ERROR) {
1578                                         mprintf((1, "File %s - IFF error: %s",p,iff_errormsg(iff_error)));
1579                                         Error("File %s - IFF error: %s",p,iff_errormsg(iff_error));
1580                                 }
1581
1582                                 satellite_bitmap = &satellite_bm_instance;
1583                                 gr_remap_bitmap_good( satellite_bitmap, pal, iff_transparent_color, -1);
1584
1585                                 break;
1586                         }
1587
1588                         case 5:                                                 //earth pos
1589                         case 7: {                                               //station pos
1590                                 vms_matrix tm;
1591                                 vms_angvec ta;
1592                                 int pitch,head;
1593
1594                                 sscanf(p,"%d,%d",&head,&pitch);
1595
1596                                 ta.h = i2f(head)/360;
1597                                 ta.p = -i2f(pitch)/360;
1598                                 ta.b = 0;
1599
1600                                 vm_angles_2_matrix(&tm,&ta);
1601
1602                                 if (var==5)
1603                                         satellite_pos = tm.fvec;
1604                                         //vm_vec_copy_scale(&satellite_pos,&tm.fvec,SATELLITE_DIST);
1605                                 else
1606                                         station_pos = tm.fvec;
1607
1608                                 break;
1609                         }
1610
1611                         case 6:                                         //planet size
1612                                 satellite_size = i2f(atoi(p));
1613                                 break;
1614                 }
1615
1616                 var++;
1617
1618         }
1619
1620         Assert(var == NUM_VARS);
1621
1622
1623         // OK, now the data is loaded.  Initialize everything
1624
1625         //find the exit sequence by searching all segments for a side with
1626         //children == -2
1627
1628         for (segnum=0,exit_segnum=-1;exit_segnum==-1 && segnum<=Highest_segment_index;segnum++)
1629                 for (sidenum=0;sidenum<6;sidenum++)
1630                         if (Segments[segnum].children[sidenum] == -2) {
1631                                 exit_segnum = segnum;
1632                                 exit_side = sidenum;
1633                                 break;
1634                         }
1635
1636         Assert(exit_segnum!=-1);
1637
1638         compute_segment_center(&mine_exit_point,&Segments[exit_segnum]);
1639         extract_orient_from_segment(&mine_exit_orient,&Segments[exit_segnum]);
1640         compute_center_point_on_side(&mine_side_exit_point,&Segments[exit_segnum],exit_side);
1641
1642         vm_vec_scale_add(&mine_ground_exit_point,&mine_exit_point,&mine_exit_orient.uvec,-i2f(20));
1643
1644         //compute orientation of surface
1645         {
1646                 vms_vector tv;
1647                 vms_matrix exit_orient,tm;
1648
1649                 vm_angles_2_matrix(&exit_orient,&exit_angles);
1650                 vm_transpose_matrix(&exit_orient);
1651                 vm_matrix_x_matrix(&surface_orient,&mine_exit_orient,&exit_orient);
1652
1653                 vm_copy_transpose_matrix(&tm,&surface_orient);
1654                 vm_vec_rotate(&tv,&station_pos,&tm);
1655                 vm_vec_scale_add(&station_pos,&mine_exit_point,&tv,STATION_DIST);
1656
1657                 vm_vec_rotate(&tv,&satellite_pos,&tm);
1658                 vm_vec_scale_add(&satellite_pos,&mine_exit_point,&tv,SATELLITE_DIST);
1659
1660                 vm_vector_2_matrix(&tm,&tv,&surface_orient.uvec,NULL);
1661                 vm_vec_copy_scale(&satellite_upvec,&tm.uvec,SATELLITE_HEIGHT);
1662
1663
1664         }
1665
1666         cfclose(ifile);
1667
1668         endlevel_data_loaded = 1;
1669
1670 }