Search in sources :

Example 26 with WeaponType

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

the class BuildView method mousePressed.

public void mousePressed(MouseEvent e) {
    // locations, but only if those locations are make sense
    if (e.getButton() == MouseEvent.BUTTON3) {
        JPopupMenu popup = new JPopupMenu();
        JMenuItem item;
        final int selectedRow = equipmentTable.rowAtPoint(e.getPoint());
        final Mounted eq = (Mounted) equipmentTable.getModel().getValueAt(selectedRow, CriticalTableModel.EQUIPMENT);
        final String[] locNames = BattleArmor.MOUNT_LOC_NAMES;
        // A list of the valid locations we can add the selected eq to
        ArrayList<Integer> validLocs = new ArrayList<Integer>();
        int numLocs = BattleArmor.MOUNT_NUM_LOCS;
        for (int loc = 0; loc < numLocs; loc++) {
            if (TestBattleArmor.isMountLegal(getBattleArmor(), eq, loc)) {
                validLocs.add(loc);
            }
        }
        if (eq.getLocation() == BattleArmor.LOC_SQUAD && !(eq.getType() instanceof InfantryWeapon)) {
            // Add a menu item for each potential location
            for (Integer location : validLocs) {
                if (UnitUtil.isValidLocation(getBattleArmor(), eq.getType(), location)) {
                    item = new JMenuItem("Add to " + locNames[location]);
                    final int loc = location;
                    item.addActionListener(new ActionListener() {

                        public void actionPerformed(ActionEvent e) {
                            mountEquipmentInLocation(loc, selectedRow);
                        }
                    });
                    popup.add(item);
                }
            }
            if (!UnitUtil.isArmor(eq.getType()) && !eq.isSquadSupportWeapon()) {
                item = new JMenuItem("Make individual weapon");
                item.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        eq.setLocation(BattleArmor.LOC_TROOPER_1);
                        ((BuildTab) getParent().getParent()).refreshAll();
                    }
                });
                popup.add(item);
            }
        } else {
            if (!UnitUtil.isArmor(eq.getType()) && !(eq.getType() instanceof InfantryWeapon) && !((eq.getType() instanceof WeaponType) && (eq.getType().hasFlag(WeaponType.F_TASER) || ((WeaponType) eq.getType()).getAmmoType() == AmmoType.T_NARC))) {
                item = new JMenuItem("Make squad weapon");
                item.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        eq.setLocation(BattleArmor.LOC_SQUAD);
                        ((BuildTab) getParent().getParent()).refreshAll();
                    }
                });
                popup.add(item);
            }
        }
        // Allow number of shots selection
        if ((getBattleArmor() instanceof BattleArmor) && eq.getType() instanceof AmmoType) {
            AmmoType at = (AmmoType) eq.getType();
            int maxNumShots = 4;
            int stepSize = 1;
            if (at.getAmmoType() == AmmoType.T_BA_TUBE) {
                maxNumShots = 8;
                stepSize = 2;
            }
            for (int i = at.getShots(); i <= maxNumShots; i += stepSize) {
                if (i == eq.getBaseShotsLeft()) {
                    continue;
                }
                item = new JMenuItem("Set Shots: " + i);
                final int shots = i;
                item.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        eq.setShotsLeft(shots);
                        ((BuildTab) getParent().getParent()).refreshAll();
                    }
                });
                popup.add(item);
            }
        }
        // Allow making this a squad support weapon
        if ((eq.getType() instanceof WeaponType) && !eq.isSquadSupportWeapon() && !eq.getType().hasFlag(WeaponType.F_INFANTRY) && eq.getLocation() == BattleArmor.LOC_SQUAD && getBattleArmor().getChassisType() != BattleArmor.CHASSIS_TYPE_QUAD) {
            item = new JMenuItem("Mount as squad support weapon");
            item.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    eq.setSquadSupportWeapon(true);
                    ((BuildTab) getParent().getParent()).refreshAll();
                }
            });
            popup.add(item);
        }
        // Adding ammo as a squad support mount is slightly different
        if ((eq.getType() instanceof AmmoType) && !eq.getType().hasFlag(WeaponType.F_MISSILE) && !eq.isSquadSupportWeapon() && eq.getLocation() == BattleArmor.LOC_SQUAD && getBattleArmor().getChassisType() != BattleArmor.CHASSIS_TYPE_QUAD) {
            boolean enabled = false;
            for (Mounted weapon : getBattleArmor().getWeaponList()) {
                WeaponType wtype = (WeaponType) weapon.getType();
                if (weapon.isSquadSupportWeapon() && AmmoType.isAmmoValid(eq, wtype)) {
                    enabled = true;
                }
            }
            item = new JMenuItem("Mount as squad support weapon");
            item.setEnabled(enabled);
            item.setToolTipText("Ammo can only be squad mounted along " + "with a weapon that uses it");
            item.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    eq.setSquadSupportWeapon(true);
                    ((BuildTab) getParent().getParent()).refreshAll();
                }
            });
            popup.add(item);
        }
        // Allow removing squad support weapon
        if (eq.isSquadSupportWeapon()) {
            item = new JMenuItem("Remove squad support weapon mount");
            item.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    eq.setSquadSupportWeapon(false);
                    // squad support weapon
                    for (Mounted ammo : getBattleArmor().getAmmo()) {
                        ammo.setSquadSupportWeapon(false);
                    }
                    ((BuildTab) getParent().getParent()).refreshAll();
                }
            });
            popup.add(item);
        }
        // See if we should allow linking this to a DWP
        if (getBattleArmor().hasWorkingMisc(MiscType.F_DETACHABLE_WEAPON_PACK) && !eq.getType().hasFlag(MiscType.F_DETACHABLE_WEAPON_PACK) && !eq.getType().hasFlag(WeaponType.F_MISSILE) && !(eq.getType() instanceof AmmoType) && !eq.isDWPMounted() && (getBattleArmor()).canMountDWP()) {
            for (Mounted m : getBattleArmor().getMisc()) {
                // If this isn't a DWP or it's a full DWP, skip
                if (!m.getType().hasFlag(MiscType.F_DETACHABLE_WEAPON_PACK) || m.getLinked() != null) {
                    continue;
                }
                String locName;
                if (m.getBaMountLoc() == BattleArmor.MOUNT_LOC_NONE) {
                    locName = "None";
                } else {
                    locName = BattleArmor.MOUNT_LOC_NAMES[m.getBaMountLoc()];
                }
                item = new JMenuItem("Mount in " + m.getName() + " (" + locName + ")");
                final Mounted dwp = m;
                item.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        eq.setLinked(dwp);
                        dwp.setLinked(eq);
                        eq.setDWPMounted(true);
                        ((BuildTab) getParent().getParent()).refreshAll();
                    }
                });
                popup.add(item);
            }
        }
        // Should we allow mounting Ammo in a DWP?
        if ((eq.getType() instanceof AmmoType) && getBattleArmor().hasWorkingMisc(MiscType.F_DETACHABLE_WEAPON_PACK) && !eq.isDWPMounted() && (getBattleArmor()).canMountDWP()) {
            for (final Mounted m : getBattleArmor().getMisc()) {
                // If this isn't a DWP, skip
                if (!m.getType().hasFlag(MiscType.F_DETACHABLE_WEAPON_PACK)) {
                    continue;
                }
                // We only want to enable the menu item if the DWP has a
                // mounted weapon and we clicked on a valid ammo type
                boolean enabled = false;
                if (m.getLinked() != null) {
                    EquipmentType etype = m.getLinked().getType();
                    if (etype instanceof WeaponType) {
                        WeaponType wtype = (WeaponType) etype;
                        if (AmmoType.isAmmoValid(eq, wtype)) {
                            enabled = true;
                        }
                    }
                }
                String locName;
                if (m.getBaMountLoc() == BattleArmor.MOUNT_LOC_NONE) {
                    locName = "None";
                } else {
                    locName = BattleArmor.MOUNT_LOC_NAMES[m.getBaMountLoc()];
                }
                item = new JMenuItem("Mount in " + m.getName() + " (" + locName + ")");
                item.setToolTipText("Ammo can only be mounted in a DWP " + "with a valid weapon.");
                item.setEnabled(enabled);
                item.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        m.getLinked().setLinked(eq);
                        eq.setDWPMounted(true);
                        ((BuildTab) getParent().getParent()).refreshAll();
                    }
                });
                popup.add(item);
            }
        }
        // Right-clicked on a DWP that has an attached weapon
        if (eq.getType().hasFlag(MiscType.F_DETACHABLE_WEAPON_PACK) && eq.getLinked() != null) {
            item = new JMenuItem("Remove attached weapon");
            item.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    Mounted attached = eq.getLinked();
                    attached.setDWPMounted(false);
                    eq.setLinked(null);
                    eq.setLinkedBy(null);
                    attached.setLinked(null);
                    attached.setLinkedBy(null);
                    // Remove any attached ammo
                    for (Mounted ammo : getBattleArmor().getAmmo()) {
                        if (attached.equals(ammo.getLinkedBy())) {
                            ammo.setDWPMounted(false);
                            ammo.setLinked(null);
                            ammo.setLinkedBy(null);
                        }
                    }
                    ((BuildTab) getParent().getParent()).refreshAll();
                }
            });
            popup.add(item);
        }
        // See if we should allow linking this to an AP Mount
        if (getBattleArmor().hasWorkingMisc(MiscType.F_AP_MOUNT) && eq.getType().hasFlag(WeaponType.F_INFANTRY) && !eq.isAPMMounted()) {
            for (Mounted m : getBattleArmor().getMisc()) {
                // If this isn't an AP Mount or it's a full AP Mount, skip
                if (!m.getType().hasFlag(MiscType.F_AP_MOUNT) || m.getLinked() != null) {
                    continue;
                }
                // regardless of the number of gloves
                if (m.getType().hasFlag(MiscType.F_ARMORED_GLOVE)) {
                    boolean hasUsedGlove = false;
                    for (Mounted m2 : getBattleArmor().getMisc()) {
                        if (m2.getType().hasFlag(MiscType.F_ARMORED_GLOVE) && (m2.getLinked() != null)) {
                            hasUsedGlove = true;
                        }
                    }
                    if (hasUsedGlove) {
                        continue;
                    }
                }
                // Only armored gloves can carry infantry support weapons
                if (!m.getType().hasFlag(MiscType.F_ARMORED_GLOVE) && eq.getType().hasFlag(WeaponType.F_INF_SUPPORT)) {
                    continue;
                }
                String locName;
                if (m.getBaMountLoc() == BattleArmor.MOUNT_LOC_NONE) {
                    locName = "None";
                } else {
                    locName = BattleArmor.MOUNT_LOC_NAMES[m.getBaMountLoc()];
                }
                item = new JMenuItem("Mount in " + m.getName() + " (" + locName + ")");
                final Mounted apm = m;
                item.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        eq.setLinked(apm);
                        apm.setLinked(eq);
                        eq.setAPMMounted(true);
                        ((BuildTab) getParent().getParent()).refreshAll();
                    }
                });
                popup.add(item);
            }
        }
        // Right-clicked on a AP Mount that has an attached weapon
        if (eq.getType().hasFlag(MiscType.F_AP_MOUNT) && eq.getLinked() != null) {
            item = new JMenuItem("Remove attached weapon");
            item.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    Mounted attached = eq.getLinked();
                    attached.setAPMMounted(false);
                    eq.setLinked(null);
                    eq.setLinkedBy(null);
                    attached.setLinked(null);
                    attached.setLinkedBy(null);
                    ((BuildTab) getParent().getParent()).refreshAll();
                }
            });
            popup.add(item);
        }
        popup.show(this, e.getX(), e.getY());
    }
}
Also used : ActionEvent(java.awt.event.ActionEvent) ArrayList(java.util.ArrayList) EquipmentType(megamek.common.EquipmentType) JPopupMenu(javax.swing.JPopupMenu) AmmoType(megamek.common.AmmoType) ActionListener(java.awt.event.ActionListener) Mounted(megamek.common.Mounted) WeaponType(megamek.common.WeaponType) JMenuItem(javax.swing.JMenuItem) InfantryWeapon(megamek.common.weapons.infantry.InfantryWeapon) BattleArmor(megamek.common.BattleArmor) TestBattleArmor(megamek.common.verifier.TestBattleArmor)

