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

UnitUtil separation #1375

Merged
merged 6 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion megameklab/src/megameklab/printing/InventoryWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private void parseEquipment() {
ammo.merge(m.getShortName(), m.getBaseShotsLeft(), Integer::sum);
}
if ((m.getLocation() == Entity.LOC_NONE)
|| !UnitUtil.isPrintableEquipment(m.getType(), sheet.getEntity())) {
|| !PrintUtil.isPrintableEquipment(m.getType(), sheet.getEntity())) {
continue;
}
if ((sheet.getEntity() instanceof QuadVee)
Expand Down
8 changes: 4 additions & 4 deletions megameklab/src/megameklab/printing/PrintBattleArmor.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import megamek.common.*;
import megamek.common.battlevalue.BattleArmorBVCalculator;
import megameklab.util.UnitUtil;
import megameklab.util.BattleArmorUtil;
import org.apache.batik.util.SVGConstants;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGRectElement;
Expand Down Expand Up @@ -86,8 +86,8 @@ protected void writeTextFields() {
break;
}
hideElement(CHECK_MECHANIZED, !battleArmor.canDoMechanizedBA());
hideElement(CHECK_SWARM, !UnitUtil.canSwarm(battleArmor));
hideElement(CHECK_LEG, !UnitUtil.canLegAttack(battleArmor));
hideElement(CHECK_SWARM, !BattleArmorUtil.canSwarm(battleArmor));
hideElement(CHECK_LEG, !BattleArmorUtil.canLegAttack(battleArmor));
hideElement(CHECK_AP, battleArmor.countWorkingMisc(MiscType.F_AP_MOUNT) == 0);

setTextField(BV, battleArmor.calculateBattleValue(true, !showPilotInfo())
Expand Down Expand Up @@ -144,7 +144,7 @@ protected String formatWalk() {
public String formatMiscNotes() {
final StringJoiner sj = new StringJoiner(" ");
if (battleArmor.isBurdened() && ((battleArmor.getJumpMP(MPCalculationSetting.BA_UNBURDENED) > 0)
|| UnitUtil.canLegAttack(battleArmor) || UnitUtil.canSwarm(battleArmor))) {
|| BattleArmorUtil.canLegAttack(battleArmor) || BattleArmorUtil.canSwarm(battleArmor))) {
sj.add("Must detach missiles before jumping or swarm/leg attacks.");
}
if (battleArmor.hasDWP()) {
Expand Down
4 changes: 1 addition & 3 deletions megameklab/src/megameklab/printing/PrintDropship.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@

import megamek.common.*;
import megameklab.util.ImageHelper;
import megameklab.util.UnitUtil;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGRectElement;

import java.awt.print.PageFormat;
import java.io.File;
import java.time.LocalDate;
import java.util.*;

Expand Down Expand Up @@ -332,7 +330,7 @@ public String formatFeatures() {
StringJoiner sj = new StringJoiner(", ");
Map<String, Integer> eqCount = new HashMap<>();
for (Mounted mount : ship.getMisc()) {
if (UnitUtil.isPrintableEquipment(mount.getType())) {
if (PrintUtil.isPrintableEquipment(mount.getType())) {
eqCount.merge(mount.getShortName(), 1, Integer::sum);
}
}
Expand Down
147 changes: 147 additions & 0 deletions megameklab/src/megameklab/printing/PrintUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* Copyright (c) 2024 - The MegaMek Team. All Rights Reserved.
*
* This file is part of MegaMek.
*
* MegaMek is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MegaMek is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MegaMek. If not, see <http://www.gnu.org/licenses/>.
*/
package megameklab.printing;

import megamek.common.*;
import megamek.common.weapons.LegAttack;
import megamek.common.weapons.StopSwarmAttack;
import megamek.common.weapons.SwarmAttack;
import megamek.common.weapons.SwarmWeaponAttack;
import megamek.common.weapons.infantry.InfantryRifleAutoRifleWeapon;
import megameklab.util.UnitUtil;

public final class PrintUtil {

public static boolean isPrintableEquipment(EquipmentType eq) {
return isPrintableEquipment(eq, false);
}

/**
* simple method to let us know if eq should be printed on the weapons and
* equipment section of the Record sheet.
*
* @param eq The equipment to test The equipment
* @param entity The Entity it's mounted on
* @return Whether the equipment should be shown on the record sheet
*/
public static boolean isPrintableEquipment(EquipmentType eq, Entity entity) {
if (eq instanceof AmmoType) {
return ((AmmoType) eq).getAmmoType() == AmmoType.T_COOLANT_POD;
} else if (entity instanceof BattleArmor) {
return isPrintableBAEquipment(eq);
} else {
return isPrintableEquipment(eq, entity instanceof Mech);
}
}

/**
* simple method to let us know if eq should be printed on the weapons and
* equipment section of the Record sheet.
*
* @param eq The equipment to test The equipment
* @param isMech Whether the equipment is mounted on a mech
* @return Whether the equipment should be shown on the record sheet
*/
public static boolean isPrintableEquipment(EquipmentType eq, boolean isMech) {
if (UnitUtil.isArmorOrStructure(eq)) {
return false;
}

if (UnitUtil.isFixedLocationSpreadEquipment(eq)
&& !(eq instanceof MiscType) && eq.hasFlag(MiscType.F_TALON)) {
return false;
}

if (UnitUtil.isJumpJet(eq)) {
return false;
}
if (!eq.isHittable() && isMech) {
return false;
}

if ((eq instanceof MiscType)
&& (eq.hasFlag(MiscType.F_CASE)
|| eq.hasFlag(MiscType.F_ARTEMIS)
|| eq.hasFlag(MiscType.F_ARTEMIS_PROTO)
|| eq.hasFlag(MiscType.F_ARTEMIS_V)
|| eq.hasFlag(MiscType.F_APOLLO)
|| eq.hasFlag(MiscType.F_PPC_CAPACITOR)
|| (eq.hasFlag(MiscType.F_MASC) && isMech)
|| eq.hasFlag(MiscType.F_HARJEL)
|| eq.hasFlag(MiscType.F_MASS)
|| eq.hasFlag(MiscType.F_CHASSIS_MODIFICATION)
|| eq.hasFlag(MiscType.F_SPONSON_TURRET))
|| eq.hasFlag(MiscType.F_EXTERNAL_STORES_HARDPOINT)
|| eq.hasFlag(MiscType.F_BASIC_FIRECONTROL)
|| eq.hasFlag(MiscType.F_ADVANCED_FIRECONTROL)) {
return false;
}

if (UnitUtil.isHeatSink(eq)) {
return false;
}

return true;
}

private PrintUtil() { }

/**
* simple method to let us know if eq should be printed on the weapons and
* equipment section of the Record sheet.
*
* @param eq The equipment to test
* @return True when it should appear on the record sheet
*/
public static boolean isPrintableBAEquipment(EquipmentType eq) {
if (UnitUtil.isArmorOrStructure(eq)) {
return false;
}

if (UnitUtil.isJumpJet(eq)) {
return false;
}

if ((eq instanceof MiscType)
&& ((eq.hasFlag(MiscType.F_AP_MOUNT) && !eq.hasFlag(MiscType.F_BA_MANIPULATOR))
|| eq.hasFlag(MiscType.F_FIRE_RESISTANT)
|| eq.hasFlag(MiscType.F_STEALTH)
|| eq.hasFlag(MiscType.F_ARTEMIS)
|| eq.hasFlag(MiscType.F_ARTEMIS_V)
|| eq.hasFlag(MiscType.F_APOLLO)
|| eq.hasFlag(MiscType.F_HARJEL)
|| eq.hasFlag(MiscType.F_MASS)
|| eq.hasFlag(MiscType.F_DETACHABLE_WEAPON_PACK))) {
return false;
}

if (UnitUtil.isHeatSink(eq)) {
return false;
}

if ((eq instanceof LegAttack) || (eq instanceof SwarmAttack)
|| (eq instanceof StopSwarmAttack)
|| (eq instanceof InfantryRifleAutoRifleWeapon)
|| (eq instanceof SwarmWeaponAttack)) {
return false;
}

return true;
}
}
132 changes: 132 additions & 0 deletions megameklab/src/megameklab/ui/EquipmentToolTip.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright (c) 2024 - The MegaMek Team. All Rights Reserved.
*
* This file is part of MegaMek.
*
* MegaMek is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MegaMek is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MegaMek. If not, see <http://www.gnu.org/licenses/>.
*/
package megameklab.ui;

import megamek.common.*;
import megamek.common.verifier.TestEntity;
import megamek.common.weapons.infantry.InfantryWeapon;
import megamek.common.weapons.missiles.MMLWeapon;
import megamek.common.weapons.srms.SRMWeapon;
import megamek.common.weapons.srms.SRTWeapon;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

public final class EquipmentToolTip {

public static String getToolTipInfo(Entity unit, Mounted eq) {
DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols();
unusualSymbols.setDecimalSeparator('.');
unusualSymbols.setGroupingSeparator(',');
DecimalFormat myFormatter = new DecimalFormat("#,##0", unusualSymbols);
StringBuilder sb = new StringBuilder("<HTML>");
sb.append(eq.getName());
if (eq.getType() instanceof AmmoType) {
sb.append(" (").append(eq.getBaseShotsLeft()).append(" shot");
sb.append((eq.getBaseShotsLeft() == 1) ? ")" : "s)");
}
if ((eq.getType().hasFlag(MiscType.F_DETACHABLE_WEAPON_PACK)
|| eq.getType().hasFlag(MiscType.F_AP_MOUNT))
&& (eq.getLinked() != null)) {
sb.append(" (attached ").append(eq.getLinked().getName()).append(")");
}
if (eq.isSquadSupportWeapon()) {
sb.append(" (squad support weapon)");
}
if (eq.getType() instanceof InfantryWeapon) {
sb.append("<br>Damage/Trooper: ");
double infDamage = ((InfantryWeapon) eq.getType()).getInfantryDamage();
sb.append(infDamage);
sb.append("<br>Range Class: ");
sb.append(((InfantryWeapon) eq.getType()).getInfantryRange());
} else {
sb.append("<br>Crits: ");
sb.append(eq.getCriticals());
sb.append("<br>Mass: ");
if (TestEntity.usesKgStandard(unit)) {
sb.append(Math.round(eq.getTonnage() * 1000));
sb.append(" Kg");
} else {
sb.append(eq.getTonnage());
sb.append(" tons");
}

if (eq.getType() instanceof WeaponType) {
sb.append("<br>Heat: ");
sb.append(eq.getType().getHeat());
sb.append("<br>Maximum Damage: ");
sb.append(getWeaponDamageInfo((WeaponType) eq.getType()));
}
}
sb.append("<Br>Cost: ");

double cost = eq.getType().getCost(unit, false, eq.getLocation());

sb.append(myFormatter.format(cost));
sb.append(" CBills");

if (eq.isRearMounted()) {
sb.append("<br>Rear Facing");
}
if (eq.isMechTurretMounted()) {
sb.append("<br>Turret mounted");
}
if (eq.isArmored()) {
sb.append("<br>Armored");
}
if ((unit instanceof BattleArmor)
&& eq.getType().hasFlag(WeaponType.F_INF_SUPPORT)) {
sb.append("<br>* Infantry support weapons must be held in an " +
"Armored Glove");
} else if ((unit instanceof BattleArmor)
&& eq.getType().hasFlag(WeaponType.F_INFANTRY)) {
sb.append("<br>* Infantry weapons must be mounted in AP Mounts");
}

sb.append("</html>");
return sb.toString();
}

private static String getWeaponDamageInfo(WeaponType wType) {
if (wType.getDamage() == WeaponType.DAMAGE_BY_CLUSTERTABLE) {
int perMissile = 1;
if ((wType instanceof SRMWeapon) || (wType instanceof SRTWeapon) ||(wType instanceof MMLWeapon)) {
perMissile = 2;
}
return Integer.toString(wType.getRackSize() * perMissile);
} else if (wType.getDamage() == WeaponType.DAMAGE_VARIABLE) {
return Integer.toString(wType.getDamage(1));
} else if (wType.getDamage() == WeaponType.DAMAGE_SPECIAL) {
return "Special";
} else if (wType.getDamage() == WeaponType.DAMAGE_ARTILLERY) {
return Integer.toString(wType.getRackSize());
} else {
int damage = wType.getDamage();
if (wType.getAmmoType() == AmmoType.T_AC_ROTARY) {
damage *= 6;
} else if ((wType.getAmmoType() == AmmoType.T_AC_ULTRA)
|| (wType.getAmmoType() == AmmoType.T_AC_ULTRA_THB)) {
damage *= 2;
}
return Integer.toString(damage);
}
}

private EquipmentToolTip() { }
}
Loading
Loading