2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Hud/HUDlock.cpp $
15 * C module that controls missile locking
18 * Revision 1.4 2002/06/09 04:41:21 relnev
19 * added copyright header
21 * Revision 1.3 2002/05/31 07:24:28 relnev
24 * Revision 1.2 2002/05/07 03:16:45 theoddone33
25 * The Great Newline Fix
27 * Revision 1.1.1.1 2002/05/03 03:28:09 root
31 * 19 8/09/99 10:50a Jasenw
33 * 18 7/30/99 5:42p Jasenw
34 * changed coords to center HUD lock indicator.
36 * 17 7/28/99 2:49p Andsager
37 * Make hud target speed use vm_vec_mag (not vm_vec_mag_quick)
39 * 16 6/08/99 7:41p Dave
40 * Animated hud lockup icon a bit differently.
42 * 15 6/08/99 8:35a Jasenw
43 * new coords for new lock ani
45 * 14 6/07/99 4:20p Andsager
46 * Add HUD color for tagged object. Apply to target and radar.
48 * 13 6/07/99 1:42p Jasenw
50 * 12 6/04/99 10:58a Dave
51 * Updated hud lock gauge.
53 * 11 6/03/99 2:32p Jasenw
54 * changed coords for new HUD stuff
56 * 10 6/02/99 5:41p Andsager
57 * Reduce range of secondary weapons not fired from turrets in nebula.
58 * Reduce range of beams fired from turrrets in nebula
60 * 9 4/23/99 12:01p Johnson
63 * 8 2/25/99 4:19p Dave
64 * Added multiplayer_beta defines. Added cd_check define. Fixed a few
65 * release build warnings. Added more data to the squad war request and
68 * 7 1/07/99 9:07a Jasen
71 * 6 12/30/98 7:47a Jasen
72 * added coordinates for hi res
74 * 5 12/28/98 3:17p Dave
75 * Support for multiple hud bitmap filenames for hi-res mode.
77 * 4 12/21/98 5:02p Dave
78 * Modified all hud elements to be multi-resolution friendly.
80 * 3 11/05/98 5:55p Dave
81 * Big pass at reducing #includes
83 * 2 10/07/98 10:53a Dave
86 * 1 10/07/98 10:49a Dave
88 * 55 8/25/98 1:48p Dave
89 * First rev of EMP effect. Player side stuff basically done. Next comes
92 * 54 5/11/98 3:48p Lawrance
93 * Break lock when banks switch
95 * 53 5/10/98 7:05p Dave
96 * Fix endgame sequencing ESC key. Changed how host options warning popups
97 * are done. Fixed pause/message scrollback/options screen problems in mp.
98 * Make sure observer HUD doesn't try to lock weapons.
100 * 52 5/04/98 10:50p Lawrance
101 * Don't break lock when switching to secondary weapons of the same type
103 * 51 4/20/98 12:36a Mike
104 * Make team vs. team work when player is hostile. Several targeting
107 * 50 4/15/98 12:55a Lawrance
108 * Make shockwave damage 1/4 if bomb detonated by another weapon. Allow
109 * missiles to lock on bombs.
111 * 49 4/13/98 4:52p Allender
112 * remove AI_frametime and us flFrametime instead. Make lock warning work
113 * in multiplayer for aspect seeking missiles. Debris fixups
115 * 48 4/08/98 8:33p Lawrance
116 * Make player re-acquire lock when targeting subsystems on a locked ship.
118 * 47 4/03/98 10:29a Mike
119 * Make aspect seekers home on a ship even if the targeted subsystem is
120 * backfacing. Too confusing.
122 * 46 3/26/98 5:46p Lawrance
123 * call obj_team() instead of ship_team_from_obj()
125 * 45 3/26/98 5:26p John
126 * added new paging code. nonfunctional.
128 * 44 3/10/98 4:19p John
129 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
130 * & Glide have popups and print screen. Took out all >8bpp software
131 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
132 * support Fred. Made zbuffering key off of functions rather than one
135 * 43 3/10/98 11:16a Lawrance
136 * Fix potential bug with missles not locking on a subsystem that appeared
139 * 42 2/28/98 7:03p Lawrance
140 * Change player missile locking to use dot product, so we can use it in
143 * 41 2/22/98 4:17p John
144 * More string externalization classification... 190 left to go!
146 * 40 1/25/98 10:31p Lawrance
147 * Don't draw most hud gauges when viewing from another ship.
149 * 39 1/24/98 4:48p Lawrance
150 * Add 'locking_subsys_parent', needed to save/restore locking_subsys
153 * 38 1/23/98 6:25p Lawrance
154 * Change player missile locking to lock on subsystem points automatically
156 * 37 1/21/98 7:20p Lawrance
157 * Make subsystem locking only work with line-of-sight, cleaned up locking
158 * code, moved globals to player struct.
160 * 36 1/19/98 10:02p Lawrance
161 * Fix bug with locking on friendlies
163 * 35 12/30/97 4:28p Lawrance
164 * remove .ani extensions from filenames
166 * 34 12/10/97 9:57a Lawrance
167 * allow missile locks on all ships but friendly
169 * 33 11/17/97 6:37p Lawrance
170 * new gauges: extended target view, new lock triangles, support ship view
172 * 32 11/17/97 5:51p Lawrance
173 * fix bug that was sometimes causing lock to not occur
175 * 31 11/13/97 10:46p Lawrance
176 * ensure only attempt lock on HOSTILE
178 * 30 11/11/97 12:59a Lawrance
179 * fix couple of bugs with lock indicator not going away
181 * 29 11/08/97 11:07p Lawrance
182 * add in new lock indicator
184 * 28 10/28/97 9:02a Lawrance
185 * don't loop lock sound, some minor reformatting
187 * 27 10/08/97 5:07p Lawrance
188 * don't lock on friendly ships
190 * 26 9/07/97 10:02p Lawrance
191 * don't lock on a ship if player doesn't have any missiles
193 * 25 7/02/97 9:35a Hoffoss
194 * Changed all references to weapon variables in ships to 'weapons'
195 * structure variables in ships.
197 * 24 6/05/97 1:07a Lawrance
198 * changes to support sound interface
200 * 23 5/20/97 2:45p Mike
201 * Move current_target and a bunch of other stuff out of player struct.
203 * 22 4/24/97 10:55a Lawrance
204 * added Show_lock_cone to DCF
206 * 21 4/18/97 2:54p Lawrance
207 * sounds now have a default volume, when playing, pass a scaling factor
208 * not the actual volume
210 * 20 4/15/97 8:36a Lawrance
211 * fixed bug where lock indicator was lagging locked target
213 * 19 4/13/97 3:53p Lawrance
214 * separate out the non-rendering dependant portions of the HUD ( sounds,
215 * updating lock position, changing targets, etc) and put into
218 * 18 4/12/97 4:29p Lawrance
219 * get missle locking and offscreen indicator working properly with
220 * different sized screens
222 * 17 4/10/97 5:29p Lawrance
223 * hud rendering split up into hud_render_3d(), hud_render_2d() and
224 * hud_render_target_model()
226 * 16 4/09/97 4:34p Lawrance
227 * allow looped sounds to be cut off after they complete the full sample
230 * 15 3/25/97 8:17p Lawrance
231 * don't allow ASPECT homing missles to lock on debris
233 * 14 3/19/97 5:53p Lawrance
234 * integrating new Misc_sounds[] array (replaces old Game_sounds
237 * 13 3/10/97 8:53a Lawrance
238 * using hud_stop_looped_locking_sounds() in place of
239 * hud_stop_looped_sounds()
241 * 12 3/07/97 4:37p Mike
242 * Make rockeye missile home.
243 * Remove UNKNOWN and NEUTRAL teams.
245 * 11 3/04/97 2:27p Lawrance
246 * check to ensure player ship has missile banks
248 * 10 3/04/97 11:19a Lawrance
249 * supporting banked weapons
251 * 9 1/23/97 12:00p Lawrance
252 * made lock sound play at normal volume.
254 * 8 1/20/97 7:58p John
255 * Fixed some link errors with testcode.
257 * 7 1/13/97 5:36p Lawrance
258 * integrating new Game_sounds structure for general game sounds
260 * 6 1/06/97 10:43p Lawrance
261 * Changes to make save/restore functional
263 * 5 1/02/97 7:12p Lawrance
264 * adding hooks for more sounds
266 * 4 1/02/97 10:32a Lawrance
267 * fixed some bugs related to stopping looped sounds when targets die and
270 * 3 12/24/96 4:32p Lawrance
271 * some minor improvements
273 * 2 12/23/96 7:53p Lawrance
274 * missile locking working in new source files
281 #include "hudtarget.h"
282 #include "hudreticle.h"
288 #include "freespace.h"
289 #include "gamesequence.h"
294 #include "linklist.h"
299 static float Lock_start_dist;
300 static int Rotate_time_id = 1; // timer id for controlling how often to rotate triangles around lock indicator
302 int Missile_track_loop = -1;
303 int Missile_lock_loop = -1;
305 int Lock_target_box_width[GR_NUM_RESOLUTIONS] = {
309 int Lock_target_box_height[GR_NUM_RESOLUTIONS] = {
314 // the locked triangles (that orbit lock indicator) dimensions
315 float Lock_triangle_base[GR_NUM_RESOLUTIONS] = {
319 float Lock_triangle_height[GR_NUM_RESOLUTIONS] = {
324 int Lock_gauge_half_w[GR_NUM_RESOLUTIONS] = {
328 int Lock_gauge_half_h[GR_NUM_RESOLUTIONS] = {
333 // hud_frames Lock_gauge;
334 int Lock_gauge_loaded = 0;
336 int Lock_gauge_draw = 0;
337 int Lock_gauge_draw_stamp = -1;
338 #define LOCK_GAUGE_BLINK_RATE 5 // blinks/sec
340 int Lockspin_half_w[GR_NUM_RESOLUTIONS] = {
344 int Lockspin_half_h[GR_NUM_RESOLUTIONS] = {
350 char Lock_fname[GR_NUM_RESOLUTIONS][MAX_FILENAME_LEN] = {
355 char Lockspin_fname[GR_NUM_RESOLUTIONS][MAX_FILENAME_LEN] = {
360 void hud_lock_determine_lock_point(vector *lock_world_pos_out);
362 // hud_init_missile_lock() is called at the beginning of a mission
364 void hud_init_missile_lock()
366 Players[Player_num].lock_indicator_start_x = -1;
367 Players[Player_num].lock_indicator_start_y = -1;
368 Players[Player_num].lock_indicator_visible = 0;
369 Player_ai->current_target_is_locked = 0;
371 Player_ai->last_secondary_index = -1;
375 // Load in the frames need for the lead indicator
376 if (!Lock_gauge_loaded) {
378 Lock_gauge.first_frame = bm_load_animation(Lock_fname[gr_screen.res], &Lock_gauge.num_frames);
379 if ( Lock_gauge.first_frame < 0 ) {
380 Warning(LOCATION,"Cannot load hud ani: Lock_fname[gr_screen.res]\n");
383 hud_anim_init(&Lock_gauge, 0, 0, Lock_fname[gr_screen.res]);
384 hud_anim_load(&Lock_gauge);
386 hud_anim_init(&Lock_anim, 0, 0, Lockspin_fname[gr_screen.res]);
387 hud_anim_load(&Lock_anim);
389 Lock_gauge_loaded = 1;
391 Lock_gauge_draw_stamp = -1;
396 void hud_draw_diamond(int x, int y, int width, int height)
401 int x1,x2,x3,x4,y1,y2,y3,y4;
415 gr_line(x1,y1,x2,y2);
416 gr_line(x2,y2,x3,y3);
417 gr_line(x3,y3,x4,y4);
418 gr_line(x4,y4,x1,y1);
422 // hud_show_lock_indicator() will display the lock indicator for homing missiles
423 void hud_show_lock_indicator(float frametime)
425 int target_objnum, sx, sy;
428 if (!Players[Player_num].lock_indicator_visible){
432 target_objnum = Player_ai->target_objnum;
433 Assert(target_objnum != -1);
434 targetp = &Objects[target_objnum];
436 // check to see if there are any missile to fire.. we don't want to show the
437 // lock indicator if there are missiles to fire.
438 if ( !ship_secondary_bank_has_ammo(Player_obj->instance) ) {
442 hud_set_iff_color(targetp);
443 // 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));
445 if (Player_ai->current_target_is_locked) {
446 sx = Player->current_target_sx;
447 sy = Player->current_target_sy;
448 // show the rotating triangles if target is locked
449 hud_draw_lock_triangles(sx, sy, frametime);
451 sx = Players[Player_num].lock_indicator_x;
452 sy = Players[Player_num].lock_indicator_y;
455 // show locked indicator
457 if ( Lock_gauge.first_frame >= 0 ) {
458 gr_set_bitmap(Lock_gauge.first_frame);
459 gr_aabitmap(sx - Lock_gauge_half_w[gr_screen.res], sy - Lock_gauge_half_h[gr_screen.res]);
461 hud_draw_diamond(sx, sy, Lock_target_box_width[gr_screen.res], Lock_target_box_height[gr_screen.res]);
464 Lock_gauge.sx = sx - Lock_gauge_half_w[gr_screen.res];
465 Lock_gauge.sy = sy - Lock_gauge_half_h[gr_screen.res];
466 if(Player_ai->current_target_is_locked){
467 Lock_gauge.time_elapsed = 0.0f;
468 hud_anim_render(&Lock_gauge, 0.0f, 1);
470 hud_anim_render(&Lock_gauge, frametime, 1);
474 // Reset data used for player lock indicator
475 void hud_lock_reset(float lock_time_scale)
480 swp = &Player_ship->weapons;
481 wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
483 Player_ai->current_target_is_locked = 0;
484 Players[Player_num].lock_indicator_visible = 0;
485 Player->target_in_lock_cone = 0;
486 Player->lock_time_to_target = i2fl(wip->min_lock_time*lock_time_scale);
487 Player->current_target_sx = -1;
488 Player->current_target_sy = -1;
489 Player->locking_subsys=NULL;
490 Player->locking_on_center=0;
491 Player->locking_subsys_parent=-1;
492 hud_stop_looped_locking_sounds();
494 Lock_gauge_draw_stamp = -1;
497 // reset the lock anim time elapsed
498 Lock_anim.time_elapsed = 0.0f;
501 // Determine if the locking code has a point to track
502 int hud_lock_has_homing_point()
504 if ( Player_ai->targeted_subsys || Player->locking_subsys || Player->locking_on_center ) {
510 int Nebula_sec_range = 0;
511 DCF_BOOL(nebula_sec_range, Nebula_sec_range)
513 int hud_lock_world_pos_in_range(vector *target_world_pos, vector *vec_to_target)
515 float dist_to_target, weapon_range;
519 int target_in_range=1;
521 swp = &Player_ship->weapons;
522 wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
524 vm_vec_sub(vec_to_target, target_world_pos, &Player_obj->pos);
525 dist_to_target = vm_vec_mag(vec_to_target);
527 // calculate the range of the weapon, and only display the lead target indicator when
528 // if the weapon can actually hit the target
529 weapon_range = wip->max_speed * wip->lifetime;
531 // reduce firing range in nebula
532 if ((The_mission.flags & MISSION_FLAG_FULLNEB) && Nebula_sec_range) {
533 weapon_range *= 0.8f;
536 if (dist_to_target > weapon_range) {
540 return target_in_range;
543 // Determine if point to lock on is in range
544 int hud_lock_target_in_range()
546 vector target_world_pos, vec_to_target;
549 if ( !hud_lock_has_homing_point() ) {
553 targetp = &Objects[Player_ai->target_objnum];
555 if ( Player_ai->targeted_subsys != NULL ) {
556 vm_vec_unrotate(&target_world_pos, &Player_ai->targeted_subsys->system_info->pnt, &targetp->orient);
557 vm_vec_add2(&target_world_pos, &targetp->pos);
559 if ( Player->locking_subsys ) {
560 vm_vec_unrotate(&target_world_pos, &Player->locking_subsys->system_info->pnt, &targetp->orient);
561 vm_vec_add2(&target_world_pos, &targetp->pos);
563 Assert(Player->locking_on_center);
564 target_world_pos = targetp->pos;
568 return hud_lock_world_pos_in_range(&target_world_pos, &vec_to_target);
575 target_team = obj_team(&Objects[Player_ai->target_objnum]);
577 if ( Player_ship->weapons.num_secondary_banks <= 0 ) {
581 if ( Player_ship->weapons.current_secondary_bank < 0 ) {
585 // check to see if there are any missile to fire.. we don't want to show the
586 // lock indicator if there are no missiles to fire.
587 if ( !ship_secondary_bank_has_ammo(Player_obj->instance) ) {
591 // if the target is friendly, don't lock!
592 if ( hud_team_matches_filter(Player_ship->team, target_team)) {
593 // if we're in multiplayer dogfight, ignore this
594 if(!((Game_mode & GM_MULTIPLAYER) && (Netgame.type_flags & NG_TYPE_DOGFIGHT))){
602 // determine if the subsystem to lock on to has a direct line of sight
603 int hud_lock_on_subsys_ok()
610 Assert(Player_ai->target_objnum >= 0);
611 target_objp = &Objects[Player_ai->target_objnum];
613 subsys = Player_ai->targeted_subsys;
618 vm_vec_unrotate(&subobj_pos, &subsys->system_info->pnt, &target_objp->orient);
619 vm_vec_add2(&subobj_pos, &target_objp->pos);
621 if ( Player->subsys_in_view < 0 ) {
622 in_sight = ship_subsystem_in_sight(target_objp, subsys, &View_position, &subobj_pos);
624 in_sight = Player->subsys_in_view;
630 // Determine if locking point is in the locking cone
631 void hud_lock_check_if_target_in_lock_cone(vector *lock_world_pos)
634 vector vec_to_target;
636 dist = vm_vec_normalized_dir(&vec_to_target, lock_world_pos, &Player_obj->pos);
637 dot = vm_vec_dot(&Player_obj->orient.fvec, &vec_to_target);
640 Player->target_in_lock_cone = 1;
642 Player->target_in_lock_cone = 0;
647 // return 1 if current secondary weapon is different than previous secondary weapon
648 int hud_lock_secondary_weapon_changed(ship_weapon *swp)
651 if ( swp->current_secondary_bank != Player_ai->last_secondary_index ) {
657 int last_wi_index = -1;
658 int current_wi_index = -1;
661 // do a quick out if same bank is selected
662 if ( swp->current_secondary_bank == Player_ai->last_secondary_index ) {
666 // bank has changed, but it still may be the same weapon type
667 if ( swp->current_secondary_bank >= 0 ) {
668 current_wi_index = swp->secondary_bank_weapons[swp->current_secondary_bank];
671 if ( Player_ai->last_secondary_index >= 0 ) {
672 last_wi_index = swp->secondary_bank_weapons[Player_ai->last_secondary_index];
675 if ( current_wi_index != last_wi_index ) {
684 // hud_update_lock_indicator() will manage the non-rendering dependant part of
686 void hud_update_lock_indicator(float frametime)
690 vector lock_world_pos;
692 // if i'm a multiplayer observer, bail here
693 if((Game_mode & GM_MULTIPLAYER) && ((Net_player->flags & NETINFO_FLAG_OBSERVER) || (Player_obj->type == OBJ_OBSERVER)) ){
697 Assert(Player_ai->target_objnum != -1);
699 // be sure to unset this flag, then possibly set later in this function so that
700 // threat indicators work properly.
701 Player_ai->ai_flags &= ~AIF_SEEK_LOCK;
703 if ( hud_abort_lock() ) {
708 // if there is an EMP effect active, never update lock
709 if(emp_active_local()){
714 swp = &Player_ship->weapons;
715 wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
717 Lock_start_dist = wip->min_lock_time * wip->lock_pixels_per_sec;
719 // if secondary weapons change, reset the lock
720 if ( hud_lock_secondary_weapon_changed(swp) ) {
724 Player_ai->last_secondary_index = swp->current_secondary_bank;
726 if ( !(wip->wi_flags & WIF_HOMING_ASPECT) ) {
731 // Allow locking on ships and bombs (only targeted weapon allowed is a bomb, so don't bother checking flags)
732 if ( (Objects[Player_ai->target_objnum].type != OBJ_SHIP) && (Objects[Player_ai->target_objnum].type != OBJ_WEAPON) ) {
737 hud_lock_determine_lock_point(&lock_world_pos);
739 if ( !hud_lock_has_homing_point() ) {
740 Player->target_in_lock_cone=0;
743 hud_lock_check_if_target_in_lock_cone(&lock_world_pos);
745 // check if the target is within range of the current secondary weapon. If it is not,
746 // a lock will not be detected
747 if ( !hud_lock_target_in_range() ) {
748 Player->target_in_lock_cone = 0;
751 // If locking on a subsystem, and not in sight... can't lock
752 // Changed by MK on 4/3/98. It was confusing me that my hornets would not lock on my target.
753 // It will now be confusing that they lock, but don't home on your subsystem, but I think that's preferable.
754 // Often you really care about destroying the target, not just the subsystem.
755 /*if ( Player_ai->targeted_subsys ) {
756 if ( !hud_lock_on_subsys_ok() ) {
757 Player->target_in_lock_cone=0;
761 if ( !Player->target_in_lock_cone ) {
762 Player->locking_on_center=0;
763 Player->locking_subsys_parent=-1;
764 Player->locking_subsys=NULL;
767 hud_calculate_lock_position(frametime);
769 if (!Players[Player_num].lock_indicator_visible)
772 if (Player_ai->current_target_is_locked) {
773 if ( Missile_track_loop > -1 ) {
774 snd_chg_loop_status(Missile_track_loop, 0);
775 Missile_track_loop = -1;
776 Missile_lock_loop = snd_play(&Snds[SND_MISSILE_LOCK]);
780 Player_ai->ai_flags |= AIF_SEEK_LOCK; // set this flag so multiplayer's properly track lock on other ships
781 if ( Missile_lock_loop != -1 && snd_is_playing(Missile_lock_loop) ) {
782 snd_stop(Missile_lock_loop);
783 Missile_lock_loop = -1;
788 // hud_draw_lock_triangles() will draw the 4 rotating triangles around a lock indicator
789 // (This is done when a lock has been acquired)
790 #define ROTATE_DELAY 40
791 void hud_draw_lock_triangles_old(int center_x, int center_y, int radius)
793 static float ang = 0.0f;
795 float end_ang = ang + 2*PI;
796 float x3,y3,x4,y4,xpos,ypos;
798 if ( timestamp_elapsed(Rotate_time_id) ) {
799 Rotate_time_id = timestamp(ROTATE_DELAY);
803 for (; ang <= end_ang; ang += PI/2.0f) {
805 // draw the orbiting triangles
807 //ang = atan2(target_point.y,target_point.x);
808 xpos = center_x + (float)cos(ang)*(radius + Lock_triangle_height[gr_screen.res] + 2);
809 ypos = center_y - (float)sin(ang)*(radius + Lock_triangle_height[gr_screen.res] + 2);
811 x3 = xpos - Lock_triangle_base[gr_screen.res] * (float)sin(-ang);
812 y3 = ypos + Lock_triangle_base[gr_screen.res] * (float)cos(-ang);
813 x4 = xpos + Lock_triangle_base[gr_screen.res] * (float)sin(-ang);
814 y4 = ypos - Lock_triangle_base[gr_screen.res] * (float)cos(-ang);
816 xpos = xpos - Lock_triangle_base[gr_screen.res] * (float)cos(ang);
817 ypos = ypos + Lock_triangle_base[gr_screen.res] * (float)sin(ang);
819 hud_tri(x3, y3, xpos, ypos, x4, y4);
823 // draw a frame of the rotating lock triangles animation
824 void hud_draw_lock_triangles(int center_x, int center_y, float frametime)
826 if ( Lock_anim.first_frame == -1 ) {
827 hud_draw_lock_triangles_old(center_x, center_y, Lock_target_box_width[gr_screen.res]/2);
830 Lock_anim.sx = center_x - Lockspin_half_w[gr_screen.res];
831 Lock_anim.sy = center_y - Lockspin_half_h[gr_screen.res];
833 // if its still animating
834 if(Lock_anim.time_elapsed < Lock_anim.total_time){
835 hud_anim_render(&Lock_anim, frametime, 1, 0, 1);
837 // if the timestamp is unset or expired
838 if((Lock_gauge_draw_stamp < 0) || timestamp_elapsed(Lock_gauge_draw_stamp)){
840 Lock_gauge_draw_stamp = timestamp(1000 / (2 * LOCK_GAUGE_BLINK_RATE));
842 // switch between draw and dont-draw
843 Lock_gauge_draw = !Lock_gauge_draw;
846 // maybe draw the anim
847 Lock_gauge.time_elapsed = 0.0f;
849 hud_anim_render(&Lock_anim, frametime, 1, 0, 1);
855 // hud_calculate_lock_position() will determine where on the screen to draw the lock
856 // indicator, and will determine when a lock has occurred. If the lock indicator is not
857 // on the screen yet, hud_calculate_lock_start_pos() is called to pick a starting location
858 void hud_calculate_lock_position(float frametime)
863 static float pixels_moved_while_locking;
864 static float pixels_moved_while_degrading;
865 static int Need_new_start_pos = 0;
867 static double accumulated_x_pixels, accumulated_y_pixels;
870 static float last_dist_to_target;
872 static int catching_up;
874 static int maintain_lock_count = 0;
876 static float catch_up_distance = 0.0f;
878 double hypotenuse, delta_x, delta_y;
880 swp = &Player_ship->weapons;
881 wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
883 if (Player->target_in_lock_cone) {
884 if (!Players[Player_num].lock_indicator_visible) {
885 hud_calculate_lock_start_pos();
886 last_dist_to_target = 0.0f;
888 Players[Player_num].lock_indicator_x = Players[Player_num].lock_indicator_start_x;
889 Players[Player_num].lock_indicator_y = Players[Player_num].lock_indicator_start_y;
890 Players[Player_num].lock_indicator_visible = 1;
892 Players[Player_num].lock_time_to_target = i2fl(wip->min_lock_time);
896 Need_new_start_pos = 1;
898 if (Player_ai->current_target_is_locked) {
899 Players[Player_num].lock_indicator_x = Player->current_target_sx;
900 Players[Player_num].lock_indicator_y = Player->current_target_sy;
904 delta_x = Players[Player_num].lock_indicator_x - Player->current_target_sx;
905 delta_y = Players[Player_num].lock_indicator_y - Player->current_target_sy;
907 if (!delta_y && !delta_x) {
911 hypotenuse = _hypot(delta_y, delta_x);
914 Players[Player_num].lock_dist_to_target = (float)hypotenuse;
916 if (last_dist_to_target == 0) {
917 last_dist_to_target = Players[Player_num].lock_dist_to_target;
920 //nprintf(("Alan","dist to target: %.2f\n",Players[Player_num].lock_dist_to_target));
921 //nprintf(("Alan","last to target: %.2f\n\n",last_dist_to_target));
924 //nprintf(("Alan","IN CATCH UP MODE catch_up_dist is %.2f\n",catch_up_distance));
925 if ( Players[Player_num].lock_dist_to_target < catch_up_distance )
929 //nprintf(("Alan","IN NORMAL MODE\n"));
930 if ( (Players[Player_num].lock_dist_to_target - last_dist_to_target) > 2.0f ) {
932 catch_up_distance = last_dist_to_target + wip->catchup_pixel_penalty;
936 last_dist_to_target = Players[Player_num].lock_dist_to_target;
939 Players[Player_num].lock_time_to_target -= frametime;
940 if (Players[Player_num].lock_time_to_target < 0.0f)
941 Players[Player_num].lock_time_to_target = 0.0f;
944 float lock_pixels_per_sec;
945 if (Players[Player_num].lock_time_to_target > 0) {
946 lock_pixels_per_sec = Players[Player_num].lock_dist_to_target / Players[Player_num].lock_time_to_target;
948 lock_pixels_per_sec = i2fl(wip->lock_pixels_per_sec);
951 if (lock_pixels_per_sec > wip->lock_pixels_per_sec) {
952 lock_pixels_per_sec = i2fl(wip->lock_pixels_per_sec);
956 pixels_moved_while_locking = wip->catchup_pixels_per_sec * frametime;
958 pixels_moved_while_locking = lock_pixels_per_sec * frametime;
962 accumulated_x_pixels += pixels_moved_while_locking * delta_x/hypotenuse;
966 accumulated_y_pixels += pixels_moved_while_locking * delta_y/hypotenuse;
969 if (fl_abs(accumulated_x_pixels) > 1.0f) {
970 modf(accumulated_x_pixels, &int_portion);
972 Players[Player_num].lock_indicator_x -= (int)int_portion;
974 if ( fl_abs(Players[Player_num].lock_indicator_x - Player->current_target_sx) < fl_abs(int_portion) )
975 Players[Player_num].lock_indicator_x = Player->current_target_sx;
977 accumulated_x_pixels -= int_portion;
980 if (fl_abs(accumulated_y_pixels) > 1.0f) {
981 modf(accumulated_y_pixels, &int_portion);
983 Players[Player_num].lock_indicator_y -= (int)int_portion;
985 if ( fl_abs(Players[Player_num].lock_indicator_y - Player->current_target_sy) < fl_abs(int_portion) )
986 Players[Player_num].lock_indicator_y = Player->current_target_sy;
988 accumulated_y_pixels -= int_portion;
991 if ( Missile_track_loop == -1 ) {
992 Missile_track_loop = snd_play_looping( &Snds[SND_MISSILE_TRACKING], 0.0f , -1, -1);
995 if (!Players[Player_num].lock_time_to_target) {
996 if ( (Players[Player_num].lock_indicator_x == Player->current_target_sx) && (Players[Player_num].lock_indicator_y == Player->current_target_sy) ) {
997 if (maintain_lock_count++ > 1) {
998 Player_ai->current_target_is_locked = 1;
1001 maintain_lock_count = 0;
1007 if ( Missile_track_loop > -1 ) {
1008 snd_chg_loop_status(Missile_track_loop, 0);
1009 Missile_track_loop = -1;
1012 Player_ai->current_target_is_locked = 0;
1014 if (!Players[Player_num].lock_indicator_visible) {
1019 last_dist_to_target = 0.0f;
1021 if (Need_new_start_pos) {
1022 hud_calculate_lock_start_pos();
1023 Need_new_start_pos = 0;
1024 accumulated_x_pixels = 0.0f;
1025 accumulated_y_pixels = 0.0f;
1028 delta_x = Players[Player_num].lock_indicator_x - Players[Player_num].lock_indicator_start_x;
1029 delta_y = Players[Player_num].lock_indicator_y - Players[Player_num].lock_indicator_start_y;
1031 if (!delta_y && !delta_x) {
1035 hypotenuse = _hypot(delta_y, delta_x);
1038 Players[Player_num].lock_time_to_target += frametime;
1040 if (Players[Player_num].lock_time_to_target > wip->min_lock_time)
1041 Players[Player_num].lock_time_to_target = i2fl(wip->min_lock_time);
1043 pixels_moved_while_degrading = 2.0f * wip->lock_pixels_per_sec * frametime;
1046 accumulated_x_pixels += pixels_moved_while_degrading * delta_x/hypotenuse;
1049 accumulated_y_pixels += pixels_moved_while_degrading * delta_y/hypotenuse;
1051 if (fl_abs(accumulated_x_pixels) > 1.0f) {
1052 modf(accumulated_x_pixels, &int_portion);
1054 Players[Player_num].lock_indicator_x -= (int)int_portion;
1056 if ( fl_abs(Players[Player_num].lock_indicator_x - Players[Player_num].lock_indicator_start_x) < fl_abs(int_portion) )
1057 Players[Player_num].lock_indicator_x = Players[Player_num].lock_indicator_start_x;
1059 accumulated_x_pixels -= int_portion;
1062 if (fl_abs(accumulated_y_pixels) > 1.0f) {
1063 modf(accumulated_y_pixels, &int_portion);
1065 Players[Player_num].lock_indicator_y -= (int)int_portion;
1067 if ( fl_abs(Players[Player_num].lock_indicator_y - Players[Player_num].lock_indicator_start_y) < fl_abs(int_portion) )
1068 Players[Player_num].lock_indicator_y = Players[Player_num].lock_indicator_start_y;
1070 accumulated_y_pixels -= int_portion;
1073 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) ) {
1074 Players[Player_num].lock_indicator_visible = 0;
1079 // hud_calculate_lock_start_pos() will determine where to draw the starting location of the lock
1080 // indicator. It does this by picking a location that is Lock_start_dist pixels away from the current
1081 // target (in 2D). This is accomplished by finding the endpoint of a line that passes through the
1082 // origin, and connects the target and lock indicator postion (and has a magnitude of Lock_start_dist)
1083 void hud_calculate_lock_start_pos()
1088 double target_mag, target_x, target_y;
1090 delta_x = Player->current_target_sx - SCREEN_CENTER_X;
1091 delta_y = Player->current_target_sy - SCREEN_CENTER_Y;
1093 if (!delta_x && !delta_y) {
1094 Players[Player_num].lock_indicator_start_x = fl2i(SCREEN_CENTER_X + Lock_start_dist);
1095 Players[Player_num].lock_indicator_start_y = fl2i(SCREEN_CENTER_Y);
1099 hypotenuse = _hypot(delta_y, delta_x);
1101 if (hypotenuse >= Lock_start_dist) {
1102 Players[Player_num].lock_indicator_start_x = fl2i(SCREEN_CENTER_X);
1103 Players[Player_num].lock_indicator_start_y = fl2i(SCREEN_CENTER_Y);
1107 target_mag = Lock_start_dist - hypotenuse;
1108 target_x = target_mag * (delta_x / hypotenuse);
1109 target_y = target_mag * (delta_y / hypotenuse);
1111 Players[Player_num].lock_indicator_start_x = fl2i(SCREEN_CENTER_X - target_x);
1112 Players[Player_num].lock_indicator_start_y = fl2i(SCREEN_CENTER_Y - target_y);
1114 if (Players[Player_num].lock_indicator_start_x > gr_screen.clip_right)
1115 Players[Player_num].lock_indicator_start_x = gr_screen.clip_right;
1117 if (Players[Player_num].lock_indicator_start_y > gr_screen.clip_bottom)
1118 Players[Player_num].lock_indicator_start_y = gr_screen.clip_bottom;
1120 if (Players[Player_num].lock_indicator_start_x < gr_screen.clip_left)
1121 Players[Player_num].lock_indicator_start_x = gr_screen.clip_left;
1123 if (Players[Player_num].lock_indicator_start_y < gr_screen.clip_top)
1124 Players[Player_num].lock_indicator_start_y = gr_screen.clip_top;
1127 // hud_stop_looped_locking_sounds() will terminate any hud related looping sounds that are playing
1128 void hud_stop_looped_locking_sounds()
1130 if ( Missile_track_loop > -1 ) {
1131 snd_stop(Missile_track_loop);
1132 Missile_track_loop = -1;
1136 // Get a new world pos for the locking point
1137 void hud_lock_update_lock_pos(object *target_objp, vector *lock_world_pos)
1139 if ( Player_ai->targeted_subsys ) {
1140 get_subsystem_world_pos(target_objp, Player_ai->targeted_subsys, lock_world_pos);
1144 if ( Player->locking_on_center ) {
1145 *lock_world_pos = target_objp->pos;
1147 Assert(Player->locking_subsys);
1148 get_subsystem_world_pos(target_objp, Player->locking_subsys, lock_world_pos);
1152 // Try and find a new locking point
1153 void hud_lock_get_new_lock_pos(object *target_objp, vector *lock_world_pos)
1155 ship *target_shipp=NULL;
1156 int lock_in_range=0;
1157 float best_lock_dot=-1.0f, lock_dot=-1.0f;
1159 vector subsys_world_pos, vec_to_lock;
1161 if ( target_objp->type == OBJ_SHIP ) {
1162 target_shipp = &Ships[target_objp->instance];
1165 // if a large ship, lock to pos closest to center and within range
1166 if ( (target_shipp) && (Ship_info[target_shipp->ship_info_index].flags & (SIF_BIG_SHIP|SIF_HUGE_SHIP)) ) {
1167 // check all the subsystems and the center of the ship
1169 // assume best lock pos is the center of the ship
1170 *lock_world_pos=target_objp->pos;
1171 Player->locking_on_center=1;
1172 Player->locking_subsys=NULL;
1173 Player->locking_subsys_parent=-1;
1174 lock_in_range = hud_lock_world_pos_in_range(lock_world_pos, &vec_to_lock);
1175 vm_vec_normalize(&vec_to_lock);
1176 if ( lock_in_range ) {
1177 best_lock_dot=vm_vec_dot(&Player_obj->orient.fvec, &vec_to_lock);
1179 // take center if reasonable dot
1180 if ( best_lock_dot > 0.95 ) {
1184 // iterate through subsystems to see if we can get a better choice
1185 ss = GET_FIRST(&target_shipp->subsys_list);
1186 while ( ss != END_OF_LIST( &target_shipp->subsys_list ) ) {
1188 // get world pos of subsystem
1189 get_subsystem_world_pos(target_objp, ss, &subsys_world_pos);
1191 if ( hud_lock_world_pos_in_range(&subsys_world_pos, &vec_to_lock) ) {
1192 vm_vec_normalize(&vec_to_lock);
1193 lock_dot=vm_vec_dot(&Player_obj->orient.fvec, &vec_to_lock);
1194 if ( lock_dot > best_lock_dot ) {
1195 best_lock_dot=lock_dot;
1196 Player->locking_on_center=0;
1197 Player->locking_subsys=ss;
1198 Player->locking_subsys_parent=Player_ai->target_objnum;
1199 *lock_world_pos=subsys_world_pos;
1202 ss = GET_NEXT( ss );
1205 // if small ship (or weapon), just go for the center
1206 *lock_world_pos = target_objp->pos;
1207 Player->locking_on_center=1;
1208 Player->locking_subsys=NULL;
1209 Player->locking_subsys_parent=-1;
1213 // Decide which point lock should be homing on
1214 void hud_lock_determine_lock_point(vector *lock_world_pos_out)
1216 vector lock_world_pos;
1218 object *target_objp;
1220 Assert(Player_ai->target_objnum >= 0);
1221 target_objp = &Objects[Player_ai->target_objnum];
1223 Player->current_target_sx = -1;
1224 Player->current_target_sx = -1;
1226 // If subsystem is targeted, we must try to lock on that
1227 if ( Player_ai->targeted_subsys ) {
1228 hud_lock_update_lock_pos(target_objp, &lock_world_pos);
1229 Player->locking_on_center=0;
1230 Player->locking_subsys=NULL;
1231 Player->locking_subsys_parent=-1;
1233 // See if we already have a successful locked point
1234 if ( hud_lock_has_homing_point() ) {
1235 hud_lock_update_lock_pos(target_objp, &lock_world_pos);
1237 hud_lock_get_new_lock_pos(target_objp, &lock_world_pos);
1241 *lock_world_pos_out=lock_world_pos;
1243 g3_rotate_vertex(&lock_point,&lock_world_pos);
1244 g3_project_vertex(&lock_point);
1246 if (!(lock_point.flags & PF_OVERFLOW)) { // make sure point projected
1247 Player->current_target_sx = (int)lock_point.sx;
1248 Player->current_target_sy = (int)lock_point.sy;
1252 void hudlock_page_in()
1254 bm_page_in_aabitmap( Lock_gauge.first_frame, Lock_gauge.num_frames );