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

Infantry autoload tonnage fix #1452

Merged
merged 3 commits into from
Feb 6, 2020
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 MekHQ/src/mekhq/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -1675,7 +1675,7 @@ public static int selectBestBayFor(Entity cargo, Entity transport) {
for (Bay b: transport.getTransportBays()) {
if (b instanceof InfantryBay && b.canLoad(cargo)) {
//Update bay tonnage based on platoon/squad weight
b.setCurrentSpace(cargo.getWeight());
b.setCurrentSpace(b.spaceForUnit(cargo));
return b.getBayNumber();
}
}
Expand Down
31 changes: 28 additions & 3 deletions MekHQ/src/mekhq/campaign/unit/Unit.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.stream.Stream;

import megamek.common.*;
import megamek.common.InfantryBay.PlatoonType;
import mekhq.campaign.finances.Money;
import mekhq.campaign.log.ServiceLogger;
import mekhq.campaign.parts.*;
Expand Down Expand Up @@ -1452,7 +1453,7 @@ public double getInfantryCapacity() {
double bays = 0;
for (Bay b : getEntity().getTransportBays()) {
if (b instanceof InfantryBay) {
bays += b.getCapacity() / ((InfantryBay) b).getPlatoonType().getWeight();
bays += b.getCapacity();
}
}
return bays;
Expand Down Expand Up @@ -1559,13 +1560,32 @@ public void setProtoCapacity(double bays) {
public void loadTransportShip(Vector<Unit> units) {
for (Unit u : units) {
int unitType = u.getEntity().getUnitType();
double unitWeight = u.getEntity().getWeight();
double unitWeight;
if (u.getEntity().getUnitType() == UnitType.INFANTRY) {
unitWeight = calcInfantryBayWeight(u.getEntity());
} else {
unitWeight = u.getEntity().getWeight();
}
int bayNumber = Utilities.selectBestBayFor(u.getEntity(), getEntity());
u.setTransportShipId(getId(),bayNumber);
addTransportedUnit(u.getId());
updateBayCapacity(unitType, unitWeight, false, bayNumber);
}
}

/**
* Calculates transport bay space required by an infantry platoon,
* which is not the same as the flat weight of that platoon
* @param unit The Entity that we need the weight for
*/
public double calcInfantryBayWeight(Entity unit) {
PlatoonType type = PlatoonType.getPlatoonType(unit);
if ((unit instanceof Infantry) && (type == PlatoonType.MECHANIZED)) {
return type.getWeight() * ((Infantry)unit).getSquadN();
} else {
return type.getWeight();
}
}

/**
* Bay unloading utility used when removing units from bay-equipped transport units
Expand All @@ -1574,7 +1594,12 @@ public void loadTransportShip(Vector<Unit> units) {
*/
public void unloadFromTransportShip(Unit u) {
int unitType = u.getEntity().getUnitType();
double unitWeight = u.getEntity().getWeight();
double unitWeight;
if (u.getEntity().getUnitType() == UnitType.INFANTRY) {
unitWeight = calcInfantryBayWeight(u.getEntity());
} else {
unitWeight = u.getEntity().getWeight();
}
for (UUID id : u.getTransportShipId().keySet()) {
int bayNumber = u.getTransportShipId().get(id);
updateBayCapacity(unitType, unitWeight, true, bayNumber);
Expand Down
107 changes: 102 additions & 5 deletions MekHQ/src/mekhq/gui/adapter/TOEMouseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ public TOEMouseAdapter(CampaignGUI gui) {

private static final String ASSIGN_FORCE_TRN_TITLE = "Assign Force to Transport Ship";
private static final String UNASSIGN_FORCE_TRN_TITLE = "Unassign Force from Transport Ship";
private static final String MECH_CARRIERS = "Mech Transports";
private static final String PROTOMECH_CARRIERS = "ProtoMech Transports";
private static final String LVEE_CARRIERS = "Light Vehicle Transports";
private static final String HVEE_CARRIERS = "Heavy Vehicle Transports";
private static final String SHVEE_CARRIERS = "SuperHeavy Vehicle Transports";
private static final String BA_CARRIERS = "Battle Armor Transports";
private static final String INFANTRY_CARRIERS = "Infantry Transports";
private static final String ASF_CARRIERS = "Aerospace Fighter Transports";
private static final String SC_CARRIERS = "Small Craft Transports";

@Override
public void actionPerformed(ActionEvent action) {
Expand Down Expand Up @@ -1149,6 +1158,16 @@ private void maybeShowPopup(MouseEvent e) {
//This checks to see if the ship is in a basic state that can accept units.
//Capacity gets checked once the action is submitted.
menu = new JMenu(TOEMouseAdapter.ASSIGN_FORCE_TRN_TITLE);
// Add submenus for different types of transports
JMenu m_trn = new JMenu(TOEMouseAdapter.MECH_CARRIERS);
JMenu pm_trn = new JMenu(TOEMouseAdapter.PROTOMECH_CARRIERS);
JMenu lv_trn = new JMenu(TOEMouseAdapter.LVEE_CARRIERS);
JMenu hv_trn = new JMenu(TOEMouseAdapter.HVEE_CARRIERS);
JMenu shv_trn = new JMenu(TOEMouseAdapter.SHVEE_CARRIERS);
JMenu ba_trn = new JMenu(TOEMouseAdapter.BA_CARRIERS);
JMenu i_trn = new JMenu(TOEMouseAdapter.INFANTRY_CARRIERS);
JMenu a_trn = new JMenu(TOEMouseAdapter.ASF_CARRIERS);
JMenu sc_trn = new JMenu(TOEMouseAdapter.SC_CARRIERS);
if (unitsInForces.size() > 0) {
Unit unit = unitsInForces.get(0);
String unitIds = "" + unit.getId().toString();
Expand All @@ -1168,13 +1187,73 @@ private void maybeShowPopup(MouseEvent e) {
if (ship == null || ship.isSalvage() || ship.getCommander() == null) {
continue;
}
menuItem = new JMenuItem(ship.getName());
menuItem.setActionCommand(TOEMouseAdapter.COMMAND_ASSIGN_TO_SHIP + id + "|" + unitIds);
menuItem.addActionListener(this);
menuItem.setEnabled(true);
menu.add(menuItem);
//Now, add this ship to the appropriate submenu(s). Most transports will fit into multiple
//categories
if (ship.getASFCapacity() > 0) {
a_trn.add(transportMenuItem(ship.getName(),id,unitIds));
a_trn.setEnabled(true);
}
if (ship.getBattleArmorCapacity() > 0) {
ba_trn.add(transportMenuItem(ship.getName(),id,unitIds));
ba_trn.setEnabled(true);
}
if (ship.getInfantryCapacity() > 0) {
i_trn.add(transportMenuItem(ship.getName(),id,unitIds));
i_trn.setEnabled(true);
}
if (ship.getMechCapacity() > 0) {
m_trn.add(transportMenuItem(ship.getName(),id,unitIds));
m_trn.setEnabled(true);
}
if (ship.getProtomechCapacity() > 0) {
pm_trn.add(transportMenuItem(ship.getName(),id,unitIds));
pm_trn.setEnabled(true);
}
if (ship.getSmallCraftCapacity() > 0) {
sc_trn.add(transportMenuItem(ship.getName(),id,unitIds));
sc_trn.setEnabled(true);
}
if (ship.getLightVehicleCapacity() > 0) {
lv_trn.add(transportMenuItem(ship.getName(),id,unitIds));
lv_trn.setEnabled(true);
}
if (ship.getHeavyVehicleCapacity() > 0) {
hv_trn.add(transportMenuItem(ship.getName(),id,unitIds));
hv_trn.setEnabled(true);
}
if (ship.getSuperHeavyVehicleCapacity() > 0) {
shv_trn.add(transportMenuItem(ship.getName(),id,unitIds));
shv_trn.setEnabled(true);
}
}
}
if (a_trn.getMenuComponentCount() > 0 || a_trn.getItemCount() > 0) {
menu.add(a_trn);
}
if (ba_trn.getMenuComponentCount() > 0 || ba_trn.getItemCount() > 0) {
menu.add(ba_trn);
}
if (i_trn.getMenuComponentCount() > 0 || i_trn.getItemCount() > 0) {
menu.add(i_trn);
}
if (m_trn.getMenuComponentCount() > 0 || m_trn.getItemCount() > 0) {
menu.add(m_trn);
}
if (pm_trn.getMenuComponentCount() > 0 || pm_trn.getItemCount() > 0) {
menu.add(pm_trn);
}
if (sc_trn.getMenuComponentCount() > 0 || sc_trn.getItemCount() > 0) {
menu.add(sc_trn);
}
if (lv_trn.getMenuComponentCount() > 0 || lv_trn.getItemCount() > 0) {
menu.add(lv_trn);
}
if (hv_trn.getMenuComponentCount() > 0 || hv_trn.getItemCount() > 0) {
menu.add(hv_trn);
}
if (shv_trn.getMenuComponentCount() > 0 || shv_trn.getItemCount() > 0) {
menu.add(shv_trn);
}
if (menu.getMenuComponentCount() > 0 || menu.getItemCount() > 0) {
popup.add(menu);
}
Expand Down Expand Up @@ -1540,4 +1619,22 @@ private void clearTransportAssignment(Unit currentUnit) {
}
}
}

/**
* Worker function that creates a new instance of a JMenuItem for a set of transport ship characteristics
* Used to have a single ship appear on multiple menu entries defined by type of unit transported
*
* @param shipName String name of this ship.
* @param shipId
* @param unitIds String of units delimited by | used to fill out actionPerformed(ActionEvent)
*/

private JMenuItem transportMenuItem(String shipName, UUID shipId, String unitIds) {
JMenuItem menuItem = new JMenuItem(shipName);
menuItem.setActionCommand(TOEMouseAdapter.COMMAND_ASSIGN_TO_SHIP + shipId + "|" + unitIds);
menuItem.addActionListener(this);
menuItem.setEnabled(true);

return menuItem;
}
}