]> icculus.org git repositories - taylor/freespace2.git/blob - src/hud/hudlock.cpp
The Great Newline Fix
[taylor/freespace2.git] / src / hud / hudlock.cpp
1 /*
2  * $Logfile: /Freespace2/code/Hud/HUDlock.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * C module that controls missile locking
8  *
9  * $Log$
10  * Revision 1.2  2002/05/07 03:16:45  theoddone33
11  * The Great Newline Fix
12  *
13  * Revision 1.1.1.1  2002/05/03 03:28:09  root
14  * Initial import.
15  *
16  * 
17  * 19    8/09/99 10:50a Jasenw
18  * 
19  * 18    7/30/99 5:42p Jasenw
20  * changed coords to center HUD lock indicator.
21  * 
22  * 17    7/28/99 2:49p Andsager
23  * Make hud target speed use vm_vec_mag (not vm_vec_mag_quick)
24  * 
25  * 16    6/08/99 7:41p Dave
26  * Animated hud lockup icon a bit differently.
27  * 
28  * 15    6/08/99 8:35a Jasenw
29  * new coords for new lock ani
30  * 
31  * 14    6/07/99 4:20p Andsager
32  * Add HUD color for tagged object.  Apply to target and radar.
33  * 
34  * 13    6/07/99 1:42p Jasenw
35  * 
36  * 12    6/04/99 10:58a Dave
37  * Updated hud lock gauge.
38  * 
39  * 11    6/03/99 2:32p Jasenw
40  * changed coords for new HUD stuff
41  * 
42  * 10    6/02/99 5:41p Andsager
43  * Reduce range of secondary weapons not fired from turrets in nebula.
44  * Reduce range of beams fired from turrrets in nebula
45  * 
46  * 9     4/23/99 12:01p Johnson
47  * Added SIF_HUGE_SHIP
48  * 
49  * 8     2/25/99 4:19p Dave
50  * Added multiplayer_beta defines. Added cd_check define. Fixed a few
51  * release build warnings. Added more data to the squad war request and
52  * response packets.
53  * 
54  * 7     1/07/99 9:07a Jasen
55  * HUD coords
56  * 
57  * 6     12/30/98 7:47a Jasen
58  * added coordinates for hi res
59  * 
60  * 5     12/28/98 3:17p Dave
61  * Support for multiple hud bitmap filenames for hi-res mode.
62  * 
63  * 4     12/21/98 5:02p Dave
64  * Modified all hud elements to be multi-resolution friendly.
65  * 
66  * 3     11/05/98 5:55p Dave
67  * Big pass at reducing #includes
68  * 
69  * 2     10/07/98 10:53a Dave
70  * Initial checkin.
71  * 
72  * 1     10/07/98 10:49a Dave
73  * 
74  * 55    8/25/98 1:48p Dave
75  * First rev of EMP effect. Player side stuff basically done. Next comes
76  * AI code.
77  * 
78  * 54    5/11/98 3:48p Lawrance
79  * Break lock when banks switch
80  * 
81  * 53    5/10/98 7:05p Dave
82  * Fix endgame sequencing ESC key. Changed how host options warning popups
83  * are done. Fixed pause/message scrollback/options screen problems in mp.
84  * Make sure observer HUD doesn't try to lock weapons.
85  * 
86  * 52    5/04/98 10:50p Lawrance
87  * Don't break lock when switching to secondary weapons of the same type
88  * 
89  * 51    4/20/98 12:36a Mike
90  * Make team vs. team work when player is hostile.  Several targeting
91  * problems.
92  * 
93  * 50    4/15/98 12:55a Lawrance
94  * Make shockwave damage 1/4 if bomb detonated by another weapon.  Allow
95  * missiles to lock on bombs.
96  * 
97  * 49    4/13/98 4:52p Allender
98  * remove AI_frametime and us flFrametime instead.  Make lock warning work
99  * in multiplayer for aspect seeking missiles.  Debris fixups
100  * 
101  * 48    4/08/98 8:33p Lawrance
102  * Make player re-acquire lock when targeting subsystems on a locked ship.
103  * 
104  * 47    4/03/98 10:29a Mike
105  * Make aspect seekers home on a ship even if the targeted subsystem is
106  * backfacing.  Too confusing.
107  * 
108  * 46    3/26/98 5:46p Lawrance
109  * call obj_team() instead of ship_team_from_obj()
110  * 
111  * 45    3/26/98 5:26p John
112  * added new paging code. nonfunctional.
113  * 
114  * 44    3/10/98 4:19p John
115  * Cleaned up graphics lib.  Took out most unused gr functions.   Made D3D
116  * & Glide have popups and print screen.  Took out all >8bpp software
117  * support.  Made Fred zbuffer.  Made zbuffer allocate dynamically to
118  * support Fred.  Made zbuffering key off of functions rather than one
119  * global variable.
120  * 
121  * 43    3/10/98 11:16a Lawrance
122  * Fix potential bug with missles not locking on a subsystem that appeared
123  * in view
124  * 
125  * 42    2/28/98 7:03p Lawrance
126  * Change player missile locking to use dot product, so we can use it in
127  * the external views
128  * 
129  * 41    2/22/98 4:17p John
130  * More string externalization classification... 190 left to go!
131  * 
132  * 40    1/25/98 10:31p Lawrance
133  * Don't draw most hud gauges when viewing from another ship.
134  * 
135  * 39    1/24/98 4:48p Lawrance
136  * Add 'locking_subsys_parent', needed to save/restore locking_subsys
137  * pointer.
138  * 
139  * 38    1/23/98 6:25p Lawrance
140  * Change player missile locking to lock on subsystem points automatically
141  * 
142  * 37    1/21/98 7:20p Lawrance
143  * Make subsystem locking only work with line-of-sight, cleaned up locking
144  * code, moved globals to player struct.
145  * 
146  * 36    1/19/98 10:02p Lawrance
147  * Fix bug with locking on friendlies
148  * 
149  * 35    12/30/97 4:28p Lawrance
150  * remove .ani extensions from filenames
151  * 
152  * 34    12/10/97 9:57a Lawrance
153  * allow missile locks on all ships but friendly
154  * 
155  * 33    11/17/97 6:37p Lawrance
156  * new gauges: extended target view, new lock triangles, support ship view
157  * 
158  * 32    11/17/97 5:51p Lawrance
159  * fix bug that was sometimes causing lock to not occur
160  * 
161  * 31    11/13/97 10:46p Lawrance
162  * ensure only attempt lock on HOSTILE
163  * 
164  * 30    11/11/97 12:59a Lawrance
165  * fix couple of bugs with lock indicator not going away
166  * 
167  * 29    11/08/97 11:07p Lawrance
168  * add in new lock indicator
169  * 
170  * 28    10/28/97 9:02a Lawrance
171  * don't loop lock sound, some minor reformatting
172  * 
173  * 27    10/08/97 5:07p Lawrance
174  * don't lock on friendly ships
175  * 
176  * 26    9/07/97 10:02p Lawrance
177  * don't lock on a ship if player doesn't have any missiles
178  * 
179  * 25    7/02/97 9:35a Hoffoss
180  * Changed all references to weapon variables in ships to 'weapons'
181  * structure variables in ships.
182  * 
183  * 24    6/05/97 1:07a Lawrance
184  * changes to support sound interface
185  * 
186  * 23    5/20/97 2:45p Mike
187  * Move current_target and a bunch of other stuff out of player struct.
188  * 
189  * 22    4/24/97 10:55a Lawrance
190  * added Show_lock_cone to DCF
191  * 
192  * 21    4/18/97 2:54p Lawrance
193  * sounds now have a default volume, when playing, pass a scaling factor
194  * not the actual volume
195  * 
196  * 20    4/15/97 8:36a Lawrance
197  * fixed bug where lock indicator was lagging locked target
198  * 
199  * 19    4/13/97 3:53p Lawrance
200  * separate out the non-rendering dependant portions of the HUD ( sounds,
201  * updating lock position, changing targets, etc) and put into
202  * hud_update_frame()
203  * 
204  * 18    4/12/97 4:29p Lawrance
205  * get missle locking and offscreen indicator working properly with
206  * different sized screens
207  * 
208  * 17    4/10/97 5:29p Lawrance
209  * hud rendering split up into hud_render_3d(), hud_render_2d() and
210  * hud_render_target_model()
211  * 
212  * 16    4/09/97 4:34p Lawrance
213  * allow looped sounds to be cut off after they complete the full sample
214  * duration
215  * 
216  * 15    3/25/97 8:17p Lawrance
217  * don't allow ASPECT homing missles to lock on debris
218  * 
219  * 14    3/19/97 5:53p Lawrance
220  * integrating new Misc_sounds[] array (replaces old Game_sounds
221  * structure)
222  * 
223  * 13    3/10/97 8:53a Lawrance
224  * using hud_stop_looped_locking_sounds() in place of
225  * hud_stop_looped_sounds()
226  * 
227  * 12    3/07/97 4:37p Mike
228  * Make rockeye missile home.
229  * Remove UNKNOWN and NEUTRAL teams.
230  * 
231  * 11    3/04/97 2:27p Lawrance
232  * check to ensure player ship has missile banks
233  * 
234  * 10    3/04/97 11:19a Lawrance
235  * supporting banked weapons
236  * 
237  * 9     1/23/97 12:00p Lawrance
238  * made lock sound play at normal volume.
239  * 
240  * 8     1/20/97 7:58p John
241  * Fixed some link errors with testcode.
242  * 
243  * 7     1/13/97 5:36p Lawrance
244  * integrating new Game_sounds structure for general game sounds
245  * 
246  * 6     1/06/97 10:43p Lawrance
247  * Changes to make save/restore functional
248  * 
249  * 5     1/02/97 7:12p Lawrance
250  * adding hooks for more sounds
251  * 
252  * 4     1/02/97 10:32a Lawrance
253  * fixed some bugs related to stopping looped sounds when targets die and
254  * going to menus
255  * 
256  * 3     12/24/96 4:32p Lawrance
257  * some minor improvements
258  * 
259  * 2     12/23/96 7:53p Lawrance
260  * missile locking working in new source files
261  *
262  * $NoKeywords: $
263  */
264
265 #include "hud.h"
266 #include "hudlock.h"
267 #include "hudtarget.h"
268 #include "hudreticle.h"
269 #include "player.h"
270 #include "ship.h"
271 #include "weapon.h"
272 #include "sound.h"
273 #include "timer.h"
274 #include "freespace.h"
275 #include "gamesequence.h"
276 #include "gamesnd.h"
277 #include "ai.h"
278 #include "bmpman.h"
279 #include "3d.h"
280 #include "linklist.h"
281 #include "multi.h"
282 #include "emp.h"
283
284
285 static float Lock_start_dist;
286 static int Rotate_time_id = 1;  // timer id for controlling how often to rotate triangles around lock indicator
287
288 int Missile_track_loop = -1;
289 int Missile_lock_loop = -1;
290
291 int Lock_target_box_width[GR_NUM_RESOLUTIONS] = {
292         19,
293         30
294 };
295 int Lock_target_box_height[GR_NUM_RESOLUTIONS] = {
296         19,
297         30
298 };
299
300 // the locked triangles (that orbit lock indicator) dimensions
301 float Lock_triangle_base[GR_NUM_RESOLUTIONS] = {
302         4.0f,
303         6.5f
304 };
305 float Lock_triangle_height[GR_NUM_RESOLUTIONS] = {
306         4.0f,
307         6.5f
308 };
309
310 int Lock_gauge_half_w[GR_NUM_RESOLUTIONS] = {
311         17,
312         28
313 };
314 int Lock_gauge_half_h[GR_NUM_RESOLUTIONS] = {
315         15, 
316         25
317 };
318
319 // hud_frames Lock_gauge;
320 int Lock_gauge_loaded = 0;
321 hud_anim Lock_gauge;
322 int Lock_gauge_draw = 0;
323 int Lock_gauge_draw_stamp = -1;
324 #define LOCK_GAUGE_BLINK_RATE                   5                       // blinks/sec
325
326 int Lockspin_half_w[GR_NUM_RESOLUTIONS] = {
327         31,
328         50
329 };
330 int Lockspin_half_h[GR_NUM_RESOLUTIONS] = {
331         32, 
332         52
333 };
334 hud_anim        Lock_anim;
335
336 char Lock_fname[GR_NUM_RESOLUTIONS][MAX_FILENAME_LEN] = {
337         "lock1",
338         "2_lock1"
339 };
340
341 char Lockspin_fname[GR_NUM_RESOLUTIONS][MAX_FILENAME_LEN] = {
342         "lockspin",
343         "2_lockspin"
344 };
345
346 void hud_lock_determine_lock_point(vector *lock_world_pos_out);
347
348 // hud_init_missile_lock() is called at the beginning of a mission
349 //
350 void hud_init_missile_lock()
351 {
352         Players[Player_num].lock_indicator_start_x = -1;
353         Players[Player_num].lock_indicator_start_y = -1;
354         Players[Player_num].lock_indicator_visible = 0;
355         Player_ai->current_target_is_locked = 0;
356
357         Player_ai->last_secondary_index = -1;
358
359         Rotate_time_id = 1;
360
361         // Load in the frames need for the lead indicator
362         if (!Lock_gauge_loaded) {
363                 /*
364                 Lock_gauge.first_frame = bm_load_animation(Lock_fname[gr_screen.res], &Lock_gauge.num_frames);
365                 if ( Lock_gauge.first_frame < 0 ) {
366                         Warning(LOCATION,"Cannot load hud ani: Lock_fname[gr_screen.res]\n");
367                 }
368                 */
369                 hud_anim_init(&Lock_gauge, 0, 0, Lock_fname[gr_screen.res]);
370                 hud_anim_load(&Lock_gauge);
371
372                 hud_anim_init(&Lock_anim, 0, 0, Lockspin_fname[gr_screen.res]);
373                 hud_anim_load(&Lock_anim);
374
375                 Lock_gauge_loaded = 1;
376                 
377                 Lock_gauge_draw_stamp = -1;
378                 Lock_gauge_draw = 0;
379         }
380 }
381
382 void hud_draw_diamond(int x, int y, int width, int height)
383 {
384         Assert(height>0);
385         Assert(width>0);
386
387         int x1,x2,x3,x4,y1,y2,y3,y4;
388
389         x1=x;
390         y1=y-height/2;
391
392         x2=x+width/2;
393         y2=y;
394
395         x3=x;
396         y3=y+height/2;
397
398         x4=x-width/2;
399         y4=y;
400
401         gr_line(x1,y1,x2,y2);
402         gr_line(x2,y2,x3,y3);
403         gr_line(x3,y3,x4,y4);
404         gr_line(x4,y4,x1,y1);
405 }
406
407
408 // hud_show_lock_indicator() will display the lock indicator for homing missiles
409 void hud_show_lock_indicator(float frametime)
410 {
411         int                     target_objnum, sx, sy;
412         object          *targetp;
413
414         if (!Players[Player_num].lock_indicator_visible){
415                 return;
416         }
417
418         target_objnum = Player_ai->target_objnum;
419         Assert(target_objnum != -1);
420         targetp = &Objects[target_objnum];
421
422         // check to see if there are any missile to fire.. we don't want to show the 
423         // lock indicator if there are missiles to fire.
424         if ( !ship_secondary_bank_has_ammo(Player_obj->instance) ) {
425                 return;
426         }
427         
428         hud_set_iff_color(targetp);
429 //      nprintf(("Alan","lockx: %d, locky: %d TargetX: %d, TargetY: %d\n", Players[Player_num].lock_indicator_x, Players[Player_num].lock_indicator_y, Player->current_target_sx, Player->current_target_sy));
430
431         if (Player_ai->current_target_is_locked) {
432                 sx = Player->current_target_sx;
433                 sy = Player->current_target_sy;
434                 // show the rotating triangles if target is locked
435                 hud_draw_lock_triangles(sx, sy, frametime);
436         } else {
437                 sx = Players[Player_num].lock_indicator_x;
438                 sy = Players[Player_num].lock_indicator_y;
439         }
440
441         // show locked indicator
442         /*
443         if ( Lock_gauge.first_frame >= 0 ) {
444                 gr_set_bitmap(Lock_gauge.first_frame);
445                 gr_aabitmap(sx - Lock_gauge_half_w[gr_screen.res], sy - Lock_gauge_half_h[gr_screen.res]);
446         } else {
447                 hud_draw_diamond(sx, sy, Lock_target_box_width[gr_screen.res], Lock_target_box_height[gr_screen.res]);
448         }
449         */
450         Lock_gauge.sx = sx - Lock_gauge_half_w[gr_screen.res];
451         Lock_gauge.sy = sy - Lock_gauge_half_h[gr_screen.res];
452         if(Player_ai->current_target_is_locked){
453                 Lock_gauge.time_elapsed = 0.0f;                 
454                 hud_anim_render(&Lock_gauge, 0.0f, 1);          
455         } else {
456                 hud_anim_render(&Lock_gauge, frametime, 1);
457         }
458 }
459
460 // Reset data used for player lock indicator
461 void hud_lock_reset(float lock_time_scale)
462 {
463         weapon_info     *wip;
464         ship_weapon     *swp;
465
466         swp = &Player_ship->weapons;
467         wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
468
469         Player_ai->current_target_is_locked = 0;
470         Players[Player_num].lock_indicator_visible = 0;
471         Player->target_in_lock_cone = 0;
472         Player->lock_time_to_target = i2fl(wip->min_lock_time*lock_time_scale);
473         Player->current_target_sx = -1;
474         Player->current_target_sy = -1;
475         Player->locking_subsys=NULL;
476         Player->locking_on_center=0;
477         Player->locking_subsys_parent=-1;
478         hud_stop_looped_locking_sounds();
479
480         Lock_gauge_draw_stamp = -1;
481         Lock_gauge_draw = 0;
482
483         // reset the lock anim time elapsed
484         Lock_anim.time_elapsed = 0.0f;
485 }
486
487 // Determine if the locking code has a point to track
488 int hud_lock_has_homing_point()
489 {
490         if ( Player_ai->targeted_subsys || Player->locking_subsys || Player->locking_on_center ) {
491                 return 1;
492         }
493         return 0;
494 }
495
496 int Nebula_sec_range = 0;
497 DCF_BOOL(nebula_sec_range, Nebula_sec_range)
498
499 int hud_lock_world_pos_in_range(vector *target_world_pos, vector *vec_to_target)
500 {
501         float                   dist_to_target, weapon_range;
502         weapon_info     *wip;
503         ship_weapon     *swp;
504
505         int target_in_range=1;
506
507         swp = &Player_ship->weapons;
508         wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
509
510         vm_vec_sub(vec_to_target, target_world_pos, &Player_obj->pos);
511         dist_to_target = vm_vec_mag(vec_to_target);
512
513         // calculate the range of the weapon, and only display the lead target indicator when
514         // if the weapon can actually hit the target
515         weapon_range = wip->max_speed * wip->lifetime;
516
517         // reduce firing range in nebula
518         if ((The_mission.flags & MISSION_FLAG_FULLNEB) && Nebula_sec_range) {
519                 weapon_range *= 0.8f;
520         }
521
522         if (dist_to_target > weapon_range) {
523                 target_in_range=0;
524         }
525
526         return target_in_range;
527 }
528
529 // Determine if point to lock on is in range
530 int hud_lock_target_in_range()
531 {
532         vector          target_world_pos, vec_to_target;
533         object          *targetp;
534
535         if ( !hud_lock_has_homing_point() ) {
536                 return 0;
537         }
538
539         targetp = &Objects[Player_ai->target_objnum];
540
541         if ( Player_ai->targeted_subsys != NULL ) {
542                 vm_vec_unrotate(&target_world_pos, &Player_ai->targeted_subsys->system_info->pnt, &targetp->orient);
543                 vm_vec_add2(&target_world_pos, &targetp->pos);
544         } else {
545                 if ( Player->locking_subsys ) {
546                         vm_vec_unrotate(&target_world_pos, &Player->locking_subsys->system_info->pnt, &targetp->orient);
547                         vm_vec_add2(&target_world_pos, &targetp->pos);
548                 } else {
549                         Assert(Player->locking_on_center);
550                         target_world_pos = targetp->pos;
551                 }
552         }
553
554         return hud_lock_world_pos_in_range(&target_world_pos, &vec_to_target);
555 }
556
557 int hud_abort_lock()
558 {
559         int target_team;
560
561         target_team = obj_team(&Objects[Player_ai->target_objnum]);
562
563         if ( Player_ship->weapons.num_secondary_banks <= 0 ) {
564                 return 1;
565         }
566
567         if ( Player_ship->weapons.current_secondary_bank < 0 ) {
568                 return 1;
569         }
570
571         // check to see if there are any missile to fire.. we don't want to show the 
572         // lock indicator if there are no missiles to fire.
573         if ( !ship_secondary_bank_has_ammo(Player_obj->instance) ) {
574                 return 1;
575         }
576
577         // if the target is friendly, don't lock!
578         if ( hud_team_matches_filter(Player_ship->team, target_team)) {
579                 // if we're in multiplayer dogfight, ignore this
580                 if(!((Game_mode & GM_MULTIPLAYER) && (Netgame.type_flags & NG_TYPE_DOGFIGHT))){
581                         return 1;
582                 }
583         }
584
585         return 0;
586 }
587
588 // determine if the subsystem to lock on to has a direct line of sight
589 int hud_lock_on_subsys_ok()
590 {
591         ship_subsys             *subsys;
592         vector                  subobj_pos;
593         object                  *target_objp;
594         int                             in_sight=0;
595         
596         Assert(Player_ai->target_objnum >= 0);
597         target_objp     = &Objects[Player_ai->target_objnum];
598
599         subsys = Player_ai->targeted_subsys;
600         if ( !subsys ) {
601                 return 0;
602         }
603
604         vm_vec_unrotate(&subobj_pos, &subsys->system_info->pnt, &target_objp->orient);
605         vm_vec_add2(&subobj_pos, &target_objp->pos);
606
607         if ( Player->subsys_in_view < 0 ) {
608                 in_sight = ship_subsystem_in_sight(target_objp, subsys, &View_position, &subobj_pos);
609         } else {
610                 in_sight = Player->subsys_in_view;
611         }
612
613         return in_sight;
614 }
615
616 // Determine if locking point is in the locking cone
617 void hud_lock_check_if_target_in_lock_cone(vector *lock_world_pos)
618 {
619         float           dist, dot;
620         vector  vec_to_target;
621
622         dist = vm_vec_normalized_dir(&vec_to_target, lock_world_pos, &Player_obj->pos);
623         dot = vm_vec_dot(&Player_obj->orient.fvec, &vec_to_target);
624
625         if ( dot > 0.85) {
626                 Player->target_in_lock_cone = 1;
627         } else {
628                 Player->target_in_lock_cone = 0;
629         }
630
631 }
632
633 // return 1 if current secondary weapon is different than previous secondary weapon
634 int hud_lock_secondary_weapon_changed(ship_weapon *swp)
635 {
636
637         if ( swp->current_secondary_bank != Player_ai->last_secondary_index ) {
638                 return 1;
639         }
640
641         return 0;
642 /*
643         int last_wi_index = -1;
644         int current_wi_index = -1;
645
646
647         // do a quick out if same bank is selected
648         if ( swp->current_secondary_bank == Player_ai->last_secondary_index ) {
649                 return 0;
650         }
651
652         // bank has changed, but it still may be the same weapon type
653         if ( swp->current_secondary_bank >= 0 ) {
654                 current_wi_index = swp->secondary_bank_weapons[swp->current_secondary_bank];
655         }
656
657         if ( Player_ai->last_secondary_index >= 0 ) {
658                 last_wi_index = swp->secondary_bank_weapons[Player_ai->last_secondary_index];
659         }
660
661         if ( current_wi_index != last_wi_index ) {
662                 return 1;
663         }
664
665         return 0;
666 */
667
668 }
669
670 // hud_update_lock_indicator() will manage the non-rendering dependant part of
671 // missle locking
672 void hud_update_lock_indicator(float frametime)
673 {
674         ship_weapon *swp;
675         weapon_info     *wip;
676         vector          lock_world_pos;
677
678         // if i'm a multiplayer observer, bail here
679         if((Game_mode & GM_MULTIPLAYER) && ((Net_player->flags & NETINFO_FLAG_OBSERVER) || (Player_obj->type == OBJ_OBSERVER)) ){
680                 return;
681         }
682
683         Assert(Player_ai->target_objnum != -1);
684
685         // be sure to unset this flag, then possibly set later in this function so that
686         // threat indicators work properly.
687         Player_ai->ai_flags &= ~AIF_SEEK_LOCK;
688
689         if ( hud_abort_lock() ) {
690                 hud_lock_reset();
691                 return;
692         }
693
694         // if there is an EMP effect active, never update lock
695         if(emp_active_local()){
696                 hud_lock_reset();
697                 return;
698         }
699
700         swp = &Player_ship->weapons;
701         wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
702
703         Lock_start_dist = wip->min_lock_time * wip->lock_pixels_per_sec;
704
705         // if secondary weapons change, reset the lock
706         if ( hud_lock_secondary_weapon_changed(swp) ) {
707                 hud_lock_reset();
708         }
709                 
710         Player_ai->last_secondary_index = swp->current_secondary_bank;
711
712         if ( !(wip->wi_flags & WIF_HOMING_ASPECT) ) {
713                 hud_lock_reset();
714                 return;         
715         }
716
717         // Allow locking on ships and bombs (only targeted weapon allowed is a bomb, so don't bother checking flags)
718         if ( (Objects[Player_ai->target_objnum].type != OBJ_SHIP) && (Objects[Player_ai->target_objnum].type != OBJ_WEAPON) ) { 
719                 hud_lock_reset();
720                 return;
721         }
722
723         hud_lock_determine_lock_point(&lock_world_pos);
724
725         if ( !hud_lock_has_homing_point() ) {
726                 Player->target_in_lock_cone=0;
727         }
728
729         hud_lock_check_if_target_in_lock_cone(&lock_world_pos);
730
731         // check if the target is within range of the current secondary weapon.  If it is not,
732         // a lock will not be detected
733         if ( !hud_lock_target_in_range() ) {
734                 Player->target_in_lock_cone = 0;
735         }
736
737         // If locking on a subsystem, and not in sight... can't lock
738         //      Changed by MK on 4/3/98.  It was confusing me that my hornets would not lock on my target.
739         //      It will now be confusing that they lock, but don't home on your subsystem, but I think that's preferable.
740         //      Often you really care about destroying the target, not just the subsystem.
741         /*if ( Player_ai->targeted_subsys ) {
742                 if ( !hud_lock_on_subsys_ok() ) {
743                         Player->target_in_lock_cone=0;
744                 }
745         }*/
746
747         if ( !Player->target_in_lock_cone ) {
748                 Player->locking_on_center=0;
749                 Player->locking_subsys_parent=-1;
750                 Player->locking_subsys=NULL;
751         }
752                 
753         hud_calculate_lock_position(frametime);
754
755         if (!Players[Player_num].lock_indicator_visible)
756                 return;
757
758         if (Player_ai->current_target_is_locked) {
759                 if ( Missile_track_loop > -1 )  {
760                         snd_chg_loop_status(Missile_track_loop, 0);
761                         Missile_track_loop = -1;
762                         Missile_lock_loop = snd_play(&Snds[SND_MISSILE_LOCK]);
763                 }
764         }
765         else {
766                 Player_ai->ai_flags |= AIF_SEEK_LOCK;           // set this flag so multiplayer's properly track lock on other ships
767                 if ( Missile_lock_loop != -1 && snd_is_playing(Missile_lock_loop) ) {
768                         snd_stop(Missile_lock_loop);
769                         Missile_lock_loop = -1;
770                 }
771         }
772 }
773
774 // hud_draw_lock_triangles() will draw the 4 rotating triangles around a lock indicator
775 // (This is done when a lock has been acquired)
776 #define ROTATE_DELAY 40
777 void hud_draw_lock_triangles_old(int center_x, int center_y, int radius)
778 {
779         static float ang = 0.0f;
780
781         float end_ang = ang + 2*PI;
782         float x3,y3,x4,y4,xpos,ypos;
783
784         if ( timestamp_elapsed(Rotate_time_id) ) {
785                 Rotate_time_id = timestamp(ROTATE_DELAY);
786                 ang += PI/12;
787         }
788
789         for (ang; ang <= end_ang; ang += PI/2.0f) {
790
791                 // draw the orbiting triangles
792
793                 //ang = atan2(target_point.y,target_point.x);
794                 xpos = center_x + (float)cos(ang)*(radius + Lock_triangle_height[gr_screen.res] + 2);
795                 ypos = center_y - (float)sin(ang)*(radius + Lock_triangle_height[gr_screen.res] + 2);
796                         
797                 x3 = xpos - Lock_triangle_base[gr_screen.res] * (float)sin(-ang);
798                 y3 = ypos + Lock_triangle_base[gr_screen.res] * (float)cos(-ang);
799                 x4 = xpos + Lock_triangle_base[gr_screen.res] * (float)sin(-ang);
800                 y4 = ypos - Lock_triangle_base[gr_screen.res] * (float)cos(-ang);
801
802                 xpos = xpos - Lock_triangle_base[gr_screen.res] * (float)cos(ang);
803                 ypos = ypos + Lock_triangle_base[gr_screen.res] * (float)sin(ang);
804
805                 hud_tri(x3, y3, xpos, ypos, x4, y4);
806         } // end for
807 }
808
809 // draw a frame of the rotating lock triangles animation
810 void hud_draw_lock_triangles(int center_x, int center_y, float frametime)
811 {
812         if ( Lock_anim.first_frame == -1 ) {
813                 hud_draw_lock_triangles_old(center_x, center_y, Lock_target_box_width[gr_screen.res]/2);
814         } else {
815                 // render the anim
816                 Lock_anim.sx = center_x - Lockspin_half_w[gr_screen.res];
817                 Lock_anim.sy = center_y - Lockspin_half_h[gr_screen.res];
818
819                 // if its still animating
820                 if(Lock_anim.time_elapsed < Lock_anim.total_time){
821                         hud_anim_render(&Lock_anim, frametime, 1, 0, 1);
822                 } else {
823                         // if the timestamp is unset or expired
824                         if((Lock_gauge_draw_stamp < 0) || timestamp_elapsed(Lock_gauge_draw_stamp)){
825                                 // reset timestamp
826                                 Lock_gauge_draw_stamp = timestamp(1000 / (2 * LOCK_GAUGE_BLINK_RATE));
827
828                                 // switch between draw and dont-draw
829                                 Lock_gauge_draw = !Lock_gauge_draw;
830                         }
831
832                         // maybe draw the anim
833                         Lock_gauge.time_elapsed = 0.0f;                 
834                         if(Lock_gauge_draw){
835                                 hud_anim_render(&Lock_anim, frametime, 1, 0, 1);
836                         }                       
837                 }               
838         }
839 }
840
841 // hud_calculate_lock_position()  will determine where on the screen to draw the lock 
842 // indicator, and will determine when a lock has occurred.  If the lock indicator is not
843 // on the screen yet, hud_calculate_lock_start_pos() is called to pick a starting location
844 void hud_calculate_lock_position(float frametime)
845 {
846         ship_weapon *swp;
847         weapon_info     *wip;
848
849         static float pixels_moved_while_locking;
850         static float pixels_moved_while_degrading;
851         static int Need_new_start_pos = 0;
852
853         static double accumulated_x_pixels, accumulated_y_pixels;
854         double int_portion;
855
856         static float last_dist_to_target;
857         
858         static int catching_up;
859
860         static int maintain_lock_count = 0;
861
862         static float catch_up_distance = 0.0f;
863
864         double hypotenuse, delta_x, delta_y;
865
866         swp = &Player_ship->weapons;
867         wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
868
869         if (Player->target_in_lock_cone) {
870                 if (!Players[Player_num].lock_indicator_visible) {
871                         hud_calculate_lock_start_pos();
872                         last_dist_to_target = 0.0f;
873
874                         Players[Player_num].lock_indicator_x = Players[Player_num].lock_indicator_start_x;
875                         Players[Player_num].lock_indicator_y = Players[Player_num].lock_indicator_start_y;
876                         Players[Player_num].lock_indicator_visible = 1;
877
878                         Players[Player_num].lock_time_to_target = i2fl(wip->min_lock_time);
879                         catching_up = 0;
880                 }
881
882                 Need_new_start_pos = 1;
883
884                 if (Player_ai->current_target_is_locked) {
885                         Players[Player_num].lock_indicator_x = Player->current_target_sx;
886                         Players[Player_num].lock_indicator_y = Player->current_target_sy;
887                         return;
888                 }
889
890                 delta_x = Players[Player_num].lock_indicator_x - Player->current_target_sx;
891                 delta_y = Players[Player_num].lock_indicator_y - Player->current_target_sy;
892
893                 if (!delta_y && !delta_x) {
894                         hypotenuse = 0.0f;
895                 }
896                 else {
897                         hypotenuse = _hypot(delta_y, delta_x);
898                 }
899
900                 Players[Player_num].lock_dist_to_target = (float)hypotenuse;
901
902                 if (last_dist_to_target == 0) {
903                         last_dist_to_target = Players[Player_num].lock_dist_to_target;
904                 }
905
906                 //nprintf(("Alan","dist to target: %.2f\n",Players[Player_num].lock_dist_to_target));
907                 //nprintf(("Alan","last to target: %.2f\n\n",last_dist_to_target));
908
909                 if (catching_up) {
910                         //nprintf(("Alan","IN CATCH UP MODE  catch_up_dist is %.2f\n",catch_up_distance));      
911                         if ( Players[Player_num].lock_dist_to_target < catch_up_distance )
912                                 catching_up = 0;
913                 }
914                 else {
915                         //nprintf(("Alan","IN NORMAL MODE\n"));
916                         if ( (Players[Player_num].lock_dist_to_target - last_dist_to_target) > 2.0f ) {
917                                 catching_up = 1;
918                                 catch_up_distance = last_dist_to_target + wip->catchup_pixel_penalty;
919                         }
920                 }
921
922                 last_dist_to_target = Players[Player_num].lock_dist_to_target;
923
924                 if (!catching_up) {
925                         Players[Player_num].lock_time_to_target -= frametime;
926                         if (Players[Player_num].lock_time_to_target < 0.0f)
927                                 Players[Player_num].lock_time_to_target = 0.0f;
928                 }
929
930                 float lock_pixels_per_sec;
931                 if (Players[Player_num].lock_time_to_target > 0) {
932                         lock_pixels_per_sec = Players[Player_num].lock_dist_to_target / Players[Player_num].lock_time_to_target;
933                 } else {
934                         lock_pixels_per_sec = i2fl(wip->lock_pixels_per_sec);
935                 }
936
937                 if (lock_pixels_per_sec > wip->lock_pixels_per_sec) {
938                         lock_pixels_per_sec = i2fl(wip->lock_pixels_per_sec);
939                 }
940                 
941                 if (catching_up) {
942                         pixels_moved_while_locking = wip->catchup_pixels_per_sec * frametime;
943                 } else {
944                         pixels_moved_while_locking = lock_pixels_per_sec * frametime;
945                 }
946                 
947                 if (delta_x != 0) {
948                         accumulated_x_pixels += pixels_moved_while_locking * delta_x/hypotenuse; 
949                 }
950
951                 if (delta_y != 0) {
952                         accumulated_y_pixels += pixels_moved_while_locking * delta_y/hypotenuse; 
953                 }
954
955                 if (fl_abs(accumulated_x_pixels) > 1.0f) {
956                         modf(accumulated_x_pixels, &int_portion);
957
958                         Players[Player_num].lock_indicator_x -= (int)int_portion;
959
960                         if ( fl_abs(Players[Player_num].lock_indicator_x - Player->current_target_sx) < fl_abs(int_portion) )
961                                 Players[Player_num].lock_indicator_x = Player->current_target_sx;
962
963                         accumulated_x_pixels -= int_portion;
964                 }
965
966                 if (fl_abs(accumulated_y_pixels) > 1.0f) {
967                         modf(accumulated_y_pixels, &int_portion);
968
969                         Players[Player_num].lock_indicator_y -= (int)int_portion;
970
971                         if ( fl_abs(Players[Player_num].lock_indicator_y - Player->current_target_sy) < fl_abs(int_portion) )
972                                 Players[Player_num].lock_indicator_y = Player->current_target_sy;
973
974                         accumulated_y_pixels -= int_portion;
975                 }
976
977                 if ( Missile_track_loop == -1 ) {       
978                         Missile_track_loop = snd_play_looping( &Snds[SND_MISSILE_TRACKING], 0.0f , -1, -1);
979                 }
980
981                 if (!Players[Player_num].lock_time_to_target) {
982                         if ( (Players[Player_num].lock_indicator_x == Player->current_target_sx) && (Players[Player_num].lock_indicator_y == Player->current_target_sy) ) {
983                                 if (maintain_lock_count++ > 1) {
984                                         Player_ai->current_target_is_locked = 1;
985                                 }
986                         } else {
987                                 maintain_lock_count = 0;
988                         }
989                 }
990
991         } else {
992
993                 if ( Missile_track_loop > -1 )  {
994                         snd_chg_loop_status(Missile_track_loop, 0);
995                         Missile_track_loop = -1;
996                 }
997
998                 Player_ai->current_target_is_locked = 0;
999
1000                 if (!Players[Player_num].lock_indicator_visible) {
1001                         return;
1002                 }
1003
1004                 catching_up = 0;
1005                 last_dist_to_target = 0.0f;
1006
1007                 if (Need_new_start_pos) {
1008                         hud_calculate_lock_start_pos();
1009                         Need_new_start_pos = 0;
1010                         accumulated_x_pixels = 0.0f;
1011                         accumulated_y_pixels = 0.0f;
1012                 }
1013
1014                 delta_x = Players[Player_num].lock_indicator_x - Players[Player_num].lock_indicator_start_x;
1015                 delta_y = Players[Player_num].lock_indicator_y - Players[Player_num].lock_indicator_start_y;
1016
1017                 if (!delta_y && !delta_x) {
1018                         hypotenuse = 0.0f;
1019                 }
1020                 else {
1021                         hypotenuse = _hypot(delta_y, delta_x);
1022                 }
1023
1024                 Players[Player_num].lock_time_to_target += frametime;
1025
1026                 if (Players[Player_num].lock_time_to_target > wip->min_lock_time)
1027                         Players[Player_num].lock_time_to_target = i2fl(wip->min_lock_time);
1028
1029                 pixels_moved_while_degrading = 2.0f * wip->lock_pixels_per_sec * frametime;
1030
1031                 if (delta_x != 0)
1032                         accumulated_x_pixels += pixels_moved_while_degrading * delta_x/hypotenuse; 
1033
1034                 if (delta_y != 0)
1035                         accumulated_y_pixels += pixels_moved_while_degrading * delta_y/hypotenuse; 
1036
1037                 if (fl_abs(accumulated_x_pixels) > 1.0f) {
1038                         modf(accumulated_x_pixels, &int_portion);
1039
1040                         Players[Player_num].lock_indicator_x -= (int)int_portion;
1041
1042                         if ( fl_abs(Players[Player_num].lock_indicator_x - Players[Player_num].lock_indicator_start_x) < fl_abs(int_portion) )
1043                                 Players[Player_num].lock_indicator_x = Players[Player_num].lock_indicator_start_x;
1044
1045                         accumulated_x_pixels -= int_portion;
1046                 }
1047
1048                 if (fl_abs(accumulated_y_pixels) > 1.0f) {
1049                         modf(accumulated_y_pixels, &int_portion);
1050
1051                         Players[Player_num].lock_indicator_y -= (int)int_portion;
1052
1053                         if ( fl_abs(Players[Player_num].lock_indicator_y - Players[Player_num].lock_indicator_start_y) < fl_abs(int_portion) )
1054                                 Players[Player_num].lock_indicator_y = Players[Player_num].lock_indicator_start_y;
1055
1056                         accumulated_y_pixels -= int_portion;
1057                 }
1058
1059                 if ( (Players[Player_num].lock_indicator_x == Players[Player_num].lock_indicator_start_x) && (Players[Player_num].lock_indicator_y == Players[Player_num].lock_indicator_start_y) ) {
1060                         Players[Player_num].lock_indicator_visible = 0;
1061                 }
1062         }
1063 }
1064
1065 // hud_calculate_lock_start_pos() will determine where to draw the starting location of the lock
1066 // indicator.  It does this by picking a location that is Lock_start_dist pixels away from the current
1067 // target (in 2D).  This is accomplished by finding the endpoint of a line that passes through the 
1068 // origin, and connects the target and lock indicator postion (and has a magnitude of Lock_start_dist)
1069 void hud_calculate_lock_start_pos()
1070 {
1071         double hypotenuse;
1072         double delta_y;
1073         double delta_x;
1074         double target_mag, target_x, target_y;
1075
1076         delta_x = Player->current_target_sx - SCREEN_CENTER_X;
1077         delta_y = Player->current_target_sy - SCREEN_CENTER_Y;
1078
1079         if (!delta_x && !delta_y) {
1080                 Players[Player_num].lock_indicator_start_x = fl2i(SCREEN_CENTER_X + Lock_start_dist);
1081                 Players[Player_num].lock_indicator_start_y = fl2i(SCREEN_CENTER_Y);
1082                 return;
1083         }
1084
1085         hypotenuse = _hypot(delta_y, delta_x);
1086
1087         if (hypotenuse >= Lock_start_dist) {
1088                 Players[Player_num].lock_indicator_start_x = fl2i(SCREEN_CENTER_X);
1089                 Players[Player_num].lock_indicator_start_y = fl2i(SCREEN_CENTER_Y);
1090                 return;
1091         }
1092
1093         target_mag = Lock_start_dist - hypotenuse;
1094         target_x = target_mag * (delta_x / hypotenuse);
1095         target_y = target_mag * (delta_y / hypotenuse);
1096
1097         Players[Player_num].lock_indicator_start_x = fl2i(SCREEN_CENTER_X - target_x);
1098         Players[Player_num].lock_indicator_start_y = fl2i(SCREEN_CENTER_Y - target_y);
1099
1100         if (Players[Player_num].lock_indicator_start_x > gr_screen.clip_right)
1101                 Players[Player_num].lock_indicator_start_x = gr_screen.clip_right;
1102
1103         if (Players[Player_num].lock_indicator_start_y > gr_screen.clip_bottom)
1104                 Players[Player_num].lock_indicator_start_y = gr_screen.clip_bottom;
1105
1106         if (Players[Player_num].lock_indicator_start_x < gr_screen.clip_left)
1107                 Players[Player_num].lock_indicator_start_x = gr_screen.clip_left;
1108
1109         if (Players[Player_num].lock_indicator_start_y < gr_screen.clip_top)
1110                 Players[Player_num].lock_indicator_start_y = gr_screen.clip_top;
1111 }
1112
1113 // hud_stop_looped_locking_sounds() will terminate any hud related looping sounds that are playing
1114 void hud_stop_looped_locking_sounds()
1115 {
1116         if ( Missile_track_loop > -1 )  {
1117                 snd_stop(Missile_track_loop);
1118                 Missile_track_loop = -1;
1119         }
1120 }
1121
1122 // Get a new world pos for the locking point
1123 void hud_lock_update_lock_pos(object *target_objp, vector *lock_world_pos)
1124 {
1125         if ( Player_ai->targeted_subsys ) {
1126                 get_subsystem_world_pos(target_objp, Player_ai->targeted_subsys, lock_world_pos);
1127                 return;
1128         }
1129
1130         if ( Player->locking_on_center ) {
1131                 *lock_world_pos = target_objp->pos;
1132         } else {
1133                 Assert(Player->locking_subsys);
1134                 get_subsystem_world_pos(target_objp, Player->locking_subsys, lock_world_pos);
1135         }
1136 }
1137
1138 // Try and find a new locking point
1139 void hud_lock_get_new_lock_pos(object *target_objp, vector *lock_world_pos)
1140 {
1141         ship                    *target_shipp=NULL;
1142         int                     lock_in_range=0;
1143         float                   best_lock_dot=-1.0f, lock_dot=-1.0f;
1144         ship_subsys     *ss;
1145         vector          subsys_world_pos, vec_to_lock;
1146
1147         if ( target_objp->type == OBJ_SHIP ) {
1148                 target_shipp = &Ships[target_objp->instance];
1149         }
1150
1151         // if a large ship, lock to pos closest to center and within range
1152         if ( (target_shipp) && (Ship_info[target_shipp->ship_info_index].flags & (SIF_BIG_SHIP|SIF_HUGE_SHIP)) ) {
1153                 // check all the subsystems and the center of the ship
1154                 
1155                 // assume best lock pos is the center of the ship
1156                 *lock_world_pos=target_objp->pos;
1157                 Player->locking_on_center=1;
1158                 Player->locking_subsys=NULL;
1159                 Player->locking_subsys_parent=-1;
1160                 lock_in_range = hud_lock_world_pos_in_range(lock_world_pos, &vec_to_lock);
1161                 vm_vec_normalize(&vec_to_lock);
1162                 if ( lock_in_range ) {
1163                         best_lock_dot=vm_vec_dot(&Player_obj->orient.fvec, &vec_to_lock);
1164                 } 
1165                 // take center if reasonable dot
1166                 if ( best_lock_dot > 0.95 ) {
1167                         return;
1168                 }
1169
1170                 // iterate through subsystems to see if we can get a better choice
1171                 ss = GET_FIRST(&target_shipp->subsys_list);
1172                 while ( ss != END_OF_LIST( &target_shipp->subsys_list ) ) {
1173
1174                         // get world pos of subsystem
1175                         get_subsystem_world_pos(target_objp, ss, &subsys_world_pos);
1176
1177                         if ( hud_lock_world_pos_in_range(&subsys_world_pos, &vec_to_lock) ) {
1178                                 vm_vec_normalize(&vec_to_lock);
1179                                 lock_dot=vm_vec_dot(&Player_obj->orient.fvec, &vec_to_lock);
1180                                 if ( lock_dot > best_lock_dot ) {
1181                                         best_lock_dot=lock_dot;
1182                                         Player->locking_on_center=0;
1183                                         Player->locking_subsys=ss;
1184                                         Player->locking_subsys_parent=Player_ai->target_objnum;
1185                                         *lock_world_pos=subsys_world_pos;
1186                                 }
1187                         }
1188                         ss = GET_NEXT( ss );
1189                 }
1190         } else {
1191                 // if small ship (or weapon), just go for the center
1192                 *lock_world_pos = target_objp->pos;
1193                 Player->locking_on_center=1;
1194                 Player->locking_subsys=NULL;
1195                 Player->locking_subsys_parent=-1;
1196         }
1197 }
1198
1199 // Decide which point lock should be homing on
1200 void hud_lock_determine_lock_point(vector *lock_world_pos_out)
1201 {
1202         vector  lock_world_pos;
1203         vertex  lock_point;
1204         object  *target_objp;
1205
1206         Assert(Player_ai->target_objnum >= 0);
1207         target_objp = &Objects[Player_ai->target_objnum];
1208
1209         Player->current_target_sx = -1;
1210         Player->current_target_sx = -1;
1211
1212         // If subsystem is targeted, we must try to lock on that
1213         if ( Player_ai->targeted_subsys ) {
1214                 hud_lock_update_lock_pos(target_objp, &lock_world_pos);
1215                 Player->locking_on_center=0;
1216                 Player->locking_subsys=NULL;
1217                 Player->locking_subsys_parent=-1;
1218         } else {
1219                 // See if we already have a successful locked point
1220                 if ( hud_lock_has_homing_point() ) {
1221                         hud_lock_update_lock_pos(target_objp, &lock_world_pos);
1222                 } else {
1223                         hud_lock_get_new_lock_pos(target_objp, &lock_world_pos);
1224                 }
1225         }
1226
1227         *lock_world_pos_out=lock_world_pos;
1228
1229         g3_rotate_vertex(&lock_point,&lock_world_pos);
1230         g3_project_vertex(&lock_point);
1231
1232         if (!(lock_point.flags & PF_OVERFLOW)) {  // make sure point projected
1233                 Player->current_target_sx = (int)lock_point.sx;
1234                 Player->current_target_sy = (int)lock_point.sy;
1235         }
1236 }
1237
1238 void hudlock_page_in()
1239 {
1240         bm_page_in_aabitmap( Lock_gauge.first_frame, Lock_gauge.num_frames );
1241 }