Search in sources :

Example 16 with LocationFullException

use of megamek.common.LocationFullException in project megameklab by MegaMek.

the class CriticalTransferHandler method addEquipmentMech.

/**
 * @param mech
 * @param eq
 * @return
 */
private boolean addEquipmentMech(Mech mech, Mounted eq, int slotNumber) throws LocationFullException {
    int totalCrits = UnitUtil.getCritsUsed(getUnit(), eq.getType());
    // How much space we have in the selected location
    int primaryLocSpace = UnitUtil.getContiguousNumberOfCrits(getUnit(), location, slotNumber);
    if ((eq.getType().isSpreadable() || eq.isSplitable()) && (totalCrits > 1)) {
        int critsUsed = 0;
        int primaryLocation = location;
        int nextLocation = getUnit().getTransferLocation(location);
        // Determine if we should spread equipment over multiple locations
        if ((eq.getType().getCriticals(getUnit()) > primaryLocSpace) && !((eq.getType() instanceof MiscType) && eq.getType().hasFlag(MiscType.F_TARGCOMP)) && !(getUnit() instanceof LandAirMech)) {
            if (location == Mech.LOC_RT) {
                String[] locations = { "Center Torso", "Right Leg", "Right Arm" };
                JComboBox<String> combo = new JComboBox<String>(locations);
                JOptionPane jop = new JOptionPane(combo, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
                JDialog dlg = jop.createDialog("Select secondary location.");
                combo.grabFocus();
                combo.getEditor().selectAll();
                dlg.setVisible(true);
                int value = ((Integer) jop.getValue()).intValue();
                if (value == JOptionPane.CANCEL_OPTION) {
                    return false;
                }
                if (combo.getSelectedIndex() == 1) {
                    nextLocation = Mech.LOC_RLEG;
                } else if (combo.getSelectedIndex() == 2) {
                    nextLocation = Mech.LOC_RARM;
                }
            } else if (location == Mech.LOC_LT) {
                String[] locations = { "Center Torso", "Left Leg", "Left Arm" };
                JComboBox<String> combo = new JComboBox<String>(locations);
                JOptionPane jop = new JOptionPane(combo, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
                JDialog dlg = jop.createDialog("Select secondary location.");
                combo.grabFocus();
                combo.getEditor().selectAll();
                dlg.setVisible(true);
                int value = ((Integer) jop.getValue()).intValue();
                if (value == JOptionPane.CANCEL_OPTION) {
                    return false;
                }
                if (combo.getSelectedIndex() == 1) {
                    nextLocation = Mech.LOC_LLEG;
                } else if (combo.getSelectedIndex() == 2) {
                    nextLocation = Mech.LOC_LARM;
                }
            } else if (location == Mech.LOC_CT) {
                String[] locations = { "Left Torso", "Right Torso" };
                JComboBox<String> combo = new JComboBox<String>(locations);
                JOptionPane jop = new JOptionPane(combo, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
                JDialog dlg = jop.createDialog(null, "Select secondary location.");
                combo.grabFocus();
                combo.getEditor().selectAll();
                dlg.setVisible(true);
                int value = ((Integer) jop.getValue()).intValue();
                if (value == JOptionPane.CANCEL_OPTION) {
                    return false;
                }
                if (combo.getSelectedIndex() == 1) {
                    nextLocation = Mech.LOC_RT;
                } else {
                    nextLocation = Mech.LOC_LT;
                }
            }
        }
        // Determine how much usable space we have in both locations
        int secondarySpace = UnitUtil.getHighestContinuousNumberOfCrits(getUnit(), nextLocation);
        // Check for available space
        if ((primaryLocSpace < totalCrits) && ((nextLocation == Entity.LOC_DESTROYED) || ((primaryLocSpace + secondarySpace) < totalCrits))) {
            throw new LocationFullException(eq.getName() + " does not fit there in " + getUnit().getLocationAbbr(location) + " on " + getUnit().getDisplayName());
        }
        int currLoc = location;
        for (; critsUsed < totalCrits; critsUsed++) {
            mech.addEquipment(eq, currLoc, false, slotNumber);
            slotNumber = (slotNumber + 1) % mech.getNumberOfCriticals(currLoc);
            primaryLocSpace--;
            if (primaryLocSpace == 0) {
                slotNumber = 0;
                currLoc = nextLocation;
                totalCrits -= critsUsed;
                critsUsed = 0;
            }
        }
        int secondary = Entity.LOC_NONE;
        if ((primaryLocSpace <= 0) && (slotNumber > 0)) {
            secondary = nextLocation;
        }
        changeMountStatus(eq, primaryLocation, secondary, false);
    } else if (primaryLocSpace >= totalCrits) {
        if ((eq.getType() instanceof WeaponType) && eq.getType().hasFlag(WeaponType.F_VGL)) {
            String[] facings;
            if (location == Mech.LOC_LT) {
                facings = new String[4];
                facings[0] = "Front";
                facings[1] = "Front-Left";
                facings[2] = "Rear-Left";
                facings[3] = "Rear";
            } else if (location == Mech.LOC_RT) {
                facings = new String[4];
                facings[0] = "Front";
                facings[1] = "Front-Right";
                facings[2] = "Rear-Right";
                facings[3] = "Rear";
            } else if (location == Mech.LOC_CT) {
                facings = new String[2];
                facings[0] = "Front";
                facings[1] = "Rear";
            } else {
                JOptionPane.showMessageDialog(null, "VGL must be placed in torso location!", "Invalid location", JOptionPane.WARNING_MESSAGE);
                return false;
            }
            String facing = (String) JOptionPane.showInputDialog(null, "Please choose the facing of the VGL", "Choose Facing", JOptionPane.QUESTION_MESSAGE, null, facings, facings[0]);
            if (facing == null) {
                return false;
            }
            mech.addEquipment(eq, location, false, slotNumber);
            if (facing.equals("Front-Left")) {
                eq.setFacing(5);
            } else if (facing.equals("Front-Right")) {
                eq.setFacing(1);
            } else if (facing.equals("Rear-Right")) {
                eq.setFacing(2);
            } else if (facing.equals("Rear-Left")) {
                eq.setFacing(4);
            } else if (facing.equals("Rear")) {
                eq.setFacing(3);
                UnitUtil.changeMountStatus(getUnit(), eq, location, -1, true);
            }
        } else {
            mech.addEquipment(eq, location, false, slotNumber);
        }
        changeMountStatus(eq, location, false);
    } else {
        throw new LocationFullException(eq.getName() + " does not fit there in " + getUnit().getLocationAbbr(location) + " on " + getUnit().getDisplayName());
    }
    return true;
}
Also used : LocationFullException(megamek.common.LocationFullException) JComboBox(javax.swing.JComboBox) MiscType(megamek.common.MiscType) WeaponType(megamek.common.WeaponType) JOptionPane(javax.swing.JOptionPane) JDialog(javax.swing.JDialog) LandAirMech(megamek.common.LandAirMech)

Example 17 with LocationFullException

use of megamek.common.LocationFullException in project megameklab by MegaMek.

the class UnitUtil method createSpreadMounts.

/**
 * create a Mounted and corresponding CriticalSlots for the passed in
 * <code>EquipmentType</code> on the passed in <code>Mech</code>
 *
 * @param unit
 * @param equip
 * @return
 */
public static Mounted createSpreadMounts(Mech unit, EquipmentType equip) {
    final String METHOD_NAME = "createSpreadMounts(Mech, EquipmentType)";
    // how many non-spreadable contiguous blocks of crits?
    int blocks = 0;
    boolean isMisc = equip instanceof MiscType;
    blocks = equip.getCriticals(unit);
    List<Integer> locations = new ArrayList<Integer>();
    if (isMisc) {
        if ((equip.hasFlag(MiscType.F_INDUSTRIAL_TSM) || equip.hasFlag(MiscType.F_TSM))) {
            // all crits user placeable
            for (int i = 0; i < equip.getCriticals(unit); i++) {
                locations.add(Entity.LOC_NONE);
            }
        } else if (equip.hasFlag(MiscType.F_ENVIRONMENTAL_SEALING)) {
            // 1 crit in each location
            for (int i = 0; i < unit.locations(); i++) {
                locations.add(i);
            }
        } else if (equip.hasFlag(MiscType.F_STEALTH)) {
            // 2 in arms, legs, side torsos
            locations.add(Mech.LOC_LLEG);
            locations.add(Mech.LOC_RLEG);
            locations.add(Mech.LOC_LARM);
            locations.add(Mech.LOC_RARM);
            locations.add(Mech.LOC_LT);
            locations.add(Mech.LOC_RT);
            blocks = 6;
            // Need to account for the center leg
            if (unit instanceof TripodMech) {
                locations.add(Mech.LOC_CLEG);
                blocks++;
            }
        } else if (equip.hasFlag(MiscType.F_SCM)) {
            // 1 in arms, legs, side torsos
            locations.add(Mech.LOC_LLEG);
            locations.add(Mech.LOC_RLEG);
            locations.add(Mech.LOC_LARM);
            locations.add(Mech.LOC_RARM);
            locations.add(Mech.LOC_LT);
            locations.add(Mech.LOC_RT);
            blocks = 6;
        } else if ((equip.hasFlag(MiscType.F_TRACKS) || equip.hasFlag(MiscType.F_TALON) || equip.hasFlag(MiscType.F_JUMP_BOOSTER))) {
            // 1 block in each leg
            locations.add(Mech.LOC_LLEG);
            locations.add(Mech.LOC_RLEG);
            if (unit instanceof QuadMech) {
                locations.add(Mech.LOC_LARM);
                locations.add(Mech.LOC_RARM);
            }
            blocks = (unit instanceof BipedMech ? 2 : 4);
            // Need to account for the center leg
            if (unit instanceof TripodMech) {
                locations.add(Mech.LOC_CLEG);
                blocks = 3;
            }
        } else if (equip.hasFlag(MiscType.F_PARTIAL_WING)) {
            // one block in each side torso
            locations.add(Mech.LOC_LT);
            locations.add(Mech.LOC_RT);
            blocks = 2;
        } else if ((equip.hasFlag(MiscType.F_VOIDSIG) || equip.hasFlag(MiscType.F_NULLSIG) || equip.hasFlag(MiscType.F_BLUE_SHIELD))) {
            // Need to account for the center leg
            if (unit instanceof TripodMech) {
                blocks++;
            }
            // 1 crit in each location, except the head
            for (int i = Mech.LOC_CT; i < unit.locations(); i++) {
                locations.add(i);
            }
        } else if (equip.hasFlag(MiscType.F_CHAMELEON_SHIELD)) {
            // Need to account for the center leg
            if (unit instanceof TripodMech) {
                blocks++;
            }
            // 1 crit in each location except head and CT
            for (int i = Mech.LOC_RT; i < unit.locations(); i++) {
                locations.add(i);
            }
        }
    }
    boolean firstBlock = true;
    Mounted mount = new Mounted(unit, equip);
    for (; blocks > 0; blocks--) {
        // how many crits per block?
        int crits = UnitUtil.getCritsUsed(unit, equip);
        for (int i = 0; i < crits; i++) {
            try {
                if (firstBlock || (locations.get(0) == Entity.LOC_NONE)) {
                    // create only one mount per equipment, for BV and stuff
                    addMounted(unit, mount, locations.get(0), false);
                    if (firstBlock) {
                        firstBlock = false;
                    }
                    if (locations.get(0) == Entity.LOC_NONE) {
                        // only user-placable spread stuff gets location
                        // none
                        // for those, we need to create a mount for each
                        // crit,
                        // otherwise we can't correctly let the user place
                        // them
                        // luckily, that only affects TSM, so BV works out
                        // correctly
                        mount = new Mounted(unit, equip);
                    }
                } else {
                    CriticalSlot cs = new CriticalSlot(mount);
                    if (!unit.addCritical(locations.get(0), cs)) {
                        UnitUtil.removeCriticals(unit, mount);
                        JOptionPane.showMessageDialog(null, "No room for equipment", mount.getName() + " does not fit into " + unit.getLocationName(locations.get(0)), JOptionPane.INFORMATION_MESSAGE);
                        unit.getMisc().remove(mount);
                        unit.getEquipment().remove(mount);
                        return null;
                    }
                }
            } catch (LocationFullException lfe) {
                getLogger().log(UnitUtil.class, METHOD_NAME, lfe);
                JOptionPane.showMessageDialog(null, lfe.getMessage(), mount.getName() + " does not fit into " + unit.getLocationName(locations.get(0)), JOptionPane.INFORMATION_MESSAGE);
                unit.getMisc().remove(mount);
                unit.getEquipment().remove(mount);
                return null;
            }
        }
        locations.remove(0);
    }
    return mount;
}
Also used : TripodMech(megamek.common.TripodMech) QuadMech(megamek.common.QuadMech) CriticalSlot(megamek.common.CriticalSlot) MiscType(megamek.common.MiscType) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) LocationFullException(megamek.common.LocationFullException) Mounted(megamek.common.Mounted) BipedMech(megamek.common.BipedMech)

Example 18 with LocationFullException

use of megamek.common.LocationFullException in project megameklab by MegaMek.

the class MainUI method createNewUnit.

@Override
public void createNewUnit(long entityType, boolean isPrimitive, boolean isIndustrial, Entity oldEntity) {
    setEntity(new Infantry());
    getEntity().setYear(3145);
    getEntity().setTechLevel(TechConstants.T_IS_TW_NON_BOX);
    getEntity().setArmorTechLevel(TechConstants.T_IS_TW_NON_BOX);
    ((Infantry) getEntity()).setSquadN(4);
    ((Infantry) getEntity()).setSquadSize(7);
    ((Infantry) getEntity()).setPrimaryWeapon((InfantryWeapon) EquipmentType.get("InfantryAssaultRifle"));
    try {
        getEntity().addEquipment(EquipmentType.get("InfantryAssaultRifle"), Infantry.LOC_INFANTRY);
    } catch (LocationFullException ex) {
    }
    getEntity().autoSetInternal();
    getEntity().setChassis("New");
    getEntity().setModel("Infantry");
}
Also used : Infantry(megamek.common.Infantry) LocationFullException(megamek.common.LocationFullException)

Example 19 with LocationFullException

use of megamek.common.LocationFullException in project megameklab by MegaMek.

the class EquipmentTab method addEquipment.

private void addEquipment(EquipmentType equip) {
    boolean success = false;
    Mounted mount = null;
    boolean isMisc = equip instanceof MiscType;
    if (isMisc && equip.hasFlag(MiscType.F_TARGCOMP)) {
        if (!UnitUtil.hasTargComp(getMech())) {
            mount = UnitUtil.updateTC(getMech(), equip);
            success = mount != null;
        }
    } else if (isMisc && UnitUtil.isFixedLocationSpreadEquipment(equip)) {
        mount = UnitUtil.createSpreadMounts(getMech(), equip);
        success = mount != null;
    } else {
        try {
            mount = new Mounted(getMech(), equip);
            getMech().addEquipment(mount, Entity.LOC_NONE, false);
            success = true;
        } catch (LocationFullException lfe) {
        // this can't happen, we add to Entity.LOC_NONE
        }
    }
    if (success) {
        equipmentList.addCrit(mount);
    }
}
Also used : LocationFullException(megamek.common.LocationFullException) Mounted(megamek.common.Mounted) MiscType(megamek.common.MiscType)

Example 20 with LocationFullException

use of megamek.common.LocationFullException in project megameklab by MegaMek.

the class StructureTab method patchworkChanged.

@Override
public void patchworkChanged(int location, EquipmentType armor) {
    UnitUtil.resetArmor(getAero(), location);
    // TODO: move this construction data out of the ui
    int crits = 0;
    switch(EquipmentType.getArmorType(armor)) {
        case EquipmentType.T_ARMOR_STEALTH_VEHICLE:
        case EquipmentType.T_ARMOR_LIGHT_FERRO:
        case EquipmentType.T_ARMOR_FERRO_FIBROUS:
        case EquipmentType.T_ARMOR_FERRO_FIBROUS_PROTO:
        case EquipmentType.T_ARMOR_FERRO_LAMELLOR:
        case EquipmentType.T_ARMOR_REFLECTIVE:
        case EquipmentType.T_ARMOR_REACTIVE:
            crits = 1;
            break;
        case EquipmentType.T_ARMOR_HEAVY_FERRO:
            crits = 2;
            break;
    }
    if (getAero().getEmptyCriticals(location) < crits) {
        JOptionPane.showMessageDialog(null, armor.getName() + " does not fit in location " + getAero().getLocationName(location) + ". Resetting to Standard Armor in this location.", "Error", JOptionPane.INFORMATION_MESSAGE);
    } else {
        getAero().setArmorType(EquipmentType.getArmorType(armor), location);
        getAero().setArmorTechLevel(armor.getTechLevel(getTechManager().getGameYear(), armor.isClan()));
        for (; crits > 0; crits--) {
            try {
                getAero().addEquipment(new Mounted(getAero(), armor), location, false);
            } catch (LocationFullException ex) {
            }
        }
    }
    panArmor.refresh();
    panArmorAllocation.setFromEntity(getAero());
    refresh.refreshBuild();
    refresh.refreshPreview();
    refresh.refreshSummary();
    refresh.refreshStatus();
}
Also used : LocationFullException(megamek.common.LocationFullException) Mounted(megamek.common.Mounted)

Aggregations

LocationFullException (megamek.common.LocationFullException)23 Mounted (megamek.common.Mounted)21 MiscType (megamek.common.MiscType)9 AmmoType (megamek.common.AmmoType)5 CriticalSlot (megamek.common.CriticalSlot)5 EquipmentType (megamek.common.EquipmentType)5 Dimension (java.awt.Dimension)3 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 JComboBox (javax.swing.JComboBox)3 JLabel (javax.swing.JLabel)3 BattleArmor (megamek.common.BattleArmor)3 WeaponType (megamek.common.WeaponType)3 GridBagConstraints (java.awt.GridBagConstraints)2 GridBagLayout (java.awt.GridBagLayout)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 BorderFactory (javax.swing.BorderFactory)2