Search in sources :

Example 31 with AmmoType

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

the class BayWeaponCriticalTree method removeEquipment.

/**
 * Removes equipment node.
 *
 * @param node          The node to remove
 * @param shouldRefresh If false, will not trigger refreshes. This is used when removing
 *                      all equipment in a bay to hold the refresh until the end.
 * @param updateMount   If true, the mount location will be set to LOC_NONE. The transfer handler
 *                      takes care of this separately to keep from unallocating equipment that
 *                      has been transferred to another bay.
 */
private void removeEquipment(final EquipmentNode node, boolean shouldRefresh, boolean updateMount) {
    model.removeNodeFromParent(node);
    setRootVisible(((TreeNode) model.getRoot()).getChildCount() == 0);
    final Mounted mounted = node.getMounted();
    // process of removing a bay.
    if ((node.getParent() instanceof BayNode) && (node.getParent().getParent() != null)) {
        Mounted bay = ((BayNode) node.getParent()).getMounted();
        if (mounted.getType() instanceof WeaponType) {
            bay.getBayWeapons().removeElement(eSource.getEntity().getEquipmentNum(mounted));
        } else if (mounted.getType() instanceof AmmoType) {
            bay.getBayAmmo().removeElement(eSource.getEntity().getEquipmentNum(mounted));
        }
        if ((node.getMounted().getType() instanceof WeaponType) && bay.getBayWeapons().size() == 0) {
            removeBay((EquipmentNode) node.getParent(), false, true);
        }
    }
    if (updateMount) {
        Mounted moveTo = null;
        if (mounted.getType() instanceof AmmoType) {
            moveTo = UnitUtil.findUnallocatedAmmo(eSource.getEntity(), mounted.getType());
            if (null != moveTo) {
                moveTo.setShotsLeft(moveTo.getBaseShotsLeft() + mounted.getBaseShotsLeft());
                UnitUtil.removeCriticals(eSource.getEntity(), mounted);
                UnitUtil.removeMounted(eSource.getEntity(), mounted);
            }
        }
        if (null == moveTo) {
            UnitUtil.removeCriticals(eSource.getEntity(), mounted);
            UnitUtil.changeMountStatus(eSource.getEntity(), mounted, Entity.LOC_NONE, Entity.LOC_NONE, false);
            if ((node.getMounted().getType() instanceof WeaponType) && (node.getMounted().getLinkedBy() != null)) {
                UnitUtil.removeCriticals(eSource.getEntity(), node.getMounted().getLinkedBy());
                UnitUtil.changeMountStatus(eSource.getEntity(), node.getMounted().getLinkedBy(), Entity.LOC_NONE, Entity.LOC_NONE, false);
            }
        }
        UnitUtil.compactCriticals(eSource.getEntity());
    }
    if (shouldRefresh) {
        refresh.refreshEquipment();
        refresh.refreshBuild();
        refresh.refreshPreview();
        refresh.refreshStatus();
        refresh.refreshSummary();
    }
}
Also used : AmmoType(megamek.common.AmmoType) MutableTreeNode(javax.swing.tree.MutableTreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreeNode(javax.swing.tree.TreeNode) Mounted(megamek.common.Mounted) WeaponType(megamek.common.WeaponType)

Example 32 with AmmoType

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

the class BayWeaponCriticalTree method addAmmoToBay.

public void addAmmoToBay(Mounted bay, Mounted eq, int shots) {
    AmmoType at = (AmmoType) eq.getType();
    eq.setShotsLeft(eq.getBaseShotsLeft() - shots);
    if (eq.getBaseShotsLeft() <= 0) {
        UnitUtil.removeMounted(eSource.getEntity(), eq);
    }
    Optional<Mounted> addMount = bay.getBayAmmo().stream().map(n -> eSource.getEntity().getEquipment(n)).filter(m -> m.getType() == at).findFirst();
    if (addMount.isPresent()) {
        addMount.get().setShotsLeft(addMount.get().getBaseShotsLeft() + shots);
        refresh.refreshEquipment();
        refresh.refreshBuild();
        refresh.refreshPreview();
        refresh.refreshStatus();
        refresh.refreshSummary();
    } else {
        try {
            Mounted m = eSource.getEntity().addEquipment(at, bay.getLocation());
            m.setShotsLeft(shots);
            addToBay(bay, m);
        } catch (LocationFullException e) {
        }
    }
}
Also used : AmmoType(megamek.common.AmmoType) Color(java.awt.Color) Enumeration(java.util.Enumeration) EntitySource(megameklab.com.ui.EntitySource) Vector(java.util.Vector) MouseAdapter(java.awt.event.MouseAdapter) SmallCraft(megamek.common.SmallCraft) TestAero(megamek.common.verifier.TestAero) MouseListener(java.awt.event.MouseListener) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) ToolTipManager(javax.swing.ToolTipManager) TreePath(javax.swing.tree.TreePath) LocationFullException(megamek.common.LocationFullException) Set(java.util.Set) JMenu(javax.swing.JMenu) Component(java.awt.Component) MutableTreeNode(javax.swing.tree.MutableTreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) Dimension(java.awt.Dimension) List(java.util.List) WeaponType(megamek.common.WeaponType) Graphics(java.awt.Graphics) MiscType(megamek.common.MiscType) Optional(java.util.Optional) Entity(megamek.common.Entity) RefreshListener(megameklab.com.util.RefreshListener) InputEvent(java.awt.event.InputEvent) Rectangle(java.awt.Rectangle) LogLevel(megamek.common.logging.LogLevel) TreeNode(javax.swing.tree.TreeNode) ArrayList(java.util.ArrayList) PPCBayWeapon(megamek.common.weapons.bayweapons.PPCBayWeapon) HashSet(java.util.HashSet) EquipmentType(megamek.common.EquipmentType) JMenuItem(javax.swing.JMenuItem) MegaMekLab(megameklab.com.MegaMekLab) CConfig(megameklab.com.util.CConfig) Mounted(megamek.common.Mounted) UnitUtil(megameklab.com.util.UnitUtil) DefaultTreeCellRenderer(javax.swing.tree.DefaultTreeCellRenderer) BayWeapon(megamek.common.weapons.bayweapons.BayWeapon) AmmoType(megamek.common.AmmoType) JPopupMenu(javax.swing.JPopupMenu) Nullable(megamek.common.annotations.Nullable) TreeSelectionModel(javax.swing.tree.TreeSelectionModel) JTree(javax.swing.JTree) MouseEvent(java.awt.event.MouseEvent) JLabel(javax.swing.JLabel) StringJoiner(java.util.StringJoiner) PPCWeapon(megamek.common.weapons.ppc.PPCWeapon) Collections(java.util.Collections) LocationFullException(megamek.common.LocationFullException) Mounted(megamek.common.Mounted)

