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