-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathhudlock.cpp
More file actions
2227 lines (1776 loc) · 68.1 KB
/
Copy pathhudlock.cpp
File metadata and controls
2227 lines (1776 loc) · 68.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
*/
#include "ai/ai.h"
#include "debugconsole/console.h"
#include "gamesequence/gamesequence.h"
#include "gamesnd/gamesnd.h"
#include "globalincs/linklist.h"
#include "hud/hudlock.h"
#include "iff_defs/iff_defs.h"
#include "io/timer.h"
#include "mission/missionparse.h"
#include "network/multi.h"
#include "object/object.h"
#include "playerman/player.h"
#include "render/3d.h"
#include "ship/ship.h"
#include "weapon/emp.h"
#include "weapon/weapon.h"
// Used for aspect locks. -MageKing17
#define VIRTUAL_FRAME_HALF_WIDTH 320.0f
#define VIRTUAL_FRAME_HALF_HEIGHT 240.0f
vec3d lock_world_pos;
static float Lock_start_dist;
sound_handle Missile_track_loop = sound_handle::invalid();
sound_handle Missile_lock_loop = sound_handle::invalid();
int Lock_target_box_width[GR_NUM_RESOLUTIONS] = {
19,
30
};
int Lock_target_box_height[GR_NUM_RESOLUTIONS] = {
19,
30
};
// the locked triangles (that orbit lock indicator) dimensions
float Lock_triangle_base[GR_NUM_RESOLUTIONS] = {
4.0f,
6.5f
};
float Lock_triangle_height[GR_NUM_RESOLUTIONS] = {
4.0f,
6.5f
};
int Lock_gauge_half_w[NUM_HUD_RETICLE_STYLES][GR_NUM_RESOLUTIONS] = {
{ 15, 24 },
{ 17, 28 }
};
int Lock_gauge_half_h[GR_NUM_RESOLUTIONS] = {
15,
25
};
#define LOCK_GAUGE_BLINK_RATE 5 // blinks/sec
int Lockspin_half_w[NUM_HUD_RETICLE_STYLES][GR_NUM_RESOLUTIONS] = {
{ 16, 26 },
{ 31, 50 }
};
int Lockspin_half_h[NUM_HUD_RETICLE_STYLES][GR_NUM_RESOLUTIONS] = {
{ 16, 26 },
{ 32, 52 }
};
char Lock_fname[NUM_HUD_RETICLE_STYLES][GR_NUM_RESOLUTIONS][MAX_FILENAME_LEN] =
{
{ "lock1_fs1", "2_lock1_fs1" },
{ "lock1", "2_lock1" }
};
char Lockspin_fname[NUM_HUD_RETICLE_STYLES][GR_NUM_RESOLUTIONS][MAX_FILENAME_LEN] =
{
{ "lockspin_fs1", "2_lockspin_fs1" },
{ "lockspin", "2_lockspin" }
};
void hud_lock_determine_lock_point(vec3d *lock_world_pos_out);
void hud_lock_determine_lock_point(lock_info *current_lock);
// hud_init_missile_lock() is called at the beginning of a mission
//
void hud_init_missile_lock()
{
Players[Player_num].lock_indicator_start_x = -1;
Players[Player_num].lock_indicator_start_y = -1;
Players[Player_num].lock_indicator_visible = 0;
Player_ai->current_target_is_locked = 0;
Player_ai->last_secondary_index = -1;
}
HudGaugeLock::HudGaugeLock():
HudGauge3DAnchor(HUD_OBJECT_LOCK, HUD_LEAD_INDICATOR, false, false, VM_DEAD_VIEW, 255, 255, 255)
{
}
void HudGaugeLock::initGaugeHalfSize(int w, int h)
{
Lock_gauge_half_w = w;
Lock_gauge_half_h = h;
}
void HudGaugeLock::initSpinHalfSize(int w, int h)
{
Lockspin_half_w = w;
Lockspin_half_h = h;
}
void HudGaugeLock::initTriHeight(float h)
{
Lock_triangle_height = h;
}
void HudGaugeLock::initTriBase(float length)
{
Lock_triangle_base = length;
}
void HudGaugeLock::initTargetBoxSize(int w, int h)
{
Lock_target_box_width = w;
Lock_target_box_height = h;
}
void HudGaugeLock::initLoopLockedAnim(bool loop)
{
loop_locked_anim = loop;
}
void HudGaugeLock::initBlinkLockedAnim(bool blink)
{
blink_locked_anim = blink;
}
void HudGaugeLock::initBitmaps(char *lock_gauge_fname, char *lock_anim_fname)
{
hud_anim_init(&Lock_gauge, 0, 0, lock_gauge_fname);
hud_anim_load(&Lock_gauge);
hud_anim_init(&Lock_anim, 0, 0, lock_anim_fname);
hud_anim_load(&Lock_anim);
}
void HudGaugeLock::initialize()
{
Lock_gauge_draw_stamp = -1;
Lock_gauge_draw = 0;
Rotate_time_id = 1;
Last_lock_status = false;
Lock_anim.time_elapsed = 0.0f;
Lock_gauge.time_elapsed = 0.0f;
HudGauge::initialize();
}
// hud_show_lock_indicator() will display the lock indicator for homing missiles.
// lock_point_pos should be the world coordinates of the target being locked. Assuming all the
// necessary locking calculations are done for this frame, this function will compute
// where the indicator should be relative to the player's viewpoint and will render accordingly.
void HudGaugeLock::renderOld(float frametime)
{
int target_objnum, sx, sy;
object *targetp;
vertex lock_point;
bool locked = Player_ai->current_target_is_locked ? true : false;
bool reset_timers = false;
if ( locked != Last_lock_status ) {
// check if player lock status has changed since the last frame.
reset_timers = true;
Last_lock_status = locked;
}
if (Player_ai->target_objnum == -1) {
return;
}
// 1 is the only value for which your target is actually dying
if (Player->target_is_dying == 1) {
return;
}
if (!Players[Player_num].lock_indicator_visible){
return;
}
target_objnum = Player_ai->target_objnum;
Assert(target_objnum != -1);
targetp = &Objects[target_objnum];
// check to see if there are any missile to fire.. we don't want to show the
// lock indicator if there are missiles to fire.
if ( !ship_secondary_bank_has_ammo(Player_obj->instance) ) {
return;
}
bool in_frame = g3_in_frame() > 0;
if(!in_frame)
g3_start_frame(0);
gr_set_screen_scale(base_w, base_h);
// Get the target's current position on the screen. If he's not on there,
// we're not going to draw the lock indicator even if he's in front
// of our ship, so bail out.
g3_rotate_vertex(&lock_point, &lock_world_pos);
g3_project_vertex(&lock_point);
if (lock_point.codes & PF_OVERFLOW) {
gr_reset_screen_scale();
if(!in_frame)
g3_end_frame();
return;
}
hud_set_iff_color(targetp);
// nprintf(("Alan","lockx: %d, locky: %d TargetX: %d, TargetY: %d\n", Players[Player_num].lock_indicator_x, Players[Player_num].lock_indicator_y, Player->current_target_sx, Player->current_target_sy));
// We have the coordinates of the lock indicator relative to the target in our "virtual frame"
// so, we calculate where it should be drawn based on the player's viewpoint.
if (Player_ai->current_target_is_locked) {
sx = fl2i(lock_point.screen.xyw.x);
sy = fl2i(lock_point.screen.xyw.y);
gr_unsize_screen_pos(&sx, &sy);
// show the rotating triangles if target is locked
renderLockTriangles(sx, sy, frametime);
if ( reset_timers ) {
Lock_gauge.time_elapsed = 0.0f;
}
} else {
const float scaling_factor = (gr_screen.clip_center_x < gr_screen.clip_center_y) ? (gr_screen.clip_center_x / VIRTUAL_FRAME_HALF_WIDTH) : (gr_screen.clip_center_y / VIRTUAL_FRAME_HALF_HEIGHT);
sx = fl2i(lock_point.screen.xyw.x) - fl2i(i2fl(Player->current_target_sx - Players[Player_num].lock_indicator_x) * scaling_factor);
sy = fl2i(lock_point.screen.xyw.y) - fl2i(i2fl(Player->current_target_sy - Players[Player_num].lock_indicator_y) * scaling_factor);
gr_unsize_screen_pos(&sx, &sy);
if ( reset_timers ) {
Lock_gauge_draw_stamp = -1;
Lock_gauge_draw = 0;
Lock_anim.time_elapsed = 0.0f;
}
}
// show locked indicator
Lock_gauge.sx = sx - Lock_gauge_half_w;
Lock_gauge.sy = sy - Lock_gauge_half_h;
if (Player_ai->current_target_is_locked) {
hud_anim_render(&Lock_gauge, 0.0f, 1);
} else {
hud_anim_render(&Lock_gauge, frametime, 1);
}
gr_reset_screen_scale();
if(!in_frame)
g3_end_frame();
}
// hud_show_lock_indicator() will display the lock indicator for homing missiles.
// lock_point_pos should be the world coordinates of the target being locked. Assuming all the
// necessary locking calculations are done for this frame, this function will compute
// where the indicator should be relative to the player's viewpoint and will render accordingly.
void HudGaugeLock::render(float frametime)
{
size_t i;
lock_info *current_lock;
vertex lock_point;
int sx, sy;
// check to see if there are any missile to fire.. we don't want to show the
// lock indicator if there are missiles to fire.
if ( !ship_secondary_bank_has_ammo(Player_obj->instance) ) {
return;
}
bool in_frame = g3_in_frame() > 0;
if(!in_frame)
g3_start_frame(0);
gr_set_screen_scale(base_w, base_h);
// go through all present lock indicators
for ( i = 0; i < Player_ship->missile_locks.size(); ++i ) {
current_lock = &Player_ship->missile_locks[i];
if ( !current_lock->indicator_visible ) {
continue;
}
// Get the target's current position on the screen. If he's not on there,
// we're not going to draw the lock indicator even if he's in front
// of our ship, so bail out.
g3_rotate_vertex(&lock_point, ¤t_lock->world_pos);
g3_project_vertex(&lock_point);
if (lock_point.codes & PF_OVERFLOW) {
continue;
}
hud_set_iff_color(current_lock->obj);
// We have the coordinates of the lock indicator relative to the target in our "virtual frame"
// so, we calculate where it should be drawn based on the player's viewpoint.
if ( current_lock->locked ) {
sx = fl2i(lock_point.screen.xyw.x);
sy = fl2i(lock_point.screen.xyw.y);
gr_unsize_screen_pos(&sx, &sy);
// show the rotating triangles if target is locked
renderLockTrianglesNew(sx, sy, frametime, current_lock);
} else {
const float scaling_factor = (gr_screen.clip_center_x < gr_screen.clip_center_y)
? (gr_screen.clip_center_x / VIRTUAL_FRAME_HALF_WIDTH)
: (gr_screen.clip_center_y / VIRTUAL_FRAME_HALF_HEIGHT);
sx = fl2i(lock_point.screen.xyw.x) - fl2i(i2fl(current_lock->current_target_sx - current_lock->indicator_x) * scaling_factor);
sy = fl2i(lock_point.screen.xyw.y) - fl2i(i2fl(current_lock->current_target_sy - current_lock->indicator_y) * scaling_factor);
gr_unsize_screen_pos(&sx, &sy);
}
Lock_gauge.sx = sx - Lock_gauge_half_w;
Lock_gauge.sy = sy - Lock_gauge_half_h;
if( current_lock->locked ){
current_lock->lock_gauge_time_elapsed = 0.0f;
Lock_gauge.time_elapsed = current_lock->lock_gauge_time_elapsed;
hud_anim_render(&Lock_gauge, 0.0f, 1);
} else {
// manually track the animation time, since we may have more than one lock
current_lock->lock_gauge_time_elapsed += frametime;
if (current_lock->lock_gauge_time_elapsed > Lock_gauge.total_time) {
current_lock->lock_gauge_time_elapsed = 0.0f;
}
Lock_gauge.time_elapsed = current_lock->lock_gauge_time_elapsed;
hud_anim_render(&Lock_gauge, 0.0f, 1);
}
}
gr_reset_screen_scale();
if(!in_frame)
g3_end_frame();
}
// Reset data used for player lock indicator
void hud_lock_reset(float lock_time_scale)
{
weapon_info *wip;
ship_weapon *swp;
swp = &Player_ship->weapons;
if ((swp->current_secondary_bank >= 0) && (swp->secondary_bank_weapons[swp->current_secondary_bank] >= 0)) {
Assert(swp->current_secondary_bank < MAX_SHIP_SECONDARY_BANKS);
Assert(swp->secondary_bank_weapons[swp->current_secondary_bank] < weapon_info_size());
wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
Player->lock_time_to_target = i2fl(wip->min_lock_time*lock_time_scale);
} else {
Player->lock_time_to_target = 0.0f;
}
Player_ai->current_target_is_locked = 0;
Players[Player_num].lock_indicator_visible = 0;
Player->target_in_lock_cone = 0;
Player->current_target_sx = -1;
Player->current_target_sy = -1;
Player->locking_subsys=NULL;
Player->locking_on_center=0;
Player->locking_subsys_parent=-1;
hud_stop_looped_locking_sounds();
// reset the lock anim time elapsed
// Lock_anim.time_elapsed = 0.0f;
for ( auto & missile_locks : Player_ship->missile_locks) {
ship_clear_lock(&missile_locks);
}
}
// Determine if the locking code has a point to track
int hud_lock_has_homing_point()
{
if ( Player_ai->targeted_subsys || Player->locking_subsys || Player->locking_on_center ) {
return 1;
}
return 0;
}
int Nebula_sec_range = 0;
DCF_BOOL(nebula_sec_range, Nebula_sec_range);
// Determine if point to lock on is in range
int hud_lock_target_in_range()
{
vec3d target_world_pos;
object *targetp;
if ( !hud_lock_has_homing_point() ) {
return 0;
}
targetp = &Objects[Player_ai->target_objnum];
if ( Player_ai->targeted_subsys != NULL ) {
vm_vec_unrotate(&target_world_pos, &Player_ai->targeted_subsys->system_info->pnt, &targetp->orient);
vm_vec_add2(&target_world_pos, &targetp->pos);
} else {
if ( Player->locking_subsys ) {
vm_vec_unrotate(&target_world_pos, &Player->locking_subsys->system_info->pnt, &targetp->orient);
vm_vec_add2(&target_world_pos, &targetp->pos);
} else {
Assert(Player->locking_on_center);
target_world_pos = targetp->pos;
}
}
ship_weapon* swp = &Player_ship->weapons;
weapon_info* wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
return weapon_secondary_world_pos_in_range(Player_obj, wip, &target_world_pos);
}
int hud_abort_lock()
{
int target_team;
weapon_info *wip;
ship_weapon *swp;
if ( Player_ship->weapons.num_secondary_banks <= 0 ) {
return 1;
}
if ( Player_ship->weapons.current_secondary_bank < 0 ) {
return 1;
}
// check to see if there are any missile to fire.. we don't want to show the
// lock indicator if there are no missiles to fire.
if ( !ship_secondary_bank_has_ammo(Player_obj->instance) ) {
return 1;
}
if ( Player_ship->flags[Ship::Ship_Flags::No_secondary_lockon] ) {
return 1;
}
swp = &Player_ship->weapons;
wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
// if we're on the same team and the team doesn't attack itself, then don't lock!
if ( (Player_ai->target_objnum >= 0) ) {
target_team = obj_team(&Objects[Player_ai->target_objnum]);
if ( ( Player_ship->team == target_team) && ( !iff_x_attacks_y(Player_ship->team, target_team) )
&& !weapon_has_iff_restrictions(wip)) {
// if we're in multiplayer dogfight, ignore this
// remember to check if we're firing a missile that doesn't require a current target
if(!MULTI_DOGFIGHT || wip->target_restrict == LR_ANY_TARGETS) {
return 1;
}
}
}
// Reset locks on launch if this weapon allows it and if we've recently fired.
if ( wip->launch_reset_locks && !timestamp_elapsed(swp->next_secondary_fire_stamp[swp->current_secondary_bank]) ) {
return 1;
}
return 0;
}
// determine if the subsystem to lock on to has a direct line of sight
int hud_lock_on_subsys_ok()
{
ship_subsys *subsys;
vec3d subobj_pos;
object *target_objp;
int in_sight=0;
Assert(Player_ai->target_objnum >= 0);
target_objp = &Objects[Player_ai->target_objnum];
subsys = Player_ai->targeted_subsys;
if ( !subsys ) {
return 0;
}
vm_vec_unrotate(&subobj_pos, &subsys->system_info->pnt, &target_objp->orient);
vm_vec_add2(&subobj_pos, &target_objp->pos);
if ( Player->subsys_in_view < 0 ) {
in_sight = ship_subsystem_in_sight(target_objp, subsys, &View_position, &subobj_pos) ? 1 : 0;
} else {
in_sight = Player->subsys_in_view;
}
return in_sight;
}
// Determine if locking point is in the locking cone
void hud_lock_check_if_target_in_lock_cone()
{
float dot;
vec3d vec_to_target;
vm_vec_normalized_dir(&vec_to_target, &lock_world_pos, &Player_obj->pos);
dot = vm_vec_dot(&Player_obj->orient.vec.fvec, &vec_to_target);
if ( dot > 0.85) {
Player->target_in_lock_cone = 1;
} else {
Player->target_in_lock_cone = 0;
}
}
void hud_lock_check_if_target_in_lock_cone(lock_info *current_lock, weapon_info *wip)
{
float dot;
vec3d vec_to_target;
vm_vec_normalized_dir(&vec_to_target, ¤t_lock->world_pos, &Player_obj->pos);
dot = vm_vec_dot(&Player_obj->orient.vec.fvec, &vec_to_target);
current_lock->target_in_lock_cone = dot > wip->lock_fov;
}
// return 1 if current secondary weapon is different than previous secondary weapon
int hud_lock_secondary_weapon_changed(ship_weapon *swp)
{
if ( swp->current_secondary_bank != Player_ai->last_secondary_index ) {
return 1;
}
return 0;
/*
int last_wi_index = -1;
int current_wi_index = -1;
// do a quick out if same bank is selected
if ( swp->current_secondary_bank == Player_ai->last_secondary_index ) {
return 0;
}
// bank has changed, but it still may be the same weapon type
if ( swp->current_secondary_bank >= 0 ) {
current_wi_index = swp->secondary_bank_weapons[swp->current_secondary_bank];
}
if ( Player_ai->last_secondary_index >= 0 ) {
last_wi_index = swp->secondary_bank_weapons[Player_ai->last_secondary_index];
}
if ( current_wi_index != last_wi_index ) {
return 1;
}
return 0;
*/
}
// hud_do_lock_indicator() manages missle locking, both the non-rendering calculations and the 2D HUD rendering
void hud_do_lock_indicator(float frametime)
{
ship_weapon *swp;
weapon_info *wip;
// if i'm a multiplayer observer, bail here
if((Game_mode & GM_MULTIPLAYER) && ((Net_player->flags & NETINFO_FLAG_OBSERVER) || (Player_obj->type == OBJ_OBSERVER)) ){
return;
}
Assert(Player_ai->target_objnum >= 0);
// be sure to unset this flag, then possibly set later in this function so that
// threat indicators work properly.
Player_ai->ai_flags.remove(AI::AI_Flags::Seek_lock);
if ( hud_abort_lock() ) {
hud_lock_reset();
return;
}
// if there is an EMP effect active, never update lock
if(emp_active_local()){
hud_lock_reset();
return;
}
swp = &Player_ship->weapons;
wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
Lock_start_dist = wip->min_lock_time * wip->lock_pixels_per_sec;
// if secondary weapons change, reset the lock
if ( hud_lock_secondary_weapon_changed(swp) ) {
hud_lock_reset();
}
Player_ai->last_secondary_index = swp->current_secondary_bank;
object *tobjp = &Objects[Player_ai->target_objnum];
vec3d dir_to_target;
vm_vec_normalized_dir(&dir_to_target, &tobjp->pos, &Player_obj->pos);
if ( !(wip->is_locked_homing()) ) {
hud_lock_reset();
return;
}
// Allow locking on ships and bombs (only targeted weapon allowed is a bomb, so don't bother checking flags)
if ( (Objects[Player_ai->target_objnum].type != OBJ_SHIP) && (Objects[Player_ai->target_objnum].type != OBJ_WEAPON) ) {
hud_lock_reset();
return;
}
// Javelins must lock on engines if locking on a ship and those must be in sight
if (wip->wi_flags[Weapon::Info_Flags::Homing_javelin] &&
tobjp->type == OBJ_SHIP &&
Player->locking_subsys != NULL) {
vec3d subobj_pos;
vm_vec_unrotate(&subobj_pos, &Player->locking_subsys->system_info->pnt, &tobjp->orient);
vm_vec_add2(&subobj_pos, &tobjp->pos);
bool target_subsys_in_sight = ship_subsystem_in_sight(tobjp, Player->locking_subsys, &Player_obj->pos, &subobj_pos);
if (!target_subsys_in_sight || Player->locking_subsys->system_info->type != SUBSYSTEM_ENGINE) {
Player->locking_subsys =
ship_get_closest_subsys_in_sight(&Ships[tobjp->instance], SUBSYSTEM_ENGINE, &Player_obj->pos);
}
}
if (wip->wi_flags[Weapon::Info_Flags::Homing_javelin] &&
tobjp->type == OBJ_SHIP &&
Player->locking_subsys == NULL) {
Player->locking_subsys =
ship_get_closest_subsys_in_sight(&Ships[tobjp->instance], SUBSYSTEM_ENGINE, &Player_obj->pos);
if (Player->locking_subsys == NULL) {
hud_lock_reset();
return;
}
}
hud_lock_determine_lock_point(&lock_world_pos);
if ( !hud_lock_has_homing_point() ) {
Player->target_in_lock_cone=0;
}
hud_lock_check_if_target_in_lock_cone();
// check if the target is within range of the current secondary weapon. If it is not,
// a lock will not be detected
if ( !hud_lock_target_in_range() ) {
Player->target_in_lock_cone = 0;
}
// If locking on a subsystem, and not in sight... can't lock
// Changed by MK on 4/3/98. It was confusing me that my hornets would not lock on my target.
// It will now be confusing that they lock, but don't home on your subsystem, but I think that's preferable.
// Often you really care about destroying the target, not just the subsystem.
/*if ( Player_ai->targeted_subsys ) {
if ( !hud_lock_on_subsys_ok() ) {
Player->target_in_lock_cone=0;
}
}*/
if ( !Player->target_in_lock_cone ) {
Player->locking_on_center=0;
Player->locking_subsys_parent=-1;
Player->locking_subsys=NULL;
}
hud_calculate_lock_position(frametime);
if (!Players[Player_num].lock_indicator_visible)
return;
if (Player_ai->current_target_is_locked) {
if (Missile_track_loop.isValid()) {
snd_stop(Missile_track_loop);
Missile_track_loop = sound_handle::invalid();
if (wip->hud_locked_snd.isValid())
{
Missile_lock_loop = snd_play(gamesnd_get_game_sound(wip->hud_locked_snd));
}
else
{
Missile_lock_loop = snd_play(gamesnd_get_game_sound(ship_get_sound(Player_obj, GameSounds::MISSILE_LOCK)));
}
}
}
else {
Player_ai->ai_flags.set(AI::AI_Flags::Seek_lock); // set this flag so multiplayer's properly track lock on other ships
if (Missile_lock_loop.isValid() && snd_is_playing(Missile_lock_loop)) {
snd_stop(Missile_lock_loop);
Missile_lock_loop = sound_handle::invalid();
}
}
}
void hud_lock_acquire_current_target(object *target_objp, ship_subsys **target_subsys)
{
ship *target_shipp=nullptr;
int lock_in_range=0;
float best_lock_dot=-1.0f, lock_dot=-1.0f;
ship_subsys *ss;
vec3d subsys_world_pos, vec_to_lock;
if ( target_objp->type == OBJ_SHIP ) {
target_shipp = &Ships[target_objp->instance];
}
ship_weapon* swp = &Player_ship->weapons;
weapon_info* wip = &Weapon_info[swp->secondary_bank_weapons[swp->current_secondary_bank]];
// Reset target subsys in case it isn't needed
if (*target_subsys != nullptr) *target_subsys = nullptr;
// if a large ship, lock to pos closest to center and within range
if ( (target_shipp) && (Ship_info[target_shipp->ship_info_index].is_big_or_huge()) ) {
// check all the subsystems and the center of the ship
// assume best lock pos is the center of the ship
lock_in_range = weapon_secondary_world_pos_in_range(Player_obj, wip, &target_objp->pos);
vm_vec_normalized_dir(&vec_to_lock, &target_objp->pos, &Player_obj->pos);
if ( lock_in_range ) {
best_lock_dot=vm_vec_dot(&Player_obj->orient.vec.fvec, &vec_to_lock);
}
// take center if reasonable dot
if ( best_lock_dot > 0.95 ) {
return;
}
// iterate through subsystems to see if we can get a better choice
ss = GET_FIRST(&target_shipp->subsys_list);
while ( ss != END_OF_LIST( &target_shipp->subsys_list ) ) {
// get world pos of subsystem
get_subsystem_world_pos(target_objp, ss, &subsys_world_pos);
if ( weapon_secondary_world_pos_in_range(Player_obj, wip, &subsys_world_pos) ) {
vm_vec_normalized_dir(&vec_to_lock, &subsys_world_pos, &Player_obj->pos);
lock_dot=vm_vec_dot(&Player_obj->orient.vec.fvec, &vec_to_lock);
if ( lock_dot > best_lock_dot ) {
best_lock_dot=lock_dot;
*target_subsys = ss;
}
}
ss = GET_NEXT( ss );
}
}
}
void hud_lock_acquire_uncaged_subsystem(weapon_info *wip, lock_info *lock, float *best_dot, int *least_num_locks)
{
Assert( lock->obj->type == OBJ_SHIP );
ship *sp = &Ships[lock->obj->instance];
float ss_dot;
int current_num_locks = 0;
if ( Ship_info[sp->ship_info_index].is_big_or_huge() ) {
for (ship_subsys* ss = GET_FIRST(&sp->subsys_list); ss != END_OF_LIST(&sp->subsys_list); ss = GET_NEXT(ss) ) {
if (!weapon_multilock_can_lock_on_subsys(Player_obj, lock->obj, ss, wip, &ss_dot))
continue;
// check for existing locks
current_num_locks = 0;
bool actively_locking = false;
for ( auto & missile_lock : Player_ship->missile_locks) {
if (missile_lock.obj != nullptr && OBJ_INDEX(missile_lock.obj) == OBJ_INDEX(lock->obj) ) {
if (missile_lock.subsys != nullptr && missile_lock.subsys == ss ) {
if ( !missile_lock.locked ) {
// we're already currently locking on this subsystem so let's not throw another aspect lock on it.
actively_locking = true;
continue;
}
++current_num_locks;
}
}
}
if ( !actively_locking
&& current_num_locks < wip->max_seekers_per_target
&& current_num_locks <= *least_num_locks
&& ss_dot > *best_dot ) {
lock->subsys = ss;
*best_dot = ss_dot;
*least_num_locks = current_num_locks;
}
}
}
}
void hud_lock_acquire_uncaged_target(lock_info *current_lock, weapon_info *wip)
{
object *A;
float dot;
size_t i = 0;
object* best_obj = nullptr;
ship_subsys *best_subsys = nullptr;
float best_dot = 0.0f;
int current_num_locks = 0;
int least_num_locks = INT_MAX;
bool actively_locking = false;
for ( A = GET_FIRST(&obj_used_list); A !=END_OF_LIST(&obj_used_list); A = GET_NEXT(A) ) {
if (A->flags[Object::Object_Flags::Should_be_dead])
continue;
if (!weapon_multilock_can_lock_on_target(Player_obj, A, wip, &dot))
continue;
bool in_range = weapon_secondary_world_pos_in_range(Player_obj, wip, &A->pos);
if ( A->type == OBJ_SHIP && Ship_info[Ships[A->instance].ship_info_index].is_big_or_huge() ) {
lock_info temp_lock;
temp_lock.obj = A;
temp_lock.subsys = nullptr;
float ss_dot = 0.0f;
int ss_num_locks = INT_MAX;
hud_lock_acquire_uncaged_subsystem(wip, &temp_lock, &ss_dot, &ss_num_locks);
if ( temp_lock.subsys != nullptr && ss_num_locks < wip->max_seekers_per_target && ss_num_locks <= least_num_locks && ss_dot > best_dot ) {
best_subsys = temp_lock.subsys;
best_obj = A;
best_dot = ss_dot;
least_num_locks = ss_num_locks;
}
} else {
if ( !in_range ) {
continue;
}
if ( dot < wip->lock_fov ) {
continue;
}
current_num_locks = 0;
actively_locking = false;
for ( i = 0; i < Player_ship->missile_locks.size(); ++i ) {
if ( Player_ship->missile_locks[i].obj != nullptr && OBJ_INDEX(Player_ship->missile_locks[i].obj) == OBJ_INDEX(A) && Player_ship->missile_locks[i].subsys == nullptr) {
if ( !Player_ship->missile_locks[i].locked ) {
// we're already currently locking on this subsystem so let's not throw another aspect lock on it.
actively_locking = true;
continue;
}
current_num_locks++;
}
}
if ( !actively_locking
&& current_num_locks < wip->max_seekers_per_target
&& current_num_locks <= least_num_locks
&& dot > best_dot ) {
best_subsys = nullptr;
best_obj = A;
best_dot = dot;
least_num_locks = current_num_locks;
}
}
}
current_lock->obj = best_obj;
current_lock->subsys = best_subsys;
}
void hud_lock_acquire_uncaged_target_weapon(lock_info* current_lock, weapon_info* wip)
{
object* A;
float dot;
size_t i = 0;
object* best_obj = nullptr;
ship_subsys* best_subsys = nullptr;
float best_dot = 0.0f;
int current_num_locks = 0;
int least_num_locks = INT_MAX;
bool actively_locking = false;
for (A = GET_FIRST(&obj_used_list); A != END_OF_LIST(&obj_used_list); A = GET_NEXT(A)) {
if (A->flags[Object::Object_Flags::Should_be_dead])
continue;
if (!weapon_multilock_can_lock_on_target(Player_obj, A, wip, &dot, true))
continue;
bool in_range = weapon_secondary_world_pos_in_range(Player_obj, wip, &A->pos);
if (!in_range) {
continue;
}
if (dot < wip->lock_fov) {
continue;
}
current_num_locks = 0;
actively_locking = false;
for (i = 0; i < Player_ship->missile_locks.size(); ++i) {
if (Player_ship->missile_locks[i].obj != nullptr && OBJ_INDEX(Player_ship->missile_locks[i].obj) == OBJ_INDEX(A) && Player_ship->missile_locks[i].subsys == nullptr) {
if (!Player_ship->missile_locks[i].locked) {
// we're already currently locking on this subsystem so let's not throw another aspect lock on it.
actively_locking = true;
continue;
}
current_num_locks++;
}
}
if (!actively_locking
&& current_num_locks < wip->max_seekers_per_target
&& current_num_locks <= least_num_locks
&& dot > best_dot) {
best_subsys = nullptr;
best_obj = A;
best_dot = dot;
least_num_locks = current_num_locks;
}
}
current_lock->obj = best_obj;
current_lock->subsys = best_subsys;
}
void hud_lock_determine_lock_target(lock_info *lock_slot, weapon_info *wip)
{
if ( lock_slot->obj != nullptr) {
return;
}
if (wip->target_restrict == LR_ANY_TARGETS) {
// if this weapon is uncaged, grab the best possible target within the player's lock cone and weapon distance
vec3d vec_to_target;
vec3d target_pos;
object *objp;
float dot;
if ( lock_slot->obj != nullptr) {
// lock slot occupied; do a check to see if this is a valid lock
objp = lock_slot->obj;
if ( lock_slot->subsys ) {
vm_vec_unrotate(&target_pos, &Player->locking_subsys->system_info->pnt, &objp->orient);
vm_vec_add2(&target_pos, &objp->pos);
} else {
target_pos = objp->pos;
}
vm_vec_normalized_dir(&vec_to_target, &target_pos, &Eye_position);
dot = vm_vec_dot(&Player_obj->orient.vec.fvec, &vec_to_target);
if (!weapon_secondary_world_pos_in_range(Player_obj, wip, &lock_slot->obj->pos) || dot < wip->lock_fov) {
// set this lock slot to empty
ship_clear_lock(lock_slot);
if (wip->target_restrict_objecttypes == LR_Objecttypes::LRO_SHIPS)
hud_lock_acquire_uncaged_target(lock_slot, wip);
else