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