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