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