Skip to content

Commit 168b331

Browse files
authored
Cleanup of Shield Hitpoint Threshold (#6851)
* Cleanup of Shield Hitpoint Threshold Overall cleanup that does a few things: 1) Changes `ship_is_shield_up` from integer to bool. 2) `ship_is_shield_up` was only ever called to look at one quadrant, never for all quadrants, so took the opportunity to update the 'all-quadrant' block to account for ships with a non-standard number of shield quadrants. 3) ` MAX(2.0f, Shield_percent_skips_damage * shield_get_max_quad(ship_objp)` was used many times throughout the code (and will be used at least 2 more times with #6848), so simply consolidated all of those uses into a new function called `ship_shield_hitpoint_threshold`. Happy to edit the name however. 4) Removed un-needed redundant comments from `ship_is_shield_up` definition in `ship.h` since those comments were already present in the actual function within `shield.cpp`, and all the other functions used comments in that file instead. Again happy to tune or edit with any other choices. * use proper default arg style * actually commit the updated files * appease modern clang * clang round 2
1 parent 365d9c5 commit 168b331

3 files changed

Lines changed: 28 additions & 21 deletions

File tree

code/ship/shield.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -903,27 +903,37 @@ void create_shield_explosion_all(object *objp)
903903
}
904904
}
905905

906+
/**
907+
* Returns the lowest threshold of shield hitpoints that triggers a shield hit
908+
*
909+
* @return If all_quadrants is true, looks at entire shield, otherwise just one quadrant
910+
*/
911+
float ship_shield_hitpoint_threshold(const object* obj, bool all_quadrants)
912+
{
913+
if (all_quadrants) {
914+
// All quadrants
915+
auto num_quads = static_cast<float>(obj->shield_quadrant.size());
916+
return MAX(2.0f * num_quads, Shield_percent_skips_damage * shield_get_max_strength(obj));
917+
} else {
918+
// Just one quadrant
919+
return MAX(2.0f, Shield_percent_skips_damage * shield_get_max_quad(obj));
920+
}
921+
}
922+
906923
/**
907924
* Returns true if the shield presents any opposition to something trying to force through it.
908925
*
909926
* @return If quadrant is -1, looks at entire shield, otherwise just one quadrant
910927
*/
911-
int ship_is_shield_up( const object *obj, int quadrant )
928+
bool ship_is_shield_up(const object *obj, int quadrant)
912929
{
913930
if ( (quadrant >= 0) && (quadrant < static_cast<int>(obj->shield_quadrant.size()))) {
914931
// Just check one quadrant
915-
if (shield_get_quad(obj, quadrant) > MAX(2.0f, Shield_percent_skips_damage * shield_get_max_quad(obj))) {
916-
return 1;
917-
}
932+
return ( shield_get_quad(obj, quadrant) > ship_shield_hitpoint_threshold(obj, false) );
918933
} else {
919934
// Check all quadrants
920-
float strength = shield_get_strength(obj);
921-
922-
if ( strength > MAX(2.0f*4.0f, Shield_percent_skips_damage * shield_get_max_strength(obj)) ) {
923-
return 1;
924-
}
935+
return ( shield_get_strength(obj) > ship_shield_hitpoint_threshold(obj, true) );
925936
}
926-
return 0; // no shield strength
927937
}
928938

929939
// return quadrant containing hit_pnt.

code/ship/ship.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,11 +1771,8 @@ extern void add_shield_point_multi(int objnum, int tri_num, vec3d *hit_pos);
17711771
extern void shield_point_multi_setup();
17721772
extern void shield_hit_close();
17731773

1774-
// Returns true if the shield presents any opposition to something
1775-
// trying to force through it.
1776-
// If quadrant is -1, looks at entire shield, otherwise
1777-
// just one quadrant
1778-
int ship_is_shield_up( const object *obj, int quadrant );
1774+
float ship_shield_hitpoint_threshold(const object* obj, bool all_quadrants = false);
1775+
bool ship_is_shield_up(const object *obj, int quadrant);
17791776

17801777
//=================================================
17811778
void ship_model_replicate_submodels(object *objp);

code/ship/shiphit.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,8 +2425,8 @@ static void ship_do_damage(object *ship_objp, object *other_obj, const vec3d *hi
24252425
HitType::SHIELD,
24262426
0.0f,
24272427
// we have to do this annoying thing where we reduce the shield health a bit because it turns out the last X percent of a shield doesn't matter
2428-
MAX(0.0f, ship_objp->shield_quadrant[quadrant] - MAX(2.0f, Shield_percent_skips_damage * shield_get_max_quad(ship_objp))),
2429-
shield_get_max_quad(ship_objp) - MAX(2.0f, Shield_percent_skips_damage * shield_get_max_quad(ship_objp)),
2428+
MAX(0.0f, ship_objp->shield_quadrant[quadrant] - ship_shield_hitpoint_threshold(ship_objp)),
2429+
shield_get_max_quad(ship_objp) - ship_shield_hitpoint_threshold(ship_objp),
24302430
};
24312431
} else {
24322432
impact_data[static_cast<std::underlying_type_t<HitType>>(HitType::HULL)] = ConditionData {
@@ -2458,8 +2458,8 @@ static void ship_do_damage(object *ship_objp, object *other_obj, const vec3d *hi
24582458
HitType::SHIELD,
24592459
0.0f,
24602460
// we have to do this annoying thing where we reduce the shield health a bit because it turns out the last X percent of a shield doesn't matter
2461-
MAX(0.0f, ship_objp->shield_quadrant[quadrant] - MAX(2.0f, Shield_percent_skips_damage * shield_get_max_quad(ship_objp))),
2462-
shield_get_max_quad(ship_objp) - MAX(2.0f, Shield_percent_skips_damage * shield_get_max_quad(ship_objp)),
2461+
MAX(0.0f, ship_objp->shield_quadrant[quadrant] - ship_shield_hitpoint_threshold(ship_objp)),
2462+
shield_get_max_quad(ship_objp) - ship_shield_hitpoint_threshold(ship_objp),
24632463
};
24642464

24652465
if ( damage > 0.0f ) {
@@ -2882,8 +2882,8 @@ void ship_apply_local_damage(object *ship_objp, object *other_obj, const vec3d *
28822882
HitType::SHIELD,
28832883
0.0f,
28842884
// we have to do this annoying thing where we reduce the shield health a bit because it turns out the last X percent of a shield doesn't matter
2885-
MAX(0.0f, ship_objp->shield_quadrant[quadrant] - MAX(2.0f, Shield_percent_skips_damage * shield_get_max_quad(ship_objp))),
2886-
shield_get_max_quad(ship_objp) - MAX(2.0f, Shield_percent_skips_damage * shield_get_max_quad(ship_objp)),
2885+
MAX(0.0f, ship_objp->shield_quadrant[quadrant] - ship_shield_hitpoint_threshold(ship_objp)),
2886+
shield_get_max_quad(ship_objp) - ship_shield_hitpoint_threshold(ship_objp),
28872887
};
28882888
} else {
28892889
impact_data[static_cast<std::underlying_type_t<HitType>>(HitType::HULL)] = ConditionData {

0 commit comments

Comments
 (0)