2 * $Logfile: /Freespace2/code/Starfield/Supernova.cpp $
7 * Include file for nebula stuff
10 * Revision 1.2 2002/05/07 03:16:52 theoddone33
11 * The Great Newline Fix
13 * Revision 1.1.1.1 2002/05/03 03:28:10 root
17 * 5 9/09/99 11:40p Dave
18 * Handle an Assert() in beam code. Added supernova sounds. Play the right
19 * 2 end movies properly, based upon what the player did in the mission.
21 * 4 9/03/99 1:32a Dave
22 * CD checking by act. Added support to play 2 cutscenes in a row
23 * seamlessly. Fixed super low level cfile bug related to files in the
24 * root directory of a CD. Added cheat code to set campaign mission # in
27 * 3 7/31/99 4:15p Dave
28 * Fixed supernova particle velocities. Handle OBJ_NONE in target
29 * monitoring view. Properly use objectives notify gauge colors.
31 * 2 7/21/99 8:10p Dave
32 * First run of supernova effect.
38 #include "freespace.h"
42 #include "starfield.h"
43 #include "supernova.h"
48 #include "popupdead.h"
50 #include "missioncampaign.h"
51 #include "gamesequence.h"
56 // --------------------------------------------------------------------------------------------------------------------------
57 // SUPERNOVA DEFINES/VARS
61 #define SUPERNOVA_SOUND_1_TIME 15.0f
62 #define SUPERNOVA_SOUND_2_TIME 5.0f
63 int Supernova_sound_1_played = 0;
64 int Supernova_sound_2_played = 0;
66 // countdown for supernova
67 float Supernova_time_total = -1.0f;
68 float Supernova_time = -1.0f;
69 int Supernova_finished = 0;
70 int Supernova_popup = 0;
71 float Supernova_fade_to_white = 0.0f;
72 int Supernova_particle_stamp = -1;
74 // supernova camera pos
75 vector Supernova_camera_pos;
76 matrix Supernova_camera_orient;
78 int Supernova_status = SUPERNOVA_NONE;
80 // --------------------------------------------------------------------------------------------------------------------------
81 // SUPERNOVA FUNCTIONS
85 void supernova_level_init()
87 Supernova_time_total = -1.0f;
88 Supernova_time = -1.0f;
89 Supernova_finished = 0;
90 Supernova_fade_to_white = 0.0f;
92 Supernova_particle_stamp = -1;
94 Supernova_sound_1_played = 0;
95 Supernova_sound_2_played = 0;
97 Supernova_status = SUPERNOVA_NONE;
101 void supernova_start(int seconds)
104 if((float)seconds < SUPERNOVA_CUT_TIME){
108 // no supernova in multiplayer
109 if(Game_mode & GM_MULTIPLAYER){
113 // only good if we have one sun
118 Supernova_time_total = (float)seconds;
119 Supernova_time = (float)seconds;
120 Supernova_finished = 0;
121 Supernova_fade_to_white = 0.0f;
123 Supernova_particle_stamp = -1;
125 Supernova_status = SUPERNOVA_STARTED;
128 int sn_particles = 100;
132 sn_particles = Dc_arg_int;
134 void supernova_do_particles()
138 vector norm, sun_temp;
141 if((Player_obj == NULL) || (Player_ship == NULL)){
146 if((Supernova_particle_stamp == -1) || timestamp_elapsed(Supernova_particle_stamp)){
147 Supernova_particle_stamp = timestamp(sn_particles);
150 stars_get_sun_pos(0, &sun_temp);
151 vm_vec_add2(&sun_temp, &Player_obj->pos);
152 vm_vec_sub(&norm, &Player_obj->pos, &sun_temp);
153 vm_vec_normalize(&norm);
155 particle_emitter whee;
156 whee.max_life = 1.0f;
157 whee.min_life = 0.6f;
158 whee.max_vel = 50.0f;
159 whee.min_vel = 25.0f;
160 whee.normal_variance = 0.75f;
164 whee.max_rad = 1.25f;
167 for(idx=0; idx<10; idx++){
168 submodel_get_two_random_points(Player_ship->modelnum, 0, &ta, &tb);
170 // rotate into world space
171 vm_vec_unrotate(&a, &ta, &Player_obj->orient);
172 vm_vec_add2(&a, &Player_obj->pos);
175 vm_vec_scale(&whee.vel, 30.0f);
176 vm_vec_add2(&whee.vel, &Player_obj->phys_info.vel);
178 particle_emit(&whee, PARTICLE_FIRE, (uint)-1);
180 vm_vec_unrotate(&b, &tb, &Player_obj->orient);
181 vm_vec_add2(&b, &Player_obj->pos);
183 particle_emit(&whee, PARTICLE_FIRE, (uint)-1);
188 // call once per frame
189 float sn_shudder = 0.45f;
192 dc_get_arg(ARG_FLOAT);
193 sn_shudder = Dc_arg_float;
196 void supernova_process()
200 // if the supernova is running
201 sn_stage = supernova_active();
203 Supernova_time -= flFrametime;
206 if((Supernova_time <= SUPERNOVA_SOUND_1_TIME) && !Supernova_sound_1_played){
207 Supernova_sound_1_played = 1;
208 snd_play(&Snds[SND_SUPERNOVA_1], 0.0f, 1.0f, SND_PRIORITY_MUST_PLAY);
210 if((Supernova_time <= SUPERNOVA_SOUND_2_TIME) && !Supernova_sound_2_played){
211 Supernova_sound_2_played = 1;
212 snd_play(&Snds[SND_SUPERNOVA_2], 0.0f, 1.0f, SND_PRIORITY_MUST_PLAY);
215 // if we've crossed from stage 1 to stage 2 kill all particles and stick a bunch on the player ship
216 if((sn_stage == 1) && (supernova_active() == 2)){
217 // first kill all active particles so we have a bunch of free ones
221 // if we're in stage 2, emit particles
222 if((sn_stage >= 2) && (sn_stage != 5)){
223 supernova_do_particles();
226 // if we've got negative. the supernova is done
227 if(Supernova_time < 0.0f){
228 Supernova_finished = 1;
229 Supernova_fade_to_white += flFrametime;
231 // start the dead popup
232 if(Supernova_fade_to_white >= SUPERNOVA_FADE_TO_WHITE_TIME){
233 if(!Supernova_popup){
234 // main freespace 2 campaign? if so - end it now
235 if((Game_mode & GM_CAMPAIGN_MODE) && !stricmp(Campaign.filename, "freespace2") && Campaign_ended_in_mission){
236 gameseq_post_event(GS_EVENT_END_CAMPAIGN);
242 Supernova_finished = 2;
248 // is there a supernova active
249 int supernova_active()
251 // if the supernova has "finished". fade to white and dead popup
252 if(Supernova_finished == 1){
253 Supernova_status = SUPERNOVA_HIT;
256 if(Supernova_finished == 2){
257 Supernova_status = SUPERNOVA_HIT;
262 if((Supernova_time_total <= 0.0f) || (Supernova_time <= 0.0f)){
267 if(Supernova_time < (SUPERNOVA_CUT_TIME - SUPERNOVA_CAMERA_MOVE_TIME)){
268 Supernova_status = SUPERNOVA_HIT;
273 if(Supernova_time < SUPERNOVA_CUT_TIME){
274 Supernova_status = SUPERNOVA_HIT;
282 // time left before the supernova hits
283 float supernova_time_left()
285 return Supernova_time;
288 // pct complete the supernova (0.0 to 1.0)
289 float supernova_pct_complete()
292 if(!supernova_active()){
296 return (Supernova_time_total - Supernova_time) / Supernova_time_total;
299 // if the camera should cut to the "you-are-toast" cam
300 int supernova_camera_cut()
302 // if we're not in a supernova
303 if(!supernova_active()){
307 // if we're past the critical time
308 if(Supernova_time <= SUPERNOVA_CUT_TIME){
316 // apply a shake to the orient matrix
317 void supernova_apply_shake(matrix *eye_orient, float intensity)
325 // Make eye shake due to engine wash
328 tangles.p += 0.07f * intensity * (float) (r1-RAND_MAX/2)/RAND_MAX;
329 tangles.h += 0.07f * intensity * (float) (r2-RAND_MAX/2)/RAND_MAX;
332 vm_angles_2_matrix(&tm, &tangles);
333 Assert(vm_vec_mag(&tm.fvec) > 0.0f);
334 Assert(vm_vec_mag(&tm.rvec) > 0.0f);
335 Assert(vm_vec_mag(&tm.uvec) > 0.0f);
336 vm_matrix_x_matrix(&tm2, eye_orient, &tm);
340 // get view params from supernova
341 float sn_distance = 300.0f; // shockwave moving at 1000/ms ?
342 float sn_cam_distance = 25.0f;
345 dc_get_arg(ARG_FLOAT);
346 sn_distance = Dc_arg_float;
350 dc_get_arg(ARG_FLOAT);
351 sn_cam_distance = Dc_arg_float;
353 void supernova_set_view(vector *eye_pos, matrix *eye_orient)
356 vector sun_temp, sun;
359 float cut_pct = 1.0f - (Supernova_time / SUPERNOVA_CUT_TIME);
361 // set the controls for the heart of the sun
362 stars_get_sun_pos(0, &sun_temp);
363 vm_vec_add2(&sun_temp, &Player_obj->pos);
364 vm_vec_sub(&sun, &sun_temp, &Player_obj->pos);
365 vm_vec_normalize(&sun);
367 // always set the camera pos
369 vm_vector_2_matrix(&whee, &move, NULL, NULL);
370 vm_vec_scale_add(&Supernova_camera_pos, &Player_obj->pos, &whee.rvec, sn_cam_distance);
371 vm_vec_scale_add2(&Supernova_camera_pos, &whee.uvec, 30.0f);
372 *eye_pos = Supernova_camera_pos;
374 // if we're no longer moving the camera
375 if(Supernova_time < (SUPERNOVA_CUT_TIME - SUPERNOVA_CAMERA_MOVE_TIME)){
376 // *eye_pos = Supernova_camera_pos;
377 *eye_orient = Supernova_camera_orient;
380 supernova_apply_shake(eye_orient, cut_pct * sn_shudder);
386 // get a vector somewhere between the supernova shockwave and the player ship
387 at = Player_obj->pos;
388 vm_vec_scale_add2(&at, &sun, sn_distance);
389 vm_vec_sub(&move, &Player_obj->pos, &at);
390 vm_vec_normalize(&move);
392 // linearly move towards the player pos
393 float pct = ((SUPERNOVA_CUT_TIME - Supernova_time) / SUPERNOVA_CAMERA_MOVE_TIME);
394 vm_vec_scale_add2(&at, &move, sn_distance * pct);
396 *eye_pos = Supernova_camera_pos;
397 vm_vec_sub(&view, &at, eye_pos);
398 vm_vec_normalize(&view);
399 vm_vector_2_matrix(&Supernova_camera_orient, &view, NULL, NULL);
400 *eye_orient = Supernova_camera_orient;
404 supernova_apply_shake(eye_orient, cut_pct * sn_shudder);