⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 boardutilities.java

📁 MegaMek is a networked Java clone of BattleTech, a turn-based sci-fi boardgame for 2+ players. Fight
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            } 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));                    }                }            }        }            }        /**      * Burning woods, with chance to be burnt down already     */    protected static void PostProcessForestFire(IHex[] hexSet, int modifier) {        int n;        IHex field;        int level, newlevel;        int severity;         ITerrainFactory f = Terrains.getTerrainFactory();        for (n=0;n<hexSet.length;n++) {            field = hexSet[n];            level = field.terrainLevel(Terrains.WOODS);            if (level != ITerrain.LEVEL_NONE) {                severity = Compute.randomInt(5) - 2 + modifier;                newlevel = level - severity;                                if (newlevel <= level) {                    field.removeTerrain(Terrains.WOODS);                    if(newlevel <= 0) {                        field.addTerrain(f.createTerrain(Terrains.ROUGH,1));                    } else {                        field.addTerrain(f.createTerrain(Terrains.WOODS,newlevel));                        field.addTerrain(f.createTerrain(Terrains.FIRE,1));                        field.addTerrain(f.createTerrain(Terrains.SMOKE,1));                    }                }            }        }            }        /** Dries up all bodies of water by 1-3 levels.     * dried up water becomes swamp then rough     */    protected static void PostProcessDrought(IHex[] hexSet, int modifier) {        int n;        IHex field;        int level, newlevel;        int severity = 1 + Compute.randomInt(3) + modifier;        if(severity < 0)return;        ITerrainFactory f = Terrains.getTerrainFactory();        for (n=0;n<hexSet.length;n++) {            field = hexSet[n];            if(field.containsTerrain(Terrains.SWAMP)) {                field.removeTerrain(Terrains.SWAMP); //any swamps are dried up to hardened mud                if (field.terrainsPresent() == 0 &&                    Compute.randomInt(100) < 30) {                    //if no other terrains present, 30% chance to change to rough                    field.addTerrain(f.createTerrain(Terrains.ROUGH,1));                }            }            level = field.terrainLevel(Terrains.WATER);            if (level != ITerrain.LEVEL_NONE) {                newlevel = level - severity;                field.removeTerrain(Terrains.WATER);                if (newlevel == 0) {                    field.addTerrain(f.createTerrain(Terrains.SWAMP,1));                } else if(newlevel < 0) {                    field.addTerrain(f.createTerrain(Terrains.ROUGH,1));                } else {                    field.addTerrain(f.createTerrain(Terrains.WATER,newlevel));                }                if (level > severity)                    newlevel = severity;                else newlevel = level;                                field.setElevation(field.getElevation() - newlevel);            }        }    }        private static boolean hexCouldBeCliff(IBoard board, Coords c) {        int elevation = board.getHex(c).getElevation();        boolean higher = false;        boolean lower = false;        int count = 0;        for(int dir=0;dir < 6;dir++) {            Coords t = c.translated(dir);            if(board.contains(t)) {                IHex hex = board.getHex(t);                int el = hex.getElevation();                 if(el > elevation) {                    lower = true;                }                else if(el < elevation) {                    higher = true;                }                else {                    count++;                }            }        }        return higher && lower && count <= 3 && count > 0;    }        private static void findCliffNeighbours(IBoard board, Coords c, Vector<Coords> candidate, HashSet<Coords> ignore) {        candidate.add(c);        ignore.add(c);        int elevation = board.getHex(c).getElevation();        for(int dir=0;dir < 6;dir++) {            Coords t = c.translated(dir);            if(board.contains(t) && !ignore.contains(t)) {                if(hexCouldBeCliff(board, t)) {                    IHex hex = board.getHex(t);                    int el = hex.getElevation();                     if(el == elevation) {                        findCliffNeighbours(board, t, candidate, ignore);                    }                }                else ignore.add(t);            }        }    }        protected static void addCliffs(IBoard board, int modifier) {        HashSet<Coords> ignore = new HashSet(); //previously considered hexes        Vector<Coords> candidate = new Vector();        for(int x=0;x<board.getWidth();x++) {            for(int y=0;y<board.getHeight();y++) {                Coords c = new Coords(x,y);                int elevation = board.getHex(c).getElevation();                if(ignore.contains(c))continue;                if(!hexCouldBeCliff(board,c)) {                    ignore.add(c);                    continue;                }                findCliffNeighbours(board,c, candidate,ignore);                //is the candidate interesting (at least 3 hexes)?                if(candidate.size() >= 3 && Compute.randomInt(100) < modifier) {                    if(elevation > 0)                        elevation --;                    else                        elevation ++;                    for(Enumeration<Coords> e=candidate.elements();e.hasMoreElements();) {                        c = e.nextElement();                        IHex hex = board.getHex(c);                        hex.setElevation(elevation);                    }                }                candidate.removeAllElements();            }        }    }        /**      * Generates the elevations      * @param hilliness The Hilliness     * @param width The Width of the map.     * @param height The Height of the map.     * @param range Max difference betweenn highest and lowest level.     * @param invertProb Probability for the invertion of the map (0..100)     * @param invertNegate If 1, invert negative hexes, else do nothing     * @param elevationMap here is the result stored     */    public static void generateElevation(int hilliness, int width, int height,            int range, int invertProb, int invertNegative,            int elevationMap[][], int algorithm) {        int minLevel = 0;        int maxLevel = range;        boolean invert = (Compute.randomInt(100) < invertProb);                        /* init elevation map with 0 */        for (int w = 0; w < width; w++) {            for (int h = 0; h < height; h++) {                elevationMap[w][h] = 0;            }        }        /* generate landscape */        switch (algorithm) {        case 0:             cutSteps(hilliness, width, height, elevationMap);            break;        case 1:             midPoint(hilliness, width, height, elevationMap);            break;        case 2:            cutSteps(hilliness, width, height, elevationMap);            midPoint(hilliness, width, height, elevationMap);            break;        }                /* and now normalize it */        int min = elevationMap[0][0];        int max = elevationMap[0][0];        for (int w = 0; w < width; w++) {            for (int h = 0; h < height; h++) {                if (elevationMap[w][h] > max) {                    max = elevationMap[w][h];                }                if (elevationMap[w][h] < min) {                    min = elevationMap[w][h];                }            }        }                double scale = (double)(maxLevel - minLevel) / (double)(max - min);        int inc = (int)(-scale * min + minLevel);        int[] elevationCount = new int[maxLevel + 1];        for (int w = 0; w < width; w++) {            for (int h = 0; h < height; h++) {                elevationMap[w][h] *= scale;                elevationMap[w][h] += inc;                elevationCount[elevationMap[w][h]]++;            }        }        int mostElevation = 0;        for (int lvl = 1; lvl <= range; lvl++) {            if (elevationCount[lvl] > elevationCount[mostElevation]) {                mostElevation = lvl;            }        }        for (int w=0; w<width; w++)   {            for (int h=0; h<height; h++) {                elevationMap[w][h]-=mostElevation;                if (invert) {                    elevationMap[w][h] *= -1;                }            }        }        // invert negative terrain?        if (invertNegative == 1) {            for (int w=0; w<width; w++)   {                for (int h=0; h<height; h++) {                    if (elevationMap[w][h] < 0) {                        elevationMap[w][h] *= -1;                    }                }            }        }    }        public static void generateMountain(IBoard board, int width,            Coords centre, int height, int capStyle) {        final int mapW = board.getWidth();        final int mapH = board.getHeight();                ITerrainFactory tf = Terrains.getTerrainFactory();                for(int x=0;x<mapW;x++) {            for(int y=0;y<mapH;y++) {                Coords c = new Coords(x,y);                int distance = c.distance(centre);                int elev = (100 * height * (width - distance))/width;                elev = (elev / 100) + (Compute.randomInt(100) < (elev % 100) ? 1 : 0);                                IHex hex = board.getHex(c);                                if(elev >= height - 2) {                    switch(capStyle) {                    case MapSettings.MOUNTAIN_SNOWCAPPED:                        hex.setTheme("snow");                        break;                    case MapSettings.MOUNTAIN_VOLCANO_ACTIVE:                    case MapSettings.MOUNTAIN_VOLCANO_DORMANT:                        hex.setTheme("lunar");                        break;                    case MapSettings.MOUNTAIN_LAKE:                        int lake = (width/4);                        int depth = ((lake - distance) +1);                        if(depth < 1) {  //eliminates depth 0 water                        	depth = 1;                        }                        hex.addTerrain(tf.createTerrain(Terrains.WATER,(depth)));                        elev -= (Math.abs(lake - elev) -1);                        break;                        }                }                if(elev == height) {                    //for volcanoes, invert the peak                    switch(capStyle) {                    case MapSettings.MOUNTAIN_VOLCANO_ACTIVE:                        hex.removeAllTerrains();                        hex.addTerrain(tf.createTerrain(Terrains.MAGMA,2));                        elev -= 2;                        break;                    case MapSettings.MOUNTAIN_VOLCANO_DORMANT:                        hex.removeAllTerrains();                        hex.addTerrain(tf.createTerrain(Terrains.MAGMA,1));                        elev -= 2;                        break;                    case MapSettings.MOUNTAIN_VOLCANO_EXTINCT:                        hex.setTheme("lunar");                        elev -= 2;                        break;                    }                }                                if(hex.getElevation() < elev)                    hex.setElevation(elev);            }        }                    }    /**     * Flips the board around the vertical axis (North-for-South) and/or     * the horizontal axis (East-for-West).  The dimensions of the board     * will remain the same, but the terrain of the hexes will be swiched.     *     * @param   horiz - a <code>boolean</code> value that, if <code>true</code>,     *          indicates that the board is being flipped North-for-South.     * @param   vert - a <code>boolean</code> value that, if <code>true</code>,     *          indicates that the board is being flipped East-for-West.     */    public static void flip(IBoard board, boolean horiz, boolean vert ) {        // If we're not flipping around *some* axis, do nothing.        if ( !vert && !horiz ) {            return;        }        // We only walk through half the board, but *which* half?        int stopX;        int stopY;        int width = board.getWidth();        int height = board.getHeight();                if ( horiz ) {            // West half of board.            stopX = width/2;            stopY = height;        } else {            // North half of board.            stopX = width;            stopY = height/2;        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -