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