1 /* $Id: cntrlcen.c,v 1.20 2005-08-02 06:13:56 chris 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.
17 * Code for the control center
26 static char rcsid[] = "$Id: cntrlcen.c,v 1.20 2005-08-02 06:13:56 chris Exp $";
31 #if !defined(_WIN32) && !defined(macintosh)
56 //@@vms_vector controlcen_gun_points[MAX_CONTROLCEN_GUNS];
57 //@@vms_vector controlcen_gun_dirs[MAX_CONTROLCEN_GUNS];
59 reactor Reactors[MAX_REACTORS];
62 control_center_triggers ControlCenterTriggers;
64 int N_controlcen_guns;
65 int Control_center_been_hit;
66 int Control_center_player_been_seen;
67 int Control_center_next_fire_time;
68 int Control_center_present;
70 vms_vector Gun_pos[MAX_CONTROLCEN_GUNS], Gun_dir[MAX_CONTROLCEN_GUNS];
72 void do_countdown_frame();
74 // -----------------------------------------------------------------------------
75 //return the position & orientation of a gun on the control center object
76 void calc_controlcen_gun_point(vms_vector *gun_point,vms_vector *gun_dir,object *obj,int gun_num)
81 Assert(obj->type == OBJ_CNTRLCEN);
82 Assert(obj->render_type==RT_POLYOBJ);
84 reactor = &Reactors[obj->id];
86 Assert(gun_num < reactor->n_guns);
88 //instance gun position & orientation
90 vm_copy_transpose_matrix(&m,&obj->orient);
92 vm_vec_rotate(gun_point,&reactor->gun_points[gun_num],&m);
93 vm_vec_add2(gun_point,&obj->pos);
94 vm_vec_rotate(gun_dir,&reactor->gun_dirs[gun_num],&m);
97 // -----------------------------------------------------------------------------
98 // Look at control center guns, find best one to fire at *objp.
99 // Return best gun number (one whose direction dotted with vector to player is largest).
100 // If best gun has negative dot, return -1, meaning no gun is good.
101 int calc_best_gun(int num_guns, vms_vector *gun_pos, vms_vector *gun_dir, vms_vector *objpos)
110 for (i=0; i<num_guns; i++) {
114 vm_vec_sub(&gun_vec, objpos, &gun_pos[i]);
115 vm_vec_normalize_quick(&gun_vec);
116 dot = vm_vec_dot(&gun_dir[i], &gun_vec);
118 if (dot > best_dot) {
124 Assert(best_gun != -1); // Contact Mike. This is impossible. Or maybe you're getting an unnormalized vector somewhere.
133 extern fix Player_time_of_death; // object.c
135 int Dead_controlcen_object_num=-1;
137 //how long to blow up on insane
138 int Base_control_center_explosion_time=DEFAULT_CONTROL_CENTER_EXPLOSION_TIME;
140 int Control_center_destroyed = 0;
141 fix Countdown_timer=0;
142 int Countdown_seconds_left=0, Total_countdown_time=0; //in whole seconds
144 int Alan_pavlish_reactor_times[NDL] = {90, 60, 45, 35, 30};
146 // -----------------------------------------------------------------------------
147 // Called every frame. If control center been destroyed, then actually do something.
148 void do_controlcen_dead_frame(void)
150 if ((Dead_controlcen_object_num != -1) && (Countdown_seconds_left > 0))
151 if (d_rand() < FrameTime*4)
152 create_small_fireball_on_object(&Objects[Dead_controlcen_object_num], F1_0, 1);
154 if (Control_center_destroyed && !Endlevel_sequence)
155 do_countdown_frame();
158 #define COUNTDOWN_VOICE_TIME fl2f(12.75)
160 void do_countdown_frame()
165 if (!Control_center_destroyed) return;
167 if (!is_D2_OEM && !is_MAC_SHARE && !is_SHAREWARE) // get countdown in OEM and SHAREWARE only
169 // On last level, we don't want a countdown.
170 if (PLAYING_BUILTIN_MISSION && Current_level_num == Last_level)
172 if (!(Game_mode & GM_MULTI))
174 if (Game_mode & GM_MULTI_ROBOTS)
179 // Control center destroyed, rock the player's ship.
180 fc = Countdown_seconds_left;
184 // At Trainee, decrease rocking of ship by 4x.
186 if (Difficulty_level == 0)
189 ConsoleObject->mtype.phys_info.rotvel.x += (fixmul(d_rand() - 16384, 3*F1_0/16 + (F1_0*(16-fc))/32))/div_scale;
190 ConsoleObject->mtype.phys_info.rotvel.z += (fixmul(d_rand() - 16384, 3*F1_0/16 + (F1_0*(16-fc))/32))/div_scale;
191 // Hook in the rumble sound effect here.
193 old_time = Countdown_timer;
194 Countdown_timer -= RealFrameTime;
195 Countdown_seconds_left = f2i(Countdown_timer + F1_0*7/8);
197 if ( (old_time > COUNTDOWN_VOICE_TIME ) && (Countdown_timer <= COUNTDOWN_VOICE_TIME) ) {
198 digi_play_sample( SOUND_COUNTDOWN_13_SECS, F3_0 );
200 if ( f2i(old_time + F1_0*7/8) != Countdown_seconds_left ) {
201 if ( (Countdown_seconds_left>=0) && (Countdown_seconds_left<10) )
202 digi_play_sample( SOUND_COUNTDOWN_0_SECS+Countdown_seconds_left, F3_0 );
203 if ( Countdown_seconds_left==Total_countdown_time-1)
204 digi_play_sample( SOUND_COUNTDOWN_29_SECS, F3_0 );
207 if (Countdown_timer > 0) {
209 size = (i2f(Total_countdown_time)-Countdown_timer) / fl2f(0.65);
210 old_size = (i2f(Total_countdown_time)-old_time) / fl2f(0.65);
211 if (size != old_size && (Countdown_seconds_left < (Total_countdown_time-5) )) { // Every 2 seconds!
212 //@@if (Dead_controlcen_object_num != -1) {
213 //@@ vms_vector vp; //,v,c;
214 //@@ compute_segment_center(&vp, &Segments[Objects[Dead_controlcen_object_num].segnum]);
215 //@@ object_create_explosion( Objects[Dead_controlcen_object_num].segnum, &vp, size*10, VCLIP_SMALL_EXPLOSION);
218 digi_play_sample( SOUND_CONTROL_CENTER_WARNING_SIREN, F3_0 );
224 digi_play_sample( SOUND_MINE_BLEW_UP, F1_0 );
226 flash_value = f2i(-Countdown_timer * (64 / 4)); // 4 seconds to total whiteness
227 PALETTE_FLASH_SET(flash_value,flash_value,flash_value);
229 if (PaletteBlueAdd > 64 ) {
230 gr_set_current_canvas( NULL );
231 gr_clear_canvas(BM_XRGB(31,31,31)); //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) {
263 rval = !PHYSFS_delete(PLAYER_DIR "secret.sgc");
264 mprintf((0, "Deleting secret.sgc, return value = %i\n", rval));
267 if (Base_control_center_explosion_time != DEFAULT_CONTROL_CENTER_EXPLOSION_TIME)
268 Total_countdown_time = Base_control_center_explosion_time + Base_control_center_explosion_time * (NDL-Difficulty_level-1)/2;
270 Total_countdown_time = Alan_pavlish_reactor_times[Difficulty_level];
272 Countdown_timer = i2f(Total_countdown_time);
274 if (!Control_center_present || objp==NULL) {
275 //Assert(objp == NULL);
279 //Assert(objp != NULL);
281 Dead_controlcen_object_num = OBJECT_NUMBER(objp);
284 int Last_time_cc_vis_check = 0;
286 // -----------------------------------------------------------------------------
287 //do whatever this thing does in a frame
288 void do_controlcen_frame(object *obj)
292 // If a boss level, then Control_center_present will be 0.
293 if (!Control_center_present)
297 if (!Robot_firing_enabled || (Game_suspended & SUSP_ROBOTS))
300 if (!Robot_firing_enabled)
304 if (!(Control_center_been_hit || Control_center_player_been_seen)) {
305 if (!(FrameCount % 8)) { // Do every so often...
306 vms_vector vec_to_player;
309 segment *segp = &Segments[obj->segnum];
311 // This is a hack. Since the control center is not processed by
312 // ai_do_frame, it doesn't know to deal with cloaked dudes. It
313 // seems to work in single-player mode because it is actually using
314 // the value of Believed_player_position that was set by the last
315 // person to go through ai_do_frame. But since a no-robots game
316 // never goes through ai_do_frame, I'm making it so the control
317 // center can spot cloaked dudes.
319 if (Game_mode & GM_MULTI)
320 Believed_player_pos = Objects[Players[Player_num].objnum].pos;
322 // Hack for special control centers which are isolated and not reachable because the
323 // real control center is inside the boss.
324 for (i=0; i<MAX_SIDES_PER_SEGMENT; i++)
325 if (IS_CHILD(segp->children[i]))
327 if (i == MAX_SIDES_PER_SEGMENT)
330 vm_vec_sub(&vec_to_player, &ConsoleObject->pos, &obj->pos);
331 dist_to_player = vm_vec_normalize_quick(&vec_to_player);
332 if (dist_to_player < F1_0*200) {
333 Control_center_player_been_seen = player_is_visible_from_object(obj, &obj->pos, 0, &vec_to_player);
334 Control_center_next_fire_time = 0;
341 // Periodically, make the reactor fall asleep if player not visible.
342 if (Control_center_been_hit || Control_center_player_been_seen) {
343 if ((Last_time_cc_vis_check + F1_0*5 < GameTime) || (Last_time_cc_vis_check > GameTime)) {
344 vms_vector vec_to_player;
347 vm_vec_sub(&vec_to_player, &ConsoleObject->pos, &obj->pos);
348 dist_to_player = vm_vec_normalize_quick(&vec_to_player);
349 Last_time_cc_vis_check = GameTime;
350 if (dist_to_player < F1_0*120) {
351 Control_center_player_been_seen = player_is_visible_from_object(obj, &obj->pos, 0, &vec_to_player);
352 if (!Control_center_player_been_seen)
353 Control_center_been_hit = 0;
359 if ((Control_center_next_fire_time < 0) && !(Player_is_dead && (GameTime > Player_time_of_death+F1_0*2))) {
360 if (Players[Player_num].flags & PLAYER_FLAGS_CLOAKED)
361 best_gun_num = calc_best_gun(N_controlcen_guns, Gun_pos, Gun_dir, &Believed_player_pos);
363 best_gun_num = calc_best_gun(N_controlcen_guns, Gun_pos, Gun_dir, &ConsoleObject->pos);
365 if (best_gun_num != -1) {
366 int rand_prob, count;
367 vms_vector vec_to_goal;
371 if (Players[Player_num].flags & PLAYER_FLAGS_CLOAKED) {
372 vm_vec_sub(&vec_to_goal, &Believed_player_pos, &Gun_pos[best_gun_num]);
373 dist_to_player = vm_vec_normalize_quick(&vec_to_goal);
375 vm_vec_sub(&vec_to_goal, &ConsoleObject->pos, &Gun_pos[best_gun_num]);
376 dist_to_player = vm_vec_normalize_quick(&vec_to_goal);
379 if (dist_to_player > F1_0*300)
381 Control_center_been_hit = 0;
382 Control_center_player_been_seen = 0;
387 if (Game_mode & GM_MULTI)
388 multi_send_controlcen_fire(&vec_to_goal, best_gun_num, OBJECT_NUMBER(obj));
390 Laser_create_new_easy( &vec_to_goal, &Gun_pos[best_gun_num], OBJECT_NUMBER(obj), CONTROLCEN_WEAPON_NUM, 1 );
392 // some of time, based on level, fire another thing, not directly at player, so it might hit him if he's constantly moving.
393 rand_prob = F1_0/(abs(Current_level_num)/4+2);
395 while ((d_rand() > rand_prob) && (count < 4)) {
398 make_random_vector(&randvec);
399 vm_vec_scale_add2(&vec_to_goal, &randvec, F1_0/6);
400 vm_vec_normalize_quick(&vec_to_goal);
402 if (Game_mode & GM_MULTI)
403 multi_send_controlcen_fire(&vec_to_goal, best_gun_num, OBJECT_NUMBER(obj));
405 Laser_create_new_easy( &vec_to_goal, &Gun_pos[best_gun_num], OBJECT_NUMBER(obj), CONTROLCEN_WEAPON_NUM, 0 );
409 delta_fire_time = (NDL - Difficulty_level) * F1_0/4;
410 if (Difficulty_level == 0)
411 delta_fire_time += F1_0/2;
413 if (Game_mode & GM_MULTI) // slow down rate of fire in multi player
414 delta_fire_time *= 2;
416 Control_center_next_fire_time = delta_fire_time;
420 Control_center_next_fire_time -= FrameTime;
424 int Reactor_strength=-1; //-1 mean not set by designer
426 // -----------------------------------------------------------------------------
427 // This must be called at the start of each level.
428 // If this level contains a boss and mode != multiplayer, don't do control center stuff. (Ghost out control center object.)
429 // If this level contains a boss and mode == multiplayer, do control center stuff.
430 void init_controlcen_for_level(void)
434 int cntrlcen_objnum=-1, boss_objnum=-1;
436 for (i=0; i<=Highest_object_index; i++) {
438 if (objp->type == OBJ_CNTRLCEN)
440 if (cntrlcen_objnum != -1)
441 mprintf((1, "Warning: Two or more control centers including %i and %i\n", i, cntrlcen_objnum));
446 if ((objp->type == OBJ_ROBOT) && (Robot_info[objp->id].boss_flag)) {
447 // mprintf((0, "Found boss robot %d.\n", objp->id));
448 if (boss_objnum != -1)
449 mprintf((1, "Warning: Two or more bosses including %i and %i\n", i, boss_objnum));
456 if (cntrlcen_objnum == -1) {
457 mprintf((1, "Warning: No control center.\n"));
462 if ( (boss_objnum != -1) && !((Game_mode & GM_MULTI) && !(Game_mode & GM_MULTI_ROBOTS)) ) {
463 if (cntrlcen_objnum != -1) {
464 // mprintf((0, "Ghosting control center\n"));
465 Objects[cntrlcen_objnum].type = OBJ_GHOST;
466 Objects[cntrlcen_objnum].render_type = RT_NONE;
467 Control_center_present = 0;
470 // Compute all gun positions.
471 objp = &Objects[cntrlcen_objnum];
472 N_controlcen_guns = Reactors[objp->id].n_guns;
473 for (i=0; i<N_controlcen_guns; i++)
474 calc_controlcen_gun_point(&Gun_pos[i], &Gun_dir[i], objp, i);
475 Control_center_present = 1;
477 if (Reactor_strength == -1) { //use old defaults
478 // Boost control center strength at higher levels.
479 if (Current_level_num >= 0)
480 objp->shields = F1_0*200 + (F1_0*200/4) * Current_level_num;
482 objp->shields = F1_0*200 - Current_level_num*F1_0*150;
485 objp->shields = i2f(Reactor_strength);
490 // Say the control center has not yet been hit.
491 Control_center_been_hit = 0;
492 Control_center_player_been_seen = 0;
493 Control_center_next_fire_time = 0;
495 Dead_controlcen_object_num = -1;
498 void special_reactor_stuff(void)
500 mprintf((0, "Mucking with reactor countdown time.\n"));
501 if (Control_center_destroyed) {
502 Countdown_timer += i2f(Base_control_center_explosion_time + (NDL-1-Difficulty_level)*Base_control_center_explosion_time/(NDL-1));
503 Total_countdown_time = f2i(Countdown_timer)+2; // Will prevent "Self destruct sequence activated" message from replaying.
509 * reads n reactor structs from a CFILE
511 extern int reactor_read_n(reactor *r, int n, CFILE *fp)
515 for (i = 0; i < n; i++) {
516 r[i].model_num = cfile_read_int(fp);
517 r[i].n_guns = cfile_read_int(fp);
518 for (j = 0; j < MAX_CONTROLCEN_GUNS; j++)
519 cfile_read_vector(&(r[i].gun_points[j]), fp);
520 for (j = 0; j < MAX_CONTROLCEN_GUNS; j++)
521 cfile_read_vector(&(r[i].gun_dirs[j]), fp);
527 * reads a control_center_triggers structure from a CFILE
529 extern int control_center_triggers_read_n(control_center_triggers *cct, int n, CFILE *fp)
533 for (i = 0; i < n; i++)
535 cct->num_links = cfile_read_short(fp);
536 for (j = 0; j < MAX_CONTROLCEN_LINKS; j++)
537 cct->seg[j] = cfile_read_short(fp);
538 for (j = 0; j < MAX_CONTROLCEN_LINKS; j++)
539 cct->side[j] = cfile_read_short(fp);
545 int control_center_triggers_write(control_center_triggers *cct, PHYSFS_file *fp)
549 PHYSFS_writeSLE16(fp, cct->num_links);
550 for (j = 0; j < MAX_CONTROLCEN_LINKS; j++)
551 PHYSFS_writeSLE16(fp, cct->seg[j]);
552 for (j = 0; j < MAX_CONTROLCEN_LINKS; j++)
553 PHYSFS_writeSLE16(fp, cct->side[j]);