Search in sources :

Example 26 with CriticalSlot

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

the class CriticalView method refresh.

public void refresh() {
    leftPanel.removeAll();
    rightPanel.removeAll();
    bodyPanel.removeAll();
    frontPanel.removeAll();
    rearPanel.removeAll();
    turretPanel.removeAll();
    dualTurretPanel.removeAll();
    fullTurretPanel.removeAll();
    rearLeftPanel.removeAll();
    rearRightPanel.removeAll();
    this.remove(fullTurretPanel);
    if (getTank() instanceof VTOL) {
        if (getTank().hasNoTurret()) {
            turretPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "Rotor", TitledBorder.TOP, TitledBorder.DEFAULT_POSITION));
            fullTurretPanel.add(turretPanel);
            this.add(fullTurretPanel);
        } else {
            dualTurretPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "Turret", TitledBorder.TOP, TitledBorder.DEFAULT_POSITION));
            fullTurretPanel.add(dualTurretPanel);
            turretPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "Rotor", TitledBorder.TOP, TitledBorder.DEFAULT_POSITION));
            fullTurretPanel.add(turretPanel);
            this.add(fullTurretPanel);
        }
    } else if (!getTank().hasNoDualTurret()) {
        dualTurretPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "Front Turret", TitledBorder.TOP, TitledBorder.DEFAULT_POSITION));
        fullTurretPanel.add(dualTurretPanel);
        turretPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "Rear Turret", TitledBorder.TOP, TitledBorder.DEFAULT_POSITION));
        fullTurretPanel.add(turretPanel);
        this.add(fullTurretPanel);
    } else if (!getTank().hasNoTurret()) {
        turretPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "Turret", TitledBorder.TOP, TitledBorder.DEFAULT_POSITION));
        fullTurretPanel.add(turretPanel);
        this.add(fullTurretPanel);
    }
    synchronized (getTank()) {
        for (int location = 0; location < getTank().locations(); location++) {
            // JPanel locationPanel = new JPanel();
            Vector<String> critNames = new Vector<String>(1, 1);
            for (int slot = 0; slot < getTank().getNumberOfCriticals(location); slot++) {
                CriticalSlot cs = getTank().getCritical(location, slot);
                if (cs == null) {
                    continue;
                } else if (cs.getType() == CriticalSlot.TYPE_SYSTEM) {
                    critNames.add(getMech().getSystemName(cs.getIndex()));
                } else if (cs.getType() == CriticalSlot.TYPE_EQUIPMENT) {
                    try {
                        Mounted m = cs.getMount();
                        // Critical didn't get removed. Remove it now.
                        if (m == null) {
                            m = cs.getMount();
                            if (m == null) {
                                getTank().setCritical(location, slot, null);
                                continue;
                            }
                            cs.setMount(m);
                        }
                        StringBuffer critName = new StringBuffer(m.getName());
                        if (critName.length() > 25) {
                            critName.setLength(25);
                            critName.append("...");
                        }
                        if (m.isRearMounted()) {
                            critName.append(" (R)");
                        }
                        if (m.isSponsonTurretMounted()) {
                            critName.append(" (ST)");
                        }
                        if (m.isPintleTurretMounted()) {
                            critName.append(" (PT)");
                        }
                        critNames.add(critName.toString());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
            if (critNames.size() == 0) {
                critNames.add(MtfFile.EMPTY);
            }
            DropTargetCriticalList<String> criticalSlotList = null;
            DropTargetCriticalList<String> dropTargetCriticalList = new DropTargetCriticalList<String>(critNames, eSource, refresh, showEmpty);
            criticalSlotList = dropTargetCriticalList;
            criticalSlotList.setVisibleRowCount(critNames.size());
            criticalSlotList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            criticalSlotList.setFont(new Font("Arial", Font.PLAIN, 10));
            criticalSlotList.setName(Integer.toString(location));
            criticalSlotList.setBorder(BorderFactory.createEtchedBorder(Color.WHITE.brighter(), Color.BLACK.darker()));
            if (!(getTank()).isSuperHeavy()) {
                switch(location) {
                    case Tank.LOC_FRONT:
                        frontPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_LEFT:
                        leftPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_RIGHT:
                        rightPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_BODY:
                        bodyPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_REAR:
                        rearPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_TURRET:
                        turretPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_TURRET_2:
                        dualTurretPanel.add(criticalSlotList);
                        break;
                }
            } else if (getTank() instanceof VTOL) {
                switch(location) {
                    case Tank.LOC_FRONT:
                        frontPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_LEFT:
                        leftPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_RIGHT:
                        rightPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_BODY:
                        bodyPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_REAR:
                        rearPanel.add(criticalSlotList);
                        break;
                    case VTOL.LOC_ROTOR:
                        turretPanel.add(criticalSlotList);
                        break;
                }
            } else {
                switch(location) {
                    case Tank.LOC_FRONT:
                        frontPanel.add(criticalSlotList);
                        break;
                    case SuperHeavyTank.LOC_FRONTLEFT:
                        leftPanel.add(criticalSlotList);
                        break;
                    case SuperHeavyTank.LOC_FRONTRIGHT:
                        rightPanel.add(criticalSlotList);
                        break;
                    case SuperHeavyTank.LOC_REARLEFT:
                        rearLeftPanel.add(criticalSlotList);
                        break;
                    case SuperHeavyTank.LOC_REARRIGHT:
                        rearRightPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_BODY:
                        bodyPanel.add(criticalSlotList);
                        break;
                    case SuperHeavyTank.LOC_REAR:
                        rearPanel.add(criticalSlotList);
                        break;
                    case SuperHeavyTank.LOC_TURRET:
                        turretPanel.add(criticalSlotList);
                        break;
                    case SuperHeavyTank.LOC_TURRET_2:
                        dualTurretPanel.add(criticalSlotList);
                        break;
                }
            }
        }
        middlePanel2.setVisible(getTank().isSuperHeavy() && !(getTank() instanceof VTOL));
        frontPanel.repaint();
        bodyPanel.repaint();
        leftPanel.repaint();
        rightPanel.repaint();
        rearLeftPanel.repaint();
        rearRightPanel.repaint();
        rearPanel.repaint();
        turretPanel.repaint();
        dualTurretPanel.repaint();
    }
}
Also used : VTOL(megamek.common.VTOL) DropTargetCriticalList(megameklab.com.util.DropTargetCriticalList) CriticalSlot(megamek.common.CriticalSlot) Mounted(megamek.common.Mounted) Vector(java.util.Vector) Font(java.awt.Font)

Example 27 with CriticalSlot

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

the class PrintMech method writeLocationCriticals.

private void writeLocationCriticals(int loc, SVGRectElement svgRect) {
    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();
    double rollX = viewX;
    double critX = viewX + viewWidth * 0.11;
    double gap = 0;
    if (mech.getNumberOfCriticals(loc) > 6) {
        gap = viewHeight * 0.05;
    }
    double lineHeight = (viewHeight - gap) / mech.getNumberOfCriticals(loc);
    double currY = viewY;
    float fontSize = (float) lineHeight * 0.85f;
    Mounted startingMount = null;
    double startingMountY = 0;
    double endingMountY = 0;
    double connWidth = viewWidth * 0.02;
    double x = viewX + viewWidth * 0.075;
    x += addTextElement(canvas, x, viewY - 1, mech.getLocationName(loc), fontSize * 1.25f, "start", "bold");
    if (mech.isClan() && UnitUtil.hasAmmo(mech, loc) && !mech.hasCASEII(loc)) {
        addTextElement(canvas, x + fontSize / 2, viewY - 1, "(CASE)", fontSize, "start", "normal");
    }
    for (int slot = 0; slot < mech.getNumberOfCriticals(loc); slot++) {
        currY += lineHeight;
        if (slot == 6) {
            currY += gap;
        }
        addTextElement(canvas, rollX, currY, ((slot % 6) + 1) + ".", fontSize, "start", "bold");
        CriticalSlot crit = mech.getCritical(loc, slot);
        String style = "bold";
        String fill = "#000000";
        if ((null == crit) || ((crit.getType() == CriticalSlot.TYPE_EQUIPMENT) && (!crit.getMount().getType().isHittable()))) {
            style = "standard";
            fill = "#3f3f3f";
            addTextElement(canvas, critX, currY, formatCritName(crit), fontSize, "start", style, fill);
        } else if (crit.isArmored()) {
            Element pip = createPip(critX, currY - fontSize * 0.8, fontSize * 0.4, 0.7);
            canvas.appendChild(pip);
            addTextElement(canvas, critX + fontSize, currY, formatCritName(crit), fontSize, "start", style, fill);
        } else if ((crit.getType() == CriticalSlot.TYPE_EQUIPMENT) && (crit.getMount().getType() instanceof MiscType) && (crit.getMount().getType().hasFlag(MiscType.F_MODULAR_ARMOR))) {
            String critName = formatCritName(crit);
            addTextElement(canvas, critX, currY, critName, fontSize, "start", style, fill);
            x = critX + getTextLength(critName, fontSize);
            double remainingW = viewX + viewWidth - x;
            double spacing = remainingW / 6.0;
            double radius = spacing * 0.25;
            double y = currY - lineHeight + spacing;
            double y2 = currY - spacing;
            x += spacing;
            for (int i = 0; i < 10; i++) {
                if (i == 5) {
                    x -= spacing * 5.5;
                    y = y2;
                }
                Element pip = createPip(x, y, radius, 0.5);
                canvas.appendChild(pip);
                x += spacing;
            }
        } else {
            addTextElement(canvas, critX, currY, formatCritName(crit), fontSize, "start", style, fill);
        }
        Mounted m = null;
        if ((null != crit) && (crit.getType() == CriticalSlot.TYPE_EQUIPMENT) && (crit.getMount().getType().isHittable()) && (crit.getMount().getType().getCriticals(mech) > (mech.isSuperHeavy() ? 2 : 1))) {
            m = crit.getMount();
        }
        if ((startingMount != null) && (startingMount != m)) {
            connectSlots(canvas, critX - 1, startingMountY, connWidth, endingMountY - startingMountY);
        }
        if (m != startingMount) {
            startingMount = m;
            if (null != m) {
                startingMountY = currY - lineHeight * 0.6;
            }
        } else {
            endingMountY = currY;
        }
    }
    if ((null != startingMount) && (mech.getNumberOfCriticals(startingMount.getType(), loc) > 1)) {
        connectSlots(canvas, critX - 1, startingMountY, connWidth, endingMountY - startingMountY);
    }
}
Also used : Mounted(megamek.common.Mounted) CriticalSlot(megamek.common.CriticalSlot) SVGRectElement(org.w3c.dom.svg.SVGRectElement) Element(org.w3c.dom.Element) MiscType(megamek.common.MiscType) Rectangle2D(java.awt.geom.Rectangle2D)

Example 28 with CriticalSlot

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

the class BACriticalSuit method addMounted.

public void addMounted(int loc, Mounted m) {
    // Don't mount unmounted equipment
    if (loc == BattleArmor.MOUNT_LOC_NONE) {
        return;
    }
    // AP Weapons that are mounted in an AP Mount don't take up slots
    if (m.isAPMMounted() && m.getLinkedBy() != null && m.getLinkedBy().getType().hasFlag(MiscType.F_AP_MOUNT)) {
        return;
    }
    // as they get a special slot added for them
    if (m.getType().hasFlag(MiscType.F_BA_MANIPULATOR)) {
        int slot = crits[loc].length - 1;
        crits[loc][slot] = new CriticalSlot(m);
    }
    int critsToAdd;
    if (m.getType().isSpreadable()) {
        critsToAdd = 1;
    } else {
        critsToAdd = m.getCriticals();
    }
    if (critsToAdd == 0) {
        return;
    }
    for (int slot = 0; slot < getNumCriticals(loc); slot++) {
        if (crits[loc][slot] == null) {
            crits[loc][slot] = new CriticalSlot(m);
            critsToAdd--;
            if (critsToAdd <= 0) {
                break;
            }
        }
    }
}
Also used : CriticalSlot(megamek.common.CriticalSlot)

Example 29 with CriticalSlot

use of megamek.common.CriticalSlot 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 30 with CriticalSlot

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

CriticalSlot (megamek.common.CriticalSlot)38 Mounted (megamek.common.Mounted)28 EntityLoadingException (megamek.common.loaders.EntityLoadingException)11 BattleArmor (megamek.common.BattleArmor)7 MiscType (megamek.common.MiscType)7 Vector (java.util.Vector)6 TestBattleArmor (megamek.common.verifier.TestBattleArmor)6 WeaponType (megamek.common.WeaponType)5 Font (java.awt.Font)4 ArrayList (java.util.ArrayList)4 EquipmentType (megamek.common.EquipmentType)4 LocationFullException (megamek.common.LocationFullException)4 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)3 IOException (java.io.IOException)3 AmmoType (megamek.common.AmmoType)3 Dimension (java.awt.Dimension)2 Transferable (java.awt.datatransfer.Transferable)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 InputEvent (java.awt.event.InputEvent)2