Example 33 with AmmoType

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

the class BayWeaponCriticalTree method removeAmmo.

/**
 * Moves one or more shots of ammo from this location to unallocated. This should not be used if
 * there is only one slot of ammo left in the location;
 * use {@link #removeEquipment(EquipmentNode) removeEquipment}
 * instead.
 *
 * @param ammo  The allocated ammo to remove.
 * @param shots The number of shots to remove.
 */
private void removeAmmo(final Mounted ammo, int shots) {
    AmmoType at = (AmmoType) ammo.getType();
    ammo.setShotsLeft(ammo.getBaseShotsLeft() - shots);
    Mounted unallocated = UnitUtil.findUnallocatedAmmo(eSource.getEntity(), at);
    for (Mounted m : eSource.getEntity().getAmmo()) {
        if ((m.getLocation() == Entity.LOC_NONE) && (m.getType() == at)) {
            unallocated = m;
            break;
        }
    }
    if (null != unallocated) {
        unallocated.setShotsLeft(ammo.getBaseShotsLeft() + shots);
    } else {
        unallocated = new Mounted(eSource.getEntity(), at);
        unallocated.setShotsLeft(shots);
        try {
            eSource.getEntity().addEquipment(unallocated, Entity.LOC_NONE, false);
        } catch (LocationFullException e) {
        }
    }
    refresh.refreshEquipment();
    refresh.refreshBuild();
    refresh.refreshPreview();
    refresh.refreshStatus();
    refresh.refreshSummary();
}
Also used : AmmoType(megamek.common.AmmoType) LocationFullException(megamek.common.LocationFullException) Mounted(megamek.common.Mounted)

