]> icculus.org git repositories - btb/d2x.git/blob - main/laser.c
move old per-file change logs into new file ChangeLog-old
[btb/d2x.git] / main / laser.c
1 /* $Id: laser.c,v 1.12 2004-08-28 23:17:45 schaffner 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  * This will contain the laser code
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <conf.h>
23 #endif
24
25 #ifdef RCS
26 char laser_rcsid[] = "$Id: laser.c,v 1.12 2004-08-28 23:17:45 schaffner Exp $";
27 #endif
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <time.h>
32
33 #include "inferno.h"
34 #include "game.h"
35 #include "bm.h"
36 #include "object.h"
37 #include "laser.h"
38 #include "args.h"
39 #include "segment.h"
40 #include "fvi.h"
41 #include "segpoint.h"
42 #include "error.h"
43 #include "mono.h"
44 #include "key.h"
45 #include "texmap.h"
46 #include "textures.h"
47 #include "render.h"
48 #include "vclip.h"
49 #include "fireball.h"
50 #include "polyobj.h"
51 #include "robot.h"
52 #include "weapon.h"
53 #include "newdemo.h"
54 #include "timer.h"
55 #include "player.h"
56 #include "sounds.h"
57 #ifdef NETWORK
58 #include "network.h"
59 #endif
60 #include "ai.h"
61 #ifdef NETWORK
62 #include "modem.h"
63 #endif
64 #include "powerup.h"
65 #include "multi.h"
66 #include "physics.h"
67 #include "multi.h"
68
69 #ifdef TACTILE
70 #include "tactile.h"
71 #endif
72
73 int Laser_rapid_fire = 0;
74
75
76 object *Guided_missile[MAX_PLAYERS]={NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
77 int Guided_missile_sig[MAX_PLAYERS]={-1,-1,-1,-1,-1,-1,-1,-1};
78
79 int find_homing_object_complete(vms_vector *curpos, object *tracker, int track_obj_type1, int track_obj_type2);
80
81 extern char Multi_is_guided;
82 extern char BounceCheat;
83                                                                                                 
84 extern void newdemo_record_guided_end();
85 extern void newdemo_record_guided_start();
86
87 int find_homing_object(vms_vector *curpos, object *tracker);
88
89 //---------------------------------------------------------------------------------
90 // Called by render code.... determines if the laser is from a robot or the
91 // player and calls the appropriate routine.
92
93 void Laser_render(object *obj)
94 {
95
96 //      Commented out by John (sort of, typed by Mike) on 6/8/94
97 #if 0
98         switch( obj->id )       {
99         case WEAPON_TYPE_WEAK_LASER:
100         case WEAPON_TYPE_STRONG_LASER:
101         case WEAPON_TYPE_CANNON_BALL:
102         case WEAPON_TYPE_MISSILE:
103                 break;
104         default:
105                 Error( "Invalid weapon type in Laser_render\n" );
106         }
107 #endif
108         
109         switch( Weapon_info[obj->id].render_type )      {
110         case WEAPON_RENDER_LASER:
111                 Int3(); // Not supported anymore!
112                                         //Laser_draw_one(obj-Objects, Weapon_info[obj->id].bitmap );
113                 break;
114         case WEAPON_RENDER_BLOB:
115                 draw_object_blob(obj, Weapon_info[obj->id].bitmap  );
116                 break;
117         case WEAPON_RENDER_POLYMODEL:
118                 break;
119         case WEAPON_RENDER_VCLIP:
120                 Int3(); //      Oops, not supported, type added by mk on 09/09/94, but not for lasers...
121         default:
122                 Error( "Invalid weapon render type in Laser_render\n" );
123         }
124
125 }
126
127 //---------------------------------------------------------------------------------
128 // Draws a texture-mapped laser bolt
129
130 //void Laser_draw_one( int objnum, grs_bitmap * bmp )
131 //{
132 //      int t1, t2, t3;
133 //      g3s_point p1, p2;
134 //      object *obj;
135 //      vms_vector start_pos,end_pos;
136 //
137 //      obj = &Objects[objnum];
138 //
139 //      start_pos = obj->pos;
140 //      vm_vec_scale_add(&end_pos,&start_pos,&obj->orient.fvec,-Laser_length);
141 //
142 //      g3_rotate_point(&p1,&start_pos);
143 //      g3_rotate_point(&p2,&end_pos);
144 //
145 //      t1 = Lighting_on;
146 //      t2 = Interpolation_method;
147 //      t3 = Transparency_on;
148 //
149 //      Lighting_on  = 0;
150 //      //Interpolation_method = 3;     // Full perspective
151 //      Interpolation_method = 1;       // Linear
152 //      Transparency_on = 1;
153 //
154 //      //gr_setcolor( gr_getcolor(31,15,0));
155 //      //g3_draw_line_ptrs(p1,p2);
156 //      //g3_draw_rod(p1,0x2000,p2,0x2000);
157 //      //g3_draw_rod(p1,Laser_width,p2,Laser_width);
158 //      g3_draw_rod_tmap(bmp,&p2,Laser_width,&p1,Laser_width,0);
159 //      Lighting_on = t1;
160 //      Interpolation_method = t2;
161 //      Transparency_on = t3;
162 //
163 //}
164
165 //      Changed by MK on 09/07/94
166 //      I want you to be able to blow up your own bombs.
167 //      AND...Your proximity bombs can blow you up if they're 2.0 seconds or more old.
168 //      Changed by MK on 06/06/95: Now must be 4.0 seconds old.  Much valid Net-complaining.
169 int laser_are_related( int o1, int o2 )
170 {
171         if ( (o1<0) || (o2<0) ) 
172                 return 0;
173
174         // See if o2 is the parent of o1
175         if ( Objects[o1].type == OBJ_WEAPON  )
176                 if ( (Objects[o1].ctype.laser_info.parent_num==o2) && (Objects[o1].ctype.laser_info.parent_signature==Objects[o2].signature) )
177                 {
178                         //      o1 is a weapon, o2 is the parent of 1, so if o1 is PROXIMITY_BOMB and o2 is player, they are related only if o1 < 2.0 seconds old
179                         if ((Objects[o1].id == PHOENIX_ID && (GameTime > Objects[o1].ctype.laser_info.creation_time + F1_0/4)) || 
180                            (Objects[o1].id == GUIDEDMISS_ID && (GameTime > Objects[o1].ctype.laser_info.creation_time + F1_0*2)) || 
181                                 (((Objects[o1].id == PROXIMITY_ID) || (Objects[o1].id == SUPERPROX_ID)) && (GameTime > Objects[o1].ctype.laser_info.creation_time + F1_0*4))) {
182                                 return 0;
183                         } else
184                                 return 1;
185                 }
186
187         // See if o1 is the parent of o2
188         if ( Objects[o2].type == OBJ_WEAPON  )
189         {
190                 if ( (Objects[o2].ctype.laser_info.parent_num==o1) && (Objects[o2].ctype.laser_info.parent_signature==Objects[o1].signature) )
191                 {
192                         //      o2 is a weapon, o1 is the parent of 2, so if o2 is PROXIMITY_BOMB and o1 is player, they are related only if o1 < 2.0 seconds old
193                         if ((Objects[o2].id == PHOENIX_ID && (GameTime > Objects[o2].ctype.laser_info.creation_time + F1_0/4)) || 
194                            (Objects[o2].id == GUIDEDMISS_ID && (GameTime > Objects[o2].ctype.laser_info.creation_time + F1_0*2)) || 
195                                 (((Objects[o2].id == PROXIMITY_ID) || (Objects[o2].id == SUPERPROX_ID)) && (GameTime > Objects[o2].ctype.laser_info.creation_time + F1_0*4))) {
196                                 return 0;
197                         } else
198                                 return 1;
199                 }
200         }
201
202         // They must both be weapons
203         if ( Objects[o1].type != OBJ_WEAPON || Objects[o2].type != OBJ_WEAPON ) 
204                 return 0;
205
206         //      Here is the 09/07/94 change -- Siblings must be identical, others can hurt each other
207         // See if they're siblings...
208         //      MK: 06/08/95, Don't allow prox bombs to detonate for 3/4 second.  Else too likely to get toasted by your own bomb if hit by opponent.
209         if ( Objects[o1].ctype.laser_info.parent_signature==Objects[o2].ctype.laser_info.parent_signature )
210         {
211                 if (Objects[o1].id == PROXIMITY_ID  || Objects[o2].id == PROXIMITY_ID || Objects[o1].id == SUPERPROX_ID || Objects[o2].id == SUPERPROX_ID) {
212                         //      If neither is older than 1/2 second, then can't blow up!
213                         if ((GameTime > (Objects[o1].ctype.laser_info.creation_time + F1_0/2)) || (GameTime > (Objects[o2].ctype.laser_info.creation_time + F1_0/2)))
214                                 return 0;
215                         else
216                                 return 1;
217                 } else
218                         return 1;
219         }
220
221         //      Anything can cause a collision with a robot super prox mine.
222         if (Objects[o1].id == ROBOT_SUPERPROX_ID || Objects[o2].id == ROBOT_SUPERPROX_ID ||
223                  Objects[o1].id == PROXIMITY_ID || Objects[o2].id == PROXIMITY_ID ||
224                  Objects[o1].id == SUPERPROX_ID || Objects[o2].id == SUPERPROX_ID ||
225                  Objects[o1].id == PMINE_ID || Objects[o2].id == PMINE_ID)
226                 return 0;
227
228         return 1;
229 }
230
231 //--unused-- int Muzzle_scale=2;
232 int Laser_offset=0;
233
234 void do_muzzle_stuff(int segnum, vms_vector *pos)
235 {
236         Muzzle_data[Muzzle_queue_index].create_time = timer_get_fixed_seconds();
237         Muzzle_data[Muzzle_queue_index].segnum = segnum;
238         Muzzle_data[Muzzle_queue_index].pos = *pos;
239         Muzzle_queue_index++;
240         if (Muzzle_queue_index >= MUZZLE_QUEUE_MAX)
241                 Muzzle_queue_index = 0;
242 }
243
244 //creates a weapon object
245 int create_weapon_object(int weapon_type,int segnum,vms_vector *position)
246 {
247         int rtype=-1;
248         fix laser_radius = -1;
249         int objnum;
250         object *obj;
251
252         switch( Weapon_info[weapon_type].render_type )  {
253
254                 case WEAPON_RENDER_BLOB:
255                         rtype = RT_LASER;                       // Render as a laser even if blob (see render code above for explanation)
256                         laser_radius = Weapon_info[weapon_type].blob_size;
257                         break;
258                 case WEAPON_RENDER_POLYMODEL:
259                         laser_radius = 0;       //      Filled in below.
260                         rtype = RT_POLYOBJ;
261                         break;
262                 case WEAPON_RENDER_LASER:
263                         Int3();         // Not supported anymore
264                         break;
265                 case WEAPON_RENDER_NONE:
266                         rtype = RT_NONE;
267                         laser_radius = F1_0;
268                         break;
269                 case WEAPON_RENDER_VCLIP:
270                         rtype = RT_WEAPON_VCLIP;
271                         laser_radius = Weapon_info[weapon_type].blob_size;
272                         break;
273                 default:
274                         Error( "Invalid weapon render type in Laser_create_new\n" );
275         }
276
277         Assert(laser_radius != -1);
278         Assert(rtype != -1);
279
280         objnum = obj_create( OBJ_WEAPON, weapon_type, segnum, position, NULL, laser_radius, CT_WEAPON, MT_PHYSICS, rtype );
281
282         obj = &Objects[objnum];
283
284         if (Weapon_info[weapon_type].render_type == WEAPON_RENDER_POLYMODEL) {
285                 obj->rtype.pobj_info.model_num = Weapon_info[obj->id].model_num;
286                 obj->size = fixdiv(Polygon_models[obj->rtype.pobj_info.model_num].rad,Weapon_info[obj->id].po_len_to_width_ratio);
287         }
288
289         obj->mtype.phys_info.mass = Weapon_info[weapon_type].mass;
290         obj->mtype.phys_info.drag = Weapon_info[weapon_type].drag;
291         vm_vec_zero(&obj->mtype.phys_info.thrust);
292
293         if (Weapon_info[weapon_type].bounce==1)
294                 obj->mtype.phys_info.flags |= PF_BOUNCE;
295
296         if (Weapon_info[weapon_type].bounce==2 || BounceCheat)
297                 obj->mtype.phys_info.flags |= PF_BOUNCE+PF_BOUNCES_TWICE;
298
299
300         return objnum;
301 }
302
303 extern int Doing_lighting_hack_flag;
304
305 //      -------------------------------------------------------------------------------------------------------------------------------
306 //      ***** HEY ARTISTS!! *****
307 //      Here are the constants you're looking for! --MK
308
309 //      Change the following constants to affect the look of the omega cannon.
310 //      Changing these constants will not affect the damage done.
311 //      WARNING: If you change DESIRED_OMEGA_DIST and MAX_OMEGA_BLOBS, you don't merely change the look of the cannon,
312 //      you change its range.  If you decrease DESIRED_OMEGA_DIST, you decrease how far the gun can fire.
313 #define MIN_OMEGA_BLOBS         3                               //      No matter how close the obstruction, at this many blobs created.
314 #define MIN_OMEGA_DIST                  (F1_0*3)                //      At least this distance between blobs, unless doing so would violate MIN_OMEGA_BLOBS
315 #define DESIRED_OMEGA_DIST      (F1_0*5)                //      This is the desired distance between blobs.  For distances > MIN_OMEGA_BLOBS*DESIRED_OMEGA_DIST, but not very large, this will apply.
316 #define MAX_OMEGA_BLOBS         16                              //      No matter how far away the obstruction, this is the maximum number of blobs.
317 #define MAX_OMEGA_DIST                  (MAX_OMEGA_BLOBS * DESIRED_OMEGA_DIST)          //      Maximum extent of lightning blobs.
318
319 //      Additionally, several constants which apply to homing objects in general control the behavior of the Omega Cannon.
320 //      They are defined in laser.h.  They are copied here for reference.  These values are valid on 1/10/96:
321 //      If you want the Omega Cannon view cone to be different than the Homing Missile viewcone, contact MK to make the change.
322 //      (Unless you are a programmer, in which case, do it yourself!)
323 #define OMEGA_MIN_TRACKABLE_DOT                 (15*F1_0/16)            //      Larger values mean narrower cone.  F1_0 means damn near impossible.  0 means 180 degree field of view.
324 #define OMEGA_MAX_TRACKABLE_DIST                MAX_OMEGA_DIST  //      An object must be at least this close to be tracked.
325
326 //      Note, you don't need to change these constants.  You can control damage and energy consumption by changing the
327 //      usual bitmaps.tbl parameters.
328 #define OMEGA_DAMAGE_SCALE                      32                              //      Controls how much damage is done.  This gets multiplied by FrameTime and then again by the damage specified in bitmaps.tbl in the $WEAPON line.
329 #define OMEGA_ENERGY_CONSUMPTION        16                              //      Controls how much energy is consumed.  This gets multiplied by FrameTime and then again by the energy parameter from bitmaps.tbl.
330 //      -------------------------------------------------------------------------------------------------------------------------------
331
332 void delete_old_omega_blobs(object *parent_objp)
333 {
334         int     i;
335         int     parent_num;
336         int     count = 0;
337
338         parent_num = parent_objp->ctype.laser_info.parent_num;
339
340         for (i=0; i<=Highest_object_index; i++)
341                 if (Objects[i].type == OBJ_WEAPON)
342                         if (Objects[i].id == OMEGA_ID)
343                                 if (Objects[i].ctype.laser_info.parent_num == parent_num) {
344                                         obj_delete(i);
345                                         count++;
346                                 }
347
348         mprintf((0, "%i Omega blobs deleted in frame %i\n", count, FrameCount));
349 }
350
351 // ---------------------------------------------------------------------------------
352 void create_omega_blobs(int firing_segnum, vms_vector *firing_pos, vms_vector *goal_pos, object *parent_objp)
353 {
354         int                     i, last_segnum, last_created_objnum = -1;
355         vms_vector      vec_to_goal;
356         fix                     dist_to_goal;
357         int                     num_omega_blobs;
358         fix                     omega_blob_dist;
359         vms_vector      omega_delta_vector;
360         vms_vector      blob_pos, perturb_vec;
361         fix                     perturb_array[MAX_OMEGA_BLOBS];
362
363         if (Game_mode & GM_MULTI)
364                 delete_old_omega_blobs(parent_objp);
365
366         vm_vec_sub(&vec_to_goal, goal_pos, firing_pos);
367
368         dist_to_goal = vm_vec_normalize_quick(&vec_to_goal);
369
370         if (dist_to_goal < MIN_OMEGA_BLOBS * MIN_OMEGA_DIST) {
371                 omega_blob_dist = MIN_OMEGA_DIST;
372                 num_omega_blobs = dist_to_goal/omega_blob_dist;
373                 if (num_omega_blobs == 0)
374                         num_omega_blobs = 1;
375         } else {
376                 omega_blob_dist = DESIRED_OMEGA_DIST;
377                 num_omega_blobs = dist_to_goal / omega_blob_dist;
378                 if (num_omega_blobs > MAX_OMEGA_BLOBS) {
379                         num_omega_blobs = MAX_OMEGA_BLOBS;
380                         omega_blob_dist = dist_to_goal / num_omega_blobs;
381                 } else if (num_omega_blobs < MIN_OMEGA_BLOBS) {
382                         num_omega_blobs = MIN_OMEGA_BLOBS;
383                         omega_blob_dist = dist_to_goal / num_omega_blobs;
384                 }
385         }
386
387         omega_delta_vector = vec_to_goal;
388         vm_vec_scale(&omega_delta_vector, omega_blob_dist);
389
390         //      Now, create all the blobs
391         blob_pos = *firing_pos;
392         last_segnum = firing_segnum;
393
394         //      If nearby, don't perturb vector.  If not nearby, start halfway out.
395         if (dist_to_goal < MIN_OMEGA_DIST*4) {
396                 for (i=0; i<num_omega_blobs; i++)
397                         perturb_array[i] = 0;
398         } else {
399                 vm_vec_scale_add2(&blob_pos, &omega_delta_vector, F1_0/2);      //      Put first blob half way out.
400                 for (i=0; i<num_omega_blobs/2; i++) {
401                         perturb_array[i] = F1_0*i + F1_0/4;
402                         perturb_array[num_omega_blobs-1-i] = F1_0*i;
403                 }
404         }
405
406         //      Create random perturbation vector, but favor _not_ going up in player's reference.
407         make_random_vector(&perturb_vec);
408         vm_vec_scale_add2(&perturb_vec, &parent_objp->orient.uvec, -F1_0/2);
409
410         Doing_lighting_hack_flag = 1;   //      Ugly, but prevents blobs which are probably outside the mine from killing framerate.
411
412         for (i=0; i<num_omega_blobs; i++) {
413                 vms_vector      temp_pos;
414                 int                     blob_objnum, segnum;
415
416                 //      This will put the last blob right at the destination object, causing damage.
417                 if (i == num_omega_blobs-1)
418                         vm_vec_scale_add2(&blob_pos, &omega_delta_vector, 15*F1_0/32);  //      Move last blob another (almost) half section
419
420                 //      Every so often, re-perturb blobs
421                 if ((i % 4) == 3) {
422                         vms_vector      temp_vec;
423
424                         make_random_vector(&temp_vec);
425                         vm_vec_scale_add2(&perturb_vec, &temp_vec, F1_0/4);
426                 }
427
428                 vm_vec_scale_add(&temp_pos, &blob_pos, &perturb_vec, perturb_array[i]);
429
430                 segnum = find_point_seg(&temp_pos, last_segnum);
431                 if (segnum != -1) {
432                         object          *objp;
433
434                         last_segnum = segnum;
435                         blob_objnum = obj_create(OBJ_WEAPON, OMEGA_ID, segnum, &temp_pos, NULL, 0, CT_WEAPON, MT_PHYSICS, RT_WEAPON_VCLIP );
436                         if (blob_objnum == -1)
437                                 break;
438
439                         last_created_objnum = blob_objnum;
440
441                         objp = &Objects[blob_objnum];
442
443                         objp->lifeleft = ONE_FRAME_TIME;
444                         objp->mtype.phys_info.velocity = vec_to_goal;
445
446                         //      Only make the last one move fast, else multiple blobs might collide with target.
447                         vm_vec_scale(&objp->mtype.phys_info.velocity, F1_0*4);
448
449                         objp->size = Weapon_info[objp->id].blob_size;
450
451                         objp->shields = fixmul(OMEGA_DAMAGE_SCALE*FrameTime, Weapon_info[objp->id].strength[Difficulty_level]);
452         
453                         objp->ctype.laser_info.parent_type                      = parent_objp->type;
454                         objp->ctype.laser_info.parent_signature = parent_objp->signature;
455                         objp->ctype.laser_info.parent_num                       = parent_objp-Objects;
456                         objp->movement_type = MT_NONE;  //      Only last one moves, that will get bashed below.
457
458                 }
459
460                 vm_vec_add2(&blob_pos, &omega_delta_vector);
461
462         }
463
464         //      Make last one move faster, but it's already moving at speed = F1_0*4.
465         if (last_created_objnum != -1) {
466                 vm_vec_scale(&Objects[last_created_objnum].mtype.phys_info.velocity, Weapon_info[OMEGA_ID].speed[Difficulty_level]/4);
467                 Objects[last_created_objnum].movement_type = MT_PHYSICS;
468         }
469
470         Doing_lighting_hack_flag = 0;
471 }
472
473 #define MIN_OMEGA_CHARGE        (MAX_OMEGA_CHARGE/8)
474 #define OMEGA_CHARGE_SCALE      4                       //      FrameTime / OMEGA_CHARGE_SCALE added to Omega_charge every frame.
475 fix     Omega_charge = MAX_OMEGA_CHARGE;
476
477 #define OMEGA_CHARGE_SCALE      4
478
479 int     Last_omega_fire_frame=0;
480
481 // ---------------------------------------------------------------------------------
482 //      Call this every frame to recharge the Omega Cannon.
483 void omega_charge_frame(void)
484 {
485         fix     delta_charge, old_omega_charge;
486
487         if (Omega_charge == MAX_OMEGA_CHARGE)
488                 return;
489
490         if (!(player_has_weapon(OMEGA_INDEX, 0) & HAS_WEAPON_FLAG))
491                 return;
492
493         if (Player_is_dead)
494                 return;
495
496         if ((Primary_weapon == OMEGA_INDEX) && (Omega_charge == 0) && (Players[Player_num].energy == 0)) {
497                 Primary_weapon--;
498                 auto_select_weapon(0);
499         }
500
501         //      Don't charge while firing.
502         if ((Last_omega_fire_frame == FrameCount) || (Last_omega_fire_frame == FrameCount-1))
503                 return;
504
505         if (Players[Player_num].energy) {
506                 fix     energy_used;
507
508                 old_omega_charge = Omega_charge;
509                 Omega_charge += FrameTime/OMEGA_CHARGE_SCALE;
510                 if (Omega_charge > MAX_OMEGA_CHARGE)
511                         Omega_charge = MAX_OMEGA_CHARGE;
512
513                 delta_charge = Omega_charge - old_omega_charge;
514
515                 energy_used = fixmul(F1_0*190/17, delta_charge);
516                 if (Difficulty_level < 2)
517                         energy_used = fixmul(energy_used, i2f(Difficulty_level+2)/4);
518
519                 Players[Player_num].energy -= energy_used;
520                 if (Players[Player_num].energy < 0)
521                         Players[Player_num].energy = 0;
522         }
523
524
525 }
526
527 // -- fix       Last_omega_muzzle_flash_time;
528
529 // ---------------------------------------------------------------------------------
530 //      *objp is the object firing the omega cannon
531 //      *pos is the location from which the omega bolt starts
532 void do_omega_stuff(object *parent_objp, vms_vector *firing_pos, object *weapon_objp)
533 {
534         int                     lock_objnum, firing_segnum;
535         vms_vector      goal_pos;
536         int                     pnum = parent_objp->id;
537
538         if (pnum == Player_num) {
539                 //      If charge >= min, or (some charge and zero energy), allow to fire.
540                 if (!((Omega_charge >= MIN_OMEGA_CHARGE) || (Omega_charge && !Players[pnum].energy))) {
541                         obj_delete(weapon_objp-Objects);
542                         return;
543                 }
544
545                 Omega_charge -= FrameTime;
546                 if (Omega_charge < 0)
547                         Omega_charge = 0;
548
549                 //      Ensure that the lightning cannon can be fired next frame.
550                 Next_laser_fire_time = GameTime+1;
551
552                 Last_omega_fire_frame = FrameCount;
553         }
554
555         weapon_objp->ctype.laser_info.parent_type = OBJ_PLAYER;
556         weapon_objp->ctype.laser_info.parent_num = Players[pnum].objnum;
557         weapon_objp->ctype.laser_info.parent_signature = Objects[Players[pnum].objnum].signature;
558
559         lock_objnum = find_homing_object(firing_pos, weapon_objp);
560
561         firing_segnum = find_point_seg(firing_pos, parent_objp->segnum);
562
563         //      Play sound.
564         if ( parent_objp == Viewer )
565                 digi_play_sample( Weapon_info[weapon_objp->id].flash_sound, F1_0 );
566         else
567                 digi_link_sound_to_pos( Weapon_info[weapon_objp->id].flash_sound, weapon_objp->segnum, 0, &weapon_objp->pos, 0, F1_0 );
568
569         // -- if ((Last_omega_muzzle_flash_time + F1_0/4 < GameTime) || (Last_omega_muzzle_flash_time > GameTime)) {
570         // --   do_muzzle_stuff(firing_segnum, firing_pos);
571         // --   Last_omega_muzzle_flash_time = GameTime;
572         // -- }
573
574         //      Delete the original object.  Its only purpose in life was to determine which object to home in on.
575         obj_delete(weapon_objp-Objects);
576
577         //      If couldn't lock on anything, fire straight ahead.
578         if (lock_objnum == -1) {
579                 fvi_query       fq;
580                 fvi_info                hit_data;
581                 int                     fate;
582                 vms_vector      perturb_vec, perturbed_fvec;
583
584                 make_random_vector(&perturb_vec);
585                 vm_vec_scale_add(&perturbed_fvec, &parent_objp->orient.fvec, &perturb_vec, F1_0/16);
586
587                 vm_vec_scale_add(&goal_pos, firing_pos, &perturbed_fvec, MAX_OMEGA_DIST);
588                 fq.startseg = firing_segnum;
589                 if (fq.startseg == -1) {
590                         mprintf((1, "Trying to fire Omega Cannon, but gun is outside mine.  Aborting!\n"));
591                         return;
592                 }
593                 fq.p0                                           = firing_pos;
594                 fq.p1                                           = &goal_pos;
595                 fq.rad                                  = 0;
596                 fq.thisobjnum                   = parent_objp-Objects;
597                 fq.ignore_obj_list      = NULL;
598                 fq.flags                                        = FQ_IGNORE_POWERUPS | FQ_TRANSPOINT | FQ_CHECK_OBJS;           //what about trans walls???
599
600                 fate = find_vector_intersection(&fq, &hit_data);
601                 if (fate != HIT_NONE) {
602                         Assert(hit_data.hit_seg != -1);         //      How can this be?  We went from inside the mine to outside without hitting anything?
603                         goal_pos = hit_data.hit_pnt;
604                 }
605         } else
606                 goal_pos = Objects[lock_objnum].pos;
607
608         //      This is where we create a pile of omega blobs!
609         create_omega_blobs(firing_segnum, firing_pos, &goal_pos, parent_objp);
610
611 }
612
613 // ---------------------------------------------------------------------------------
614 // Initializes a laser after Fire is pressed 
615 //      Returns object number.
616 int Laser_create_new( vms_vector * direction, vms_vector * position, int segnum, int parent, int weapon_type, int make_sound )
617 {
618         int objnum;
619         object *obj;
620         fix parent_speed, weapon_speed;
621         fix volume;
622         fix laser_length=0;
623
624         Assert( weapon_type < N_weapon_types );
625
626         if ( (weapon_type<0) || (weapon_type>=N_weapon_types) )
627                 weapon_type = 0;
628
629         //      Don't let homing blobs make muzzle flash.
630         if (Objects[parent].type == OBJ_ROBOT)
631                 do_muzzle_stuff(segnum, position);
632
633         objnum = create_weapon_object(weapon_type,segnum,position);
634
635         if ( objnum < 0 ) {
636                 mprintf((1, "Can't create laser - Out of objects!\n" ));
637                 return -1;
638         }
639
640         obj = &Objects[objnum];
641
642         //      Do the special Omega Cannon stuff.  Then return on account of everything that follows does
643         //      not apply to the Omega Cannon.
644         if (weapon_type == OMEGA_ID) {
645                 // Create orientation matrix for tracking purposes.
646                 vm_vector_2_matrix( &obj->orient, direction, &Objects[parent].orient.uvec ,NULL);
647
648                 if (( &Objects[parent] != Viewer ) && (Objects[parent].type != OBJ_WEAPON))     {
649                         // Muzzle flash         
650                         if (Weapon_info[obj->id].flash_vclip > -1 )
651                                 object_create_muzzle_flash( obj->segnum, &obj->pos, Weapon_info[obj->id].flash_size, Weapon_info[obj->id].flash_vclip );
652                 }
653
654                 do_omega_stuff(&Objects[parent], position, obj);
655
656                 return objnum;
657         }
658
659         if (Objects[parent].type == OBJ_PLAYER) {
660                 if (weapon_type == FUSION_ID) {
661
662                         if (Fusion_charge <= 0)
663                                 obj->ctype.laser_info.multiplier = F1_0;
664                         else if (Fusion_charge <= 4*F1_0)
665                                 obj->ctype.laser_info.multiplier = F1_0 + Fusion_charge/2;
666                         else
667                                 obj->ctype.laser_info.multiplier = 4*F1_0;
668
669                 } else if ((weapon_type >= LASER_ID && weapon_type <= MAX_SUPER_LASER_LEVEL) && (Players[Objects[parent].id].flags & PLAYER_FLAGS_QUAD_LASERS))
670                         obj->ctype.laser_info.multiplier = F1_0*3/4;
671                 else if (weapon_type == GUIDEDMISS_ID) {
672                         if (parent==Players[Player_num].objnum) {
673                                 Guided_missile[Player_num]= obj;
674                                 Guided_missile_sig[Player_num] = obj->signature;
675                                 if (Newdemo_state==ND_STATE_RECORDING)
676                                         newdemo_record_guided_start();
677                         }
678                 }
679         }
680
681         //      Make children of smart bomb bounce so if they hit a wall right away, they
682         //      won't detonate.  The frame interval code will clear this bit after 1/2 second.
683         if ((weapon_type == PLAYER_SMART_HOMING_ID) || (weapon_type == SMART_MINE_HOMING_ID) || (weapon_type == ROBOT_SMART_HOMING_ID) || (weapon_type == ROBOT_SMART_MINE_HOMING_ID) || (weapon_type == EARTHSHAKER_MEGA_ID))
684                 obj->mtype.phys_info.flags |= PF_BOUNCE;
685
686         if (Weapon_info[weapon_type].render_type == WEAPON_RENDER_POLYMODEL)
687                 laser_length = Polygon_models[obj->rtype.pobj_info.model_num].rad * 2;
688
689         if (weapon_type == FLARE_ID)
690                 obj->mtype.phys_info.flags |= PF_STICK;         //this obj sticks to walls
691         
692         obj->shields = Weapon_info[obj->id].strength[Difficulty_level];
693         
694         // Fill in laser-specific data
695
696         obj->lifeleft                                                   = Weapon_info[obj->id].lifetime;
697         obj->ctype.laser_info.parent_type               = Objects[parent].type;
698         obj->ctype.laser_info.parent_signature = Objects[parent].signature;
699         obj->ctype.laser_info.parent_num                        = parent;
700
701         //      Assign parent type to highest level creator.  This propagates parent type down from
702         //      the original creator through weapons which create children of their own (ie, smart missile)
703         if (Objects[parent].type == OBJ_WEAPON) {
704                 int     highest_parent = parent;
705                 int     count;
706
707                 count = 0;
708                 while ((count++ < 10) && (Objects[highest_parent].type == OBJ_WEAPON)) {
709                         int     next_parent;
710
711                         next_parent = Objects[highest_parent].ctype.laser_info.parent_num;
712                         if (Objects[next_parent].signature != Objects[highest_parent].ctype.laser_info.parent_signature)
713                                 break;  //      Probably means parent was killed.  Just continue.
714
715                         if (next_parent == highest_parent) {
716                                 Int3(); //      Hmm, object is parent of itself.  This would seem to be bad, no?
717                                 break;
718                         }
719
720                         highest_parent = next_parent;
721
722                         obj->ctype.laser_info.parent_num                        = highest_parent;
723                         obj->ctype.laser_info.parent_type               = Objects[highest_parent].type;
724                         obj->ctype.laser_info.parent_signature = Objects[highest_parent].signature;
725                 }
726         }
727
728         // Create orientation matrix so we can look from this pov
729         //      Homing missiles also need an orientation matrix so they know if they can make a turn.
730         if ((obj->render_type == RT_POLYOBJ) || (Weapon_info[obj->id].homing_flag))
731                 vm_vector_2_matrix( &obj->orient,direction, &Objects[parent].orient.uvec ,NULL);
732
733         if (( &Objects[parent] != Viewer ) && (Objects[parent].type != OBJ_WEAPON))     {
734                 // Muzzle flash         
735                 if (Weapon_info[obj->id].flash_vclip > -1 )
736                         object_create_muzzle_flash( obj->segnum, &obj->pos, Weapon_info[obj->id].flash_size, Weapon_info[obj->id].flash_vclip );
737         }
738
739         volume = F1_0;
740         if (Weapon_info[obj->id].flash_sound > -1 )     {
741                 if (make_sound) {
742                         if ( parent == (Viewer-Objects) )       {
743                                 if (weapon_type == VULCAN_ID)   // Make your own vulcan gun  1/2 as loud.
744                                         volume = F1_0 / 2;
745                                 digi_play_sample( Weapon_info[obj->id].flash_sound, volume );
746                         } else {
747                                 digi_link_sound_to_pos( Weapon_info[obj->id].flash_sound, obj->segnum, 0, &obj->pos, 0, volume );
748                         }
749                 }
750         }
751
752         //      Fire the laser from the gun tip so that the back end of the laser bolt is at the gun tip.
753         // Move 1 frame, so that the end-tip of the laser is touching the gun barrel.
754         // This also jitters the laser a bit so that it doesn't alias.
755         //      Don't do for weapons created by weapons.
756         if ((Objects[parent].type == OBJ_PLAYER) && (Weapon_info[weapon_type].render_type != WEAPON_RENDER_NONE) && (weapon_type != FLARE_ID)) {
757                 vms_vector      end_pos;
758                 int                     end_segnum;
759
760                 vm_vec_scale_add( &end_pos, &obj->pos, direction, Laser_offset+(laser_length/2) );
761                 end_segnum = find_point_seg(&end_pos, obj->segnum);
762                 if (end_segnum != obj->segnum) {
763                         // mprintf(0, "Warning: Laser tip not in same segment as player.\n");
764                         if (end_segnum != -1) {
765                                 obj->pos = end_pos;
766                                 obj_relink(obj-Objects, end_segnum);
767                         } else
768                                 mprintf((0, "Warning: Laser tip outside mine.  Laser not being moved to end of gun.\n"));
769                 } else
770                         obj->pos = end_pos;
771         }
772
773         //      Here's where to fix the problem with objects which are moving backwards imparting higher velocity to their weaponfire.
774         //      Find out if moving backwards.
775         if ((weapon_type == PROXIMITY_ID) || (weapon_type == SUPERPROX_ID)) {
776                 parent_speed = vm_vec_mag_quick(&Objects[parent].mtype.phys_info.velocity);
777                 if (vm_vec_dot(&Objects[parent].mtype.phys_info.velocity, &Objects[parent].orient.fvec) < 0)
778                         parent_speed = -parent_speed;
779         } else
780                 parent_speed = 0;
781
782         weapon_speed = Weapon_info[obj->id].speed[Difficulty_level];
783         if (Weapon_info[obj->id].speedvar != 128) {
784                 fix     randval;
785
786                 //      Get a scale factor between speedvar% and 1.0.
787                 randval = F1_0 - ((d_rand() * Weapon_info[obj->id].speedvar) >> 6);
788                 weapon_speed = fixmul(weapon_speed, randval);
789         }
790
791         //      Ugly hack (too bad we're on a deadline), for homing missiles dropped by smart bomb, start them out slower.
792         if ((obj->id == PLAYER_SMART_HOMING_ID) || (obj->id == SMART_MINE_HOMING_ID) || (obj->id == ROBOT_SMART_HOMING_ID) || (obj->id == ROBOT_SMART_MINE_HOMING_ID) || (obj->id == EARTHSHAKER_MEGA_ID))
793                 weapon_speed /= 4;
794
795         if (Weapon_info[obj->id].thrust != 0)
796                 weapon_speed /= 2;
797
798         vm_vec_copy_scale( &obj->mtype.phys_info.velocity, direction, weapon_speed + parent_speed );
799
800         //      Set thrust 
801         if (Weapon_info[weapon_type].thrust != 0) {
802                 obj->mtype.phys_info.thrust = obj->mtype.phys_info.velocity;
803                 vm_vec_scale(&obj->mtype.phys_info.thrust, fixdiv(Weapon_info[obj->id].thrust, weapon_speed+parent_speed));
804         }
805
806         if ((obj->type == OBJ_WEAPON) && (obj->id == FLARE_ID))
807                 obj->lifeleft += (d_rand()-16384) << 2;         //      add in -2..2 seconds
808
809         //      mprintf( 0, "Weapon speed = %.1f (%.1f)\n", f2fl(Weapon_info[obj->id].speed[Difficulty_level] + parent_speed), f2fl(parent_speed) );
810
811         return objnum;
812 }
813
814 //      -----------------------------------------------------------------------------------------------------------
815 //      Calls Laser_create_new, but takes care of the segment and point computation for you.
816 int Laser_create_new_easy( vms_vector * direction, vms_vector * position, int parent, int weapon_type, int make_sound )
817 {
818         fvi_query       fq;
819         fvi_info                hit_data;
820         object          *pobjp = &Objects[parent];
821         int                     fate;
822
823         //      Find segment containing laser fire position.  If the robot is straddling a segment, the position from
824         //      which it fires may be in a different segment, which is bad news for find_vector_intersection.  So, cast
825         //      a ray from the object center (whose segment we know) to the laser position.  Then, in the call to Laser_create_new
826         //      use the data returned from this call to find_vector_intersection.
827         //      Note that while find_vector_intersection is pretty slow, it is not terribly slow if the destination point is
828         //      in the same segment as the source point.
829
830         fq.p0                                           = &pobjp->pos;
831         fq.startseg                             = pobjp->segnum;
832         fq.p1                                           = position;
833         fq.rad                                  = 0;
834         fq.thisobjnum                   = pobjp-Objects;
835         fq.ignore_obj_list      = NULL;
836         fq.flags                                        = FQ_TRANSWALL | FQ_CHECK_OBJS;         //what about trans walls???
837
838         fate = find_vector_intersection(&fq, &hit_data);
839         if (fate != HIT_NONE  || hit_data.hit_seg==-1) {
840                 mprintf((1, "Warning: Laser from parent=%i stuck in wall or object, didn't fire!\n", parent));
841                 return -1;
842         }
843
844         return Laser_create_new( direction, &hit_data.hit_pnt, hit_data.hit_seg, parent, weapon_type, make_sound );
845
846 }
847
848 int             Muzzle_queue_index = 0;
849
850 muzzle_info             Muzzle_data[MUZZLE_QUEUE_MAX];
851
852 //      -----------------------------------------------------------------------------------------------------------
853 //      Determine if two objects are on a line of sight.  If so, return true, else return false.
854 //      Calls fvi.
855 int object_to_object_visibility(object *obj1, object *obj2, int trans_type)
856 {
857         fvi_query       fq;
858         fvi_info                hit_data;
859         int                     fate;
860
861         fq.p0                                           = &obj1->pos;
862         fq.startseg                             = obj1->segnum;
863         fq.p1                                           = &obj2->pos;
864         fq.rad                                  = 0x10;
865         fq.thisobjnum                   = obj1-Objects;
866         fq.ignore_obj_list      = NULL;
867         fq.flags                                        = trans_type;
868
869         fate = find_vector_intersection(&fq, &hit_data);
870
871         if (fate == HIT_WALL)
872                 return 0;
873         else if (fate == HIT_NONE)
874                 return 1;
875         else
876                 Int3();         //      Contact Mike: Oops, what happened?  What is fate?
877                                                 // 2 = hit object (impossible), 3 = bad starting point (bad)
878
879         return 0;
880 }
881
882 fix     Min_trackable_dot = MIN_TRACKABLE_DOT;
883
884 //      -----------------------------------------------------------------------------------------------------------
885 //      Return true if weapon *tracker is able to track object Objects[track_goal], else return false.
886 //      In order for the object to be trackable, it must be within a reasonable turning radius for the missile
887 //      and it must not be obstructed by a wall.
888 int object_is_trackable(int track_goal, object *tracker, fix *dot)
889 {
890         vms_vector      vector_to_goal;
891         object          *objp;
892
893         if (track_goal == -1)
894                 return 0;
895
896         if (Game_mode & GM_MULTI_COOP)
897                 return 0;
898
899         objp = &Objects[track_goal];
900
901         //      Don't track player if he's cloaked.
902         if ((track_goal == Players[Player_num].objnum) && (Players[Player_num].flags & PLAYER_FLAGS_CLOAKED))
903                 return 0;
904
905         //      Can't track AI object if he's cloaked.
906         if (objp->type == OBJ_ROBOT) {
907                 if (objp->ctype.ai_info.CLOAKED)
908                         return 0;
909                 //      Your missiles don't track your escort.
910                 if (Robot_info[objp->id].companion)
911                         if (tracker->ctype.laser_info.parent_type == OBJ_PLAYER)
912                                 return 0;
913         }
914         vm_vec_sub(&vector_to_goal, &objp->pos, &tracker->pos);
915         vm_vec_normalize_quick(&vector_to_goal);
916         *dot = vm_vec_dot(&vector_to_goal, &tracker->orient.fvec);
917
918         if ((*dot < Min_trackable_dot) && (*dot > F1_0*9/10)) {
919                 // -- mprintf((0, "."));
920                 vm_vec_normalize(&vector_to_goal);
921                 *dot = vm_vec_dot(&vector_to_goal, &tracker->orient.fvec);
922         }
923
924 //mprintf((0, " OIT: dot=%5.2f ", f2fl(dot)));
925
926         // mprintf((0, "object_is_trackable: [%3i] %7.3f, min = %7.3f\n", track_goal, f2fl(dot), f2fl(Min_trackable_dot)));
927  
928         if (*dot >= Min_trackable_dot) {
929                 int     rval;
930                 //      dot is in legal range, now see if object is visible
931                 rval =  object_to_object_visibility(tracker, objp, FQ_TRANSWALL);
932 //mprintf((0, " TRACK "));
933                 return rval;
934         } else {
935 //mprintf((0, " LOST! "));
936                 return 0;
937         }
938
939 }
940
941 extern int Robots_kill_robots_cheat;
942
943 //      --------------------------------------------------------------------------------------------
944 int call_find_homing_object_complete(object *tracker, vms_vector *curpos)
945 {
946         if (Game_mode & GM_MULTI) {
947                 if (tracker->ctype.laser_info.parent_type == OBJ_PLAYER) {
948                         //      It's fired by a player, so if robots present, track robot, else track player.
949                         if (Game_mode & GM_MULTI_COOP)
950                                 return find_homing_object_complete( curpos, tracker, OBJ_ROBOT, -1);
951                         else
952                                 return find_homing_object_complete( curpos, tracker, OBJ_PLAYER, OBJ_ROBOT);
953                 } else {
954                         int     goal2_type = -1;
955
956                         if (Robots_kill_robots_cheat)
957                                 goal2_type = OBJ_ROBOT;
958                         Assert(tracker->ctype.laser_info.parent_type == OBJ_ROBOT);
959                         return find_homing_object_complete(curpos, tracker, OBJ_PLAYER, goal2_type);
960                 }               
961         } else
962                 return find_homing_object_complete( curpos, tracker, OBJ_ROBOT, -1);
963 }
964
965 //      --------------------------------------------------------------------------------------------
966 //      Find object to home in on.
967 //      Scan list of objects rendered last frame, find one that satisfies function of nearness to center and distance.
968 int find_homing_object(vms_vector *curpos, object *tracker)
969 {
970         int     i;
971         fix     max_dot = -F1_0*2;
972         int     best_objnum = -1;
973
974         //      Contact Mike: This is a bad and stupid thing.  Who called this routine with an illegal laser type??
975         Assert((Weapon_info[tracker->id].homing_flag) || (tracker->id == OMEGA_ID));
976
977         //      Find an object to track based on game mode (eg, whether in network play) and who fired it.
978
979         if (Game_mode & GM_MULTI)
980                 return call_find_homing_object_complete(tracker, curpos);
981         else {
982                 int     cur_min_trackable_dot;
983
984                 cur_min_trackable_dot = MIN_TRACKABLE_DOT;
985                 if ((tracker->type == OBJ_WEAPON) && (tracker->id == OMEGA_ID))
986                         cur_min_trackable_dot = OMEGA_MIN_TRACKABLE_DOT;
987
988                 //      Not in network mode.  If not fired by player, then track player.
989                 if (tracker->ctype.laser_info.parent_num != Players[Player_num].objnum) {
990                         if (!(Players[Player_num].flags & PLAYER_FLAGS_CLOAKED))
991                                 best_objnum = ConsoleObject - Objects;
992                 } else {
993                         int     window_num = -1;
994                         fix     dist, max_trackable_dist;
995
996                         //      Find the window which has the forward view.
997                         for (i=0; i<MAX_RENDERED_WINDOWS; i++)
998                                 if (Window_rendered_data[i].frame >= FrameCount-1)
999                                         if (Window_rendered_data[i].viewer == ConsoleObject)
1000                                                 if (!Window_rendered_data[i].rear_view) {
1001                                                         window_num = i;
1002                                                         break;
1003                                                 }
1004
1005                         //      Couldn't find suitable view from this frame, so do complete search.
1006                         if (window_num == -1) {
1007                                 mprintf((0, "Note: Calling find_homing_object_complete because no suitable rendered window.\n"));
1008                                 return call_find_homing_object_complete(tracker, curpos);
1009                         }
1010
1011                         max_trackable_dist = MAX_TRACKABLE_DIST;
1012                         if (tracker->id == OMEGA_ID)
1013                                 max_trackable_dist = OMEGA_MAX_TRACKABLE_DIST;
1014
1015                         //      Not in network mode and fired by player.
1016                         for (i=Window_rendered_data[window_num].num_objects-1; i>=0; i--) {
1017                                 fix                     dot; //, dist;
1018                                 vms_vector      vec_to_curobj;
1019                                 int                     objnum = Window_rendered_data[window_num].rendered_objects[i];
1020                                 object          *curobjp = &Objects[objnum];
1021
1022                                 if (objnum == Players[Player_num].objnum)
1023                                         continue;
1024
1025                                 //      Can't track AI object if he's cloaked.
1026                                 if (curobjp->type == OBJ_ROBOT) {
1027                                         if (curobjp->ctype.ai_info.CLOAKED)
1028                                                 continue;
1029
1030                                         //      Your missiles don't track your escort.
1031                                         if (Robot_info[curobjp->id].companion)
1032                                                 if (tracker->ctype.laser_info.parent_type == OBJ_PLAYER)
1033                                                         continue;
1034                                 }
1035
1036                                 vm_vec_sub(&vec_to_curobj, &curobjp->pos, curpos);
1037                                 dist = vm_vec_normalize_quick(&vec_to_curobj);
1038                                 if (dist < max_trackable_dist) {
1039                                         dot = vm_vec_dot(&vec_to_curobj, &tracker->orient.fvec);
1040
1041                                         // mprintf(0, "Object %i: dist = %7.3f, dot = %7.3f\n", objnum, f2fl(dist), f2fl(dot));
1042
1043                                         //      Note: This uses the constant, not-scaled-by-frametime value, because it is only used
1044                                         //      to determine if an object is initially trackable.  find_homing_object is called on subsequent
1045                                         //      frames to determine if the object remains trackable.
1046                                         // mprintf((0, "find_homing_object:  [%3i] %7.3f, min = %7.3f\n", curobjp-Objects, f2fl(dot), f2fl(MIN_TRACKABLE_DOT)));
1047                                         if (dot > cur_min_trackable_dot) {
1048                                                 if (dot > max_dot) {
1049                                                         if (object_to_object_visibility(tracker, &Objects[objnum], FQ_TRANSWALL)) {
1050                                                                 max_dot = dot;
1051                                                                 best_objnum = objnum;
1052                                                         }
1053                                                 }
1054                                         } else if (dot > F1_0 - (F1_0 - cur_min_trackable_dot)*2) {
1055                                                 vm_vec_normalize(&vec_to_curobj);
1056                                                 dot = vm_vec_dot(&vec_to_curobj, &tracker->orient.fvec);
1057                                                 if (dot > cur_min_trackable_dot) {
1058                                                         if (dot > max_dot) {
1059                                                                 if (object_to_object_visibility(tracker, &Objects[objnum], FQ_TRANSWALL)) {
1060                                                                         max_dot = dot;
1061                                                                         best_objnum = objnum;
1062                                                                 }
1063                                                         }
1064                                                 }
1065                                         }
1066                                 }
1067                         }
1068                 }
1069         }
1070
1071         // mprintf(0, "Selecting object #%i\n=n", best_objnum);
1072
1073         return best_objnum;
1074 }
1075
1076 //      --------------------------------------------------------------------------------------------
1077 //      Find object to home in on.
1078 //      Scan list of objects rendered last frame, find one that satisfies function of nearness to center and distance.
1079 //      Can track two kinds of objects.  If you are only interested in one type, set track_obj_type2 to NULL
1080 //      Always track proximity bombs.  --MK, 06/14/95
1081 //      Make homing objects not track parent's prox bombs.
1082 int find_homing_object_complete(vms_vector *curpos, object *tracker, int track_obj_type1, int track_obj_type2)
1083 {
1084         int     objnum;
1085         fix     max_dot = -F1_0*2;
1086         int     best_objnum = -1;
1087         fix     max_trackable_dist;
1088         fix     min_trackable_dot;
1089
1090         //      Contact Mike: This is a bad and stupid thing.  Who called this routine with an illegal laser type??
1091         Assert((Weapon_info[tracker->id].homing_flag) || (tracker->id == OMEGA_ID));
1092
1093         max_trackable_dist = MAX_TRACKABLE_DIST;
1094         min_trackable_dot = MIN_TRACKABLE_DOT;
1095
1096         if (tracker->id == OMEGA_ID) {
1097                 max_trackable_dist = OMEGA_MAX_TRACKABLE_DIST;
1098                 min_trackable_dot = OMEGA_MIN_TRACKABLE_DOT;
1099         }
1100
1101         for (objnum=0; objnum<=Highest_object_index; objnum++) {
1102                 int                     is_proximity = 0;
1103                 fix                     dot, dist;
1104                 vms_vector      vec_to_curobj;
1105                 object          *curobjp = &Objects[objnum];
1106
1107                 if ((curobjp->type != track_obj_type1) && (curobjp->type != track_obj_type2))
1108                 {
1109                         if ((curobjp->type == OBJ_WEAPON) && ((curobjp->id == PROXIMITY_ID) || (curobjp->id == SUPERPROX_ID))) {
1110                                 if (curobjp->ctype.laser_info.parent_signature != tracker->ctype.laser_info.parent_signature)
1111                                         is_proximity = 1;
1112                                 else
1113                                         continue;
1114                         } else
1115                                 continue;
1116                 }
1117
1118                 if (objnum == tracker->ctype.laser_info.parent_num) // Don't track shooter
1119                         continue;
1120
1121                 //      Don't track cloaked players.
1122                 if (curobjp->type == OBJ_PLAYER)
1123                 {
1124                         if (Players[curobjp->id].flags & PLAYER_FLAGS_CLOAKED)
1125                                 continue;
1126                         // Don't track teammates in team games
1127                         #ifdef NETWORK
1128                         if ((Game_mode & GM_TEAM) && (Objects[tracker->ctype.laser_info.parent_num].type == OBJ_PLAYER) && (get_team(curobjp->id) == get_team(Objects[tracker->ctype.laser_info.parent_num].id)))
1129                                 continue;
1130                         #endif
1131                 }
1132
1133                 //      Can't track AI object if he's cloaked.
1134                 if (curobjp->type == OBJ_ROBOT) {
1135                         if (curobjp->ctype.ai_info.CLOAKED)
1136                                 continue;
1137
1138                         //      Your missiles don't track your escort.
1139                         if (Robot_info[curobjp->id].companion)
1140                                 if (tracker->ctype.laser_info.parent_type == OBJ_PLAYER)
1141                                         continue;
1142                 }
1143
1144                 vm_vec_sub(&vec_to_curobj, &curobjp->pos, curpos);
1145                 dist = vm_vec_mag_quick(&vec_to_curobj);
1146
1147                 if (dist < max_trackable_dist) {
1148                         vm_vec_normalize_quick(&vec_to_curobj);
1149                         dot = vm_vec_dot(&vec_to_curobj, &tracker->orient.fvec);
1150                         if (is_proximity)
1151                                 dot = ((dot << 3) + dot) >> 3;          //      I suspect Watcom would be too stupid to figure out the obvious...
1152
1153                         //      Note: This uses the constant, not-scaled-by-frametime value, because it is only used
1154                         //      to determine if an object is initially trackable.  find_homing_object is called on subsequent
1155                         //      frames to determine if the object remains trackable.
1156                         // mprintf((0, "fho_complete:        [%3i] %7.3f, min = %7.3f\n", curobjp-Objects, f2fl(dot), f2fl(MIN_TRACKABLE_DOT)));
1157                         if (dot > min_trackable_dot) {
1158                                 // mprintf(0, "Object %i: dist = %7.3f, dot = %7.3f\n", objnum, f2fl(dist), f2fl(dot));
1159                                 if (dot > max_dot) {
1160                                         if (object_to_object_visibility(tracker, &Objects[objnum], FQ_TRANSWALL)) {
1161                                                 max_dot = dot;
1162                                                 best_objnum = objnum;
1163                                         }
1164                                 }
1165                         }
1166                 }
1167
1168         }
1169
1170         // -- mprintf((0, "Selecting object #%i in find_homing_object_complete\n\n", best_objnum));
1171
1172         return best_objnum;
1173 }
1174
1175 //      ------------------------------------------------------------------------------------------------------------
1176 //      See if legal to keep tracking currently tracked object.  If not, see if another object is trackable.  If not, return -1,
1177 //      else return object number of tracking object.
1178 //      Computes and returns a fairly precise dot product.
1179 int track_track_goal(int track_goal, object *tracker, fix *dot)
1180 {
1181         //      Every 8 frames for each object, scan all objects.
1182         if (object_is_trackable(track_goal, tracker, dot) && ((((tracker-Objects) ^ FrameCount) % 8) != 0)) {
1183                 //mprintf((0, "ttg: QO"));
1184                 return track_goal;
1185         } else if ((((tracker-Objects) ^ FrameCount) % 4) == 0) {
1186                 int     rval = -2;
1187
1188                 //      If player fired missile, then search for an object, if not, then give up.
1189                 if (Objects[tracker->ctype.laser_info.parent_num].type == OBJ_PLAYER) {
1190                         int     goal_type;
1191
1192                         if (track_goal == -1) 
1193                         {
1194                                 if (Game_mode & GM_MULTI)
1195                                 {
1196                                         if (Game_mode & GM_MULTI_COOP)
1197                                                 rval = find_homing_object_complete( &tracker->pos, tracker, OBJ_ROBOT, -1);
1198                                         else if (Game_mode & GM_MULTI_ROBOTS)           //      Not cooperative, if robots, track either robot or player
1199                                                 rval = find_homing_object_complete( &tracker->pos, tracker, OBJ_PLAYER, OBJ_ROBOT);
1200                                         else            //      Not cooperative and no robots, track only a player
1201                                                 rval = find_homing_object_complete( &tracker->pos, tracker, OBJ_PLAYER, -1);
1202                                 }
1203                                 else
1204                                         rval = find_homing_object_complete(&tracker->pos, tracker, OBJ_PLAYER, OBJ_ROBOT);
1205                         } 
1206                         else 
1207                         {
1208                                 goal_type = Objects[tracker->ctype.laser_info.track_goal].type;
1209                                 if ((goal_type == OBJ_PLAYER) || (goal_type == OBJ_ROBOT))
1210                                         rval = find_homing_object_complete(&tracker->pos, tracker, goal_type, -1);
1211                                 else
1212                                         rval = -1;
1213                         }
1214                 } 
1215                 else {
1216                         int     goal_type, goal2_type = -1;
1217
1218                         if (Robots_kill_robots_cheat)
1219                                 goal2_type = OBJ_ROBOT;
1220
1221                         if (track_goal == -1)
1222                                 rval = find_homing_object_complete(&tracker->pos, tracker, OBJ_PLAYER, goal2_type);
1223                         else {
1224                                 goal_type = Objects[tracker->ctype.laser_info.track_goal].type;
1225                                 rval = find_homing_object_complete(&tracker->pos, tracker, goal_type, goal2_type);
1226                         }
1227                 }
1228
1229                 Assert(rval != -2);             //      This means it never got set which is bad!  Contact Mike.
1230                 return rval;
1231         }
1232
1233 //if (track_goal != -1)
1234 // mprintf((0, "Object %i not tracking anything.\n", tracker-Objects));
1235
1236         return -1;
1237 }
1238
1239 //-------------- Initializes a laser after Fire is pressed -----------------
1240
1241 void Laser_player_fire_spread_delay(object *obj, int laser_type, int gun_num, fix spreadr, fix spreadu, fix delay_time, int make_sound, int harmless)
1242 {
1243         int                     LaserSeg, Fate; 
1244         vms_vector      LaserPos, LaserDir;
1245         fvi_query       fq;
1246         fvi_info                hit_data;
1247         vms_vector      gun_point, *pnt;
1248         vms_matrix      m;
1249         int                     objnum;
1250
1251         create_awareness_event(obj, PA_WEAPON_WALL_COLLISION);
1252
1253         // Find the initial position of the laser
1254         pnt = &Player_ship->gun_points[gun_num];
1255
1256         vm_copy_transpose_matrix(&m,&obj->orient);
1257         vm_vec_rotate(&gun_point,pnt,&m);
1258
1259         vm_vec_add(&LaserPos,&obj->pos,&gun_point);
1260
1261         //      If supposed to fire at a delayed time (delay_time), then move this point backwards.
1262         if (delay_time)
1263                 vm_vec_scale_add2(&LaserPos, &obj->orient.fvec, -fixmul(delay_time, Weapon_info[laser_type].speed[Difficulty_level]));
1264
1265 //      do_muzzle_stuff(obj, &Pos);
1266
1267         //--------------- Find LaserPos and LaserSeg ------------------
1268         fq.p0                                           = &obj->pos;
1269         fq.startseg                             = obj->segnum;
1270         fq.p1                                           = &LaserPos;
1271         fq.rad                                  = 0x10;
1272         fq.thisobjnum                   = obj-Objects;
1273         fq.ignore_obj_list      = NULL;
1274         fq.flags                                        = FQ_CHECK_OBJS | FQ_IGNORE_POWERUPS;
1275
1276         Fate = find_vector_intersection(&fq, &hit_data);
1277
1278         LaserSeg = hit_data.hit_seg;
1279
1280         if (LaserSeg == -1)             //some sort of annoying error
1281                 return;
1282
1283         //SORT OF HACK... IF ABOVE WAS CORRECT THIS WOULDNT BE NECESSARY.
1284         if ( vm_vec_dist_quick(&LaserPos, &obj->pos) > 0x50000 )
1285                 return;
1286         
1287         if (Fate==HIT_WALL) {
1288                 if (delay_time)
1289                         mprintf((0, "Your DELAYED laser is stuck thru a wall!\n" ));
1290                 else
1291                         mprintf((0, "Your laser is stuck thru a wall!\n" ));
1292                 return;         
1293         }
1294
1295         if (Fate==HIT_OBJECT) {
1296 //              if ( Objects[hit_data.hit_object].type == OBJ_ROBOT )
1297 //                      Objects[hit_data.hit_object].flags |= OF_SHOULD_BE_DEAD;
1298                 mprintf((0, "Your laser is stuck in an object!\n" ));
1299 //              if ( Objects[hit_data.hit_object].type != OBJ_POWERUP )
1300 //                      return;         
1301         //as of 12/6/94, we don't care if the laser is stuck in an object. We
1302         //just fire away normally
1303         }
1304
1305         //      Now, make laser spread out.
1306         LaserDir = obj->orient.fvec;
1307         if ((spreadr != 0) || (spreadu != 0)) {
1308                 vm_vec_scale_add2(&LaserDir, &obj->orient.rvec, spreadr);
1309                 vm_vec_scale_add2(&LaserDir, &obj->orient.uvec, spreadu);
1310         }
1311
1312         objnum = Laser_create_new( &LaserDir, &LaserPos, LaserSeg, obj-Objects, laser_type, make_sound );
1313
1314         //      Omega cannon is a hack, not surprisingly.  Don't want to do the rest of this stuff.
1315         if (laser_type == OMEGA_ID)
1316                 return;
1317
1318         if (objnum == -1)
1319                 return;
1320
1321 #ifdef NETWORK
1322         if (laser_type==GUIDEDMISS_ID && Multi_is_guided) {
1323                 mprintf ((0,"Guided missile %s activated!\n",Players[obj->id].callsign));
1324                 Guided_missile[obj->id]=&Objects[objnum];
1325         }
1326
1327         Multi_is_guided=0;
1328 #endif
1329
1330         if (laser_type == CONCUSSION_ID ||
1331                          laser_type == HOMING_ID ||
1332                          laser_type == SMART_ID ||
1333                          laser_type == MEGA_ID ||
1334                          laser_type == FLASH_ID ||
1335                          //laser_type == GUIDEDMISS_ID ||
1336                          //laser_type == SUPERPROX_ID ||
1337                          laser_type == MERCURY_ID ||
1338                          laser_type == EARTHSHAKER_ID)
1339                 if (Missile_viewer == NULL && obj->id==Player_num)
1340                         Missile_viewer = &Objects[objnum];
1341
1342         //      If this weapon is supposed to be silent, set that bit!
1343         if (!make_sound)
1344                 Objects[objnum].flags |= OF_SILENT;
1345
1346         //      If this weapon is supposed to be silent, set that bit!
1347         if (harmless)
1348                 Objects[objnum].flags |= OF_HARMLESS;
1349
1350         //      If the object firing the laser is the player, then indicate the laser object so robots can dodge.
1351         //      New by MK on 6/8/95, don't let robots evade proximity bombs, thereby decreasing uselessness of bombs.
1352         if ((obj == ConsoleObject) && ((Objects[objnum].id != PROXIMITY_ID) && (Objects[objnum].id != SUPERPROX_ID)))
1353                 Player_fired_laser_this_frame = objnum;
1354
1355         if (Weapon_info[laser_type].homing_flag) {
1356                 if (obj == ConsoleObject)
1357                 {
1358                         Objects[objnum].ctype.laser_info.track_goal = find_homing_object(&LaserPos, &Objects[objnum]);
1359                         #ifdef NETWORK
1360                         Network_laser_track = Objects[objnum].ctype.laser_info.track_goal;
1361                         #endif
1362                 }
1363                 #ifdef NETWORK
1364                 else // Some other player shot the homing thing
1365                 {
1366                         Assert(Game_mode & GM_MULTI);
1367                         Objects[objnum].ctype.laser_info.track_goal = Network_laser_track;
1368                 }
1369                 #endif
1370 //              mprintf((0, "Selecting object #%i in find_homing_object_complete\n", Network_laser_track));
1371         }
1372 }
1373
1374 //      -----------------------------------------------------------------------------------------------------------
1375 void Laser_player_fire_spread(object *obj, int laser_type, int gun_num, fix spreadr, fix spreadu, int make_sound, int harmless)
1376 {
1377         Laser_player_fire_spread_delay(obj, laser_type, gun_num, spreadr, spreadu, 0, make_sound, harmless);
1378 }
1379
1380
1381 //      -----------------------------------------------------------------------------------------------------------
1382 void Laser_player_fire(object *obj, int laser_type, int gun_num, int make_sound, int harmless)
1383 {
1384         Laser_player_fire_spread(obj, laser_type, gun_num, 0, 0, make_sound, harmless);
1385 }
1386
1387 //      -----------------------------------------------------------------------------------------------------------
1388 void Flare_create(object *obj)
1389 {
1390         fix     energy_usage;
1391
1392         energy_usage = Weapon_info[FLARE_ID].energy_usage;
1393
1394         if (Difficulty_level < 2)
1395                 energy_usage = fixmul(energy_usage, i2f(Difficulty_level+2)/4);
1396
1397 //      MK, 11/04/95: Allowed to fire flare even if no energy.
1398 // --   if (Players[Player_num].energy >= energy_usage) {
1399                 Players[Player_num].energy -= energy_usage;
1400
1401                 if (Players[Player_num].energy <= 0) {
1402                         Players[Player_num].energy = 0; 
1403                         // -- auto_select_weapon(0);
1404                 }
1405
1406                 Laser_player_fire( obj, FLARE_ID, 6, 1, 0);
1407
1408                 #ifdef NETWORK
1409                 if (Game_mode & GM_MULTI) {
1410                         Network_laser_fired = 1;
1411                         Network_laser_gun = FLARE_ADJUST;
1412                         Network_laser_flags = 0;
1413                         Network_laser_level = 0;
1414                 }
1415                 #endif
1416 // --   }
1417
1418 }
1419
1420 #define HOMING_MISSILE_SCALE    16
1421
1422 #define LIMIT_HOMERS    1
1423 #define HOMER_MAX_FPS   30
1424 #define HOMER_MIN_DELAY (1000 / HOMER_MAX_FPS)
1425
1426 //--------------------------------------------------------------------
1427 //      Set object *objp's orientation to (or towards if I'm ambitious) its velocity.
1428 void homing_missile_turn_towards_velocity(object *objp, vms_vector *norm_vel)
1429 {
1430         vms_vector      new_fvec;
1431
1432 #ifdef LIMIT_HOMERS
1433         static time_t   last_time = -1;
1434         static int nFrames = 1;
1435         time_t this_time, delta_time;
1436         fix frame_time;
1437         int fps;
1438         if (last_time == -1) {
1439                 last_time = clock ();
1440                 frame_time = FrameTime;
1441         } else {
1442                 nFrames++;
1443                 this_time = clock ();
1444                 delta_time = this_time - last_time;
1445                 if (delta_time < HOMER_MIN_DELAY) {
1446                         return;
1447                 } else {
1448                         fps = (1000 + delta_time / 2) / delta_time;
1449                         frame_time = fps ? (f1_0 + fps / 2) / fps : f1_0;
1450                         //                     frame_time /= nFrames;
1451                         nFrames = 0;
1452                         last_time = this_time;
1453                 }
1454         }
1455 #else
1456         fix frame_time = FrameTime;
1457 #endif
1458
1459         new_fvec = *norm_vel;
1460
1461         vm_vec_scale(&new_fvec, frame_time * HOMING_MISSILE_SCALE);
1462         vm_vec_add2(&new_fvec, &objp->orient.fvec);
1463         vm_vec_normalize_quick(&new_fvec);
1464
1465 //      if ((norm_vel->x == 0) && (norm_vel->y == 0) && (norm_vel->z == 0))
1466 //              return;
1467
1468         vm_vector_2_matrix(&objp->orient, &new_fvec, NULL, NULL);
1469 }
1470
1471 //-------------------------------------------------------------------------------------------
1472 //sequence this laser object for this _frame_ (underscores added here to aid MK in his searching!)
1473 void Laser_do_weapon_sequence(object *obj)
1474 {
1475         Assert(obj->control_type == CT_WEAPON);
1476
1477         //      Ok, this is a big hack by MK.
1478         //      If you want an object to last for exactly one frame, then give it a lifeleft of ONE_FRAME_TIME
1479         if (obj->lifeleft == ONE_FRAME_TIME) {
1480                 if (Game_mode & GM_MULTI)
1481                         obj->lifeleft = OMEGA_MULTI_LIFELEFT;
1482                 else
1483                         obj->lifeleft = 0;
1484                 obj->render_type = RT_NONE;
1485         }
1486
1487         if (obj->lifeleft < 0 ) {               // We died of old age
1488                 obj->flags |= OF_SHOULD_BE_DEAD;
1489                 if ( Weapon_info[obj->id].damage_radius )
1490                         explode_badass_weapon(obj,&obj->pos);
1491                 return;
1492         }
1493
1494         //delete weapons that are not moving
1495         if (    !((FrameCount ^ obj->signature) & 3) &&
1496                         (obj->id != FLARE_ID) &&
1497                         (Weapon_info[obj->id].speed[Difficulty_level] > 0) &&
1498                         (vm_vec_mag_quick(&obj->mtype.phys_info.velocity) < F2_0)) {
1499                 obj_delete(obj-Objects);
1500                 return;
1501         }
1502
1503         if ( obj->id == FUSION_ID ) {           //always set fusion weapon to max vel
1504
1505                 vm_vec_normalize_quick(&obj->mtype.phys_info.velocity);
1506
1507                 vm_vec_scale(&obj->mtype.phys_info.velocity, Weapon_info[obj->id].speed[Difficulty_level]);
1508         }
1509
1510 // --   //      The Super Spreadfire (Helix) blobs travel in a sinusoidal path.  That is accomplished
1511 // --   //      by modifying velocity (direction) in the frame interval.
1512 // --   if (obj->id == SSPREADFIRE_ID) {
1513 // --           fix     age, sinval, cosval;
1514 // --           vms_vector      p, newp;
1515 // --           fix     speed;
1516 // -- 
1517 // --           speed = vm_vec_mag_quick(&obj->phys_info.velocity);
1518 // -- 
1519 // --           age = Weapon_info[obj->id].lifetime - obj->lifeleft;
1520 // -- 
1521 // --           fix_fast_sincos(age, &sinval, &cosval);
1522 // -- 
1523 // --           //      Note: Below code assumes x=1, y=0.  Need to scale this for values around a circle for 5 helix positions.
1524 // --           p.x = cosval << 3;
1525 // --           p.y = sinval << 3;
1526 // --           p.z = 0;
1527 // -- 
1528 // --           vm_vec_rotate(&newp, &p, &obj->orient);
1529 // -- 
1530 // --           vm_vec_add(&goal_point, &obj->pos, &newp);
1531 // -- 
1532 // --           vm_vec_sub(&vec_to_goal, &goal_point, obj
1533 // --   }
1534
1535
1536         //      For homing missiles, turn towards target. (unless it's the guided missile)
1537         if (Weapon_info[obj->id].homing_flag && !(obj->id==GUIDEDMISS_ID && obj==Guided_missile[Objects[obj->ctype.laser_info.parent_num].id] && obj->signature==Guided_missile[Objects[obj->ctype.laser_info.parent_num].id]->signature)) {
1538                 vms_vector              vector_to_object, temp_vec;
1539                 fix                             dot=F1_0;
1540                 fix                             speed, max_speed;
1541
1542                 //      For first 1/2 second of life, missile flies straight.
1543                 if (obj->ctype.laser_info.creation_time + HOMING_MISSILE_STRAIGHT_TIME < GameTime) {
1544
1545                         int     track_goal = obj->ctype.laser_info.track_goal;
1546
1547                         //mprintf((0, "%5i: mtd=%5.2f", FrameCount, f2fl(Min_trackable_dot)));
1548
1549                         //      If it's time to do tracking, then it's time to grow up, stop bouncing and start exploding!.
1550                         if ((obj->id == ROBOT_SMART_MINE_HOMING_ID) || (obj->id == ROBOT_SMART_HOMING_ID) || (obj->id == SMART_MINE_HOMING_ID) || (obj->id == PLAYER_SMART_HOMING_ID) || (obj->id == EARTHSHAKER_MEGA_ID)) {
1551                                 // if (obj->mtype.phys_info.flags & PF_BOUNCE) mprintf(0, "Debouncing smart child %i\n", obj-Objects);
1552                                 obj->mtype.phys_info.flags &= ~PF_BOUNCE;
1553                         }
1554
1555                         //      Make sure the object we are tracking is still trackable.
1556                         track_goal = track_track_goal(track_goal, obj, &dot);
1557
1558                         //mprintf((0, " after ttg=%3i ", track_goal));
1559
1560                         if (track_goal == Players[Player_num].objnum) {
1561                                 fix     dist_to_player;
1562
1563                                 dist_to_player = vm_vec_dist_quick(&obj->pos, &Objects[track_goal].pos);
1564                                 if ((dist_to_player < Players[Player_num].homing_object_dist) || (Players[Player_num].homing_object_dist < 0))
1565                                         Players[Player_num].homing_object_dist = dist_to_player;
1566                                         
1567                         }
1568
1569                         if (track_goal != -1) {
1570                                 vm_vec_sub(&vector_to_object, &Objects[track_goal].pos, &obj->pos);
1571
1572                                 vm_vec_normalize_quick(&vector_to_object);
1573                                 temp_vec = obj->mtype.phys_info.velocity;
1574                                 speed = vm_vec_normalize_quick(&temp_vec);
1575                                 max_speed = Weapon_info[obj->id].speed[Difficulty_level];
1576                                 if (speed+F1_0 < max_speed) {
1577                                         speed += fixmul(max_speed, FrameTime/2);
1578                                         if (speed > max_speed)
1579                                                 speed = max_speed;
1580                                 }
1581
1582                                 // -- dot = vm_vec_dot(&temp_vec, &vector_to_object);
1583 //mprintf((0, " dot=%5.2f ", f2fl(dot)));
1584                                 vm_vec_add2(&temp_vec, &vector_to_object);
1585                                 //      The boss' smart children track better...
1586                                 if (Weapon_info[obj->id].render_type != WEAPON_RENDER_POLYMODEL)
1587                                         vm_vec_add2(&temp_vec, &vector_to_object);
1588                                 vm_vec_normalize_quick(&temp_vec);
1589                                 obj->mtype.phys_info.velocity = temp_vec;
1590                                 vm_vec_scale(&obj->mtype.phys_info.velocity, speed);
1591
1592                                 //      Subtract off life proportional to amount turned.
1593                                 //      For hardest turn, it will lose 2 seconds per second.
1594                                 {
1595                                         fix     lifelost, absdot;
1596                                 
1597                                         absdot = abs(F1_0 - dot);
1598                                 
1599                                         lifelost = fixmul(absdot*32, FrameTime);
1600                                         obj->lifeleft -= lifelost;
1601                                         // -- mprintf((0, "Missile %3i, dot = %7.3f life lost = %7.3f, life left = %7.3f\n", obj-Objects, f2fl(dot), f2fl(lifelost), f2fl(obj->lifeleft)));
1602                                 }
1603
1604                                 //      Only polygon objects have visible orientation, so only they should turn.
1605                                 if (Weapon_info[obj->id].render_type == WEAPON_RENDER_POLYMODEL)
1606                                         homing_missile_turn_towards_velocity(obj, &temp_vec);           //      temp_vec is normalized velocity.
1607                         }
1608                 }
1609
1610 //mprintf((0, "\n"));
1611         }
1612
1613         //      Make sure weapon is not moving faster than allowed speed.
1614         {
1615                 fix     weapon_speed;
1616
1617                 weapon_speed = vm_vec_mag_quick(&obj->mtype.phys_info.velocity);
1618                 if (weapon_speed > Weapon_info[obj->id].speed[Difficulty_level]) {
1619                         //      Only slow down if not allowed to move.  Makes sense, huh?  Allows proxbombs to get moved by physics force. --MK, 2/13/96
1620                         if (Weapon_info[obj->id].speed[Difficulty_level]) {
1621                                 fix     scale_factor;
1622
1623                                 scale_factor = fixdiv(Weapon_info[obj->id].speed[Difficulty_level], weapon_speed);
1624                                 vm_vec_scale(&obj->mtype.phys_info.velocity, scale_factor);
1625                         }
1626                 }
1627         }
1628 }
1629
1630 fix     Last_laser_fired_time = 0;
1631
1632 extern int Player_fired_laser_this_frame;
1633
1634 int     Zbonkers = 0;
1635
1636 //      --------------------------------------------------------------------------------------------------
1637 // Assumption: This is only called by the actual console player, not for network players
1638
1639 int do_laser_firing_player(void)
1640 {
1641         player  *plp = &Players[Player_num];
1642         fix             energy_used;
1643         int             ammo_used,primary_ammo;
1644         int             weapon_index;
1645         int             rval = 0;
1646         int             nfires = 1;
1647         fix             addval;
1648         static int Spreadfire_toggle=0;
1649         static int Helix_orientation = 0;
1650
1651         if (Player_is_dead)
1652                 return 0;
1653
1654         weapon_index = Primary_weapon_to_weapon_info[Primary_weapon];
1655         energy_used = Weapon_info[weapon_index].energy_usage;
1656         if (Primary_weapon == OMEGA_INDEX)
1657                 energy_used = 0;        //      Omega consumes energy when recharging, not when firing.
1658
1659         if (Difficulty_level < 2)
1660                 energy_used = fixmul(energy_used, i2f(Difficulty_level+2)/4);
1661
1662         //      MK, 01/26/96, Helix use 2x energy in multiplayer.  bitmaps.tbl parm should have been reduced for single player.
1663         if (weapon_index == HELIX_INDEX)
1664                 if (Game_mode & GM_MULTI)
1665                         energy_used *= 2;
1666
1667         ammo_used = Weapon_info[weapon_index].ammo_usage;
1668
1669         addval = 2*FrameTime;
1670         if (addval > F1_0)
1671                 addval = F1_0;
1672
1673         if ((Last_laser_fired_time + 2*FrameTime < GameTime) || (GameTime < Last_laser_fired_time))
1674                 Next_laser_fire_time = GameTime;
1675
1676         Last_laser_fired_time = GameTime;
1677
1678         primary_ammo = (Primary_weapon == GAUSS_INDEX)?(plp->primary_ammo[VULCAN_INDEX]):(plp->primary_ammo[Primary_weapon]);
1679
1680         if      (!((plp->energy >= energy_used) && (primary_ammo >= ammo_used)))
1681                 auto_select_weapon(0);          //      Make sure the player can fire from this weapon.
1682
1683 if (Zbonkers) {
1684         Zbonkers = 0;
1685         GameTime = 0;
1686 }
1687
1688         while (Next_laser_fire_time <= GameTime) {
1689                 if      ((plp->energy >= energy_used) && (primary_ammo >= ammo_used)) {
1690                         int     laser_level, flags;
1691
1692 //mprintf(0, ".");
1693                         if (Laser_rapid_fire!=0xBADA55)
1694                                 Next_laser_fire_time += Weapon_info[weapon_index].fire_wait;
1695                         else
1696                                 Next_laser_fire_time += F1_0/25;
1697
1698                         laser_level = Players[Player_num].laser_level;
1699         
1700                         flags = 0;
1701
1702                         if (Primary_weapon == SPREADFIRE_INDEX) {
1703                                 if (Spreadfire_toggle)
1704                                         flags |= LASER_SPREADFIRE_TOGGLED;
1705                                 Spreadfire_toggle = !Spreadfire_toggle;
1706                         }
1707
1708                         if (Primary_weapon == HELIX_INDEX) {
1709                                 Helix_orientation++;
1710                                 flags |= ((Helix_orientation & LASER_HELIX_MASK) << LASER_HELIX_SHIFT);
1711                         }
1712
1713                         if (Players[Player_num].flags & PLAYER_FLAGS_QUAD_LASERS)
1714                                 flags |= LASER_QUAD;
1715
1716                         rval += do_laser_firing(Players[Player_num].objnum, Primary_weapon, laser_level, flags, nfires);
1717
1718                         plp->energy -= (energy_used * rval) / Weapon_info[weapon_index].fire_count;
1719                         if (plp->energy < 0)
1720                                 plp->energy = 0;
1721
1722                         if ((Primary_weapon == VULCAN_INDEX) || (Primary_weapon == GAUSS_INDEX)) {
1723                                 if (ammo_used > plp->primary_ammo[VULCAN_INDEX])
1724                                         plp->primary_ammo[VULCAN_INDEX] = 0;
1725                                 else
1726                                         plp->primary_ammo[VULCAN_INDEX] -= ammo_used;
1727                         }
1728
1729                         auto_select_weapon(0);          //      Make sure the player can fire from this weapon.
1730
1731                 } else {
1732                         auto_select_weapon(0);          //      Make sure the player can fire from this weapon.
1733                         Next_laser_fire_time = GameTime;        //      Prevents shots-to-fire from building up.
1734                         break;  //      Couldn't fire weapon, so abort.
1735                 }
1736         }
1737 //mprintf(0, "  fires = %i\n", rval);
1738
1739         Global_laser_firing_count = 0;  
1740
1741         return rval;
1742 }
1743
1744 // -- #define   MAX_LIGHTNING_DISTANCE  (F1_0*300)
1745 // -- #define   MAX_LIGHTNING_BLOBS             16
1746 // -- #define   LIGHTNING_BLOB_DISTANCE (MAX_LIGHTNING_DISTANCE/MAX_LIGHTNING_BLOBS)
1747 // -- 
1748 // -- #define   LIGHTNING_BLOB_ID                       13
1749 // -- 
1750 // -- #define   LIGHTNING_TIME          (F1_0/4)
1751 // -- #define   LIGHTNING_DELAY (F1_0/8)
1752 // -- 
1753 // -- int       Lightning_gun_num = 1;
1754 // -- 
1755 // -- fix       Lightning_start_time = -F1_0*10, Lightning_last_time;
1756 // -- 
1757 // -- //        --------------------------------------------------------------------------------------------------
1758 // -- //        Return -1 if failed to create at least one blob.  Else return index of last blob created.
1759 // -- int create_lightning_blobs(vms_vector *direction, vms_vector *start_pos, int start_segnum, int parent)
1760 // -- {
1761 // --   int                     i;
1762 // --   fvi_query       fq;
1763 // --   fvi_info                hit_data;
1764 // --   vms_vector      end_pos;
1765 // --   vms_vector      norm_dir;
1766 // --   int                     fate;
1767 // --   int                     num_blobs;
1768 // --   vms_vector      tvec;
1769 // --   fix                     dist_to_hit_point;
1770 // --   vms_vector      point_pos, delta_pos;
1771 // --   int                     objnum;
1772 // --   vms_vector      *gun_pos;
1773 // --   vms_matrix      m;
1774 // --   vms_vector      gun_pos2;
1775 // -- 
1776 // --   if (Players[Player_num].energy > F1_0)
1777 // --           Players[Player_num].energy -= F1_0;
1778 // -- 
1779 // --   if (Players[Player_num].energy <= F1_0) {
1780 // --           Players[Player_num].energy = 0; 
1781 // --           auto_select_weapon(0);
1782 // --           return -1;
1783 // --   }
1784 // -- 
1785 // --   norm_dir = *direction;
1786 // -- 
1787 // --   vm_vec_normalize_quick(&norm_dir);
1788 // --   vm_vec_scale_add(&end_pos, start_pos, &norm_dir, MAX_LIGHTNING_DISTANCE);
1789 // -- 
1790 // --   fq.p0                                           = start_pos;
1791 // --   fq.startseg                             = start_segnum;
1792 // --   fq.p1                                           = &end_pos;
1793 // --   fq.rad                                  = 0;
1794 // --   fq.thisobjnum                   = parent;
1795 // --   fq.ignore_obj_list      = NULL;
1796 // --   fq.flags                                        = FQ_TRANSWALL | FQ_CHECK_OBJS;
1797 // -- 
1798 // --   fate = find_vector_intersection(&fq, &hit_data);
1799 // --   if (hit_data.hit_seg == -1) {
1800 // --           mprintf((1, "Warning: Lightning bolt has hit seg of -1.\n"));
1801 // --           return -1;
1802 // --   }
1803 // -- 
1804 // --   dist_to_hit_point = vm_vec_mag(vm_vec_sub(&tvec, &hit_data.hit_pnt, start_pos));
1805 // --   num_blobs = dist_to_hit_point/LIGHTNING_BLOB_DISTANCE;
1806 // -- 
1807 // --   if (num_blobs > MAX_LIGHTNING_BLOBS)
1808 // --           num_blobs = MAX_LIGHTNING_BLOBS;
1809 // -- 
1810 // --   if (num_blobs < MAX_LIGHTNING_BLOBS/4)
1811 // --           num_blobs = MAX_LIGHTNING_BLOBS/4;
1812 // -- 
1813 // --   // Find the initial position of the laser
1814 // --   gun_pos = &Player_ship->gun_points[Lightning_gun_num];
1815 // --   vm_copy_transpose_matrix(&m,&Objects[parent].orient);
1816 // --   vm_vec_rotate(&gun_pos2, gun_pos, &m);
1817 // --   vm_vec_add(&point_pos, &Objects[parent].pos, &gun_pos2);
1818 // -- 
1819 // --   delta_pos = norm_dir;
1820 // --   vm_vec_scale(&delta_pos, dist_to_hit_point/num_blobs);
1821 // -- 
1822 // --   for (i=0; i<num_blobs; i++) {
1823 // --           int                     point_seg;
1824 // --           object          *obj;
1825 // -- 
1826 // --           vm_vec_add2(&point_pos, &delta_pos);
1827 // --           point_seg = find_point_seg(&point_pos, start_segnum);
1828 // --           if (point_seg == -1)    //      Hey, we thought we were creating points on a line, but we left the mine!
1829 // --                   continue;
1830 // -- 
1831 // --           objnum = Laser_create_new( direction, &point_pos, point_seg, parent, LIGHTNING_BLOB_ID, 0 );
1832 // -- 
1833 // --           if ( objnum < 0 )       {
1834 // --                   mprintf((1, "Can't create lightning blob - Out of objects!\n" ));
1835 // --                   Int3();
1836 // --                   return -1;
1837 // --           }
1838 // -- 
1839 // --           obj = &Objects[objnum];
1840 // -- 
1841 // --           digi_play_sample( Weapon_info[obj->id].flash_sound, F1_0 );
1842 // -- 
1843 // --           // -- vm_vec_scale( &obj->mtype.phys_info.velocity, F1_0/2);
1844 // -- 
1845 // --           obj->lifeleft = (LIGHTNING_TIME + LIGHTNING_DELAY)/2;
1846 // -- 
1847 // --   }
1848 // -- 
1849 // --   return objnum;
1850 // -- 
1851 // -- }
1852 // -- 
1853 // -- //        --------------------------------------------------------------------------------------------------
1854 // -- //        Lightning Cannon.
1855 // -- //        While being fired, creates path of blobs forward from player until it hits something.
1856 // -- //        Up to MAX_LIGHTNING_BLOBS blobs, spaced LIGHTNING_BLOB_DISTANCE units apart.
1857 // -- //        When the player releases the firing key, the blobs move forward.
1858 // -- void lightning_frame(void)
1859 // -- {
1860 // --   if ((GameTime - Lightning_start_time < LIGHTNING_TIME) && (GameTime - Lightning_start_time > 0)) {
1861 // --           if (GameTime - Lightning_last_time > LIGHTNING_DELAY) {
1862 // --                   create_lightning_blobs(&ConsoleObject->orient.fvec, &ConsoleObject->pos, ConsoleObject->segnum, ConsoleObject-Objects);
1863 // --                   Lightning_last_time = GameTime;
1864 // --           }
1865 // --   }
1866 // -- }
1867
1868 //      --------------------------------------------------------------------------------------------------
1869 //      Object "objnum" fires weapon "weapon_num" of level "level".  (Right now (9/24/94) level is used only for type 0 laser.
1870 //      Flags are the player flags.  For network mode, set to 0.
1871 //      It is assumed that this is a player object (as in multiplayer), and therefore the gun positions are known.
1872 //      Returns number of times a weapon was fired.  This is typically 1, but might be more for low frame rates.
1873 //      More than one shot is fired with a pseudo-delay so that players on show machines can fire (for themselves
1874 //      or other players) often enough for things like the vulcan cannon.
1875 int do_laser_firing(int objnum, int weapon_num, int level, int flags, int nfires)
1876 {
1877         object  *objp = &Objects[objnum];
1878
1879         switch (weapon_num) {
1880                 case LASER_INDEX: {
1881                         int weapon_num;
1882
1883                         Laser_offset = ((F1_0*2)*(d_rand()%8))/8;
1884
1885                         if (level <= MAX_LASER_LEVEL)
1886                                 weapon_num = LASER_ID + level;
1887                         else
1888                                 weapon_num = SUPER_LASER_ID + (level-MAX_LASER_LEVEL-1);
1889
1890                         Laser_player_fire( objp, weapon_num, 0, 1, 0);
1891                         Laser_player_fire( objp, weapon_num, 1, 0, 0);
1892
1893                         if (flags & LASER_QUAD) {
1894                                 //      hideous system to make quad laser 1.5x powerful as normal laser, make every other quad laser bolt harmless
1895                                 Laser_player_fire( objp, weapon_num, 2, 0, 0);
1896                                 Laser_player_fire( objp, weapon_num, 3, 0, 0);
1897                         }
1898                         break;
1899                 }
1900                 case VULCAN_INDEX: {
1901                         //      Only make sound for 1/4 of vulcan bullets.
1902                         int     make_sound = 1;
1903                         //if (d_rand() > 24576)
1904                         //      make_sound = 1;
1905                         Laser_player_fire_spread( objp, VULCAN_ID, 6, d_rand()/8 - 32767/16, d_rand()/8 - 32767/16, make_sound, 0);
1906                         if (nfires > 1) {
1907                                 Laser_player_fire_spread( objp, VULCAN_ID, 6, d_rand()/8 - 32767/16, d_rand()/8 - 32767/16, 0, 0);
1908                                 if (nfires > 2) {
1909                                         Laser_player_fire_spread( objp, VULCAN_ID, 6, d_rand()/8 - 32767/16, d_rand()/8 - 32767/16, 0, 0);
1910                                 }
1911                         }
1912                         break;
1913                 }
1914                 case SPREADFIRE_INDEX:
1915                         if (flags & LASER_SPREADFIRE_TOGGLED) {
1916                                 Laser_player_fire_spread( objp, SPREADFIRE_ID, 6, F1_0/16, 0, 0, 0);
1917                                 Laser_player_fire_spread( objp, SPREADFIRE_ID, 6, -F1_0/16, 0, 0, 0);
1918                                 Laser_player_fire_spread( objp, SPREADFIRE_ID, 6, 0, 0, 1, 0);
1919                         } else {
1920                                 Laser_player_fire_spread( objp, SPREADFIRE_ID, 6, 0, F1_0/16, 0, 0);
1921                                 Laser_player_fire_spread( objp, SPREADFIRE_ID, 6, 0, -F1_0/16, 0, 0);
1922                                 Laser_player_fire_spread( objp, SPREADFIRE_ID, 6, 0, 0, 1, 0);
1923                         }
1924                         break;
1925
1926                 case PLASMA_INDEX:
1927                         Laser_player_fire( objp, PLASMA_ID, 0, 1, 0);
1928                         Laser_player_fire( objp, PLASMA_ID, 1, 0, 0);
1929                         if (nfires > 1) {
1930                                 Laser_player_fire_spread_delay( objp, PLASMA_ID, 0, 0, 0, FrameTime/2, 1, 0);
1931                                 Laser_player_fire_spread_delay( objp, PLASMA_ID, 1, 0, 0, FrameTime/2, 0, 0);
1932                         }
1933                         break;
1934
1935                 case FUSION_INDEX: {
1936                         vms_vector      force_vec;
1937
1938 //                      mprintf((0, "Fusion multiplier %f.\n", f2fl(Fusion_charge)));
1939
1940                         Laser_player_fire( objp, FUSION_ID, 0, 1, 0);
1941                         Laser_player_fire( objp, FUSION_ID, 1, 1, 0);
1942
1943                         flags = (sbyte)(Fusion_charge >> 12);
1944
1945                         Fusion_charge = 0;
1946
1947                         force_vec.x = -(objp->orient.fvec.x << 7);
1948                         force_vec.y = -(objp->orient.fvec.y << 7);
1949                         force_vec.z = -(objp->orient.fvec.z << 7);
1950                         phys_apply_force(objp, &force_vec);
1951
1952                         force_vec.x = (force_vec.x >> 4) + d_rand() - 16384;
1953                         force_vec.y = (force_vec.y >> 4) + d_rand() - 16384;
1954                         force_vec.z = (force_vec.z >> 4) + d_rand() - 16384;
1955                         phys_apply_rot(objp, &force_vec);
1956
1957                 }
1958                         break;
1959                 case SUPER_LASER_INDEX: {
1960                         int super_level = 3;            //make some new kind of laser eventually
1961                         Laser_player_fire( objp, super_level, 0, 1, 0);
1962                         Laser_player_fire( objp, super_level, 1, 0, 0);
1963
1964                         if (flags & LASER_QUAD) {
1965                                 //      hideous system to make quad laser 1.5x powerful as normal laser, make every other quad laser bolt harmless
1966                                 Laser_player_fire( objp, super_level, 2, 0, 0);
1967                                 Laser_player_fire( objp, super_level, 3, 0, 0);
1968                         }
1969                         break;
1970                 }
1971                 case GAUSS_INDEX: {
1972                         //      Only make sound for 1/4 of vulcan bullets.
1973                         int     make_sound = 1;
1974                         //if (d_rand() > 24576)
1975                         //      make_sound = 1;
1976                         
1977                         Laser_player_fire_spread( objp, GAUSS_ID, 6, (d_rand()/8 - 32767/16)/5, (d_rand()/8 - 32767/16)/5, make_sound, 0);
1978                         if (nfires > 1) {
1979                                 Laser_player_fire_spread( objp, GAUSS_ID, 6, (d_rand()/8 - 32767/16)/5, (d_rand()/8 - 32767/16)/5, 0, 0);
1980                                 if (nfires > 2) {
1981                                         Laser_player_fire_spread( objp, GAUSS_ID, 6, (d_rand()/8 - 32767/16)/5, (d_rand()/8 - 32767/16)/5, 0, 0);
1982                                 }
1983                         }
1984                         break;
1985                 }
1986                 case HELIX_INDEX: {
1987                         int helix_orient;
1988                         fix spreadr,spreadu;
1989                         helix_orient = (flags >> LASER_HELIX_SHIFT) & LASER_HELIX_MASK;
1990                         switch(helix_orient) {
1991
1992                                 case 0: spreadr =  F1_0/16; spreadu = 0;       break; // Vertical
1993                                 case 1: spreadr =  F1_0/17; spreadu = F1_0/42; break; //  22.5 degrees
1994                                 case 2: spreadr =  F1_0/22; spreadu = F1_0/22; break; //  45   degrees
1995                                 case 3: spreadr =  F1_0/42; spreadu = F1_0/17; break; //  67.5 degrees
1996                                 case 4: spreadr =  0;       spreadu = F1_0/16; break; //  90   degrees
1997                                 case 5: spreadr = -F1_0/42; spreadu = F1_0/17; break; // 112.5 degrees
1998                                 case 6: spreadr = -F1_0/22; spreadu = F1_0/22; break; // 135   degrees  
1999                                 case 7: spreadr = -F1_0/17; spreadu = F1_0/42; break; // 157.5 degrees
2000                                 default:
2001                                         Error("Invalid helix_orientation value %x\n",helix_orient);
2002                         }
2003
2004                         Laser_player_fire_spread( objp, HELIX_ID, 6,  0,  0, 1, 0);
2005                         Laser_player_fire_spread( objp, HELIX_ID, 6,  spreadr,  spreadu, 0, 0);
2006                         Laser_player_fire_spread( objp, HELIX_ID, 6, -spreadr, -spreadu, 0, 0);
2007                         Laser_player_fire_spread( objp, HELIX_ID, 6,  spreadr*2,  spreadu*2, 0, 0);
2008                         Laser_player_fire_spread( objp, HELIX_ID, 6, -spreadr*2, -spreadu*2, 0, 0);
2009                         break;
2010                 }
2011
2012                 case PHOENIX_INDEX:
2013                         Laser_player_fire( objp, PHOENIX_ID, 0, 1, 0);
2014                         Laser_player_fire( objp, PHOENIX_ID, 1, 0, 0);
2015                         if (nfires > 1) {
2016                                 Laser_player_fire_spread_delay( objp, PHOENIX_ID, 0, 0, 0, FrameTime/2, 1, 0);
2017                                 Laser_player_fire_spread_delay( objp, PHOENIX_ID, 1, 0, 0, FrameTime/2, 0, 0);
2018                         }
2019                         break;
2020
2021                 case OMEGA_INDEX:
2022                         Laser_player_fire( objp, OMEGA_ID, 1, 1, 0);
2023                         break;
2024
2025                 default:
2026                         Int3(); //      Contact Yuan: Unknown Primary weapon type, setting to 0.
2027                         Primary_weapon = 0;
2028         }
2029
2030         // Set values to be recognized during comunication phase, if we are the
2031         //  one shooting
2032 #ifdef NETWORK
2033         if ((Game_mode & GM_MULTI) && (objnum == Players[Player_num].objnum))
2034         {
2035                 // mprintf((0, "Flags on fire: %d.\n", flags));
2036                 Network_laser_fired = nfires;
2037                 Network_laser_gun = weapon_num;
2038                 Network_laser_flags = flags;
2039                 Network_laser_level = level;
2040         }
2041 #endif
2042
2043         return nfires;
2044 }
2045
2046 #define MAX_SMART_DISTANCE      (F1_0*150)
2047 #define MAX_OBJDISTS                    30
2048
2049 //      -------------------------------------------------------------------------------------------
2050 //      if goal_obj == -1, then create random vector
2051 int create_homing_missile(object *objp, int goal_obj, int objtype, int make_sound)
2052 {
2053         int                     objnum;
2054         vms_vector      vector_to_goal;
2055         vms_vector      random_vector;
2056         //vms_vector    goal_pos;
2057
2058         if (goal_obj == -1) {
2059                 make_random_vector(&vector_to_goal);
2060         } else {
2061                 vm_vec_normalized_dir_quick(&vector_to_goal, &Objects[goal_obj].pos, &objp->pos);
2062                 make_random_vector(&random_vector);
2063                 vm_vec_scale_add2(&vector_to_goal, &random_vector, F1_0/4);
2064                 vm_vec_normalize_quick(&vector_to_goal);
2065         }               
2066
2067         //      Create a vector towards the goal, then add some noise to it.
2068         objnum = Laser_create_new(&vector_to_goal, &objp->pos, objp->segnum, objp-Objects, objtype, make_sound);
2069         if (objnum == -1)
2070                 return -1;
2071
2072         // Fixed to make sure the right person gets credit for the kill
2073
2074 //      Objects[objnum].ctype.laser_info.parent_num = objp->ctype.laser_info.parent_num;
2075 //      Objects[objnum].ctype.laser_info.parent_type = objp->ctype.laser_info.parent_type;
2076 //      Objects[objnum].ctype.laser_info.parent_signature = objp->ctype.laser_info.parent_signature;
2077
2078         Objects[objnum].ctype.laser_info.track_goal = goal_obj;
2079
2080         return objnum;
2081 }
2082
2083 extern void blast_nearby_glass(object *objp, fix damage);
2084
2085 //-----------------------------------------------------------------------------
2086 // Create the children of a smart bomb, which is a bunch of homing missiles.
2087 void create_smart_children(object *objp, int num_smart_children)
2088 {
2089         int     parent_type, parent_num;
2090         int     make_sound;
2091         int     numobjs=0;
2092         int     objlist[MAX_OBJDISTS];
2093         int     blob_id;
2094
2095         if (objp->type == OBJ_WEAPON) {
2096                 parent_type = objp->ctype.laser_info.parent_type;
2097                 parent_num = objp->ctype.laser_info.parent_num;
2098         } else if (objp->type == OBJ_ROBOT) {
2099                 parent_type = OBJ_ROBOT;
2100                 parent_num = objp-Objects;
2101         } else {
2102                 Int3(); //      Hey, what kind of object is this!?
2103                 parent_type = 0;
2104                 parent_num = 0;
2105         }
2106
2107         if (objp->id == EARTHSHAKER_ID)
2108                 blast_nearby_glass(objp, Weapon_info[EARTHSHAKER_ID].strength[Difficulty_level]);
2109
2110 // -- DEBUG --
2111         if ((objp->type == OBJ_WEAPON) && ((objp->id == SMART_ID) || (objp->id == SUPERPROX_ID) || (objp->id == ROBOT_SUPERPROX_ID) || (objp->id == EARTHSHAKER_ID)))
2112                 Assert(Weapon_info[objp->id].children != -1);
2113 // -- DEBUG --
2114
2115         if (((objp->type == OBJ_WEAPON) && (Weapon_info[objp->id].children != -1)) || (objp->type == OBJ_ROBOT)) {
2116                 int     i, objnum;
2117
2118                 if (Game_mode & GM_MULTI)
2119                         d_srand(8321L);
2120
2121                 for (objnum=0; objnum<=Highest_object_index; objnum++) {
2122                         object  *curobjp = &Objects[objnum];
2123
2124                         if ((((curobjp->type == OBJ_ROBOT) && (!curobjp->ctype.ai_info.CLOAKED)) || (curobjp->type == OBJ_PLAYER)) && (objnum != parent_num)) {
2125                                 fix     dist;
2126
2127                                 if (curobjp->type == OBJ_PLAYER)
2128                                 {
2129                                         if ((parent_type == OBJ_PLAYER) && (Game_mode & GM_MULTI_COOP))
2130                                                 continue;
2131 #ifdef NETWORK
2132                                         if ((Game_mode & GM_TEAM) && (get_team(curobjp->id) == get_team(Objects[parent_num].id)))
2133                                                 continue;
2134 #endif
2135
2136                                         if (Players[curobjp->id].flags & PLAYER_FLAGS_CLOAKED)
2137                                                 continue;
2138                                 }
2139
2140                                 //      Robot blobs can't track robots.
2141                                 if (curobjp->type == OBJ_ROBOT) {
2142                                         if (parent_type == OBJ_ROBOT)
2143                                                 continue;
2144
2145                                         //      Your shots won't track the buddy.
2146                                         if (parent_type == OBJ_PLAYER)
2147                                                 if (Robot_info[curobjp->id].companion)
2148                                                         continue;
2149                                 }
2150
2151                                 dist = vm_vec_dist_quick(&objp->pos, &curobjp->pos);
2152                                 if (dist < MAX_SMART_DISTANCE) {
2153                                         int     oovis;
2154
2155                                         oovis = object_to_object_visibility(objp, curobjp, FQ_TRANSWALL);
2156
2157                                         if (oovis) { //object_to_object_visibility(objp, curobjp, FQ_TRANSWALL)) {
2158                                                 objlist[numobjs] = objnum;
2159                                                 numobjs++;
2160                                                 if (numobjs >= MAX_OBJDISTS) {
2161                                                         mprintf((0, "Warning -- too many objects near smart bomb explosion.  See laser.c.\n"));
2162                                                         numobjs = MAX_OBJDISTS;
2163                                                         break;
2164                                                 }
2165                                         }
2166                                 }
2167                         }
2168                 }
2169
2170                 //      Get type of weapon for child from parent.
2171                 if (objp->type == OBJ_WEAPON) {
2172                         blob_id = Weapon_info[objp->id].children;
2173                         Assert(blob_id != -1);          //      Hmm, missing data in bitmaps.tbl.  Need "children=NN" parameter.
2174                 } else {
2175                         Assert(objp->type == OBJ_ROBOT);
2176                         blob_id = ROBOT_SMART_HOMING_ID;
2177                 }
2178
2179 // --           //determine what kind of blob to drop
2180 // --           //      Note: parent_type is not the type of the weapon's parent.  It is actually the type of the weapon's
2181 // --           //      earliest ancestor.  This deals with the issue of weapons spewing weapons which spew weapons.
2182 // --           switch (parent_type) {
2183 // --                   case OBJ_WEAPON:
2184 // --                           Int3(); //      Should this ever happen?
2185 // --                           switch (objp->id) {
2186 // --                                   case SUPERPROX_ID:                      blob_id = SMART_MINE_HOMING_ID; break;
2187 // --                                   case ROBOT_SUPERPROX_ID:        blob_id = ROBOT_SMART_MINE_HOMING_ID; break;
2188 // --                                   case EARTHSHAKER_ID:                    blob_id = EARTHSHAKER_MEGA_ID; break;
2189 // --                                   default:                                                        Int3(); //bogus id for weapon  
2190 // --                           }
2191 // --                           break;
2192 // --                   case OBJ_PLAYER:
2193 // --                           switch (objp->id) {
2194 // --                                   case SUPERPROX_ID:                      blob_id = SMART_MINE_HOMING_ID; break;
2195 // --                                   case ROBOT_SUPERPROX_ID:        Int3(); break;
2196 // --                                   case EARTHSHAKER_ID:                    blob_id = EARTHSHAKER_MEGA_ID; break;
2197 // --                                   case SMART_ID:                                  blob_id = PLAYER_SMART_HOMING_ID; break;
2198 // --                                   default:                                                        Int3(); //bogus id for weapon  
2199 // --                           }
2200 // --                           break;
2201 // --                   case OBJ_ROBOT:
2202 // --                           switch (objp->id) {
2203 // --                                   case ROBOT_SUPERPROX_ID:        blob_id = ROBOT_SMART_MINE_HOMING_ID; break;
2204 // --                                   // -- case EARTHSHAKER_ID:                      blob_id = EARTHSHAKER_MEGA_ID; break;
2205 // --                                   case SMART_ID:                                  blob_id = ROBOT_SMART_HOMING_ID; break;
2206 // --                                   default:                                                        blob_id = ROBOT_SMART_HOMING_ID; break;
2207 // --                           }
2208 // --                           break;
2209 // --                   default:                                        Int3(); //bogus type for parent object
2210 // --           }
2211
2212                 make_sound = 1;
2213                 for (i=0; i<num_smart_children; i++) {
2214                         int objnum;
2215                         objnum = (numobjs==0)?-1:objlist[(d_rand() * numobjs) >> 15];
2216                         create_homing_missile(objp, objnum, blob_id, make_sound);
2217                         make_sound = 0;
2218                 }
2219         }
2220 }
2221
2222 int Missile_gun = 0;
2223
2224 //give up control of the guided missile
2225 void release_guided_missile(int player_num)
2226 {
2227         if (player_num == Player_num)
2228          {                      
2229           if (Guided_missile[player_num]==NULL)
2230                         return;
2231         
2232                 Missile_viewer = Guided_missile[player_num];
2233 #ifdef NETWORK
2234                 if (Game_mode & GM_MULTI)
2235                  multi_send_guided_info (Guided_missile[Player_num],1);
2236 #endif
2237                 if (Newdemo_state==ND_STATE_RECORDING)
2238                  newdemo_record_guided_end();
2239          }      
2240
2241         Guided_missile[player_num] = NULL;
2242 }
2243
2244 int Proximity_dropped=0,Smartmines_dropped=0;
2245
2246 //      -------------------------------------------------------------------------------------------
2247 //parameter determines whether or not to do autoselect if have run out of ammo
2248 //this is needed because if you drop a bomb with the B key, you don't want
2249 //want to autoselect if the bomb isn't actually selected. 
2250 void do_missile_firing(int do_autoselect)
2251 {
2252         int gun_flag=0;
2253
2254         Assert(Secondary_weapon < MAX_SECONDARY_WEAPONS);
2255
2256         if (Guided_missile[Player_num] && Guided_missile[Player_num]->signature==Guided_missile_sig[Player_num]) {
2257                 release_guided_missile(Player_num);
2258                 Next_missile_fire_time = GameTime + Weapon_info[Secondary_weapon_to_weapon_info[Secondary_weapon]].fire_wait;
2259                 return;
2260         }
2261
2262         if (!Player_is_dead && (Players[Player_num].secondary_ammo[Secondary_weapon] > 0))      {
2263
2264                 int weapon_id,weapon_gun;
2265
2266                 Players[Player_num].secondary_ammo[Secondary_weapon]--;
2267
2268                 weapon_id = Secondary_weapon_to_weapon_info[Secondary_weapon];
2269
2270                 if (Laser_rapid_fire!=0xBADA55)
2271                         Next_missile_fire_time = GameTime + Weapon_info[weapon_id].fire_wait;
2272                 else
2273                         Next_missile_fire_time = GameTime + F1_0/25;
2274
2275                 weapon_gun = Secondary_weapon_to_gun_num[Secondary_weapon];
2276
2277                 if (weapon_gun==4) {            //alternate left/right
2278                         weapon_gun += (gun_flag = (Missile_gun & 1));
2279                         Missile_gun++;
2280                 }
2281
2282                 Laser_player_fire( ConsoleObject, weapon_id, weapon_gun, 1, 0);
2283
2284                 if (Secondary_weapon == PROXIMITY_INDEX) {
2285                         if (++Proximity_dropped == 4) {
2286                                 Proximity_dropped = 0;
2287 #ifdef NETWORK
2288                                 maybe_drop_net_powerup(POW_PROXIMITY_WEAPON);
2289 #endif
2290                         }
2291                 }
2292                 else if (Secondary_weapon == SMART_MINE_INDEX) {
2293                         if (++Smartmines_dropped == 4) {
2294                                 Smartmines_dropped = 0;
2295 #ifdef NETWORK
2296                                 maybe_drop_net_powerup(POW_SMART_MINE);
2297 #endif
2298                         }
2299                 }
2300 #ifdef NETWORK
2301                 else if (Secondary_weapon != CONCUSSION_INDEX)
2302                         maybe_drop_net_powerup(Secondary_weapon_to_powerup[Secondary_weapon]);
2303 #endif
2304                 
2305                 if (Secondary_weapon == MEGA_INDEX || Secondary_weapon == SMISSILE5_INDEX) {
2306                         vms_vector force_vec;
2307
2308                         force_vec.x = -(ConsoleObject->orient.fvec.x << 7);
2309                         force_vec.y = -(ConsoleObject->orient.fvec.y << 7);
2310                         force_vec.z = -(ConsoleObject->orient.fvec.z << 7);
2311                         phys_apply_force(ConsoleObject, &force_vec);
2312         
2313                         force_vec.x = (force_vec.x >> 4) + d_rand() - 16384;
2314                         force_vec.y = (force_vec.y >> 4) + d_rand() - 16384;
2315                         force_vec.z = (force_vec.z >> 4) + d_rand() - 16384;
2316                         phys_apply_rot(ConsoleObject, &force_vec);
2317                 }
2318
2319 #ifdef NETWORK
2320                 if (Game_mode & GM_MULTI) 
2321                 {
2322                         Network_laser_fired = 1;                //how many
2323                         Network_laser_gun = Secondary_weapon + MISSILE_ADJUST;
2324                         Network_laser_flags = gun_flag;
2325                         Network_laser_level = 0;
2326                 }
2327 #endif
2328
2329                 if (do_autoselect)
2330                         auto_select_weapon(1);          //select next missile, if this one out of ammo
2331         }
2332 }
2333