Example 27 with WeaponType

use of megamek.common.WeaponType 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 28 with WeaponType

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

the class BayWeaponCriticalTree method deleteEquipment.

/**
 * Removes the node and removes the equipment from the unit entirely.
 * @param node
 */
private void deleteEquipment(final EquipmentNode node) {
    model.removeNodeFromParent(node);
    setRootVisible(((TreeNode) model.getRoot()).getChildCount() == 0);
    final Mounted mounted = node.getMounted();
    UnitUtil.removeMounted(eSource.getEntity(), mounted);
    if ((mounted.getType() instanceof WeaponType) && (mounted.getLinkedBy() != null)) {
        UnitUtil.removeMounted(eSource.getEntity(), mounted.getLinkedBy());
    }
    UnitUtil.compactCriticals(eSource.getEntity());
    refresh.refreshEquipment();
    refresh.refreshBuild();
    refresh.refreshPreview();
    refresh.refreshStatus();
    refresh.refreshSummary();
}
Also used : MutableTreeNode(javax.swing.tree.MutableTreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreeNode(javax.swing.tree.TreeNode) Mounted(megamek.common.Mounted) WeaponType(megamek.common.WeaponType)

Example 29 with WeaponType

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

the class BayWeaponCriticalTree method initRoot.

/**
 * Runs through all equipment mounted on the vessel and adds nodes for the ones that match this
 * tree's location and facing.
 *
 * @return A new root node
 */
private TreeNode initRoot() {
    MutableTreeNode root = new DefaultMutableTreeNode();
    Set<Integer> eqSet = new HashSet<>();
    for (Mounted bay : eSource.getEntity().getWeaponBayList()) {
        if ((bay.getLocation() == location) && ((facing == BOTH) || (bay.isRearMounted() == (facing == AFT)))) {
            eqSet.add(eSource.getEntity().getEquipmentNum(bay));
            EquipmentNode bayNode = new BayNode(bay);
            root.insert(bayNode, root.getChildCount());
            bayNode.setParent(root);
            for (Integer wNum : bay.getBayWeapons()) {
                final Mounted weapon = eSource.getEntity().getEquipment(wNum);
                EquipmentNode node = new EquipmentNode(weapon);
                bayNode.insert(node, bayNode.getChildCount());
                node.setParent(bayNode);
                eqSet.add(wNum);
            }
            for (Integer aNum : bay.getBayAmmo()) {
                final Mounted ammo = eSource.getEntity().getEquipment(aNum);
                EquipmentNode node = new EquipmentNode(ammo);
                bayNode.insert(node, bayNode.getChildCount());
                node.setParent(bayNode);
                eqSet.add(aNum);
            }
        }
    }
    for (int eqIndex = 0; eqIndex < eSource.getEntity().getEquipment().size(); eqIndex++) {
        final Mounted eq = eSource.getEntity().getEquipment(eqIndex);
        if ((eq.getLinked() != null) && (eq.getLinked().getType() instanceof WeaponType)) {
            continue;
        }
        if (!eqSet.contains(eqIndex) && (eq.getLocation() == location) && ((facing == BOTH) || (eq.isRearMounted() == (facing == AFT)))) {
            EquipmentNode node = new EquipmentNode(eq);
            root.insert(node, root.getChildCount());
            node.setParent(root);
        }
    }
    return root;
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) Mounted(megamek.common.Mounted) WeaponType(megamek.common.WeaponType) MutableTreeNode(javax.swing.tree.MutableTreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) HashSet(java.util.HashSet)

Example 30 with WeaponType

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

the class BayWeaponCriticalTree method setBayFacing.

/**
 * Sets the rearMounted flag on this bay and all equipment mounted in it.
 *
 * @param node
 * @param rear
 */
private void setBayFacing(EquipmentNode node, boolean rear) {
    if (node.isLeaf() && (node.getParent() instanceof BayNode)) {
        node = (EquipmentNode) node.getParent();
    }
    for (Enumeration<MutableTreeNode> e = node.children(); e.hasMoreElements(); ) {
        final Mounted m = ((EquipmentNode) e.nextElement()).getMounted();
        UnitUtil.changeMountStatus(eSource.getEntity(), m, location, Entity.LOC_NONE, rear);
        if ((m.getType() instanceof WeaponType) && (m.getLinkedBy() != null)) {
            UnitUtil.changeMountStatus(eSource.getEntity(), m.getLinkedBy(), location, Entity.LOC_NONE, rear);
        }
    }
    UnitUtil.changeMountStatus(eSource.getEntity(), node.getMounted(), location, Entity.LOC_NONE, rear);
    refresh.refreshBuild();
    refresh.refreshPreview();
    refresh.refreshStatus();
    refresh.refreshStatus();
    refresh.refreshSummary();
}
Also used : Mounted(megamek.common.Mounted) WeaponType(megamek.common.WeaponType) MutableTreeNode(javax.swing.tree.MutableTreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode)

Aggregations

WeaponType (megamek.common.WeaponType)31 Mounted (megamek.common.Mounted)24 AmmoType (megamek.common.AmmoType)14 MiscType (megamek.common.MiscType)12 Vector (java.util.Vector)8 InfantryWeapon (megamek.common.weapons.infantry.InfantryWeapon)8 ArrayList (java.util.ArrayList)7 JMenuItem (javax.swing.JMenuItem)6 JPopupMenu (javax.swing.JPopupMenu)6 BattleArmor (megamek.common.BattleArmor)6 CriticalSlot (megamek.common.CriticalSlot)6 Entity (megamek.common.Entity)6 EquipmentType (megamek.common.EquipmentType)6 PPCWeapon (megamek.common.weapons.ppc.PPCWeapon)6 ActionEvent (java.awt.event.ActionEvent)5 LocationFullException (megamek.common.LocationFullException)5 TestBattleArmor (megamek.common.verifier.TestBattleArmor)5 Dimension (java.awt.Dimension)4 ActionListener (java.awt.event.ActionListener)4 MouseEvent (java.awt.event.MouseEvent)4