Search in sources :

Example 76 with Mounted

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

the class AeroBayTransferHandler method createTransferable.

@Override
protected Transferable createTransferable(JComponent c) {
    if (c instanceof BayWeaponCriticalTree) {
        return new StringSelection(((BayWeaponCriticalTree) c).encodeSelection());
    } else {
        JTable table = (JTable) c;
        StringJoiner sj = new StringJoiner(",");
        for (int row : table.getSelectedRows()) {
            Mounted mount = (Mounted) ((CriticalTableModel) table.getModel()).getValueAt(row, CriticalTableModel.EQUIPMENT);
            sj.add(Integer.toString(eSource.getEntity().getEquipmentNum(mount)));
        }
        return new StringSelection(sj.toString());
    }
}
Also used : Mounted(megamek.common.Mounted) JTable(javax.swing.JTable) StringJoiner(java.util.StringJoiner) StringSelection(java.awt.datatransfer.StringSelection)

Example 77 with Mounted

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

the class BayWeaponCriticalTree method addBay.

/**
 * Moves a bay and all its contents from another location
 * @param bay
 */
public void addBay(Mounted bay) {
    // First move the bay here
    moveToArc(bay);
    BayNode bayNode = new BayNode(bay);
    model.insertNodeInto(bayNode, (MutableTreeNode) model.getRoot(), ((TreeNode) model.getRoot()).getChildCount());
    bayNode.setParent((MutableTreeNode) model.getRoot());
    if (isRootVisible()) {
        expandRow(0);
        setRootVisible(false);
    }
    // Now go through all the equipment in the bay
    for (Integer eqNum : bay.getBayWeapons()) {
        final Mounted weapon = eSource.getEntity().getEquipment(eqNum);
        moveToArc(weapon);
        EquipmentNode node = new EquipmentNode(weapon);
        model.insertNodeInto(node, bayNode, bayNode.getChildCount());
        node.setParent(bayNode);
        if (weapon.getLinkedBy() != null) {
            moveToArc(weapon.getLinkedBy());
        }
    }
    for (Integer eqNum : bay.getBayAmmo()) {
        final Mounted ammo = eSource.getEntity().getEquipment(eqNum);
        moveToArc(ammo);
        EquipmentNode node = new EquipmentNode(ammo);
        model.insertNodeInto(node, bayNode, bayNode.getChildCount());
        node.setParent(bayNode);
    }
}
Also used : Mounted(megamek.common.Mounted)

Example 78 with Mounted

use of megamek.common.Mounted 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 79 with Mounted

use of megamek.common.Mounted 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 80 with Mounted

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

the class PrintMech method writeEquipment.

