2 * $Logfile: /Freespace2/code/Starfield/Supernova.cpp $
7 * Include file for nebula stuff
10 * Revision 1.1 2002/05/03 03:28:10 root
14 * 5 9/09/99 11:40p Dave
15 * Handle an Assert() in beam code. Added supernova sounds. Play the right
16 * 2 end movies properly, based upon what the player did in the mission.
18 * 4 9/03/99 1:32a Dave
19 * CD checking by act. Added support to play 2 cutscenes in a row
20 * seamlessly. Fixed super low level cfile bug related to files in the
21 * root directory of a CD. Added cheat code to set campaign mission # in
24 * 3 7/31/99 4:15p Dave
25 * Fixed supernova particle velocities. Handle OBJ_NONE in target
26 * monitoring view. Properly use objectives notify gauge colors.
28 * 2 7/21/99 8:10p Dave
29 * First run of supernova effect.
35 #include "freespace.h"
39 #include "starfield.h"
40 #include "supernova.h"
45 #include "popupdead.h"
47 #include "missioncampaign.h"
48 #include "gamesequence.h"
53 // --------------------------------------------------------------------------------------------------------------------------
54 // SUPERNOVA DEFINES/VARS
58 #define SUPERNOVA_SOUND_1_TIME 15.0f
59 #define SUPERNOVA_SOUND_2_TIME 5.0f
60 int Supernova_sound_1_played = 0;
61 int Supernova_sound_2_played = 0;
63 // countdown for supernova
64 float Supernova_time_total = -1.0f;
65 float Supernova_time = -1.0f;
66 int Supernova_finished = 0;
67 int Supernova_popup = 0;
68 float Supernova_fade_to_white = 0.0f;
69 int Supernova_particle_stamp = -1;
71 // supernova camera pos
72 vector Supernova_camera_pos;
73 matrix Supernova_camera_orient;
75 int Supernova_status = SUPERNOVA_NONE;
77 // --------------------------------------------------------------------------------------------------------------------------
78 // SUPERNOVA FUNCTIONS
82 void supernova_level_init()
84 Supernova_time_total = -1.0f;
85 Supernova_time = -1.0f;
86 Supernova_finished = 0;
87 Supernova_fade_to_white = 0.0f;
89 Supernova_particle_stamp = -1;
91 Supernova_sound_1_played = 0;
92 Supernova_sound_2_played = 0;
94 Supernova_status = SUPERNOVA_NONE;
98 void supernova_start(int seconds)
101 if((float)seconds < SUPERNOVA_CUT_TIME){
105 // no supernova in multiplayer
106 if(Game_mode & GM_MULTIPLAYER){
110 // only good if we have one sun
115 Supernova_time_total = (float)seconds;
116 Supernova_time = (float)seconds;
117 Supernova_finished = 0;
118 Supernova_fade_to_white = 0.0f;
120 Supernova_particle_stamp = -1;
122 Supernova_status = SUPERNOVA_STARTED;
125 int sn_particles = 100;
129 sn_particles = Dc_arg_int;
131 void supernova_do_particles()
135 vector norm, sun_temp;
138 if((Player_obj == NULL) || (Player_ship == NULL)){
143 if((Supernova_particle_stamp == -1) || timestamp_elapsed(Supernova_particle_stamp)){
144 Supernova_particle_stamp = timestamp(sn_particles);
147 stars_get_sun_pos(0, &sun_temp);
148 vm_vec_add2(&sun_temp, &Player_obj->pos);
149 vm_vec_sub(&norm, &Player_obj->pos, &sun_temp);
150 vm_vec_normalize(&norm);
152 particle_emitter whee;
153 whee.max_life = 1.0f;
154 whee.min_life = 0.6f;
155 whee.max_vel = 50.0f;
156 whee.min_vel = 25.0f;
157 whee.normal_variance = 0.75f;
161 whee.max_rad = 1.25f;
164 for(idx=0; idx<10; idx++){
165 submodel_get_two_random_points(Player_ship->modelnum, 0, &ta, &tb);
167 // rotate into world space
168 vm_vec_unrotate(&a, &ta, &Player_obj->orient);
169 vm_vec_add2(&a, &Player_obj->pos);
172 vm_vec_scale(&whee.vel, 30.0f);
173 vm_vec_add2(&whee.vel, &Player_obj->phys_info.vel);
175 particle_emit(&whee, PARTICLE_FIRE, (uint)-1);
177 vm_vec_unrotate(&b, &tb, &Player_obj->orient);
178 vm_vec_add2(&b, &Player_obj->pos);
180 particle_emit(&whee, PARTICLE_FIRE, (uint)-1);
185 // call once per frame
186 float sn_shudder = 0.45f;
189 dc_get_arg(ARG_FLOAT);
190 sn_shudder = Dc_arg_float;
193 void supernova_process()
197 // if the supernova is running
198 sn_stage = supernova_active();
200 Supernova_time -= flFrametime;
203 if((Supernova_time <= SUPERNOVA_SOUND_1_TIME) && !Supernova_sound_1_played){
204 Supernova_sound_1_played = 1;
205 snd_play(&Snds[SND_SUPERNOVA_1], 0.0f, 1.0f, SND_PRIORITY_MUST_PLAY);
207 if((Supernova_time <= SUPERNOVA_SOUND_2_TIME) && !Supernova_sound_2_played){
208 Supernova_sound_2_played = 1;
209 snd_play(&Snds[SND_SUPERNOVA_2], 0.0f, 1.0f, SND_PRIORITY_MUST_PLAY);
212 // if we've crossed from stage 1 to stage 2 kill all particles and stick a bunch on the player ship
213 if((sn_stage == 1) && (supernova_active() == 2)){
214 // first kill all active particles so we have a bunch of free ones
218 // if we're in stage 2, emit particles
219 if((sn_stage >= 2) && (sn_stage != 5)){
220 supernova_do_particles();
223 // if we've got negative. the supernova is done
224 if(Supernova_time < 0.0f){
225 Supernova_finished = 1;
226 Supernova_fade_to_white += flFrametime;
228 // start the dead popup
229 if(Supernova_fade_to_white >= SUPERNOVA_FADE_TO_WHITE_TIME){
230 if(!Supernova_popup){
231 // main freespace 2 campaign? if so - end it now
232 if((Game_mode & GM_CAMPAIGN_MODE) && !stricmp(Campaign.filename, "freespace2") && Campaign_ended_in_mission){
233 gameseq_post_event(GS_EVENT_END_CAMPAIGN);
239 Supernova_finished = 2;
245 // is there a supernova active
246 int supernova_active()
248 // if the supernova has "finished". fade to white and dead popup
249 if(Supernova_finished == 1){
250 Supernova_status = SUPERNOVA_HIT;
253 if(Supernova_finished == 2){
254 Supernova_status = SUPERNOVA_HIT;
259 if((Supernova_time_total <= 0.0f) || (Supernova_time <= 0.0f)){
264 if(Supernova_time < (SUPERNOVA_CUT_TIME - SUPERNOVA_CAMERA_MOVE_TIME)){
265 Supernova_status = SUPERNOVA_HIT;
270 if(Supernova_time < SUPERNOVA_CUT_TIME){
271 Supernova_status = SUPERNOVA_HIT;
279 // time left before the supernova hits
280 float supernova_time_left()
282 return Supernova_time;
285 // pct complete the supernova (0.0 to 1.0)
286 float supernova_pct_complete()
289 if(!supernova_active()){
293 return (Supernova_time_total - Supernova_time) / Supernova_time_total;
296 // if the camera should cut to the "you-are-toast" cam
297 int supernova_camera_cut()
299 // if we're not in a supernova
300 if(!supernova_active()){
304 // if we're past the critical time
305 if(Supernova_time <= SUPERNOVA_CUT_TIME){
313 // apply a shake to the orient matrix
314 void supernova_apply_shake(matrix *eye_orient, float intensity)
322 // Make eye shake due to engine wash
325 tangles.p += 0.07f * intensity * (float) (r1-RAND_MAX/2)/RAND_MAX;
326 tangles.h += 0.07f * intensity * (float) (r2-RAND_MAX/2)/RAND_MAX;
329 vm_angles_2_matrix(&tm, &tangles);
330 Assert(vm_vec_mag(&tm.fvec) > 0.0f);
331 Assert(vm_vec_mag(&tm.rvec) > 0.0f);
332 Assert(vm_vec_mag(&tm.uvec) > 0.0f);
333 vm_matrix_x_matrix(&tm2, eye_orient, &tm);
337 // get view params from supernova
338 float sn_distance = 300.0f; // shockwave moving at 1000/ms ?
339 float sn_cam_distance = 25.0f;
342 dc_get_arg(ARG_FLOAT);
343 sn_distance = Dc_arg_float;
347 dc_get_arg(ARG_FLOAT);
348 sn_cam_distance = Dc_arg_float;
350 void supernova_set_view(vector *eye_pos, matrix *eye_orient)
353 vector sun_temp, sun;
356 float cut_pct = 1.0f - (Supernova_time / SUPERNOVA_CUT_TIME);
358 // set the controls for the heart of the sun
359 stars_get_sun_pos(0, &sun_temp);
360 vm_vec_add2(&sun_temp, &Player_obj->pos);
361 vm_vec_sub(&sun, &sun_temp, &Player_obj->pos);
362 vm_vec_normalize(&sun);
364 // always set the camera pos
366 vm_vector_2_matrix(&whee, &move, NULL, NULL);
367 vm_vec_scale_add(&Supernova_camera_pos, &Player_obj->pos, &whee.rvec, sn_cam_distance);
368 vm_vec_scale_add2(&Supernova_camera_pos, &whee.uvec, 30.0f);
369 *eye_pos = Supernova_camera_pos;
371 // if we're no longer moving the camera
372 if(Supernova_time < (SUPERNOVA_CUT_TIME - SUPERNOVA_CAMERA_MOVE_TIME)){
373 // *eye_pos = Supernova_camera_pos;
374 *eye_orient = Supernova_camera_orient;
377 supernova_apply_shake(eye_orient, cut_pct * sn_shudder);
383 // get a vector somewhere between the supernova shockwave and the player ship
384 at = Player_obj->pos;
385 vm_vec_scale_add2(&at, &sun, sn_distance);
386 vm_vec_sub(&move, &Player_obj->pos, &at);
387 vm_vec_normalize(&move);
389 // linearly move towards the player pos
390 float pct = ((SUPERNOVA_CUT_TIME - Supernova_time) / SUPERNOVA_CAMERA_MOVE_TIME);
391 vm_vec_scale_add2(&at, &move, sn_distance * pct);
393 *eye_pos = Supernova_camera_pos;
394 vm_vec_sub(&view, &at, eye_pos);
395 vm_vec_normalize(&view);
396 vm_vector_2_matrix(&Supernova_camera_orient, &view, NULL, NULL);
397 *eye_orient = Supernova_camera_orient;
401 supernova_apply_shake(eye_orient, cut_pct * sn_shudder);