Search in sources :

Example 16 with WeaponType

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

the class BayWeaponCriticalTree method canTakeEquipment.

/**
 * Determines whether the equipment can be added to the bay. For a weapon this is determined
 * by bay type and AV limit. For ammo this is determined by the presence of a weapon in that location
 * that can use the ammo. For weapon enhancements this is determined by the presence of a weapon
 * that can use the enhancement that doesn't already have one.
 *
 * @param bay
 * @param eq
 * @return
 */
private boolean canTakeEquipment(Mounted bay, Mounted eq) {
    if (eq.getType() instanceof WeaponType) {
        if (((WeaponType) eq.getType()).getBayType() != bay.getType()) {
            return false;
        }
        // find current av
        double av = 0;
        for (Integer wNum : bay.getBayWeapons()) {
            final Mounted w = eSource.getEntity().getEquipment(wNum);
            // That's 1d6 for rifle and 2d6 for canon.
            if (w.getType().hasFlag(WeaponType.F_PLASMA)) {
                if (((WeaponType) w.getType()).getDamage() == WeaponType.DAMAGE_VARIABLE) {
                    av += 7;
                } else {
                    av += 13.5;
                }
            } else {
                av += ((WeaponType) w.getType()).getShortAV();
            }
            // Capacitors in bays are always considered charged.
            if ((w.getLinkedBy() != null) && (w.getLinkedBy().getType().hasFlag(MiscType.F_PPC_CAPACITOR))) {
                av += 5;
            }
        }
        if (((WeaponType) eq.getType()).isCapital()) {
            return av + ((WeaponType) eq.getType()).getShortAV() <= 70;
        } else {
            return av + ((WeaponType) eq.getType()).getShortAV() <= 700;
        }
    } else if (eq.getType() instanceof AmmoType) {
        for (int eqNum : bay.getBayWeapons()) {
            final WeaponType weapon = (WeaponType) eSource.getEntity().getEquipment(eqNum).getType();
            if ((weapon.getAmmoType() == ((AmmoType) eq.getType()).getAmmoType()) && (weapon.getRackSize() == ((AmmoType) eq.getType()).getRackSize())) {
                return true;
            }
        }
    } else if ((eq.getType() instanceof MiscType) && ((eq.getType().hasFlag(MiscType.F_ARTEMIS) || eq.getType().hasFlag(MiscType.F_ARTEMIS_V)) || eq.getType().hasFlag(MiscType.F_ARTEMIS_PROTO))) {
        for (int eqNum : bay.getBayWeapons()) {
            final Mounted weapon = eSource.getEntity().getEquipment(eqNum);
            final WeaponType wtype = (WeaponType) weapon.getType();
            if ((weapon.getLinkedBy() == null) && ((wtype.getAmmoType() == AmmoType.T_LRM) || (wtype.getAmmoType() == AmmoType.T_SRM) || (wtype.getAmmoType() == AmmoType.T_MML) || (wtype.getAmmoType() == AmmoType.T_NLRM))) {
                return true;
            }
        }
    } else if ((eq.getType() instanceof MiscType) && eq.getType().hasFlag(MiscType.F_APOLLO)) {
        for (int eqNum : bay.getBayWeapons()) {
            final Mounted weapon = eSource.getEntity().getEquipment(eqNum);
            final WeaponType wtype = (WeaponType) weapon.getType();
            if ((weapon.getLinkedBy() == null) && (wtype.getAmmoType() == AmmoType.T_MRM)) {
                return true;
            }
        }
    } else if ((eq.getType() instanceof MiscType) && eq.getType().hasFlag(MiscType.F_PPC_CAPACITOR) && (bay.getType() instanceof PPCBayWeapon)) {
        for (int eqNum : bay.getBayWeapons()) {
            if (eSource.getEntity().getEquipment(eqNum).getLinkedBy() == null) {
                return true;
            }
        }
    }
    return false;
}
Also used : AmmoType(megamek.common.AmmoType) Mounted(megamek.common.Mounted) WeaponType(megamek.common.WeaponType) MiscType(megamek.common.MiscType) PPCBayWeapon(megamek.common.weapons.bayweapons.PPCBayWeapon)

Example 17 with WeaponType

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

the class BayWeaponCriticalTree method addToBay.

/**
 * Adds an equipment mount to a bay. Changes the equipment mount's location and updates the bay's
 * weapon or ammo list if necessary.
 *
 * @param bay
 * @param eq
 */
