Package megamek.common

Examples of megamek.common.ITerrainFactory


     */
    public static IHex decode(ParsedXML node, IGame game) {
        String attrStr = null;
        int attrVal = 0;
        IHex retVal = null;
        ITerrainFactory f = Terrains.getTerrainFactory();

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

        // Make sure that the node is for a Hex object.
        if (!node.getName().equals("hex")) {
            throw new IllegalStateException("Not passed a hex node.");
        }

        // TODO : perform version checking.

        // Find the terrains node.
        Enumeration<?> children = node.elements();
        while (children.hasMoreElements()) {
            ParsedXML child = (ParsedXML) children.nextElement();
            if (child.getName().equals("terrains")) {

                // There should be only one terrains node.
                if (null != retVal) {
                    throw new IllegalStateException(
                            "More than one 'terrains' node in a hex node.");
                }

                // Create an array to hold all the terrains.
                ITerrain[] terrains = new ITerrain[Terrains.SIZE];

                // Walk through the subnodes, parsing out terrain nodes.
                Enumeration<?> subnodes = child.elements();
                while (subnodes.hasMoreElements()) {

                    // Is this a "terrain" node?
                    ParsedXML subnode = (ParsedXML) subnodes.nextElement();
                    if (subnode.getName().equals("terrain")) {

                        // Try to parse the terrain node.
                        try {
                            final int type = Integer.parseInt(subnode
                                    .getAttribute("type"));
                            final boolean exitsSpecified = StringUtil
                                    .parseBoolean(subnode
                                            .getAttribute("exitsSpecified"));
                            final int level = Integer.parseInt(subnode
                                    .getAttribute("level"));
                            final int exits = Integer.parseInt(subnode
                                    .getAttribute("exits"));
                            terrains[type] = f.createTerrain(type, level,
                                    exitsSpecified, exits);
                        } catch (Throwable thrown) {
                            throw new IllegalStateException(
                                    "Couldn't parse a terrain from a hex node.");
                        }
View Full Code Here


            return;

        Report r = new Report(5290);
        vPhaseReport.add(r);

        ITerrainFactory tf = Terrains.getTerrainFactory();
        for (Iterator<Coords> i = elevators[roll].positions.iterator(); i
                .hasNext();) {
            Coords c = i.next();
            IHex hex = server.getGame().getBoard().getHex(c);
            ITerrain terr = hex.getTerrain(Terrains.ELEVATOR);
            // Swap the elevator and hex elevations
            // Entity elevations are not adjusted. This makes sense for
            // everything except possibly
            // VTOLs - lets assume they take an updraft and remain at the same
            // height relative to the hex
            int elevation = hex.getElevation();
            hex.setElevation(terr.getLevel());
            hex.removeTerrain(Terrains.ELEVATOR);
            hex.addTerrain(tf.createTerrain(Terrains.ELEVATOR, elevation, true,
                    terr.getExits()));
            server.sendChangedHex(c);
        }
    }
View Full Code Here

            System.out.println(s + ": " + System.currentTimeMillis());
        }
    }

    private void resolveWeather() {
        ITerrainFactory tf = Terrains.getTerrainFactory();
        IBoard board = game.getBoard();
        int width = board.getWidth();
        int height = board.getHeight();
        PlanetaryConditions conditions = game.getPlanetaryConditions();
        boolean lightSnow = false;
        boolean deepSnow = false;
        boolean ice = false;
       
        if(!conditions.isTerrainAffected())
            return;
       
        debugTime("resolve weather 1", true);

        //first we need to increment the conditions
        if(conditions.getWeather() == PlanetaryConditions.WE_MOD_SNOW && game.getBoard().onGround()) {
            modSnowTurn = modSnowTurn + 1;
            if(modSnowTurn == 9) {
                lightSnow = true;
            }
            if(modSnowTurn == 19) {
                deepSnow = true;
                ice = true;
            }
        }
        if(conditions.getWeather() == PlanetaryConditions.WE_HEAVY_SNOW && game.getBoard().onGround()) { 
            heavySnowTurn = heavySnowTurn + 1;
            if(heavySnowTurn == 4) {
                lightSnow = true;
            }
            if(heavySnowTurn == 14) {
                deepSnow = true;
            }
            if(heavySnowTurn == 19) {
                ice = true;
            }
        }
        if(conditions.getWeather() == PlanetaryConditions.WE_SLEET && game.getBoard().onGround()) { 
            sleetTurn = sleetTurn + 1;
            if(sleetTurn == 14) {
                ice = true;
            }
        }
        if(conditions.getWeather() == PlanetaryConditions.WE_ICE_STORM && game.getBoard().onGround()) { 
            iceTurn = iceTurn + 1;
            if(iceTurn == 14) {
                ice = true;
            }
        }
       
        if(lightSnow) {
            Report r = new Report(5505, Report.PUBLIC);
            vPhaseReport.addElement(r);
        }
        if(deepSnow) {
            Report r = new Report(5510, Report.PUBLIC);
            vPhaseReport.addElement(r);
        }
        if(ice) {
            Report r = new Report(5515, Report.PUBLIC);
            vPhaseReport.addElement(r);
        }
           
        // Cycle through all hexes, checking for the appropriate weather changes
        for (int currentXCoord = 0; currentXCoord < width; currentXCoord++ ) {
            for (int currentYCoord = 0; currentYCoord < height; currentYCoord++) {
                Coords currentCoords = new Coords(currentXCoord, currentYCoord);
                IHex currentHex = board.getHex(currentXCoord, currentYCoord);

                //check for fires and potentially put them out
                if (currentHex.containsTerrain(Terrains.FIRE)) {
                    //only standard fires get put out
                    if(currentHex.terrainLevel(Terrains.FIRE) == 1) {
                        if(conditions.putOutFire()) {
                            server.removeFire(currentCoords, "weather conditions");   
                        }
                    } else {
                        //inferno fires should become regular fires
                        currentHex.removeTerrain(Terrains.FIRE);
                        currentHex.addTerrain(tf.createTerrain(Terrains.FIRE,1));
                        server.sendChangedHex(currentCoords);
                    }
                }  
               
                if(ice && !currentHex.containsTerrain(Terrains.ICE)
                        && currentHex.containsTerrain(Terrains.WATER)) {
                    currentHex.addTerrain(tf.createTerrain(Terrains.ICE, 1));
                    server.sendChangedHex(currentCoords);
                }
               
                if(lightSnow
                        && !currentHex.containsTerrain(Terrains.SNOW)
                        && !(currentHex.containsTerrain(Terrains.WATER) && !currentHex.containsTerrain(Terrains.ICE))
                        && !currentHex.containsTerrain(Terrains.MAGMA)) {
                    currentHex.addTerrain(tf.createTerrain(Terrains.SNOW, 1));
                    server.sendChangedHex(currentCoords);
                }
               
                if(deepSnow && !(currentHex.terrainLevel(Terrains.SNOW) > 1)
                        && !(currentHex.containsTerrain(Terrains.WATER) && !currentHex.containsTerrain(Terrains.ICE))
                        && !currentHex.containsTerrain(Terrains.MAGMA)) {
                    currentHex.addTerrain(tf.createTerrain(Terrains.SNOW, 2));
                    server.sendChangedHex(currentCoords);
                }
               
                //check for the melting of any snow or ice
                if(currentHex.terrainLevel(Terrains.SNOW) > 1
                        && currentHex.containsTerrain(Terrains.FIRE) && currentHex.getFireTurn() == 3) {
                    currentHex.removeTerrain(Terrains.SNOW);
                    if(!currentHex.containsTerrain(Terrains.MUD) && !currentHex.containsTerrain(Terrains.WATER)) {
                        currentHex.addTerrain(tf.createTerrain(Terrains.MUD, 1));
                    }
                }
               
                if(currentHex.terrainLevel(Terrains.SNOW) == 1
                        && currentHex.containsTerrain(Terrains.FIRE) && currentHex.getFireTurn() == 1) {
                    currentHex.removeTerrain(Terrains.SNOW);
                    if(!currentHex.containsTerrain(Terrains.MUD) && !currentHex.containsTerrain(Terrains.WATER)) {
                        currentHex.addTerrain(tf.createTerrain(Terrains.MUD, 1));
                    }
                }
               
                if(currentHex.containsTerrain(Terrains.ICE)
                        && currentHex.containsTerrain(Terrains.FIRE) && currentHex.getFireTurn() == 2) {
                    currentHex.removeTerrain(Terrains.ICE);
                    if(!currentHex.containsTerrain(Terrains.MUD) && !currentHex.containsTerrain(Terrains.WATER)) {
                        currentHex.addTerrain(tf.createTerrain(Terrains.MUD, 1));
                    }
                }
               
                //check for rapids/torrents created by wind
                //FIXME: This doesn't seem to be doing anything
                if(conditions.getWindStrength() > PlanetaryConditions.WI_MOD_GALE
                        && currentHex.containsTerrain(Terrains.WATER) && currentHex.depth() > 0) {
                   
                    if(conditions.getWindStrength() > PlanetaryConditions.WI_STORM) {
                        if(!(currentHex.terrainLevel(Terrains.RAPIDS) > 1)) {
                            currentHex.addTerrain(tf.createTerrain(Terrains.RAPIDS, 2));
                        }
                    } else {
                        if(!currentHex.containsTerrain(Terrains.RAPIDS)) {
                            currentHex.addTerrain(tf.createTerrain(Terrains.RAPIDS, 1));
                        }
                    }                  
                }
            }
        }
View Full Code Here

        return (hex.containsTerrain(Terrains.WATER) || hex
                .containsTerrain(Terrains.MAGMA));
    }

    private void addRoad(IHex hex, int exitDirection, int type) {
        ITerrainFactory tf = Terrains.getTerrainFactory();
        if (hex.containsTerrain(Terrains.WATER)) {
            hex.removeTerrain(Terrains.WATER);
            hex.addTerrain(tf.createTerrain(Terrains.WATER, 0));
            type = 1;
        }
        hex.addTerrain(tf.createTerrain(Terrains.ROAD, type, true,
                (1 << exitDirection) & 63));
    }
View Full Code Here

        hex.addTerrain(tf.createTerrain(Terrains.ROAD, type, true,
                (1 << exitDirection) & 63));
    }

    private void addBridge(IHex hex, int exits, int altitude, int cf) {
        ITerrainFactory tf = Terrains.getTerrainFactory();
        int bridgeElevation = altitude - hex.getElevation();

        hex.addTerrain(tf.createTerrain(Terrains.BRIDGE,
                getBuildingTypeByCF(cf), true, (exits & 63)));
        hex.addTerrain(tf.createTerrain(Terrains.BRIDGE_ELEV, bridgeElevation));
        hex.addTerrain(tf.createTerrain(Terrains.BRIDGE_CF, cf));
    }
View Full Code Here

    private static void placeBuilding(IBoard board, BuildingTemplate building) {
        int type = building.getType();
        int cf = building.getCF();
        int height = building.getHeight();
        ITerrainFactory tf = Terrains.getTerrainFactory();
        ArrayList<IHex> hexes = new ArrayList<IHex>();
        int level = 0;
        for (Iterator<Coords> i = building.getCoords(); i.hasNext();) {
            Coords c = i.next();
            IHex hex = board.getHex(c);
            // work out exits...
            int exits = 0;
            for (int dir = 0; dir < 6; dir++) {
                if (building.containsCoords(c.translated(dir))) {
                    exits |= (1 << dir);
                }
            }

            // remove everything
            hex.removeAllTerrains();
            hex.addTerrain(tf.createTerrain(Terrains.PAVEMENT, 1));
            hex.addTerrain(tf.createTerrain(Terrains.BUILDING, type, true,
                    exits));
            hex.addTerrain(tf.createTerrain(Terrains.BLDG_CF, cf));
            hex.addTerrain(tf.createTerrain(Terrains.BLDG_ELEV, height));
            // hex.addTerrain(tf.createTerrain(Terrains.BLDG_BASEMENT,
            // building.getBasement()));
            hexes.add(hex);
            level += hex.getElevation();
        }
View Full Code Here

            unUsed.add(field);
        } else {
            findAllUnused(board, terrainType, alreadyUsed, unUsed, field,
                    reverseHex);
        }
        ITerrainFactory f = Terrains.getTerrainFactory();
        for (int i = 0; i < count; i++) {
            if (unUsed.isEmpty()) {
                return;
            }
            int which = Compute.randomInt(unUsed.size());
            Iterator<IHex> iter = unUsed.iterator();
            for (int n = 0; n < (which - 1); n++)
                iter.next();
            field = iter.next();
            if (exclusive) {
                field.removeAllTerrains();
            }
            int tempInt = (Compute.randomInt(100) < probMore) ? 2 : 1;
            ITerrain tempTerrain = f.createTerrain(terrainType, tempInt);
            field.addTerrain(tempTerrain);
            unUsed.remove(field);
            findAllUnused(board, terrainType, alreadyUsed, unUsed, field,
                    reverseHex);
        }
View Full Code Here

                nextLeft = 4;
                break;
        } // switch
        /* place the river */
        field = board.getHex(p.x, p.y);
        ITerrainFactory f = Terrains.getTerrainFactory();
        do {
            /* first the hex itself */
            field.removeAllTerrains();
            field.addTerrain(f.createTerrain(Terrains.WATER, 1));
            riverHexes.add(field);
            p = reverseHex.get(field);
            /* then maybe the left and right neighbours */
            riverHexes.addAll(extendRiverToSide(board, p, Compute.randomInt(3),
                    nextLeft, reverseHex));
View Full Code Here

     * more elevation
     */
    protected static void postProcessFlood(IHex[] hexSet, int modifier) {
        int n;
        IHex field;
        ITerrainFactory f = Terrains.getTerrainFactory();
        for (n = 0; n < hexSet.length; n++) {
            field = hexSet[n];
            int elev = field.getElevation() - modifier;
            if (elev == 0 && !(field.containsTerrain(Terrains.WATER))
                    && !(field.containsTerrain(Terrains.PAVEMENT))) {
                field.addTerrain(f.createTerrain(Terrains.SWAMP, 1));
            } else if (elev < 0) {
                if (elev < -4)
                    elev = -4;
                field.removeAllTerrains();
                field.addTerrain(f.createTerrain(Terrains.WATER, -elev));
                field.setElevation(modifier);
            }
        }
    }
View Full Code Here

     * Converts water hexes to ice hexes. Works best with snow&ice theme.
     */
    protected static void postProcessDeepFreeze(IHex[] hexSet, int modifier) {
        int n;
        IHex field;
        ITerrainFactory f = Terrains.getTerrainFactory();
        for (n = 0; n < hexSet.length; n++) {
            field = hexSet[n];
            if (field.containsTerrain(Terrains.WATER)) {
                int level = field.terrainLevel(Terrains.WATER);
                if (modifier != 0) {
                    level -= modifier;
                    field.removeTerrain(Terrains.WATER);
                    if (level > 0) {
                        field.addTerrain(f.createTerrain(Terrains.WATER,
                                        level));
                    }
                }
                field.addTerrain(f.createTerrain(Terrains.ICE, 1));
            } else if (field.containsTerrain(Terrains.SWAMP)) {
                field.removeTerrain(Terrains.SWAMP);
                if (field.terrainsPresent() == 0) {
                    if (Compute.randomInt(100) < 30) {
                        // if no other terrains present, 30% chance to change to
                        // rough
                        field.addTerrain(f.createTerrain(Terrains.ROUGH, 1));
                    } else {
                        field.addTerrain(f.createTerrain(Terrains.ICE, 1));
                    }
                }
            }
        }
    }
View Full Code Here

TOP

Related Classes of megamek.common.ITerrainFactory

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.