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