Skip to content

Commit

Permalink
move some armor construction util to MM, test if armor allocation exc…
Browse files Browse the repository at this point in the history
…eeds actual armor
  • Loading branch information
SJuliez committed Feb 18, 2025
1 parent b0c3523 commit de7a422
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ public void useRemainingTonnageArmor() {
double totalTonnage = getBattleArmor().getTrooperWeight();
double remainingTonnage = TestEntity.floor(
totalTonnage - currentTonnage, TestEntity.Ceil.KILO);
int points = (int) UnitUtil.getRawArmorPoints(getBattleArmor(), remainingTonnage);
int points = (int) TestEntity.getRawArmorPoints(getBattleArmor(), remainingTonnage);
int maxArmor = MathUtility.clamp(getBattleArmor().getMaximumArmorPoints(), 0,
points + getBattleArmor().getOArmor(BattleArmor.LOC_TROOPER_1));
armorFactorChanged(maxArmor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ public void armorPointsChanged(int location, int front, int rear) {

@Override
public void autoAllocateArmor() {
int pointsToAllocate = UnitUtil.getArmorPoints(getTank(), getTank().getLabArmorTonnage());
int pointsToAllocate = TestEntity.getArmorPoints(getTank());

for (int location = 0; location < getTank().locations(); location++) {
getTank().initializeArmor(0, location);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ public void autoAllocateArmor() {
}

// divide armor among positions, with more toward the front
int points = UnitUtil.getArmorPoints(getAero(), getAero().getLabArmorTonnage());
int points = TestEntity.getArmorPoints(getAero());
int nose = (int)Math.floor(points * 0.3);
int wing = (int)Math.floor(points * 0.25);
int aft = (int)Math.floor(points * 0.2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import megamek.common.*;
import megamek.common.equipment.ArmorType;
import megamek.common.verifier.TestEntity;
import megamek.common.verifier.TestSupportVehicle;
import megameklab.ui.generalUnit.ArmorLocationView.ArmorLocationListener;
import megameklab.ui.listeners.ArmorAllocationListener;
Expand Down Expand Up @@ -206,7 +207,7 @@ public void setFromEntity(Entity en) {
}
if (en.hasETypeFlag(Entity.ETYPE_SMALL_CRAFT)
|| en.hasETypeFlag(Entity.ETYPE_JUMPSHIP)) {
locView.setMinimum((int) (UnitUtil.getSIBonusArmorPoints(en) / locationViews.size()));
locView.setMinimum((int) (TestEntity.getSIBonusArmorPoints(en) / locationViews.size()));
}
if (showPatchwork) {
double pointsPerTon = UnitUtil.getArmorPointsPerTon(en);
Expand All @@ -224,8 +225,8 @@ public void setFromEntity(Entity en) {
}
}
int maxArmorPoints = UnitUtil.getMaximumArmorPoints(en);
int raw = (int) (UnitUtil.getRawArmorPoints(en, en.getLabArmorTonnage())
+ UnitUtil.getSIBonusArmorPoints(en));
int raw = (int) (TestEntity.getRawArmorPoints(en, en.getLabArmorTonnage())
+ TestEntity.getSIBonusArmorPoints(en));
int currentPoints = en.getTotalOArmor();
int armorPoints;
if (showPatchwork) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import megamek.common.*;
import megamek.common.annotations.Nullable;
import megamek.common.equipment.ArmorType;
import megamek.common.verifier.TestEntity;
import megamek.common.verifier.TestProtoMek;
import megameklab.ui.listeners.ArmorAllocationListener;
import megameklab.ui.util.TechComboBox;
Expand Down Expand Up @@ -133,7 +134,7 @@ public void setFromEntity(Entity en) {
} else if (en.hasETypeFlag(Entity.ETYPE_PROTOMEK)) {
final int max = TestProtoMek.maxArmorFactor((ProtoMek) en);
spnArmorPointsModel.setValue(Math.min(max,
(int) UnitUtil.getRawArmorPoints(en, en.getLabArmorTonnage())));
(int) TestEntity.getRawArmorPoints(en, en.getLabArmorTonnage())));
spnArmorPointsModel.setMaximum(max);
} else {
spnArmorPointsModel.setValue(en.getTotalOArmor());
Expand Down
4 changes: 2 additions & 2 deletions megameklab/src/megameklab/ui/largeAero/DSStructureTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ public void autoAllocateArmor() {
}

// divide armor (in excess of bonus from SI) among positions, with more toward the front
int bonusPerFacing = (int) UnitUtil.getSIBonusArmorPoints(getSmallCraft()) / ARMOR_FACINGS;
int points = UnitUtil.getArmorPoints(getSmallCraft(), getSmallCraft().getLabArmorTonnage())
int bonusPerFacing = (int) TestEntity.getSIBonusArmorPoints(getSmallCraft()) / ARMOR_FACINGS;
int points = TestEntity.getArmorPoints(getSmallCraft())
- bonusPerFacing * 4;
int nose = (int)Math.floor(points * 0.3);
int wing = (int)Math.floor(points * 0.25);
Expand Down
4 changes: 2 additions & 2 deletions megameklab/src/megameklab/ui/largeAero/WSStructureTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,8 @@ public void autoAllocateArmor() {
}

// divide armor (in excess of bonus from SI) among positions, with more toward the front
int bonusPerFacing = (int) Math.floor(UnitUtil.getSIBonusArmorPoints(getJumpship()) / ARMOR_FACINGS);
int points = UnitUtil.getArmorPoints(getJumpship(), getJumpship().getLabArmorTonnage())
int bonusPerFacing = (int) Math.floor(TestEntity.getSIBonusArmorPoints(getJumpship()) / ARMOR_FACINGS);
int points = TestEntity.getArmorPoints(getJumpship())
- bonusPerFacing * 6;
int nose = (int)Math.floor(points * 0.22);
int foreSides = (int)Math.floor(points * 0.18);
Expand Down
2 changes: 1 addition & 1 deletion megameklab/src/megameklab/ui/mek/BMStructureTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ public void armorPointsChanged(int location, int front, int rear) {

@Override
public void autoAllocateArmor() {
double pointsToAllocate = UnitUtil.getArmorPoints(getMek(), getMek().getLabArmorTonnage());
double pointsToAllocate = TestEntity.getArmorPoints(getMek());
double maxArmor = UnitUtil.getMaximumArmorPoints(getMek());
if (pointsToAllocate > maxArmor) {
pointsToAllocate = maxArmor;
Expand Down
2 changes: 1 addition & 1 deletion megameklab/src/megameklab/ui/protoMek/PMStructureTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ public void useRemainingTonnageArmor() {
double remainingTonnage = TestEntity.floor(
totalTonnage - currentTonnage, TestEntity.Ceil.KILO);
// We can only use remaining tonnage equal to whole points of armor.
remainingTonnage = (int) UnitUtil.getRawArmorPoints(getProtoMek(), remainingTonnage)
remainingTonnage = (int) TestEntity.getRawArmorPoints(getProtoMek(), remainingTonnage)
* ArmorType.forEntity(getProtoMek()).getWeightPerPoint();
double maxArmor = MathUtility.clamp(getProtoMek().getLabArmorTonnage() + remainingTonnage, 0,
UnitUtil.getMaximumArmorTonnage(getProtoMek()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public void autoAllocateArmor() {
rear = (getEntity() instanceof LargeSupportTank) ? LargeSupportTank.LOC_REAR : Tank.LOC_REAR;
body = Tank.LOC_BODY;
}
int pointsToAllocate = UnitUtil.getArmorPoints(getEntity(), getEntity().getLabArmorTonnage());
int pointsToAllocate = TestEntity.getArmorPoints(getEntity());

for (int location = 0; location < getEntity().locations(); location++) {
getEntity().initializeArmor(0, location);
Expand Down
69 changes: 0 additions & 69 deletions megameklab/src/megameklab/util/UnitUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -940,75 +940,6 @@ public static double getMaximumArmorTonnage(Entity unit) {
return armorWeight;
}

/**
* Computes the total number of armor points available to the unit for a given
* tonnage of armor.
* This does not round down the calculation or take into account any maximum
* number of armor
* points or tonnage allowed to the unit.
*
* NOTE: only use for non-patchwork armor
*
* @param unit
* @param armorTons
* @return the number of armor points available for the armor tonnage
*/
public static double getRawArmorPoints(Entity unit, double armorTons) {
if (unit.hasETypeFlag(Entity.ETYPE_PROTOMEK)) {
return Math.round(armorTons / ArmorType.forEntity(unit).getWeightPerPoint());
} else if (unit.isSupportVehicle()) {
return Math.floor(armorTons / TestSupportVehicle.armorWeightPerPoint(unit));
} else if ((unit instanceof Jumpship)
&& unit.getArmorType(unit.firstArmorIndex()) == EquipmentType.T_ARMOR_PRIMITIVE_AERO) {
// Because primitive JumpShip armor has an extra step of rounding we have to
// give it special treatment.
// Standard armor value is computed first, rounded down, then the primitive
// armor mod is applied.
return Math.floor(Math.floor(armorTons * TestAdvancedAerospace.armorPointsPerTon((Jumpship) unit,
EquipmentType.T_ARMOR_AEROSPACE, false)) * 0.66);
}
return armorTons * UnitUtil.getArmorPointsPerTon(unit);
}

/**
* Computes the total number of additional points provided for aerospace vessels
* based on
* their SI. This is usually a whole number but may be a fractional amount for
* primitive
* JumpShips.
*
* @param entity The unit to compute bonus armor for.
* @return The number of extra armor points received for SI. This is the total
* number, which
* is usually divided evenly among armor facings.
*/
public static double getSIBonusArmorPoints(Entity entity) {
double points = 0.0;
if (entity.hasETypeFlag(Entity.ETYPE_SMALL_CRAFT)) {
points = ((SmallCraft) entity).getSI() * (entity.locations() - 1);
} else if (entity.hasETypeFlag(Entity.ETYPE_JUMPSHIP)) {
points = Math.round(((Jumpship) entity).getSI() / 10.0) * 6;
}
if (entity.isPrimitive()) {
return points * ArmorType.of(EquipmentType.T_ARMOR_PRIMITIVE_AERO, false).getArmorPointsMultiplier();
} else {
return points;
}
}

/**
* NOTE: only use for non-patchwork armor
*
* @param unit The entity
* @param armorTons
* @return
*/
public static int getArmorPoints(Entity unit, double armorTons) {
int raw = (int) Math.floor(UnitUtil.getRawArmorPoints(unit, armorTons)
+ UnitUtil.getSIBonusArmorPoints(unit));
return Math.min(raw, UnitUtil.getMaximumArmorPoints(unit));
}

/**
* Calculate the number of armor points per ton of armor for the given unit.
*
Expand Down

0 comments on commit de7a422

Please sign in to comment.