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