2 * $Logfile: /Freespace2/code/Hud/HUDlock.cpp $
7 * C module that controls missile locking
10 * Revision 1.1 2002/05/03 03:28:09 root
14 * 19 8/09/99 10:50a Jasenw
16 * 18 7/30/99 5:42p Jasenw
17 * changed coords to center HUD lock indicator.
19 * 17 7/28/99 2:49p Andsager
20 * Make hud target speed use vm_vec_mag (not vm_vec_mag_quick)
22 * 16 6/08/99 7:41p Dave
23 * Animated hud lockup icon a bit differently.
25 * 15 6/08/99 8:35a Jasenw
26 * new coords for new lock ani
28 * 14 6/07/99 4:20p Andsager
29 * Add HUD color for tagged object. Apply to target and radar.
31 * 13 6/07/99 1:42p Jasenw
33 * 12 6/04/99 10:58a Dave
34 * Updated hud lock gauge.
36 * 11 6/03/99 2:32p Jasenw
37 * changed coords for new HUD stuff
39 * 10 6/02/99 5:41p Andsager
40 * Reduce range of secondary weapons not fired from turrets in nebula.
41 * Reduce range of beams fired from turrrets in nebula
43 * 9 4/23/99 12:01p Johnson
46 * 8 2/25/99 4:19p Dave
47 * Added multiplayer_beta defines. Added cd_check define. Fixed a few
48 * release build warnings. Added more data to the squad war request and
51 * 7 1/07/99 9:07a Jasen
54 * 6 12/30/98 7:47a Jasen
55 * added coordinates for hi res
57 * 5 12/28/98 3:17p Dave
58 * Support for multiple hud bitmap filenames for hi-res mode.
60 * 4 12/21/98 5:02p Dave
61 * Modified all hud elements to be multi-resolution friendly.
63 * 3 11/05/98 5:55p Dave
64 * Big pass at reducing #includes
66 * 2 10/07/98 10:53a Dave
69 * 1 10/07/98 10:49a Dave
71 * 55 8/25/98 1:48p Dave
72 * First rev of EMP effect. Player side stuff basically done. Next comes
75 * 54 5/11/98 3:48p Lawrance
76 * Break lock when banks switch
78 * 53 5/10/98 7:05p Dave
79 * Fix endgame sequencing ESC key. Changed how host options warning popups
80 * are done. Fixed pause/message scrollback/options screen problems in mp.
81 * Make sure observer HUD doesn't try to lock weapons.
83 * 52 5/04/98 10:50p Lawrance
84 * Don't break lock when switching to secondary weapons of the same type
86 * 51 4/20/98 12:36a Mike
87 * Make team vs. team work when player is hostile. Several targeting
90 * 50 4/15/98 12:55a Lawrance
91 * Make shockwave damage 1/4 if bomb detonated by another weapon. Allow
92 * missiles to lock on bombs.
94 * 49 4/13/98 4:52p Allender
95 * remove AI_frametime and us flFrametime instead. Make lock warning work
96 * in multiplayer for aspect seeking missiles. Debris fixups
98 * 48 4/08/98 8:33p Lawrance
99 * Make player re-acquire lock when targeting subsystems on a locked ship.
101 * 47 4/03/98 10:29a Mike
102 * Make aspect seekers home on a ship even if the targeted subsystem is
103 * backfacing. Too confusing.
105 * 46 3/26/98 5:46p Lawrance
106 * call obj_team() instead of ship_team_from_obj()
108 * 45 3/26/98 5:26p John
109 * added new paging code. nonfunctional.
111 * 44 3/10/98 4:19p John
112 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
113 * & Glide have popups and print screen. Took out all >8bpp software
114 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
115 * support Fred. Made zbuffering key off of functions rather than one
118 * 43 3/10/98 11:16a Lawrance
119 * Fix potential bug with missles not locking on a subsystem that appeared
122 * 42 2/28/98 7:03p Lawrance
123 * Change player missile locking to use dot product, so we can use it in
126 * 41 2/22/98 4:17p John
127 * More string externalization classification... 190 left to go!
129 * 40 1/25/98 10:31p Lawrance
130 * Don't draw most hud gauges when viewing from another ship.
132 * 39 1/24/98 4:48p Lawrance
133 * Add 'locking_subsys_parent', needed to save/restore locking_subsys
136 * 38 1/23/98 6:25p Lawrance
137 * Change player missile locking to lock on subsystem points automatically
139 * 37 1/21/98 7:20p Lawrance
140 * Make subsystem locking only work with line-of-sight, cleaned up locking
141 * code, moved globals to player struct.
143 * 36 1/19/98 10:02p Lawrance
144 * Fix bug with locking on friendlies
146 * 35 12/30/97 4:28p Lawrance
147 * remove .ani extensions from filenames
149 * 34 12/10/97 9:57a Lawrance
150 * allow missile locks on all ships but friendly
152 * 33 11/17/97 6:37p Lawrance
153 * new gauges: extended target view, new lock triangles, support ship view
155 * 32 11/17/97 5:51p Lawrance
156 * fix bug that was sometimes causing lock to not occur
158 * 31 11/13/97 10:46p Lawrance
159 * ensure only attempt lock on HOSTILE
161 * 30 11/11/97 12:59a Lawrance
162 * fix couple of bugs with lock indicator not going away
164 * 29 11/08/97 11:07p Lawrance
165 * add in new lock indicator
167 * 28 10/28/97 9:02a Lawrance
168 * don't loop lock sound, some minor reformatting
170 * 27 10/08/97 5:07p Lawrance
171 * don't lock on friendly ships
173 * 26 9/07/97 10:02p Lawrance
174 * don't lock on a ship if player doesn't have any missiles
176 * 25 7/02/97 9:35a Hoffoss
177 * Changed all references to weapon variables in ships to 'weapons'
178 * structure variables in ships.
180 * 24 6/05/97 1:07a Lawrance
181 * changes to support sound interface
183 * 23 5/20/97 2:45p Mike
184 * Move current_target and a bunch of other stuff out of player struct.
186 * 22 4/24/97 10:55a Lawrance
187 * added Show_lock_cone to DCF
189 * 21 4/18/97 2:54p Lawrance
190 * sounds now have a default volume, when playing, pass a scaling factor
191 * not the actual volume
193 * 20 4/15/97 8:36a Lawrance
194 * fixed bug where lock indicator was lagging locked target
196 * 19 4/13/97 3:53p Lawrance
197 * separate out the non-rendering dependant portions of the HUD ( sounds,
198 * updating lock position, changing targets, etc) and put into
201 * 18 4/12/97 4:29p Lawrance
202 * get missle locking and offscreen indicator working properly with
203 * different sized screens
205 * 17 4/10/97 5:29p Lawrance
206 * hud rendering split up into hud_render_3d(), hud_render_2d() and
207 * hud_render_target_model()
209 * 16 4/09/97 4:34p Lawrance
210 * allow looped sounds to be cut off after they complete the full sample
213 * 15 3/25/97 8:17p Lawrance
214 * don't allow ASPECT homing missles to lock on debris
216 * 14 3/19/97 5:53p Lawrance
217 * integrating new Misc_sounds[] array (replaces old Game_sounds
220 * 13 3/10/97 8:53a Lawrance
221 * using hud_stop_looped_locking_sounds() in place of
222 * hud_stop_looped_sounds()
224 * 12 3/07/97 4:37p Mike
225 * Make rockeye missile home.
226 * Remove UNKNOWN and NEUTRAL teams.
228 * 11 3/04/97 2:27p Lawrance
229 * check to ensure player ship has missile banks
231 * 10 3/04/97 11:19a Lawrance
232 * supporting banked weapons
234 * 9 1/23/97 12:00p Lawrance
235 * made lock sound play at normal volume.
237 * 8 1/20/97 7:58p John
238 * Fixed some link errors with testcode.
240 * 7 1/13/97 5:36p Lawrance
241 * integrating new Game_sounds structure for general game sounds
243 * 6 1/06/97 10:43p Lawrance
244 * Changes to make save/restore functional
246 * 5 1/02/97 7:12p Lawrance
247 * adding hooks for more sounds
249 * 4 1/02/97 10:32a Lawrance
250 * fixed some bugs related to stopping looped sounds when targets die and
253 * 3 12/24/96 4:32p Lawrance
254 * some minor improvements
256 * 2 12/23/96 7:53p Lawrance
257 * missile locking working in new source files
264 #include "hudtarget.h"
265 #include "hudreticle.h"
271 #include "freespace.h"
272 #include "gamesequence.h"
277 #include "linklist.h"
282 static float Lock_start_dist;
283 static int Rotate_time_id = 1; // timer id for controlling how often to rotate triangles around lock indicator
285 int Missile_track_loop = -1;
286 int Missile_lock_loop = -1;
288 int Lock_target_box_width[GR_NUM_RESOLUTIONS] = {
292 int Lock_target_box_height[GR_NUM_RESOLUTIONS] = {
297 // the locked triangles (that orbit lock indicator) dimensions
298 float Lock_triangle_base[GR_NUM_RESOLUTIONS] = {
302 float Lock_triangle_height[GR_NUM_RESOLUTIONS] = {
307 int Lock_gauge_half_w[GR_NUM_RESOLUTIONS] = {
311 int Lock_gauge_half_h[GR_NUM_RESOLUTIONS] = {
316 // hud_frames Lock_gauge;
317 int Lock_gauge_loaded = 0;
319 int Lock_gauge_draw = 0;
320 int Lock_gauge_draw_stamp = -1;
321 #define LOCK_GAUGE_BLINK_RATE 5 // blinks/sec
323 int Lockspin_half_w[GR_NUM_RESOLUTIONS] = {
327 int Lockspin_half_h[GR_NUM_RESOLUTIONS] = {
333 char Lock_fname[GR_NUM_RESOLUTIONS][MAX_FILENAME_LEN] = {
338 char Lockspin_fname[GR_NUM_RESOLUTIONS][MAX_FILENAME_LEN] = {
343 void hud_lock_determine_lock_point(vector *lock_world_pos_out);
345 // hud_init_missile_lock() is called at the beginning of a mission
347 void hud_init_missile_lock()
349 Players[Player_num].lock_indicator_start_x = -1;
350 Players[Player_num].lock_indicator_start_y = -1;
351 Players[Player_num].lock_indicator_visible = 0;
352 Player_ai->current_target_is_locked = 0;
354 Player_ai->last_secondary_index = -1;
358 // Load in the frames need for the lead indicator
359 if (!Lock_gauge_loaded) {
361 Lock_gauge.first_frame = bm_load_animation(Lock_fname[gr_screen.res], &Lock_gauge.num_frames);
362 if ( Lock_gauge.first_frame < 0 ) {
363 Warning(LOCATION,"Cannot load hud ani: Lock_fname[gr_screen.res]\n");
366 hud_anim_init(&Lock_gauge, 0, 0, Lock_fname[gr_screen.res]);
367 hud_anim_load(&Lock_gauge);
369 hud_anim_init(&Lock_anim, 0, 0, Lockspin_fname[gr_screen.res]);
370 hud_anim_load(&Lock_anim);
372 Lock_gauge_loaded = 1;
374 Lock_gauge_draw_stamp = -1;
379 void hud_draw_diamond(int x, int y, int width, int height)
384 int x1,x2,x3,x4,y1,y2,y3,y4;
398 gr_line(x1,y1,x2,y2);
399 gr_line(x2,y2,x3,y3);
400 gr_line(x3,y3,x4,y4);
401 gr_line(x4,y4,x1,y1);
405 // hud_show_lock_indicator() will display the lock indicator for homing missiles
406 void hud_show_lock_indicator(float frametime)
408 int target_objnum, sx, sy;
411 if (!Players[Player_num].lock_indicator_visible){
415 target_objnum = Player_ai->target_objnum;
416 Assert(target_objnum != -1);
417 targetp = &Objects[target_objnum];
419 // check to see if there are any missile to fire.. we don't want to show the
420 // lock indicator if there are missiles to fire.
421 if ( !ship_secondary_bank_has_ammo(Player_obj->instance) ) {
425 hud_set_iff_color(targetp);
426 // 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));
428 if (Player_ai->current_target_is_locked) {
429 sx = Player->current_target_sx;
430 sy = Player->current_target_sy;
431 // show the rotating triangles if target is locked
432 hud_draw_lock_triangles(sx, sy, frametime);
434 sx = Players[Player_num].lock_indicator_x;
435 sy = Players[Player_num].lock_indicator_y;
438 // show locked indicator
440 if ( Lock_gauge.first_frame >= 0 ) {
441 gr_set_bitmap(Lock_gauge.first_frame);
442 gr_aabitmap(sx - Lock_gauge_half_w[gr_screen.res], sy - Lock_gauge_half_h[gr_screen.res]);
444 hud_draw_diamond(sx, sy, Lock_target_box_width[gr_screen.res], Lock_target_box_height[gr_screen.res]);
447 Lock_gauge.sx = sx - Lock_gauge_half_w[gr_screen.res];
448 Lock_gauge.sy = sy - Lock_gauge_half_h[gr_screen.res];
449 if(Player_ai->current_target_is_locked){
450 Lock_gauge.time_elapsed = 0.0f;
451 hud_anim_render(&Lock_gauge, 0.0f, 1);
453 hud_anim_render(&Lock_gauge, frametime, 1);
457 // Reset data used for player lock indicator
458 void hud_lock_reset(float lock_time_scale)
463 swp = &Player_ship->weapons;
464 wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
466 Player_ai->current_target_is_locked = 0;
467 Players[Player_num].lock_indicator_visible = 0;
468 Player->target_in_lock_cone = 0;
469 Player->lock_time_to_target = i2fl(wip->min_lock_time*lock_time_scale);
470 Player->current_target_sx = -1;
471 Player->current_target_sy = -1;
472 Player->locking_subsys=NULL;
473 Player->locking_on_center=0;
474 Player->locking_subsys_parent=-1;
475 hud_stop_looped_locking_sounds();
477 Lock_gauge_draw_stamp = -1;
480 // reset the lock anim time elapsed
481 Lock_anim.time_elapsed = 0.0f;
484 // Determine if the locking code has a point to track
485 int hud_lock_has_homing_point()
487 if ( Player_ai->targeted_subsys || Player->locking_subsys || Player->locking_on_center ) {
493 int Nebula_sec_range = 0;
494 DCF_BOOL(nebula_sec_range, Nebula_sec_range)
496 int hud_lock_world_pos_in_range(vector *target_world_pos, vector *vec_to_target)
498 float dist_to_target, weapon_range;
502 int target_in_range=1;
504 swp = &Player_ship->weapons;
505 wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
507 vm_vec_sub(vec_to_target, target_world_pos, &Player_obj->pos);
508 dist_to_target = vm_vec_mag(vec_to_target);
510 // calculate the range of the weapon, and only display the lead target indicator when
511 // if the weapon can actually hit the target
512 weapon_range = wip->max_speed * wip->lifetime;
514 // reduce firing range in nebula
515 if ((The_mission.flags & MISSION_FLAG_FULLNEB) && Nebula_sec_range) {
516 weapon_range *= 0.8f;
519 if (dist_to_target > weapon_range) {
523 return target_in_range;
526 // Determine if point to lock on is in range
527 int hud_lock_target_in_range()
529 vector target_world_pos, vec_to_target;
532 if ( !hud_lock_has_homing_point() ) {
536 targetp = &Objects[Player_ai->target_objnum];
538 if ( Player_ai->targeted_subsys != NULL ) {
539 vm_vec_unrotate(&target_world_pos, &Player_ai->targeted_subsys->system_info->pnt, &targetp->orient);
540 vm_vec_add2(&target_world_pos, &targetp->pos);
542 if ( Player->locking_subsys ) {
543 vm_vec_unrotate(&target_world_pos, &Player->locking_subsys->system_info->pnt, &targetp->orient);
544 vm_vec_add2(&target_world_pos, &targetp->pos);
546 Assert(Player->locking_on_center);
547 target_world_pos = targetp->pos;
551 return hud_lock_world_pos_in_range(&target_world_pos, &vec_to_target);
558 target_team = obj_team(&Objects[Player_ai->target_objnum]);
560 if ( Player_ship->weapons.num_secondary_banks <= 0 ) {
564 if ( Player_ship->weapons.current_secondary_bank < 0 ) {
568 // check to see if there are any missile to fire.. we don't want to show the
569 // lock indicator if there are no missiles to fire.
570 if ( !ship_secondary_bank_has_ammo(Player_obj->instance) ) {
574 // if the target is friendly, don't lock!
575 if ( hud_team_matches_filter(Player_ship->team, target_team)) {
576 // if we're in multiplayer dogfight, ignore this
577 if(!((Game_mode & GM_MULTIPLAYER) && (Netgame.type_flags & NG_TYPE_DOGFIGHT))){
585 // determine if the subsystem to lock on to has a direct line of sight
586 int hud_lock_on_subsys_ok()
593 Assert(Player_ai->target_objnum >= 0);
594 target_objp = &Objects[Player_ai->target_objnum];
596 subsys = Player_ai->targeted_subsys;
601 vm_vec_unrotate(&subobj_pos, &subsys->system_info->pnt, &target_objp->orient);
602 vm_vec_add2(&subobj_pos, &target_objp->pos);
604 if ( Player->subsys_in_view < 0 ) {
605 in_sight = ship_subsystem_in_sight(target_objp, subsys, &View_position, &subobj_pos);
607 in_sight = Player->subsys_in_view;
613 // Determine if locking point is in the locking cone
614 void hud_lock_check_if_target_in_lock_cone(vector *lock_world_pos)
617 vector vec_to_target;
619 dist = vm_vec_normalized_dir(&vec_to_target, lock_world_pos, &Player_obj->pos);
620 dot = vm_vec_dot(&Player_obj->orient.fvec, &vec_to_target);
623 Player->target_in_lock_cone = 1;
625 Player->target_in_lock_cone = 0;
630 // return 1 if current secondary weapon is different than previous secondary weapon
631 int hud_lock_secondary_weapon_changed(ship_weapon *swp)
634 if ( swp->current_secondary_bank != Player_ai->last_secondary_index ) {
640 int last_wi_index = -1;
641 int current_wi_index = -1;
644 // do a quick out if same bank is selected
645 if ( swp->current_secondary_bank == Player_ai->last_secondary_index ) {
649 // bank has changed, but it still may be the same weapon type
650 if ( swp->current_secondary_bank >= 0 ) {
651 current_wi_index = swp->secondary_bank_weapons[swp->current_secondary_bank];
654 if ( Player_ai->last_secondary_index >= 0 ) {
655 last_wi_index = swp->secondary_bank_weapons[Player_ai->last_secondary_index];
658 if ( current_wi_index != last_wi_index ) {
667 // hud_update_lock_indicator() will manage the non-rendering dependant part of
669 void hud_update_lock_indicator(float frametime)
673 vector lock_world_pos;
675 // if i'm a multiplayer observer, bail here
676 if((Game_mode & GM_MULTIPLAYER) && ((Net_player->flags & NETINFO_FLAG_OBSERVER) || (Player_obj->type == OBJ_OBSERVER)) ){
680 Assert(Player_ai->target_objnum != -1);
682 // be sure to unset this flag, then possibly set later in this function so that
683 // threat indicators work properly.
684 Player_ai->ai_flags &= ~AIF_SEEK_LOCK;
686 if ( hud_abort_lock() ) {
691 // if there is an EMP effect active, never update lock
692 if(emp_active_local()){
697 swp = &Player_ship->weapons;
698 wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
700 Lock_start_dist = wip->min_lock_time * wip->lock_pixels_per_sec;
702 // if secondary weapons change, reset the lock
703 if ( hud_lock_secondary_weapon_changed(swp) ) {
707 Player_ai->last_secondary_index = swp->current_secondary_bank;
709 if ( !(wip->wi_flags & WIF_HOMING_ASPECT) ) {
714 // Allow locking on ships and bombs (only targeted weapon allowed is a bomb, so don't bother checking flags)
715 if ( (Objects[Player_ai->target_objnum].type != OBJ_SHIP) && (Objects[Player_ai->target_objnum].type != OBJ_WEAPON) ) {
720 hud_lock_determine_lock_point(&lock_world_pos);
722 if ( !hud_lock_has_homing_point() ) {
723 Player->target_in_lock_cone=0;
726 hud_lock_check_if_target_in_lock_cone(&lock_world_pos);
728 // check if the target is within range of the current secondary weapon. If it is not,
729 // a lock will not be detected
730 if ( !hud_lock_target_in_range() ) {
731 Player->target_in_lock_cone = 0;
734 // If locking on a subsystem, and not in sight... can't lock
735 // Changed by MK on 4/3/98. It was confusing me that my hornets would not lock on my target.
736 // It will now be confusing that they lock, but don't home on your subsystem, but I think that's preferable.
737 // Often you really care about destroying the target, not just the subsystem.
738 /*if ( Player_ai->targeted_subsys ) {
739 if ( !hud_lock_on_subsys_ok() ) {
740 Player->target_in_lock_cone=0;
744 if ( !Player->target_in_lock_cone ) {
745 Player->locking_on_center=0;
746 Player->locking_subsys_parent=-1;
747 Player->locking_subsys=NULL;
750 hud_calculate_lock_position(frametime);
752 if (!Players[Player_num].lock_indicator_visible)
755 if (Player_ai->current_target_is_locked) {
756 if ( Missile_track_loop > -1 ) {
757 snd_chg_loop_status(Missile_track_loop, 0);
758 Missile_track_loop = -1;
759 Missile_lock_loop = snd_play(&Snds[SND_MISSILE_LOCK]);
763 Player_ai->ai_flags |= AIF_SEEK_LOCK; // set this flag so multiplayer's properly track lock on other ships
764 if ( Missile_lock_loop != -1 && snd_is_playing(Missile_lock_loop) ) {
765 snd_stop(Missile_lock_loop);
766 Missile_lock_loop = -1;
771 // hud_draw_lock_triangles() will draw the 4 rotating triangles around a lock indicator
772 // (This is done when a lock has been acquired)
773 #define ROTATE_DELAY 40
774 void hud_draw_lock_triangles_old(int center_x, int center_y, int radius)
776 static float ang = 0.0f;
778 float end_ang = ang + 2*PI;
779 float x3,y3,x4,y4,xpos,ypos;
781 if ( timestamp_elapsed(Rotate_time_id) ) {
782 Rotate_time_id = timestamp(ROTATE_DELAY);
786 for (ang; ang <= end_ang; ang += PI/2.0f) {
788 // draw the orbiting triangles
790 //ang = atan2(target_point.y,target_point.x);
791 xpos = center_x + (float)cos(ang)*(radius + Lock_triangle_height[gr_screen.res] + 2);
792 ypos = center_y - (float)sin(ang)*(radius + Lock_triangle_height[gr_screen.res] + 2);
794 x3 = xpos - Lock_triangle_base[gr_screen.res] * (float)sin(-ang);
795 y3 = ypos + Lock_triangle_base[gr_screen.res] * (float)cos(-ang);
796 x4 = xpos + Lock_triangle_base[gr_screen.res] * (float)sin(-ang);
797 y4 = ypos - Lock_triangle_base[gr_screen.res] * (float)cos(-ang);
799 xpos = xpos - Lock_triangle_base[gr_screen.res] * (float)cos(ang);
800 ypos = ypos + Lock_triangle_base[gr_screen.res] * (float)sin(ang);
802 hud_tri(x3, y3, xpos, ypos, x4, y4);
806 // draw a frame of the rotating lock triangles animation
807 void hud_draw_lock_triangles(int center_x, int center_y, float frametime)
809 if ( Lock_anim.first_frame == -1 ) {
810 hud_draw_lock_triangles_old(center_x, center_y, Lock_target_box_width[gr_screen.res]/2);
813 Lock_anim.sx = center_x - Lockspin_half_w[gr_screen.res];
814 Lock_anim.sy = center_y - Lockspin_half_h[gr_screen.res];
816 // if its still animating
817 if(Lock_anim.time_elapsed < Lock_anim.total_time){
818 hud_anim_render(&Lock_anim, frametime, 1, 0, 1);
820 // if the timestamp is unset or expired
821 if((Lock_gauge_draw_stamp < 0) || timestamp_elapsed(Lock_gauge_draw_stamp)){
823 Lock_gauge_draw_stamp = timestamp(1000 / (2 * LOCK_GAUGE_BLINK_RATE));
825 // switch between draw and dont-draw
826 Lock_gauge_draw = !Lock_gauge_draw;
829 // maybe draw the anim
830 Lock_gauge.time_elapsed = 0.0f;
832 hud_anim_render(&Lock_anim, frametime, 1, 0, 1);
838 // hud_calculate_lock_position() will determine where on the screen to draw the lock
839 // indicator, and will determine when a lock has occurred. If the lock indicator is not
840 // on the screen yet, hud_calculate_lock_start_pos() is called to pick a starting location
841 void hud_calculate_lock_position(float frametime)
846 static float pixels_moved_while_locking;
847 static float pixels_moved_while_degrading;
848 static int Need_new_start_pos = 0;
850 static double accumulated_x_pixels, accumulated_y_pixels;
853 static float last_dist_to_target;
855 static int catching_up;
857 static int maintain_lock_count = 0;
859 static float catch_up_distance = 0.0f;
861 double hypotenuse, delta_x, delta_y;
863 swp = &Player_ship->weapons;
864 wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
866 if (Player->target_in_lock_cone) {
867 if (!Players[Player_num].lock_indicator_visible) {
868 hud_calculate_lock_start_pos();
869 last_dist_to_target = 0.0f;
871 Players[Player_num].lock_indicator_x = Players[Player_num].lock_indicator_start_x;
872 Players[Player_num].lock_indicator_y = Players[Player_num].lock_indicator_start_y;
873 Players[Player_num].lock_indicator_visible = 1;
875 Players[Player_num].lock_time_to_target = i2fl(wip->min_lock_time);
879 Need_new_start_pos = 1;
881 if (Player_ai->current_target_is_locked) {
882 Players[Player_num].lock_indicator_x = Player->current_target_sx;
883 Players[Player_num].lock_indicator_y = Player->current_target_sy;
887 delta_x = Players[Player_num].lock_indicator_x - Player->current_target_sx;
888 delta_y = Players[Player_num].lock_indicator_y - Player->current_target_sy;
890 if (!delta_y && !delta_x) {
894 hypotenuse = _hypot(delta_y, delta_x);
897 Players[Player_num].lock_dist_to_target = (float)hypotenuse;
899 if (last_dist_to_target == 0) {
900 last_dist_to_target = Players[Player_num].lock_dist_to_target;
903 //nprintf(("Alan","dist to target: %.2f\n",Players[Player_num].lock_dist_to_target));
904 //nprintf(("Alan","last to target: %.2f\n\n",last_dist_to_target));
907 //nprintf(("Alan","IN CATCH UP MODE catch_up_dist is %.2f\n",catch_up_distance));
908 if ( Players[Player_num].lock_dist_to_target < catch_up_distance )
912 //nprintf(("Alan","IN NORMAL MODE\n"));
913 if ( (Players[Player_num].lock_dist_to_target - last_dist_to_target) > 2.0f ) {
915 catch_up_distance = last_dist_to_target + wip->catchup_pixel_penalty;
919 last_dist_to_target = Players[Player_num].lock_dist_to_target;
922 Players[Player_num].lock_time_to_target -= frametime;
923 if (Players[Player_num].lock_time_to_target < 0.0f)
924 Players[Player_num].lock_time_to_target = 0.0f;
927 float lock_pixels_per_sec;
928 if (Players[Player_num].lock_time_to_target > 0) {
929 lock_pixels_per_sec = Players[Player_num].lock_dist_to_target / Players[Player_num].lock_time_to_target;
931 lock_pixels_per_sec = i2fl(wip->lock_pixels_per_sec);
934 if (lock_pixels_per_sec > wip->lock_pixels_per_sec) {
935 lock_pixels_per_sec = i2fl(wip->lock_pixels_per_sec);
939 pixels_moved_while_locking = wip->catchup_pixels_per_sec * frametime;
941 pixels_moved_while_locking = lock_pixels_per_sec * frametime;
945 accumulated_x_pixels += pixels_moved_while_locking * delta_x/hypotenuse;
949 accumulated_y_pixels += pixels_moved_while_locking * delta_y/hypotenuse;
952 if (fl_abs(accumulated_x_pixels) > 1.0f) {
953 modf(accumulated_x_pixels, &int_portion);
955 Players[Player_num].lock_indicator_x -= (int)int_portion;
957 if ( fl_abs(Players[Player_num].lock_indicator_x - Player->current_target_sx) < fl_abs(int_portion) )
958 Players[Player_num].lock_indicator_x = Player->current_target_sx;
960 accumulated_x_pixels -= int_portion;
963 if (fl_abs(accumulated_y_pixels) > 1.0f) {
964 modf(accumulated_y_pixels, &int_portion);
966 Players[Player_num].lock_indicator_y -= (int)int_portion;
968 if ( fl_abs(Players[Player_num].lock_indicator_y - Player->current_target_sy) < fl_abs(int_portion) )
969 Players[Player_num].lock_indicator_y = Player->current_target_sy;
971 accumulated_y_pixels -= int_portion;
974 if ( Missile_track_loop == -1 ) {
975 Missile_track_loop = snd_play_looping( &Snds[SND_MISSILE_TRACKING], 0.0f , -1, -1);
978 if (!Players[Player_num].lock_time_to_target) {
979 if ( (Players[Player_num].lock_indicator_x == Player->current_target_sx) && (Players[Player_num].lock_indicator_y == Player->current_target_sy) ) {
980 if (maintain_lock_count++ > 1) {
981 Player_ai->current_target_is_locked = 1;
984 maintain_lock_count = 0;
990 if ( Missile_track_loop > -1 ) {
991 snd_chg_loop_status(Missile_track_loop, 0);
992 Missile_track_loop = -1;
995 Player_ai->current_target_is_locked = 0;
997 if (!Players[Player_num].lock_indicator_visible) {
1002 last_dist_to_target = 0.0f;
1004 if (Need_new_start_pos) {
1005 hud_calculate_lock_start_pos();
1006 Need_new_start_pos = 0;
1007 accumulated_x_pixels = 0.0f;
1008 accumulated_y_pixels = 0.0f;
1011 delta_x = Players[Player_num].lock_indicator_x - Players[Player_num].lock_indicator_start_x;
1012 delta_y = Players[Player_num].lock_indicator_y - Players[Player_num].lock_indicator_start_y;
1014 if (!delta_y && !delta_x) {
1018 hypotenuse = _hypot(delta_y, delta_x);
1021 Players[Player_num].lock_time_to_target += frametime;
1023 if (Players[Player_num].lock_time_to_target > wip->min_lock_time)
1024 Players[Player_num].lock_time_to_target = i2fl(wip->min_lock_time);
1026 pixels_moved_while_degrading = 2.0f * wip->lock_pixels_per_sec * frametime;
1029 accumulated_x_pixels += pixels_moved_while_degrading * delta_x/hypotenuse;
1032 accumulated_y_pixels += pixels_moved_while_degrading * delta_y/hypotenuse;
1034 if (fl_abs(accumulated_x_pixels) > 1.0f) {
1035 modf(accumulated_x_pixels, &int_portion);
1037 Players[Player_num].lock_indicator_x -= (int)int_portion;
1039 if ( fl_abs(Players[Player_num].lock_indicator_x - Players[Player_num].lock_indicator_start_x) < fl_abs(int_portion) )
1040 Players[Player_num].lock_indicator_x = Players[Player_num].lock_indicator_start_x;
1042 accumulated_x_pixels -= int_portion;
1045 if (fl_abs(accumulated_y_pixels) > 1.0f) {
1046 modf(accumulated_y_pixels, &int_portion);
1048 Players[Player_num].lock_indicator_y -= (int)int_portion;
1050 if ( fl_abs(Players[Player_num].lock_indicator_y - Players[Player_num].lock_indicator_start_y) < fl_abs(int_portion) )
1051 Players[Player_num].lock_indicator_y = Players[Player_num].lock_indicator_start_y;
1053 accumulated_y_pixels -= int_portion;
1056 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) ) {
1057 Players[Player_num].lock_indicator_visible = 0;
1062 // hud_calculate_lock_start_pos() will determine where to draw the starting location of the lock
1063 // indicator. It does this by picking a location that is Lock_start_dist pixels away from the current
1064 // target (in 2D). This is accomplished by finding the endpoint of a line that passes through the
1065 // origin, and connects the target and lock indicator postion (and has a magnitude of Lock_start_dist)
1066 void hud_calculate_lock_start_pos()
1071 double target_mag, target_x, target_y;
1073 delta_x = Player->current_target_sx - SCREEN_CENTER_X;
1074 delta_y = Player->current_target_sy - SCREEN_CENTER_Y;
1076 if (!delta_x && !delta_y) {
1077 Players[Player_num].lock_indicator_start_x = fl2i(SCREEN_CENTER_X + Lock_start_dist);
1078 Players[Player_num].lock_indicator_start_y = fl2i(SCREEN_CENTER_Y);
1082 hypotenuse = _hypot(delta_y, delta_x);
1084 if (hypotenuse >= Lock_start_dist) {
1085 Players[Player_num].lock_indicator_start_x = fl2i(SCREEN_CENTER_X);
1086 Players[Player_num].lock_indicator_start_y = fl2i(SCREEN_CENTER_Y);
1090 target_mag = Lock_start_dist - hypotenuse;
1091 target_x = target_mag * (delta_x / hypotenuse);
1092 target_y = target_mag * (delta_y / hypotenuse);
1094 Players[Player_num].lock_indicator_start_x = fl2i(SCREEN_CENTER_X - target_x);
1095 Players[Player_num].lock_indicator_start_y = fl2i(SCREEN_CENTER_Y - target_y);
1097 if (Players[Player_num].lock_indicator_start_x > gr_screen.clip_right)
1098 Players[Player_num].lock_indicator_start_x = gr_screen.clip_right;
1100 if (Players[Player_num].lock_indicator_start_y > gr_screen.clip_bottom)
1101 Players[Player_num].lock_indicator_start_y = gr_screen.clip_bottom;
1103 if (Players[Player_num].lock_indicator_start_x < gr_screen.clip_left)
1104 Players[Player_num].lock_indicator_start_x = gr_screen.clip_left;
1106 if (Players[Player_num].lock_indicator_start_y < gr_screen.clip_top)
1107 Players[Player_num].lock_indicator_start_y = gr_screen.clip_top;
1110 // hud_stop_looped_locking_sounds() will terminate any hud related looping sounds that are playing
1111 void hud_stop_looped_locking_sounds()
1113 if ( Missile_track_loop > -1 ) {
1114 snd_stop(Missile_track_loop);
1115 Missile_track_loop = -1;
1119 // Get a new world pos for the locking point
1120 void hud_lock_update_lock_pos(object *target_objp, vector *lock_world_pos)
1122 if ( Player_ai->targeted_subsys ) {
1123 get_subsystem_world_pos(target_objp, Player_ai->targeted_subsys, lock_world_pos);
1127 if ( Player->locking_on_center ) {
1128 *lock_world_pos = target_objp->pos;
1130 Assert(Player->locking_subsys);
1131 get_subsystem_world_pos(target_objp, Player->locking_subsys, lock_world_pos);
1135 // Try and find a new locking point
1136 void hud_lock_get_new_lock_pos(object *target_objp, vector *lock_world_pos)
1138 ship *target_shipp=NULL;
1139 int lock_in_range=0;
1140 float best_lock_dot=-1.0f, lock_dot=-1.0f;
1142 vector subsys_world_pos, vec_to_lock;
1144 if ( target_objp->type == OBJ_SHIP ) {
1145 target_shipp = &Ships[target_objp->instance];
1148 // if a large ship, lock to pos closest to center and within range
1149 if ( (target_shipp) && (Ship_info[target_shipp->ship_info_index].flags & (SIF_BIG_SHIP|SIF_HUGE_SHIP)) ) {
1150 // check all the subsystems and the center of the ship
1152 // assume best lock pos is the center of the ship
1153 *lock_world_pos=target_objp->pos;
1154 Player->locking_on_center=1;
1155 Player->locking_subsys=NULL;
1156 Player->locking_subsys_parent=-1;
1157 lock_in_range = hud_lock_world_pos_in_range(lock_world_pos, &vec_to_lock);
1158 vm_vec_normalize(&vec_to_lock);
1159 if ( lock_in_range ) {
1160 best_lock_dot=vm_vec_dot(&Player_obj->orient.fvec, &vec_to_lock);
1162 // take center if reasonable dot
1163 if ( best_lock_dot > 0.95 ) {
1167 // iterate through subsystems to see if we can get a better choice
1168 ss = GET_FIRST(&target_shipp->subsys_list);
1169 while ( ss != END_OF_LIST( &target_shipp->subsys_list ) ) {
1171 // get world pos of subsystem
1172 get_subsystem_world_pos(target_objp, ss, &subsys_world_pos);
1174 if ( hud_lock_world_pos_in_range(&subsys_world_pos, &vec_to_lock) ) {
1175 vm_vec_normalize(&vec_to_lock);
1176 lock_dot=vm_vec_dot(&Player_obj->orient.fvec, &vec_to_lock);
1177 if ( lock_dot > best_lock_dot ) {
1178 best_lock_dot=lock_dot;
1179 Player->locking_on_center=0;
1180 Player->locking_subsys=ss;
1181 Player->locking_subsys_parent=Player_ai->target_objnum;
1182 *lock_world_pos=subsys_world_pos;
1185 ss = GET_NEXT( ss );
1188 // if small ship (or weapon), just go for the center
1189 *lock_world_pos = target_objp->pos;
1190 Player->locking_on_center=1;
1191 Player->locking_subsys=NULL;
1192 Player->locking_subsys_parent=-1;
1196 // Decide which point lock should be homing on
1197 void hud_lock_determine_lock_point(vector *lock_world_pos_out)
1199 vector lock_world_pos;
1201 object *target_objp;
1203 Assert(Player_ai->target_objnum >= 0);
1204 target_objp = &Objects[Player_ai->target_objnum];
1206 Player->current_target_sx = -1;
1207 Player->current_target_sx = -1;
1209 // If subsystem is targeted, we must try to lock on that
1210 if ( Player_ai->targeted_subsys ) {
1211 hud_lock_update_lock_pos(target_objp, &lock_world_pos);
1212 Player->locking_on_center=0;
1213 Player->locking_subsys=NULL;
1214 Player->locking_subsys_parent=-1;
1216 // See if we already have a successful locked point
1217 if ( hud_lock_has_homing_point() ) {
1218 hud_lock_update_lock_pos(target_objp, &lock_world_pos);
1220 hud_lock_get_new_lock_pos(target_objp, &lock_world_pos);
1224 *lock_world_pos_out=lock_world_pos;
1226 g3_rotate_vertex(&lock_point,&lock_world_pos);
1227 g3_project_vertex(&lock_point);
1229 if (!(lock_point.flags & PF_OVERFLOW)) { // make sure point projected
1230 Player->current_target_sx = (int)lock_point.sx;
1231 Player->current_target_sy = (int)lock_point.sy;
1235 void hudlock_page_in()
1237 bm_page_in_aabitmap( Lock_gauge.first_frame, Lock_gauge.num_frames );