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