Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preventing three Compute::getTargetTotalNP NPEs #3022

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 21 additions & 29 deletions megamek/src/megamek/common/Compute.java
Original file line number Diff line number Diff line change
Expand Up @@ -2865,52 +2865,44 @@ public static ToHitData getStrafingTerrainModifier(IGame game,
}

/**
* Calculates the current theoretical damage absorbable(armor+structure, etc) by the given target.
* Calculates the current theoretical damage absorbable (armor+structure, etc) by the given target.
* Used as a measure of the potential durability of the target under fire.
*/
public static int getTargetTotalHP(IGame game, Targetable target) {
int targetType = target.getTargetType();
int targetId = target.getTargetId();
Coords position = target.getPosition();

//First, handle buildings versus entities, since they are handled differently.
if(targetType == Targetable.TYPE_BUILDING) {
//Buildings are a simple sum of their current CF and armor values.
Building parentBuilding = game.getBoard().getBuildingAt(position); //the building the targeted hex belongs to. We have to get this and then get values for the specific hex internally to it.
int targetCF = parentBuilding.getCurrentCF(position);
int targetArmor = parentBuilding.getArmor(position);
return targetCF + targetArmor;
// First, handle buildings versus entities, since they are handled differently.
if (targetType == Targetable.TYPE_BUILDING) {
// Buildings are a simple sum of their current CF and armor values.
// the building the targeted hex belongs to. We have to get this and then get values for the specific hex internally to it.
final Building parentBuilding = game.getBoard().getBuildingAt(position);
return (parentBuilding == null) ? 0
: parentBuilding.getCurrentCF(position) + parentBuilding.getArmor(position);
} else if (targetType == Targetable.TYPE_ENTITY) {
//I don't *think* we have to handle infantry differently here- I think these methods should return the total number of men remaining as internal structure.
//I don't *think* we have to handle infantry differently here - I think these methods should return the total number of men remaining as internal structure.
Entity targetEntity = game.getEntity(targetId);

if (targetEntity instanceof GunEmplacement) { //If this is a gun emplacement, handle it as the building hex it is in.
Building parentBuilding = game.getBoard().getBuildingAt(position);
int targetCF = parentBuilding.getCurrentCF(position);
int targetArmor = parentBuilding.getArmor(position);
return targetCF + targetArmor;
if (targetEntity == null) {
return 0;
} else if (targetEntity instanceof GunEmplacement) {
// If this is a gun emplacement, handle it as the building hex it is in.
final Building parentBuilding = game.getBoard().getBuildingAt(position);
return (parentBuilding == null) ? 0
: parentBuilding.getCurrentCF(position) + parentBuilding.getArmor(position);
} else {
return targetEntity.getTotalArmor() + targetEntity.getTotalInternal();
}
int targetArmor = targetEntity.getTotalArmor();
int targetStructure = targetEntity.getTotalInternal();
return targetArmor + targetStructure;
} else if (targetType == Targetable.TYPE_HEX_CLEAR) {
// clearing a hex - the "HP" is the terrain factor of destroyable terrain on this hex
IHex mhex = game.getBoard().getHex(position);
int terrainTypes[] = mhex.getTerrainTypes();
int totalTF = 0;

for (int i = 0; i < terrainTypes.length; i++) {
int tf = 0;
int terType = terrainTypes[i];
if (mhex.containsTerrain(terType)) {
tf = mhex.getTerrain(terType).getTerrainFactor();
}

totalTF += tf;
for (final int terrainType : mhex.getTerrainTypes()) {
totalTF += mhex.containsTerrain(terrainType) ? mhex.getTerrain(terrainType).getTerrainFactor() : 0;
}

return totalTF;
} else { //something else, e.g. terrain. We probably don't need to handle it for now.
} else { // something else, e.g. terrain. We probably don't need to handle it for now.
return 0;
}
}
Expand Down