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