FS2_Open
Open source remastering of the Freespace 2 engine
hudtarget.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) Volition, Inc. 1999. All rights reserved.
3  *
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 the
6  * source.
7  *
8 */
9 
10 
11 
12 
13 #include "asteroid/asteroid.h"
14 #include "cmdline/cmdline.h"
15 #include "debris/debris.h"
16 #include "freespace2/freespace.h" // for flFrametime
17 #include "gamesnd/gamesnd.h"
18 #include "globalincs/alphacolors.h"
19 #include "globalincs/linklist.h"
20 #include "hud/hudartillery.h"
21 #include "hud/hudbrackets.h"
22 #include "hud/hudlock.h"
23 #include "hud/hudmessage.h"
24 #include "hud/hudparse.h"
25 #include "hud/hudreticle.h"
26 #include "hud/hudshield.h"
27 #include "hud/hudtarget.h"
28 #include "hud/hudtargetbox.h"
29 #include "iff_defs/iff_defs.h"
30 #include "io/timer.h"
31 #include "jumpnode/jumpnode.h"
32 #include "localization/localize.h"
33 #include "mission/missionhotkey.h"
34 #include "mission/missionmessage.h"
35 #include "model/model.h"
36 #include "network/multi.h"
37 #include "network/multiutil.h"
38 #include "object/object.h"
39 #include "parse/parselo.h"
40 #include "playerman/player.h"
41 #include "render/3dinternal.h"
42 #include "ship/awacs.h"
43 #include "ship/ship.h"
44 #include "ship/subsysdamage.h"
45 #include "weapon/emp.h"
46 #include "weapon/weapon.h"
47 
48 // If any of these bits in the ship->flags are set, ignore this ship when targeting
50 
51 // Global values for the target bracket width and height, used for debugging
53 
54 // offscreen triangle that point the the off-screen target
56  6.0f,
57  9.5f
58 };
60  7.0f,
61  11.0f
62 };
64  10.0f,
65  16.0f
66 };
68  10.0f,
69  16.0f
70 };
71 
73 
74 // The following variables are global to this file, and do not need to be persistent from frame-to-frame
75 // This means the variables are not player-specific
76 object* hostile_obj = NULL;
77 
78 static int ballistic_hud_index = 0; // Goober5000
79 
80 extern object obj_used_list; // dummy node in linked list of active objects
81 extern char *Cargo_names[];
82 
83 // shader is used to shade the target box
85 
86 // the target triangle (that orbits the reticle) dimensions
88  6.0f,
89  9.5f
90 };
92  7.0f,
93  11.0f
94 };
95 
96 // stuff for hotkey targeting lists
99 
100 /*
101 // coordinates and widths used to render the HUD afterburner energy gauge
102 int current_hud->Aburn_coords[GR_NUM_RESOLUTIONS][4] = {
103  { // GR_640
104  171, 265, 60, 60
105  },
106  { // GR_1024
107  274, 424, 86, 96
108  }
109 };
110 
111 // coordinates and widths used to render the HUD weapons energy gauge
112 int current_hud->Wenergy_coords[GR_NUM_RESOLUTIONS][4] = {
113  { // GR_640
114  416, 265, 60, 60
115  },
116  { // GR_1024
117  666, 424, 86, 96
118  }
119 };*/
120 
121 #define MIN_DISTANCE_TO_CONSIDER_THREAT 1500 // min distance to show hostile warning triangle
122 
124 // lists for target in reticle cycling
126 #define RL_USED (1<<0)
127 #define RL_USE_DOT (1<<1) // use dot product result, not distance
128 
129 typedef struct _reticle_list {
131  object *objp;
132  float dist, dot;
133  int flags;
134 } reticle_list;
135 
136 #define RESET_TARGET_IN_RETICLE 750
140 #define MAX_RETICLE_TARGETS 50
142 
144 // used for closest target cycling
146 #define TL_RESET 1500
147 #define TURRET_RESET 1000
148 static int Tl_hostile_reset_timestamp;
149 static int Tl_friendly_reset_timestamp;
150 static int Target_next_uninspected_object_timestamp;
151 static int Target_newest_ship_timestamp;
152 static int Target_next_turret_timestamp;
153 
154 // animation frames for the hud targeting gauges
155 // frames: 0 => out of range lead
156 // 1 => in range lead
158 {
159  {
160  { // GR_640
161  12.5f, // half-width
162  12.5f // half-height
163  },
164  { // GR_1024
165  20.0f, // half-width
166  20.0f // half-height
167  }
168  },
169  {
170  { // GR_640
171  8.0f, // half-width
172  8.0f // half-height
173  },
174  { // GR_1024
175  13.0f, // half-width
176  13.0f // half-height
177  }
178  }
179 };
183 {
184  { "lead1_fs1", "2_lead1_fs1" },
185  { "lead1", "2_lead1" }
186 };
187 
188 // animation frames for the countermeasures gauge
189 // frames: 0 => background
193  { // GR_640
194  497, 343
195  },
196  { // GR_1024
197  880, 602
198  }
199 };
201  { // GR_640
202  533, 347
203  },
204  { // GR_1024
205  916, 606
206  }
207 };
209  { // GR_640
210  506, 347
211  },
212  { // GR_1024
213  889, 606
214  }
215 };
217  "countermeasure1",
218  "countermeasure1"
219 };
220 
221 #define TOGGLE_TEXT_AUTOT 0
222 #define TOGGLE_TEXT_TARGET 1
223 #define TOGGLE_TEXT_AUTOS 2
224 #define TOGGLE_TEXT_SPEED 3
225 
227 
228 
229 // animation files for the weapons gauge
230 #define NUM_WEAPON_GAUGES 5
234 // for primaries
236 {
237  { // normal HUD
238  { // GR_640
239  // based on the # of primaries
240  {509, 273}, // top of weapon gauge, first frame, always
241  {497, 293}, // for the first primary
242  {497, 305} // for the second primary
243  },
244  { // GR_1024
245  // based on the # of primaries
246  {892, 525}, // top of weapon gauge, first frame, always
247  {880, 545}, // for the first primary
248  {880, 557} // for the second primary
249  }
250  },
251  { // ballistic HUD - slightly different alignment
252  { // GR_640
253  // based on the # of primaries
254  {485, 273}, // top of weapon gauge, first frame, always
255  {485, 293}, // for the first primary
256  {485, 305} // for the second primary
257  },
258  { // GR_1024
259  // based on the # of primaries
260  {868, 525}, // top of weapon gauge, first frame, always
261  {868, 545}, // for the first primary
262  {868, 557} // for the second primary
263  }
264  }
265 };
267 {
268  { // normal HUD
269  { // GR_640
270  // based on the # of secondaries
271  {497, 318}, // bottom of gauge, 0 secondaries
272  {497, 318}, // bottom of gauge, 1 secondaries
273  {497, 317}, // middle of gauge, 2 secondaries AND middle of gauge, 3 secondaries
274  {497, 326}, // bottom of gauge, 2 secondaries AND middle of gauge, 3 secondaries
275  {497, 335} // bottom of gauge, 3 secondaries
276  },
277  { // GR_1024
278  // based on the # of secondaries
279  {880, 570}, // bottom of gauge, 0 secondaries
280  {880, 570}, // bottom of gauge, 1 secondaries
281  {880, 569}, // middle of gauge, 2 secondaries AND middle of gauge, 3 secondaries
282  {880, 578}, // bottom of gauge, 2 secondaries AND middle of gauge, 3 secondaries
283  {880, 587} // bottom of gauge, 3 secondaries
284  }
285  },
286  { // ballistic HUD - slightly different alignment
287  { // GR_640
288  // based on the # of secondaries
289  {485, 318}, // bottom of gauge, 0 secondaries
290  {485, 318}, // bottom of gauge, 1 secondaries
291  {485, 317}, // middle of gauge, 2 secondaries AND middle of gauge, 3 secondaries
292  {485, 326}, // bottom of gauge, 2 secondaries AND middle of gauge, 3 secondaries
293  {485, 335} // bottom of gauge, 3 secondaries
294  },
295  { // GR_1024
296  // based on the # of secondaries
297  {868, 570}, // bottom of gauge, 0 secondaries
298  {868, 570}, // bottom of gauge, 1 secondaries
299  {868, 569}, // middle of gauge, 2 secondaries AND middle of gauge, 3 secondaries
300  {868, 578}, // bottom of gauge, 2 secondaries AND middle of gauge, 3 secondaries
301  {868, 587} // bottom of gauge, 3 secondaries
302  }
303  }
304 };
306 {
307  { // normal HUD
308  { // GR_640
309  518, 274
310  },
311  { // GR_1024
312  901, 527
313  }
314  },
315  { // ballistic HUD - slightly different alignment
316  { // GR_640
317  487, 274
318  },
319  { // GR_1024
320  870, 527
321  }
322  }
323 };
325  { // GR_640
326  {530, 285}, // fire-linked thingie, for the first primary
327  {530, 295} // fire-linked thingie, for the second primary
328  },
329  { // GR_1024
330  {913, 537}, // fire-linked thingie, for the first primary
331  {913, 547} // fire-linked thingie, for the second primary
332  }
333 };
335  { // GR_640
336  {536, 285}, // weapon name, first primary
337  {536, 295} // weapon name, second primary
338  },
339  { // GR_1024
340  {919, 537}, // weapon name, first primary
341  {919, 547} // weapon name, second primary
342  }
343 };
345  525, // where to draw the second thingie if this weapon is fire-linked
346  908
347 };
349  530, // where to draw the first thingie if this weapon is selected at all (fire-linked or not)
350  913
351 };
353  { // GR_640
354  309, // y location of where to draw text for the first secondary
355  318, // y location of where to draw text for the second secondary
356  327 // y location of where to draw text for the third secondary
357  },
358  { // GR_1024
359  561, // y location of where to draw text for the third secondary
360  570, // y location of where to draw text for the third secondary
361  579 // y location of where to draw text for the third secondary
362  }
363 };
365  { // GR_640
366  285, // y location of where to draw text for the first primary
367  295 // y location of where to draw text for the second primary
368  },
369  { // GR_1024
370  537, // y location of where to draw text for the first primary
371  547 // y location of where to draw text for the second primary
372  }
373 };
375  536, // x location of where to draw weapon name
376  919
377 };
379  525, // x location of where to draw weapon ammo count
380  908
381 };
383  525, // x location of where to draw primary weapon ammo count
384  908
385 };
387  615, // x location of where to draw the weapon reload time
388  998
389 };
391 {
392 //XSTR:OFF
393  { // normal HUD
394  { // GR_640
395  "weapons1",
396  "weapons2",
397  "weapons3",
398  "weapons4",
399  "weapons5"
400  },
401  { // GR_1024
402  "weapons1",
403  "weapons2",
404  "weapons3",
405  "weapons4",
406  "weapons5"
407  }
408  },
409  { // ballistic HUD - slightly different alignment
410  { // GR_640
411  "weapons1_b",
412  "weapons2_b",
413  "weapons3_b",
414  "weapons4_b",
415  "weapons5_b"
416  },
417  { // GR_1024
418  "weapons1_b",
419  "weapons2_b",
420  "weapons3_b",
421  "weapons4_b",
422  "weapons5_b"
423  }
424  }
425 //XSTR:ON
426 };
427 
428 // Flash the line for a weapon. This normally occurs when the player tries to fire that
429 // weapon, but the firing fails (due to lack of energy or damaged weapons subsystem).
430 #define MAX_WEAPON_FLASH_LINES 7 // 3 primary and 4 secondary
431 typedef struct weapon_flash
432 {
436 } weapon_flash;
438 
439 // Data used for the proximity warning
440 typedef struct homing_beep_info
441 {
442  int snd_handle; // sound handle for last played beep
443  fix last_time_played; // time beep was last played
444  int min_cycle_time; // time (in ms) for fastest cycling of the sound
445  int max_cycle_time; // time (in ms) for slowest cycling of the sound
446  float min_cycle_dist; // distance at which fastest cycling occurs
447  float max_cycle_dist; // distance at which slowest cycling occurs
448  float precalced_interp; // a precalculated value used in a linear interpretation
450 
451 homing_beep_info Homing_beep = { -1, 0, 150, 1000, 30.0f, 1500.0f, 1.729412f };
452 
453 // Set at the start of a mission, used to decide how to draw the separation for the warning missile indicators
456 
457 void hud_maybe_flash_weapon(int index);
458 
459 // if a given object should be ignored because of AWACS effects
461 {
462  // if objp is ship object, first check if can be targeted with team info
463  if (objp->type == OBJ_SHIP) {
464  if (Player_ship != NULL) {
466  return 0;
467  }
468  }
469  }
470 
471  // check for invalid status
472  if((Player_ship != NULL) && (awacs_get_level(objp, Player_ship) < 1.0f)){
473  return 1;
474  }
475 
476  // valid
477  return 0;
478 }
479 
481 {
482  if (next_flag) {
483  return GET_NEXT(cur);
484  } else {
485  return GET_LAST(cur);
486  }
487 }
488 
489 // select a sorted turret subsystem on a ship if no other subsys has been selected
491 {
493  if (!((Player_ai->target_objnum >= 0) && (Player_ai->target_objnum < MAX_OBJECTS))) {
494  return;
495  }
498  return;
499  }
500 
501  if (Ship_info[shipp->ship_info_index].flags & (SIF_BIG_SHIP|SIF_HUGE_SHIP)) {
502  if (shipp->last_targeted_subobject[Player_num] == NULL) {
504  }
505  }
506 
507 }
508 
509 // -----------------------------------------------------------------------
510 // clear out the linked list of targets in the reticle
512 {
513  reticle_list *cur;
514  for ( cur = GET_FIRST(rlist); cur != END_OF_LIST(rlist); cur = GET_NEXT(cur) ) {
515  cur->flags = 0;
516  }
517  list_init(rlist);
518 }
519 
520 // --------------------------------------------------------------------------------------
521 // hud_reticle_list_init()
523 {
524  int i;
525 
526  for ( i = 0; i < MAX_RETICLE_TARGETS; i++ ) {
527  Reticle_list[i].flags = 0;
528  }
529 
531  list_init(&Reticle_save_list);
532  list_init(&Reticle_cur_list);
533 }
534 
535 // --------------------------------------------------------------------------------------
536 // hud_check_reticle_list()
537 //
538 //
540 {
541  reticle_list *rl, *temp;
542 
543  // cull dying objects from reticle list
544  rl = GET_FIRST(&Reticle_cur_list);
545  while( rl !=END_OF_LIST(&Reticle_cur_list) ) {
546  temp = GET_NEXT(rl);
547  if ( rl->objp->flags & OF_SHOULD_BE_DEAD ) {
548  list_remove( &Reticle_cur_list, rl );
549  rl->flags = 0;
550  }
551  rl = temp;
552  }
553 
555  hud_reticle_clear_list(&Reticle_save_list);
557  }
558 }
559 
560 // --------------------------------------------------------------------------------------
561 // hud_reticle_list_find_free()
562 //
563 //
565 {
566  int i;
567 
568  // find a free reticle_list element
569  for ( i = 0; i < MAX_RETICLE_TARGETS; i++ ) {
570  if ( !(Reticle_list[i].flags & RL_USED) ) {
571  break;
572  }
573  }
574 
575  if ( i == MAX_RETICLE_TARGETS ) {
576 // nprintf(("Warning","Warning ==> Ran out of reticle target elements...\n"));
577  return -1;
578  }
579 
580  return i;
581 }
582 
583 // --------------------------------------------------------------------------------------
584 // hud_stuff_reticle_list()
585 //
586 //
587 #define RETICLE_DEFAULT_DIST 100000.0f
588 #define RETICLE_DEFAULT_DOT 1.0f
589 void hud_stuff_reticle_list(reticle_list *rl, object *objp, float measure, int dot_flag)
590 {
591  if ( dot_flag ) {
592  rl->dot = measure;
594  rl->flags |= RL_USE_DOT;
595  }
596  else {
597  rl->dist = measure;
598  rl->dot = RETICLE_DEFAULT_DOT;
599  }
600  rl->objp = objp;
601 }
602 
603 // --------------------------------------------------------------------------------------
604 // hud_reticle_list_update()
605 //
606 // Update Reticle_cur_list with an object that lies in the reticle
607 //
608 // parmeters: objp => object pointer to target
609 // measure => distance or dot product, depending on dot_flag
610 // dot_flag => if 0, measure is distance, if 1 measure is dot
611 //
612 void hud_reticle_list_update(object *objp, float measure, int dot_flag)
613 {
614  reticle_list *rl, *new_rl;
615  int i;
617 
618  if (objp->type == OBJ_JUMP_NODE) {
619  for (jnp = Jump_nodes.begin(); jnp != Jump_nodes.end(); ++jnp) {
620  if( jnp->GetSCPObject() != objp )
621  continue;
622 
623  if( jnp->IsHidden() )
624  return;
625  }
626  }
627 
628  for ( rl = GET_FIRST(&Reticle_cur_list); rl != END_OF_LIST(&Reticle_cur_list); rl = GET_NEXT(rl) ) {
629  if ( rl->objp == objp )
630  return;
631  }
632 
634  if ( i == -1 )
635  return;
636 
637  new_rl = &Reticle_list[i];
638  new_rl->flags |= RL_USED;
639  hud_stuff_reticle_list(new_rl, objp, measure, dot_flag);
640 
641  int was_inserted = 0;
642 
643  if ( EMPTY(&Reticle_cur_list) ) {
644  list_insert(&Reticle_cur_list, new_rl);
645  was_inserted = 1;
646  }
647  else {
648  for ( rl = GET_FIRST(&Reticle_cur_list); rl != END_OF_LIST(&Reticle_cur_list); rl = GET_NEXT(rl) ) {
649  if ( !dot_flag ) {
650  // compare based on distance
651  if ( measure < rl->dist ) {
652  list_insert_before(rl, new_rl);
653  was_inserted = 1;
654  break;
655  }
656  }
657  else {
658  // compare based on dot
659  if ( measure > rl->dot ) {
660  list_insert_before(rl, new_rl);
661  was_inserted = 1;
662  break;
663  }
664  }
665  } // end for
666  }
667 
668  if ( !was_inserted ) {
669  list_append(&Reticle_cur_list, new_rl);
670  }
671 }
672 
673 // --------------------------------------------------------------------------------------
674 // hud_reticle_pick_target()
675 //
676 // Pick a target from Reticle_cur_list, based on what is in Reticle_save_list
677 //
678 //
680 {
681  reticle_list *cur_rl, *save_rl, *new_rl;
682  object *return_objp;
683  int in_save_list, i;
684 
685  return_objp = NULL;
686 
687  // As a first step, see if both ships and debris are in the list. If so, cull the debris.
688  int debris_in_list = 0;
689  int ship_in_list = 0;
690  for ( cur_rl = GET_FIRST(&Reticle_cur_list); cur_rl != END_OF_LIST(&Reticle_cur_list); cur_rl = GET_NEXT(cur_rl) ) {
691  if ( (cur_rl->objp->type == OBJ_SHIP) || (cur_rl->objp->type == OBJ_JUMP_NODE) ) {
692  ship_in_list = 1;
693  continue;
694  }
695 
696  if ( cur_rl->objp->type == OBJ_WEAPON ) {
698  ship_in_list = 1;
699  continue;
700  }
701  }
702 
703  if ( (cur_rl->objp->type == OBJ_DEBRIS) || (cur_rl->objp->type == OBJ_ASTEROID) ) {
704  debris_in_list = 1;
705  continue;
706  }
707  }
708 
709  if ( ship_in_list && debris_in_list ) {
710  // cull debris
711  reticle_list *rl, *next;
712 
713  rl = GET_FIRST(&Reticle_cur_list);
714  while ( rl != &Reticle_cur_list ) {
715  next = rl->next;
716  if ( (rl->objp->type == OBJ_DEBRIS) || (rl->objp->type == OBJ_ASTEROID) ){
717  list_remove(&Reticle_cur_list,rl);
718  rl->flags = 0;
719  }
720  rl = next;
721  }
722  }
723 
724  for ( cur_rl = GET_FIRST(&Reticle_cur_list); cur_rl != END_OF_LIST(&Reticle_cur_list); cur_rl = GET_NEXT(cur_rl) ) {
725  in_save_list = 0;
726  for ( save_rl = GET_FIRST(&Reticle_save_list); save_rl != END_OF_LIST(&Reticle_save_list); save_rl = GET_NEXT(save_rl) ) {
727  if ( cur_rl->objp == save_rl->objp ) {
728  in_save_list = 1;
729  break;
730  }
731  }
732 
733  if ( !in_save_list ) {
734  return_objp = cur_rl->objp;
736  if ( i == -1 )
737  break;
738 
739  new_rl = &Reticle_list[i];
740  new_rl->flags |= RL_USED;
741  if ( cur_rl->flags & RL_USE_DOT ) {
742  hud_stuff_reticle_list(new_rl, cur_rl->objp, cur_rl->dot, 1);
743  }
744  else {
745  hud_stuff_reticle_list(new_rl, cur_rl->objp, cur_rl->dist, 0);
746  }
747 
748  list_append(&Reticle_save_list, new_rl);
749  break;
750  }
751  } // end for
752 
753  if ( return_objp == NULL && !EMPTY(&Reticle_cur_list) ) {
755  if ( i == -1 )
756  return NULL;
757  new_rl = &Reticle_list[i];
758  cur_rl = GET_FIRST(&Reticle_cur_list);
759  *new_rl = *cur_rl;
760  return_objp = cur_rl->objp;
761  hud_reticle_clear_list(&Reticle_save_list);
762  list_append(&Reticle_save_list, new_rl);
763  }
764 
765  return return_objp;
766 }
767 
768 // hud_target_hotkey_add_remove takes as its parameter which hotkey (1-0) to add/remove the current
769 // target from. This function behaves like the Shift-<selection> does in Windows -- using shift # will toggle
770 // the current target in and out of the selection set.
771 void hud_target_hotkey_add_remove( int k, object *ctarget, int how_to_add )
772 {
773  htarget_list *hitem, *plist;
774 
775  // don't do anything if a standalone multiplayer server
777  return;
778 
779  if ( (k < 0) || (k >= MAX_KEYED_TARGETS) ) {
780  nprintf(("Warning", "Bogus hotkey %d sent to hud_target_hotkey_add_remove\n", k));
781  return;
782  }
783 
784  plist = &(Players[Player_num].keyed_targets[k]);
785 
786  // we must operate only on ships
787  if ( ctarget->type != OBJ_SHIP )
788  return;
789 
790  // don't allow player into hotkey set
791  if ( ctarget == Player_obj )
792  return;
793 
794  // don't put dying or departing
795  if ( Ships[ctarget->instance].flags & (SF_DYING|SF_DEPARTING) )
796  return;
797 
798  // don't add mission file added hotkey assignments if there are player added assignments
799  // already in the list
800  if ( (how_to_add == HOTKEY_MISSION_FILE_ADDED) && NOT_EMPTY(plist) ) {
801  for ( hitem = GET_FIRST(plist); hitem != END_OF_LIST(plist); hitem = GET_NEXT(hitem) ) {
802  if ( hitem->how_added == HOTKEY_USER_ADDED )
803  return;
804  }
805  }
806 
807  // determine if the current target is currently in the set or not
808  for ( hitem = GET_FIRST(plist); hitem != END_OF_LIST(plist); hitem = GET_NEXT(hitem) ) {
809  if ( hitem->objp == ctarget )
810  break;
811  }
812 
813  // if hitem == end of the list, then the target should be added, else it should be removed
814  if ( hitem == END_OF_LIST(plist) ) {
815  if ( EMPTY(&htarget_free_list) ) {
816  Int3(); // get Allender -- no more free hotkey target items
817  return;
818  }
819 
820  nprintf(("network", "Hotkey: Adding %s\n", Ships[ctarget->instance].ship_name));
821  hitem = GET_FIRST( &htarget_free_list );
822  list_remove( &htarget_free_list, hitem );
823  list_append( plist, hitem );
824  hitem->objp = ctarget;
825  hitem->how_added = how_to_add;
826  } else {
827  nprintf(("network", "Hotkey: Removing %s\n", Ships[ctarget->instance].ship_name));
828  list_remove( plist, hitem );
829  list_append( &htarget_free_list, hitem );
830  hitem->objp = NULL; // for safety
831  }
832 }
833 
834 // the following function clears the hotkey set given by parameter passed in
836 {
837  htarget_list *hitem, *plist, *temp;
838 
839  plist = &(Players[Player_num].keyed_targets[k]);
840  hitem = GET_FIRST(plist);
841  while ( hitem != END_OF_LIST(plist) ) {
842  temp = GET_NEXT(hitem);
843  list_remove( plist, hitem );
844  list_append( &htarget_free_list, hitem );
845  hitem->objp = NULL;
846  hitem = temp;
847  }
848  if ( Players[Player_num].current_hotkey_set == k ) // clear this variable if we removed the bindings
850 }
851 
852 // the next function sets the current selected set to be N. If there is just one ship in the selection
853 // set, this ship will become the new target. If there is more than one ship in the selection set,
854 // then the current_target will remain what it was.
856 {
857  int visible_count = 0;
858  htarget_list *hitem, *plist, *target, *next_target, *first_target;
859  int target_objnum;
860 
861  plist = &(Players[Player_num].keyed_targets[k]);
862 
863  if ( EMPTY( plist ) ) // no items in list, then do nothing
864  return;
865 
866  // a simple walk of the list to get the count
867  for ( hitem = GET_FIRST(plist); hitem != END_OF_LIST(plist); hitem = GET_NEXT(hitem) ){
868  if (awacs_get_level(hitem->objp, Player_ship, 1) > 1) {
869  visible_count++;
870  }
871  }
872 
873  // no visible ships in list
874  if (visible_count == 0) {
875  return;
876  }
877 
878  // set the current target to be the "next" ship in the list. Scan the list to see if our
879  // current target is in the set. If so, target the next ship in the list, otherwise target
880  // the first
881  // set first_target - first visible item in list
882  // target - item in list that is the player's currently selected target
883  // next_target - next visible item in list following target
884  target_objnum = Player_ai->target_objnum;
885  target = NULL;
886  next_target = NULL;
887  first_target = NULL;
888  for ( hitem = GET_FIRST(plist); hitem != END_OF_LIST(plist); hitem = GET_NEXT(hitem) ) {
889 
890  if (awacs_get_level(hitem->objp, Player_ship, 1) > 1) {
891  // get the first valid target
892  if (first_target == NULL) {
893  first_target = hitem;
894  }
895 
896  // get the next target in the list following the player currently selected target
897  if (target != NULL) {
898  next_target = hitem;
899  break;
900  }
901  }
902 
903  // mark the player currently selected target
904  if ( OBJ_INDEX(hitem->objp) == target_objnum ) {
905  target = hitem;
906  }
907  }
908 
909  // if current target is not in list, then target and next_target will be NULL
910  // so we use the first found target
911  if (target == NULL) {
912  Assert(first_target != NULL);
913  if (first_target != NULL) {
914  target = first_target;
915  next_target = first_target;
916  } else {
917  // this should not happen
918  return;
919  }
920  // if current target is in the list but this is not our current selection set,
921  // then we don't want to change target.
922  } else if (Players[Player_num].current_hotkey_set != k) {
923  next_target = target;
924  }
925 
926  // update target if more than 1 is visible
927  if (visible_count > 1) {
928  // next already found (after current target in list)
929  if (next_target != NULL) {
930  target = next_target;
931  } else {
932 
933  // next is before current target, so search from start of list
934  for ( hitem = GET_FIRST(plist); hitem != END_OF_LIST(plist); hitem = GET_NEXT(hitem) ) {
935  if (awacs_get_level(hitem->objp, Player_ship, 1) > 1) {
936  target = hitem;
937  break;
938  }
939  }
940  }
941  }
942 
943  Assert( target != END_OF_LIST(plist) );
944 
945  if ( Player_obj != target->objp ){
947  hud_shield_hit_reset(target->objp);
948  }
949 
951 }
952 
953 // hud_init_targeting_colors() will initialize the shader and gradient objects used
954 // on the HUD
955 //
956 
958 
959 void hud_make_shader(shader *sh, ubyte r, ubyte g, ubyte b, float dimmer = 1000.0f)
960 {
961  // The m matrix converts all colors to shades of green
962  //float tmp = 16.0f*(0.0015625f * i2fl(HUD_color_alpha+1.0f));
963  float tmp = 0.025f * i2fl(HUD_color_alpha+1.0f);
964 
965  ubyte R = ubyte(r * tmp);
966  ubyte G = ubyte(r * tmp);
967  ubyte B = ubyte(r * tmp);
968  ubyte A = ubyte((float(r) / dimmer)*(i2fl(HUD_color_alpha) / 15.0f) * 255.0f);
969 
970  gr_create_shader( sh, R, G, B, A );
971 }
972 
974 {
975  gr_init_color( &HUD_color_homing_indicator, 0x7f, 0x7f, 0 ); // yellow
976 
977  hud_make_shader(&Training_msg_glass, 61, 61, 85, 500.0f);
978 
980 }
981 
983 {
984  int i;
985 
986  // clear out the keyed target list
987  for (i = 0; i < MAX_KEYED_TARGETS; i++ )
988  list_init( &(Players[Player_num].keyed_targets[i]) );
989 
991 
992  // place all of the hoykey target items back onto the free list
993  list_init( &htarget_free_list );
994  for ( i = 0; i < MAX_HOTKEY_TARGET_ITEMS; i++ )
995  list_append( &htarget_free_list, &htarget_items[i] );
996 }
997 
998 // Init data used for the weapons display on the HUD
1000 {
1001  Weapon_flash_info.is_bright = 0;
1002  for ( int i = 0; i < MAX_WEAPON_FLASH_LINES; i++ ) {
1003  Weapon_flash_info.flash_duration[i] = 1;
1004  Weapon_flash_info.flash_next[i] = 1;
1005  }
1006 
1007  // The E: There used to be a number of checks here. They are no longer needed, as the new HUD code handles it on its own.
1009 }
1010 
1011 // init data used to play the homing "proximity warning" sound
1013 {
1014  Homing_beep.snd_handle = -1;
1015  Homing_beep.last_time_played = 0;
1016  Homing_beep.precalced_interp = (Homing_beep.max_cycle_dist-Homing_beep.min_cycle_dist) / (Homing_beep.max_cycle_time - Homing_beep.min_cycle_time );
1017 }
1018 
1020 {
1021  int i;
1022 
1023  // decide whether to realign HUD for ballistic primaries
1024  ballistic_hud_index = 0;
1025  for (i = 0; i < Player_ship->weapons.num_primary_banks; i++)
1026  {
1028  {
1029  ballistic_hud_index = 1;
1030  break;
1031  }
1032  }
1033 }
1034 
1035 // hud_init_targeting() will set the current target to point to the dummy node
1036 // in the object used list
1037 //
1039 {
1040  Assert(Player_ai != NULL);
1041 
1042  // make sure there is no current target
1044  Player_ai->last_target = -1;
1045  Player_ai->last_subsys_target = NULL;
1046  Player_ai->last_dist = 0.0f;
1047  Player_ai->last_speed = 0.0f;
1048 
1052 
1053  // Init the lists that hold targets in reticle (to allow cycling of targets in reticle)
1056 
1057  hud_weapons_init();
1058 
1060  Max_warning_missile_dist = 1500.0f;
1061 
1062  Tl_hostile_reset_timestamp = timestamp(0);
1063  Tl_friendly_reset_timestamp = timestamp(0);
1064  Target_next_uninspected_object_timestamp = timestamp(0);
1065  Target_newest_ship_timestamp = timestamp(0);
1066  Target_next_turret_timestamp = timestamp(0);
1067 
1070  } else {
1072  }
1073 }
1074 
1075 // Target the next or previous subobject on the currently selected ship, based on next_flag.
1076 void hud_target_subobject_common(int next_flag)
1077 {
1078  if (Player_ai->target_objnum == -1) {
1079  HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "No target selected.", 322));
1081  return;
1082  }
1083 
1086  return;
1087  }
1088 
1089  ship_subsys *start, *start2, *A;
1090  ship_subsys *subsys_to_target=NULL;
1091  ship *target_shipp;
1092 
1093  target_shipp = &Ships[Objects[Player_ai->target_objnum].instance];
1094 
1095  if (!Player_ai->targeted_subsys) {
1096  start = END_OF_LIST(&target_shipp->subsys_list);
1097  } else {
1098  start = Player_ai->targeted_subsys;
1099  }
1100 
1101  start2 = advance_subsys(start, next_flag);
1102 
1103  for ( A = start2; A != start; A = advance_subsys(A, next_flag) ) {
1104 
1105  if ( A == &target_shipp->subsys_list ) {
1106  continue;
1107  }
1108 
1109  // ignore turrets
1110  if ( A->system_info->type == SUBSYSTEM_TURRET ) {
1111  continue;
1112  }
1113 
1114  if ( A->flags & SSF_UNTARGETABLE ) {
1115  continue;
1116  }
1117 
1118  subsys_to_target = A;
1119  break;
1120 
1121  } // end for
1122 
1123  if ( subsys_to_target == NULL ) {
1125  } else {
1128  }
1129 }
1130 
1131 object *advance_fb(object *objp, int next_flag)
1132 {
1133  if (next_flag)
1134  return GET_NEXT(objp);
1135  else
1136  return GET_LAST(objp);
1137 }
1138 
1139 // Target the previous subobject on the currently selected ship.
1140 //
1141 
1143 {
1145 }
1146 
1148 {
1150 }
1151 
1152 // hud_target_next() will set the Players[Player_num].current_target to the next target in the object
1153 // used list whose team matches the team parameter. The player is NOT included in the target list.
1154 //
1155 // parameters: team_mask => team(s) of ship to target next
1156 //
1157 
1158 void hud_target_common(int team_mask, int next_flag)
1159 {
1160  object *A, *start, *start2;
1161  ship *shipp;
1162  int is_ship, target_found = FALSE;
1164 
1165  if (Player_ai->target_objnum == -1)
1166  start = &obj_used_list;
1167  else
1168  start = &Objects[Player_ai->target_objnum];
1169 
1170  start2 = advance_fb(start, next_flag);
1171 
1172  for ( A = start2; A != start; A = advance_fb(A, next_flag) ) {
1173  is_ship = 0;
1174 
1175  if (A == &obj_used_list)
1176  continue;
1177 
1178  if ( (A == Player_obj) || ((A->type != OBJ_SHIP) && (A->type != OBJ_WEAPON) && (A->type != OBJ_JUMP_NODE)) )
1179  continue;
1180 
1181  if ( hud_target_invalid_awacs(A) )
1182  continue;
1183 
1184  if (A->type == OBJ_WEAPON) {
1187  continue;
1188 
1189  if (Weapons[A->instance].lssm_stage == 3)
1190  continue;
1191  }
1192 
1193  if (A->type == OBJ_SHIP) {
1195  continue;
1196 
1197  is_ship = 1;
1198  }
1199 
1200  if (A->type == OBJ_JUMP_NODE) {
1201  for (jnp = Jump_nodes.begin(); jnp != Jump_nodes.end(); ++jnp) {
1202  if( jnp->GetSCPObject() == A )
1203  break;
1204  }
1205 
1206  if( jnp->IsHidden() )
1207  continue;
1208  }
1209 
1210  if ( vm_vec_same(&A->pos, &Eye_position) )
1211  continue;
1212 
1213  if ( is_ship ) {
1214  shipp = &Ships[A->instance]; // get a pointer to the ship information
1215 
1216  if (!iff_matches_mask(shipp->team, team_mask)) {
1217  continue;
1218  }
1219 
1220  if ( A == Player_obj || (shipp->flags & TARGET_SHIP_IGNORE_FLAGS) ){
1221  continue;
1222  }
1223 
1224  // if we've reached here, it is a valid next target
1225  if ( Player_ai->target_objnum != A-Objects ) {
1226  target_found = TRUE;
1229  // if ship is BIG|HUGE and last subsys is NULL, get turret
1232  }
1233  } else {
1234  target_found = TRUE;
1237  }
1238 
1239  break;
1240  }
1241 
1242  if ( target_found == FALSE ) {
1244  }
1245 }
1246 
1247 void hud_target_next(int team_mask)
1248 {
1249  hud_target_common(team_mask, 1);
1250 }
1251 
1252 void hud_target_prev(int team_mask)
1253 {
1254  hud_target_common(team_mask, 0);
1255 }
1256 
1257 // -------------------------------------------------------------------
1258 // advance_missile_obj()
1259 //
1261 {
1262  if (next_flag){
1263  return GET_NEXT(mo);
1264  }
1265 
1266  return GET_LAST(mo);
1267 }
1268 
1269 ship_obj *advance_ship(ship_obj *so, int next_flag)
1270 {
1271  if (next_flag){
1272  return GET_NEXT(so);
1273  }
1274 
1275  return GET_LAST(so);
1276 }
1277 
1296 static object* select_next_target_by_distance( const bool targeting_from_closest_to_farthest, const int valid_team_mask, const int attacked_object_number = -1, const int target_filters = (SIF_CARGO | SIF_NAVBUOY)) {
1297  object *minimum_object_ptr, *maximum_object_ptr, *nearest_object_ptr;
1298  minimum_object_ptr = maximum_object_ptr = nearest_object_ptr = NULL;
1299  float current_distance = hud_find_target_distance(&Objects[Player_ai->target_objnum], Player_obj);
1300  float minimum_distance = 1e20f;
1301  float maximum_distance = 0.0f;
1302  int player_object_index = OBJ_INDEX(Player_obj);
1303 
1304  float nearest_distance;
1305  if ( targeting_from_closest_to_farthest ) {
1306  nearest_distance = 1e20f;
1307  } else {
1308  nearest_distance = 0.0f;
1309  }
1310 
1311  ship_obj *ship_object_ptr;
1312  object *prospective_victim_ptr;
1313  ship *prospective_victim_ship_ptr;
1314  for ( ship_object_ptr = GET_FIRST(&Ship_obj_list); ship_object_ptr != END_OF_LIST(&Ship_obj_list); ship_object_ptr = GET_NEXT( ship_object_ptr) ) {
1315  prospective_victim_ptr = &Objects[ ship_object_ptr->objnum];
1316  // get a pointer to the ship information
1317  prospective_victim_ship_ptr = &Ships[prospective_victim_ptr->instance];
1318 
1319  float new_distance;
1320  if ( (prospective_victim_ptr == Player_obj) || (prospective_victim_ship_ptr->flags & TARGET_SHIP_IGNORE_FLAGS) ) {
1321  continue;
1322  }
1323 
1324  // choose from the correct team
1325  if ( !iff_matches_mask(prospective_victim_ship_ptr->team, valid_team_mask) ) {
1326  continue;
1327  }
1328 
1329  // don't use object if it is already a target
1330  if ( OBJ_INDEX(prospective_victim_ptr) == Player_ai->target_objnum ) {
1331  continue;
1332  }
1333 
1334 
1335  if( attacked_object_number == -1 ) {
1336  // always ignore navbuoys and cargo
1337  if ( Ship_info[prospective_victim_ship_ptr->ship_info_index].flags & target_filters ) {
1338  continue;
1339  }
1340 
1341  if(hud_target_invalid_awacs(prospective_victim_ptr)){
1342  continue;
1343  }
1344 
1345  new_distance = hud_find_target_distance(prospective_victim_ptr,Player_obj);
1346  } else {
1347  // Filter out any target that is not targeting the player --Mastadon
1348  if ( (attacked_object_number == player_object_index) && (Ai_info[prospective_victim_ship_ptr->ai_index].target_objnum != player_object_index) ) {
1349  continue;
1350  }
1351  esct eval_ship_as_closest_target_args;
1352  eval_ship_as_closest_target_args.attacked_objnum = attacked_object_number;
1353  eval_ship_as_closest_target_args.check_all_turrets = (attacked_object_number == player_object_index);
1354  eval_ship_as_closest_target_args.check_nearest_turret = FALSE;
1355  // We don't ever filter our target selection to just bombers or fighters
1356  // because the select next attacker logic doesn't. --Mastadon
1357  eval_ship_as_closest_target_args.filter = 0;
1358  eval_ship_as_closest_target_args.team_mask = valid_team_mask;
1359  // We always get the turret attacking, since that's how the select next
1360  // attacker logic does it. --Mastadon
1361  eval_ship_as_closest_target_args.turret_attacking_target = 1;
1362  eval_ship_as_closest_target_args.shipp = prospective_victim_ship_ptr;
1363  evaluate_ship_as_closest_target( &eval_ship_as_closest_target_args );
1364 
1365  new_distance = eval_ship_as_closest_target_args.min_distance;
1366  }
1367 
1368 
1369  if (new_distance <= minimum_distance) {
1370  minimum_distance = new_distance;
1371  minimum_object_ptr = prospective_victim_ptr;
1372  }
1373 
1374  if (new_distance >= maximum_distance) {
1375  maximum_distance = new_distance;
1376  maximum_object_ptr = prospective_victim_ptr;
1377  }
1378 
1379  float diff = 0.0f;
1380  if ( targeting_from_closest_to_farthest ) {
1381  diff = new_distance - current_distance;
1382  if ( diff > 0.0f ) {
1383  if ( diff < ( nearest_distance - current_distance ) ) {
1384  nearest_distance = new_distance;
1385  nearest_object_ptr = prospective_victim_ptr;
1386  }
1387  }
1388  } else {
1389  diff = current_distance - new_distance;
1390  if ( diff > 0.0f ) {
1391  if ( diff < ( current_distance - nearest_distance ) ) {
1392  nearest_distance = new_distance;
1393  nearest_object_ptr = prospective_victim_ptr;
1394  }
1395  }
1396  }
1397  }
1398 
1399  if ( nearest_object_ptr == NULL ) {
1400 
1401  if ( targeting_from_closest_to_farthest ) {
1402  if ( minimum_object_ptr != NULL ) {
1403  nearest_object_ptr = minimum_object_ptr;
1404  }
1405  } else {
1406  if ( maximum_object_ptr != NULL ) {
1407  nearest_object_ptr = maximum_object_ptr;
1408  }
1409  }
1410  }
1411 
1412  return nearest_object_ptr;
1413 }
1414 
1416 // -------------------------------------------------------------------
1417 // hud_target_missile()
1418 //
1419 // Target the closest locked missile that is locked on locked_obj
1420 //
1421 // input: source_obj => pointer to object that fired weapon
1422 // next_flag => 0 -> previous 1 -> next
1423 //
1424 // NOTE: this function is only allows targeting bombs
1425 void hud_target_missile(object *source_obj, int next_flag)
1426 {
1427  missile_obj *end, *start, *mo;
1428  object *A, *target_objp;
1429  ai_info *aip;
1430  weapon *wp;
1431  weapon_info *wip;
1432  int target_found = 0;
1433 
1434  if ( source_obj->type != OBJ_SHIP )
1435  return;
1436 
1437  Assert( Ships[source_obj->instance].ai_index != -1 );
1438  aip = &Ai_info[Ships[source_obj->instance].ai_index];
1439 
1440  end = &Missile_obj_list;
1441  if (aip->target_objnum != -1) {
1442  target_objp = &Objects[aip->target_objnum];
1443  if ( target_objp->type == OBJ_WEAPON && Weapon_info[Weapons[target_objp->instance].weapon_info_index].subtype == WP_MISSILE ) { // must be a missile
1445  }
1446  }
1447 
1448  start = advance_missile_obj(end, next_flag);
1449 
1450  for ( mo = start; mo != end; mo = advance_missile_obj(mo, next_flag) ) {
1451  if ( mo == &Missile_obj_list ){
1452  continue;
1453  }
1454 
1455  Assert(mo->objnum >= 0 && mo->objnum < MAX_OBJECTS);
1456  A = &Objects[mo->objnum];
1457 
1458  Assert(A->type == OBJ_WEAPON);
1459  Assert((A->instance >= 0) && (A->instance < MAX_WEAPONS));
1460  wp = &Weapons[A->instance];
1461  wip = &Weapon_info[wp->weapon_info_index];
1462 
1463  // only allow targeting of bombs
1464  if ( !(wip->wi_flags2 & WIF2_CAN_BE_TARGETED) ) {
1465  if ( !(wip->wi_flags & WIF_BOMB) ) {
1466  continue;
1467  }
1468  }
1469 
1470  if (wp->lssm_stage==3){
1471  continue;
1472  }
1473 
1474  // only allow targeting of hostile bombs
1476  continue;
1477  }
1478 
1479  if(hud_target_invalid_awacs(A)){
1480  continue;
1481  }
1482 
1483  // if we've reached here, got a new target
1484  target_found = TRUE;
1485  set_target_objnum( aip, OBJ_INDEX(A) );
1487  break;
1488  } // end for
1489 
1490  if ( !target_found ) {
1491  // if no bomb is found, search for bombers
1492  ship_obj *startShip, *so;
1493 
1494  if ( (aip->target_objnum != -1)
1495  && (Objects[aip->target_objnum].type == OBJ_SHIP)
1496  && ((Ship_info[Ships[Objects[aip->target_objnum].instance].ship_info_index].flags & SIF_BOMBER)
1498  int index = Ships[Objects[aip->target_objnum].instance].ship_list_index;
1499  startShip = get_ship_obj_ptr_from_index(index);
1500  } else {
1501  startShip = GET_FIRST(&Ship_obj_list);
1502  }
1503 
1504  for (so=advance_ship(startShip, next_flag); so!=startShip; so=advance_ship(so, next_flag)) {
1505  A = &Objects[so->objnum];
1506 
1507  // don't look at header
1508  if (so == &Ship_obj_list) {
1509  continue;
1510  }
1511 
1512  // only allow targeting of hostile bombs
1514  continue;
1515  }
1516 
1517  if(hud_target_invalid_awacs(A)){
1518  continue;
1519  }
1520 
1521  // check if ship type is bomber
1522  if ( !(Ship_info[Ships[A->instance].ship_info_index].flags & SIF_BOMBER) && !(A->flags & OF_TARGETABLE_AS_BOMB) ) {
1523  continue;
1524  }
1525 
1526  // check if ignore
1527  if ( Ships[A->instance].flags & TARGET_SHIP_IGNORE_FLAGS ){
1528  continue;
1529  }
1530 
1531  // found a good one
1532  target_found = TRUE;
1533  set_target_objnum( aip, OBJ_INDEX(A) );
1535  break;
1536  }
1537  }
1538 
1539  if ( !target_found ) {
1540  snd_play( &Snds[SND_TARGET_FAIL], 0.0f );
1541  }
1542 }
1543 
1544 // Return !0 if shipp can be scanned, otherwise return 0
1546 {
1547  ship_info *sip;
1548 
1549  sip = &Ship_info[shipp->ship_info_index];
1550 
1551  // ignore cargo that has already been scanned
1552  if (shipp->flags & SF_CARGO_REVEALED)
1553  return 0;
1554 
1555  // allow ships with scannable flag set
1556  if (shipp->flags & SF_SCANNABLE)
1557  return 1;
1558 
1559  // ignore ships that don't carry cargo
1560  if ((sip->class_type < 0) || !(Ship_types[sip->class_type].ship_bools & STI_SHIP_SCANNABLE))
1561  return 0;
1562 
1563  return 1;
1564 }
1565 
1566 // target the next/prev uninspected cargo container
1567 void hud_target_uninspected_cargo(int next_flag)
1568 {
1569  object *A, *start, *start2;
1570  ship *shipp;
1571  int target_found = 0;
1572 
1573  if (Player_ai->target_objnum == -1) {
1574  start = &obj_used_list;
1575  } else {
1576  start = &Objects[Player_ai->target_objnum];
1577  }
1578 
1579  start2 = advance_fb(start, next_flag);
1580 
1581  for ( A = start2; A != start; A = advance_fb(A, next_flag) ) {
1582  if ( A == &obj_used_list ) {
1583  continue;
1584  }
1585 
1586  if (A == Player_obj || (A->type != OBJ_SHIP) ) {
1587  continue;
1588  }
1589 
1590  shipp = &Ships[A->instance]; // get a pointer to the ship information
1591 
1592  if ( shipp->flags & TARGET_SHIP_IGNORE_FLAGS ) {
1593  continue;
1594  }
1595 
1596  // ignore all non-cargo carrying craft
1597  if ( !hud_target_ship_can_be_scanned(shipp) ) {
1598  continue;
1599  }
1600 
1601  if(hud_target_invalid_awacs(A)){
1602  continue;
1603  }
1604 
1605  // if we've reached here, it is a valid next target
1606  if ( Player_ai->target_objnum != OBJ_INDEX(A) ) {
1607  target_found = TRUE;
1610  }
1611  }
1612 
1613  if ( target_found == FALSE ) {
1615  }
1616 }
1617 
1618 // target the newest ship in the area
1620 {
1621  object *A, *player_target_objp;
1622  object *newest_obj=NULL;
1623  ship *shipp;
1624  ship_obj *so;
1625  uint current_target_arrived_time = 0xffffffff, newest_time = 0;
1626 
1627  if ( Player_ai->target_objnum >= 0 ) {
1628  player_target_objp = &Objects[Player_ai->target_objnum];
1629  if ( player_target_objp->type == OBJ_SHIP ) {
1630  current_target_arrived_time = Ships[player_target_objp->instance].create_time;
1631  }
1632  } else {
1633  player_target_objp = NULL;
1634  }
1635 
1636  // If no target is selected, then simply target the closest uninspected cargo
1637  if ( Player_ai->target_objnum == -1 || timestamp_elapsed(Target_newest_ship_timestamp) ) {
1638  current_target_arrived_time = 0xffffffff;
1639  }
1640 
1641  Target_newest_ship_timestamp = timestamp(TL_RESET);
1642 
1643  for ( so = GET_FIRST(&Ship_obj_list); so != END_OF_LIST(&Ship_obj_list); so = GET_NEXT(so) ) {
1644  A = &Objects[so->objnum];
1645  shipp = &Ships[A->instance]; // get a pointer to the ship information
1646 
1647  if ( (A == Player_obj) || (shipp->flags & TARGET_SHIP_IGNORE_FLAGS) )
1648  continue;
1649 
1650  // ignore navbuoys
1651  if ( Ship_info[shipp->ship_info_index].flags & SIF_NAVBUOY ) {
1652  continue;
1653  }
1654 
1655  if ( A == player_target_objp ) {
1656  continue;
1657  }
1658 
1659  if(hud_target_invalid_awacs(A)){
1660  continue;
1661  }
1662 
1663  if ( (shipp->create_time >= newest_time) && (shipp->create_time <= current_target_arrived_time) ) {
1664  newest_time = shipp->create_time;
1665  newest_obj = A;
1666  }
1667  }
1668 
1669  if (newest_obj) {
1670  set_target_objnum( Player_ai, OBJ_INDEX(newest_obj) );
1671  hud_shield_hit_reset(newest_obj);
1672  // if BIG|HUGE and no selected subsystem, get sorted turret
1675  }
1676  else {
1678  }
1679 }
1680 
1681 #define TYPE_NONE 0
1682 #define TYPE_FACING_BEAM 1
1683 #define TYPE_FACING_FLAK 2
1684 #define TYPE_FACING_MISSILE 3
1685 #define TYPE_FACING_LASER 4
1686 #define TYPE_NONFACING_BEAM 5
1687 #define TYPE_NONFACING_FLAK 6
1688 #define TYPE_NONFACING_MISSILE 7
1689 #define TYPE_NONFACING_LASER 8
1690 #define TYPE_NONFACING_INC 4
1691 
1692 typedef struct eval_next_turret {
1694  int type;
1695  float dist;
1697 
1698 int turret_compare_func(const void *e1, const void *e2)
1699 {
1702 
1703  Assert(p1->type != TYPE_NONE);
1704  Assert(p2->type != TYPE_NONE);
1705 
1706  if (p1->type != p2->type) {
1707  return (p1->type - p2->type);
1708  } else {
1709  float delta_dist = p1->dist - p2->dist;
1710  if (delta_dist < 0) {
1711  return -1;
1712  } else if (delta_dist > 0) {
1713  return 1;
1714  } else {
1715  return 0;
1716  }
1717  }
1718 }
1719 
1720 extern bool turret_weapon_has_flags(ship_weapon *swp, int flags);
1721 extern bool turret_weapon_has_subtype(ship_weapon *swp, int subtype);
1722 // target the next/prev live turret on the current target
1723 // auto_advance from hud_update_closest_turret
1724 void hud_target_live_turret(int next_flag, int auto_advance, int only_player_target)
1725 {
1726  ship_subsys *A;
1727  ship_subsys *live_turret=NULL;
1728  ship *target_shipp;
1729  object *objp;
1731  int num_live_turrets = 0;
1732 
1733  // make sure we're targeting a ship
1734  if (Player_ai->target_objnum == -1 && !auto_advance) {
1736  return;
1737  }
1738 
1739  // only targeting subsystems on ship
1740  if ((Objects[Player_ai->target_objnum].type != OBJ_SHIP) && (!auto_advance)) {
1742  return;
1743  }
1744 
1745  // set some pointers
1746  objp = &Objects[Player_ai->target_objnum];
1747  target_shipp = &Ships[objp->instance];
1748 
1749  // set timestamp
1750  int timestamp_val = 0;
1751  if (!auto_advance) {
1752  timestamp_val = Target_next_turret_timestamp;
1753  Target_next_turret_timestamp = timestamp(TURRET_RESET);
1754  }
1755 
1756  // If no target is selected, then simply target the closest (or facing) turret
1757  int last_subsys_turret = FALSE;
1758  if (Player_ai->targeted_subsys != NULL) {
1761  last_subsys_turret = TRUE;
1762  }
1763  }
1764  }
1765 
1766  // do we want the closest turret (or the one our ship is pointing at)
1767  int get_closest_turret = (auto_advance || !last_subsys_turret || timestamp_elapsed(timestamp_val));
1768 
1769  // initialize eval struct
1770  memset(ent,0, sizeof(ent));
1771  int use_straight_ahead_turret = FALSE;
1772 
1773  // go through list of turrets
1774  for (A=GET_FIRST(&target_shipp->subsys_list); A!=END_OF_LIST(&target_shipp->subsys_list); A=GET_NEXT(A)) {
1775  // get a turret
1776  if (A->system_info->type == SUBSYSTEM_TURRET) {
1777  // niffiwan: ignore untargetable turrets
1778  if ( A->flags & SSF_UNTARGETABLE ) {
1779  continue;
1780  }
1781  // check turret has hit points and has a weapon
1782  if ( (A->current_hits > 0) && (A->weapons.num_primary_banks > 0 || A->weapons.num_secondary_banks > 0) ) {
1783  if ( !only_player_target || (A->turret_enemy_objnum == OBJ_INDEX(Player_obj)) ) {
1784  vec3d gsubpos, vec_to_subsys;
1785  float distance, dot;
1786  // get world pos of subsystem and its distance
1787  get_subsystem_world_pos(objp, A, &gsubpos);
1788  distance = vm_vec_normalized_dir(&vec_to_subsys, &gsubpos, &View_position);
1789 
1790  // check if facing and in view
1791  int facing = ship_subsystem_in_sight(objp, A, &View_position, &gsubpos, 0);
1792 
1793  if (!auto_advance && get_closest_turret && !only_player_target) {
1794  // if within 3 degrees and not previous subsys, use subsys in front
1795  dot = vm_vec_dot(&vec_to_subsys, &Player_obj->orient.vec.fvec);
1796  if ((dot > 0.9986) && facing) {
1797  use_straight_ahead_turret = TRUE;
1798  break;
1799  }
1800  }
1801 
1802  // set weapon_type to allow sort of ent on type
1804  ent[num_live_turrets].type = TYPE_FACING_BEAM;
1805  } else if (turret_weapon_has_flags(&A->weapons, WIF_FLAK)) {
1806  ent[num_live_turrets].type = TYPE_FACING_FLAK;
1807  } else {
1809  ent[num_live_turrets].type = TYPE_FACING_MISSILE;
1810  } else if (turret_weapon_has_subtype(&A->weapons, WP_LASER)) {
1811  ent[num_live_turrets].type = TYPE_FACING_LASER;
1812  } else {
1813  //Turret not live, bail
1814  continue;
1815  }
1816  }
1817 
1818  // fill out ent struct
1819  ent[num_live_turrets].ss = A;
1820  ent[num_live_turrets].dist = distance;
1821  if (!facing) {
1822  ent[num_live_turrets].type += TYPE_NONFACING_INC;
1823  }
1824  num_live_turrets++;
1825  }
1826  }
1827  }
1828  }
1829 
1830  // sort the list if we're not using turret straight ahead of us
1831  if (!use_straight_ahead_turret) {
1832  insertion_sort(ent, num_live_turrets, sizeof(eval_next_turret), turret_compare_func);
1833  }
1834 
1835  if (use_straight_ahead_turret) {
1836  // use the straight ahead turret
1837  live_turret = A;
1838  } else {
1839  // check if we have a currently targeted turret and find its position after the sort
1840  int i, start_index, next_index;
1841  if (get_closest_turret) {
1842  start_index = 0;
1843  } else {
1844  start_index = -1;
1845  for (i=0; i<num_live_turrets; i++) {
1846  if (ent[i].ss == Player_ai->targeted_subsys) {
1847  start_index = i;
1848  break;
1849  }
1850  }
1851  // check that we started with a turret
1852  if (start_index == -1) {
1853  start_index = 0;
1854  }
1855  }
1856 
1857  // set next live turret
1858  if (num_live_turrets == 0) {
1859  // no live turrets
1860  live_turret = NULL;
1861  } else if (num_live_turrets == 1 || get_closest_turret) {
1862  // only 1 live turret, so set it
1863  live_turret = ent[0].ss;
1864  } else {
1865  if (next_flag) {
1866  // advance to next closest turret
1867  next_index = start_index + 1;
1868  if (next_index == num_live_turrets) {
1869  next_index = 0;
1870  }
1871  } else {
1872  // go to next farther turret
1873  next_index = start_index - 1;
1874  if (next_index == -1) {
1875  next_index = num_live_turrets - 1;
1876  }
1877  }
1878 
1879  // set the next turret to be targeted based on next_index
1880  live_turret = ent[next_index].ss;
1881  }
1882 
1883  //if (live_turret) {
1884  // debug info
1885  // mprintf(("name %s, index: %d, type: %d\n", live_turret->system_info->subobj_name, next_index, ent[next_index].type));
1886  //}
1887  }
1888 
1889  if ( live_turret != NULL ) {
1892  } else {
1893  if (!auto_advance) {
1895  }
1896  }
1897 }
1898 
1899 
1900 // -------------------------------------------------------------------
1901 // hud_target_closest_locked_missile()
1902 //
1903 // Target the closest locked missile that is locked on locked_obj
1904 //
1905 // input: locked_obj => pointer to object that you want to find
1906 // closest missile to
1907 //
1908 void hud_target_closest_locked_missile(object *locked_obj)
1909 {
1910  object *A, *nearest_obj=NULL;
1911  weapon *wp;
1912  weapon_info *wip;
1913  float nearest_dist, dist;
1914  int target_found = FALSE;
1915  missile_obj *mo;
1916 
1917  nearest_dist = 10000.0f;
1918 
1919  for ( mo = GET_NEXT(&Missile_obj_list); mo != END_OF_LIST(&Missile_obj_list); mo = GET_NEXT(mo) ) {
1920  Assert(mo->objnum >= 0 && mo->objnum < MAX_OBJECTS);
1921  A = &Objects[mo->objnum];
1922 
1923  if (A->type != OBJ_WEAPON){
1924  continue;
1925  }
1926 
1927  Assert((A->instance >= 0) && (A->instance < MAX_WEAPONS));
1928  wp = &Weapons[A->instance];
1929  wip = &Weapon_info[wp->weapon_info_index];
1930 
1931  if ( wip->subtype != WP_MISSILE ){
1932  continue;
1933  }
1934 
1935  if ( !(wip->wi_flags & WIF_HOMING ) ){
1936  continue;
1937  }
1938 
1939  if (wp->lssm_stage==3){
1940  continue;
1941  }
1942 
1943  if(hud_target_invalid_awacs(A)){
1944  continue;
1945  }
1946 
1947 
1948  if (wp->homing_object == locked_obj) {
1949  dist = vm_vec_dist_quick(&A->pos, &locked_obj->pos); // Find distance!
1950 
1951  if (dist < nearest_dist) {
1952  nearest_obj = A;
1953  nearest_dist = dist;
1954  }
1955  }
1956  } // end for
1957 
1958  if (nearest_dist < 10000.0f) {
1959  Assert(nearest_obj);
1960  set_target_objnum( Player_ai, OBJ_INDEX(nearest_obj) );
1961  hud_shield_hit_reset(nearest_obj);
1962  target_found = TRUE;
1963  }
1964 
1965  if ( !target_found ){
1966  snd_play( &Snds[SND_TARGET_FAIL], 0.0f );
1967  }
1968 }
1969 
1970 // select a new target, by auto-targeting
1972 {
1973  if ( Framecount < 2 ) {
1974  return;
1975  }
1976 
1977  // No auto-targeting after dead.
1978  if (Game_mode & (GM_DEAD | GM_DEAD_BLEW_UP))
1979  return;
1980 
1981  // try target next ship in hotkey set, if any -- Backslash
1982  if ( Player->current_hotkey_set != -1 ) {
1984  }
1985 
1986  int valid_team_mask = iff_get_attackee_mask(Player_ship->team);
1987 
1988  // if none, try targeting closest hostile fighter/bomber
1989  if ( Player_ai->target_objnum == -1 ) { //-V581
1990  hud_target_closest(valid_team_mask, -1, FALSE, TRUE);
1991  }
1992 
1993  // No fighter/bombers exists, so go ahead an target the closest hostile
1994  if ( Player_ai->target_objnum == -1 ) { //-V581
1995  hud_target_closest(valid_team_mask, -1, FALSE);
1996  }
1997 
1998  // um, ok. Try targeting asteroids that are on a collision course for an escort ship
1999  if ( Player_ai->target_objnum == -1 ) { //-V581
2001  }
2002 }
2003 
2004 
2005 // Given that object 'targeter' is targeting object 'targetee',
2006 // how far are they? This uses the point closest to the targeter
2007 // object on the targetee's bounding box. So if targeter is inside
2008 // targtee's bounding box, the distance is 0.
2009 float hud_find_target_distance( object *targetee, object *targeter )
2010 {
2011  vec3d tmp_pnt;
2012 
2013  int model_num = -1;
2014 
2015  // Which model is it?
2016  switch( targetee->type ) {
2017  case OBJ_SHIP:
2018  model_num = Ship_info[Ships[targetee->instance].ship_info_index].model_num;
2019  break;
2020  case OBJ_DEBRIS:
2021 // model_num = Debris[targetee->instance].model_num;
2022  break;
2023  case OBJ_WEAPON:
2024  // Don't find model_num since circles would work better
2025  //model_num = Weapon_info[Weapons[targetee->instance].weapon_info_index].model_num;
2026  break;
2027  case OBJ_ASTEROID:
2028  // Don't find model_num since circles would work better
2029  //model_num = Asteroid_info[Asteroids[targetee->instance].type].model_num;
2030  break;
2031  case OBJ_JUMP_NODE:
2032  // Don't find model_num since circles would work better
2033  //model_num = Jump_nodes[targetee->instance].modelnum;
2034  break;
2035  }
2036 
2037  float dist = 0.0f;
2038 
2039  // New way, that uses bounding box.
2040  if ( model_num > -1 ) {
2041  dist = model_find_closest_point( &tmp_pnt, model_num, -1, &targetee->orient, &targetee->pos, &targeter->pos );
2042  } else {
2043  // Old way, that uses radius.
2044  dist = vm_vec_dist_quick(&targetee->pos, &targeter->pos) - targetee->radius;
2045  if ( dist < 0.0f ) {
2046  dist = 0.0f;
2047  }
2048  }
2049  return dist;
2050 }
2051 
2052 //
2053 
2070 {
2071  int targeting_player, turret_is_attacking;
2072  ship_subsys *ss;
2073  float new_distance;
2074 
2075  // initialize
2076  esct_p->min_distance = FLT_MAX;
2077  esct_p->check_nearest_turret = FALSE;
2078  turret_is_attacking = FALSE;
2079 
2080 
2081  object *objp = &Objects[esct_p->shipp->objnum];
2082  Assert(objp->type == OBJ_SHIP);
2083  if (objp->type != OBJ_SHIP) {
2084  return false;
2085  }
2086 
2087  // player being targeted, so we will want closest distance from player
2088  targeting_player = (esct_p->attacked_objnum == OBJ_INDEX(Player_obj));
2089 
2090  // filter on team
2091  if ( !iff_matches_mask(esct_p->shipp->team, esct_p->team_mask) ) {
2092  return false;
2093  }
2094 
2095  // check if player or ignore ship
2096  if ( (esct_p->shipp->objnum == OBJ_INDEX(Player_obj)) || (esct_p->shipp->flags & TARGET_SHIP_IGNORE_FLAGS) ) {
2097  return false;
2098  }
2099 
2100  // bail if harmless
2101  if ( Ship_info[esct_p->shipp->ship_info_index].class_type > -1 && !(Ship_types[Ship_info[esct_p->shipp->ship_info_index].class_type].hud_bools & STI_HUD_TARGET_AS_THREAT)) {
2102  return false;
2103  }
2104 
2105  // only look at targets that are AWACS valid
2106  if (hud_target_invalid_awacs(&Objects[esct_p->shipp->objnum])) {
2107  return false;
2108  }
2109 
2110  // If filter is set, only target fighters and bombers
2111  if ( esct_p->filter ) {
2112  if ( !(Ship_info[esct_p->shipp->ship_info_index].flags & (SIF_FIGHTER | SIF_BOMBER)) ) {
2113  return false;
2114  }
2115  }
2116 
2117  // find closest turret to player if BIG or HUGE ship
2118  if (Ship_info[esct_p->shipp->ship_info_index].flags & (SIF_BIG_SHIP|SIF_HUGE_SHIP)) {
2119  for (ss=GET_FIRST(&esct_p->shipp->subsys_list); ss!=END_OF_LIST(&esct_p->shipp->subsys_list); ss=GET_NEXT(ss)) {
2120 
2121  if (ss->flags & SSF_UNTARGETABLE)
2122  continue;
2123 
2124  if ( (ss->system_info->type == SUBSYSTEM_TURRET) && (ss->current_hits > 0) ) {
2125 
2126  if (esct_p->check_all_turrets || (ss->turret_enemy_objnum == esct_p->attacked_objnum)) {
2127  turret_is_attacking = 1;
2128  esct_p->check_nearest_turret = TRUE;
2129 
2130  if ( !esct_p->turret_attacking_target || (esct_p->turret_attacking_target && (ss->turret_enemy_objnum == esct_p->attacked_objnum)) ) {
2131  vec3d gsubpos;
2132  // get world pos of subsystem
2133  vm_vec_unrotate(&gsubpos, &ss->system_info->pnt, &objp->orient);
2134  vm_vec_add2(&gsubpos, &objp->pos);
2135  new_distance = vm_vec_dist_quick(&gsubpos, &Player_obj->pos);
2136 
2137  /*
2138  // GET TURRET TYPE, FAVOR BEAM, FLAK, OTHER
2139  int turret_type = ss->system_info->turret_weapon_type;
2140  if (Weapon_info[turret_type].wi_flags & WIF_BEAM) {
2141  new_distance *= 0.3f;
2142  } else if (Weapon_info[turret_type].wi_flags & WIF_FLAK) {
2143  new_distance *= 0.6f;
2144  } */
2145 
2146  // get the closest distance
2147  if (new_distance <= esct_p->min_distance) {
2148  esct_p->min_distance = new_distance;
2149  }
2150  }
2151  }
2152  }
2153  }
2154  }
2155 
2156  // If no turret is attacking, check if objp is actually targeting attacked_objnum
2157  // don't bail if targeting is for player
2158  if ( !targeting_player && !turret_is_attacking ) {
2159  ai_info *aip = &Ai_info[esct_p->shipp->ai_index];
2160 
2161  if (aip->target_objnum != esct_p->attacked_objnum) {
2162  return false;
2163  }
2164 
2165  if ( (Game_mode & GM_NORMAL) && ( aip->mode != AIM_CHASE ) && (aip->mode != AIM_STRAFE) && (aip->mode != AIM_EVADE) && (aip->mode != AIM_EVADE_WEAPON) && (aip->mode != AIM_AVOID)) {
2166  return false;
2167  }
2168  }
2169 
2170  // consider the ship alone if there are no attacking turrets
2171  if ( !turret_is_attacking ) {
2172  //new_distance = hud_find_target_distance(objp, Player_obj);
2173  new_distance = vm_vec_dist_quick(&objp->pos, &Player_obj->pos);
2174 
2175  if (new_distance <= esct_p->min_distance) {
2176  esct_p->min_distance = new_distance;
2177  esct_p->check_nearest_turret = FALSE;
2178  }
2179  }
2180 
2181  return true;
2182 }
2183 
2212 int hud_target_closest(int team_mask, int attacked_objnum, int play_fail_snd, int filter, int get_closest_turret_attacking_player)
2213 {
2214  object *A;
2215  object *nearest_obj = &obj_used_list;
2216  ship *shipp;
2217  ship_obj *so;
2218  int check_nearest_turret = FALSE;
2219 
2220  // evaluate ship closest target struct
2221  esct eval_ship_as_closest_target_args;
2222 
2223  float min_distance = FLT_MAX;
2224  int target_found = FALSE;
2225 
2226  int player_obj_index = OBJ_INDEX(Player_obj);
2227  ship_subsys *ss;
2228 
2229  int initial_attacked_objnum = attacked_objnum;
2230 
2231  if ( (attacked_objnum >= 0) && (attacked_objnum != player_obj_index) ) {
2232  // bail if player does not have target
2233  if ( Player_ai->target_objnum == -1 ) {
2234  goto Target_closest_done;
2235  }
2236 
2237  if ( Objects[attacked_objnum].type != OBJ_SHIP ) {
2238  goto Target_closest_done;
2239  }
2240 
2241  // bail if ship is to be ignored
2242  if (Ships[Objects[attacked_objnum].instance].flags & TARGET_SHIP_IGNORE_FLAGS) {
2243  goto Target_closest_done;
2244  }
2245  }
2246 
2247  if (attacked_objnum == -1) {
2248  attacked_objnum = player_obj_index;
2249  }
2250 
2251  // check all turrets if for player.
2252  eval_ship_as_closest_target_args.check_all_turrets = (attacked_objnum == player_obj_index);
2253  eval_ship_as_closest_target_args.filter = filter;
2254  eval_ship_as_closest_target_args.team_mask = team_mask;
2255  eval_ship_as_closest_target_args.attacked_objnum = attacked_objnum;
2256  eval_ship_as_closest_target_args.turret_attacking_target = get_closest_turret_attacking_player;
2257 
2258  for ( so=GET_FIRST(&Ship_obj_list); so!=END_OF_LIST(&Ship_obj_list); so=GET_NEXT(so) ) {
2259 
2260  A = &Objects[so->objnum];
2261  shipp = &Ships[A->instance]; // get a pointer to the ship information
2262 
2263  // fill in rest of eval_ship_as_closest_target_args
2264  eval_ship_as_closest_target_args.shipp = shipp;
2265 
2266  // Filter out any target that is not targeting the player --Mastadon
2267  if ( (initial_attacked_objnum == player_obj_index) && (Ai_info[shipp->ai_index].target_objnum != player_obj_index) ) {
2268  continue;
2269  }
2270  // check each shipp on list and update nearest obj and subsys
2271  evaluate_ship_as_closest_target(&eval_ship_as_closest_target_args);
2272  if (eval_ship_as_closest_target_args.min_distance < min_distance) {
2273  target_found = TRUE;
2274  min_distance = eval_ship_as_closest_target_args.min_distance;
2275  nearest_obj = A;
2276  check_nearest_turret = eval_ship_as_closest_target_args.check_nearest_turret;
2277  }
2278  }
2279 
2280  Target_closest_done:
2281 
2282  // maybe ignore target if too far away
2283  // DKA 9/8/99 Remove distance check
2284  /*
2285  if (target_found) {
2286  // get distance to nearest attacker
2287  float dist = vm_vec_dist_quick(&Objects[attacked_objnum].pos, &nearest_obj->pos);
2288 
2289  // no distance limit for player obj
2290  if ((attacked_objnum != player_obj_index) && (dist > MIN_DISTANCE_TO_CONSIDER_THREAT)) {
2291  target_found = FALSE;
2292  }
2293  } */
2294 
2295  if (target_found) {
2296  set_target_objnum(Player_ai, OBJ_INDEX(nearest_obj));
2297  hud_shield_hit_reset(nearest_obj);
2298  if ( check_nearest_turret ) {
2299 
2300  // if former subobject was not a turret do, not change subsystem
2301  ss = Ships[nearest_obj->instance].last_targeted_subobject[Player_num];
2302  if (ss == NULL || get_closest_turret_attacking_player) {
2303  // update nearest turret with later func
2304  hud_target_live_turret(1, 1, get_closest_turret_attacking_player);
2306  }
2307  } else {
2309  }
2310  } else {
2311  // no target found, maybe play fail sound
2312  if (play_fail_snd == TRUE) {
2314  }
2315  }
2316 
2317  return target_found;
2318 }
2319 
2320 // auto update closest turret to attack on big or huge ships
2322 {
2323  hud_target_live_turret(1, 1);
2324 
2325 /*
2326  float nearest_distance, new_distance;
2327  ship_subsys *ss, *closest_subsys;
2328  ship *shipp;
2329  object *objp;
2330 
2331  nearest_distance = FLT_MAX;
2332  objp = &Objects[Player_ai->target_objnum];
2333  shipp = &Ships[objp->instance];
2334  closest_subsys = NULL;
2335 
2336 
2337  Assert(Ship_info[shipp->ship_info_index].flags & (SIF_BIG_SHIP|SIF_HUGE_SHIP));
2338 
2339  for (ss=GET_FIRST(&shipp->subsys_list); ss!=END_OF_LIST(&shipp->subsys_list); ss=GET_NEXT(ss)) {
2340  if ( (ss->system_info->type == SUBSYSTEM_TURRET) && (ss->current_hits > 0) ) {
2341  // make sure turret is not "unused"
2342  if (ss->system_info->turret_weapon_type >= 0) {
2343  vec3d gsubpos;
2344  // get world pos of subsystem
2345  vm_vec_unrotate(&gsubpos, &ss->system_info->pnt, &objp->orient);
2346  vm_vec_add2(&gsubpos, &objp->pos);
2347  new_distance = vm_vec_dist_quick(&gsubpos, &Player_obj->pos);
2348 
2349  // GET TURRET TYPE, FAVOR BEAM, FLAK, OTHER
2350  int turret_type = ss->system_info->turret_weapon_type;
2351  if (Weapon_info[turret_type].wi_flags & WIF_BEAM) {
2352  new_distance *= 0.3f;
2353  } else if (Weapon_info[turret_type].wi_flags & WIF_FLAK) {
2354  new_distance *= 0.6f;
2355  }
2356 
2357  // check if facing and in view
2358  int facing = ship_subsystem_in_sight(objp, ss, &View_position, &gsubpos, 0);
2359 
2360  if (facing) {
2361  new_distance *= 0.5f;
2362  }
2363 
2364  // get the closest distance
2365  if (new_distance <= nearest_distance) {
2366  nearest_distance = new_distance;
2367  closest_subsys = ss;
2368  }
2369  }
2370  }
2371  }
2372 
2373  // check if new subsys to target
2374  if (Player_ai->targeted_subsys != NULL) {
2375  set_targeted_subsys(Player_ai, closest_subsys, Player_ai->target_objnum);
2376  shipp->last_targeted_subobject[Player_num] = Player_ai->targeted_subsys;
2377  }
2378  */
2379 }
2380 
2381 
2382 // --------------------------------------------------------------------
2383 // hud_target_targets_target()
2384 //
2385 // Target your target's target. Your target is specified by objnum passed
2386 // as a parameter.
2387 //
2389 {
2390  object *objp = NULL;
2391  object *tt_objp = NULL;
2392  int tt_objnum;
2393 
2395  goto ttt_fail;
2396  }
2397 
2398  objp = &Objects[Player_ai->target_objnum];
2399  if ( objp->type != OBJ_SHIP ) {
2400  goto ttt_fail;
2401  }
2402 
2403  tt_objnum = Ai_info[Ships[objp->instance].ai_index].target_objnum;
2404  if ( tt_objnum < 0 || tt_objnum >= MAX_OBJECTS ) {
2405  goto ttt_fail;
2406  }
2407 
2408  if ( tt_objnum == OBJ_INDEX(Player_obj) ) {
2409  goto ttt_fail;
2410  }
2411 
2412  tt_objp = &Objects[tt_objnum];
2413 
2414  if (hud_target_invalid_awacs(tt_objp)) {
2415  goto ttt_fail;
2416  }
2417 
2418  if ( tt_objp->type != OBJ_SHIP ) {
2419  goto ttt_fail;
2420  }
2421 
2422  if ( Ships[tt_objp->instance].flags & TARGET_SHIP_IGNORE_FLAGS ) {
2423  goto ttt_fail;
2424  }
2425 
2426  // if we've reached here, found player target's target
2427  set_target_objnum( Player_ai, tt_objnum );
2428  hud_shield_hit_reset(&Objects[tt_objnum]);
2429  if (Objects[tt_objnum].type == OBJ_SHIP) {
2430  hud_maybe_set_sorted_turret_subsys(&Ships[Objects[tt_objnum].instance]);
2431  }
2432  hud_restore_subsystem_target(&Ships[Objects[tt_objnum].instance]);
2433  return;
2434 
2435  ttt_fail:
2436  snd_play( &Snds[SND_TARGET_FAIL], 0.0f );
2437 }
2438 
2439 // Return !0 if target_objp is a valid object type for targeting in reticle, otherwise return 0
2440 int object_targetable_in_reticle(object *target_objp)
2441 {
2442  int obj_type;
2444 
2445  if (target_objp == Player_obj ) {
2446  return 0;
2447  }
2448 
2449  obj_type = target_objp->type;
2450 
2451  if ( (obj_type == OBJ_SHIP) || (obj_type == OBJ_DEBRIS) || (obj_type == OBJ_WEAPON) || (obj_type == OBJ_ASTEROID) )
2452  {
2453  return 1;
2454  } else if ( obj_type == OBJ_JUMP_NODE )
2455  {
2456  for (jnp = Jump_nodes.begin(); jnp != Jump_nodes.end(); ++jnp) {
2457  if(jnp->GetSCPObject() == target_objp)
2458  break;
2459  }
2460 
2461  if (!jnp->IsHidden())
2462  return 1;
2463  }
2464 
2465  return 0;
2466 }
2467 
2468 // hud_target_in_reticle_new() will target the object that is closest to the player, and who is
2469 // intersected by a ray passed from the center of the reticle out along the forward vector of the
2470 // player.
2471 //
2472 // targeting of objects of type OBJ_SHIP and OBJ_DEBRIS are supported
2473 //
2474 // Method: A ray is cast from the center of the reticle, and we keep track of any eligible object
2475 // the ray intersects. We take the ship closest to us that intersects an object.
2476 //
2477 // Since this method may work poorly with objects that are far away, hud_target_in_reticle_old()
2478 // is called if no intersections are found.
2479 //
2480 //
2481 #define TARGET_IN_RETICLE_DISTANCE 10000.0f
2482 
2484 {
2485  vec3d terminus;
2486  object *A;
2487  mc_info mc;
2488  float dist;
2490 
2491  hud_reticle_clear_list(&Reticle_cur_list);
2493 
2494  // Get 3d vector through center of reticle
2496 
2497  mc_info_init(&mc);
2498  mc.model_instance_num = -1;
2499  mc.model_num = 0;
2500  for ( A = GET_FIRST(&obj_used_list); A !=END_OF_LIST(&obj_used_list); A = GET_NEXT(A) ) {
2501  if ( !object_targetable_in_reticle(A) ) {
2502  continue;
2503  }
2504 
2505  if ( A->type == OBJ_WEAPON ) {
2508  continue;
2509  }
2510  }
2511  if (Weapons[A->instance].lssm_stage==3){
2512  continue;
2513  }
2514  }
2515 
2516 
2517  if ( A->type == OBJ_SHIP ) {
2519  continue;
2520  }
2521  }
2522 
2523  if(hud_target_invalid_awacs(A)){
2524  continue;
2525  }
2526 
2527  switch (A->type) {
2528  case OBJ_SHIP:
2529  mc.model_num = Ship_info[Ships[A->instance].ship_info_index].model_num;
2530  break;
2531  case OBJ_DEBRIS:
2533  break;
2534  case OBJ_WEAPON:
2536  break;
2537  case OBJ_ASTEROID:
2538  {
2539  int pof = 0;
2541  mc.model_num = Asteroid_info[Asteroids[A->instance].asteroid_type].model_num[pof];
2542  }
2543  break;
2544  case OBJ_JUMP_NODE:
2545  for (jnp = Jump_nodes.begin(); jnp != Jump_nodes.end(); ++jnp) {
2546  if(jnp->GetSCPObject() == A)
2547  break;
2548  }
2549 
2550  mc.model_num = jnp->GetModelNumber();
2551  break;
2552  default:
2553  Int3(); // Illegal object type.
2554  }
2555 
2556  if (mc.model_num == -1) {
2557  // so just check distance of a point
2558  vec3d temp_v;
2559  float angle;
2560  vm_vec_sub(&temp_v, &A->pos, &Eye_position);
2561  vm_vec_normalize(&temp_v);
2562  angle = vm_vec_dot(&Player_obj->orient.vec.fvec, &temp_v);
2563  if (angle > 0.99f) {
2564  dist = vm_vec_mag_squared(&temp_v);
2565  hud_reticle_list_update(A, dist, 0);
2566  }
2567 
2568  } else {
2569 
2571  mc.orient = &A->orient; // The object's orient
2572  mc.pos = &A->pos; // The object's position
2573  mc.p0 = &Eye_position; // Point 1 of ray to check
2574  mc.p1 = &terminus; // Point 2 of ray to check
2575  mc.flags = MC_CHECK_MODEL; // | MC_ONLY_BOUND_BOX; // check the model, but only its bounding box
2576 
2577  model_collide(&mc);
2578  if ( mc.num_hits ) {
2580  hud_reticle_list_update(A, dist, 0);
2581  }
2582  }
2583  } // end for (go to next object)
2584 
2585  hud_target_in_reticle_old(); // try the old method (works well with ships far away)
2586 }
2587 
2588 // hud_target_in_reticle_old() will target the object that is closest to the reticle center and inside
2589 // the reticle
2590 //
2591 // targeting of objects of type OBJ_SHIP and OBJ_DEBRIS are supported
2592 //
2593 //
2594 // Method: take the dot product of the foward vector and the vector to target. Take
2595 // the one that is closest to 1 and at least MIN_DOT_FOR_TARGET
2596 //
2597 // IMPORTANT: The MIN_DOT_FOR_TARGET value was arrived at by trial and error and
2598 // is only valid for the HUD reticle in use at that time.
2599 
2600 #define MIN_DOT_FOR_TARGET 0.9726// fov for targeting in reticle
2601 
2603 {
2604  object *A, *target_obj;
2605  float dot;
2606  vec3d vec_to_target;
2607 
2608  for ( A = GET_FIRST(&obj_used_list); A !=END_OF_LIST(&obj_used_list); A = GET_NEXT(A) ) {
2609  if ( !object_targetable_in_reticle(A) ) {
2610  continue;
2611  }
2612 
2613  if ( A->type == OBJ_WEAPON ) {
2616  continue;
2617  }
2618  }
2619 
2620  if (Weapons[A->instance].lssm_stage==3){
2621  continue;
2622  }
2623  }
2624 
2625  if ( A->type == OBJ_SHIP ) {
2627  continue;
2628  }
2629  }
2630 
2631  if(hud_target_invalid_awacs(A)){
2632  continue;
2633  }
2634 
2635  if ( vm_vec_same( &A->pos, &Eye_position ) ) {
2636  continue;
2637  }
2638 
2639  vm_vec_normalized_dir(&vec_to_target, &A->pos, &Eye_position);
2640  dot = vm_vec_dot(&Player_obj->orient.vec.fvec, &vec_to_target);
2641 
2642  if ( dot > MIN_DOT_FOR_TARGET ) {
2643  hud_reticle_list_update(A, dot, 1);
2644  }
2645  }
2646 
2647  target_obj = hud_reticle_pick_target();
2648  if ( target_obj != NULL ) {
2649  set_target_objnum( Player_ai, OBJ_INDEX(target_obj) );
2650  hud_shield_hit_reset(target_obj);
2651  if ( target_obj->type == OBJ_SHIP ) {
2652  // if BIG|HUGE, maybe set subsys to turret
2655  }
2656  }
2657  else {
2658  snd_play( &Snds[SND_TARGET_FAIL], 0.0f );
2659  }
2660 }
2661 
2662 // hud_target_subsystem_in_reticle() will target the subsystem that is within the reticle and
2663 // is closest to the reticle center. The current target is the only object that is searched for
2664 // subsystems
2665 //
2666 // Method: take the dot product of the foward vector and the vector to target. Take
2667 // the one that is closest to 1 and at least MIN_DOT_FOR_TARGET
2668 //
2669 // IMPORTANT: The MIN_DOT_FOR_TARGET value was arrived at by trial and error and
2670 // is only valid for the HUD reticle in use at that time.
2671 //
2672 
2674 {
2675  object* targetp;
2676  ship_subsys *subsys;
2677  ship_subsys *nearest_subsys = NULL;
2678  vec3d subobj_pos;
2679 
2680  float dot, best_dot;
2681  vec3d vec_to_target;
2682  best_dot = -1.0f;
2683 
2684  if ( Player_ai->target_objnum == -1){
2686  }
2687 
2688  if ( Player_ai->target_objnum == -1) { //-V581
2690  return;
2691  }
2692 
2693  targetp = &Objects[Player_ai->target_objnum];
2694 
2695  if ( targetp->type != OBJ_SHIP ){ // only targeting subsystems on ship
2696  return;
2697  }
2698 
2699  int shipnum = targetp->instance;
2700 
2701  if ( targetp->type == OBJ_SHIP ) {
2702  if ( Ships[shipnum].flags & TARGET_SHIP_IGNORE_FLAGS ) {
2703  return;
2704  }
2705  }
2706 
2707  for (subsys = GET_FIRST(&Ships[shipnum].subsys_list); subsys != END_OF_LIST(&Ships[shipnum].subsys_list) ; subsys = GET_NEXT( subsys ) ) {
2708 
2709  //if the subsystem isn't targetable, skip it
2710  if (subsys->flags & SSF_UNTARGETABLE)
2711  continue;
2712 
2713  get_subsystem_world_pos(targetp, subsys, &subobj_pos);
2714 
2715  vm_vec_normalized_dir(&vec_to_target, &subobj_pos, &Eye_position);
2716  dot = vm_vec_dot(&Player_obj->orient.vec.fvec, &vec_to_target);
2717 
2718  if ( dot > best_dot ) {
2719  best_dot = dot;
2720  if ( best_dot > MIN_DOT_FOR_TARGET )
2721  nearest_subsys = subsys;
2722  }
2723  } // end for
2724 
2725  if ( nearest_subsys != NULL ) {
2727  char r_name[NAME_LENGTH];
2728  int i;
2730  for (i = 0; r_name[i] > 0; i++) {
2731  if (r_name[i] == '|')
2732  r_name[i] = ' ';
2733  }
2734  HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Targeting subsystem %s.", 323), r_name);
2736  }
2737  else {
2739  }
2740 }
2741 
2742 #define T_LENGTH 8
2743 #define T_OFFSET_FROM_CIRCLE -13
2744 #define T_BASE_LENGTH 4
2745 
2746 // On entry:
2747 // color set
2748 void HudGaugeOrientationTee::renderOrientation(object *from_objp, object *to_objp, matrix *from_orientp)
2749 {
2750  float dot_product;
2751  vec3d target_to_obj;
2752  float x1,y1,x2,y2,x3,y3,x4,y4;
2753 
2754  vm_vec_sub(&target_to_obj, &from_objp->pos, &to_objp->pos);
2755 
2756  vm_vec_normalize(&target_to_obj);
2757 
2758  // calculate the dot product between the target_to_player vector and the targets forward vector
2759  //
2760  // 0 - vectors are perpendicular
2761  // 1 - vectors are collinear and in the same direction (target is facing player)
2762  // -1 - vectors are collinear and in the opposite direction (target is facing away from player)
2763  dot_product = vm_vec_dot(&from_orientp->vec.fvec, &target_to_obj);
2764 
2765  if (vm_vec_dot(&from_orientp->vec.rvec, &target_to_obj) >= 0) {
2766  if (dot_product >= 0){
2767  dot_product = -PI_2*dot_product + PI;
2768  } else {
2769  dot_product = -PI_2*dot_product - PI;
2770  }
2771  } else {
2772  dot_product *= PI_2; //(range is now -PI/2 => PI/2)
2773  }
2774 
2775  y1 = sinf(dot_product) * (Radius - T_OFFSET_FROM_CIRCLE);
2776  x1 = cosf(dot_product) * (Radius - T_OFFSET_FROM_CIRCLE);
2777 
2778  y1 += position[1];
2779  x1 += position[0];
2780 
2781  x1 += HUD_offset_x;
2782  y1 += HUD_offset_y;
2783 
2784  y2 = sinf(dot_product) * (Radius - T_OFFSET_FROM_CIRCLE - T_LENGTH);
2785  x2 = cosf(dot_product) * (Radius - T_OFFSET_FROM_CIRCLE - T_LENGTH);
2786 
2787  y2 += position[1];
2788  x2 += position[0];
2789 
2790  x2 += HUD_offset_x;
2791  y2 += HUD_offset_y;
2792 
2793  x3 = x1 - T_BASE_LENGTH * sinf(dot_product);
2794  y3 = y1 + T_BASE_LENGTH * cosf(dot_product);
2795  x4 = x1 + T_BASE_LENGTH * sinf(dot_product);
2796  y4 = y1 - T_BASE_LENGTH * cosf(dot_product);
2797 
2798  // HACK! Should be antialiased!
2799  renderLine(fl2i(x3),fl2i(y3),fl2i(x4),fl2i(y4)); // bottom of T
2800  renderLine(fl2i(x1),fl2i(y1),fl2i(x2),fl2i(y2)); // part of T pointing towards center
2801 }
2802 
2803 void hud_tri(float x1,float y1,float x2,float y2,float x3,float y3)
2804 {
2805  int i;
2806 
2807  // Make the triangle always be the correct handiness so
2808  // the tmapper won't think its back-facing and throw it out.
2809  float det = (y2-y1)*(x3-x1) - (x2-x1)*(y3-y1);
2810  if ( det >= 0.0f ) {
2811  float tmp;
2812 
2813  // swap y1 & y3
2814  tmp = y1;
2815  y1 = y3;
2816  y3 = tmp;
2817 
2818  // swap x1 & x3
2819  tmp = x1;
2820  x1 = x3;
2821  x3 = tmp;
2822  }
2823 
2824  vertex * vertlist[3];
2825  vertex verts[3];
2826 
2827  // zero verts[] out, this is a faster way (nods to Kazan) to make sure that
2828  // the specular colors are set to 0 to avoid rendering problems - taylor
2829  memset(verts, 0, sizeof(verts));
2830 
2831  for (i=0; i<3; i++ )
2832  vertlist[i] = &verts[i];
2833 
2834  verts[0].screen.xyw.x = x1;
2835  verts[0].screen.xyw.y = y1;
2836  verts[0].screen.xyw.w = 0.0f;
2837  verts[0].texture_position.u = 0.0f;
2838  verts[0].texture_position.v = 0.0f;
2839  verts[0].flags = PF_PROJECTED;
2840  verts[0].codes = 0;
2841  verts[0].r = (ubyte)gr_screen.current_color.red;
2842  verts[0].g = (ubyte)gr_screen.current_color.green;
2843  verts[0].b = (ubyte)gr_screen.current_color.blue;
2844  verts[0].a = (ubyte)gr_screen.current_color.alpha;
2845 
2846  verts[1].screen.xyw.x = x2;
2847  verts[1].screen.xyw.y = y2;
2848  verts[1].screen.xyw.w = 0.0f;
2849  verts[1].texture_position.u = 0.0f;
2850  verts[1].texture_position.v = 0.0f;
2851  verts[1].flags = PF_PROJECTED;
2852  verts[1].codes = 0;
2853  verts[1].r = (ubyte)gr_screen.current_color.red;
2854  verts[1].g = (ubyte)gr_screen.current_color.green;
2855  verts[1].b = (ubyte)gr_screen.current_color.blue;
2856  verts[1].a = (ubyte)gr_screen.current_color.alpha;
2857 
2858  verts[2].screen.xyw.x = x3;
2859  verts[2].screen.xyw.y = y3;
2860  verts[2].screen.xyw.w = 0.0f;
2861  verts[2].texture_position.u = 0.0f;
2862  verts[2].texture_position.v = 0.0f;
2863  verts[2].flags = PF_PROJECTED;
2864  verts[2].codes = 0;
2865  verts[2].r = (ubyte)gr_screen.current_color.red;
2866  verts[2].g = (ubyte)gr_screen.current_color.green;
2867  verts[2].b = (ubyte)gr_screen.current_color.blue;
2868  verts[2].a = (ubyte)gr_screen.current_color.alpha;
2869 
2870  for (i=0; i<3; i++)
2871  gr_resize_screen_posf(&verts[i].screen.xyw.x, &verts[i].screen.xyw.y);
2872 
2873  uint saved_mode = gr_zbuffer_get();
2874  int cull = gr_set_cull(0);
2875 
2877 
2878  //gr_tmapper( 3, vertlist, TMAP_FLAG_TRILIST );
2880 
2881  gr_zbuffer_set( saved_mode );
2882  gr_set_cull(cull);
2883 }
2884 
2885 
2886 void hud_tri_empty(float x1,float y1,float x2,float y2,float x3,float y3)
2887 {
2888  gr_line(fl2i(x1),fl2i(y1),fl2i(x2),fl2i(y2));
2889  gr_line(fl2i(x2),fl2i(y2),fl2i(x3),fl2i(y3));
2890  gr_line(fl2i(x3),fl2i(y3),fl2i(x1),fl2i(y1));
2891 }
2892 
2895 {
2896 
2897 }
2898 
2899 HudGaugeReticleTriangle::HudGaugeReticleTriangle(int _gauge_object, int _gauge_config):
2900 HudGauge(_gauge_object, _gauge_config, true, false, VM_EXTERNAL | VM_DEAD_VIEW | VM_WARP_CHASE | VM_PADLOCK_ANY | VM_OTHER_SHIP, 255, 255, 255)
2901 {
2902 
2903 }
2904 
2906 {
2907  Radius = length;
2908 }
2909 
2911 {
2913 }
2914 
2916 {
2918 }
2919 
2920 void HudGaugeReticleTriangle::render(float frametime)
2921 {
2922 }
2923 
2924 // Render a missile warning triangle that has a tail on it to indicate distance
2925 void HudGaugeReticleTriangle::renderTriangleMissileTail(float ang, float xpos, float ypos, float cur_dist, int draw_solid, int draw_inside)
2926 {
2927  float x1=0.0f;
2928  float x2=0.0f;
2929  float y1=0.0f;
2930  float y2=0.0f;
2931  float xtail=0.0f;
2932  float ytail=0.0f;
2933 
2934  float sin_ang, cos_ang, tail_len;
2935 
2936  float max_tail_len=20.0f;
2937 
2938  sin_ang=sinf(ang);
2939  cos_ang=cosf(ang);
2940 
2941  if ( cur_dist < Min_warning_missile_dist ) {
2942  tail_len = 0.0f;
2943  } else if ( cur_dist > Max_warning_missile_dist ) {
2944  tail_len = max_tail_len;
2945  } else {
2946  tail_len = cur_dist/Max_warning_missile_dist * max_tail_len;
2947  }
2948 
2949  if ( draw_inside ) {
2950  x1 = xpos - Target_triangle_base * -sin_ang;
2951  y1 = ypos + Target_triangle_base * cos_ang;
2952  x2 = xpos + Target_triangle_base * -sin_ang;
2953  y2 = ypos - Target_triangle_base * cos_ang;
2954 
2955  xpos -= Target_triangle_height * cos_ang;
2956  ypos += Target_triangle_height * sin_ang;
2957 
2958  if ( tail_len > 0 ) {
2959  xtail = xpos - tail_len * cos_ang;
2960  ytail = ypos + tail_len * sin_ang;
2961  }
2962 
2963  } else {
2964  x1 = xpos - Target_triangle_base * -sin_ang;
2965  y1 = ypos + Target_triangle_base * cos_ang;
2966  x2 = xpos + Target_triangle_base * -sin_ang;
2967  y2 = ypos - Target_triangle_base * cos_ang;
2968 
2969  xpos += Target_triangle_height * cos_ang;
2970  ypos -= Target_triangle_height * sin_ang;
2971 
2972  if ( tail_len > 0 ) {
2973  xtail = xpos + tail_len * cos_ang;
2974  ytail = ypos - tail_len * sin_ang;
2975  }
2976  }
2977 
2979  if (draw_solid) {
2980  hud_tri(xpos,ypos,x1,y1,x2,y2);
2981  } else {
2982  hud_tri_empty(xpos,ypos,x1,y1,x2,y2);
2983  }
2984 
2985  // draw the tail indicating length
2986  if ( tail_len > 0 ) {
2987  gr_line(fl2i(xpos), fl2i(ypos), fl2i(xtail), fl2i(ytail));
2988  }
2990 }
2991 
2992 // Render a triangle on the outside of the targeting circle.
2993 // Must be inside a g3_start_frame().
2994 // If aspect_flag !0, then render filled, indicating aspect lock.
2995 // If show_interior !0, then point inwards to positions inside reticle
2996 void HudGaugeReticleTriangle::renderTriangle(vec3d *hostile_pos, int aspect_flag, int show_interior, int split_tri)
2997 {
2998  vertex hostile_vertex;
2999  float ang;
3000  float xpos,ypos,cur_dist,sin_ang,cos_ang;
3001  int draw_inside=0;
3002 
3003  // determine if the given object is within the targeting reticle
3004  // (which means the triangle is not drawn)
3005 
3006  cur_dist = vm_vec_dist_quick(&Player_obj->pos, hostile_pos);
3007 
3008  g3_rotate_vertex(&hostile_vertex, hostile_pos);
3009  g3_project_vertex(&hostile_vertex);
3010 
3011  if (hostile_vertex.codes == 0) { // on screen
3012  int projected_x, projected_y;
3013 
3014  if (!(hostile_vertex.flags & PF_OVERFLOW)) { // make sure point projected
3015  int mag_squared;
3016 
3017  projected_x = fl2i(hostile_vertex.screen.xyw.x);
3018  projected_y = fl2i(hostile_vertex.screen.xyw.y);
3019 
3020  unsize(&projected_x, &projected_y);
3021 
3022  mag_squared = (projected_x - position[0]) * (projected_x - position[0]) +
3023  (projected_y - position[1]) * (projected_y - position[1]);
3024 
3025  if ( mag_squared < Radius*Radius ) {
3026  if ( show_interior ) {
3027  draw_inside=1;
3028  } else {
3029  return;
3030  }
3031  }
3032  }
3033  }
3034 
3035  int HUD_nose_scaled_x = HUD_nose_x;
3036  int HUD_nose_scaled_y = HUD_nose_y;
3037 
3038  gr_resize_screen_pos(&HUD_nose_scaled_x, &HUD_nose_scaled_y);
3039 
3040  unsize( &hostile_vertex.world.xyz.x, &hostile_vertex.world.xyz.y );
3041 
3042  ang = atan2_safe(hostile_vertex.world.xyz.y,hostile_vertex.world.xyz.x);
3043  sin_ang=sinf(ang);
3044  cos_ang=cosf(ang);
3045 
3046  if ( draw_inside ) {
3047  xpos = position[0] + cos_ang*(Radius-7);
3048  ypos = position[1] - sin_ang*(Radius-7);
3049  } else {
3050  xpos = position[0] + cos_ang*(Radius+4);
3051  ypos = position[1] - sin_ang*(Radius+4);
3052  }
3053 
3054  xpos += HUD_offset_x + HUD_nose_x;
3055  ypos += HUD_offset_y + HUD_nose_y;
3056 
3057  if ( split_tri ) {
3058  // renderTriangleMissileSplit(ang, xpos, ypos, cur_dist, aspect_flag, draw_inside);
3059  renderTriangleMissileTail(ang, xpos, ypos, cur_dist, aspect_flag, draw_inside);
3060  } else {
3061  float x1=0.0f;
3062  float x2=0.0f;
3063  float y1=0.0f;
3064  float y2=0.0f;
3065 
3066  if ( draw_inside ) {
3067  x1 = xpos - Target_triangle_base * -sin_ang;
3068  y1 = ypos + Target_triangle_base * cos_ang;
3069  x2 = xpos + Target_triangle_base * -sin_ang;
3070  y2 = ypos - Target_triangle_base * cos_ang;
3071 
3072  xpos -= Target_triangle_height * cos_ang;
3073  ypos += Target_triangle_height * sin_ang;
3074 
3075  } else {
3076  x1 = xpos - Target_triangle_base * -sin_ang;
3077  y1 = ypos + Target_triangle_base * cos_ang;
3078  x2 = xpos + Target_triangle_base * -sin_ang;
3079  y2 = ypos - Target_triangle_base * cos_ang;
3080 
3081  xpos += Target_triangle_height * cos_ang;
3082  ypos -= Target_triangle_height * sin_ang;
3083  }
3084 
3085  //renderPrintf(position[0], position[1], "%d", fl2i((360*ang)/(2*PI)));
3087  if (aspect_flag) {
3088  hud_tri(xpos,ypos,x1,y1,x2,y2);
3089  } else {
3090  hud_tri_empty(xpos,ypos,x1,y1,x2,y2);
3091  }
3093  }
3094 }
3095 
3096 // Show all homing missiles locked onto the player.
3097 // Also, play the beep!
3099 {
3100  object *A;
3101  missile_obj *mo;
3102  weapon *wp;
3103  float dist, nearest_dist;
3104  int closest_is_aspect=0;
3105 
3106  nearest_dist = Homing_beep.max_cycle_dist;
3107 
3108  for ( mo = GET_NEXT(&Missile_obj_list); mo != END_OF_LIST(&Missile_obj_list); mo = GET_NEXT(mo) ) {
3109  A = &Objects[mo->objnum];
3110  Assert((A->instance >= 0) && (A->instance < MAX_WEAPONS));
3111 
3112  wp = &Weapons[A->instance];
3113 
3114  if (wp->homing_object == Player_obj) {
3115  dist = vm_vec_dist_quick(&A->pos, &Player_obj->pos);
3116 
3117  if (dist < nearest_dist) {
3118  nearest_dist = dist;
3120  closest_is_aspect=1;
3121  } else {
3122  closest_is_aspect=0;
3123  }
3124  }
3125  }
3126  }
3127 
3128  // See if need to play warning beep.
3129  if (nearest_dist < Homing_beep.max_cycle_dist ) {
3130  float delta_time;
3131  float cycle_time;
3132 
3133  delta_time = f2fl(Missiontime - Homing_beep.last_time_played);
3134 
3135  // figure out the cycle time by doing a linear interpretation
3136  cycle_time = Homing_beep.min_cycle_time + (nearest_dist-Homing_beep.min_cycle_dist) * Homing_beep.precalced_interp;
3137 
3138  // play a new 'beep' if cycle time has elapsed
3139  if ( (delta_time*1000) > cycle_time ) {
3140  Homing_beep.last_time_played = Missiontime;
3141  if ( snd_is_playing(Homing_beep.snd_handle) ) {
3142  snd_stop(Homing_beep.snd_handle);
3143  }
3144 
3145  if ( closest_is_aspect ) {
3147  } else {
3149  }
3150  }
3151  }
3152 }
3153 
3156 {
3157 }
3158 
3160 {
3161  object *A;
3162  missile_obj *mo;
3163  weapon *wp;
3164 
3165  bool in_frame = g3_in_frame() > 0;
3166  if(!in_frame)
3167  g3_start_frame(0);
3169 
3170  gr_set_color_fast(&HUD_color_homing_indicator);
3171 
3172  for ( mo = GET_NEXT(&Missile_obj_list); mo != END_OF_LIST(&Missile_obj_list); mo = GET_NEXT(mo) ) {
3173  A = &Objects[mo->objnum];
3174  Assert((A->instance >= 0) && (A->instance < MAX_WEAPONS));
3175 
3176  wp = &Weapons[A->instance];
3177 
3178  if (wp->homing_object == Player_obj) {
3180  }
3181  }
3182 
3184  if(!in_frame)
3185  g3_end_frame();
3186 }
3187 
3190 {
3191 }
3192 
3194 {
3195  Radius = length;
3196 }
3197 
3199 {
3200 }
3201 
3202 // hud_show_orientation_tee() will draw the orientation gauge that orbits the inside of the
3203 // outer reticle ring. If the T is at 12 o'clock, the target is facing the player, if the T
3204 // is at 6 o'clock the target is facing away from the player. If the T is at 3 or 9 o'clock
3205 // the target is facing 90 away from the player.
3206 void HudGaugeOrientationTee::render(float frametime)
3207 {
3208  object* targetp;
3209 
3211  return;
3212 
3213  targetp = &Objects[Player_ai->target_objnum];
3214 
3215  if ( maybeFlashSexp() == 1 ) {
3216  hud_set_iff_color( targetp );
3217  } else {
3218  hud_set_iff_color( targetp, 1);
3219  }
3220  renderOrientation(targetp, Player_obj, &targetp->orient);
3221 }
3222 
3223 // routine to draw a bounding box around a remote detonate missile and distance to
3225 {
3226  missile_obj *mo;
3227  object *mobjp;
3228  vertex target_point;
3229 
3230  memset(&target_point, 0, sizeof(target_point));
3231 
3232  // check for currently locked missiles (highest precedence)
3233  for ( mo = GET_FIRST(&Missile_obj_list); mo != END_OF_LIST(&Missile_obj_list); mo = GET_NEXT(mo) ) {
3234  Assert(mo->objnum >= 0 && mo->objnum < MAX_OBJECTS);
3235  mobjp = &Objects[mo->objnum];
3236 
3237  if ((Player_obj != NULL) && (mobjp->parent_sig == Player_obj->parent_sig)) {
3239  // get box center point
3240  g3_rotate_vertex(&target_point,&mobjp->pos);
3241 
3242  // project vertex
3243  g3_project_vertex(&target_point);
3244 
3245  if (!(target_point.flags & PF_OVERFLOW)) { // make sure point projected
3246  switch ( mobjp->type ) {
3247  case OBJ_WEAPON:
3248  hud_target_add_display_list(mobjp, &target_point, &mobjp->pos, 0, iff_get_color(IFF_COLOR_MESSAGE, 1), NULL, TARGET_DISPLAY_DIST);
3249  break;
3250 
3251  default:
3252  Int3(); // should never happen
3253  return;
3254  }
3255 
3256  // do only for the first remote detonate missile
3257  break;
3258  }
3259  }
3260  }
3261  }
3262 }
3263 
3264 // routine to possibly draw a bounding box around a ship sending a message to the player
3266 {
3267  object *targetp;
3268  vertex target_point; // temp vertex used to find screen position for 3-D object;
3269 
3270  // don't draw brackets if no ship sending a message
3271  if ( Message_shipnum == -1 )
3272  return;
3273 
3274  targetp = &Objects[Ships[Message_shipnum].objnum];
3275  Assert ( targetp != NULL );
3276 
3277  Assert ( targetp->type == OBJ_SHIP );
3278 
3279  // Don't do this for the ship you're flying!
3280  if ( targetp == Player_obj ) {
3281  return;
3282  }
3283 
3284  // Goober5000 - don't draw if primitive sensors
3285  if ( Ships[Player_obj->instance].flags2 & SF2_PRIMITIVE_SENSORS ) {
3286  return;
3287  }
3288 
3289  // Karajorma - If we've gone to all the trouble to make our friendly ships stealthed they shouldn't then give away
3290  // their position cause they're feeling chatty
3291  // MageKing17 - Make the check see if they're actually stealthed at the time, and may as well include a check for
3292  // being hidden from sensors, too; logic copied from a similar check in hudescort.cpp
3294  || ((Ships[Message_shipnum].flags2 & SF2_STEALTH) && ((Ships[Message_shipnum].team != Player_ship->team) || (Ships[Message_shipnum].flags2 & SF2_FRIENDLY_STEALTH_INVIS)))
3295  ) {
3296  return;
3297  }
3298 
3299  Assert ( targetp->instance >=0 && targetp->instance < MAX_SHIPS );
3300 
3301  // check the object flags to see if this ship is gone. If so, then don't do this stuff anymore
3302  if ( targetp->flags & OF_SHOULD_BE_DEAD ) {
3303  Message_shipnum = -1;
3304  return;
3305  }
3306 
3307  memset(&target_point, 0, sizeof(target_point));
3308 
3309  // find the current target vertex
3310  g3_rotate_vertex(&target_point, &targetp->pos);
3311  g3_project_vertex(&target_point);
3312 
3313  if (!(target_point.flags & PF_OVERFLOW)) { // make sure point projected
3314  hud_target_add_display_list(targetp, &target_point, &targetp->pos, 10, iff_get_color(IFF_COLOR_MESSAGE, 1), NULL, 0);
3315  } else if (target_point.codes != 0) { // target center is not on screen
3316  // draw the offscreen indicator at the edge of the screen where the target is closest to
3317  // AL 11-19-97: only show offscreen indicator if player sensors are functioning
3319  if ( hud_sensors_ok(Player_ship, 0) ) {
3320  hud_target_add_display_list(targetp, &target_point, &targetp->pos, 0, iff_get_color(IFF_COLOR_MESSAGE, 1), NULL, TARGET_DISPLAY_DIST);
3321  }
3322  }
3323  }
3324 }
3325 
3326 // hud_prune_hotkeys()
3327 //
3328 // Check for ships that are dying, departed or dead. These should be removed from the player's
3329 // hotkey lists.
3331 {
3332  int i;
3333  htarget_list *hitem, *plist;
3334  object *objp;
3335  ship *sp;
3336 
3337  for ( i = 0; i < MAX_KEYED_TARGETS; i++ ) {
3338  plist = &(Players[Player_num].keyed_targets[i]);
3339  if ( EMPTY( plist ) ) // no items in list, then do nothing
3340  continue;
3341 
3342  hitem = GET_FIRST(plist);
3343  while ( hitem != END_OF_LIST(plist) ) {
3344  int remove_item;
3345 
3346  remove_item = 0;
3347 
3348  objp = hitem->objp;
3349  Assert ( objp != NULL );
3350  if ( objp->type == OBJ_SHIP ) {
3351  Assert ( objp->instance >=0 && objp->instance < MAX_SHIPS );
3352  sp = &Ships[objp->instance];
3353  } else {
3354  // if the object isn't a ship, it shouldn't be on the list, so remove it without question
3355  remove_item = 1;
3356  sp = NULL;
3357  }
3358 
3359  // check to see if the object is dying -- if so, remove it from the list
3360  // check to see if the ship is departing -- if so, remove it from the list
3361  if ( remove_item || (objp->flags & OF_SHOULD_BE_DEAD) || (sp->flags & (SF_DEPARTING|SF_DYING)) ) {
3362  if ( sp != NULL ) {
3363  nprintf(("Network", "Hotkey: Pruning %s\n", sp->ship_name));
3364  }
3365 
3366  htarget_list *temp;
3367  temp = GET_NEXT(hitem);
3368  list_remove( plist, hitem );
3369  list_append( &htarget_free_list, hitem );
3370  hitem->objp = NULL;
3371  hitem = temp;
3372  continue;
3373  }
3374  hitem = GET_NEXT( hitem );
3375  } // end while
3376  } // end for
3377 
3378  // save the hotkey sets with mission time reaches a certain point. Code was put here because this
3379  // function always called for both single/multiplayer. Maybe not the best location, but whatever.
3381 }
3382 
3384 
3385 // hud_show_selection_set draws some indicator around all the ships in the current selection set. No
3386 // indicators will be drawn if there is only 1 ship in the set.
3388 {
3389  htarget_list *hitem, *plist;
3390  object *targetp;
3391  int set, count;
3392  vertex target_point; // temp vertex used to find screen position for 3-D object;
3393 
3394  HUD_drew_selection_bracket_on_target = 0;
3395 
3397  if ( set == -1 )
3398  return;
3399 
3400  Assert ( (set >= 0) && (set < MAX_KEYED_TARGETS) );
3401  plist = &(Players[Player_num].keyed_targets[set]);
3402 
3403  count = 0;
3404  for ( hitem = GET_FIRST(plist); hitem != END_OF_LIST(plist); hitem = GET_NEXT(hitem) )
3405  count++;
3406 
3407  if ( count == 0 ) { // only one ship, do nothing
3409  return;
3410  }
3411 
3412  memset(&target_point, 0, sizeof(target_point));
3413 
3414  for ( hitem = GET_FIRST(plist); hitem != END_OF_LIST(plist); hitem = GET_NEXT(hitem) ) {
3415  targetp = hitem->objp;
3416  Assert ( targetp != NULL );
3417 
3418  ship *target_shipp = NULL;
3419 
3420  Assert ( targetp->type == OBJ_SHIP );
3421  Assert ( targetp->instance >=0 && targetp->instance < MAX_SHIPS );
3422  target_shipp = &Ships[targetp->instance];
3423 
3424  if ( (Game_mode & GM_MULTIPLAYER) && (target_shipp == Player_ship) ) {
3425  continue;
3426  }
3427 
3428  // Goober5000 - don't draw indicators for non-visible ships, per Mantis #1972
3429  // (the only way we could get here is if the hotkey set contained a mix of visible
3430  // and invisible ships)
3431  if (awacs_get_level(targetp, Player_ship, 1) < 1) {
3432  continue;
3433  }
3434 
3435  // find the current target vertex
3436  //
3437  g3_rotate_vertex(&target_point,&targetp->pos);
3438  g3_project_vertex(&target_point);
3439 
3440  if (!(target_point.flags & PF_OVERFLOW)) { // make sure point projected
3441 
3442  switch ( targetp->type ) {
3443  case OBJ_SHIP:
3444  break;
3445 
3446  default:
3447  Int3(); // should never happen
3448  return;
3449  }
3450  if ( OBJ_INDEX(targetp) == Player_ai->target_objnum ) {
3451  hud_target_add_display_list(targetp, &target_point, &targetp->pos, 5, iff_get_color(IFF_COLOR_SELECTION, 1), NULL, 0);
3452  HUD_drew_selection_bracket_on_target = 1;
3453  } else if ( Cmdline_targetinfo ) { //Backslash -- show the distance and a lead indicator
3455  } else {
3456  hud_target_add_display_list(targetp, &target_point, &targetp->pos, 5, iff_get_color(IFF_COLOR_SELECTION, 1), NULL, 0);
3457  }
3458  }
3459 
3460  if (target_point.codes != 0) { // target center is not on screen
3461  // draw the offscreen indicator at the edge of the screen where the target is closest to
3462  // AL 11-19-97: only show offscreen indicator if player sensors are functioning
3463 
3464  if ( OBJ_INDEX(targetp) != Player_ai->target_objnum ) {
3465  if ( hud_sensors_ok(Player_ship, 0) ) {
3466  hud_target_add_display_list(targetp, &target_point, &targetp->pos, 5, iff_get_color(IFF_COLOR_SELECTION, 1), NULL, 0);
3467  }
3468  }
3469  }
3470  }
3471 }
3472 
3473 // hud_show_targeting_gauges() will display the targeting information on the HUD. Called once per frame.
3474 //
3475 // Must be inside a g3_start_frame()
3476 // input: frametime => time in seconds since last update
3477 // in_cockpit => flag (default value 1) indicating whether viewpoint is from cockpit or external
3478 void hud_show_targeting_gauges(float frametime)
3479 {
3480  vertex target_point; // temp vertex used to find screen position for 3-D object;
3481  vec3d target_pos;
3482 
3484 
3485  if (Player_ai->target_objnum == -1)
3486  return;
3487 
3488  object * targetp = &Objects[Player_ai->target_objnum];
3490 
3491  // check to see if there is even a current target
3492  if ( targetp == &obj_used_list ) {
3493  return;
3494  }
3495 
3496  // AL 1/20/97: Point to targted subsystem if one exists
3497  if ( Player_ai->targeted_subsys != NULL ) {
3498  get_subsystem_world_pos(targetp, Player_ai->targeted_subsys, &target_pos);
3499  } else {
3500  target_pos = targetp->pos;
3501  }
3502 
3503  // find the current target vertex
3504  //
3505  // The 2D screen pos depends on the current viewer position and orientation.
3506  g3_rotate_vertex(&target_point,&target_pos);
3507 
3508  hud_set_iff_color( targetp, 1 );
3509  g3_project_vertex(&target_point);
3510 
3511  if (!(target_point.flags & PF_OVERFLOW)) { // make sure point projected
3512  if (target_point.codes == 0) { // target center is not on screen
3513  int target_display_flags;
3514 
3515  if(Cmdline_targetinfo) {
3517  } else {
3519  }
3520 
3521  hud_target_add_display_list(targetp, &target_point, &targetp->pos, 0, NULL, NULL, target_display_flags);
3522  }
3523  } else {
3524  Hud_target_w = 0;
3525  Hud_target_h = 0;
3526  }
3527 
3528  // update cargo scanning
3529  hud_cargo_scan_update(targetp, frametime);
3530 
3531  // display the lock indicator
3532  if (!Player->target_is_dying) {
3533  hud_do_lock_indicator(frametime);
3534 
3535  // update and render artillery
3538  }
3539 
3540  if (target_point.codes != 0) {
3541  // draw the offscreen indicator at the edge of the screen where the target is closest to
3543 
3544  // AL 11-11-97: don't draw the indicator if the ship is messaging, the indicator is drawn
3545  // in the message sending color in hud_show_message_sender()
3547  hud_target_add_display_list(targetp, &target_point, &targetp->pos, 0, NULL, NULL, 0);
3548  }
3549  }
3550 }
3551 
3552 // hud_show_hostile_triangle() will draw an empty triangle that oribits around the outer
3553 // circle of the reticle. It will point to the closest enemy that is firing on the player.
3554 // Currently, it points to the closest enemy that has the player as its target_objnum and has
3555 // SM_ATTACK or SM_SUPER_ATTACK as its ai submode.
3557 {
3558  object* A;
3559  float min_distance=1e20f;
3560  float new_distance=0.0f;
3561  object* nearest_obj = &obj_used_list;
3562  ai_info *aip;
3563  ship_obj *so;
3564  ship *sp;
3565  ship_subsys *ss;
3566 
3567  int player_obj_index = OBJ_INDEX(Player_obj);
3568  int turret_is_attacking = 0;
3569 
3570  hostile_obj = NULL;
3571 
3572  for ( so = GET_FIRST(&Ship_obj_list); so != END_OF_LIST(&Ship_obj_list); so = GET_NEXT(so) ) {
3573 
3574  A = &Objects[so->objnum];
3575  sp = &Ships[A->instance];
3576 
3577  // only look at ships who attack us
3578  if ( (A == Player_obj) || !(iff_x_attacks_y(Ships[A->instance].team, Player_ship->team)) ) {
3579  continue;
3580  }
3581 
3582  aip = &Ai_info[Ships[A->instance].ai_index];
3583 
3584  // don't look at ignore ships
3585  if ( sp->flags & TARGET_SHIP_IGNORE_FLAGS ) {
3586  continue;
3587  }
3588 
3589  // always ignore cargo containers and navbuoys
3590  if ( Ship_info[sp->ship_info_index].class_type > -1 && !(Ship_types[Ship_info[sp->ship_info_index].class_type].hud_bools & STI_HUD_SHOW_ATTACK_DIRECTION) ) {
3591  continue;
3592  }
3593 
3594  // check if ship is stealthy
3595  if (awacs_get_level(&Objects[sp->objnum], Player_ship, 1) < 1) {
3596  continue;
3597  }
3598 
3599  turret_is_attacking = 0;
3600 
3601  // check if any turrets on ship are firing at the player (only on non fighter-bombers)
3602  if ( !(Ship_info[sp->ship_info_index].flags & (SIF_FIGHTER|SIF_BOMBER)) ) {
3603  for (ss = GET_FIRST(&sp->subsys_list); ss != END_OF_LIST(&sp->subsys_list); ss = GET_NEXT(ss) ) {
3604  if (ss->flags & SSF_UNTARGETABLE)
3605  continue;
3606 
3607  if ( (ss->system_info->type == SUBSYSTEM_TURRET) && (ss->current_hits > 0) ) {
3608 
3609  if ( ss->turret_enemy_objnum == player_obj_index ) {
3610  turret_is_attacking = 1;
3611 
3612  vec3d gsubpos;
3613  // get world pos of subsystem
3614  vm_vec_unrotate(&gsubpos, &ss->system_info->pnt, &A->orient);
3615  vm_vec_add2(&gsubpos, &A->pos);
3616  new_distance = vm_vec_dist_quick(&gsubpos, &Player_obj->pos);
3617 
3618  if (new_distance <= min_distance) {
3619  min_distance=new_distance;
3620  nearest_obj = A;
3621  }
3622  }
3623  }
3624  }
3625  }
3626 
3627  if ( !turret_is_attacking ) {
3628  // check for ships attacking the player
3629  if ( aip->target_objnum != Player_ship->objnum ) {
3630  continue;
3631  }
3632 
3633  // ignore enemy if not in chase mode
3634  if ( (Game_mode & GM_NORMAL) && (aip->mode != AIM_CHASE) ) {
3635  continue;
3636  }
3637 
3638  new_distance = vm_vec_dist_quick(&A->pos, &Player_obj->pos);
3639 
3640  if (new_distance <= min_distance) {
3641  min_distance=new_distance;
3642  nearest_obj = A;
3643  }
3644  }
3645  }
3646 
3647  if ( nearest_obj == &obj_used_list ) {
3648  return;
3649  }
3650 
3651  if ( min_distance > MIN_DISTANCE_TO_CONSIDER_THREAT ) {
3652  return;
3653  }
3654 
3655  hostile_obj = nearest_obj;
3656 
3657  // hook to maybe warn player about this attacking ship
3658  ship_maybe_warn_player(&Ships[nearest_obj->instance], min_distance);
3659 }
3660 
3663 {
3664 }
3665 
3666 void HudGaugeHostileTriangle::render(float frametime)
3667 {
3668  if (hostile_obj && maybeFlashSexp() != 1) {
3669  bool in_frame = g3_in_frame() > 0;
3670  if(!in_frame)
3671  g3_start_frame(0);
3672 
3673  // hud_set_iff_color( TEAM_HOSTILE, 1 ); // Note: This should really be TEAM_HOSTILE, not opposite of Player_ship->team.
3674  hud_set_iff_color( hostile_obj, 1 );
3675  renderTriangle(&hostile_obj->pos, 0, 1, 0);
3676 
3677  if(!in_frame)
3678  g3_end_frame();
3679  }
3680 }
3681 
3682 void hud_calculate_lead_pos(vec3d *lead_target_pos, vec3d *target_pos, object *targetp, weapon_info *wip, float dist_to_target, vec3d *rel_pos)
3683 {
3684  vec3d target_moving_direction;
3685  vec3d last_delta_vector;
3686  float time_to_target, target_moved_dist;
3687 
3688  if(wip->max_speed != 0) {
3689  time_to_target = dist_to_target / wip->max_speed;
3690  } else {
3691  time_to_target = 0;
3692  }
3693 
3694  target_moved_dist = targetp->phys_info.speed * time_to_target;
3695 
3696  target_moving_direction = targetp->phys_info.vel;
3697 
3699  vm_vec_scale_sub2(&target_moving_direction, &Player_obj->phys_info.vel, wip->vel_inherit_amount);
3700 
3701  // test if the target is moving at all
3702  if ( vm_vec_mag_quick(&target_moving_direction) < 0.1f ) { // Find distance!
3703  *lead_target_pos = *target_pos;
3704  } else {
3705  vm_vec_normalize(&target_moving_direction);
3706  vm_vec_scale(&target_moving_direction, target_moved_dist);
3707  vm_vec_add(lead_target_pos, target_pos, &target_moving_direction );
3708  polish_predicted_target_pos(wip, targetp, target_pos, lead_target_pos, dist_to_target, &last_delta_vector, 1); // Not used:, float time_to_enemy)
3709 
3710  if(rel_pos) { // needed for quick lead indicators, not needed for normal lead indicators.
3711  vm_vec_add2(lead_target_pos, rel_pos);
3712  }
3713  }
3714 }
3715 
3716 // Return the bank number for the primary weapon that can fire the farthest, from
3717 // the number of active primary weapons
3718 // input: range => output parameter... it is the range of the selected bank
3720 {
3721  int i, best_bank, bank_to_fire, num_to_test;
3722  float weapon_range, farthest_weapon_range;
3723  ship_weapon *swp;
3724  weapon_info *wip;
3725 
3726  swp = &Player_ship->weapons;
3727 
3728  farthest_weapon_range = 0.0f;
3729  best_bank = -1;
3730 
3731  if ( Player_ship->flags & SF_PRIMARY_LINKED ) {
3732  num_to_test = swp->num_primary_banks;
3733  } else {
3734  num_to_test = MIN(1, swp->num_primary_banks);
3735  }
3736 
3737  for ( i = 0; i < num_to_test; i++ )
3738  {
3739  bank_to_fire = (swp->current_primary_bank + i) % swp->num_primary_banks;
3740 
3741  // calculate the range of the weapon, and only display the lead target indicator
3742  // if the weapon can actually hit the target
3743  Assert(bank_to_fire >= 0 && bank_to_fire < swp->num_primary_banks);
3744  Assert(swp->primary_bank_weapons[bank_to_fire] < MAX_WEAPON_TYPES);
3745 
3746  if (swp->primary_bank_weapons[bank_to_fire] < 0)
3747  continue;
3748 
3749  wip = &Weapon_info[swp->primary_bank_weapons[bank_to_fire]];
3750  weapon_range = MIN((wip->max_speed * wip->lifetime), wip->weapon_range);
3751 
3752  // don't consider this primary if it's a ballistic that's out of ammo - Goober5000
3753  if ( wip->wi_flags2 & WIF2_BALLISTIC )
3754  {
3755  if ( swp->primary_bank_ammo[bank_to_fire] <= 0)
3756  {
3757  continue;
3758  }
3759  }
3760 
3761  if ( weapon_range > farthest_weapon_range )
3762  {
3763  best_bank = bank_to_fire;
3764  farthest_weapon_range = weapon_range;
3765  }
3766  }
3767 
3768  *range = farthest_weapon_range;
3769  return best_bank;
3770 }
3771 
3772 // -----------------------------------------------------------------------------
3773 // polish_predicted_target_pos()
3774 //
3775 // Called by the draw lead indicator code to predict where the enemy is going to be
3776 //
3777 void polish_predicted_target_pos(weapon_info *wip, object *targetp, vec3d *enemy_pos, vec3d *predicted_enemy_pos, float dist_to_enemy, vec3d *last_delta_vec, int num_polish_steps)
3778 {
3779  int iteration;
3780  vec3d player_pos = Player_obj->pos;
3781  float time_to_enemy;
3782  vec3d last_predicted_enemy_pos = *predicted_enemy_pos;
3783 
3784  float weapon_speed = wip->max_speed;
3785 
3786  vm_vec_zero(last_delta_vec);
3787 
3788  // additive velocity stuff
3789  // not just the player's main target
3790  vec3d enemy_vel = targetp->phys_info.vel;
3793  }
3794 
3795  for (iteration=0; iteration < num_polish_steps; iteration++) {
3796  dist_to_enemy = vm_vec_dist_quick(predicted_enemy_pos, &player_pos);
3797  time_to_enemy = dist_to_enemy/weapon_speed;
3798  vm_vec_scale_add(predicted_enemy_pos, enemy_pos, &enemy_vel, time_to_enemy);
3799  vm_vec_sub(last_delta_vec, predicted_enemy_pos, &last_predicted_enemy_pos);
3800  last_predicted_enemy_pos= *predicted_enemy_pos;
3801  }
3802 }
3803 
3806 {
3807 
3808 }
3809 
3811 {
3812  Lead_indicator_half[0] = w;
3813  Lead_indicator_half[1] = h;
3814 }
3815 
3817 {
3818  Lead_indicator_gauge.first_frame = bm_load_animation(fname, &Lead_indicator_gauge.num_frames);
3819  if ( Lead_indicator_gauge.first_frame < 0 ) {
3820  Warning(LOCATION,"Cannot load hud ani: %s\n", fname);
3821  }
3822 }
3823 
3825 {
3826  bm_page_in_aabitmap(Lead_indicator_gauge.first_frame, Lead_indicator_gauge.num_frames);
3827 }
3828 
3829 // determine the correct frame to draw for the lead indicator
3830 // 0 -> center only (in secondary range only)
3831 // 1 -> full (in secondary and primary range)
3832 // 2 -> oustide only (in primary range only)
3833 //
3834 // input: prange => range of current primary weapon
3835 // srange => range of current secondary weapon
3836 // dist_to_target => current dist to target
3837 //
3838 // exit: 0-2 => frame offset
3839 // -1 => don't draw anything
3840 int HudGaugeLeadIndicator::pickFrame(float prange, float srange, float dist_to_target)
3841 {
3842  int frame_offset=-1;
3843  int in_prange=0, in_srange=0;
3844 
3845  if ( dist_to_target < prange ) {
3846  in_prange=1;
3847  }
3848 
3849  if ( dist_to_target < srange ) {
3850  in_srange=1;
3851  }
3852 
3853  if ( in_prange && in_srange ) {
3854  frame_offset=1;
3855  } else if ( in_prange && !in_srange ) {
3856  frame_offset=2;
3857  } else if ( !in_prange && in_srange ) {
3858  frame_offset=0;
3859  } else {
3860  frame_offset=-1;
3861  }
3862 
3863  return frame_offset;
3864 }
3865 
3866 void HudGaugeLeadIndicator::render(float frametime)
3867 {
3868  if(Player->target_is_dying) {
3869  return;
3870  }
3871 
3872  bool in_frame = g3_in_frame() > 0;
3873  if(!in_frame)
3874  g3_start_frame(0);
3875 
3876  // first render the current target the player has selected.
3878 
3879  // if extra targeting info is enabled, render lead indicators for objects in the target display list.
3880  for(size_t i = 0; i < target_display_list.size(); i++) {
3881  if ( (target_display_list[i].flags & TARGET_DISPLAY_LEAD) && target_display_list[i].objp ) {
3882 
3883  // set the color
3884  if( target_display_list[i].bracket_clr.red && target_display_list[i].bracket_clr.green &&
3885  target_display_list[i].bracket_clr.blue ) {
3886  gr_set_color_fast(&target_display_list[i].bracket_clr);
3887  } else {
3888  // use IFF colors if none defined.
3889  hud_set_iff_color(target_display_list[i].objp, 1);
3890  }
3891 
3892  renderLeadQuick(&target_display_list[i].target_pos, target_display_list[i].objp);
3893  }
3894  }
3895 
3896  if(!in_frame)
3897  g3_end_frame();
3898 }
3899 
3900 void HudGaugeLeadIndicator::renderIndicator(int frame_offset, object *targetp, vec3d *lead_target_pos)
3901 {
3902  vertex lead_target_vertex;
3903  int sx, sy;
3904 
3905  g3_rotate_vertex(&lead_target_vertex, lead_target_pos);
3906 
3907  if (lead_target_vertex.codes == 0) { // on screen
3908  g3_project_vertex(&lead_target_vertex);
3909 
3910  if (!(lead_target_vertex.flags & PF_OVERFLOW)) {
3911  if ( maybeFlashSexp() == 1 ) {
3912  hud_set_iff_color(targetp, 0);
3913  } else {
3914  hud_set_iff_color(targetp, 1);
3915  }
3916 
3917  if ( Lead_indicator_gauge.first_frame + frame_offset >= 0 ) {
3918  sx = fl2i(lead_target_vertex.screen.xyw.x);
3919  sy = fl2i(lead_target_vertex.screen.xyw.y);
3920 
3921  unsize(&sx, &sy);
3922  renderBitmap(Lead_indicator_gauge.first_frame + frame_offset, fl2i(sx - Lead_indicator_half[0]), fl2i(sy - Lead_indicator_half[1]));
3923  }
3924  }
3925  }
3926 }
3927 
3928 // HudGaugeLeadIndicator::renderTargetLead() determine where to draw the lead target box and display it
3930 {
3931  vec3d target_pos;
3932  vec3d source_pos;
3933  vec3d *rel_pos;
3934  vec3d lead_target_pos;
3935  object *targetp;
3936  polymodel *pm;
3937  ship_weapon *swp;
3938  weapon_info *wip;
3939  weapon_info *tmp=NULL;
3940  float dist_to_target, prange, srange;
3941  int bank_to_fire, frame_offset;
3942 
3943  if (Player_ai->target_objnum == -1)
3944  return;
3945 
3946  targetp = &Objects[Player_ai->target_objnum];
3947  if ( (targetp->type != OBJ_SHIP) && (targetp->type != OBJ_WEAPON) && (targetp->type != OBJ_ASTEROID) ) {
3948  return;
3949  }
3950 
3951  // only allow bombs to have lead indicator displayed
3952  if ( targetp->type == OBJ_WEAPON ) {
3955  return;
3956  }
3957  }
3958  }
3959 
3960  // If the target is out of range, then draw the correct frame for the lead indicator
3961  if ( Lead_indicator_gauge.first_frame == -1 ) {
3962  Int3();
3963  return;
3964  }
3965 
3966  // AL 1/20/97: Point to targted subsystem if one exists
3967  if ( Player_ai->targeted_subsys != NULL ) {
3968  get_subsystem_world_pos(targetp, Player_ai->targeted_subsys, &target_pos);
3969  } else {
3970  target_pos = targetp->pos;
3971  }
3972 
3973  pm = model_get(Ship_info[Player_ship->ship_info_index].model_num);
3974  swp = &Player_ship->weapons;
3975 
3976  // Added to take care of situation where there are no primary banks on the player ship
3977  // (this may not be possible, depending on what we decide for the weapons loadout rules)
3978  if ( swp->num_primary_banks == 0 )
3979  return;
3980 
3981  bank_to_fire = hud_get_best_primary_bank(&prange);
3982 
3983  if ( bank_to_fire < 0 )
3984  return;
3985 
3986  wip = &Weapon_info[swp->primary_bank_weapons[bank_to_fire]];
3987 
3988  if (pm->n_guns && bank_to_fire != -1 ) {
3989  rel_pos = &pm->gun_banks[bank_to_fire].pnt[0];
3990  } else {
3991  rel_pos = NULL;
3992  }
3993 
3994  // source_pos will contain the world coordinate of where to base the lead indicator prediction
3995  // from. Normally, this will be the world pos of the gun turret of the currently selected primary
3996  // weapon.
3997  source_pos = Player_obj->pos;
3998  if (rel_pos != NULL) {
3999  vec3d gun_point;
4000  vm_vec_unrotate(&gun_point, rel_pos, &Player_obj->orient);
4001  vm_vec_add2(&source_pos, &gun_point);
4002  }
4003 
4004  // Determine "accurate" distance to target.
4005  // This is the distance from the player ship to:
4006  // (if targeting a subsystem) the distance to the subsystem centre
4007  // (playing it safe, will usually be in range at slightly further away due to subsys radius)
4008  // (otherwise) the closest point on the bounding box of the target
4009  if ( Player_ai->targeted_subsys != NULL ) {
4010  dist_to_target = vm_vec_dist(&target_pos, &Player_obj->pos);
4011  } else {
4012  dist_to_target = hud_find_target_distance(targetp, Player_obj);
4013  }
4014 
4016 
4017  if ( (swp->current_secondary_bank >= 0) && (swp->secondary_bank_weapons[swp->current_secondary_bank] >= 0) )
4018  {
4019  int bank = swp->current_secondary_bank;
4020  tmp = &Weapon_info[swp->secondary_bank_weapons[bank]];
4021  if ( !(tmp->wi_flags & WIF_HOMING) && !(tmp->wi_flags & WIF_LOCKED_HOMING && Player->target_in_lock_cone) ) {
4022  //The secondary lead indicator is handled farther below if it is a non-locking type
4023  srange = -1.0f;
4024  }
4025  }
4026 
4027  frame_offset = pickFrame(prange, srange, dist_to_target);
4028  if ( frame_offset < 0 ) {
4029  return;
4030  }
4031 
4032  hud_calculate_lead_pos(&lead_target_pos, &target_pos, targetp, wip, dist_to_target);
4033  renderIndicator(frame_offset, targetp, &lead_target_pos);
4034 
4035  //do dumbfire lead indicator - color is orange (255,128,0) - bright, (192,96,0) - dim
4036  //phreak changed 9/01/02
4037  if((swp->current_secondary_bank>=0) && (swp->secondary_bank_weapons[swp->current_secondary_bank] >= 0)) {
4038  int bank=swp->current_secondary_bank;
4039  wip=&Weapon_info[swp->secondary_bank_weapons[bank]];
4040 
4041  //get out of here if the secondary weapon is a homer or if its out of range
4042  if ( wip->wi_flags & WIF_HOMING )
4043  return;
4044 
4045  double max_dist = MIN((wip->lifetime * wip->max_speed), wip->weapon_range);
4046 
4047  if (dist_to_target > max_dist)
4048  return;
4049  }
4050 
4051  hud_calculate_lead_pos(&lead_target_pos, &target_pos, targetp, wip, dist_to_target);
4052  renderIndicator(0, targetp, &lead_target_pos);
4053 }
4054 
4055 //Backslash
4056 // A stripped-down version of the lead indicator, only shows primary weapons
4057 // and works for a specified target (not just the current selected target).
4058 // Ideally I'd like to later turn this into something (or make a new function) that would actually WORK with gun convergence/normals
4059 // instead of the existing code (copied from above) that does some calculations and then is ignored ;-)
4060 // (Go look, what's it actually DO with source_pos?)
4061 // And also, something that could be called for multiple weapons, ITTS style.
4062 void HudGaugeLeadIndicator::renderLeadQuick(vec3d *target_world_pos, object *targetp)
4063 {
4064  vec3d source_pos;
4065  vec3d *rel_pos;
4066  vec3d lead_target_pos;
4067  polymodel *pm;
4068  ship_weapon *swp;
4069  weapon_info *wip;
4070  float dist_to_target, prange;
4071  int bank_to_fire, frame_offset;
4072 
4073  if ( (targetp->type != OBJ_SHIP) && (targetp->type != OBJ_WEAPON) && (targetp->type != OBJ_ASTEROID) ) {
4074  return;
4075  }
4076 
4077  // only allow bombs to have lead indicator displayed
4078  if ( targetp->type == OBJ_WEAPON ) {
4081  return;
4082  }
4083  }
4084  }
4085 
4086  // If the target is out of range, then draw the correct frame for the lead indicator
4087  if ( Lead_indicator_gauge.first_frame == -1 ) {
4088  Int3();
4089  return;
4090  }
4091 
4092  pm = model_get(Ship_info[Player_ship->ship_info_index].model_num);
4093  swp = &Player_ship->weapons;
4094 
4095  // Added to take care of situation where there are no primary banks on the player ship
4096  // (this may not be possible, depending on what we decide for the weapons loadout rules)
4097  if ( swp->num_primary_banks == 0 )
4098  return;
4099 
4100  bank_to_fire = hud_get_best_primary_bank(&prange); //Backslash note: this!
4101  if ( bank_to_fire < 0 )
4102  return;
4103  wip = &Weapon_info[swp->primary_bank_weapons[bank_to_fire]];
4104 
4105  if (pm->n_guns && bank_to_fire != -1 ) {
4106  rel_pos = &pm->gun_banks[bank_to_fire].pnt[0];
4107  } else {
4108  rel_pos = NULL;
4109  }
4110 // vec3d firing_vec;
4111 // vm_vec_unrotate(&firing_vec, &po->gun_banks[bank_to_fire].norm[pt], &obj->orient);
4112 // vm_vector_2_matrix(&firing_orient, &firing_vec, NULL, NULL);
4113 
4114  // source_pos will contain the world coordinate of where to base the lead indicator prediction
4115  // from. Normally, this will be the world pos of the gun turret of the currently selected primary
4116  // weapon.
4117  source_pos = Player_obj->pos;
4118  if (rel_pos != NULL) {
4119  vec3d gun_point;
4120  vm_vec_unrotate(&gun_point, rel_pos, &Player_obj->orient);
4121  vm_vec_add2(&source_pos, &gun_point);
4122  }
4123 
4124  // Determine "accurate" distance to target. This is the distance from the player ship
4125  // to the closest point on the bounding box of the target
4126  dist_to_target = hud_find_target_distance(targetp, Player_obj);
4127 
4128  frame_offset = pickFrame(prange, -1.0f, dist_to_target);
4129  if ( frame_offset < 0 ) {
4130  return;
4131  }
4132 
4133  hud_calculate_lead_pos(&lead_target_pos, target_world_pos, targetp, wip, dist_to_target, rel_pos);
4134  renderIndicator(frame_offset, targetp, &lead_target_pos);
4135 }
4136 
4139 {
4140 }
4141 
4143 {
4145 
4146  if ( Lead_sight.first_frame < 0 ) {
4147  Warning(LOCATION,"Cannot load hud ani: %s\n", fname);
4148  } else {
4149  int w, h;
4150 
4152  Lead_sight_half[0] = fl2i(w * 0.5f);
4153  Lead_sight_half[1] = fl2i(h * 0.5f);
4154  }
4155 }
4156 
4157 void HudGaugeLeadSight::renderSight(int frame_offset, vec3d *target_pos, vec3d *lead_target_pos)
4158 {
4159  vertex target_vertex;
4160  float target_sx;
4161  float target_sy;
4162 
4163  vertex lead_target_vertex;
4164  float target_lead_sx;
4165  float target_lead_sy;
4166 
4167  // first see if the lead is on screen
4168  g3_rotate_vertex(&lead_target_vertex, lead_target_pos);
4169 
4170  if (lead_target_vertex.codes != 0)
4171  return;
4172 
4173  g3_project_vertex(&lead_target_vertex);
4174 
4175  if (lead_target_vertex.flags & PF_OVERFLOW)
4176  return;
4177 
4178  target_lead_sx = lead_target_vertex.screen.xyw.x;
4179  target_lead_sy = lead_target_vertex.screen.xyw.y;
4180 
4181  // now see if the target is on screen
4182  g3_rotate_vertex(&target_vertex, target_pos);
4183 
4184  if (target_vertex.codes != 0)
4185  return;
4186 
4187  g3_project_vertex(&target_vertex);
4188 
4189  if (target_vertex.flags & PF_OVERFLOW)
4190  return;
4191 
4192  target_sx = target_vertex.screen.xyw.x;
4193  target_sy = target_vertex.screen.xyw.y;
4194 
4195  // render the lead sight
4196  if ( Lead_sight.first_frame >= 0 ) {
4197 
4198  unsize(&target_lead_sx, &target_lead_sy);
4199  unsize(&target_sx, &target_sy);
4200 
4201  float reticle_target_sx = target_sx - Lead_sight_half[0] - target_lead_sx;
4202  float reticle_target_sy = target_sy - Lead_sight_half[1] - target_lead_sy;
4203 
4204  reticle_target_sx += position[0] + 0.5f;
4205  reticle_target_sy += position[1] + 0.5f;
4206 
4207  setGaugeColor();
4208  renderBitmap(Lead_sight.first_frame + frame_offset, fl2i(reticle_target_sx) + fl2i(HUD_offset_x), fl2i(reticle_target_sy) + fl2i(HUD_offset_y));
4209  }
4210 }
4211 
4213 {
4215 }
4216 
4217 void HudGaugeLeadSight::render(float frametime)
4218 {
4219  vec3d target_pos;
4220  vec3d source_pos;
4221  vec3d *rel_pos;
4222  vec3d lead_target_pos;
4223  object *targetp;
4224  polymodel *pm;
4225  ship_weapon *swp;
4226  weapon_info *wip;
4227  float dist_to_target, prange;
4228  int bank_to_fire;
4229 
4230  if (Player_ai->target_objnum == -1)
4231  return;
4232 
4233  targetp = &Objects[Player_ai->target_objnum];
4234  if ( (targetp->type != OBJ_SHIP) && (targetp->type != OBJ_WEAPON) && (targetp->type != OBJ_ASTEROID) ) {
4235  return;
4236  }
4237 
4238  // only allow bombs to have lead indicator displayed
4239  if ( targetp->type == OBJ_WEAPON ) {
4241  return;
4242  }
4243  }
4244 
4245  // If the target is out of range, then draw the correct frame for the lead indicator
4246  if ( Lead_sight.first_frame == -1 ) {
4247  Int3();
4248  return;
4249  }
4250 
4251  // AL 1/20/97: Point to targeted subsystem if one exists
4252  if ( Player_ai->targeted_subsys != NULL ) {
4253  get_subsystem_world_pos(targetp, Player_ai->targeted_subsys, &target_pos);
4254  } else {
4255  target_pos = targetp->pos;
4256  }
4257 
4258  pm = model_get(Ship_info[Player_ship->ship_info_index].model_num);
4259  swp = &Player_ship->weapons;
4260 
4261  // Added to take care of situation where there are no primary banks on the player ship
4262  // (this may not be possible, depending on what we decide for the weapons loadout rules)
4263  if ( swp->num_primary_banks == 0 )
4264  return;
4265 
4266  bank_to_fire = hud_get_best_primary_bank(&prange);
4267  if ( bank_to_fire < 0 )
4268  return;
4269  wip = &Weapon_info[swp->primary_bank_weapons[bank_to_fire]];
4270 
4271  if (pm->n_guns && bank_to_fire != -1 ) {
4272  rel_pos = &pm->gun_banks[bank_to_fire].pnt[0];
4273  } else {
4274  rel_pos = NULL;
4275  }
4276 
4277  // source_pos will contain the world coordinate of where to base the lead indicator prediction
4278  // from. Normally, this will be the world pos of the gun turret of the currently selected primary
4279  // weapon.
4280  source_pos = Player_obj->pos;
4281  if (rel_pos != NULL) {
4282  vec3d gun_point;
4283  vm_vec_unrotate(&gun_point, rel_pos, &Player_obj->orient);
4284  vm_vec_add2(&source_pos, &gun_point);
4285  }
4286 
4287  // Determine "accurate" distance to target. This is the distance from the player ship
4288  // to the closest point on the bounding box of the target
4289  dist_to_target = hud_find_target_distance(targetp, Player_obj);
4290 
4291  bool in_frame;
4292  if ( dist_to_target < prange ) {
4293  // fire it up
4294  in_frame = g3_in_frame() > 0;
4295  if(!in_frame) {
4296  g3_start_frame(0);
4297  }
4298 
4299  hud_calculate_lead_pos(&lead_target_pos, &target_pos, targetp, wip, dist_to_target);
4300  renderSight(1, &target_pos, &lead_target_pos); // render the primary weapon lead sight
4301 
4302  if(!in_frame) {
4303  g3_end_frame();
4304  }
4305  }
4306 
4307  //do dumbfire lead indicator - color is orange (255,128,0) - bright, (192,96,0) - dim
4308  //phreak changed 9/01/02
4309  if((swp->current_secondary_bank>=0) && (swp->secondary_bank_weapons[swp->current_secondary_bank] >= 0))
4310  {
4311  int bank=swp->current_secondary_bank;
4312  wip=&Weapon_info[swp->secondary_bank_weapons[bank]];
4313 
4314  //get out of here if the secondary weapon is a homer or if its out of range
4315  if ( wip->wi_flags & WIF_HOMING ) {
4316  return;
4317  }
4318 
4319  double max_dist = MIN((wip->lifetime * wip->max_speed), wip->weapon_range);
4320 
4321  if (dist_to_target > max_dist) {
4322  return;
4323  }
4324  } else {
4325  return;
4326  }
4327 
4328  // fire it up
4329  in_frame = g3_in_frame() > 0;
4330  if(!in_frame) {
4331  g3_start_frame(0);
4332  }
4333 
4334  //give it the "in secondary range frame
4335 
4336  hud_calculate_lead_pos(&lead_target_pos, &target_pos, targetp, wip, dist_to_target);
4337  renderSight(0, &target_pos, &lead_target_pos); // now render the secondary weapon lead sight
4338 
4339  if(!in_frame) {
4340  g3_end_frame();
4341  }
4342 }
4343 
4344 // hud_cease_subsystem_targeting() will cease targeting the current targets subsystems
4345 //
4346 void hud_cease_subsystem_targeting(int print_message)
4347 {
4348  int ship_index;
4349 
4350  ship_index = Objects[Player_ai->target_objnum].instance;
4351  if ( ship_index < 0 )
4352  return;
4353 
4354  Ships[ship_index].last_targeted_subobject[Player_num] = NULL;
4355  Player_ai->targeted_subsys = NULL;
4357  if ( print_message ) {
4358  HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Deactivating sub-system targeting", 324));
4359  }
4360 
4362  hud_lock_reset();
4363 }
4364 
4365 // hud_cease_targeting() will cease all targeting (main target and subsystem)
4366 //
4368 {
4372  HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Deactivating targeting system", 325));
4373  hud_lock_reset();
4374 }
4375 
4376 // hud_restore_subsystem_target() will remember the last targeted subsystem
4377 // on a target.
4378 //
4380 {
4381  // check if there was a previously targeted sub-system for this target
4382  if ( shipp->last_targeted_subobject[Player_num] != NULL ) {
4385  }
4386  else {
4387  Player_ai->targeted_subsys = NULL;
4389  }
4390 }
4391 
4392 // --------------------------------------------------------------------------------
4393 // get_subsystem_world_pos() returns the world position for a given subsystem on a ship
4394 //
4395 vec3d* get_subsystem_world_pos(object* parent_obj, ship_subsys* subsys, vec3d* world_pos)
4396 {
4397  get_subsystem_pos(world_pos, parent_obj, subsys);
4398 
4399  return world_pos;
4400 }
4401 
4402 // ----------------------------------------------------------------------------
4403 // hud_target_change_check()
4404 //
4405 // called once per frame to account for when the target changes
4406 //
4408 {
4409  float current_speed=0.0f;
4410 
4411  // Check if player subsystem target has changed, and reset necessary player flag
4413  Player->subsys_in_view=-1;
4414  }
4415 
4416  // check if the main target has changed
4418 
4419  if ( Player_ai->target_objnum != -1){
4421  }
4422 
4423  // if we have a hotkey set active, see if new target is in set. If not in
4424  // set, deselect the current hotkey set.
4425  if ( Player->current_hotkey_set != -1 ) {
4426  htarget_list *hitem, *plist;
4427 
4429  for ( hitem = GET_FIRST(plist); hitem != END_OF_LIST(plist); hitem = GET_NEXT(hitem) ) {
4430  if ( OBJ_INDEX(hitem->objp) == Player_ai->target_objnum ){
4431  break;
4432  }
4433  }
4434  if ( hitem == END_OF_LIST(plist) ){
4435  Player->current_hotkey_set = -1;
4436  }
4437  }
4438 
4440  if ( (Player_ai->target_objnum >= 0) && (Player_ai->target_objnum < MAX_OBJECTS) ) {
4442  }
4447  Player->locking_subsys=NULL;
4450 
4453 
4457  }
4458  else {
4460  Players[Player_num].flags &= ~PLAYER_FLAGS_MATCH_TARGET; // no more target matching.
4461  }
4462 
4463  hud_lock_reset();
4464 
4465  if ( (Player_ai->target_objnum >= 0) && (Player_ai->target_objnum < MAX_OBJECTS) ) {
4468  }
4469  }
4470  }
4471  else {
4476  } else {
4478  }
4479 
4480  if ( (Player_ai->target_objnum >= 0) && (Player_ai->target_objnum < MAX_OBJECTS) ) {
4481  current_speed = Objects[Player_ai->target_objnum].phys_info.speed;
4482  }
4483 
4484  if (current_speed < Player_ai->last_speed-0.01){
4486  } else if (current_speed > Player_ai->last_speed+0.01) {
4488  } else {
4490  }
4491 
4495  }
4496  }
4497  }
4498 
4500  Player_ai->last_speed = current_speed;
4501 
4504 }
4505 
4508 {
4509 }
4510 
4511 void HudGaugeTargetTriangle::render(float frametime)
4512 {
4513  if ( Player_ai->target_objnum == -1)
4514  return;
4515 
4516  bool in_frame = g3_in_frame() > 0;
4517  if(!in_frame)
4518  g3_start_frame(0);
4519 
4520  object *targetp = &Objects[Player_ai->target_objnum];
4521 
4522  // draw the targeting triangle that orbits the outside of the outer circle of the reticle
4523  if (!Player->target_is_dying && maybeFlashSexp() != 1) {
4524 
4525  hud_set_iff_color(targetp, 1);
4526  renderTriangle(&targetp->pos, 1, 0, 0);
4527  }
4528 
4529  if(!in_frame)
4530  g3_end_frame();
4531 }
4532 
4533 // start the weapon line (on the HUD) flashing
4535 {
4536  if ( index >= MAX_WEAPON_FLASH_LINES ) {
4537  Int3(); // Get Alan
4538  return;
4539  }
4540 
4541  if ( timestamp_elapsed(Weapon_flash_info.flash_duration[index]) ) {
4542  Weapon_flash_info.flash_next[index] = timestamp(TBOX_FLASH_INTERVAL);
4543  Weapon_flash_info.is_bright &= ~(1<<index);
4544  }
4545 
4546  Weapon_flash_info.flash_duration[index] = timestamp(TBOX_FLASH_DURATION);
4547 }
4548 
4549 // maybe change the text color for the weapon line indicated by index
4551 {
4552  if ( index >= MAX_WEAPON_FLASH_LINES ) {
4553  Int3(); // Get Alan
4554  return;
4555  }
4556 
4557  // hud_set_default_color();
4559  if ( !timestamp_elapsed(Weapon_flash_info.flash_duration[index]) ) {
4560  if ( timestamp_elapsed(Weapon_flash_info.flash_next[index]) ) {
4561  Weapon_flash_info.flash_next[index] = timestamp(TBOX_FLASH_INTERVAL);
4562  Weapon_flash_info.is_bright ^= (1<<index);
4563  }
4564 
4565  if ( Weapon_flash_info.is_bright & (1<<index) ) {
4567  // hud_set_bright_color();
4568  } else {
4570  // hud_set_dim_color();
4571  }
4572  }
4573 }
4574 
4578 int hud_sensors_ok(ship *sp, int show_msg)
4579 {
4580  float sensors_str;
4581 
4582  // If playing on lowest skill level, sensors don't affect targeting
4583  // If dead, still allow player to target, despite any subsystem damage
4584  // If i'm a multiplayer observer, allow me to target
4586  return 1;
4587  }
4588 
4589  // if the ship is currently being affected by EMP
4590  if(emp_active_local()){
4591  return 0;
4592  }
4593 
4594  // ensure targeting functions are not disabled through damage
4595  sensors_str = ship_get_subsystem_strength( sp, SUBSYSTEM_SENSORS );
4596  if ( (sensors_str < MIN_SENSOR_STR_TO_TARGET) || (ship_subsys_disrupted(sp, SUBSYSTEM_SENSORS)) ) {
4597  if ( show_msg ) {
4598  HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Targeting is disabled due to sensors damage", 330));
4600  }
4601  return 0;
4602  } else {
4603  return 1;
4604  }
4605 }
4606 
4608 {
4609  float str;
4610 
4611  // If playing on the lowest skill level, communications always ok
4612  // If dead, still allow player to communicate, despite any subsystem damage
4613  if ( Game_skill_level == 0 || (Game_mode & GM_DEAD) ) {
4614  return COMM_OK;
4615  }
4616 
4617  // Goober5000 - if the ship is the player, and he's dying, return OK (so laments can be played)
4618  if ((sp == Player_ship) && (sp->flags & SF_DYING))
4619  return COMM_OK;
4620 
4621  // Goober5000 - check for scrambled communications
4623  return COMM_SCRAMBLED;
4624 
4626 
4627  if ( (str <= 0.01) || ship_subsys_disrupted(sp, SUBSYSTEM_COMMUNICATION) ) {
4628  return COMM_DESTROYED;
4629  } else if ( str < MIN_COMM_STR_TO_MESSAGE ) {
4630  return COMM_DAMAGED;
4631  }
4632 
4633  return COMM_OK;
4634 }
4635 
4636 // target the next or previous hostile/friendly ship
4637 void hud_target_next_list(int hostile, int next_flag, int team_mask, int attacked_objnum, int play_fail_snd, int filter, int get_closest_turret_attacking_player)
4638 {
4639  int timestamp_val, valid_team_mask;
4640 
4641  if ( hostile ) {
4642  timestamp_val = Tl_hostile_reset_timestamp;
4643  Tl_hostile_reset_timestamp = timestamp(TL_RESET);
4644  if ( team_mask == -1 ) {
4645  valid_team_mask = iff_get_attackee_mask(Player_ship->team);
4646  } else {
4647  valid_team_mask = team_mask;
4648  }
4649  } else {
4650  // everyone hates a traitor including other traitors so the friendly target option shouldn't work for them
4651  if (Player_ship->team == Iff_traitor) {
4652  snd_play( &Snds[SND_TARGET_FAIL], 0.0f );
4653  return;
4654  }
4655 
4656  timestamp_val = Tl_friendly_reset_timestamp;
4657  Tl_friendly_reset_timestamp = timestamp(TL_RESET);
4658  valid_team_mask = iff_get_mask(Player_ship->team);
4659  }
4660 
4661  // If no target is selected, then simply target the closest ship
4662  if ( Player_ai->target_objnum == -1 || timestamp_elapsed(timestamp_val) ) {
4663  hud_target_closest(valid_team_mask, attacked_objnum, play_fail_snd, filter, get_closest_turret_attacking_player);
4664  return;
4665  }
4666 
4667  object *nearest_object = select_next_target_by_distance((next_flag != 0), valid_team_mask, attacked_objnum);
4668 
4669  if (nearest_object != NULL) {
4670  // set new target
4671  set_target_objnum( Player_ai, OBJ_INDEX(nearest_object) );
4672  hud_shield_hit_reset(nearest_object);
4673 
4674  // maybe set new turret subsystem
4676  hud_restore_subsystem_target(&Ships[nearest_object->instance]);
4677  }
4678  else {
4679  snd_play( &Snds[SND_TARGET_FAIL], 0.0f );
4680  }
4681 }
4682 
4685 {
4686 }
4687 
4689 {
4690  Auto_text_offsets[0] = x;
4691  Auto_text_offsets[1] = y;
4692 }
4693 
4695 {
4696  Target_text_offsets[0] = x;
4697  Target_text_offsets[1] = y;
4698 }
4699 
4701 {
4703  if ( Toggle_frame.first_frame < 0 ) {
4704  Warning(LOCATION,"Cannot load hud ani: %s\n", fname);
4705  }
4706 }
4707 
4708 void HudGaugeAutoTarget::initOnColor(int r, int g, int b, int a)
4709 {
4710  if ( r == -1 || g == -1 || b == -1 || a == -1 ) {
4711  Use_on_color = false;
4712  gr_init_alphacolor(&On_color, 0, 0, 0, 0);
4713  return;
4714  }
4715 
4716  Use_on_color = true;
4717  gr_init_alphacolor(&On_color, r, g, b, a);
4718 }
4719 
4720 void HudGaugeAutoTarget::initOffColor(int r, int g, int b, int a)
4721 {
4722  if ( r == -1 || g == -1 || b == -1 || a == -1 ) {
4723  Use_off_color = false;
4724  gr_init_alphacolor(&Off_color, 0, 0, 0, 0);
4725  return;
4726  }
4727 
4728  Use_off_color = true;
4729  gr_init_alphacolor(&Off_color, r, g, b, a);
4730 }
4731 
4732 void HudGaugeAutoTarget::render(float frametime)
4733 {
4735  return;
4736 
4737  int frame_offset;
4738 
4740  frame_offset = 1;
4741  } else {
4742  frame_offset = 0;
4743  }
4744 
4745  // draw the box background
4746  setGaugeColor();
4747  renderBitmap(Toggle_frame.first_frame+frame_offset, position[0], position[1]);
4748 
4749  // draw the text on top
4750  if (frame_offset == 1) {
4751  //static color text_color;
4752  //gr_init_alphacolor(&text_color, 0, 0, 0, Toggle_text_alpha);
4753  if ( Use_on_color ) {
4755  }
4756  } else if ( Use_off_color ) {
4758  }
4759 
4760  renderString(position[0] + Auto_text_offsets[0], position[1] + Auto_text_offsets[1], XSTR("auto", 1463));
4761  renderString(position[0] + Target_text_offsets[0], position[1] + Target_text_offsets[1], XSTR("target", 1465));
4762 }
4763 
4765 {
4767 }
4768 
4771 {
4772 }
4773 
4775 {
4776  Auto_text_offsets[0] = x;
4777  Auto_text_offsets[1] = y;
4778 }
4779 
4781 {
4782  Speed_text_offsets[0] = x;
4783  Speed_text_offsets[1] = y;
4784 }
4785 
4787 {
4789  if ( Toggle_frame.first_frame < 0 ) {
4790  Warning(LOCATION,"Cannot load hud ani: %s\n", fname);
4791  }
4792 }
4793 
4794 void HudGaugeAutoSpeed::initOnColor(int r, int g, int b, int a)
4795 {
4796  if ( r == -1 || g == -1 || b == -1 || a == -1 ) {
4797  Use_on_color = false;
4798  gr_init_alphacolor(&On_color, 0, 0, 0, 0);
4799  return;
4800  }
4801 
4802  Use_on_color = true;
4803  gr_init_alphacolor(&On_color, r, g, b, a);
4804 }
4805 
4806 void HudGaugeAutoSpeed::initOffColor(int r, int g, int b, int a)
4807 {
4808  if ( r == -1 || g == -1 || b == -1 || a == -1 ) {
4809  Use_off_color = false;
4810  gr_init_alphacolor(&Off_color, 0, 0, 0, 0);
4811  return;
4812  }
4813 
4814  Use_off_color = true;
4815  gr_init_alphacolor(&Off_color, r, g, b, a);
4816 }
4817 
4818 void HudGaugeAutoSpeed::render(float frametime)
4819 {
4821  return;
4822 
4823  int frame_offset;
4824 
4826  frame_offset = 3;
4827  } else {
4828  frame_offset = 2;
4829  }
4830 
4831  setGaugeColor();
4832 
4833  renderBitmap(Toggle_frame.first_frame+frame_offset, position[0], position[1]);
4834 
4835  // draw the text on top
4836  if (frame_offset == 3) {
4837  //static color text_color;
4838  //gr_init_alphacolor(&text_color, 0, 0, 0, Toggle_text_alpha);
4839  if ( Use_on_color ) {
4841  }
4842  } else if ( Use_off_color ) {
4844  }
4845  renderString(position[0] + Auto_text_offsets[0], position[1] + Auto_text_offsets[1], XSTR("auto", 1463));
4846  renderString(position[0] + Speed_text_offsets[0], position[1] + Speed_text_offsets[1], XSTR("speed", 1464));
4847 }
4848 
4850 {
4852 }
4853 
4854 // Set the player target to the closest friendly repair ship
4855 // input: goal_objnum => Try to find repair ship where aip->goal_objnum matches this
4856 // output: 1 => A repair ship was targeted
4857 // 0 => No targeting change
4859 {
4860  object *A;
4861  object *nearest_obj=&obj_used_list;
4862  ship *shipp;
4863  ship_obj *so;
4864  float min_distance=1e20f;
4865  float new_distance=0.0f;
4866  int rval=0;
4867 
4868  for ( so = GET_FIRST(&Ship_obj_list); so != END_OF_LIST(&Ship_obj_list); so = GET_NEXT(so) ) {
4869  A = &Objects[so->objnum];
4870  shipp = &Ships[A->instance]; // get a pointer to the ship information
4871 
4872  // ignore all ships that aren't repair ships
4873  if ( !(Ship_info[shipp->ship_info_index].flags & SIF_SUPPORT) ) {
4874  continue;
4875  }
4876 
4877  if ( (A == Player_obj) || (shipp->flags & TARGET_SHIP_IGNORE_FLAGS) )
4878  continue;
4879 
4880  // only consider friendly ships
4881  if ( !(Player_ship->team == shipp->team)) {
4882  continue;
4883  }
4884 
4885  if(hud_target_invalid_awacs(A)){
4886  continue;
4887  }
4888 
4889  if ( goal_objnum >= 0 ) {
4890  if ( Ai_info[shipp->ai_index].goal_objnum != goal_objnum ) {
4891  continue;
4892  }
4893  }
4894 
4895  new_distance = hud_find_target_distance(A,Player_obj);
4896 
4897  if (new_distance <= min_distance) {
4898  min_distance=new_distance;
4899  nearest_obj = A;
4900  }
4901  }
4902 
4903  if (nearest_obj != &obj_used_list) {
4904  set_target_objnum( Player_ai, OBJ_INDEX(nearest_obj) );
4905  hud_shield_hit_reset(nearest_obj);
4907  rval=1;
4908  }
4909  else {
4910  // inform player how to get a support ship
4911  if ( goal_objnum == -1 ) {
4912  HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "No support ships in area. Use messaging to call one in.", 332));
4913  }
4914  rval=0;
4915  }
4916 
4917  return rval;
4918 }
4919 
4921 {
4923  TARGET_SHIP_IGNORE_FLAGS &= ~SF_HIDDEN_FROM_SENSORS;
4924  HUD_sourced_printf(HUD_SOURCE_HIDDEN, NOX("Target hiding from sensors disabled"));
4925  } else {
4927  HUD_sourced_printf(HUD_SOURCE_HIDDEN, NOX("Target hiding from sensors enabled"));
4928  }
4929 }
4930 
4931 // target the closest uninspected object
4933 {
4934  object *A, *nearest_obj = NULL;
4935  ship *shipp;
4936  ship_obj *so;
4937  float min_distance = 1e20f;
4938  float new_distance = 0.0f;
4939 
4940  for ( so = GET_FIRST(&Ship_obj_list); so != END_OF_LIST(&Ship_obj_list); so = GET_NEXT(so) ) {
4941 
4942  A = &Objects[so->objnum];
4943  shipp = &Ships[A->instance]; // get a pointer to the ship information
4944 
4945  if ( (A == Player_obj) || (shipp->flags & TARGET_SHIP_IGNORE_FLAGS) ){
4946  continue;
4947  }
4948 
4949  if(hud_target_invalid_awacs(A)){
4950  continue;
4951  }
4952 
4953  // ignore all non-cargo carrying craft
4954  if ( !hud_target_ship_can_be_scanned(shipp) ) {
4955  continue;
4956  }
4957 
4958  new_distance = hud_find_target_distance(A,Player_obj);
4959 
4960  if (new_distance <= min_distance) {
4961  min_distance=new_distance;
4962  nearest_obj = A;
4963  }
4964  }
4965 
4966  if (nearest_obj != NULL) {
4967  set_target_objnum( Player_ai, OBJ_INDEX(nearest_obj) );
4968  hud_shield_hit_reset(nearest_obj);
4970  }
4971  else {
4973  }
4974 }
4975 
4976 // target the next or previous uninspected/unscanned object
4978 {
4979  object *A, *min_obj, *max_obj, *nearest_obj;
4980  ship *shipp;
4981  ship_obj *so;
4982  float cur_dist, min_dist, max_dist, new_dist, nearest_dist, diff;
4983 
4984  // If no target is selected, then simply target the closest uninspected cargo
4985  if ( Player_ai->target_objnum == -1 || timestamp_elapsed(Target_next_uninspected_object_timestamp) ) {
4986  Target_next_uninspected_object_timestamp = timestamp(TL_RESET);
4988  return;
4989  }
4990 
4991  Target_next_uninspected_object_timestamp = timestamp(TL_RESET);
4992 
4994 
4995  min_obj = max_obj = nearest_obj = NULL;
4996  min_dist = 1e20f;
4997  max_dist = 0.0f;
4998  if ( next_flag ) {
4999  nearest_dist = 1e20f;
5000  } else {
5001  nearest_dist = 0.0f;
5002  }
5003 
5004  for ( so = GET_FIRST(&Ship_obj_list); so != END_OF_LIST(&Ship_obj_list); so = GET_NEXT(so) ) {
5005  A = &Objects[so->objnum];
5006  shipp = &Ships[A->instance]; // get a pointer to the ship information
5007 
5008  if ( (A == Player_obj) || (shipp->flags & TARGET_SHIP_IGNORE_FLAGS) )
5009  continue;
5010 
5011  // ignore all non-cargo carrying craft
5012  if ( !hud_target_ship_can_be_scanned(shipp) ) {
5013  continue;
5014  }
5015 
5016  // don't use object if it is already a target
5017  if ( OBJ_INDEX(A) == Player_ai->target_objnum ) {
5018  continue;
5019  }
5020 
5021  if(hud_target_invalid_awacs(A)){
5022  continue;
5023  }
5024 
5025  new_dist = hud_find_target_distance(A, Player_obj);
5026 
5027  if (new_dist <= min_dist) {
5028  min_dist = new_dist;
5029  min_obj = A;
5030  }
5031 
5032  if (new_dist >= max_dist) {
5033  max_dist = new_dist;
5034  max_obj = A;
5035  }
5036 
5037  if ( next_flag ) {
5038  diff = new_dist - cur_dist;
5039  if ( diff > 0 ) {
5040  if ( diff < ( nearest_dist - cur_dist ) ) {
5041  nearest_dist = new_dist;
5042  nearest_obj = A;
5043  }
5044  }
5045  } else {
5046  diff = cur_dist - new_dist;
5047  if ( diff > 0 ) {
5048  if ( diff < ( cur_dist - nearest_dist ) ) {
5049  nearest_dist = new_dist;
5050  nearest_obj = A;
5051  }
5052  }
5053  }
5054  }
5055 
5056  if ( nearest_obj == NULL ) {
5057 
5058  if ( next_flag ) {
5059  if ( min_obj != NULL ) {
5060  nearest_obj = min_obj;
5061  }
5062  } else {
5063  if ( max_obj != NULL ) {
5064  nearest_obj = max_obj;
5065  }
5066  }
5067  }
5068 
5069  if (nearest_obj != NULL) {
5070  set_target_objnum( Player_ai, OBJ_INDEX(nearest_obj) );
5071  hud_shield_hit_reset(nearest_obj);
5073  }
5074  else {
5076  }
5077 }
5078 
5079 // ----------------------------------------------------------------
5080 //
5081 // Target Last Transmission Sender code START
5082 //
5083 // ----------------------------------------------------------------
5084 
5085 typedef struct transmit_target
5086 {
5087  int objnum;
5088  int objsig;
5089 } transmit_target;
5090 
5091 static int Transmit_target_next_slot = 0;
5092 static int Transmit_target_current_slot = -1;
5093 static int Transmit_target_reset_timer = timestamp(0);
5094 
5095 #define MAX_TRANSMIT_TARGETS 10
5096 static transmit_target Transmit_target_list[MAX_TRANSMIT_TARGETS];
5097 
5098 // called once per level to initialize the target last transmission sender list
5100 {
5101  int i;
5102 
5103  for ( i = 0; i < MAX_TRANSMIT_TARGETS; i++ ) {
5104  Transmit_target_list[i].objnum = -1;
5105  Transmit_target_list[i].objsig = -1;
5106  }
5107 
5108  Transmit_target_next_slot = 0;
5109  Transmit_target_current_slot = 0;
5110  Transmit_target_reset_timer = timestamp(0);
5111 }
5112 
5113 // internal function only.. used to find index for last recorded ship transmission
5115 {
5116  int latest_slot;
5117 
5118  latest_slot = Transmit_target_next_slot - 1;
5119  if ( latest_slot < 0 ) {
5120  latest_slot = MAX_TRANSMIT_TARGETS - 1;
5121  }
5122 
5123  return latest_slot;
5124 }
5125 
5126 // called externally to set the player target to the last ship which sent a transmission to the player
5128 {
5129  int i;
5130 
5131  if ( Transmit_target_current_slot < 0 ) {
5132  Transmit_target_current_slot = hud_target_last_transmit_newest();
5133  }
5134 
5135  // If timed out, then simply target the last ship to transmit
5136  if ( timestamp_elapsed(Transmit_target_reset_timer) ) {
5137  Transmit_target_current_slot = hud_target_last_transmit_newest();
5138  }
5139 
5140  Transmit_target_reset_timer = timestamp(TL_RESET);
5141 
5142  int play_fail_sound = 1;
5143  int transmit_index = Transmit_target_current_slot;
5144  Assert(transmit_index >= 0);
5145  for ( i = 0; i < MAX_TRANSMIT_TARGETS; i++ ) {
5146  if ( Transmit_target_list[transmit_index].objnum >= 0 ) {
5147  int transmit_objnum = Transmit_target_list[transmit_index].objnum;
5148 
5149  if ( Player_ai->target_objnum == transmit_objnum ) {
5150  play_fail_sound = 0;
5151  } else {
5152  if ( Transmit_target_list[transmit_index].objsig == Objects[Transmit_target_list[transmit_index].objnum].signature ) {
5153  if ( !(Ships[Objects[transmit_objnum].instance].flags & TARGET_SHIP_IGNORE_FLAGS) ) {
5154  Transmit_target_current_slot = transmit_index-1;
5155  if ( Transmit_target_current_slot < 0 ) {
5156  Transmit_target_current_slot = MAX_TRANSMIT_TARGETS - 1;
5157  }
5158  break;
5159  }
5160  }
5161  }
5162  }
5163 
5164  transmit_index--;
5165  if ( transmit_index < 0 ) {
5166  transmit_index = MAX_TRANSMIT_TARGETS - 1;
5167  }
5168  }
5169 
5170  if ( i == MAX_TRANSMIT_TARGETS ) {
5171  if ( play_fail_sound ) {
5173  }
5174  Transmit_target_current_slot = -1;
5175  return;
5176  }
5177 
5178  if(hud_target_invalid_awacs(&Objects[Transmit_target_list[transmit_index].objnum])){
5179  return;
5180  }
5181 
5182  // target new ship!
5183  // Fix bug in targeting due to Alt-Y (target last ship sending transmission).
5184  // Was just bogus code in the call to hud_restore_subsystem_target(). -- MK, 9/15/99, 1:59 pm.
5185  int targeted_objnum;
5186  targeted_objnum = Transmit_target_list[transmit_index].objnum;
5187  Assert((targeted_objnum >= 0) && (targeted_objnum < MAX_OBJECTS));
5188 
5189  if ((targeted_objnum >= 0) && (targeted_objnum < MAX_OBJECTS)) {
5190  set_target_objnum( Player_ai, Transmit_target_list[transmit_index].objnum );
5191  hud_shield_hit_reset(&Objects[Transmit_target_list[transmit_index].objnum]);
5192  hud_restore_subsystem_target(&Ships[Objects[Transmit_target_list[transmit_index].objnum].instance]);
5193  }
5194 }
5195 
5196 // called externally to add a message sender to the list
5198 {
5199  object *ship_objp;
5200  int ship_objnum;
5201 
5202  ship_objnum = Ships[ship_num].objnum;
5203  Assert(ship_objnum >= 0 && ship_objnum < MAX_OBJECTS);
5204  ship_objp = &Objects[ship_objnum];
5205  Assert(ship_objp->type == OBJ_SHIP);
5206 
5207  // don't add ourselves to the list
5208  if (Player_obj == ship_objp) {
5209  return;
5210  }
5211 
5212  Transmit_target_list[Transmit_target_next_slot].objnum = ship_objnum;
5213  Transmit_target_list[Transmit_target_next_slot].objsig = ship_objp->signature;
5214  Transmit_target_next_slot++;
5215  if ( Transmit_target_next_slot >= MAX_TRANSMIT_TARGETS ) {
5216  Transmit_target_next_slot = 0;
5217  }
5218 }
5219 
5220 // target a random ship (useful for EMP stuff)
5222 {
5223  int shipnum;
5224  int objnum;
5225 
5226  shipnum = ship_get_random_targetable_ship();
5227  if((shipnum < 0) || (Ships[shipnum].objnum < 0)){
5228  return;
5229  }
5230  objnum = Ships[shipnum].objnum;
5231 
5232  if((objnum >= 0) && (Player_ai != NULL) && !hud_target_invalid_awacs(&Objects[objnum])){
5233  // never target yourself
5234  if(objnum == OBJ_INDEX(Player_obj)){
5236  } else {
5237  set_target_objnum(Player_ai, objnum);
5238  hud_shield_hit_reset(&Objects[objnum]);
5239  }
5240  }
5241 }
5242 
5243 // ----------------------------------------------------------------
5244 //
5245 // Target Last Transmission Sender code END
5246 //
5247 // ----------------------------------------------------------------
5248 
5250 {
5251  int i;
5252 
5253  for ( i = 0; i < NUM_WEAPON_GAUGES; i++ ) {
5254  bm_page_in_aabitmap( Weapon_gauges[ballistic_hud_index][i].first_frame, Weapon_gauges[ballistic_hud_index][i].num_frames);