1 /* $Id: cntrlcen.c,v 1.9 2003-04-03 07:15:43 btb Exp $ */
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.
20 static char rcsid[] = "$Id: cntrlcen.c,v 1.9 2003-04-03 07:15:43 btb Exp $";
51 //@@vms_vector controlcen_gun_points[MAX_CONTROLCEN_GUNS];
52 //@@vms_vector controlcen_gun_dirs[MAX_CONTROLCEN_GUNS];
54 reactor Reactors[MAX_REACTORS];
57 control_center_triggers ControlCenterTriggers;
59 int N_controlcen_guns;
60 int Control_center_been_hit;
61 int Control_center_player_been_seen;
62 int Control_center_next_fire_time;
63 int Control_center_present;
65 vms_vector Gun_pos[MAX_CONTROLCEN_GUNS], Gun_dir[MAX_CONTROLCEN_GUNS];
67 void do_countdown_frame();
69 // -----------------------------------------------------------------------------
70 //return the position & orientation of a gun on the control center object
71 void calc_controlcen_gun_point(vms_vector *gun_point,vms_vector *gun_dir,object *obj,int gun_num)
76 Assert(obj->type == OBJ_CNTRLCEN);
77 Assert(obj->render_type==RT_POLYOBJ);
79 reactor = &Reactors[obj->id];
81 Assert(gun_num < reactor->n_guns);
83 //instance gun position & orientation
85 vm_copy_transpose_matrix(&m,&obj->orient);
87 vm_vec_rotate(gun_point,&reactor->gun_points[gun_num],&m);
88 vm_vec_add2(gun_point,&obj->pos);
89 vm_vec_rotate(gun_dir,&reactor->gun_dirs[gun_num],&m);
92 // -----------------------------------------------------------------------------
93 // Look at control center guns, find best one to fire at *objp.
94 // Return best gun number (one whose direction dotted with vector to player is largest).
95 // If best gun has negative dot, return -1, meaning no gun is good.
96 int calc_best_gun(int num_guns, vms_vector *gun_pos, vms_vector *gun_dir, vms_vector *objpos)
105 for (i=0; i<num_guns; i++) {
109 vm_vec_sub(&gun_vec, objpos, &gun_pos[i]);
110 vm_vec_normalize_quick(&gun_vec);
111 dot = vm_vec_dot(&gun_dir[i], &gun_vec);
113 if (dot > best_dot) {
119 Assert(best_gun != -1); // Contact Mike. This is impossible. Or maybe you're getting an unnormalized vector somewhere.
128 extern fix Player_time_of_death; // object.c
130 int Dead_controlcen_object_num=-1;
132 //how long to blow up on insane
133 int Base_control_center_explosion_time=DEFAULT_CONTROL_CENTER_EXPLOSION_TIME;
135 int Control_center_destroyed = 0;
136 fix Countdown_timer=0;
137 int Countdown_seconds_left=0, Total_countdown_time=0; //in whole seconds
139 int Alan_pavlish_reactor_times[NDL] = {90, 60, 45, 35, 30};
141 // -----------------------------------------------------------------------------
142 // Called every frame. If control center been destroyed, then actually do something.
143 void do_controlcen_dead_frame(void)
145 if ((Dead_controlcen_object_num != -1) && (Countdown_seconds_left > 0))
146 if (d_rand() < FrameTime*4)
147 create_small_fireball_on_object(&Objects[Dead_controlcen_object_num], F1_0, 1);
149 if (Control_center_destroyed && !Endlevel_sequence)
150 do_countdown_frame();
153 #define COUNTDOWN_VOICE_TIME fl2f(12.75)
155 void do_countdown_frame()
160 if (!Control_center_destroyed) return;
162 #if !defined(D2_OEM) && !defined(SHAREWARE) // get countdown in OEM and SHAREWARE only
163 // On last level, we don't want a countdown.
164 if ((Current_mission_num == Builtin_mission_num) && (Current_level_num == Last_level))
166 if (!(Game_mode & GM_MULTI))
168 if (Game_mode & GM_MULTI_ROBOTS)
173 // Control center destroyed, rock the player's ship.
174 fc = Countdown_seconds_left;
178 // At Trainee, decrease rocking of ship by 4x.
180 if (Difficulty_level == 0)
183 ConsoleObject->mtype.phys_info.rotvel.x += (fixmul(d_rand() - 16384, 3*F1_0/16 + (F1_0*(16-fc))/32))/div_scale;
184 ConsoleObject->mtype.phys_info.rotvel.z += (fixmul(d_rand() - 16384, 3*F1_0/16 + (F1_0*(16-fc))/32))/div_scale;
185 // Hook in the rumble sound effect here.
187 old_time = Countdown_timer;
188 Countdown_timer -= RealFrameTime;
189 Countdown_seconds_left = f2i(Countdown_timer + F1_0*7/8);
191 if ( (old_time > COUNTDOWN_VOICE_TIME ) && (Countdown_timer <= COUNTDOWN_VOICE_TIME) ) {
192 digi_play_sample( SOUND_COUNTDOWN_13_SECS, F3_0 );
194 if ( f2i(old_time + F1_0*7/8) != Countdown_seconds_left ) {
195 if ( (Countdown_seconds_left>=0) && (Countdown_seconds_left<10) )
196 digi_play_sample( SOUND_COUNTDOWN_0_SECS+Countdown_seconds_left, F3_0 );
197 if ( Countdown_seconds_left==Total_countdown_time-1)
198 digi_play_sample( SOUND_COUNTDOWN_29_SECS, F3_0 );
201 if (Countdown_timer > 0) {
203 size = (i2f(Total_countdown_time)-Countdown_timer) / fl2f(0.65);
204 old_size = (i2f(Total_countdown_time)-old_time) / fl2f(0.65);
205 if (size != old_size && (Countdown_seconds_left < (Total_countdown_time-5) )) { // Every 2 seconds!
206 //@@if (Dead_controlcen_object_num != -1) {
207 //@@ vms_vector vp; //,v,c;
208 //@@ compute_segment_center(&vp, &Segments[Objects[Dead_controlcen_object_num].segnum]);
209 //@@ object_create_explosion( Objects[Dead_controlcen_object_num].segnum, &vp, size*10, VCLIP_SMALL_EXPLOSION);
212 digi_play_sample( SOUND_CONTROL_CENTER_WARNING_SIREN, F3_0 );
218 digi_play_sample( SOUND_MINE_BLEW_UP, F1_0 );
220 flash_value = f2i(-Countdown_timer * (64 / 4)); // 4 seconds to total whiteness
221 PALETTE_FLASH_SET(flash_value,flash_value,flash_value);
223 if (PaletteBlueAdd > 64 ) {
225 dd_gr_set_current_canvas(NULL),
226 gr_set_current_canvas( NULL )
229 dd_gr_clear_canvas(BM_XRGB(31,31,31)),
230 gr_clear_canvas(BM_XRGB(31,31,31))
231 ); //make screen all white to match palette effect
232 reset_cockpit(); //force cockpit redraw next time
233 reset_palette_add(); //restore palette for death message
234 //controlcen->MaxCapacity = Fuelcen_max_amount;
235 //gauge_message( "Control Center Reset" );
236 DoPlayerDead(); //kill_player();
241 // -----------------------------------------------------------------------------
242 // Called when control center gets destroyed.
243 // This code is common to whether control center is implicitly imbedded in a boss,
244 // or is an object of its own.
245 // if objp == NULL that means the boss was the control center and don't set Dead_controlcen_object_num
246 void do_controlcen_destroyed_stuff(object *objp)
250 if ((Game_mode & GM_MULTI_ROBOTS) && Control_center_destroyed)
251 return; // Don't allow resetting if control center and boss on same level
253 // Must toggle walls whether it is a boss or control center.
254 for (i=0;i<ControlCenterTriggers.num_links;i++)
255 wall_toggle(&Segments[ControlCenterTriggers.seg[i]], ControlCenterTriggers.side[i]);
257 // And start the countdown stuff.
258 Control_center_destroyed = 1;
260 // If a secret level, delete secret.sgc to indicate that we can't return to our secret level.
261 if (Current_level_num < 0) {
264 rval = unlink("secret.sgc");
266 rval = unlink(":Players:secret.sgc");
268 mprintf((0, "Deleting secret.sgc, return value = %i\n", rval));
271 if (Base_control_center_explosion_time != DEFAULT_CONTROL_CENTER_EXPLOSION_TIME)
272 Total_countdown_time = Base_control_center_explosion_time + Base_control_center_explosion_time * (NDL-Difficulty_level-1)/2;
274 Total_countdown_time = Alan_pavlish_reactor_times[Difficulty_level];
276 Countdown_timer = i2f(Total_countdown_time);
278 if (!Control_center_present || objp==NULL) {
279 //Assert(objp == NULL);
283 //Assert(objp != NULL);
285 Dead_controlcen_object_num = objp-Objects;
288 int Last_time_cc_vis_check = 0;
290 // -----------------------------------------------------------------------------
291 //do whatever this thing does in a frame
292 void do_controlcen_frame(object *obj)
296 // If a boss level, then Control_center_present will be 0.
297 if (!Control_center_present)
301 if (!Robot_firing_enabled || (Game_suspended & SUSP_ROBOTS))
304 if (!Robot_firing_enabled)
308 if (!(Control_center_been_hit || Control_center_player_been_seen)) {
309 if (!(FrameCount % 8)) { // Do every so often...
310 vms_vector vec_to_player;
313 segment *segp = &Segments[obj->segnum];
315 // This is a hack. Since the control center is not processed by
316 // ai_do_frame, it doesn't know to deal with cloaked dudes. It
317 // seems to work in single-player mode because it is actually using
318 // the value of Believed_player_position that was set by the last
319 // person to go through ai_do_frame. But since a no-robots game
320 // never goes through ai_do_frame, I'm making it so the control
321 // center can spot cloaked dudes.
323 if (Game_mode & GM_MULTI)
324 Believed_player_pos = Objects[Players[Player_num].objnum].pos;
326 // Hack for special control centers which are isolated and not reachable because the
327 // real control center is inside the boss.
328 for (i=0; i<MAX_SIDES_PER_SEGMENT; i++)
329 if (IS_CHILD(segp->children[i]))
331 if (i == MAX_SIDES_PER_SEGMENT)
334 vm_vec_sub(&vec_to_player, &ConsoleObject->pos, &obj->pos);
335 dist_to_player = vm_vec_normalize_quick(&vec_to_player);
336 if (dist_to_player < F1_0*200) {
337 Control_center_player_been_seen = player_is_visible_from_object(obj, &obj->pos, 0, &vec_to_player);
338 Control_center_next_fire_time = 0;
345 // Periodically, make the reactor fall asleep if player not visible.
346 if (Control_center_been_hit || Control_center_player_been_seen) {
347 if ((Last_time_cc_vis_check + F1_0*5 < GameTime) || (Last_time_cc_vis_check > GameTime)) {
348 vms_vector vec_to_player;
351 vm_vec_sub(&vec_to_player, &ConsoleObject->pos, &obj->pos);
352 dist_to_player = vm_vec_normalize_quick(&vec_to_player);
353 Last_time_cc_vis_check = GameTime;
354 if (dist_to_player < F1_0*120) {
355 Control_center_player_been_seen = player_is_visible_from_object(obj, &obj->pos, 0, &vec_to_player);
356 if (!Control_center_player_been_seen)
357 Control_center_been_hit = 0;
363 if ((Control_center_next_fire_time < 0) && !(Player_is_dead && (GameTime > Player_time_of_death+F1_0*2))) {
364 if (Players[Player_num].flags & PLAYER_FLAGS_CLOAKED)
365 best_gun_num = calc_best_gun(N_controlcen_guns, Gun_pos, Gun_dir, &Believed_player_pos);
367 best_gun_num = calc_best_gun(N_controlcen_guns, Gun_pos, Gun_dir, &ConsoleObject->pos);
369 if (best_gun_num != -1) {
370 int rand_prob, count;
371 vms_vector vec_to_goal;
375 if (Players[Player_num].flags & PLAYER_FLAGS_CLOAKED) {
376 vm_vec_sub(&vec_to_goal, &Believed_player_pos, &Gun_pos[best_gun_num]);
377 dist_to_player = vm_vec_normalize_quick(&vec_to_goal);
379 vm_vec_sub(&vec_to_goal, &ConsoleObject->pos, &Gun_pos[best_gun_num]);
380 dist_to_player = vm_vec_normalize_quick(&vec_to_goal);
383 if (dist_to_player > F1_0*300)
385 Control_center_been_hit = 0;
386 Control_center_player_been_seen = 0;
391 if (Game_mode & GM_MULTI)
392 multi_send_controlcen_fire(&vec_to_goal, best_gun_num, obj-Objects);
394 Laser_create_new_easy( &vec_to_goal, &Gun_pos[best_gun_num], obj-Objects, CONTROLCEN_WEAPON_NUM, 1);
396 // some of time, based on level, fire another thing, not directly at player, so it might hit him if he's constantly moving.
397 rand_prob = F1_0/(abs(Current_level_num)/4+2);
399 while ((d_rand() > rand_prob) && (count < 4)) {
402 make_random_vector(&randvec);
403 vm_vec_scale_add2(&vec_to_goal, &randvec, F1_0/6);
404 vm_vec_normalize_quick(&vec_to_goal);
406 if (Game_mode & GM_MULTI)
407 multi_send_controlcen_fire(&vec_to_goal, best_gun_num, obj-Objects);
409 Laser_create_new_easy( &vec_to_goal, &Gun_pos[best_gun_num], obj-Objects, CONTROLCEN_WEAPON_NUM, 0);
413 delta_fire_time = (NDL - Difficulty_level) * F1_0/4;
414 if (Difficulty_level == 0)
415 delta_fire_time += F1_0/2;
417 if (Game_mode & GM_MULTI) // slow down rate of fire in multi player
418 delta_fire_time *= 2;
420 Control_center_next_fire_time = delta_fire_time;
424 Control_center_next_fire_time -= FrameTime;
428 int Reactor_strength=-1; //-1 mean not set by designer
430 // -----------------------------------------------------------------------------
431 // This must be called at the start of each level.
432 // If this level contains a boss and mode != multiplayer, don't do control center stuff. (Ghost out control center object.)
433 // If this level contains a boss and mode == multiplayer, do control center stuff.
434 void init_controlcen_for_level(void)
438 int cntrlcen_objnum=-1, boss_objnum=-1;
440 for (i=0; i<=Highest_object_index; i++) {
442 if (objp->type == OBJ_CNTRLCEN)
444 if (cntrlcen_objnum != -1)
445 mprintf((1, "Warning: Two or more control centers including %i and %i\n", i, cntrlcen_objnum));
450 if ((objp->type == OBJ_ROBOT) && (Robot_info[objp->id].boss_flag)) {
451 // mprintf((0, "Found boss robot %d.\n", objp->id));
452 if (boss_objnum != -1)
453 mprintf((1, "Warning: Two or more bosses including %i and %i\n", i, boss_objnum));
460 if (cntrlcen_objnum == -1) {
461 mprintf((1, "Warning: No control center.\n"));
466 if ( (boss_objnum != -1) && !((Game_mode & GM_MULTI) && !(Game_mode & GM_MULTI_ROBOTS)) ) {
467 if (cntrlcen_objnum != -1) {
468 // mprintf((0, "Ghosting control center\n"));
469 Objects[cntrlcen_objnum].type = OBJ_GHOST;
470 Objects[cntrlcen_objnum].render_type = RT_NONE;
471 Control_center_present = 0;
474 // Compute all gun positions.
475 objp = &Objects[cntrlcen_objnum];
476 N_controlcen_guns = Reactors[objp->id].n_guns;
477 for (i=0; i<N_controlcen_guns; i++)
478 calc_controlcen_gun_point(&Gun_pos[i], &Gun_dir[i], objp, i);
479 Control_center_present = 1;
481 if (Reactor_strength == -1) { //use old defaults
482 // Boost control center strength at higher levels.
483 if (Current_level_num >= 0)
484 objp->shields = F1_0*200 + (F1_0*200/4) * Current_level_num;
486 objp->shields = F1_0*200 - Current_level_num*F1_0*150;
489 objp->shields = i2f(Reactor_strength);
494 // Say the control center has not yet been hit.
495 Control_center_been_hit = 0;
496 Control_center_player_been_seen = 0;
497 Control_center_next_fire_time = 0;
499 Dead_controlcen_object_num = -1;
502 void special_reactor_stuff(void)
504 mprintf((0, "Mucking with reactor countdown time.\n"));
505 if (Control_center_destroyed) {
506 Countdown_timer += i2f(Base_control_center_explosion_time + (NDL-1-Difficulty_level)*Base_control_center_explosion_time/(NDL-1));
507 Total_countdown_time = f2i(Countdown_timer)+2; // Will prevent "Self destruct sequence activated" message from replaying.
513 * reads n reactor structs from a CFILE
515 extern int reactor_read_n(reactor *r, int n, CFILE *fp)
519 for (i = 0; i < n; i++) {
520 r[i].model_num = cfile_read_int(fp);
521 r[i].n_guns = cfile_read_int(fp);
522 for (j = 0; j < MAX_CONTROLCEN_GUNS; j++)
523 cfile_read_vector(&(r[i].gun_points[j]), fp);
524 for (j = 0; j < MAX_CONTROLCEN_GUNS; j++)
525 cfile_read_vector(&(r[i].gun_dirs[j]), fp);
531 * reads a control_center_triggers structure from a CFILE
533 extern int control_center_triggers_read_n(control_center_triggers *cct, int n, CFILE *fp)
537 for (i = 0; i < n; i++)
539 cct->num_links = cfile_read_short(fp);
540 for (j = 0; j < MAX_CONTROLCEN_LINKS; j++)
541 cct->seg[j] = cfile_read_short(fp);
542 for (j = 0; j < MAX_CONTROLCEN_LINKS; j++)
543 cct->side[j] = cfile_read_short(fp);