Skip to content

Commit a545a4e

Browse files
Merge pull request #3087 from Baezon/lock-restriction-safety
Safety for lock restriction handling
2 parents fc0662a + 7666246 commit a545a4e

File tree

4 files changed

+16
-18
lines changed

4 files changed

+16
-18
lines changed

code/ai/aicode.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9065,7 +9065,7 @@ void ai_chase()
90659065
}
90669066
}
90679067

9068-
if (timestamp_elapsed(swp->next_secondary_fire_stamp[current_bank]) && weapon_can_lock_on_ship(swip, En_objp->instance)) {
9068+
if (timestamp_elapsed(swp->next_secondary_fire_stamp[current_bank]) && weapon_target_satisfies_lock_restrictions(swip, En_objp)) {
90699069
if (current_bank >= 0) {
90709070
float firing_range;
90719071

code/hud/hudlock.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ void hud_lock_acquire_uncaged_target(lock_info *current_lock, weapon_info *wip)
924924
continue;
925925
}*/
926926

927-
if (!weapon_can_lock_on_ship(wip, A->instance)) {
927+
if (!weapon_target_satisfies_lock_restrictions(wip, A)) {
928928
continue;
929929
}
930930

@@ -1042,7 +1042,7 @@ void hud_lock_determine_lock_target(lock_info *lock_slot, weapon_info *wip)
10421042
return;
10431043
}
10441044

1045-
if ( !weapon_can_lock_on_ship(wip, lock_slot->obj->instance) ) {
1045+
if ( !weapon_target_satisfies_lock_restrictions(wip, lock_slot->obj) ) {
10461046
ship_clear_lock(lock_slot);
10471047
return;
10481048
}
@@ -1110,7 +1110,7 @@ void hud_lock_determine_lock_target(lock_info *lock_slot, weapon_info *wip)
11101110
return;
11111111
}
11121112

1113-
if ( !weapon_can_lock_on_ship(wip, lock_slot->obj->instance) ) {
1113+
if ( !weapon_target_satisfies_lock_restrictions(wip, lock_slot->obj) ) {
11141114
ship_clear_lock(lock_slot);
11151115
return;
11161116
}

code/weapon/weapon.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,8 @@ void shield_impact_explosion(vec3d *hitpos, object *objp, float radius, int idx)
676676
// Swifty - return number of max simultaneous locks
677677
int weapon_get_max_missile_seekers(weapon_info *wip);
678678

679-
// return if this weapon can lock on this ship, based on its type, class, species or iff
680-
bool weapon_can_lock_on_ship(weapon_info *wip, int ship_num);
679+
// return if this weapon can lock on this target, based on its type, class, species or iff
680+
bool weapon_target_satisfies_lock_restrictions(weapon_info *wip, object* target);
681681

682682
// return if this weapon has iff restrictions, and should ignore normal iff targeting restrictions
683683
bool weapon_has_iff_restrictions(weapon_info* wip);

code/weapon/weapons.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4093,9 +4093,8 @@ void find_homing_object(object *weapon_objp, int num)
40934093
continue;
40944094

40954095
homing_object_team = obj_team(objp);
4096-
bool can_lock_on_ship = objp->type != OBJ_SHIP || weapon_can_lock_on_ship(wip, objp->instance);
40974096
bool can_attack = weapon_has_iff_restrictions(wip) || iff_x_attacks_y(wp->team, homing_object_team);
4098-
if (can_lock_on_ship && can_attack)
4097+
if (weapon_target_satisfies_lock_restrictions(wip, objp) && can_attack)
40994098
{
41004099
if ( objp->type == OBJ_SHIP )
41014100
{
@@ -5353,13 +5352,9 @@ void weapon_set_tracking_info(int weapon_objnum, int parent_objnum, int target_o
53535352

53545353
if ( parent_objp == NULL || Ships[parent_objp->instance].ai_index >= 0 ) {
53555354
int target_team = -1;
5356-
int target_ship_num = -1;
53575355
if ( target_objnum >= 0 ) {
53585356
int obj_type = Objects[target_objnum].type;
53595357

5360-
if (obj_type == OBJ_SHIP)
5361-
target_ship_num = Objects[target_objnum].instance;
5362-
53635358
if ( (obj_type == OBJ_SHIP) || (obj_type == OBJ_WEAPON) ) {
53645359
target_team = obj_team(&Objects[target_objnum]);
53655360

@@ -5381,7 +5376,7 @@ void weapon_set_tracking_info(int weapon_objnum, int parent_objnum, int target_o
53815376
targeting_same = 0;
53825377
}
53835378

5384-
bool can_lock = weapon_has_iff_restrictions(wip) && target_ship_num >= 0 ? weapon_can_lock_on_ship(wip, target_ship_num) :
5379+
bool can_lock = (weapon_has_iff_restrictions(wip) && target_objnum >= 0) ? weapon_target_satisfies_lock_restrictions(wip, &Objects[target_objnum]) :
53855380
(!targeting_same || (MULTI_DOGFIGHT && (target_team == Iff_traitor)));
53865381

53875382
// Cyborg17 - exclude all invalid object numbers here since in multi, the lock slots can get out of sync.
@@ -8691,16 +8686,19 @@ int weapon_get_max_missile_seekers(weapon_info *wip)
86918686
}
86928687

86938688
// returns whether a homing weapon can home on a particular ship
8694-
bool weapon_can_lock_on_ship(weapon_info* wip, int ship_num)
8689+
bool weapon_target_satisfies_lock_restrictions(weapon_info* wip, object* target)
86958690
{
8691+
if (target->type != OBJ_SHIP)
8692+
return true;
8693+
86968694
auto& restrictions = wip->ship_restrict;
86978695
// if you didn't specify any restrictions, you can always lock
86988696
if (restrictions.empty()) return true;
86998697

8700-
int type_num = Ship_info[Ships[ship_num].ship_info_index].class_type;
8701-
int class_num = Ships[ship_num].ship_info_index;
8702-
int species_num = Ship_info[Ships[ship_num].ship_info_index].species;
8703-
int iff_num = Ships[ship_num].team;
8698+
int type_num = Ship_info[Ships[target->instance].ship_info_index].class_type;
8699+
int class_num = Ships[target->instance].ship_info_index;
8700+
int species_num = Ship_info[Ships[target->instance].ship_info_index].species;
8701+
int iff_num = Ships[target->instance].team;
87048702

87058703
// otherwise, you're good as long as it matches one of the allowances in the restriction list
87068704
return std::any_of(restrictions.begin(), restrictions.end(),

0 commit comments

Comments
 (0)