Skip to content

Commit

Permalink
Merge pull request #1412 from MegaMek/armor_rework
Browse files Browse the repository at this point in the history
Armor rework, part 1b
  • Loading branch information
HammerGS authored Feb 4, 2024
2 parents 8edc967 + 9a22d3e commit 9261057
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 65 deletions.
6 changes: 3 additions & 3 deletions megameklab/src/megameklab/printing/PrintEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
import java.text.NumberFormat;
import java.util.*;

import static megamek.common.EquipmentType.T_ARMOR_BA_STANDARD;
import static megamek.common.EquipmentType.T_ARMOR_STANDARD;
import static megamek.common.EquipmentType.*;

/**
* Base class for printing Entity record sheets
Expand Down Expand Up @@ -333,7 +332,8 @@ protected void drawArmor() {
boolean hasSpecial = false;
for (int loc = firstArmorLocation(); loc < getEntity().locations(); loc++) {
if ((getEntity().getArmorType(loc) != T_ARMOR_STANDARD)
&&(getEntity().getArmorType(loc) != T_ARMOR_BA_STANDARD)
&& (getEntity().getArmorType(loc) != T_ARMOR_BA_STANDARD)
&& (getEntity().getArmorType(loc) != T_ARMOR_STANDARD_PROTOMEK)
// Stealth armor loses special properties when used with patchwork, so we don't
// need to show it.
&& (getEntity().getArmorType(loc) != EquipmentType.T_ARMOR_STEALTH)
Expand Down
5 changes: 3 additions & 2 deletions megameklab/src/megameklab/ui/battleArmor/BAStructureTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import megamek.codeUtilities.MathUtility;
import megamek.common.*;
import megamek.common.equipment.ArmorType;
import megamek.common.verifier.TestBattleArmor;
import megamek.common.verifier.TestBattleArmor.BAManipulator;
import megamek.common.verifier.TestEntity;
Expand Down Expand Up @@ -456,7 +457,7 @@ public void updateTechLevel() {
panMovement.refresh();
panEnhancements.setFromEntity(getBattleArmor());
panArmor.refresh();
EquipmentType armor = panArmor.getArmor();
ArmorType armor = panArmor.getArmor();
// If the current armor is no longer available, switch to the current selection
if (EquipmentType.getArmorType(armor) != getBattleArmor().getArmorType(BattleArmor.LOC_SQUAD)
|| (armor.getTechLevel(getBattleArmor().getYear())
Expand Down Expand Up @@ -662,7 +663,7 @@ public void armorFactorChanged(int points) {
}

@Override
public void armorTypeChanged(EquipmentType armor) {
public void armorTypeChanged(ArmorType armor) {
UnitUtil.removeISorArmorMounts(getBattleArmor(), false);
int armorCount = armor.getCriticals(getBattleArmor());
getBattleArmor().setArmorTechLevel(armor.getTechLevel(getBattleArmor().getYear()));
Expand Down
30 changes: 9 additions & 21 deletions megameklab/src/megameklab/ui/generalUnit/BAProtoArmorView.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import megamek.common.*;
import megamek.common.annotations.Nullable;
import megamek.common.equipment.ArmorType;
import megamek.common.verifier.TestProtomech;
import megameklab.ui.listeners.ArmorAllocationListener;
import megameklab.ui.util.TechComboBox;
Expand Down Expand Up @@ -54,7 +55,7 @@ public void removeListener(ArmorAllocationListener l) {
private final static String CMD_MAXIMIZE = "MAXIMIZE";
private final static String CMD_REMAINING = "REMAINING";

private final TechComboBox<EquipmentType> cbArmorType = new TechComboBox<>(EquipmentType::getName);
private final TechComboBox<ArmorType> cbArmorType = new TechComboBox<>(EquipmentType::getName);
private final SpinnerNumberModel spnArmorPointsModel = new SpinnerNumberModel(0, 0, null, 1);
private final JSpinner spnArmorPoints = new JSpinner(spnArmorPointsModel);
private final JButton btnMaximize = new JButton();
Expand Down Expand Up @@ -124,10 +125,7 @@ public void setFromEntity(Entity en) {
refresh();
cbArmorType.removeActionListener(this);
spnArmorPoints.removeChangeListener(this);
String name = EquipmentType.getArmorTypeName(en.getArmorType(0),
TechConstants.isClan(en.getArmorTechLevel(0)));
EquipmentType eq = EquipmentType.get(name);
cbArmorType.setSelectedItem(eq);
cbArmorType.setSelectedItem(ArmorType.forEntity(en));
if (en.hasETypeFlag(Entity.ETYPE_BATTLEARMOR)) {
spnArmorPointsModel.setValue(Math.min(((BattleArmor)en).getMaximumArmorPoints(),
en.getOArmor(BattleArmor.LOC_TROOPER_1)));
Expand All @@ -145,8 +143,8 @@ public void setFromEntity(Entity en) {
spnArmorPoints.addChangeListener(this);
}

public @Nullable EquipmentType getArmor() {
return (EquipmentType) cbArmorType.getSelectedItem();
public @Nullable ArmorType getArmor() {
return (ArmorType) cbArmorType.getSelectedItem();
}

public int getArmorPoints() {
Expand All @@ -164,19 +162,9 @@ public void refresh() {
} else if ((etype & Entity.ETYPE_PROTOMECH) != 0) {
flag = MiscType.F_PROTOMECH_EQUIPMENT;
}
for (int at = 0; at < EquipmentType.armorNames.length; at++) {
String name = EquipmentType.getArmorTypeName(at, techManager.useClanTechBase());
EquipmentType eq = EquipmentType.get(name);
if ((null != eq) && eq.hasFlag(flag) && techManager.isLegal(eq)) {
cbArmorType.addItem(eq);
}
if (techManager.useMixedTech()) {
name = EquipmentType.getArmorTypeName(at, !techManager.useClanTechBase());
EquipmentType eq2 = EquipmentType.get(name);
if ((null != eq2) && (eq != eq2) && eq2.hasFlag(flag)
&& techManager.isLegal(eq2)) {
cbArmorType.addItem(eq2);
}
for (ArmorType armor : ArmorType.allArmorTypes()) {
if (armor.hasFlag(flag) && techManager.isLegal(armor)) {
cbArmorType.addItem(armor);
}
}
cbArmorType.setSelectedItem(prev);
Expand All @@ -198,7 +186,7 @@ public void stateChanged(ChangeEvent evt) {
@Override
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == cbArmorType) {
listeners.forEach(l -> l.armorTypeChanged((EquipmentType)cbArmorType.getSelectedItem()));
listeners.forEach(l -> l.armorTypeChanged((ArmorType) cbArmorType.getSelectedItem()));
} else if (CMD_MAXIMIZE.equals(evt.getActionCommand())) {
listeners.forEach(ArmorAllocationListener::maximizeArmor);
} else if (CMD_REMAINING.equals(evt.getActionCommand())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,16 @@ public void refresh(Entity entity) {
critLabel.setText("");
availabilityLabel.setText("");

EquipmentType armor = null;
int armorType = entity.getArmorType(0);
if ((armorType >= 0) && (armorType < EquipmentType.armorNames.length)) {
String armorName = EquipmentType.getArmorTypeName(armorType,
TechConstants.isClan(entity.getArmorTechLevel(0)));
armor = EquipmentType.get(armorName);
availabilityLabel.setText(armor.getFullRatingName(entity.isClan()));
}
ArmorType armor = ArmorType.forEntity(entity);
availabilityLabel.setText(armor.getFullRatingName());

if (entity.isSupportVehicle()) {
// FIXME: This doesn't account for patchwork armor crits.
TestSupportVehicle testSupportVehicle = (TestSupportVehicle) testEntity;
critLabel.setText(formatCrits(testSupportVehicle.getArmorSlots()));
weightLabel.setText(formatWeight(testSupportVehicle.getWeightArmor(), entity));
} else if (entity instanceof Mech) {
if (armor != null) {
critLabel.setText(formatCrits(armor.getCriticals(entity)));
}
critLabel.setText(formatCrits(armor.getCriticals(entity)));
} else if (entity instanceof Tank) {
critLabel.setText(formatCrits(getTankArmorCrits(entity)));
} else if (entity instanceof AeroSpaceFighter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package megameklab.ui.listeners;

import megamek.common.EquipmentType;
import megamek.common.equipment.ArmorType;

/**
* Listener for changes to armor. No method is required by all implementing classes so all are given a default
Expand All @@ -42,7 +43,7 @@ default void armorTypeChanged(int at, int armorTechLevel) {
*
* @param armor The equipment instance of the armor to use.
*/
default void armorTypeChanged(EquipmentType armor) {
default void armorTypeChanged(ArmorType armor) {

}

Expand Down
2 changes: 1 addition & 1 deletion megameklab/src/megameklab/ui/protoMek/PMMainUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void createNewUnit(long entitytype, boolean isPrimitive, boolean isIndust
proto.setTechLevel(TechConstants.T_CLAN_TW);
proto.setOriginalWalkMP(1);
proto.setEngine(new Engine(TestProtomech.calcEngineRating(proto), Engine.NORMAL_ENGINE, Engine.CLAN_ENGINE));
proto.setArmorType(EquipmentType.T_ARMOR_STANDARD);
proto.setArmorType(EquipmentType.T_ARMOR_STANDARD_PROTOMEK);
proto.setArmorTechLevel(getEntity().getTechLevel());

proto.autoSetInternal();
Expand Down
26 changes: 9 additions & 17 deletions megameklab/src/megameklab/ui/protoMek/PMStructureTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import megamek.common.equipment.ArmorType;
import megamek.common.verifier.TestEntity;
import megamek.common.verifier.TestProtomech;
import megamek.common.verifier.TestProtomech.ProtomechArmor;
import megameklab.ui.EntitySource;
import megameklab.ui.generalUnit.ArmorAllocationView;
import megameklab.ui.generalUnit.BAProtoArmorView;
Expand Down Expand Up @@ -266,28 +265,21 @@ private boolean freeUpSpace(int loc, int count) {
return false;
}

private void createArmorMountsAndSetArmorType(EquipmentType armor) {
ProtomechArmor pmArmor = ProtomechArmor.getArmor(EquipmentType.getArmorType(armor));
private void createArmorMountsAndSetArmorType(ArmorType armor) {
List<Mounted> armorMounts = getProtomech().getMisc().stream()
.filter(m -> EquipmentType.getArmorType(m.getType()) != EquipmentType.T_ARMOR_UNKNOWN)
.filter(m -> m.getType() instanceof ArmorType)
.collect(Collectors.toList());
for (Mounted m : armorMounts) {
UnitUtil.removeMounted(getProtomech(), m);
}

if (pmArmor == null) {
getProtomech().setArmorType(EquipmentType.T_ARMOR_STANDARD);
getProtomech().setArmorTechLevel(TechConstants.T_ALL_CLAN);
return;
} else {
getProtomech().setArmorType(pmArmor.getType());
getProtomech().setArmorTechLevel(pmArmor.getArmorTech());
}
getProtomech().setArmorType(armor.getArmorType());
getProtomech().setArmorTechLevel(armor.getStaticTechLevel().getCompoundTechLevel(armor.isClan()));

if (pmArmor.getTorsoSlots() > 0) {
if (freeUpSpace(Protomech.LOC_TORSO, pmArmor.getTorsoSlots())) {
if (armor.getCriticals(getProtomech()) > 0) {
if (freeUpSpace(Protomech.LOC_TORSO, armor.getCriticals(getProtomech()))) {
try {
Mounted mount = new Mounted(getProtomech(), pmArmor.getArmorEqType());
Mounted mount = new Mounted(getProtomech(), armor);
getProtomech().addEquipment(mount, Protomech.LOC_TORSO, false);
return;
} catch (LocationFullException ignored) {
Expand All @@ -298,7 +290,7 @@ private void createArmorMountsAndSetArmorType(EquipmentType armor) {
"Requires free torso slot. Resetting to Standard Armor",
"Location Full",
JOptionPane.INFORMATION_MESSAGE);
getProtomech().setArmorType(EquipmentType.T_ARMOR_STANDARD);
getProtomech().setArmorType(EquipmentType.T_ARMOR_STANDARD_PROTOMEK);
getProtomech().setArmorTechLevel(TechConstants.T_ALL_CLAN);
panArmor.setFromEntity(getProtomech());
}
Expand Down Expand Up @@ -448,7 +440,7 @@ public void typeChanged(int motiveType) {
}

@Override
public void armorTypeChanged(EquipmentType armor) {
public void armorTypeChanged(ArmorType armor) {
UnitUtil.removeISorArmorMounts(getProtomech(), false);
createArmorMountsAndSetArmorType(armor);
panArmorAllocation.setFromEntity(getProtomech());
Expand Down
14 changes: 5 additions & 9 deletions megameklab/src/megameklab/util/UnitUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import megamek.common.equipment.ArmorType;
import megamek.common.verifier.*;
import megamek.common.verifier.TestEntity.Ceil;
import megamek.common.verifier.TestProtomech.ProtomechArmor;
import megamek.common.weapons.*;
import megamek.common.weapons.autocannons.HVACWeapon;
import megamek.common.weapons.autocannons.UACWeapon;
Expand Down Expand Up @@ -100,7 +99,7 @@ public static boolean isFixedLocationSpreadEquipment(EquipmentType eq) {
* @return
*/
public static boolean isArmor(EquipmentType eq) {
return Arrays.asList(EquipmentType.armorNames).contains(eq.getName());
return eq instanceof ArmorType;
}

/**
Expand Down Expand Up @@ -862,7 +861,7 @@ public static double getMaximumArmorTonnage(Entity unit) {
armorWeight = Math.ceil(armorWeight * 2.0) / 2.0;
} else if (unit instanceof Protomech) {
double points = TestProtomech.maxArmorFactor((Protomech) unit);
return points * ProtomechArmor.getArmor((Protomech) unit).getWtPerPoint();
return points * ArmorType.forEntity(unit).getWeightPerPoint();
} else if (unit.isSupportVehicle()) {
// Max armor is determined by number of points.
double weight = TestSupportVehicle.maxArmorFactor(unit)
Expand Down Expand Up @@ -1423,17 +1422,14 @@ public static boolean isSupportVehicleEquipment(EquipmentType eq, Entity unit) {
* @param internalStructure true to remove IS, false to remove armor
*/
public static void removeISorArmorCrits(Entity unit, boolean internalStructure) {
ArrayList<String> mountList = new ArrayList<>();
List<String> mountList = new ArrayList<>();
if (internalStructure) {
for (String struc : EquipmentType.structureNames) {
mountList.add("IS " + struc);
mountList.add("Clan " + struc);
}
} else {
for (String armor : EquipmentType.armorNames) {
mountList.add("IS " + armor);
mountList.add("Clan " + armor);
}
mountList = ArmorType.allArmorTypes().stream().map(ArmorType::getInternalName).collect(Collectors.toList());
}

for (int location = Mech.LOC_HEAD; location < unit.locations(); location++) {
Expand Down Expand Up @@ -1472,7 +1468,7 @@ public static void removeISorArmorMounts(Entity unit, boolean internalStructure)
if (internalStructure) {
names = Arrays.asList(EquipmentType.structureNames);
} else {
names = Arrays.asList(EquipmentType.armorNames);
names = ArmorType.allArmorNames();
}
for (String name : names) {
mountList.add(String.format("Clan %1s", name));
Expand Down

0 comments on commit 9261057

Please sign in to comment.