]> icculus.org git repositories - btb/d2x.git/blob - main/cntrlcen.c
remove rcs tags
[btb/d2x.git] / main / cntrlcen.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 /*
15  *
16  * Code for the control center
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <conf.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #if !defined(_WIN32) && !defined(macintosh)
27 #include <unistd.h>
28 #endif
29
30 #include "pstypes.h"
31 #include "error.h"
32 #include "mono.h"
33
34 #include "inferno.h"
35 #include "cntrlcen.h"
36 #include "game.h"
37 #include "laser.h"
38 #include "gameseq.h"
39 #include "ai.h"
40 #ifdef NETWORK
41 #include "multi.h"
42 #endif
43 #include "wall.h"
44 #include "object.h"
45 #include "robot.h"
46 #include "vclip.h"
47 #include "fireball.h"
48 #include "endlevel.h"
49 #include "playsave.h"
50
51 //@@vms_vector controlcen_gun_points[MAX_CONTROLCEN_GUNS];
52 //@@vms_vector controlcen_gun_dirs[MAX_CONTROLCEN_GUNS];
53
54 reactor Reactors[MAX_REACTORS];
55 int Num_reactors=0;
56
57 control_center_triggers ControlCenterTriggers;
58
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;
64
65 vms_vector      Gun_pos[MAX_CONTROLCEN_GUNS], Gun_dir[MAX_CONTROLCEN_GUNS];
66
67 void do_countdown_frame();
68
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)
72 {
73         reactor *reactor;
74         vms_matrix m;
75
76         Assert(obj->type == OBJ_CNTRLCEN);
77         Assert(obj->render_type==RT_POLYOBJ);
78
79         reactor = &Reactors[obj->id];
80
81         Assert(gun_num < reactor->n_guns);
82
83         //instance gun position & orientation
84
85         vm_copy_transpose_matrix(&m,&obj->orient);
86
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);
90 }
91
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)
97 {
98         int     i;
99         fix     best_dot;
100         int     best_gun;
101
102         best_dot = -F1_0*2;
103         best_gun = -1;
104
105         for (i=0; i<num_guns; i++) {
106                 fix                     dot;
107                 vms_vector      gun_vec;
108
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);
112
113                 if (dot > best_dot) {
114                         best_dot = dot;
115                         best_gun = i;
116                 }
117         }
118
119         Assert(best_gun != -1);         // Contact Mike.  This is impossible.  Or maybe you're getting an unnormalized vector somewhere.
120
121         if (best_dot < 0)
122                 return -1;
123         else
124                 return best_gun;
125
126 }
127
128 extern fix Player_time_of_death;                //      object.c
129
130 int     Dead_controlcen_object_num=-1;
131
132 //how long to blow up on insane
133 int Base_control_center_explosion_time=DEFAULT_CONTROL_CENTER_EXPLOSION_TIME;
134
135 int Control_center_destroyed = 0;
136 fix Countdown_timer=0;
137 int Countdown_seconds_left=0, Total_countdown_time=0;           //in whole seconds
138
139 int     Alan_pavlish_reactor_times[NDL] = {90, 60, 45, 35, 30};
140
141 //      -----------------------------------------------------------------------------
142 //      Called every frame.  If control center been destroyed, then actually do something.
143 void do_controlcen_dead_frame(void)
144 {
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);
148
149         if (Control_center_destroyed && !Endlevel_sequence)
150                 do_countdown_frame();
151 }
152
153 #define COUNTDOWN_VOICE_TIME fl2f(12.75)
154
155 void do_countdown_frame()
156 {
157         fix     old_time;
158         int     fc, div_scale;
159
160         if (!Control_center_destroyed)  return;
161
162         if (!is_D2_OEM && !is_MAC_SHARE && !is_SHAREWARE)   // get countdown in OEM and SHAREWARE only
163         {
164                 // On last level, we don't want a countdown.
165                 if (PLAYING_BUILTIN_MISSION && Current_level_num == Last_level)
166                 {
167                         if (!(Game_mode & GM_MULTI))
168                                 return;
169                         if (Game_mode & GM_MULTI_ROBOTS)
170                                 return;
171                 }
172         }
173
174         //      Control center destroyed, rock the player's ship.
175         fc = Countdown_seconds_left;
176         if (fc > 16)
177                 fc = 16;
178
179         //      At Trainee, decrease rocking of ship by 4x.
180         div_scale = 1;
181         if (Difficulty_level == 0)
182                 div_scale = 4;
183
184         ConsoleObject->mtype.phys_info.rotvel.x += (fixmul(d_rand() - 16384, 3*F1_0/16 + (F1_0*(16-fc))/32))/div_scale;
185         ConsoleObject->mtype.phys_info.rotvel.z += (fixmul(d_rand() - 16384, 3*F1_0/16 + (F1_0*(16-fc))/32))/div_scale;
186         //      Hook in the rumble sound effect here.
187
188         old_time = Countdown_timer;
189         Countdown_timer -= RealFrameTime;
190         Countdown_seconds_left = f2i(Countdown_timer + F1_0*7/8);
191
192         if ( (old_time > COUNTDOWN_VOICE_TIME ) && (Countdown_timer <= COUNTDOWN_VOICE_TIME) )  {
193                 digi_play_sample( SOUND_COUNTDOWN_13_SECS, F3_0 );
194         }
195         if ( f2i(old_time + F1_0*7/8) != Countdown_seconds_left )       {
196                 if ( (Countdown_seconds_left>=0) && (Countdown_seconds_left<10) )
197                         digi_play_sample( SOUND_COUNTDOWN_0_SECS+Countdown_seconds_left, F3_0 );
198                 if ( Countdown_seconds_left==Total_countdown_time-1)
199                         digi_play_sample( SOUND_COUNTDOWN_29_SECS, F3_0 );
200         }                                               
201
202         if (Countdown_timer > 0) {
203                 fix size,old_size;
204                 size = (i2f(Total_countdown_time)-Countdown_timer) / fl2f(0.65);
205                 old_size = (i2f(Total_countdown_time)-old_time) / fl2f(0.65);
206                 if (size != old_size && (Countdown_seconds_left < (Total_countdown_time-5) ))           {                       // Every 2 seconds!
207                         //@@if (Dead_controlcen_object_num != -1) {
208                         //@@    vms_vector vp;  //,v,c;
209                         //@@    compute_segment_center(&vp, &Segments[Objects[Dead_controlcen_object_num].segnum]);
210                         //@@    object_create_explosion( Objects[Dead_controlcen_object_num].segnum, &vp, size*10, VCLIP_SMALL_EXPLOSION);
211                         //@@}
212
213                         digi_play_sample( SOUND_CONTROL_CENTER_WARNING_SIREN, F3_0 );
214                 }
215         }  else {
216                 int flash_value;
217
218                 if (old_time > 0)
219                         digi_play_sample( SOUND_MINE_BLEW_UP, F1_0 );
220
221                 flash_value = f2i(-Countdown_timer * (64 / 4)); // 4 seconds to total whiteness
222                 PALETTE_FLASH_SET(flash_value,flash_value,flash_value);
223
224                 if (PaletteBlueAdd > 64 )       {
225                         gr_set_current_canvas( NULL );
226                         gr_clear_canvas(BM_XRGB(31,31,31));                             //make screen all white to match palette effect
227                         reset_cockpit();                                                                //force cockpit redraw next time
228                         reset_palette_add();                                                    //restore palette for death message
229                         //controlcen->MaxCapacity = Fuelcen_max_amount;
230                         //gauge_message( "Control Center Reset" );
231                         DoPlayerDead();         //kill_player();
232                 }                                                                                                                                                               
233         }
234 }
235
236 //      -----------------------------------------------------------------------------
237 //      Called when control center gets destroyed.
238 //      This code is common to whether control center is implicitly imbedded in a boss,
239 //      or is an object of its own.
240 //      if objp == NULL that means the boss was the control center and don't set Dead_controlcen_object_num
241 void do_controlcen_destroyed_stuff(object *objp)
242 {
243         int     i;
244
245    if ((Game_mode & GM_MULTI_ROBOTS) && Control_center_destroyed)
246     return; // Don't allow resetting if control center and boss on same level
247
248         // Must toggle walls whether it is a boss or control center.
249         for (i=0;i<ControlCenterTriggers.num_links;i++)
250                 wall_toggle(&Segments[ControlCenterTriggers.seg[i]], ControlCenterTriggers.side[i]);
251
252         // And start the countdown stuff.
253         Control_center_destroyed = 1;
254
255         //      If a secret level, delete secret.sgc to indicate that we can't return to our secret level.
256         if (Current_level_num < 0) {
257                 int     rval;
258                 rval = !PHYSFS_delete(PLAYER_DIR "secret.sgc");
259                 mprintf((0, "Deleting secret.sgc, return value = %i\n", rval));
260         }
261
262         if (Base_control_center_explosion_time != DEFAULT_CONTROL_CENTER_EXPLOSION_TIME)
263                 Total_countdown_time = Base_control_center_explosion_time + Base_control_center_explosion_time * (NDL-Difficulty_level-1)/2;
264         else
265                 Total_countdown_time = Alan_pavlish_reactor_times[Difficulty_level];
266
267         Countdown_timer = i2f(Total_countdown_time);
268
269         if (!Control_center_present || objp==NULL) {
270                 //Assert(objp == NULL);
271                 return;
272         }
273
274         //Assert(objp != NULL);
275
276         Dead_controlcen_object_num = OBJECT_NUMBER(objp);
277 }
278
279 int     Last_time_cc_vis_check = 0;
280
281 //      -----------------------------------------------------------------------------
282 //do whatever this thing does in a frame
283 void do_controlcen_frame(object *obj)
284 {
285         int                     best_gun_num;
286
287         //      If a boss level, then Control_center_present will be 0.
288         if (!Control_center_present)
289                 return;
290
291 #ifndef NDEBUG
292         if (!Robot_firing_enabled || (Game_suspended & SUSP_ROBOTS))
293                 return;
294 #else
295         if (!Robot_firing_enabled)
296                 return;
297 #endif
298
299         if (!(Control_center_been_hit || Control_center_player_been_seen)) {
300                 if (!(FrameCount % 8)) {                //      Do every so often...
301                         vms_vector      vec_to_player;
302                         fix                     dist_to_player;
303                         int                     i;
304                         segment         *segp = &Segments[obj->segnum];
305
306                         // This is a hack.  Since the control center is not processed by
307                         // ai_do_frame, it doesn't know to deal with cloaked dudes.  It
308                         // seems to work in single-player mode because it is actually using
309                         // the value of Believed_player_position that was set by the last
310                         // person to go through ai_do_frame.  But since a no-robots game
311                         // never goes through ai_do_frame, I'm making it so the control
312                         // center can spot cloaked dudes.
313
314                         if (Game_mode & GM_MULTI)
315                                 Believed_player_pos = Objects[Players[Player_num].objnum].pos;
316
317                         //      Hack for special control centers which are isolated and not reachable because the
318                         //      real control center is inside the boss.
319                         for (i=0; i<MAX_SIDES_PER_SEGMENT; i++)
320                                 if (IS_CHILD(segp->children[i]))
321                                         break;
322                         if (i == MAX_SIDES_PER_SEGMENT)
323                                 return;
324
325                         vm_vec_sub(&vec_to_player, &ConsoleObject->pos, &obj->pos);
326                         dist_to_player = vm_vec_normalize_quick(&vec_to_player);
327                         if (dist_to_player < F1_0*200) {
328                                 Control_center_player_been_seen = player_is_visible_from_object(obj, &obj->pos, 0, &vec_to_player);
329                                 Control_center_next_fire_time = 0;
330                         }
331                 }                       
332
333                 return;
334         }
335
336         //      Periodically, make the reactor fall asleep if player not visible.
337         if (Control_center_been_hit || Control_center_player_been_seen) {
338                 if ((Last_time_cc_vis_check + F1_0*5 < GameTime) || (Last_time_cc_vis_check > GameTime)) {
339                         vms_vector      vec_to_player;
340                         fix                     dist_to_player;
341
342                         vm_vec_sub(&vec_to_player, &ConsoleObject->pos, &obj->pos);
343                         dist_to_player = vm_vec_normalize_quick(&vec_to_player);
344                         Last_time_cc_vis_check = GameTime;
345                         if (dist_to_player < F1_0*120) {
346                                 Control_center_player_been_seen = player_is_visible_from_object(obj, &obj->pos, 0, &vec_to_player);
347                                 if (!Control_center_player_been_seen)
348                                         Control_center_been_hit = 0;
349                         }
350                 }
351
352         }
353
354         if ((Control_center_next_fire_time < 0) && !(Player_is_dead && (GameTime > Player_time_of_death+F1_0*2))) {
355                 if (Players[Player_num].flags & PLAYER_FLAGS_CLOAKED)
356                         best_gun_num = calc_best_gun(N_controlcen_guns, Gun_pos, Gun_dir, &Believed_player_pos);
357                 else
358                         best_gun_num = calc_best_gun(N_controlcen_guns, Gun_pos, Gun_dir, &ConsoleObject->pos);
359
360                 if (best_gun_num != -1) {
361                         int                     rand_prob, count;
362                         vms_vector      vec_to_goal;
363                         fix                     dist_to_player;
364                         fix                     delta_fire_time;
365
366                         if (Players[Player_num].flags & PLAYER_FLAGS_CLOAKED) {
367                                 vm_vec_sub(&vec_to_goal, &Believed_player_pos, &Gun_pos[best_gun_num]);
368                                 dist_to_player = vm_vec_normalize_quick(&vec_to_goal);
369                         } else {
370                                 vm_vec_sub(&vec_to_goal, &ConsoleObject->pos, &Gun_pos[best_gun_num]);
371                                 dist_to_player = vm_vec_normalize_quick(&vec_to_goal);
372                         }
373
374                         if (dist_to_player > F1_0*300)
375                         {
376                                 Control_center_been_hit = 0;
377                                 Control_center_player_been_seen = 0;
378                                 return;
379                         }
380         
381                         #ifdef NETWORK
382                         if (Game_mode & GM_MULTI)
383                                 multi_send_controlcen_fire(&vec_to_goal, best_gun_num, OBJECT_NUMBER(obj));
384                         #endif
385                         Laser_create_new_easy( &vec_to_goal, &Gun_pos[best_gun_num], OBJECT_NUMBER(obj), CONTROLCEN_WEAPON_NUM, 1 );
386
387                         //      some of time, based on level, fire another thing, not directly at player, so it might hit him if he's constantly moving.
388                         rand_prob = F1_0/(abs(Current_level_num)/4+2);
389                         count = 0;
390                         while ((d_rand() > rand_prob) && (count < 4)) {
391                                 vms_vector      randvec;
392
393                                 make_random_vector(&randvec);
394                                 vm_vec_scale_add2(&vec_to_goal, &randvec, F1_0/6);
395                                 vm_vec_normalize_quick(&vec_to_goal);
396                                 #ifdef NETWORK
397                                 if (Game_mode & GM_MULTI)
398                                         multi_send_controlcen_fire(&vec_to_goal, best_gun_num, OBJECT_NUMBER(obj));
399                                 #endif
400                                 Laser_create_new_easy( &vec_to_goal, &Gun_pos[best_gun_num], OBJECT_NUMBER(obj), CONTROLCEN_WEAPON_NUM, 0 );
401                                 count++;
402                         }
403
404                         delta_fire_time = (NDL - Difficulty_level) * F1_0/4;
405                         if (Difficulty_level == 0)
406                                 delta_fire_time += F1_0/2;
407
408                         if (Game_mode & GM_MULTI) // slow down rate of fire in multi player
409                                 delta_fire_time *= 2;
410
411                         Control_center_next_fire_time = delta_fire_time;
412
413                 }
414         } else
415                 Control_center_next_fire_time -= FrameTime;
416
417 }
418
419 int Reactor_strength=-1;                //-1 mean not set by designer
420
421 //      -----------------------------------------------------------------------------
422 //      This must be called at the start of each level.
423 //      If this level contains a boss and mode != multiplayer, don't do control center stuff.  (Ghost out control center object.)
424 //      If this level contains a boss and mode == multiplayer, do control center stuff.
425 void init_controlcen_for_level(void)
426 {
427         int             i;
428         object  *objp;
429         int             cntrlcen_objnum=-1, boss_objnum=-1;
430
431         for (i=0; i<=Highest_object_index; i++) {
432                 objp = &Objects[i];
433                 if (objp->type == OBJ_CNTRLCEN)
434                 {
435                         if (cntrlcen_objnum != -1)
436                                 mprintf((1, "Warning: Two or more control centers including %i and %i\n", i, cntrlcen_objnum));
437                         else
438                                 cntrlcen_objnum = i;
439                 }
440
441                 if ((objp->type == OBJ_ROBOT) && (Robot_info[objp->id].boss_flag)) {
442 //                      mprintf((0, "Found boss robot %d.\n", objp->id));
443                         if (boss_objnum != -1)
444                                 mprintf((1, "Warning: Two or more bosses including %i and %i\n", i, boss_objnum));
445                         else
446                                 boss_objnum = i;
447                 }
448         }
449
450 #ifndef NDEBUG
451         if (cntrlcen_objnum == -1) {
452                 mprintf((1, "Warning: No control center.\n"));
453                 return;
454         }
455 #endif
456
457         if ( (boss_objnum != -1) && !((Game_mode & GM_MULTI) && !(Game_mode & GM_MULTI_ROBOTS)) ) {
458                 if (cntrlcen_objnum != -1) {
459 //                      mprintf((0, "Ghosting control center\n"));
460                         Objects[cntrlcen_objnum].type = OBJ_GHOST;
461                         Objects[cntrlcen_objnum].render_type = RT_NONE;
462                         Control_center_present = 0;
463                 }
464         } else {
465                 //      Compute all gun positions.
466                 objp = &Objects[cntrlcen_objnum];
467                 N_controlcen_guns = Reactors[objp->id].n_guns;
468                 for (i=0; i<N_controlcen_guns; i++)
469                         calc_controlcen_gun_point(&Gun_pos[i], &Gun_dir[i], objp, i);
470                 Control_center_present = 1;
471
472                 if (Reactor_strength == -1) {           //use old defaults
473                         //      Boost control center strength at higher levels.
474                         if (Current_level_num >= 0)
475                                 objp->shields = F1_0*200 + (F1_0*200/4) * Current_level_num;
476                         else
477                                 objp->shields = F1_0*200 - Current_level_num*F1_0*150;
478                 }
479                 else {
480                         objp->shields = i2f(Reactor_strength);
481                 }
482
483         }
484
485         //      Say the control center has not yet been hit.
486         Control_center_been_hit = 0;
487         Control_center_player_been_seen = 0;
488         Control_center_next_fire_time = 0;
489         
490         Dead_controlcen_object_num = -1;
491 }
492
493 void special_reactor_stuff(void)
494 {
495         mprintf((0, "Mucking with reactor countdown time.\n"));
496         if (Control_center_destroyed) {
497                 Countdown_timer += i2f(Base_control_center_explosion_time + (NDL-1-Difficulty_level)*Base_control_center_explosion_time/(NDL-1));
498                 Total_countdown_time = f2i(Countdown_timer)+2;  //      Will prevent "Self destruct sequence activated" message from replaying.
499         }
500 }
501
502 #ifndef FAST_FILE_IO
503 /*
504  * reads n reactor structs from a CFILE
505  */
506 extern int reactor_read_n(reactor *r, int n, CFILE *fp)
507 {
508         int i, j;
509
510         for (i = 0; i < n; i++) {
511                 r[i].model_num = cfile_read_int(fp);
512                 r[i].n_guns = cfile_read_int(fp);
513                 for (j = 0; j < MAX_CONTROLCEN_GUNS; j++)
514                         cfile_read_vector(&(r[i].gun_points[j]), fp);
515                 for (j = 0; j < MAX_CONTROLCEN_GUNS; j++)
516                         cfile_read_vector(&(r[i].gun_dirs[j]), fp);
517         }
518         return i;
519 }
520
521 /*
522  * reads a control_center_triggers structure from a CFILE
523  */
524 extern int control_center_triggers_read_n(control_center_triggers *cct, int n, CFILE *fp)
525 {
526         int i, j;
527
528         for (i = 0; i < n; i++)
529         {
530                 cct->num_links = cfile_read_short(fp);
531                 for (j = 0; j < MAX_CONTROLCEN_LINKS; j++)
532                         cct->seg[j] = cfile_read_short(fp);
533                 for (j = 0; j < MAX_CONTROLCEN_LINKS; j++)
534                         cct->side[j] = cfile_read_short(fp);
535         }
536         return i;
537 }
538 #endif
539
540 int control_center_triggers_write(control_center_triggers *cct, PHYSFS_file *fp)
541 {
542         int j;
543
544         PHYSFS_writeSLE16(fp, cct->num_links);
545         for (j = 0; j < MAX_CONTROLCEN_LINKS; j++)
546                 PHYSFS_writeSLE16(fp, cct->seg[j]);
547         for (j = 0; j < MAX_CONTROLCEN_LINKS; j++)
548                 PHYSFS_writeSLE16(fp, cct->side[j]);
549
550         return 1;
551 }