Example 34 with AmmoType

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

the class BayWeaponCriticalTree method isValidDropLocation.

/**
 * Determines whether the equipment can be dropped here. In the case of ammo or weapon
 * enhancement, there must be a matching weapon in the bay. A weapon that doesn't fit the
 * bay will be placed in a new bay.
 *
 * @param loc The drop location from the TransferSupport object passed to the TransferHandler
 * @param eq  The equipment to be dropped
 * @return    Whether the equipment can be dropped in the location
 */
public boolean isValidDropLocation(JTree.DropLocation loc, Mounted eq) {
    if (!eSource.getEntity().usesWeaponBays()) {
        return true;
    }
    Mounted bay = null;
    TreePath path = loc.getPath();
    if (null != path) {
        // without a capacitor.
        if ((eq.getType() instanceof MiscType) && eq.getType().hasFlag(MiscType.F_PPC_CAPACITOR) && (path.getLastPathComponent() instanceof EquipmentNode)) {
            EquipmentNode node = (EquipmentNode) path.getLastPathComponent();
            if (node.getMounted().getType() instanceof PPCWeapon) {
                return node.getMounted().getLinkedBy() == null;
            } else if (node.getMounted().getType() instanceof PPCBayWeapon) {
                for (Integer eqNum : node.getMounted().getBayWeapons()) {
                    if (eSource.getEntity().getEquipment(eqNum).getLinkedBy() == null) {
                        return true;
                    }
                }
                return false;
            }
        }
        bay = getBayFromPath(path);
    }
    // disallow dropping a bay into its current arc or a weapon or ammo into its current bay
    if (null != bay) {
        if ((eq == bay) || ((eq.getType() instanceof WeaponType) && (bay.getBayWeapons().contains(eSource.getEntity().getEquipmentNum(eq)))) || ((eq.getType() instanceof AmmoType) && (bay.getBayAmmo().contains(eSource.getEntity().getEquipmentNum(eq))))) {
            return false;
        }
    }
    if (eq.getType() instanceof AmmoType) {
        return (null != bay) && canTakeEquipment(bay, eq);
    }
    if (UnitUtil.isWeaponEnhancement(eq.getType())) {
        if ((null != bay) && canTakeEquipment(bay, eq)) {
            for (Integer eqNum : bay.getBayWeapons()) {
                if (eSource.getEntity().getEquipment(eqNum) == eq.getLinked()) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }
    return true;
}
Also used : AmmoType(megamek.common.AmmoType) TreePath(javax.swing.tree.TreePath) Mounted(megamek.common.Mounted) MiscType(megamek.common.MiscType) WeaponType(megamek.common.WeaponType) PPCWeapon(megamek.common.weapons.ppc.PPCWeapon) PPCBayWeapon(megamek.common.weapons.bayweapons.PPCBayWeapon)

Aggregations

AmmoType (megamek.common.AmmoType)34 Mounted (megamek.common.Mounted)32 ArrayList (java.util.ArrayList)15 MiscType (megamek.common.MiscType)15 WeaponType (megamek.common.WeaponType)15 Vector (java.util.Vector)13 Font (java.awt.Font)9 EquipmentType (megamek.common.EquipmentType)8 LocationFullException (megamek.common.LocationFullException)7 BattleArmor (megamek.common.BattleArmor)6 Weapon (megamek.common.weapons.Weapon)6 Dimension (java.awt.Dimension)5 JMenuItem (javax.swing.JMenuItem)5 JPopupMenu (javax.swing.JPopupMenu)5 InfantryWeapon (megamek.common.weapons.infantry.InfantryWeapon)5 PPCWeapon (megamek.common.weapons.ppc.PPCWeapon)5 MouseEvent (java.awt.event.MouseEvent)4 MouseListener (java.awt.event.MouseListener)4 HashMap (java.util.HashMap)4 Hashtable (java.util.Hashtable)4