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