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