2 * $Logfile: /Freespace2/code/Hud/HUDlock.cpp $
7 * C module that controls missile locking
10 * Revision 1.2 2002/05/07 03:16:45 theoddone33
11 * The Great Newline Fix
13 * Revision 1.1.1.1 2002/05/03 03:28:09 root
17 * 19 8/09/99 10:50a Jasenw
19 * 18 7/30/99 5:42p Jasenw
20 * changed coords to center HUD lock indicator.
22 * 17 7/28/99 2:49p Andsager
23 * Make hud target speed use vm_vec_mag (not vm_vec_mag_quick)
25 * 16 6/08/99 7:41p Dave
26 * Animated hud lockup icon a bit differently.
28 * 15 6/08/99 8:35a Jasenw
29 * new coords for new lock ani
31 * 14 6/07/99 4:20p Andsager
32 * Add HUD color for tagged object. Apply to target and radar.
34 * 13 6/07/99 1:42p Jasenw
36 * 12 6/04/99 10:58a Dave
37 * Updated hud lock gauge.
39 * 11 6/03/99 2:32p Jasenw
40 * changed coords for new HUD stuff
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
46 * 9 4/23/99 12:01p Johnson
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
54 * 7 1/07/99 9:07a Jasen
57 * 6 12/30/98 7:47a Jasen
58 * added coordinates for hi res
60 * 5 12/28/98 3:17p Dave
61 * Support for multiple hud bitmap filenames for hi-res mode.
63 * 4 12/21/98 5:02p Dave
64 * Modified all hud elements to be multi-resolution friendly.
66 * 3 11/05/98 5:55p Dave
67 * Big pass at reducing #includes
69 * 2 10/07/98 10:53a Dave
72 * 1 10/07/98 10:49a Dave
74 * 55 8/25/98 1:48p Dave
75 * First rev of EMP effect. Player side stuff basically done. Next comes
78 * 54 5/11/98 3:48p Lawrance
79 * Break lock when banks switch
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.
86 * 52 5/04/98 10:50p Lawrance
87 * Don't break lock when switching to secondary weapons of the same type
89 * 51 4/20/98 12:36a Mike
90 * Make team vs. team work when player is hostile. Several targeting
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.
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
101 * 48 4/08/98 8:33p Lawrance
102 * Make player re-acquire lock when targeting subsystems on a locked ship.
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.
108 * 46 3/26/98 5:46p Lawrance
109 * call obj_team() instead of ship_team_from_obj()
111 * 45 3/26/98 5:26p John
112 * added new paging code. nonfunctional.
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
121 * 43 3/10/98 11:16a Lawrance
122 * Fix potential bug with missles not locking on a subsystem that appeared
125 * 42 2/28/98 7:03p Lawrance
126 * Change player missile locking to use dot product, so we can use it in
129 * 41 2/22/98 4:17p John
130 * More string externalization classification... 190 left to go!
132 * 40 1/25/98 10:31p Lawrance
133 * Don't draw most hud gauges when viewing from another ship.
135 * 39 1/24/98 4:48p Lawrance
136 * Add 'locking_subsys_parent', needed to save/restore locking_subsys
139 * 38 1/23/98 6:25p Lawrance
140 * Change player missile locking to lock on subsystem points automatically
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.
146 * 36 1/19/98 10:02p Lawrance
147 * Fix bug with locking on friendlies
149 * 35 12/30/97 4:28p Lawrance
150 * remove .ani extensions from filenames
152 * 34 12/10/97 9:57a Lawrance
153 * allow missile locks on all ships but friendly
155 * 33 11/17/97 6:37p Lawrance
156 * new gauges: extended target view, new lock triangles, support ship view
158 * 32 11/17/97 5:51p Lawrance
159 * fix bug that was sometimes causing lock to not occur
161 * 31 11/13/97 10:46p Lawrance
162 * ensure only attempt lock on HOSTILE
164 * 30 11/11/97 12:59a Lawrance
165 * fix couple of bugs with lock indicator not going away
167 * 29 11/08/97 11:07p Lawrance
168 * add in new lock indicator
170 * 28 10/28/97 9:02a Lawrance
171 * don't loop lock sound, some minor reformatting
173 * 27 10/08/97 5:07p Lawrance
174 * don't lock on friendly ships
176 * 26 9/07/97 10:02p Lawrance
177 * don't lock on a ship if player doesn't have any missiles
179 * 25 7/02/97 9:35a Hoffoss
180 * Changed all references to weapon variables in ships to 'weapons'
181 * structure variables in ships.
183 * 24 6/05/97 1:07a Lawrance
184 * changes to support sound interface
186 * 23 5/20/97 2:45p Mike
187 * Move current_target and a bunch of other stuff out of player struct.
189 * 22 4/24/97 10:55a Lawrance
190 * added Show_lock_cone to DCF
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
196 * 20 4/15/97 8:36a Lawrance
197 * fixed bug where lock indicator was lagging locked target
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
204 * 18 4/12/97 4:29p Lawrance
205 * get missle locking and offscreen indicator working properly with
206 * different sized screens
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()
212 * 16 4/09/97 4:34p Lawrance
213 * allow looped sounds to be cut off after they complete the full sample
216 * 15 3/25/97 8:17p Lawrance
217 * don't allow ASPECT homing missles to lock on debris
219 * 14 3/19/97 5:53p Lawrance
220 * integrating new Misc_sounds[] array (replaces old Game_sounds
223 * 13 3/10/97 8:53a Lawrance
224 * using hud_stop_looped_locking_sounds() in place of
225 * hud_stop_looped_sounds()
227 * 12 3/07/97 4:37p Mike
228 * Make rockeye missile home.
229 * Remove UNKNOWN and NEUTRAL teams.
231 * 11 3/04/97 2:27p Lawrance
232 * check to ensure player ship has missile banks
234 * 10 3/04/97 11:19a Lawrance
235 * supporting banked weapons
237 * 9 1/23/97 12:00p Lawrance
238 * made lock sound play at normal volume.
240 * 8 1/20/97 7:58p John
241 * Fixed some link errors with testcode.
243 * 7 1/13/97 5:36p Lawrance
244 * integrating new Game_sounds structure for general game sounds
246 * 6 1/06/97 10:43p Lawrance
247 * Changes to make save/restore functional
249 * 5 1/02/97 7:12p Lawrance
250 * adding hooks for more sounds
252 * 4 1/02/97 10:32a Lawrance
253 * fixed some bugs related to stopping looped sounds when targets die and
256 * 3 12/24/96 4:32p Lawrance
257 * some minor improvements
259 * 2 12/23/96 7:53p Lawrance
260 * missile locking working in new source files
267 #include "hudtarget.h"
268 #include "hudreticle.h"
274 #include "freespace.h"
275 #include "gamesequence.h"
280 #include "linklist.h"
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
288 int Missile_track_loop = -1;
289 int Missile_lock_loop = -1;
291 int Lock_target_box_width[GR_NUM_RESOLUTIONS] = {
295 int Lock_target_box_height[GR_NUM_RESOLUTIONS] = {
300 // the locked triangles (that orbit lock indicator) dimensions
301 float Lock_triangle_base[GR_NUM_RESOLUTIONS] = {
305 float Lock_triangle_height[GR_NUM_RESOLUTIONS] = {
310 int Lock_gauge_half_w[GR_NUM_RESOLUTIONS] = {
314 int Lock_gauge_half_h[GR_NUM_RESOLUTIONS] = {
319 // hud_frames Lock_gauge;
320 int Lock_gauge_loaded = 0;
322 int Lock_gauge_draw = 0;
323 int Lock_gauge_draw_stamp = -1;
324 #define LOCK_GAUGE_BLINK_RATE 5 // blinks/sec
326 int Lockspin_half_w[GR_NUM_RESOLUTIONS] = {
330 int Lockspin_half_h[GR_NUM_RESOLUTIONS] = {
336 char Lock_fname[GR_NUM_RESOLUTIONS][MAX_FILENAME_LEN] = {
341 char Lockspin_fname[GR_NUM_RESOLUTIONS][MAX_FILENAME_LEN] = {
346 void hud_lock_determine_lock_point(vector *lock_world_pos_out);
348 // hud_init_missile_lock() is called at the beginning of a mission
350 void hud_init_missile_lock()
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;
357 Player_ai->last_secondary_index = -1;
361 // Load in the frames need for the lead indicator
362 if (!Lock_gauge_loaded) {
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");
369 hud_anim_init(&Lock_gauge, 0, 0, Lock_fname[gr_screen.res]);
370 hud_anim_load(&Lock_gauge);
372 hud_anim_init(&Lock_anim, 0, 0, Lockspin_fname[gr_screen.res]);
373 hud_anim_load(&Lock_anim);
375 Lock_gauge_loaded = 1;
377 Lock_gauge_draw_stamp = -1;
382 void hud_draw_diamond(int x, int y, int width, int height)
387 int x1,x2,x3,x4,y1,y2,y3,y4;
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);
408 // hud_show_lock_indicator() will display the lock indicator for homing missiles
409 void hud_show_lock_indicator(float frametime)
411 int target_objnum, sx, sy;
414 if (!Players[Player_num].lock_indicator_visible){
418 target_objnum = Player_ai->target_objnum;
419 Assert(target_objnum != -1);
420 targetp = &Objects[target_objnum];
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) ) {
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));
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);
437 sx = Players[Player_num].lock_indicator_x;
438 sy = Players[Player_num].lock_indicator_y;
441 // show locked indicator
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]);
447 hud_draw_diamond(sx, sy, Lock_target_box_width[gr_screen.res], Lock_target_box_height[gr_screen.res]);
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);
456 hud_anim_render(&Lock_gauge, frametime, 1);
460 // Reset data used for player lock indicator
461 void hud_lock_reset(float lock_time_scale)
466 swp = &Player_ship->weapons;
467 wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
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();
480 Lock_gauge_draw_stamp = -1;
483 // reset the lock anim time elapsed
484 Lock_anim.time_elapsed = 0.0f;
487 // Determine if the locking code has a point to track
488 int hud_lock_has_homing_point()
490 if ( Player_ai->targeted_subsys || Player->locking_subsys || Player->locking_on_center ) {
496 int Nebula_sec_range = 0;
497 DCF_BOOL(nebula_sec_range, Nebula_sec_range)
499 int hud_lock_world_pos_in_range(vector *target_world_pos, vector *vec_to_target)
501 float dist_to_target, weapon_range;
505 int target_in_range=1;
507 swp = &Player_ship->weapons;
508 wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
510 vm_vec_sub(vec_to_target, target_world_pos, &Player_obj->pos);
511 dist_to_target = vm_vec_mag(vec_to_target);
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;
517 // reduce firing range in nebula
518 if ((The_mission.flags & MISSION_FLAG_FULLNEB) && Nebula_sec_range) {
519 weapon_range *= 0.8f;
522 if (dist_to_target > weapon_range) {
526 return target_in_range;
529 // Determine if point to lock on is in range
530 int hud_lock_target_in_range()
532 vector target_world_pos, vec_to_target;
535 if ( !hud_lock_has_homing_point() ) {
539 targetp = &Objects[Player_ai->target_objnum];
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);
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);
549 Assert(Player->locking_on_center);
550 target_world_pos = targetp->pos;
554 return hud_lock_world_pos_in_range(&target_world_pos, &vec_to_target);
561 target_team = obj_team(&Objects[Player_ai->target_objnum]);
563 if ( Player_ship->weapons.num_secondary_banks <= 0 ) {
567 if ( Player_ship->weapons.current_secondary_bank < 0 ) {
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) ) {
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))){
588 // determine if the subsystem to lock on to has a direct line of sight
589 int hud_lock_on_subsys_ok()
596 Assert(Player_ai->target_objnum >= 0);
597 target_objp = &Objects[Player_ai->target_objnum];
599 subsys = Player_ai->targeted_subsys;
604 vm_vec_unrotate(&subobj_pos, &subsys->system_info->pnt, &target_objp->orient);
605 vm_vec_add2(&subobj_pos, &target_objp->pos);
607 if ( Player->subsys_in_view < 0 ) {
608 in_sight = ship_subsystem_in_sight(target_objp, subsys, &View_position, &subobj_pos);
610 in_sight = Player->subsys_in_view;
616 // Determine if locking point is in the locking cone
617 void hud_lock_check_if_target_in_lock_cone(vector *lock_world_pos)
620 vector vec_to_target;
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);
626 Player->target_in_lock_cone = 1;
628 Player->target_in_lock_cone = 0;
633 // return 1 if current secondary weapon is different than previous secondary weapon
634 int hud_lock_secondary_weapon_changed(ship_weapon *swp)
637 if ( swp->current_secondary_bank != Player_ai->last_secondary_index ) {
643 int last_wi_index = -1;
644 int current_wi_index = -1;
647 // do a quick out if same bank is selected
648 if ( swp->current_secondary_bank == Player_ai->last_secondary_index ) {
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];
657 if ( Player_ai->last_secondary_index >= 0 ) {
658 last_wi_index = swp->secondary_bank_weapons[Player_ai->last_secondary_index];
661 if ( current_wi_index != last_wi_index ) {
670 // hud_update_lock_indicator() will manage the non-rendering dependant part of
672 void hud_update_lock_indicator(float frametime)
676 vector lock_world_pos;
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)) ){
683 Assert(Player_ai->target_objnum != -1);
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;
689 if ( hud_abort_lock() ) {
694 // if there is an EMP effect active, never update lock
695 if(emp_active_local()){
700 swp = &Player_ship->weapons;
701 wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
703 Lock_start_dist = wip->min_lock_time * wip->lock_pixels_per_sec;
705 // if secondary weapons change, reset the lock
706 if ( hud_lock_secondary_weapon_changed(swp) ) {
710 Player_ai->last_secondary_index = swp->current_secondary_bank;
712 if ( !(wip->wi_flags & WIF_HOMING_ASPECT) ) {
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) ) {
723 hud_lock_determine_lock_point(&lock_world_pos);
725 if ( !hud_lock_has_homing_point() ) {
726 Player->target_in_lock_cone=0;
729 hud_lock_check_if_target_in_lock_cone(&lock_world_pos);
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;
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;
747 if ( !Player->target_in_lock_cone ) {
748 Player->locking_on_center=0;
749 Player->locking_subsys_parent=-1;
750 Player->locking_subsys=NULL;
753 hud_calculate_lock_position(frametime);
755 if (!Players[Player_num].lock_indicator_visible)
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]);
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;
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)
779 static float ang = 0.0f;
781 float end_ang = ang + 2*PI;
782 float x3,y3,x4,y4,xpos,ypos;
784 if ( timestamp_elapsed(Rotate_time_id) ) {
785 Rotate_time_id = timestamp(ROTATE_DELAY);
789 for (ang; ang <= end_ang; ang += PI/2.0f) {
791 // draw the orbiting triangles
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);
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);
802 xpos = xpos - Lock_triangle_base[gr_screen.res] * (float)cos(ang);
803 ypos = ypos + Lock_triangle_base[gr_screen.res] * (float)sin(ang);
805 hud_tri(x3, y3, xpos, ypos, x4, y4);
809 // draw a frame of the rotating lock triangles animation
810 void hud_draw_lock_triangles(int center_x, int center_y, float frametime)
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);
816 Lock_anim.sx = center_x - Lockspin_half_w[gr_screen.res];
817 Lock_anim.sy = center_y - Lockspin_half_h[gr_screen.res];
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);
823 // if the timestamp is unset or expired
824 if((Lock_gauge_draw_stamp < 0) || timestamp_elapsed(Lock_gauge_draw_stamp)){
826 Lock_gauge_draw_stamp = timestamp(1000 / (2 * LOCK_GAUGE_BLINK_RATE));
828 // switch between draw and dont-draw
829 Lock_gauge_draw = !Lock_gauge_draw;
832 // maybe draw the anim
833 Lock_gauge.time_elapsed = 0.0f;
835 hud_anim_render(&Lock_anim, frametime, 1, 0, 1);
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)
849 static float pixels_moved_while_locking;
850 static float pixels_moved_while_degrading;
851 static int Need_new_start_pos = 0;
853 static double accumulated_x_pixels, accumulated_y_pixels;
856 static float last_dist_to_target;
858 static int catching_up;
860 static int maintain_lock_count = 0;
862 static float catch_up_distance = 0.0f;
864 double hypotenuse, delta_x, delta_y;
866 swp = &Player_ship->weapons;
867 wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
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;
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;
878 Players[Player_num].lock_time_to_target = i2fl(wip->min_lock_time);
882 Need_new_start_pos = 1;
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;
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;
893 if (!delta_y && !delta_x) {
897 hypotenuse = _hypot(delta_y, delta_x);
900 Players[Player_num].lock_dist_to_target = (float)hypotenuse;
902 if (last_dist_to_target == 0) {
903 last_dist_to_target = Players[Player_num].lock_dist_to_target;
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));
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 )
915 //nprintf(("Alan","IN NORMAL MODE\n"));
916 if ( (Players[Player_num].lock_dist_to_target - last_dist_to_target) > 2.0f ) {
918 catch_up_distance = last_dist_to_target + wip->catchup_pixel_penalty;
922 last_dist_to_target = Players[Player_num].lock_dist_to_target;
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;
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;
934 lock_pixels_per_sec = i2fl(wip->lock_pixels_per_sec);
937 if (lock_pixels_per_sec > wip->lock_pixels_per_sec) {
938 lock_pixels_per_sec = i2fl(wip->lock_pixels_per_sec);
942 pixels_moved_while_locking = wip->catchup_pixels_per_sec * frametime;
944 pixels_moved_while_locking = lock_pixels_per_sec * frametime;
948 accumulated_x_pixels += pixels_moved_while_locking * delta_x/hypotenuse;
952 accumulated_y_pixels += pixels_moved_while_locking * delta_y/hypotenuse;
955 if (fl_abs(accumulated_x_pixels) > 1.0f) {
956 modf(accumulated_x_pixels, &int_portion);
958 Players[Player_num].lock_indicator_x -= (int)int_portion;
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;
963 accumulated_x_pixels -= int_portion;
966 if (fl_abs(accumulated_y_pixels) > 1.0f) {
967 modf(accumulated_y_pixels, &int_portion);
969 Players[Player_num].lock_indicator_y -= (int)int_portion;
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;
974 accumulated_y_pixels -= int_portion;
977 if ( Missile_track_loop == -1 ) {
978 Missile_track_loop = snd_play_looping( &Snds[SND_MISSILE_TRACKING], 0.0f , -1, -1);
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;
987 maintain_lock_count = 0;
993 if ( Missile_track_loop > -1 ) {
994 snd_chg_loop_status(Missile_track_loop, 0);
995 Missile_track_loop = -1;
998 Player_ai->current_target_is_locked = 0;
1000 if (!Players[Player_num].lock_indicator_visible) {
1005 last_dist_to_target = 0.0f;
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;
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;
1017 if (!delta_y && !delta_x) {
1021 hypotenuse = _hypot(delta_y, delta_x);
1024 Players[Player_num].lock_time_to_target += frametime;
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);
1029 pixels_moved_while_degrading = 2.0f * wip->lock_pixels_per_sec * frametime;
1032 accumulated_x_pixels += pixels_moved_while_degrading * delta_x/hypotenuse;
1035 accumulated_y_pixels += pixels_moved_while_degrading * delta_y/hypotenuse;
1037 if (fl_abs(accumulated_x_pixels) > 1.0f) {
1038 modf(accumulated_x_pixels, &int_portion);
1040 Players[Player_num].lock_indicator_x -= (int)int_portion;
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;
1045 accumulated_x_pixels -= int_portion;
1048 if (fl_abs(accumulated_y_pixels) > 1.0f) {
1049 modf(accumulated_y_pixels, &int_portion);
1051 Players[Player_num].lock_indicator_y -= (int)int_portion;
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;
1056 accumulated_y_pixels -= int_portion;
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;
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()
1074 double target_mag, target_x, target_y;
1076 delta_x = Player->current_target_sx - SCREEN_CENTER_X;
1077 delta_y = Player->current_target_sy - SCREEN_CENTER_Y;
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);
1085 hypotenuse = _hypot(delta_y, delta_x);
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);
1093 target_mag = Lock_start_dist - hypotenuse;
1094 target_x = target_mag * (delta_x / hypotenuse);
1095 target_y = target_mag * (delta_y / hypotenuse);
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);
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;
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;
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;
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;
1113 // hud_stop_looped_locking_sounds() will terminate any hud related looping sounds that are playing
1114 void hud_stop_looped_locking_sounds()
1116 if ( Missile_track_loop > -1 ) {
1117 snd_stop(Missile_track_loop);
1118 Missile_track_loop = -1;
1122 // Get a new world pos for the locking point
1123 void hud_lock_update_lock_pos(object *target_objp, vector *lock_world_pos)
1125 if ( Player_ai->targeted_subsys ) {
1126 get_subsystem_world_pos(target_objp, Player_ai->targeted_subsys, lock_world_pos);
1130 if ( Player->locking_on_center ) {
1131 *lock_world_pos = target_objp->pos;
1133 Assert(Player->locking_subsys);
1134 get_subsystem_world_pos(target_objp, Player->locking_subsys, lock_world_pos);
1138 // Try and find a new locking point
1139 void hud_lock_get_new_lock_pos(object *target_objp, vector *lock_world_pos)
1141 ship *target_shipp=NULL;
1142 int lock_in_range=0;
1143 float best_lock_dot=-1.0f, lock_dot=-1.0f;
1145 vector subsys_world_pos, vec_to_lock;
1147 if ( target_objp->type == OBJ_SHIP ) {
1148 target_shipp = &Ships[target_objp->instance];
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
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);
1165 // take center if reasonable dot
1166 if ( best_lock_dot > 0.95 ) {
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 ) ) {
1174 // get world pos of subsystem
1175 get_subsystem_world_pos(target_objp, ss, &subsys_world_pos);
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;
1188 ss = GET_NEXT( ss );
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;
1199 // Decide which point lock should be homing on
1200 void hud_lock_determine_lock_point(vector *lock_world_pos_out)
1202 vector lock_world_pos;
1204 object *target_objp;
1206 Assert(Player_ai->target_objnum >= 0);
1207 target_objp = &Objects[Player_ai->target_objnum];
1209 Player->current_target_sx = -1;
1210 Player->current_target_sx = -1;
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;
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);
1223 hud_lock_get_new_lock_pos(target_objp, &lock_world_pos);
1227 *lock_world_pos_out=lock_world_pos;
1229 g3_rotate_vertex(&lock_point,&lock_world_pos);
1230 g3_project_vertex(&lock_point);
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;
1238 void hudlock_page_in()
1240 bm_page_in_aabitmap( Lock_gauge.first_frame, Lock_gauge.num_frames );