Skip to content

Commit

Permalink
Merge pull request MegaMek#5082 from SJuliez/mechview-only-os-ammo
Browse files Browse the repository at this point in the history
MechView Ammo Block
  • Loading branch information
HammerGS authored Jan 28, 2024
2 parents a1c8342 + 40594ee commit 8241bf2
Showing 1 changed file with 12 additions and 45 deletions.
57 changes: 12 additions & 45 deletions megamek/src/megamek/common/MechView.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public MechView(final Entity entity, final boolean showDetail, final boolean use
sLoadout.addAll(getWeapons(showDetail));
}

if ((!entity.usesWeaponBays() || !showDetail) && !entity.getAmmo().isEmpty()) {
if (showAmmoBlock(showDetail)) {
sLoadout.add(new SingleLine());
sLoadout.add(getAmmo());
}
Expand Down Expand Up @@ -533,50 +533,22 @@ public MechView(final Entity entity, final boolean showDetail, final boolean use
sFluff.add(new LabeledElement("History", entity.getFluff().getHistory()));
}

EntityVerifier verifier = EntityVerifier.getInstance(new MegaMekFile(
Configuration.unitsDir(), EntityVerifier.CONFIG_FILENAME).getFile());
StringBuffer sb = new StringBuffer();
TestEntity testEntity = getTestEntity(entity, verifier);
TestEntity testEntity = TestEntity.getEntityVerifier(entity);

if (testEntity != null) {
testEntity.correctEntity(sb, entity.getTechLevel());

if (!sb.toString().isEmpty()) {
sInvalid.add(new SingleLine());
sInvalid.add(new LabeledElement(Messages.getString("MechView.InvalidReasons"), "\n" + sb.toString()));
sInvalid.add(new LabeledElement(Messages.getString("MechView.InvalidReasons"), "\n" + sb));
}
}
}

/**
* copied from megameklab.util.UnitUtil.getEntityVerifier
* @param unit the supplied entity
* @param entityVerifier the entity verifier loaded from a UnitVerifierOptions.xml
* @return a TestEntity instance for the supplied Entity.
*/
public static TestEntity getTestEntity(Entity unit, EntityVerifier entityVerifier) {
// FIXME move the same method from megameklab.util.UnitUtil.getEntityVerifier to common
TestEntity testEntity = null;
if (unit.hasETypeFlag(Entity.ETYPE_MECH)) {
testEntity = new TestMech((Mech) unit, entityVerifier.mechOption, null);
} else if (unit.hasETypeFlag(Entity.ETYPE_PROTOMECH)) {
testEntity = new TestProtomech((Protomech) unit, entityVerifier.protomechOption, null);
} else if (unit.isSupportVehicle()) {
testEntity = new TestSupportVehicle(unit, entityVerifier.tankOption, null);
} else if (unit.hasETypeFlag(Entity.ETYPE_TANK)) {
testEntity = new TestTank((Tank) unit, entityVerifier.tankOption, null);
} else if (unit.hasETypeFlag(Entity.ETYPE_SMALL_CRAFT)) {
testEntity = new TestSmallCraft((SmallCraft) unit, entityVerifier.aeroOption, null);
} else if (unit.hasETypeFlag(Entity.ETYPE_JUMPSHIP)) {
testEntity = new TestAdvancedAerospace((Jumpship) unit, entityVerifier.aeroOption, null);
} else if (unit.hasETypeFlag(Entity.ETYPE_AERO)) {
testEntity = new TestAero((Aero) unit, entityVerifier.aeroOption, null);
} else if (unit.hasETypeFlag(Entity.ETYPE_BATTLEARMOR)) {
testEntity = new TestBattleArmor((BattleArmor) unit, entityVerifier.baOption, null);
} else if (unit.hasETypeFlag(Entity.ETYPE_INFANTRY)) {
testEntity = new TestInfantry((Infantry)unit, entityVerifier.infOption, null);
}
return testEntity;
/** @return True when the unit requires an ammo block. */
private boolean showAmmoBlock(boolean showDetail) {
return (!entity.usesWeaponBays() || !showDetail) && !entity.getAmmo().stream().allMatch(this::hideAmmo);
}

private String eraText(int startYear, int endYear) {
Expand Down Expand Up @@ -1010,24 +982,19 @@ private String quirkMarker(Mounted mounted) {
return (mounted.countQuirks() > 0) ? " (Q)" : "";
}

private boolean hideAmmo(Mounted mounted) {
return ((mounted.getLinkedBy() != null) && mounted.getLinkedBy().isOneShot())
|| (mounted.getSize() == 0) || (mounted.getLocation() == Entity.LOC_NONE);
}

private ViewElement getAmmo() {
TableElement ammoTable = new TableElement(4);
ammoTable.setColNames("Ammo", "Loc", "Shots", entity.isOmni() ? "Omni" : "");
ammoTable.setJustification(TableElement.JUSTIFIED_LEFT, TableElement.JUSTIFIED_CENTER,
TableElement.JUSTIFIED_CENTER, TableElement.JUSTIFIED_CENTER);

for (Mounted mounted : entity.getAmmo()) {
// Ignore ammo for one-shot launchers
if ((mounted.getLinkedBy() != null)
&& mounted.getLinkedBy().isOneShot()) {
continue;
}
// Ignore bay ammo bins for unused munition types
if (mounted.getSize() == 0) {
continue;
}

if (mounted.getLocation() == Entity.LOC_NONE) {
if (hideAmmo(mounted)) {
continue;
}

Expand Down

0 comments on commit 8241bf2

Please sign in to comment.