comments/formatting
[btb/d2x.git] / main / endlevel.c
1 /* $Id: endlevel.c,v 1.17 2003-10-10 09:36:35 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.17 2003-10-10 09:36:35 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         #ifndef D2_OEM
438         if (Current_level_num == Last_level)
439                 return 1;       //don't play movie
440         #endif
441
442         if (Current_level_num > 0)
443                 movie_name[2] = movie_table[Current_level_num-1];
444         else {
445                 #ifndef SHAREWARE
446                         return 0;       //no escapes for secret level
447                 #else
448                         Error("Invalid level number <%d>",Current_level_num);
449                 #endif
450         }
451
452         memcpy(save_pal,gr_palette,768);
453
454         #ifndef SHAREWARE
455                 r=PlayMovie(movie_name,(Game_mode & GM_MULTI)?0:MOVIE_REQUIRED);
456         #else
457                 return  0;      // movie not played for shareware
458         #endif
459
460         if (Newdemo_state == ND_STATE_PLAYBACK) {
461                 set_screen_mode(SCREEN_GAME);
462                 memcpy(gr_palette,save_pal,768);
463         }
464
465 #ifdef NETWORK
466    if (r==MOVIE_NOT_PLAYED && (Game_mode & GM_MULTI))
467           Kmatrix_nomovie_message=1;
468         else
469           Kmatrix_nomovie_message=0;
470 #endif
471
472         return (r);
473
474 }
475
476 void
477 free_endlevel_data()
478 {
479         if (terrain_bm_instance.bm_data)
480                 d_free(terrain_bm_instance.bm_data);
481
482         if (satellite_bm_instance.bm_data)
483                 d_free(satellite_bm_instance.bm_data);
484 }
485
486 void init_endlevel()
487 {
488         //##satellite_bitmap = bm_load("earth.bbm");
489         //##terrain_bitmap = bm_load("moon.bbm");
490         //##
491         //##load_terrain("matt5b.bbm");         //load bitmap as height array
492         //##//load_terrain("ttest2.bbm");               //load bitmap as height array
493
494         #ifdef STATION_ENABLED
495         station_bitmap = bm_load("steel3.bbm");
496         station_bitmap_list[0] = &station_bitmap;
497
498         station_modelnum = load_polygon_model("station.pof",1,station_bitmap_list,NULL);
499         #endif
500
501 //!!    exit_bitmap = bm_load("steel1.bbm");
502 //!!    exit_bitmap_list[0] = &exit_bitmap;
503
504 //!!    exit_modelnum = load_polygon_model("exit01.pof",1,exit_bitmap_list,NULL);
505 //!!    destroyed_exit_modelnum = load_polygon_model("exit01d.pof",1,exit_bitmap_list,NULL);
506
507         generate_starfield();
508
509         atexit(free_endlevel_data);
510
511         terrain_bm_instance.bm_data = satellite_bm_instance.bm_data = NULL;
512 }
513
514 object external_explosion;
515 int ext_expl_playing,mine_destroyed;
516
517 extern fix flash_scale;
518
519 vms_angvec exit_angles={-0xa00,0,0};
520
521 vms_matrix surface_orient;
522
523 int endlevel_data_loaded=0;
524 extern char last_palette_loaded[];
525
526 void start_endlevel_sequence()
527 {
528         int     i;
529         int movie_played = MOVIE_NOT_PLAYED;
530         int inited;
531
532         if ((Mission_list[Current_mission_num].descent_version == 1
533                 || Current_mission_num == Builtin_mission_num)
534                 && Piggy_hamfile_version >= 3)
535                 inited = load_exit_models();
536         else
537                 inited = 1;
538
539         if (Newdemo_state == ND_STATE_RECORDING)                // stop demo recording
540                 Newdemo_state = ND_STATE_PAUSED;
541
542         if (Newdemo_state == ND_STATE_PLAYBACK) {               // don't do this if in playback mode
543                 if (Current_mission_num == Builtin_mission_num) //only play movie for built-in mission
544                         start_endlevel_movie();
545                 strcpy(last_palette_loaded,"");         //force palette load next time
546                 return;
547         }
548
549         if (Player_is_dead || ConsoleObject->flags&OF_SHOULD_BE_DEAD)
550                 return;                         //don't start if dead!
551
552         //      Dematerialize Buddy!
553         for (i=0; i<=Highest_object_index; i++)
554                 if (Objects[i].type == OBJ_ROBOT)
555                         if (Robot_info[Objects[i].id].companion) {
556                                 object_create_explosion(Objects[i].segnum, &Objects[i].pos, F1_0*7/2, VCLIP_POWERUP_DISAPPEARANCE );
557                                 Objects[i].flags |= OF_SHOULD_BE_DEAD;
558                         }
559
560         Players[Player_num].homing_object_dist = -F1_0; // Turn off homing sound.
561
562         reset_rear_view();              //turn off rear view if set
563
564 #ifdef NETWORK
565         if (Game_mode & GM_MULTI) {
566                 multi_send_endlevel_start(0);
567                 network_do_frame(1, 1);
568         }
569 #endif
570
571         if (Current_mission_num == Builtin_mission_num) {
572                 // only play movie for built-in mission
573                 if (!(Game_mode & GM_MULTI))
574                         movie_played = start_endlevel_movie();
575         }
576
577         if (inited && endlevel_data_loaded && (movie_played == MOVIE_NOT_PLAYED)) {
578                 //don't have movie.  Do rendered sequence, if available
579 #ifndef WINDOWS
580                 start_rendered_endlevel_sequence();
581 #endif
582                 return;
583         }
584
585         //don't have movie or rendered sequence, fade out
586         gr_palette_fade_out(gr_palette, 32, 0);
587
588         PlayerFinishedLevel(0);         //done with level
589 }
590
591 static int cockpit_mode_save;
592
593 void start_rendered_endlevel_sequence()
594 {
595         int last_segnum,exit_side,tunnel_length;
596
597         {
598                 int segnum,old_segnum,entry_side,i;
599
600                 //count segments in exit tunnel
601
602                 old_segnum = ConsoleObject->segnum;
603                 exit_side = find_exit_side(ConsoleObject);
604                 segnum = Segments[old_segnum].children[exit_side];
605                 tunnel_length = 0;
606                 do {
607                         entry_side = matt_find_connect_side(segnum,old_segnum);
608                         exit_side = Side_opposite[entry_side];
609                         old_segnum = segnum;
610                         segnum = Segments[segnum].children[exit_side];
611                         tunnel_length++;
612                 } while (segnum >= 0);
613
614                 if (segnum != -2) {
615                         PlayerFinishedLevel(0);         //don't do special sequence
616                         return;
617                 }
618
619                 last_segnum = old_segnum;
620
621                 //now pick transition segnum 1/3 of the way in
622
623                 old_segnum = ConsoleObject->segnum;
624                 exit_side = find_exit_side(ConsoleObject);
625                 segnum = Segments[old_segnum].children[exit_side];
626                 i=tunnel_length/3;
627                 while (i--) {
628
629                         entry_side = matt_find_connect_side(segnum,old_segnum);
630                         exit_side = Side_opposite[entry_side];
631                         old_segnum = segnum;
632                         segnum = Segments[segnum].children[exit_side];
633                 }
634                 transition_segnum = segnum;
635
636         }
637
638         cockpit_mode_save = Cockpit_mode;
639
640         #ifdef NETWORK
641         if (Game_mode & GM_MULTI) {
642                 multi_send_endlevel_start(0);
643                 network_do_frame(1, 1);
644         }
645         #endif
646
647         #ifdef SHAREWARE
648         Assert(last_segnum == exit_segnum);
649         //      songs_play_song( SONG_ENDLEVEL, 0 );    // JTS: Until we get an exit song, just don't worry
650         #endif
651
652         Endlevel_sequence = EL_FLYTHROUGH;
653
654         ConsoleObject->movement_type = MT_NONE;                 //movement handled by flythrough
655         ConsoleObject->control_type = CT_NONE;
656
657         Game_suspended |= SUSP_ROBOTS;          //robots don't move
658
659         cur_fly_speed = desired_fly_speed = FLY_SPEED;
660
661         start_endlevel_flythrough(0,ConsoleObject,cur_fly_speed);               //initialize
662
663         HUD_init_message( TXT_EXIT_SEQUENCE );
664
665         outside_mine = ext_expl_playing = 0;
666
667         flash_scale = f1_0;
668
669         //init_endlevel();
670
671         mine_destroyed=0;
672
673 }
674
675 extern flythrough_data fly_objects[];
676
677 extern object *slew_obj;
678
679 vms_angvec player_angles,player_dest_angles;
680 vms_angvec camera_desired_angles,camera_cur_angles;
681
682 #define CHASE_TURN_RATE (0x4000/4)              //max turn per second
683
684 //returns bitmask of which angles are at dest. bits 0,1,2 = p,b,h
685 int chase_angles(vms_angvec *cur_angles,vms_angvec *desired_angles)
686 {
687         vms_angvec delta_angs,alt_angles,alt_delta_angs;
688         fix total_delta,alt_total_delta;
689         fix frame_turn;
690         int mask=0;
691
692         delta_angs.p = desired_angles->p - cur_angles->p;
693         delta_angs.h = desired_angles->h - cur_angles->h;
694         delta_angs.b = desired_angles->b - cur_angles->b;
695 //delta_angs.b = 0;
696
697 //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);
698
699         total_delta = abs(delta_angs.p) + abs(delta_angs.b) + abs(delta_angs.h);
700
701         alt_angles.p = f1_0/2 - cur_angles->p;
702         alt_angles.b = cur_angles->b + f1_0/2;
703         alt_angles.h = cur_angles->h + f1_0/2;
704
705         alt_delta_angs.p = desired_angles->p - alt_angles.p;
706         alt_delta_angs.h = desired_angles->h - alt_angles.h;
707         alt_delta_angs.b = desired_angles->b - alt_angles.b;
708 //alt_delta_angs.b = 0;
709
710         alt_total_delta = abs(alt_delta_angs.p) + abs(alt_delta_angs.b) + abs(alt_delta_angs.h);
711
712 //printf("Total delta = %x, alt total_delta = %x\n",total_delta,alt_total_delta);
713
714         if (alt_total_delta < total_delta) {
715                 //mprintf((0,"FLIPPING ANGLES!\n"));
716                 //printf("FLIPPING ANGLES!\n");
717                 *cur_angles = alt_angles;
718                 delta_angs = alt_delta_angs;
719         }
720
721         frame_turn = fixmul(FrameTime,CHASE_TURN_RATE);
722
723         if (abs(delta_angs.p) < frame_turn) {
724                 cur_angles->p = desired_angles->p;
725                 mask |= 1;
726         }
727         else
728                 if (delta_angs.p > 0)
729                         cur_angles->p += frame_turn;
730                 else
731                         cur_angles->p -= frame_turn;
732
733         if (abs(delta_angs.b) < frame_turn) {
734                 cur_angles->b = desired_angles->b;
735                 mask |= 2;
736         }
737         else
738                 if (delta_angs.b > 0)
739                         cur_angles->b += frame_turn;
740                 else
741                         cur_angles->b -= frame_turn;
742 //cur_angles->b = 0;
743
744         if (abs(delta_angs.h) < frame_turn) {
745                 cur_angles->h = desired_angles->h;
746                 mask |= 4;
747         }
748         else
749                 if (delta_angs.h > 0)
750                         cur_angles->h += frame_turn;
751                 else
752                         cur_angles->h -= frame_turn;
753
754         return mask;
755 }
756
757 void stop_endlevel_sequence()
758 {
759         Interpolation_method = 0;
760
761         gr_palette_fade_out(gr_palette, 32, 0);
762
763         select_cockpit(cockpit_mode_save);
764
765         Endlevel_sequence = EL_OFF;
766
767         PlayerFinishedLevel(0);
768
769 }
770
771 #define VCLIP_BIG_PLAYER_EXPLOSION      58
772
773 //--unused-- vms_vector upvec = {0,f1_0,0};
774
775 //find the angle between the player's heading & the station
776 void get_angs_to_object(vms_angvec *av,vms_vector *targ_pos,vms_vector *cur_pos)
777 {
778         vms_vector tv;
779
780         vm_vec_sub(&tv,targ_pos,cur_pos);
781
782         vm_extract_angles_vector(av,&tv);
783 }
784
785 void do_endlevel_frame()
786 {
787         static fix timer;
788         static fix bank_rate;
789         vms_vector save_last_pos;
790         static fix explosion_wait1=0;
791         static fix explosion_wait2=0;
792         static fix ext_expl_halflife;
793
794         save_last_pos = ConsoleObject->last_pos;        //don't let move code change this
795         object_move_all();
796         ConsoleObject->last_pos = save_last_pos;
797
798         if (ext_expl_playing) {
799
800                 external_explosion.lifeleft -= FrameTime;
801                 do_explosion_sequence(&external_explosion);
802
803                 if (external_explosion.lifeleft < ext_expl_halflife)
804                         mine_destroyed = 1;
805
806                 if (external_explosion.flags & OF_SHOULD_BE_DEAD)
807                         ext_expl_playing = 0;
808         }
809
810         if (cur_fly_speed != desired_fly_speed) {
811                 fix delta = desired_fly_speed - cur_fly_speed;
812                 fix frame_accel = fixmul(FrameTime,FLY_ACCEL);
813
814                 if (abs(delta) < frame_accel)
815                         cur_fly_speed = desired_fly_speed;
816                 else
817                         if (delta > 0)
818                                 cur_fly_speed += frame_accel;
819                         else
820                                 cur_fly_speed -= frame_accel;
821         }
822
823         //do big explosions
824         if (!outside_mine) {
825
826                 if (Endlevel_sequence==EL_OUTSIDE) {
827                         vms_vector tvec;
828
829                         vm_vec_sub(&tvec,&ConsoleObject->pos,&mine_side_exit_point);
830
831                         if (vm_vec_dot(&tvec,&mine_exit_orient.fvec) > 0) {
832                                 object *tobj;
833
834                                 outside_mine = 1;
835
836                                 tobj = object_create_explosion(exit_segnum,&mine_side_exit_point,i2f(50),VCLIP_BIG_PLAYER_EXPLOSION);
837
838                                 if (tobj) {
839                                         external_explosion = *tobj;
840
841                                         tobj->flags |= OF_SHOULD_BE_DEAD;
842
843                                         flash_scale = 0;        //kill lights in mine
844
845                                         ext_expl_halflife = tobj->lifeleft;
846
847                                         ext_expl_playing = 1;
848                                 }
849
850                                 digi_link_sound_to_pos( SOUND_BIG_ENDLEVEL_EXPLOSION, exit_segnum, 0, &mine_side_exit_point, 0, i2f(3)/4 );
851                         }
852                 }
853
854                 //do explosions chasing player
855                 if ((explosion_wait1-=FrameTime) < 0) {
856                         vms_vector tpnt;
857                         int segnum;
858                         object *expl;
859                         static int sound_count;
860
861                         vm_vec_scale_add(&tpnt,&ConsoleObject->pos,&ConsoleObject->orient.fvec,-ConsoleObject->size*5);
862                         vm_vec_scale_add2(&tpnt,&ConsoleObject->orient.rvec,(d_rand()-RAND_MAX/2)*15);
863                         vm_vec_scale_add2(&tpnt,&ConsoleObject->orient.uvec,(d_rand()-RAND_MAX/2)*15);
864
865                         segnum = find_point_seg(&tpnt,ConsoleObject->segnum);
866
867                         if (segnum != -1) {
868                                 expl = object_create_explosion(segnum,&tpnt,i2f(20),VCLIP_BIG_PLAYER_EXPLOSION);
869                                 if (d_rand()<10000 || ++sound_count==7) {               //pseudo-random
870                                         digi_link_sound_to_pos( SOUND_TUNNEL_EXPLOSION, segnum, 0, &tpnt, 0, F1_0 );
871                                         sound_count=0;
872                                 }
873                         }
874
875                         explosion_wait1 = 0x2000 + d_rand()/4;
876
877                 }
878         }
879
880         //do little explosions on walls
881         if (Endlevel_sequence >= EL_FLYTHROUGH && Endlevel_sequence < EL_OUTSIDE)
882                 if ((explosion_wait2-=FrameTime) < 0) {
883                         vms_vector tpnt;
884                         fvi_query fq;
885                         fvi_info hit_data;
886
887                         //create little explosion on wall
888
889                         vm_vec_copy_scale(&tpnt,&ConsoleObject->orient.rvec,(d_rand()-RAND_MAX/2)*100);
890                         vm_vec_scale_add2(&tpnt,&ConsoleObject->orient.uvec,(d_rand()-RAND_MAX/2)*100);
891                         vm_vec_add2(&tpnt,&ConsoleObject->pos);
892
893                         if (Endlevel_sequence == EL_FLYTHROUGH)
894                                 vm_vec_scale_add2(&tpnt,&ConsoleObject->orient.fvec,d_rand()*200);
895                         else
896                                 vm_vec_scale_add2(&tpnt,&ConsoleObject->orient.fvec,d_rand()*60);
897
898                         //find hit point on wall
899
900                         fq.p0 = &ConsoleObject->pos;
901                         fq.p1 = &tpnt;
902                         fq.startseg = ConsoleObject->segnum;
903                         fq.rad = 0;
904                         fq.thisobjnum = 0;
905                         fq.ignore_obj_list = NULL;
906                         fq.flags = 0;
907
908                         find_vector_intersection(&fq,&hit_data);
909
910                         if (hit_data.hit_type==HIT_WALL && hit_data.hit_seg!=-1)
911                                 object_create_explosion(hit_data.hit_seg,&hit_data.hit_pnt,i2f(3)+d_rand()*6,VCLIP_SMALL_EXPLOSION);
912
913                         explosion_wait2 = (0xa00 + d_rand()/8)/2;
914                 }
915
916         switch (Endlevel_sequence) {
917
918                 case EL_OFF: return;
919
920                 case EL_FLYTHROUGH: {
921
922                         do_endlevel_flythrough(0);
923
924                         if (ConsoleObject->segnum == transition_segnum) {
925
926                                 if ((Current_mission_num == Builtin_mission_num) &&
927                                         (start_endlevel_movie() != MOVIE_NOT_PLAYED))
928                                         stop_endlevel_sequence();
929                                 else {
930                                         int objnum;
931
932                                         //songs_play_song( SONG_ENDLEVEL, 0 );
933
934                                         Endlevel_sequence = EL_LOOKBACK;
935
936                                         objnum = obj_create(OBJ_CAMERA, 0,
937                                                             ConsoleObject->segnum,&ConsoleObject->pos,&ConsoleObject->orient,0,
938                                                             CT_NONE,MT_NONE,RT_NONE);
939
940                                         if (objnum == -1) { //can't get object, so abort
941                                                 mprintf((1, "Can't get object for endlevel sequence.  Aborting endlevel sequence.\n"));
942                                                 stop_endlevel_sequence();
943                                                 return;
944                                         }
945
946                                         Viewer = endlevel_camera = &Objects[objnum];
947
948                                         select_cockpit(CM_LETTERBOX);
949
950                                         fly_objects[1] = fly_objects[0];
951                                         fly_objects[1].obj = endlevel_camera;
952                                         fly_objects[1].speed = (5*cur_fly_speed)/4;
953                                         fly_objects[1].offset_frac = 0x4000;
954
955                                         vm_vec_scale_add2(&endlevel_camera->pos,&endlevel_camera->orient.fvec,i2f(7));
956
957                                         timer=0x20000;
958
959                                 }
960                         }
961
962                         break;
963                 }
964
965
966                 case EL_LOOKBACK: {
967
968                         do_endlevel_flythrough(0);
969                         do_endlevel_flythrough(1);
970
971                         if (timer>0) {
972
973                                 timer -= FrameTime;
974
975                                 if (timer < 0)          //reduce speed
976                                         fly_objects[1].speed = fly_objects[0].speed;
977                         }
978
979                         if (endlevel_camera->segnum == exit_segnum) {
980                                 vms_angvec cam_angles,exit_seg_angles;
981
982                                 Endlevel_sequence = EL_OUTSIDE;
983
984                                 timer = i2f(2);
985
986                                 vm_vec_negate(&endlevel_camera->orient.fvec);
987                                 vm_vec_negate(&endlevel_camera->orient.rvec);
988
989                                 vm_extract_angles_matrix(&cam_angles,&endlevel_camera->orient);
990                                 vm_extract_angles_matrix(&exit_seg_angles,&mine_exit_orient);
991                                 bank_rate = (-exit_seg_angles.b - cam_angles.b)/2;
992
993                                 ConsoleObject->control_type = endlevel_camera->control_type = CT_NONE;
994
995 #ifdef SLEW_ON
996  slew_obj = endlevel_camera;
997 #endif
998                         }
999
1000                         break;
1001                 }
1002
1003                 case EL_OUTSIDE: {
1004                         #ifndef SLEW_ON
1005                         vms_angvec cam_angles;
1006                         #endif
1007
1008                         vm_vec_scale_add2(&ConsoleObject->pos,&ConsoleObject->orient.fvec,fixmul(FrameTime,cur_fly_speed));
1009 #ifndef SLEW_ON
1010                         vm_vec_scale_add2(&endlevel_camera->pos,&endlevel_camera->orient.fvec,fixmul(FrameTime,-2*cur_fly_speed));
1011                         vm_vec_scale_add2(&endlevel_camera->pos,&endlevel_camera->orient.uvec,fixmul(FrameTime,-cur_fly_speed/10));
1012
1013                         vm_extract_angles_matrix(&cam_angles,&endlevel_camera->orient);
1014                         cam_angles.b += fixmul(bank_rate,FrameTime);
1015                         vm_angles_2_matrix(&endlevel_camera->orient,&cam_angles);
1016 #endif
1017
1018                         timer -= FrameTime;
1019
1020                         if (timer < 0) {
1021
1022                                 Endlevel_sequence = EL_STOPPED;
1023
1024                                 vm_extract_angles_matrix(&player_angles,&ConsoleObject->orient);
1025
1026                                 timer = i2f(3);
1027
1028                         }
1029
1030                         break;
1031                 }
1032
1033                 case EL_STOPPED: {
1034
1035                         get_angs_to_object(&player_dest_angles,&station_pos,&ConsoleObject->pos);
1036                         chase_angles(&player_angles,&player_dest_angles);
1037                         vm_angles_2_matrix(&ConsoleObject->orient,&player_angles);
1038
1039                         vm_vec_scale_add2(&ConsoleObject->pos,&ConsoleObject->orient.fvec,fixmul(FrameTime,cur_fly_speed));
1040
1041                         timer -= FrameTime;
1042
1043                         if (timer < 0) {
1044
1045                                 #ifdef SLEW_ON
1046                                 slew_obj = endlevel_camera;
1047                                 _do_slew_movement(endlevel_camera,1,1);
1048                                 timer += FrameTime;             //make time stop
1049                                 break;
1050                                 #else
1051
1052                                 #ifdef SHORT_SEQUENCE
1053
1054                                 stop_endlevel_sequence();
1055
1056                                 #else
1057                                 Endlevel_sequence = EL_PANNING;
1058
1059                                 vm_extract_angles_matrix(&camera_cur_angles,&endlevel_camera->orient);
1060
1061
1062                                 timer = i2f(3);
1063
1064                                 if (Game_mode & GM_MULTI) { // try to skip part of the seq if multiplayer
1065                                         stop_endlevel_sequence();
1066                                         return;
1067                                 }
1068
1069                                 //mprintf((0,"Switching to pan...\n"));
1070                                 #endif          //SHORT_SEQUENCE
1071                                 #endif          //SLEW_ON
1072
1073                         }
1074                         break;
1075                 }
1076
1077                 #ifndef SHORT_SEQUENCE
1078                 case EL_PANNING: {
1079                         #ifndef SLEW_ON
1080                         int mask;
1081                         #endif
1082
1083                         get_angs_to_object(&player_dest_angles,&station_pos,&ConsoleObject->pos);
1084                         chase_angles(&player_angles,&player_dest_angles);
1085                         vm_angles_2_matrix(&ConsoleObject->orient,&player_angles);
1086                         vm_vec_scale_add2(&ConsoleObject->pos,&ConsoleObject->orient.fvec,fixmul(FrameTime,cur_fly_speed));
1087
1088                         #ifdef SLEW_ON
1089                         _do_slew_movement(endlevel_camera,1,1);
1090                         #else
1091
1092                         get_angs_to_object(&camera_desired_angles,&ConsoleObject->pos,&endlevel_camera->pos);
1093                         mask = chase_angles(&camera_cur_angles,&camera_desired_angles);
1094                         vm_angles_2_matrix(&endlevel_camera->orient,&camera_cur_angles);
1095
1096                         if ((mask&5) == 5) {
1097
1098                                 vms_vector tvec;
1099
1100                                 Endlevel_sequence = EL_CHASING;
1101
1102                                 vm_vec_normalized_dir_quick(&tvec,&station_pos,&ConsoleObject->pos);
1103                                 vm_vector_2_matrix(&ConsoleObject->orient,&tvec,&surface_orient.uvec,NULL);
1104
1105                                 desired_fly_speed *= 2;
1106
1107                                 //mprintf((0,"Switching to chase...\n"));
1108
1109                         }
1110                         #endif
1111
1112                         break;
1113                 }
1114
1115                 case EL_CHASING: {
1116                         fix d,speed_scale;
1117
1118                         #ifdef SLEW_ON
1119                         _do_slew_movement(endlevel_camera,1,1);
1120                         #endif
1121
1122                         get_angs_to_object(&camera_desired_angles,&ConsoleObject->pos,&endlevel_camera->pos);
1123                         chase_angles(&camera_cur_angles,&camera_desired_angles);
1124
1125                         #ifndef SLEW_ON
1126                         vm_angles_2_matrix(&endlevel_camera->orient,&camera_cur_angles);
1127                         #endif
1128
1129                         d = vm_vec_dist_quick(&ConsoleObject->pos,&endlevel_camera->pos);
1130
1131                         speed_scale = fixdiv(d,i2f(0x20));
1132                         if (d<f1_0) d=f1_0;
1133
1134                         get_angs_to_object(&player_dest_angles,&station_pos,&ConsoleObject->pos);
1135                         chase_angles(&player_angles,&player_dest_angles);
1136                         vm_angles_2_matrix(&ConsoleObject->orient,&player_angles);
1137
1138                         vm_vec_scale_add2(&ConsoleObject->pos,&ConsoleObject->orient.fvec,fixmul(FrameTime,cur_fly_speed));
1139                         #ifndef SLEW_ON
1140                         vm_vec_scale_add2(&endlevel_camera->pos,&endlevel_camera->orient.fvec,fixmul(FrameTime,fixmul(speed_scale,cur_fly_speed)));
1141
1142                         if (vm_vec_dist(&ConsoleObject->pos,&station_pos) < i2f(10))
1143                                 stop_endlevel_sequence();
1144                         #endif
1145
1146                         break;
1147
1148                 }
1149                 #endif          //ifdef SHORT_SEQUENCE
1150
1151         }
1152 }
1153
1154
1155 #define MIN_D 0x100
1156
1157 //find which side to fly out of
1158 int find_exit_side(object *obj)
1159 {
1160         int i;
1161         vms_vector prefvec,segcenter,sidevec;
1162         fix best_val=-f2_0;
1163         int best_side;
1164         segment *pseg = &Segments[obj->segnum];
1165
1166         //find exit side
1167
1168         vm_vec_normalized_dir_quick(&prefvec,&obj->pos,&obj->last_pos);
1169
1170         compute_segment_center(&segcenter,pseg);
1171
1172         best_side=-1;
1173         for (i=MAX_SIDES_PER_SEGMENT;--i >= 0;) {
1174                 fix d;
1175
1176                 if (pseg->children[i]!=-1) {
1177
1178                         compute_center_point_on_side(&sidevec,pseg,i);
1179                         vm_vec_normalized_dir_quick(&sidevec,&sidevec,&segcenter);
1180                         d = vm_vec_dotprod(&sidevec,&prefvec);
1181
1182                         if (labs(d) < MIN_D) d=0;
1183
1184                         if (d > best_val) {best_val=d; best_side=i;}
1185
1186                 }
1187         }
1188
1189         Assert(best_side!=-1);
1190
1191         return best_side;
1192 }
1193
1194 extern fix Render_zoom;                                                 //the player's zoom factor
1195
1196 extern vms_vector Viewer_eye;   //valid during render
1197
1198 void draw_exit_model()
1199 {
1200         vms_vector model_pos;
1201         int f=15,u=0;   //21;
1202
1203         vm_vec_scale_add(&model_pos,&mine_exit_point,&mine_exit_orient.fvec,i2f(f));
1204         vm_vec_scale_add2(&model_pos,&mine_exit_orient.uvec,i2f(u));
1205
1206         draw_polygon_model(&model_pos,&mine_exit_orient,NULL,(mine_destroyed)?destroyed_exit_modelnum:exit_modelnum,0,f1_0,NULL,NULL);
1207
1208 }
1209
1210 int exit_point_bmx,exit_point_bmy;
1211
1212 fix satellite_size = i2f(400);
1213
1214 #define SATELLITE_DIST          i2f(1024)
1215 #define SATELLITE_WIDTH         satellite_size
1216 #define SATELLITE_HEIGHT        ((satellite_size*9)/4)          //((satellite_size*5)/2)
1217
1218 void render_external_scene(fix eye_offset)
1219 {
1220
1221         Viewer_eye = Viewer->pos;
1222
1223         if (eye_offset)
1224                 vm_vec_scale_add2(&Viewer_eye,&Viewer->orient.rvec,eye_offset);
1225
1226         g3_set_view_matrix(&Viewer->pos,&Viewer->orient,Render_zoom);
1227
1228         //g3_draw_horizon(BM_XRGB(0,0,0),BM_XRGB(16,16,16));            //,-1);
1229         gr_clear_canvas(BM_XRGB(0,0,0));
1230
1231         g3_start_instance_matrix(&vmd_zero_vector,&surface_orient);
1232         draw_stars();
1233         g3_done_instance();
1234
1235         {       //draw satellite
1236
1237                 vms_vector delta;
1238                 g3s_point p,top_pnt;
1239
1240                 g3_rotate_point(&p,&satellite_pos);
1241                 g3_rotate_delta_vec(&delta,&satellite_upvec);
1242
1243                 g3_add_delta_vec(&top_pnt,&p,&delta);
1244
1245                 if (! (p.p3_codes & CC_BEHIND)) {
1246                         int save_im = Interpolation_method;
1247                         //p.p3_flags &= ~PF_PROJECTED;
1248                         //g3_project_point(&p);
1249                         if (! (p.p3_flags & PF_OVERFLOW)) {
1250                                 Interpolation_method = 0;
1251                                 //gr_bitmapm(f2i(p.p3_sx)-32,f2i(p.p3_sy)-32,satellite_bitmap);
1252                                 g3_draw_rod_tmap(satellite_bitmap,&p,SATELLITE_WIDTH,&top_pnt,SATELLITE_WIDTH,f1_0);
1253                                 Interpolation_method = save_im;
1254                         }
1255                 }
1256         }
1257
1258         #ifdef STATION_ENABLED
1259         draw_polygon_model(&station_pos,&vmd_identity_matrix,NULL,station_modelnum,0,f1_0,NULL,NULL);
1260         #endif
1261
1262         render_terrain(&mine_ground_exit_point,exit_point_bmx,exit_point_bmy);
1263
1264         draw_exit_model();
1265         if (ext_expl_playing)
1266                 draw_fireball(&external_explosion);
1267
1268         Lighting_on=0;
1269         render_object(ConsoleObject);
1270         Lighting_on=1;
1271 }
1272
1273 #define MAX_STARS 500
1274
1275 vms_vector stars[MAX_STARS];
1276
1277 void generate_starfield()
1278 {
1279         int i;
1280
1281         for (i=0;i<MAX_STARS;i++) {
1282
1283                 stars[i].x = (d_rand() - RAND_MAX/2) << 14;
1284                 stars[i].z = (d_rand() - RAND_MAX/2) << 14;
1285                 stars[i].y = (d_rand()/2) << 14;
1286
1287         }
1288 }
1289
1290 void draw_stars()
1291 {
1292         int i;
1293         int intensity=31;
1294         g3s_point p;
1295
1296         for (i=0;i<MAX_STARS;i++) {
1297
1298                 if ((i&63) == 0) {
1299                         gr_setcolor(BM_XRGB(intensity,intensity,intensity));
1300                         intensity-=3;
1301                 }
1302
1303                 //g3_rotate_point(&p,&stars[i]);
1304                 g3_rotate_delta_vec(&p.p3_vec,&stars[i]);
1305                 g3_code_point(&p);
1306
1307                 if (p.p3_codes == 0) {
1308
1309                         p.p3_flags &= ~PF_PROJECTED;
1310
1311                         g3_project_point(&p);
1312
1313                         gr_pixel(f2i(p.p3_sx),f2i(p.p3_sy));
1314                 }
1315         }
1316
1317 //@@    {
1318 //@@            vms_vector delta;
1319 //@@            g3s_point top_pnt;
1320 //@@
1321 //@@            g3_rotate_point(&p,&satellite_pos);
1322 //@@            g3_rotate_delta_vec(&delta,&satellite_upvec);
1323 //@@
1324 //@@            g3_add_delta_vec(&top_pnt,&p,&delta);
1325 //@@
1326 //@@            if (! (p.p3_codes & CC_BEHIND)) {
1327 //@@                    int save_im = Interpolation_method;
1328 //@@                    Interpolation_method = 0;
1329 //@@                    //p.p3_flags &= ~PF_PROJECTED;
1330 //@@                    g3_project_point(&p);
1331 //@@                    if (! (p.p3_flags & PF_OVERFLOW))
1332 //@@                            //gr_bitmapm(f2i(p.p3_sx)-32,f2i(p.p3_sy)-32,satellite_bitmap);
1333 //@@                            g3_draw_rod_tmap(satellite_bitmap,&p,SATELLITE_WIDTH,&top_pnt,SATELLITE_WIDTH,f1_0);
1334 //@@                    Interpolation_method = save_im;
1335 //@@            }
1336 //@@    }
1337
1338 }
1339
1340 void endlevel_render_mine(fix eye_offset)
1341 {
1342         int start_seg_num;
1343
1344         Viewer_eye = Viewer->pos;
1345
1346         if (Viewer->type == OBJ_PLAYER )
1347                 vm_vec_scale_add2(&Viewer_eye,&Viewer->orient.fvec,(Viewer->size*3)/4);
1348
1349         if (eye_offset)
1350                 vm_vec_scale_add2(&Viewer_eye,&Viewer->orient.rvec,eye_offset);
1351
1352         #ifdef EDITOR
1353         if (Function_mode==FMODE_EDITOR)
1354                 Viewer_eye = Viewer->pos;
1355         #endif
1356
1357         if (Endlevel_sequence >= EL_OUTSIDE) {
1358
1359                 start_seg_num = exit_segnum;
1360         }
1361         else {
1362                 start_seg_num = find_point_seg(&Viewer_eye,Viewer->segnum);
1363
1364                 if (start_seg_num==-1)
1365                         start_seg_num = Viewer->segnum;
1366         }
1367
1368         if (Endlevel_sequence == EL_LOOKBACK) {
1369                 vms_matrix headm,viewm;
1370                 vms_angvec angles = {0,0,0x7fff};
1371
1372                 vm_angles_2_matrix(&headm,&angles);
1373                 vm_matrix_x_matrix(&viewm,&Viewer->orient,&headm);
1374                 g3_set_view_matrix(&Viewer_eye,&viewm,Render_zoom);
1375         }
1376         else
1377                 g3_set_view_matrix(&Viewer_eye,&Viewer->orient,Render_zoom);
1378
1379         render_mine(start_seg_num,eye_offset, 0);
1380 }
1381
1382 void render_endlevel_frame(fix eye_offset)
1383 {
1384
1385         g3_start_frame();
1386
1387         if (Endlevel_sequence < EL_OUTSIDE)
1388                 endlevel_render_mine(eye_offset);
1389         else
1390                 render_external_scene(eye_offset);
1391
1392         g3_end_frame();
1393
1394 }
1395
1396
1397 ///////////////////////// copy of flythrough code for endlevel
1398
1399
1400 #define MAX_FLY_OBJECTS 2
1401
1402 flythrough_data fly_objects[MAX_FLY_OBJECTS];
1403
1404 flythrough_data *flydata;
1405
1406 int matt_find_connect_side(int seg0,int seg1);
1407
1408 void compute_segment_center(vms_vector *vp,segment *sp);
1409
1410 fixang delta_ang(fixang a,fixang b);
1411 fixang interp_angle(fixang dest,fixang src,fixang step);
1412
1413 #define DEFAULT_SPEED i2f(16)
1414
1415 #define MIN_D 0x100
1416
1417 //if speed is zero, use default speed
1418 void start_endlevel_flythrough(int n,object *obj,fix speed)
1419 {
1420         flydata = &fly_objects[n];
1421
1422         flydata->obj = obj;
1423
1424         flydata->first_time = 1;
1425
1426         flydata->speed = speed?speed:DEFAULT_SPEED;
1427
1428         flydata->offset_frac = 0;
1429 }
1430
1431 static vms_angvec *angvec_add2_scale(vms_angvec *dest,vms_vector *src,fix s)
1432 {
1433         dest->p += fixmul(src->x,s);
1434         dest->b  += fixmul(src->z,s);
1435         dest->h  += fixmul(src->y,s);
1436
1437         return dest;
1438 }
1439
1440 #define MAX_ANGSTEP     0x4000          //max turn per second
1441
1442 #define MAX_SLIDE_PER_SEGMENT 0x10000
1443
1444 void do_endlevel_flythrough(int n)
1445 {
1446         object *obj;
1447         segment *pseg;
1448         int old_player_seg;
1449
1450         flydata = &fly_objects[n];
1451         obj = flydata->obj;
1452         
1453         old_player_seg = obj->segnum;
1454
1455         //move the player for this frame
1456
1457         if (!flydata->first_time) {
1458
1459                 vm_vec_scale_add2(&obj->pos,&flydata->step,FrameTime);
1460                 angvec_add2_scale(&flydata->angles,&flydata->angstep,FrameTime);
1461
1462                 vm_angles_2_matrix(&obj->orient,&flydata->angles);
1463         }
1464
1465         //check new player seg
1466
1467         update_object_seg(obj);
1468         pseg = &Segments[obj->segnum];
1469
1470         if (flydata->first_time || obj->segnum != old_player_seg) {             //moved into new seg
1471                 vms_vector curcenter,nextcenter;
1472                 fix step_size,seg_time;
1473                 short entry_side,exit_side = -1;//what sides we entry and leave through
1474                 vms_vector dest_point;          //where we are heading (center of exit_side)
1475                 vms_angvec dest_angles;         //where we want to be pointing
1476                 vms_matrix dest_orient;
1477                 int up_side=0;
1478
1479                 entry_side=0;
1480
1481                 //find new exit side
1482
1483                 if (!flydata->first_time) {
1484
1485                         entry_side = matt_find_connect_side(obj->segnum,old_player_seg);
1486                         exit_side = Side_opposite[entry_side];
1487                 }
1488
1489                 if (flydata->first_time || entry_side==-1 || pseg->children[exit_side]==-1)
1490                         exit_side = find_exit_side(obj);
1491
1492                 {                                                                               //find closest side to align to
1493                         fix d,largest_d=-f1_0;
1494                         int i;
1495
1496                         for (i=0;i<6;i++) {
1497                                 #ifdef COMPACT_SEGS
1498                                 vms_vector v1;
1499                                 get_side_normal(pseg, i, 0, &v1 );
1500                                 d = vm_vec_dot(&v1,&flydata->obj->orient.uvec);
1501                                 #else
1502                                 d = vm_vec_dot(&pseg->sides[i].normals[0],&flydata->obj->orient.uvec);
1503                                 #endif
1504                                 if (d > largest_d) {largest_d = d; up_side=i;}
1505                         }
1506
1507                 }
1508
1509                 //update target point & angles
1510
1511                 compute_center_point_on_side(&dest_point,pseg,exit_side);
1512
1513                 //update target point and movement points
1514
1515                 //offset object sideways
1516                 if (flydata->offset_frac) {
1517                         int s0=-1,s1=0,i;
1518                         vms_vector s0p,s1p;
1519                         fix dist;
1520
1521                         for (i=0;i<6;i++)
1522                                 if (i!=entry_side && i!=exit_side && i!=up_side && i!=Side_opposite[up_side])
1523                                  {
1524                                         if (s0==-1)
1525                                                 s0 = i;
1526                                         else
1527                                                 s1 = i;
1528                                  }
1529
1530                         compute_center_point_on_side(&s0p,pseg,s0);
1531                         compute_center_point_on_side(&s1p,pseg,s1);
1532                         dist = fixmul(vm_vec_dist(&s0p,&s1p),flydata->offset_frac);
1533
1534                         if (dist-flydata->offset_dist > MAX_SLIDE_PER_SEGMENT)
1535                                 dist = flydata->offset_dist + MAX_SLIDE_PER_SEGMENT;
1536
1537                         flydata->offset_dist = dist;
1538
1539                         vm_vec_scale_add2(&dest_point,&obj->orient.rvec,dist);
1540
1541                 }
1542
1543                 vm_vec_sub(&flydata->step,&dest_point,&obj->pos);
1544                 step_size = vm_vec_normalize_quick(&flydata->step);
1545                 vm_vec_scale(&flydata->step,flydata->speed);
1546
1547                 compute_segment_center(&curcenter,pseg);
1548                 compute_segment_center(&nextcenter,&Segments[pseg->children[exit_side]]);
1549                 vm_vec_sub(&flydata->headvec,&nextcenter,&curcenter);
1550
1551                 #ifdef COMPACT_SEGS
1552                 {
1553                         vms_vector _v1;
1554                         get_side_normal(pseg, up_side, 0, &_v1 );
1555                         vm_vector_2_matrix(&dest_orient,&flydata->headvec,&_v1,NULL);
1556                 }
1557                 #else
1558                 vm_vector_2_matrix(&dest_orient,&flydata->headvec,&pseg->sides[up_side].normals[0],NULL);
1559                 #endif
1560                 vm_extract_angles_matrix(&dest_angles,&dest_orient);
1561
1562                 if (flydata->first_time)
1563                         vm_extract_angles_matrix(&flydata->angles,&obj->orient);
1564
1565                 seg_time = fixdiv(step_size,flydata->speed);    //how long through seg
1566
1567                 if (seg_time) {
1568                         flydata->angstep.x = max(-MAX_ANGSTEP,min(MAX_ANGSTEP,fixdiv(delta_ang(flydata->angles.p,dest_angles.p),seg_time)));
1569                         flydata->angstep.z = max(-MAX_ANGSTEP,min(MAX_ANGSTEP,fixdiv(delta_ang(flydata->angles.b,dest_angles.b),seg_time)));
1570                         flydata->angstep.y = max(-MAX_ANGSTEP,min(MAX_ANGSTEP,fixdiv(delta_ang(flydata->angles.h,dest_angles.h),seg_time)));
1571
1572                 }
1573                 else {
1574                         flydata->angles = dest_angles;
1575                         flydata->angstep.x = flydata->angstep.y = flydata->angstep.z = 0;
1576                 }
1577         }
1578
1579         flydata->first_time=0;
1580 }
1581
1582 #define JOY_NULL 15
1583 #define ROT_SPEED 8             //rate of rotation while key held down
1584 #define VEL_SPEED (15)  //rate of acceleration while key held down
1585
1586 extern short old_joy_x,old_joy_y;       //position last time around
1587
1588 #include "key.h"
1589 #include "joy.h"
1590
1591 #ifdef SLEW_ON          //this is a special routine for slewing around external scene
1592 int _do_slew_movement(object *obj, int check_keys, int check_joy )
1593 {
1594         int moved = 0;
1595         vms_vector svel, movement;                              //scaled velocity (per this frame)
1596         vms_matrix rotmat,new_pm;
1597         int joy_x,joy_y,btns;
1598         int joyx_moved,joyy_moved;
1599         vms_angvec rotang;
1600
1601         if (keyd_pressed[KEY_PAD5])
1602                 vm_vec_zero(&obj->phys_info.velocity);
1603
1604         if (check_keys) {
1605                 obj->phys_info.velocity.x += VEL_SPEED * (key_down_time(KEY_PAD9) - key_down_time(KEY_PAD7));
1606                 obj->phys_info.velocity.y += VEL_SPEED * (key_down_time(KEY_PADMINUS) - key_down_time(KEY_PADPLUS));
1607                 obj->phys_info.velocity.z += VEL_SPEED * (key_down_time(KEY_PAD8) - key_down_time(KEY_PAD2));
1608
1609                 rotang.pitch =  (key_down_time(KEY_LBRACKET) - key_down_time(KEY_RBRACKET))/ROT_SPEED;
1610                 rotang.bank  = (key_down_time(KEY_PAD1) - key_down_time(KEY_PAD3))/ROT_SPEED;
1611                 rotang.head  = (key_down_time(KEY_PAD6) - key_down_time(KEY_PAD4))/ROT_SPEED;
1612         }
1613         else
1614                 rotang.pitch = rotang.bank  = rotang.head  = 0;
1615
1616         //check for joystick movement
1617
1618         if (check_joy && joy_present)   {
1619                 joy_get_pos(&joy_x,&joy_y);
1620                 btns=joy_get_btns();
1621
1622                 joyx_moved = (abs(joy_x - old_joy_x)>JOY_NULL);
1623                 joyy_moved = (abs(joy_y - old_joy_y)>JOY_NULL);
1624
1625                 if (abs(joy_x) < JOY_NULL) joy_x = 0;
1626                 if (abs(joy_y) < JOY_NULL) joy_y = 0;
1627
1628                 if (btns)
1629                         if (!rotang.pitch) rotang.pitch = fixmul(-joy_y * 512,FrameTime); else;
1630                 else
1631                         if (joyy_moved) obj->phys_info.velocity.z = -joy_y * 8192;
1632
1633                 if (!rotang.head) rotang.head = fixmul(joy_x * 512,FrameTime);
1634
1635                 if (joyx_moved) old_joy_x = joy_x;
1636                 if (joyy_moved) old_joy_y = joy_y;
1637         }
1638
1639         moved = rotang.pitch | rotang.bank | rotang.head;
1640
1641         vm_angles_2_matrix(&rotmat,&rotang);
1642         vm_matrix_x_matrix(&new_pm,&obj->orient,&rotmat);
1643         obj->orient = new_pm;
1644         vm_transpose_matrix(&new_pm);           //make those columns rows
1645
1646         moved |= obj->phys_info.velocity.x | obj->phys_info.velocity.y | obj->phys_info.velocity.z;
1647
1648         svel = obj->phys_info.velocity;
1649         vm_vec_scale(&svel,FrameTime);          //movement in this frame
1650         vm_vec_rotate(&movement,&svel,&new_pm);
1651
1652         vm_vec_add2(&obj->pos,&movement);
1653
1654         moved |= (movement.x || movement.y || movement.z);
1655
1656         return moved;
1657 }
1658 #endif
1659
1660 #define LINE_LEN        80
1661 #define NUM_VARS        8
1662
1663 #define STATION_DIST    i2f(1024)
1664
1665 int convert_ext( char *dest, char *ext )
1666 {
1667         char *t;
1668
1669         t = strchr(dest,'.');
1670
1671         if (t && (t-dest <= 8)) {
1672                 t[1] = ext[0];                  
1673                 t[2] = ext[1];                  
1674                 t[3] = ext[2];  
1675                 return 1;
1676         }
1677         else
1678                 return 0;
1679 }
1680
1681 //called for each level to load & setup the exit sequence
1682 void load_endlevel_data(int level_num)
1683 {
1684         char filename[13];
1685         char line[LINE_LEN],*p;
1686         CFILE *ifile;
1687         int var,segnum,sidenum;
1688         int exit_side=0, i;
1689         int have_binary = 0;
1690
1691         endlevel_data_loaded = 0;               //not loaded yet
1692
1693 try_again:
1694         ;
1695
1696         if (level_num<0)                //secret level
1697                 strcpy(filename,Secret_level_names[-level_num-1]);
1698         else                                    //normal level
1699                 strcpy(filename,Level_names[level_num-1]);
1700
1701         if (!convert_ext(filename,"END"))
1702                 Error("Error converting filename <%s> for endlevel data\n",filename);
1703
1704         ifile = cfopen(filename,"rb");
1705
1706         if (!ifile) {
1707
1708                 convert_ext(filename,"txb");
1709
1710                 ifile = cfopen(filename,"rb");
1711
1712                 if (!ifile) {
1713                         if (level_num==1) {
1714                                 con_printf(CON_DEBUG, "Cannot load file text of binary version of <%s>\n",filename);
1715                                 endlevel_data_loaded = 0; // won't be able to play endlevel sequence
1716                                 return;
1717                         }
1718                         else {
1719                                 level_num = 1;
1720                                 goto try_again;
1721                         }
1722                 }
1723
1724                 have_binary = 1;
1725         }
1726
1727         //ok...this parser is pretty simple.  It ignores comments, but
1728         //everything else must be in the right place
1729
1730         var = 0;
1731
1732         while (cfgets(line,LINE_LEN,ifile)) {
1733
1734                 if (have_binary) {
1735                         for (i = 0; i < strlen(line) - 1; i++) {
1736                                 encode_rotate_left(&(line[i]));
1737                                 line[i] = line[i] ^ BITMAP_TBL_XOR;
1738                                 encode_rotate_left(&(line[i]));
1739                         }
1740                         p = line;
1741                 }
1742
1743                 if ((p=strchr(line,';'))!=NULL)
1744                         *p = 0;         //cut off comment
1745
1746                 for (p=line+strlen(line)-1;p>line && isspace(*p);*p--=0);
1747                 for (p=line;isspace(*p);p++);
1748
1749                 if (!*p)                //empty line
1750                         continue;
1751
1752                 switch (var) {
1753
1754                         case 0: {                                               //ground terrain
1755                                 int iff_error;
1756                                 ubyte pal[768];
1757
1758                                 if (terrain_bm_instance.bm_data)
1759                                         d_free(terrain_bm_instance.bm_data);
1760
1761                                 Assert(terrain_bm_instance.bm_data == NULL);
1762
1763                                 iff_error = iff_read_bitmap(p,&terrain_bm_instance,BM_LINEAR,pal);
1764                                 if (iff_error != IFF_NO_ERROR) {
1765                                         Warning("Can't load exit terrain from file %s: IFF error: %s",
1766                                                 p, iff_errormsg(iff_error));
1767                                         endlevel_data_loaded = 0; // won't be able to play endlevel sequence
1768                                         return;
1769                                 }
1770
1771                                 terrain_bitmap = &terrain_bm_instance;
1772
1773                                 gr_remap_bitmap_good( terrain_bitmap, pal, iff_transparent_color, -1);
1774
1775                                 break;
1776                         }
1777
1778                         case 1:                                                 //height map
1779
1780                                 load_terrain(p);
1781                                 break;
1782
1783
1784                         case 2:
1785
1786                                 sscanf(p,"%d,%d",&exit_point_bmx,&exit_point_bmy);
1787                                 break;
1788
1789                         case 3:                                                 //exit heading
1790
1791                                 exit_angles.h = i2f(atoi(p))/360;
1792                                 break;
1793
1794                         case 4: {                                               //planet bitmap
1795                                 int iff_error;
1796                                 ubyte pal[768];
1797
1798                                 if (satellite_bm_instance.bm_data)
1799                                         d_free(satellite_bm_instance.bm_data);
1800
1801                                 iff_error = iff_read_bitmap(p,&satellite_bm_instance,BM_LINEAR,pal);
1802                                 if (iff_error != IFF_NO_ERROR) {
1803                                         Warning("Can't load exit satellite from file %s: IFF error: %s",
1804                                                 p, iff_errormsg(iff_error));
1805                                         endlevel_data_loaded = 0; // won't be able to play endlevel sequence
1806                                         return;
1807                                 }
1808
1809                                 satellite_bitmap = &satellite_bm_instance;
1810                                 gr_remap_bitmap_good( satellite_bitmap, pal, iff_transparent_color, -1);
1811
1812                                 break;
1813                         }
1814
1815                         case 5:                                                 //earth pos
1816                         case 7: {                                               //station pos
1817                                 vms_matrix tm;
1818                                 vms_angvec ta;
1819                                 int pitch,head;
1820
1821                                 sscanf(p,"%d,%d",&head,&pitch);
1822
1823                                 ta.h = i2f(head)/360;
1824                                 ta.p = -i2f(pitch)/360;
1825                                 ta.b = 0;
1826
1827                                 vm_angles_2_matrix(&tm,&ta);
1828
1829                                 if (var==5)
1830                                         satellite_pos = tm.fvec;
1831                                         //vm_vec_copy_scale(&satellite_pos,&tm.fvec,SATELLITE_DIST);
1832                                 else
1833                                         station_pos = tm.fvec;
1834
1835                                 break;
1836                         }
1837
1838                         case 6:                                         //planet size
1839                                 satellite_size = i2f(atoi(p));
1840                                 break;
1841                 }
1842
1843                 var++;
1844
1845         }
1846
1847         Assert(var == NUM_VARS);
1848
1849
1850         // OK, now the data is loaded.  Initialize everything
1851
1852         //find the exit sequence by searching all segments for a side with
1853         //children == -2
1854
1855         for (segnum=0,exit_segnum=-1;exit_segnum==-1 && segnum<=Highest_segment_index;segnum++)
1856                 for (sidenum=0;sidenum<6;sidenum++)
1857                         if (Segments[segnum].children[sidenum] == -2) {
1858                                 exit_segnum = segnum;
1859                                 exit_side = sidenum;
1860                                 break;
1861                         }
1862
1863         Assert(exit_segnum!=-1);
1864
1865         compute_segment_center(&mine_exit_point,&Segments[exit_segnum]);
1866         extract_orient_from_segment(&mine_exit_orient,&Segments[exit_segnum]);
1867         compute_center_point_on_side(&mine_side_exit_point,&Segments[exit_segnum],exit_side);
1868
1869         vm_vec_scale_add(&mine_ground_exit_point,&mine_exit_point,&mine_exit_orient.uvec,-i2f(20));
1870
1871         //compute orientation of surface
1872         {
1873                 vms_vector tv;
1874                 vms_matrix exit_orient,tm;
1875
1876                 vm_angles_2_matrix(&exit_orient,&exit_angles);
1877                 vm_transpose_matrix(&exit_orient);
1878                 vm_matrix_x_matrix(&surface_orient,&mine_exit_orient,&exit_orient);
1879
1880                 vm_copy_transpose_matrix(&tm,&surface_orient);
1881                 vm_vec_rotate(&tv,&station_pos,&tm);
1882                 vm_vec_scale_add(&station_pos,&mine_exit_point,&tv,STATION_DIST);
1883
1884                 vm_vec_rotate(&tv,&satellite_pos,&tm);
1885                 vm_vec_scale_add(&satellite_pos,&mine_exit_point,&tv,SATELLITE_DIST);
1886
1887                 vm_vector_2_matrix(&tm,&tv,&surface_orient.uvec,NULL);
1888                 vm_vec_copy_scale(&satellite_upvec,&tm.uvec,SATELLITE_HEIGHT);
1889
1890
1891         }
1892
1893         cfclose(ifile);
1894
1895         endlevel_data_loaded = 1;
1896
1897 }