Search in sources :

Example 11 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(getTank())) {
            mount = UnitUtil.updateTC(getTank(), equip);
            success = mount != null;
        }
    } else {
        try {
            mount = new Mounted(getTank(), equip);
            int loc = Entity.LOC_NONE;
            if (isMisc && equip.hasFlag(MiscType.F_MAST_MOUNT)) {
                loc = VTOL.LOC_ROTOR;
            }
            getTank().addEquipment(mount, loc, 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 12 with LocationFullException

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

the class AeroBayTransferHandler method importData.

@Override
public boolean importData(TransferSupport support) {
    if (!support.isDrop()) {
        return false;
    }
    // Fields are equipmentNum, node child index, bay child index
    String[] source = null;
    List<Mounted> eqList = new ArrayList<>();
    try {
        source = ((String) support.getTransferable().getTransferData(DataFlavor.stringFlavor)).split(":");
        for (String field : source[0].split(",")) {
            int eqNum = Integer.parseInt(field);
            Mounted m = eSource.getEntity().getEquipment(eqNum);
            if (null != m) {
                eqList.add(m);
            }
        }
    } catch (Exception ex) {
        MegaMekLab.getLogger().log(AeroBayTransferHandler.class, // $NON-NLS-1$
        "importData(TransferSupport)", ex);
        return false;
    }
    if (eqList.isEmpty()) {
        return false;
    }
    if ((support.getComponent() instanceof BayWeaponCriticalTree)) {
        final BayWeaponCriticalTree tree = (BayWeaponCriticalTree) support.getComponent();
        if (eSource.getEntity().usesWeaponBays() && (eqList.size() == 1)) {
            // If it's a bay we move it and its entire contents. Otherwise we find the bay that was
            // dropped on and add it there. A weapon dropped on an illegal bay will create a new one
            // and non-bay equipment will be added at the top level regardless of the drop location.
            // Non-weapon bay equipment cannot be dropped on an illegal bay.
            final Mounted mount = eqList.get(0);
            if (mount.getType() instanceof BayWeapon) {
                tree.addBay(mount);
            } else if ((mount.getType() instanceof AmmoType) && (support.getUserDropAction() == AMMO_SINGLE)) {
                // Default action for ammo is to move a single slot. Holding the ctrl key when dropping
                // will create a AMMO_ALL command, which adds all the ammo of the type.
                tree.addAmmo(mount, ((AmmoType) mount.getType()).getShots(), ((JTree.DropLocation) support.getDropLocation()).getPath());
            } else {
                tree.addToArc(mount, ((JTree.DropLocation) support.getDropLocation()).getPath());
            }
        } else {
            // Small craft don't use bays.
            tree.addToLocation(eqList);
        }
    } else {
        // Target is unallocated bay table.
        for (Mounted mount : eqList) {
            if (mount.getType() instanceof AmmoType) {
                AmmoType at = (AmmoType) mount.getType();
                // Check whether we are moving one of multiple slots.
                if ((support.getUserDropAction() == AMMO_SINGLE) && (mount.getUsableShotsLeft() > at.getShots())) {
                    mount.setShotsLeft(mount.getUsableShotsLeft() - at.getShots());
                }
                Mounted addMount = UnitUtil.findUnallocatedAmmo(eSource.getEntity(), at);
                if (null != addMount) {
                    if (support.getUserDropAction() == AMMO_SINGLE) {
                        addMount.setShotsLeft(addMount.getUsableShotsLeft() + at.getShots());
                    } else {
                        addMount.setShotsLeft(addMount.getUsableShotsLeft() + mount.getUsableShotsLeft());
                    }
                } else {
                    try {
                        Mounted m = eSource.getEntity().addEquipment(at, Entity.LOC_NONE);
                        if (support.getUserDropAction() == AMMO_ALL) {
                            m.setShotsLeft(mount.getUsableShotsLeft());
                        }
                    } catch (LocationFullException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            } else {
                List<Mounted> toRemove;
                if (mount.getType() instanceof BayWeapon) {
                    toRemove = new ArrayList<>();
                    for (Integer num : mount.getBayWeapons()) {
                        toRemove.add(eSource.getEntity().getEquipment(num));
                    }
                    for (Integer num : mount.getBayAmmo()) {
                        toRemove.add(eSource.getEntity().getEquipment(num));
                    }
                } else {
                    toRemove = Collections.singletonList(mount);
                }
                for (Mounted m : toRemove) {
                    if (m.getType() instanceof AmmoType) {
                        Mounted aMount = UnitUtil.findUnallocatedAmmo(eSource.getEntity(), m.getType());
                        if (null != aMount) {
                            aMount.setShotsLeft(aMount.getUsableShotsLeft() + m.getUsableShotsLeft());
                            m.setShotsLeft(0);
                            continue;
                        }
                    }
                    UnitUtil.removeCriticals(eSource.getEntity(), m);
                    UnitUtil.changeMountStatus(eSource.getEntity(), m, Entity.LOC_NONE, Entity.LOC_NONE, false);
                    if ((mount.getType() instanceof WeaponType) && (m.getLinkedBy() != null)) {
                        UnitUtil.removeCriticals(eSource.getEntity(), m.getLinkedBy());
                        UnitUtil.changeMountStatus(eSource.getEntity(), m.getLinkedBy(), Entity.LOC_NONE, Entity.LOC_NONE, false);
                        m.getLinkedBy().setLinked(null);
                        m.setLinkedBy(null);
                    }
                }
                UnitUtil.compactCriticals(eSource.getEntity());
            }
            if (mount.getType() instanceof BayWeapon) {
                mount.getBayWeapons().clear();
                mount.getBayAmmo().clear();
                UnitUtil.removeMounted(eSource.getEntity(), mount);
            }
        }
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) BayWeapon(megamek.common.weapons.bayweapons.BayWeapon) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) LocationFullException(megamek.common.LocationFullException) IOException(java.io.IOException) AmmoType(megamek.common.AmmoType) LocationFullException(megamek.common.LocationFullException) Mounted(megamek.common.Mounted) WeaponType(megamek.common.WeaponType)

Example 13 with LocationFullException

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

the class CriticalTransferHandler method importData.

/**
 */
@Override
public boolean importData(TransferSupport info) {
    if (!info.isDrop() || !((getUnit() instanceof Mech) || (getUnit() instanceof Aero) || (getUnit() instanceof BattleArmor))) {
        return false;
    }
    int trooper = 0;
    if (info.getComponent() instanceof DropTargetCriticalList) {
        DropTargetCriticalList<?> list = (DropTargetCriticalList<?>) info.getComponent();
        if (getUnit() instanceof BattleArmor) {
            String[] split = list.getName().split(":");
            if (split.length != 2) {
                return false;
            }
            location = Integer.parseInt(split[0]);
            trooper = Integer.parseInt(split[1]);
        } else {
            location = Integer.parseInt(list.getName());
        }
        Transferable t = info.getTransferable();
        int slotNumber = list.getDropLocation().getIndex();
        try {
            Mounted eq = getUnit().getEquipment(Integer.parseInt((String) t.getTransferData(DataFlavor.stringFlavor)));
            if (getUnit() instanceof BattleArmor) {
                if ((location == eq.getBaMountLoc()) && (trooper == eq.getLocation())) {
                    return false;
                }
            } else {
                // the criticals its mounted in
                if (eq.getLocation() != Entity.LOC_NONE || eq.getSecondLocation() != Entity.LOC_NONE) {
                    UnitUtil.removeCriticals(getUnit(), eq);
                    changeMountStatus(eq, Entity.LOC_NONE, false);
                } else {
                    eq.setOmniPodMounted(UnitUtil.canPodMount(getUnit(), eq));
                }
            }
            if (!UnitUtil.isValidLocation(getUnit(), eq.getType(), location)) {
                JOptionPane.showMessageDialog(null, eq.getName() + " can't be placed in " + getUnit().getLocationName(location) + "!", "Invalid Location", JOptionPane.INFORMATION_MESSAGE);
                return false;
            }
            if (getUnit() instanceof Aero) {
                return addEquipmentAero((Aero) getUnit(), eq);
            } else if (getUnit() instanceof Mech) {
                // superheavies can put 2 ammobins or heatsinks in one crit
                if ((getUnit() instanceof Mech) && ((Mech) getUnit()).isSuperHeavy()) {
                    CriticalSlot cs = getUnit().getCritical(location, slotNumber);
                    if ((cs != null) && (cs.getType() == CriticalSlot.TYPE_EQUIPMENT) && (cs.getMount2() == null)) {
                        EquipmentType etype = cs.getMount().getType();
                        EquipmentType etype2 = eq.getType();
                        boolean canDouble = false;
                        if ((etype instanceof AmmoType) && (etype2 instanceof AmmoType)) {
                            canDouble = (((AmmoType) etype).getAmmoType() == ((AmmoType) etype2).getAmmoType()) && (((AmmoType) etype).getRackSize() == ((AmmoType) etype2).getRackSize());
                        } else if (etype.equals(etype2) && UnitUtil.isHeatSink(etype)) {
                            canDouble = etype.getCriticals(getUnit()) == 1;
                        }
                        if (canDouble) {
                            cs.setMount2(eq);
                            changeMountStatus(eq, location, false);
                            return true;
                        }
                    }
                }
                return addEquipmentMech((Mech) getUnit(), eq, slotNumber);
            } else if (getUnit() instanceof BattleArmor) {
                return addEquipmentBA((BattleArmor) getUnit(), eq, trooper);
            }
        } catch (LocationFullException lfe) {
            JOptionPane.showMessageDialog(null, lfe.getMessage(), "Location Full", JOptionPane.INFORMATION_MESSAGE);
            return false;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return true;
    }
    return false;
}
Also used : CriticalSlot(megamek.common.CriticalSlot) Transferable(java.awt.datatransfer.Transferable) EquipmentType(megamek.common.EquipmentType) EntityLoadingException(megamek.common.loaders.EntityLoadingException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) LocationFullException(megamek.common.LocationFullException) IOException(java.io.IOException) AmmoType(megamek.common.AmmoType) LocationFullException(megamek.common.LocationFullException) Mounted(megamek.common.Mounted) Mech(megamek.common.Mech) LandAirMech(megamek.common.LandAirMech) Aero(megamek.common.Aero) TestAero(megamek.common.verifier.TestAero) BattleArmor(megamek.common.BattleArmor) TestBattleArmor(megamek.common.verifier.TestBattleArmor)

Example 14 with LocationFullException

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

the class CriticalTransferHandler method addEquipmentAero.

/**
 * @param aero
 * @return
 */
private boolean addEquipmentAero(Aero aero, Mounted eq) throws LocationFullException {
    if (eq.getType() instanceof WeaponType) {
        int[] availSpace = TestAero.availableSpace(aero);
        int[] weapCount = new int[aero.locations() - 1];
        for (Mounted m : aero.getWeaponList()) {
            if (m.getLocation() != Entity.LOC_NONE) {
                weapCount[m.getLocation()]++;
            }
        }
        if ((weapCount[location] + 1) > availSpace[location]) {
            throw new LocationFullException(eq.getName() + " does not fit in " + getUnit().getLocationAbbr(location) + " on " + getUnit().getDisplayName());
        } else {
            UnitUtil.addMounted(getUnit(), eq, location, false);
        }
    } else {
        UnitUtil.addMounted(getUnit(), eq, location, false);
    }
    changeMountStatus(eq, location, false);
    return true;
}
Also used : LocationFullException(megamek.common.LocationFullException) Mounted(megamek.common.Mounted) WeaponType(megamek.common.WeaponType)

Example 15 with LocationFullException

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

the class CriticalTransferHandler method exportDone.

@Override
public void exportDone(JComponent source, Transferable data, int action) {
    if (data == null) {
        return;
    }
    Mounted mounted = null;
    try {
        mounted = getUnit().getEquipment(Integer.parseInt((String) data.getTransferData(DataFlavor.stringFlavor)));
    } catch (NumberFormatException e) {
        e.printStackTrace();
    } catch (UnsupportedFlavorException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if ((source instanceof DropTargetCriticalList) && (mounted.getLocation() != Entity.LOC_NONE)) {
        DropTargetCriticalList<?> list = (DropTargetCriticalList<?>) source;
        int loc;
        if (getUnit() instanceof BattleArmor) {
            String[] split = list.getName().split(":");
            loc = Integer.parseInt(split[0]);
        } else {
            loc = Integer.parseInt(list.getName());
            if (loc == mounted.getLocation()) {
                return;
            }
        }
        int slot = list.getSelectedIndex();
        int startSlot = slot;
        mounted = list.getMounted();
        if (mounted == null) {
            return;
        }
        if (UnitUtil.isFixedLocationSpreadEquipment(mounted.getType())) {
            return;
        }
        while (slot > 0) {
            slot--;
            CriticalSlot cs = getUnit().getCritical(loc, slot);
            if ((cs != null) && (cs.getType() == CriticalSlot.TYPE_EQUIPMENT) && cs.getMount().equals(mounted)) {
                startSlot = slot;
            }
        }
        if (!(getUnit() instanceof BattleArmor)) {
            for (int i = startSlot; i < (startSlot + UnitUtil.getCritsUsed(getUnit(), mounted.getType())); i++) {
                getUnit().setCritical(loc, i, null);
            }
        }
        Mounted linkedBy = mounted.getLinkedBy();
        if (linkedBy != null && !(getUnit() instanceof BattleArmor)) {
            UnitUtil.removeCriticals(getUnit(), linkedBy);
            try {
                UnitUtil.addMounted(getUnit(), linkedBy, mounted.getLocation(), linkedBy.isRearMounted());
            } catch (LocationFullException e) {
                UnitUtil.changeMountStatus(getUnit(), linkedBy, Entity.LOC_NONE, Entity.LOC_NONE, false);
                linkedBy.setLinked(null);
                mounted.setLinkedBy(null);
            }
        }
        // UnitUtil.compactCriticals(unit);
        refresh.refreshBuild();
    }
}
Also used : LocationFullException(megamek.common.LocationFullException) Mounted(megamek.common.Mounted) CriticalSlot(megamek.common.CriticalSlot) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) BattleArmor(megamek.common.BattleArmor) TestBattleArmor(megamek.common.verifier.TestBattleArmor)

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