@Override
protected void writeEquipment(SVGRectElement svgRect) {
    Map<Integer, Map<RecordSheetEquipmentLine, Integer>> eqMap = new TreeMap<>();
    Map<String, Integer> ammo = new TreeMap<>();
    for (Mounted m : mech.getEquipment()) {
        if ((m.getType() instanceof AmmoType) && (((AmmoType) m.getType()).getAmmoType() != AmmoType.T_COOLANT_POD)) {
            if (m.getLocation() != Entity.LOC_NONE) {
                String shortName = m.getType().getShortName().replace("Ammo", "");
                shortName = shortName.replace("(Clan)", "");
                String munition = ((AmmoType) m.getType()).getSubMunitionName().replace("(Clan) ", "");
                shortName = shortName.replace(munition, "");
                ammo.merge(shortName.trim(), m.getBaseShotsLeft(), Integer::sum);
            }
            continue;
        }
        if ((m.getType() instanceof AmmoType) || (m.getLocation() == Entity.LOC_NONE) || !UnitUtil.isPrintableEquipment(m.getType(), true)) {
            continue;
        }
        if (mech.hasETypeFlag(Entity.ETYPE_QUADVEE) && (m.getType() instanceof MiscType) && m.getType().hasFlag(MiscType.F_TRACKS)) {
            continue;
        }
        eqMap.putIfAbsent(m.getLocation(), new HashMap<>());
        RecordSheetEquipmentLine line = new RecordSheetEquipmentLine(m);
        eqMap.get(m.getLocation()).merge(line, 1, Integer::sum);
    }
    Rectangle2D bbox = getRectBBox(svgRect);
    Element canvas = (Element) ((Node) svgRect).getParentNode();
    int viewWidth = (int) bbox.getWidth();
    int viewHeight = (int) bbox.getHeight();
    int viewX = (int) bbox.getX();
    int viewY = (int) bbox.getY();
    int qtyX = (int) Math.round(viewX + viewWidth * 0.037);
    int nameX = (int) Math.round(viewX + viewWidth * 0.08);
    int locX = (int) Math.round(viewX + viewWidth * 0.41);
    int heatX = (int) Math.round(viewX + viewWidth * 0.48);
    int dmgX = (int) Math.round(viewX + viewWidth * 0.53);
    int minX = (int) Math.round(viewX + viewWidth * 0.75);
    int shortX = (int) Math.round(viewX + viewWidth * 0.82);
    int medX = (int) Math.round(viewX + viewWidth * 0.89);
    int longX = (int) Math.round(viewX + viewWidth * 0.96);
    int indent = (int) Math.round(viewWidth * 0.02);
    int currY = viewY + 10;
    float fontSize = FONT_SIZE_MEDIUM;
    float lineHeight = getFontHeight(fontSize) * 0.8f;
    addTextElement(canvas, qtyX, currY, "Qty", fontSize, "middle", "bold");
    addTextElement(canvas, nameX + indent, currY, "Type", fontSize, "start", "bold");
    addTextElement(canvas, locX, currY, "Loc", fontSize, "middle", "bold");
    addTextElement(canvas, heatX, currY, "Ht", fontSize, "middle", "bold");
    addTextElement(canvas, dmgX, currY, "Dmg", fontSize, "start", "bold");
    addTextElement(canvas, minX, currY, "Min", fontSize, "middle", "bold");
    addTextElement(canvas, shortX, currY, "Sht", fontSize, "middle", "bold");
    addTextElement(canvas, medX, currY, "Med", fontSize, "middle", "bold");
    addTextElement(canvas, longX, currY, "Lng", fontSize, "middle", "bold");
    currY += lineHeight * 1.2;
    int lines = 0;
    for (Integer loc : eqMap.keySet()) {
        for (RecordSheetEquipmentLine line : eqMap.get(loc).keySet()) {
            int rows = line.nRows();
            if ((rows == 1) && (getTextLength(line.getNameField(0, mech.isMixedTech()), fontSize) > locX - nameX)) {
                rows++;
            }
            lines += rows;
        }
    }
    if (lines > 12) {
        lineHeight = getFontHeight(fontSize) * 0.8f;
    }
    if (lines > 16) {
        fontSize = FONT_SIZE_SMALL;
    }
    if (lines > 20) {
        fontSize = FONT_SIZE_VSMALL;
    }
    for (Integer loc : eqMap.keySet()) {
        for (RecordSheetEquipmentLine line : eqMap.get(loc).keySet()) {
            for (int row = 0; row < line.nRows(); row++) {
                if (row == 0) {
                    addTextElement(canvas, qtyX, currY, Integer.toString(eqMap.get(loc).get(line)), fontSize, "middle", "normal");
                    lines = addMultilineTextElement(canvas, nameX, currY, locX - nameX - indent, lineHeight, line.getNameField(row, mech.isMixedTech()), fontSize, "start", "normal");
                } else {
                    lines = addMultilineTextElement(canvas, nameX + indent, currY, locX - nameX - indent, lineHeight, line.getNameField(row, mech.isMixedTech()), fontSize, "start", "normal");
                }
                addTextElement(canvas, locX, currY, line.getLocationField(row), fontSize, "middle", "normal");
                addTextElement(canvas, heatX, currY, line.getHeatField(row), fontSize, "middle", "normal");
                lines = Math.max(lines, addMultilineTextElement(canvas, dmgX, currY, minX - dmgX, lineHeight, line.getDamageField(row), fontSize, "start", "normal"));
                addTextElement(canvas, minX, currY, line.getMinField(row), fontSize, "middle", "normal");
                addTextElement(canvas, shortX, currY, line.getShortField(row), fontSize, "middle", "normal");
                addTextElement(canvas, medX, currY, line.getMediumField(row), fontSize, "middle", "normal");
                addTextElement(canvas, longX, currY, line.getLongField(row), fontSize, "middle", "normal");
                currY += lineHeight * lines;
            }
        }
    }
    StringJoiner quirksList = new StringJoiner(", ");
    Quirks quirks = mech.getQuirks();
    for (Enumeration<IOptionGroup> optionGroups = quirks.getGroups(); optionGroups.hasMoreElements(); ) {
        IOptionGroup optiongroup = optionGroups.nextElement();
        if (quirks.count(optiongroup.getKey()) > 0) {
            for (Enumeration<IOption> options = optiongroup.getOptions(); options.hasMoreElements(); ) {
                IOption option = options.nextElement();
                if (option != null && option.booleanValue()) {
                    quirksList.add(option.getDisplayableNameWithValue());
                }
            }
        }
    }
    if ((ammo.size() > 0) || (quirksList.length() > 0)) {
        Element svgGroup = getSVGDocument().createElementNS(svgNS, SVGConstants.SVG_G_TAG);
        canvas.appendChild(svgGroup);
        lines = 0;
        if (ammo.size() > 0) {
            lines = addMultilineTextElement(svgGroup, viewX + viewWidth * 0.025, 0, viewWidth * 0.95, lineHeight, "Ammo: " + ammo.entrySet().stream().map(e -> String.format("(%s) %d", e.getKey(), e.getValue())).collect(Collectors.joining(", ")), fontSize, "start", "normal");
        }
        if (quirksList.length() > 0) {
            lines += addMultilineTextElement(svgGroup, viewX + viewWidth * 0.025, lines * lineHeight, viewWidth * 0.95, lineHeight, "Quirks: " + quirksList.toString(), fontSize, "start", "normal");
        }
        svgGroup.setAttributeNS(null, SVGConstants.SVG_TRANSFORM_ATTRIBUTE, String.format("translate(0,%f)", viewY + viewHeight - lines * lineHeight));
    }
}
Also used : IOptionGroup(megamek.common.options.IOptionGroup) MiscType(megamek.common.MiscType) SVGRectElement(org.w3c.dom.svg.SVGRectElement) Element(org.w3c.dom.Element) Rectangle2D(java.awt.geom.Rectangle2D) IOption(megamek.common.options.IOption) TreeMap(java.util.TreeMap) Quirks(megamek.common.options.Quirks) AmmoType(megamek.common.AmmoType) RecordSheetEquipmentLine(megameklab.com.util.RecordSheetEquipmentLine) Mounted(megamek.common.Mounted) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) StringJoiner(java.util.StringJoiner)

Aggregations

Mounted (megamek.common.Mounted)162 MiscType (megamek.common.MiscType)50 LocationFullException (megamek.common.LocationFullException)39 AmmoType (megamek.common.AmmoType)36 EquipmentType (megamek.common.EquipmentType)34 CriticalSlot (megamek.common.CriticalSlot)31 ArrayList (java.util.ArrayList)30 WeaponType (megamek.common.WeaponType)29 Vector (java.util.Vector)25 EntityLoadingException (megamek.common.loaders.EntityLoadingException)21 Font (java.awt.Font)14 Entity (megamek.common.Entity)13 List (java.util.List)12 Dimension (java.awt.Dimension)11 JLabel (javax.swing.JLabel)11 JMenuItem (javax.swing.JMenuItem)11 JPopupMenu (javax.swing.JPopupMenu)11 BattleArmor (megamek.common.BattleArmor)11 ActionEvent (java.awt.event.ActionEvent)10 Weapon (megamek.common.weapons.Weapon)10