public void addToBay(@Nullable Mounted bay, Mounted eq) {
    // Check that we have a bay
    if ((null == bay) || !canTakeEquipment(bay, eq)) {
        if (eq.getType() instanceof WeaponType) {
            EquipmentType bayType = ((WeaponType) eq.getType()).getBayType();
            addToNewBay(bayType, eq);
        } else {
            addToLocation(eq);
        }
        return;
    } else if (eq.getType() instanceof MiscType) {
        if (eq.getType().hasFlag(MiscType.F_ARTEMIS) || eq.getType().hasFlag(MiscType.F_ARTEMIS_PROTO) || eq.getType().hasFlag(MiscType.F_ARTEMIS_V)) {
            for (Integer eqNum : bay.getBayWeapons()) {
                final Mounted weapon = eSource.getEntity().getEquipment(eqNum);
                final WeaponType wtype = (WeaponType) weapon.getType();
                if ((weapon.getLinkedBy() == null) && ((wtype.getAmmoType() == AmmoType.T_LRM) || (wtype.getAmmoType() == AmmoType.T_SRM) || (wtype.getAmmoType() == AmmoType.T_MML) || (wtype.getAmmoType() == AmmoType.T_NLRM))) {
                    moveToArc(eq);
                    eq.setLinked(weapon);
                    break;
                }
            }
        } else if (eq.getType().hasFlag(MiscType.F_APOLLO) || eq.getType().hasFlag(MiscType.F_PPC_CAPACITOR)) {
            for (Integer eqNum : bay.getBayWeapons()) {
                final Mounted weapon = eSource.getEntity().getEquipment(eqNum);
                if (weapon.getLinkedBy() == null) {
                    moveToArc(eq);
                    eq.setLinked(weapon);
                    break;
                }
            }
        }
    } else {
        // there.
        if (eq.getType() instanceof AmmoType) {
            Optional<Mounted> addMount = bay.getBayAmmo().stream().map(n -> eSource.getEntity().getEquipment(n)).filter(m -> m.getType() == eq.getType()).findFirst();
            if (addMount.isPresent()) {
                addMount.get().setShotsLeft(addMount.get().getBaseShotsLeft() + eq.getBaseShotsLeft());
                UnitUtil.removeMounted(eSource.getEntity(), eq);
                refresh.refreshEquipment();
                refresh.refreshBuild();
                refresh.refreshPreview();
                refresh.refreshStatus();
                refresh.refreshSummary();
                return;
            }
        }
        EquipmentNode bayNode = null;
        for (Enumeration<?> e = ((TreeNode) model.getRoot()).children(); e.hasMoreElements(); ) {
            final EquipmentNode node = (EquipmentNode) e.nextElement();
            if (node.getMounted() == bay) {
                bayNode = node;
                break;
            }
        }
        if (null != bayNode) {
            moveToArc(eq);
            EquipmentNode eqNode = new EquipmentNode(eq);
            model.insertNodeInto(eqNode, bayNode, bayNode.getChildCount());
            eqNode.setParent(bayNode);
            if (eq.getType() instanceof WeaponType) {
                bay.addWeaponToBay(eSource.getEntity().getEquipmentNum(eq));
                if (eq.getLinkedBy() != null) {
                    moveToArc(eq.getLinkedBy());
                }
            } else if (eq.getType() instanceof AmmoType) {
                bay.addAmmoToBay(eSource.getEntity().getEquipmentNum(eq));
            }
        } else {
            // $NON-NLS-1$
            MegaMekLab.getLogger().log(// $NON-NLS-1$
            BayWeaponCriticalTree.class, // $NON-NLS-1$
            "addToBay(Mounted,Mounted)", LogLevel.DEBUG, // $NON-NLS-1$
            bay.getName() + "[" + eSource.getEntity().getEquipmentNum(bay) + "] not found in " + // $NON-NLS-1$
            getLocationName());
        }
    }
    refresh.refreshEquipment();
    refresh.refreshBuild();
    refresh.refreshPreview();
    refresh.refreshStatus();
    refresh.refreshSummary();
}
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) Enumeration(java.util.Enumeration) Optional(java.util.Optional) Mounted(megamek.common.Mounted) WeaponType(megamek.common.WeaponType) MiscType(megamek.common.MiscType) EquipmentType(megamek.common.EquipmentType)

Example 18 with WeaponType

use of megamek.common.WeaponType in project spoon by INRIA.

the class TestBot method calculateWeaponAttacks.

protected Vector calculateWeaponAttacks(Entity en, Mounted mw, boolean best_only) {
    int from = en.getId();
    int weaponID = en.getEquipmentNum(mw);
    Vector result = new Vector();
    Enumeration ents = game.getValidTargets(en).elements();
    AttackOption a = null;
    AttackOption max = new AttackOption(null, null, 0, null);
    while (ents.hasMoreElements()) {
        Entity e = (Entity) ents.nextElement();
        CEntity enemy = centities.get(e);
        ToHitData th = WeaponAttackAction.toHit(game, from, e, weaponID);
        if (th.getValue() != ToHitData.IMPOSSIBLE && !(th.getValue() >= 13)) {
            double expectedDmg;
            // Are we an Infantry platoon?
            if (en instanceof Infantry) {
                // Get the expected damage, given our current
                // manpower level.
                Infantry inf = (Infantry) en;
                expectedDmg = inf.getDamage(inf.getShootingStrength());
            } else {
                // Get the expected damage of the weapon.
                expectedDmg = CEntity.getExpectedDamage((WeaponType) mw.getType());
            }
            // Infantry in the open suffer double damage.
            if (e instanceof Infantry) {
                IHex e_hex = game.getBoard().getHex(e.getPosition());
                if (!e_hex.containsTerrain(Terrains.WOODS) && !e_hex.containsTerrain(Terrains.BUILDING)) {
                    expectedDmg *= 2;
                }
            }
            a = new AttackOption(enemy, mw, expectedDmg, th);
            if (a.value > max.value) {
                if (best_only) {
                    max = a;
                } else {
                    result.add(0, a);
                }
            } else {
                result.add(a);
            }
        }
    }
    if (best_only && max.target != null) {
        result.add(max);
    }
    if (result.size() > 0) {
        result.add(new AttackOption(null, mw, 0, null));
    }
    return result;
}
Also used : Entity(megamek.common.Entity) Infantry(megamek.common.Infantry) Enumeration(java.util.Enumeration) ToHitData(megamek.common.ToHitData) WeaponType(megamek.common.WeaponType) Vector(com.sun.java.util.collections.Vector)

Example 19 with WeaponType

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

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

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