Package megamek.common

Examples of megamek.common.Tank


        Vector<Report> vDesc = new Vector<Report>();
        Report r;

        // Handle hits on "critical slots" of tanks.
        if (en instanceof Tank) {
            Tank t = (Tank) en;
            HitData hit;
            switch (cs.getIndex()) {
            case Tank.CRIT_NONE:
                // no effect
                r = new Report(6005);
                r.subject = t.getId();
                vDesc.add(r);
                break;
            case Tank.CRIT_AMMO:
                // ammo explosion
                r = new Report(6610);
                r.subject = t.getId();
                vDesc.add(r);
                int damage = 0;
                for (Mounted m : t.getAmmo()) {
                    m.setHit(true);
                    int tmp = m.getShotsLeft() * ((AmmoType) m.getType()).getDamagePerShot() * ((AmmoType) m.getType()).getRackSize();
                    m.setShotsLeft(0);
                    // non-explosive ammo can't explode
                    if (!m.getType().isExplosive()) {
                        continue;
                    }
                    damage += tmp;
                    r = new Report(6390);
                    r.subject = t.getId();
                    r.add(m.getName());
                    r.add(tmp);
                    r.newlines = 0;
                    vDesc.add(r);
                }
                hit = new HitData(loc);
                vDesc.addAll(damageEntity(t, hit, damage, true));
                break;
            case Tank.CRIT_CARGO:
                // Cargo/infantry damage
                r = new Report(6615);
                r.subject = t.getId();
                vDesc.add(r);
                Vector<Entity> passengers = t.getLoadedUnits();
                Entity target = passengers.get(Compute.randomInt(passengers.size()));
                hit = target.rollHitLocation(ToHitData.HIT_NORMAL, ToHitData.SIDE_FRONT);
                // FIXME should be original weapon damage
                vDesc.addAll(damageEntity(target, hit, 5));
                break;
            case Tank.CRIT_COMMANDER:
                if (en.crew.getOptions().booleanOption("vdni") || en.crew.getOptions().booleanOption("bvdni")) {
                    r = new Report(6191);
                    r.subject = t.getId();
                    vDesc.add(r);
                    vDesc.addAll(damageCrew(en, 1));
                } else {
                    if (en.crew.getOptions().booleanOption("pain_shunt") && !t.isCommanderHitPS()) {
                        r = new Report(6606);
                        r.subject = t.getId();
                        vDesc.add(r);
                        t.setCommanderHitPS(true);
                    } else {
                        r = new Report(6605);
                        r.subject = t.getId();
                        vDesc.add(r);
                        t.setCommanderHit(true);
                    }
                }
                // fall through here, because effects of crew stunned also apply
            case Tank.CRIT_CREW_STUNNED:
                if (en.crew.getOptions().booleanOption("vdni") || en.crew.getOptions().booleanOption("bvdni")) {
                    r = new Report(6191);
                    r.subject = t.getId();
                    vDesc.add(r);
                    vDesc.addAll(damageCrew(en, 1));
                } else {
                    if (en.crew.getOptions().booleanOption("pain_shunt")
                            || en.crew.getOptions().booleanOption("dermal_armor")) {
                        r = new Report(6186);
                        r.subject = t.getId();
                        vDesc.add(r);
                    } else {
                        t.stunCrew();
                        r = new Report(6185);
                        r.add(t.getStunnedTurns() - 1);
                        r.subject = t.getId();
                        vDesc.add(r);
                    }
                }
                break;
            case Tank.CRIT_DRIVER:
                if (en.crew.getOptions().booleanOption("vdni") || en.crew.getOptions().booleanOption("bvdni")) {
                    r = new Report(6191);
                    r.subject = t.getId();
                    vDesc.add(r);
                    vDesc.addAll(damageCrew(en, 1));
                } else {
                    if (en.crew.getOptions().booleanOption("pain_shunt") && !t.isDriverHitPS()) {
                        r = new Report(6601);
                        r.subject = t.getId();
                        vDesc.add(r);
                        t.setDriverHitPS(true);
                    } else {
                        r = new Report(6600);
                        r.subject = t.getId();
                        vDesc.add(r);
                        t.setDriverHit(true);
                    }
                }
                break;
            case Tank.CRIT_CREW_KILLED:
                if (en.crew.getOptions().booleanOption("vdni") || en.crew.getOptions().booleanOption("bvdni")) {
                    r = new Report(6191);
                    r.subject = t.getId();
                    vDesc.add(r);
                    vDesc.addAll(damageCrew(en, 1));
                } else {
                    if (en.crew.getOptions().booleanOption("pain_shunt") && !t.isCrewHitPS()) {
                        r = new Report(6191);
                        r.subject = t.getId();
                        vDesc.add(r);
                        t.setCrewHitPS(true);
                    } else {
                        r = new Report(6190);
                        r.subject = t.getId();
                        vDesc.add(r);
                        t.getCrew().setDoomed(true);
                    }
                }
                break;
            case Tank.CRIT_ENGINE:
                r = new Report(6210);
                r.subject = t.getId();
                vDesc.add(r);
                t.engineHit();
                t.engineHitsThisRound++;
                boolean engineExploded = checkEngineExplosion(t, vDesc, 1);
                if (engineExploded) {
                    vDesc.addAll(destroyEntity(en, "engine destruction", true, true));
                }

                if (t instanceof VTOL) {
                    PilotingRollData psr = t.getBasePilotingRoll();
                    IHex hex = game.getBoard().getHex(t.getPosition());
                    psr.addModifier(4, "forced landing");
                    int elevation = Math.max(hex.terrainLevel(Terrains.BLDG_ELEV), hex.terrainLevel(Terrains.BRIDGE_ELEV));
                    elevation = Math.max(elevation, 0);
                    elevation = Math.min(elevation, t.getElevation());
                    if (t.getElevation() > elevation) {
                        if (!hex.containsTerrain(Terrains.FUEL_TANK) && !hex.containsTerrain(Terrains.JUNGLE) && !hex.containsTerrain(Terrains.MAGMA) && !hex.containsTerrain(Terrains.MUD) && !hex.containsTerrain(Terrains.RUBBLE) && !hex.containsTerrain(Terrains.WATER) && !hex.containsTerrain(Terrains.WOODS)) {
                            r = new Report(2180);
                            r.subject = t.getId();
                            r.addDesc(t);
                            r.add(psr.getLastPlainDesc(), true);
                            vDesc.add(r);

                            // roll
                            final int diceRoll = Compute.d6(2);
                            r = new Report(2185);
                            r.subject = t.getId();
                            r.add(psr.getValueAsString());
                            r.add(psr.getDesc());
                            r.add(diceRoll);
                            if (diceRoll < psr.getValue()) {
                                r.choose(false);
                                vDesc.add(r);
                                vDesc.addAll(crashVTOLorWiGE(t));
                            } else {
                                r.choose(true);
                                vDesc.add(r);
                                t.setElevation(elevation);
                            }
                        } else {
                            vDesc.addAll(crashVTOLorWiGE(t));
                        }
                    }
                }
                break;
            case Tank.CRIT_FUEL_TANK:
                r = new Report(6215);
                r.subject = t.getId();
                vDesc.add(r);
                vDesc.addAll(destroyEntity(t, "fuel explosion", false, false));
                break;
            case Tank.CRIT_SENSOR:
                r = new Report(6620);
                r.subject = t.getId();
                vDesc.add(r);
                t.setSensorHits(t.getSensorHits() + 1);
                break;
            case Tank.CRIT_STABILIZER:
                r = new Report(6625);
                r.subject = t.getId();
                vDesc.add(r);
                t.setStabiliserHit(loc);
                break;
            case Tank.CRIT_TURRET_DESTROYED:
                r = new Report(6630);
                r.subject = t.getId();
                vDesc.add(r);
                t.destroyLocation(Tank.LOC_TURRET);
                vDesc.addAll(destroyEntity(t, "turret blown off", true, true));
                break;
            case Tank.CRIT_TURRET_JAM:
                if (t.isTurretEverJammed()) {
                    r = new Report(6640);
                    r.subject = t.getId();
                    vDesc.add(r);
                    t.lockTurret();
                    break;
                }
                r = new Report(6635);
                r.subject = t.getId();
                vDesc.add(r);
                t.jamTurret();
                break;
            case Tank.CRIT_TURRET_LOCK:
                r = new Report(6640);
                r.subject = t.getId();
                vDesc.add(r);
                t.lockTurret();
                break;
            case Tank.CRIT_WEAPON_DESTROYED: {
                r = new Report(6305);
                r.subject = t.getId();
                ArrayList<Mounted> weapons = new ArrayList<Mounted>();
                for (Mounted weap : t.getWeaponList()) {
                    if ((weap.getLocation() == loc) && !weap.isHit() && !weap.isDestroyed()) {
                        weapons.add(weap);
                    }
                }
                // sort weapons by BV
                Collections.sort(weapons, new WeaponComparator());
                int roll = Compute.d6();
                Mounted weapon;
                if (roll < 4) {
                    // defender should choose, we'll just use the lowest BV
                    // weapon
                    weapon = weapons.get(weapons.size() - 1);
                } else {
                    // attacker chooses, we'll use the highest BV weapon
                    weapon = weapons.get(0);
                }
                weapon.setHit(true);
                r.add(weapon.getName());
                vDesc.add(r);
                // explosive weapons e.g. gauss now explode
                vDesc.addAll(explodeEquipment(t, loc, weapon));
                weapon.setDestroyed(true);
                break;
            }
            case Tank.CRIT_WEAPON_JAM: {
                r = new Report(6645);
                r.subject = t.getId();
                ArrayList<Mounted> weapons = new ArrayList<Mounted>();
                for (Mounted weap : t.getWeaponList()) {
                    if ((weap.getLocation() == loc) && !weap.isJammed() && !weap.isHit() && !weap.isDestroyed()) {
                        weapons.add(weap);
                    }
                }
                Mounted weapon = weapons.get(Compute.randomInt(weapons.size()));
                weapon.setJammed(true);
                t.addJammedWeapon(weapon);
                r.add(weapon.getName());
                vDesc.add(r);
                break;
            }
            case VTOL.CRIT_PILOT:
                r = new Report(6650);
                r.subject = t.getId();
                vDesc.add(r);
                t.setDriverHit(true);
                PilotingRollData psr = t.getBasePilotingRoll();
                psr.addModifier(0, "pilot injury");
                if (!doSkillCheckInPlace(t, psr)) {
                    r = new Report(6675);
                    r.subject = t.getId();
                    r.addDesc(t);
                    vDesc.add(r);
                    boolean crash = true;
                    if (t.canGoDown()) {
                        t.setElevation(t.getElevation() - 1);
                        crash = !t.canGoDown();
                    }
                    if (crash) {
                        vDesc.addAll(crashVTOLorWiGE(t));
                    }
                }
                break;
            case VTOL.CRIT_COPILOT:
                r = new Report(6655);
                r.subject = t.getId();
                vDesc.add(r);
                t.setCommanderHit(true);
                break;
            case VTOL.CRIT_ROTOR_DAMAGE: {
                r = new Report(6660);
                r.subject = t.getId();
                vDesc.add(r);
                int mp = t.getOriginalWalkMP();
                if (mp > 1) {
                    t.setOriginalWalkMP(mp - 1);
                } else if (mp == 1) {
                    t.setOriginalWalkMP(0);
                    vDesc.addAll(crashVTOLorWiGE(t));
                }
                break;
            }
            case VTOL.CRIT_ROTOR_DESTROYED:
                r = new Report(6670);
                r.subject = t.getId();
                vDesc.add(r);
                t.immobilize();
                t.destroyLocation(VTOL.LOC_ROTOR);
                vDesc.addAll(crashVTOLorWiGE(t));
                break;
            case VTOL.CRIT_FLIGHT_STABILIZER:
                r = new Report(6665);
                r.subject = t.getId();
                vDesc.add(r);
                t.setStabiliserHit(VTOL.LOC_ROTOR);
                break;
            }
        } else if (en instanceof Aero) {
            Aero a = (Aero) en;

View Full Code Here


        }

        // Vehicles may suffer from criticals
        if (ae instanceof Tank) {
            Tank tank = (Tank) ae;
            if (tank.isCommanderHit()) {
                if (ae instanceof VTOL) {
                    toHit.addModifier(+1, "copilot injured");
                } else {
                    toHit.addModifier(+1, "commander injured");
                }
            }
            int sensors = tank.getSensorHits();
            if (sensors > 0) {
                toHit.addModifier(sensors, "sensor damage");
            }
            if (tank.isStabiliserHit(weapon.getLocation())) {
                toHit.addModifier(Compute.getAttackerMovementModifier(game,
                        tank.getId()).getValue(), "stabiliser damage");
            }
        }

        if (ae.hasFunctionalArmAES(weapon.getLocation()) && !weapon.isSplit()) {
            toHit.addModifier(-1, "AES modifer");
View Full Code Here

            // on a tank, remove turret if its missing
            // also, remove body
            if (target instanceof Tank) {
                mask[Tank.LOC_BODY] = false;
                Tank tank = (Tank) target;
                if (tank.hasNoTurret()) {
                    mask[Tank.LOC_TURRET] = false;
                }
                // remove non-visible sides
                if (target instanceof LargeSupportTank) {
                    if (side == ToHitData.SIDE_FRONTLEFT) {
View Full Code Here

            // on a tank, remove turret if its missing
            // also, remove body
            if (target instanceof Tank) {
                mask[Tank.LOC_BODY] = false;
                Tank tank = (Tank) target;
                if (tank.hasNoTurret()) {
                    mask[Tank.LOC_TURRET] = false;
                }
                // remove non-visible sides
                if (target instanceof LargeSupportTank) {
                    if (side == ToHitData.SIDE_FRONTLEFT) {
View Full Code Here

    public Vector<BackGroundDrawer> getBackgroundDrawers() {
        return bgDrawers;
    }

    public void setEntity(Entity e) {
        Tank t = (Tank) e;
        int a = 1;
        int a0 = 1;
        for (int i = 1; i < 6; i++) {
            a = t.getArmor(i);
            a0 = t.getOArmor(i);
            vLabels[i].setValue(t.getArmorString(i));
            WidgetUtils.setAreaColor(areas[i], vLabels[i], (double) a
                    / (double) a0);
        }
        for (int i = 7; i < 12; i++) {
            a = t.getInternal(i - 6);
            a0 = t.getOInternal(i - 6);
            vLabels[i].setValue(t.getInternalString(i - 6));
            WidgetUtils.setAreaColor(areas[i], vLabels[i], (double) a
                    / (double) a0);
        }
        if (t.hasBARArmor()) {
            vLabels[12].setValue(String.valueOf(((SupportTank)t).getBARRating()));
        } else {
            labels[12].setVisible(false);
            vLabels[12].setVisible(false);
        }
View Full Code Here

    public Vector<BackGroundDrawer> getBackgroundDrawers() {
        return bgDrawers;
    }

    public void setEntity(Entity e) {
        Tank t = (Tank) e;
        int a = 1;
        int a0 = 1;
        for (int i = 1; i < 6; i++) {
            a = t.getArmor(i);
            a0 = t.getOArmor(i);
            vLabels[i].setValue(t.getArmorString(i));
            WidgetUtils.setAreaColor(areas[i], vLabels[i], (double) a
                    / (double) a0);
        }
        for (int i = 7; i < 12; i++) {
            a = t.getInternal(i - 6);
            a0 = t.getOInternal(i - 6);
            vLabels[i].setValue(t.getInternalString(i - 6));
            WidgetUtils.setAreaColor(areas[i], vLabels[i], (double) a
                    / (double) a0);
        }
        if (t.hasBARArmor()) {
            vLabels[12].setValue(String.valueOf(((SupportTank)t).getBARRating()));
        } else {
            labels[12].setVisible(false);
            vLabels[12].setVisible(false);
        }
View Full Code Here

        return false;
    }

    public Entity getEntity() throws EntityLoadingException {
        try {
            Tank vehicle = null;

            if ((movementType == HMVMovementType.TRACKED)
                    || (movementType == HMVMovementType.WHEELED)
                    || (movementType == HMVMovementType.HOVER)
                    || (movementType == HMVMovementType.DISPLACEMENT_HULL)
                    || (movementType == HMVMovementType.HYDROFOIL)
                    || (movementType == HMVMovementType.SUBMARINE)) {
                vehicle = new Tank();
            } else if (movementType == HMVMovementType.VTOL) {
                vehicle = new VTOL();
            } else {
                throw new EntityLoadingException(
                        "Unsupported vehicle movement type:" + movementType);
            }

            vehicle.setChassis(name);
            vehicle.setModel(model);
            vehicle.setYear(year);
            vehicle.setOmni(isOmni);
            vehicle.setFluff(fluff);

            int techLevel = TechConstants.T_IS_ADVANCED;
            if (rulesLevel == 1) {
                techLevel = TechConstants.T_INTRO_BOXSET;
            } else if (rulesLevel == 2) {
                techLevel = techType == HMVTechType.CLAN ? TechConstants.T_CLAN_TW
                        : TechConstants.T_IS_TW_NON_BOX;
            } else if (techType == HMVTechType.CLAN) {
                techLevel = TechConstants.T_CLAN_ADVANCED;
            }

            vehicle.setTechLevel(techLevel);

            if (vehicle instanceof VTOL) {
                vehicle.setMovementMode(IEntityMovementMode.VTOL);
            } else {
                vehicle
                        .setMovementMode(movementType == HMVMovementType.DISPLACEMENT_HULL ? IEntityMovementMode.NAVAL
                                : movementType == HMVMovementType.HYDROFOIL ? IEntityMovementMode.HYDROFOIL
                                        : movementType == HMVMovementType.HOVER ? IEntityMovementMode.HOVER
                                                : movementType == HMVMovementType.WHEELED ? IEntityMovementMode.WHEELED
                                                        : movementType == HMVMovementType.SUBMARINE ? IEntityMovementMode.SUBMARINE
                                                        : IEntityMovementMode.TRACKED);
            }

            // This next line sets the weight to a rounded value
            // so that the suspension factor can be retrieved. The
            // real weight is set below that. Why is tonnage not directly
            // stated in the HMV file?!
            vehicle.setWeight(roundedInternalStructure * 10); // temporary
            int suspensionFactor = vehicle.getSuspensionFactor();
            vehicle.setWeight((engineRating + suspensionFactor) / cruiseMP);

            int engineFlags = Engine.TANK_ENGINE;
            if ((techType == HMVTechType.CLAN)
                    || (engineTechType == HMVTechType.CLAN)) {
                engineFlags |= Engine.CLAN_ENGINE;
            }
            vehicle
                    .setEngine(new Engine(engineRating, Engine
                            .getEngineTypeByString(engineType.toString()),
                            engineFlags));

            vehicle.setOriginalJumpMP(jumpMP);

            // hmmm...
            vehicle.setHasNoTurret(!hasTurret);

            vehicle.autoSetInternal();
            vehicle.setArmorType(armorType.toString());
            if (armorTechType == HMVTechType.CLAN) {
                switch (rulesLevel) {
                case 2:
                    vehicle.setArmorTechLevel(TechConstants.T_CLAN_TW);
                    break;
                case 3:
                    vehicle.setArmorTechLevel(TechConstants.T_CLAN_ADVANCED);
                    break;
                default:
                    throw new EntityLoadingException(
                            "Unsupported tech level: " + rulesLevel);
                }
            } else {
                switch (rulesLevel) {
                case 1:
                    vehicle.setArmorTechLevel(TechConstants.T_INTRO_BOXSET);
                    break;
                case 2:
                    vehicle.setArmorTechLevel(TechConstants.T_IS_TW_NON_BOX);
                    break;
                case 3:
                    vehicle.setArmorTechLevel(TechConstants.T_IS_ADVANCED);
                    break;
                default:
                    throw new EntityLoadingException(
                            "Unsupported tech level: " + rulesLevel);
                }
            }

            vehicle.initializeArmor(frontArmor, Tank.LOC_FRONT);
            vehicle.initializeArmor(leftArmor, Tank.LOC_LEFT);
            vehicle.initializeArmor(rightArmor, Tank.LOC_RIGHT);
            vehicle.initializeArmor(rearArmor, Tank.LOC_REAR);
            if (vehicle instanceof VTOL) {
                vehicle.initializeArmor(turretArmor, VTOL.LOC_ROTOR);
            } else if (!vehicle.hasNoTurret()) {
                vehicle.initializeArmor(turretArmor, Tank.LOC_TURRET);
            }

            addEquipment(vehicle, HMVWeaponLocation.FRONT, Tank.LOC_FRONT);
            addEquipment(vehicle, HMVWeaponLocation.LEFT, Tank.LOC_LEFT);
            addEquipment(vehicle, HMVWeaponLocation.RIGHT, Tank.LOC_RIGHT);
            addEquipment(vehicle, HMVWeaponLocation.REAR, Tank.LOC_REAR);
            if (!vehicle.hasNoTurret()) {
                addEquipment(vehicle, HMVWeaponLocation.TURRET, Tank.LOC_TURRET);
            }

            addEquipment(vehicle, HMVWeaponLocation.BODY, Tank.LOC_BODY);

            // Do we have any infantry/cargo bays?
            int capacity = (int) Math.round(Math.floor(troopSpace));
            if (capacity > 0) {
                vehicle.addTransporter(new TroopSpace(capacity));
            }

            addFailedEquipment(vehicle);

            return vehicle;
View Full Code Here

        dataFile = bb;
    }

    public Entity getEntity() throws EntityLoadingException {

        Tank t = new Tank();

        if (!dataFile.exists("Name")) {
            throw new EntityLoadingException("Could not find name block.");
        }
        t.setChassis(dataFile.getDataAsString("Name")[0]);
        if (dataFile.exists("Model") && (dataFile.getDataAsString("Model")[0] != null)) {
            t.setModel(dataFile.getDataAsString("Model")[0]);
        } else {
            t.setModel("");
        }

        setTechLevel(t);

        if (dataFile.exists("source")) {
            t.setSource(dataFile.getDataAsString("source")[0]);
        }

        if (!dataFile.exists("tonnage")) {
            throw new EntityLoadingException("Could not find weight block.");
        }
        t.setWeight(dataFile.getDataAsFloat("tonnage")[0]);

        if (!dataFile.exists("motion_type")) {
            throw new EntityLoadingException("Could not find movement block.");
        }
        String sMotion = dataFile.getDataAsString("motion_type")[0];
        int nMotion = -1;
        for (int x = 0; x < MOVES.length; x++) {
            if (sMotion.equalsIgnoreCase(MOVES[x])) {
                nMotion = x;
                break;
            }
        }
        if (nMotion == -1) {
            throw new EntityLoadingException("Invalid movment type: " + sMotion);
        }
        t.setMovementMode(nMotion);

        if (dataFile.exists("transporters")) {
            String[] transporters = dataFile.getDataAsString("transporters");
            // Walk the array of transporters.
            for (String transporter : transporters) {
                // TroopSpace:
                if (transporter.startsWith("TroopSpace:", 0)) {
                    // Everything after the ':' should be the space's size.
                    Double fsize = new Double(transporter.substring(11));
                    int size = fsize.intValue();
                    t.addTransporter(new TroopSpace(size));
                }

            } // Handle the next transportation component.

        } // End has-transporters

        int engineCode = BLKFile.FUSION;
        if (dataFile.exists("engine_type")) {
            engineCode = dataFile.getDataAsInt("engine_type")[0];
        }
        int engineFlags = Engine.TANK_ENGINE;
        if (t.isClan()) {
            engineFlags |= Engine.CLAN_ENGINE;
        }
        if (!dataFile.exists("cruiseMP")) {
            throw new EntityLoadingException("Could not find cruiseMP block.");
        }
        int engineRating = dataFile.getDataAsInt("cruiseMP")[0] * (int) t.getWeight() - t.getSuspensionFactor();
        t.setEngine(new Engine(engineRating, BLKFile.translateEngineCode(engineCode), engineFlags));

        if (dataFile.exists("armor_type")) {
            t.setArmorType(dataFile.getDataAsInt("armor_type")[0]);
        }
        if (dataFile.exists("armor_tech")) {
            t.setArmorTechLevel(dataFile.getDataAsInt("armor_tech")[0]);
        }
        if (dataFile.exists("internal_type")) {
            t.setStructureType(dataFile.getDataAsInt("internal_type")[0]);
        }

        if (!dataFile.exists("armor")) {
            throw new EntityLoadingException("Could not find armor block.");
        }

        int[] armor = dataFile.getDataAsInt("armor");

        if ((armor.length < 4) || (armor.length > 5)) {
            throw new EntityLoadingException("Incorrect armor array length");
        }

        t.setHasNoTurret(armor.length == 4);

        // add the body to the armor array
        int[] fullArmor = new int[armor.length + 1];
        fullArmor[0] = 0;
        System.arraycopy(armor, 0, fullArmor, 1, armor.length);
        for (int x = 0; x < fullArmor.length; x++) {
            t.initializeArmor(fullArmor[x], x);
        }

        t.autoSetInternal();

        loadEquipment(t, "Front", Tank.LOC_FRONT);
        loadEquipment(t, "Right", Tank.LOC_RIGHT);
        loadEquipment(t, "Left", Tank.LOC_LEFT);
        loadEquipment(t, "Rear", Tank.LOC_REAR);
        if (!t.hasNoTurret()) {
            loadEquipment(t, "Turret", Tank.LOC_TURRET);
        }
        loadEquipment(t, "Body", Tank.LOC_BODY);

        if (dataFile.exists("omni")) {
            t.setOmni(true);
        }
        return t;
    }
View Full Code Here

     * @throws <code>IllegalArgumentException</code> if the entity is
     *             <code>null</code>.
     * @throws <code>IOException</code> if there's any error on write.
     */
    public static void encode(Entity entity, Writer out) throws IOException {
        Tank tank = (Tank) entity;
        int value;

        // First, validate our input.
        if (null == entity) {
            throw new IllegalArgumentException("The entity is null.");
        }
        if (null == out) {
            throw new IllegalArgumentException("The writer is null.");
        }

        // Our EntityEncoder already gave us our root element.
        out.write("<hasNoTurret value=\"");
        out.write(tank.hasNoTurret() ? "true" : "false");
        out.write("\" /><stunnedTurns value=\"");
        value = tank.getStunnedTurns();
        out.write(String.valueOf(value));
        out.write("\" /><moveHit value=\"");
        out.write(tank.isMovementHit() ? "true" : "false");
        out.write("\" /><moveHitPending value=\"");
        out.write(tank.isMovementHitPending() ? "true" : "false");

        // Is the turret locked?
        if (tank.isTurretLocked()) {
            // Yup. Record the current facing of the tank and the turret.
            out.write("\" /><facing value=\"");
            value = tank.getFacing();
            out.write(String.valueOf(value));
            out.write("\" /><turretFacing value=\"");
            value = tank.getSecondaryFacing();
            out.write(String.valueOf(value));
            out.write("\" /><turretLocked value=\"true");
        }
        out.write("\" />");
    }
View Full Code Here

     *             <code>null</code>.
     * @throws <code>IllegalStateException</code> if the node does not contain
     *             a valid <code>Entity</code>.
     */
    public static Entity decode(ParsedXML node, IGame game) {
        Tank entity = null;
        String attrStr;
        int attrVal;

        // Did we get a null node?
        if (null == node) {
            throw new IllegalArgumentException("The Tank node is null.");
        }

        // Make sure that the node is for an Tank unit.
        attrStr = node.getAttribute("name");
        if (!node.getName().equals("class") || null == attrStr
                || !attrStr.equals("Tank")) {
            throw new IllegalStateException("Not passed an Tank node.");
        }

        // TODO : perform version checking.

        // Create the entity.
        entity = new Tank();

        // Walk the board node's children.
        Enumeration<?> children = node.elements();
        while (children.hasMoreElements()) {
            ParsedXML child = (ParsedXML) children.nextElement();
            String childName = child.getName();

            // Handle null child names.
            if (null == childName) {

                // No-op.
            }

            // Did we find the stunnedTurns node?
            else if (childName.equals("stunnedTurns")) {

                // Get the Tank's stunned turns.
                attrStr = child.getAttribute("value");
                if (null == attrStr) {
                    throw new IllegalStateException(
                            "Couldn't decode the stunnedTurns for a Tank unit.");
                }

                // Try to pull the number from the attribute string
                try {
                    attrVal = Integer.parseInt(attrStr);
                } catch (NumberFormatException exp) {
                    throw new IllegalStateException(
                            "Couldn't get an integer from " + attrStr);
                }
                entity.setStunnedTurns(attrVal);
            }

            // Did we find the hasNoTurret node?
            else if (childName.equals("hasNoTurret")) {

                // See if the Tank has a no turret.
                attrStr = child.getAttribute("value");
                if (null == attrStr) {
                    throw new IllegalStateException(
                            "Couldn't decode hasNoTurret for a Tank unit.");
                }

                // If the value is "true", the Tank has a no turret.
                if (attrStr.equals("true")) {
                    entity.setHasNoTurret(true);
                } else {
                    entity.setHasNoTurret(false);
                }
            }

            // Did we find the moveHit node?
            else if (childName.equals("moveHit")) {

                // See if the Tank has a move hit.
                attrStr = child.getAttribute("value");
                if (null == attrStr) {
                    throw new IllegalStateException(
                            "Couldn't decode moveHit for a Tank unit.");
                }

                // If the value is "true", the Tank move a hit pending.
                if (attrStr.equals("true")) {
                    entity.immobilize();
                    entity.applyDamage();
                }
            }

            // Did we find the moveHitPending node?
            else if (childName.equals("moveHitPending")) {

                // See if the Tank has a move hit pending.
                attrStr = child.getAttribute("value");
                if (null == attrStr) {
                    throw new IllegalStateException(
                            "Couldn't decode moveHitPending for a Tank unit.");
                }

                // If the value is "true", the Tank move a hit pending.
                if (attrStr.equals("true")) {
                    entity.immobilize();
                }
            }

            // Did we find the facing node?
            else if (childName.equals("facing")) {

                // Get the Tank's facing.
                attrStr = child.getAttribute("value");
                if (null == attrStr) {
                    throw new IllegalStateException(
                            "Couldn't decode the facing for a Tank unit.");
                }

                // Try to pull the number from the attribute string
                try {
                    attrVal = Integer.parseInt(attrStr);
                } catch (NumberFormatException exp) {
                    throw new IllegalStateException(
                            "Couldn't get an integer from " + attrStr);
                }
                entity.setFacing(attrVal);
            }

            // Did we find the turret's secondaryFacing node?
            else if (childName.equals("turretFacing")) {

                // Get the Tank's turret's facing.
                attrStr = child.getAttribute("value");
                if (null == attrStr) {
                    throw new IllegalStateException(
                            "Couldn't decode the turret's secondaryFacing for a Tank unit.");
                }

                // Try to pull the number from the attribute string
                try {
                    attrVal = Integer.parseInt(attrStr);
                } catch (NumberFormatException exp) {
                    throw new IllegalStateException(
                            "Couldn't get an integer from " + attrStr);
                }
                entity.setSecondaryFacing(attrVal);
            }

            // Did we find the turretLocked node?
            else if (childName.equals("turretLocked")) {

                // See if the Tank move a hit pending.
                attrStr = child.getAttribute("value");
                if (null == attrStr) {
                    throw new IllegalStateException(
                            "Couldn't decode turretLocked for a Tank unit.");
                }

                // If the value is "true", the Tank move a hit pending.
                if (attrStr.equals("true")) {
                    entity.lockTurret();
                }
            }

        } // Handle the next element.

View Full Code Here

TOP

Related Classes of megamek.common.Tank

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.