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

📄 boardutilities.java

📁 MegaMek is a networked Java clone of BattleTech, a turn-based sci-fi boardgame for 2+ players. Fight
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* * MegaMek - Copyright (C) 2005 Ben Mazur (bmazur@sev.org) * *  This program is free software; you can redistribute it and/or modify it *  under the terms of the GNU General Public License as published by the Free *  Software Foundation; either version 2 of the License, or (at your option) *  any later version. * *  This program is distributed in the hope that it will be useful, but *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *  for more details. */package megamek.common.util;import java.util.Vector;import java.util.Enumeration;import megamek.common.Board;import megamek.common.Compute;import megamek.common.Coords;import megamek.common.Hex;import megamek.common.IBoard;import megamek.common.IHex;import megamek.common.ITerrain;import megamek.common.ITerrainFactory;import megamek.common.MapSettings;import megamek.common.Terrains;import megamek.common.util.BuildingTemplate;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;public class BoardUtilities {    /**     * Combines one or more boards into one huge megaboard!     *     * @param width the width of each individual board, before the combine     * @param height the height of each individual board, before the combine     * @param sheetWidth how many sheets wide the combined map is     * @param sheetHeight how many sheets tall the combined map is     * @param boards an array of the boards to be combined     */    public static IBoard combine(int width, int height, int sheetWidth, int sheetHeight, IBoard[] boards) {        int resultWidth = width * sheetWidth;        int resultHeight = height * sheetHeight;                IHex[] resultData = new IHex[resultWidth * resultHeight];        boolean roadsAutoExit = true;                // Copy the data from the sub-boards.        for (int i = 0; i < sheetHeight; i++) {            for (int j = 0; j < sheetWidth; j++) {                IBoard b = boards[i*sheetWidth+j];                if(b.getWidth() != width || b.getHeight() != height) {                    throw new IllegalArgumentException("board is the wrong size, expected "+width+"x"+height+", got "+b.getWidth()+"x"+b.getHeight());                }                copyBoardInto(resultData, resultWidth, j * width, i * height, boards[i * sheetWidth + j]);                // Copy in the other board's options.                if ( boards[i * sheetWidth + j].getRoadsAutoExit() == false ) {                    roadsAutoExit = false;                }            }        }        IBoard result = new Board();        result.setRoadsAutoExit(roadsAutoExit);        //Initialize all hexes - buildings, exits, etc        result.newData(resultWidth, resultHeight, resultData);        return result;    }        /**     * Copies the data of another board into given array of Hexes, offset by the specified     * x and y.     *     */    protected static void copyBoardInto(IHex[] dest, int destWidth, int x, int y, IBoard copied) {        for (int i = 0; i < copied.getHeight(); i++) {            for (int j = 0; j < copied.getWidth(); j++) {                dest[(i+y) * destWidth + j+x] = copied.getHex(j,i);            }        }    }        /**     Generates a Random Board     @param width The width of the generated Board.     @param height The height of the gernerated Board.     @param steps how often the iterative method should be repeated     */    public static IBoard generateRandom(MapSettings mapSettings) {        int elevationMap[][] = new int[mapSettings.getBoardWidth()][mapSettings.getBoardHeight()];        double sizeScale = (double)(mapSettings.getBoardWidth() * mapSettings.getBoardHeight()) / ((double)(16 * 17));                generateElevation(mapSettings.getHilliness(),                mapSettings.getBoardWidth(),                 mapSettings.getBoardHeight(),                mapSettings.getRange() + 1,                 mapSettings.getProbInvert(),                mapSettings.getInvertNegativeTerrain(),                elevationMap,                mapSettings.getAlgorithmToUse());                IHex[] nb = new IHex[mapSettings.getBoardWidth() * mapSettings.getBoardHeight()];        int index = 0;        for (int h = 0; h < mapSettings.getBoardHeight(); h++) {            for (int w = 0; w < mapSettings.getBoardWidth(); w++) {                nb[index++] = new Hex(elevationMap[w][h],"",mapSettings.getTheme());            }        }                IBoard result = new Board(mapSettings.getBoardWidth(),mapSettings.getBoardHeight(), nb);        /* initalize reverseHex */        HashMap reverseHex = new HashMap(2 * mapSettings.getBoardWidth() * mapSettings.getBoardHeight());        for (int y = 0; y < mapSettings.getBoardHeight(); y++) {            for (int x = 0; x < mapSettings.getBoardWidth(); x++) {                reverseHex.put(result.getHex(x, y),new Point(x, y));            }        }                int peaks = mapSettings.getMountainPeaks();        while(peaks > 0) {            peaks--;            int mountainHeight = mapSettings.getMountainHeightMin() +             Compute.randomInt(1 + mapSettings.getMountainHeightMax() -                     mapSettings.getMountainHeightMin());            int mountainWidth = mapSettings.getMountainWidthMin() +             Compute.randomInt(1 + mapSettings.getMountainWidthMax() -                     mapSettings.getMountainWidthMin());            int mapWidth = result.getWidth();            int mapHeight = result.getHeight();                        //put the peak somewhere in the middle of the map...            Coords peak = new Coords(mapWidth/4 + Compute.randomInt((mapWidth+1)/2),                    mapHeight/4 + Compute.randomInt((mapHeight+1)/2));                        generateMountain(result, mountainWidth, peak, mountainHeight, mapSettings.getMountainStyle());                    }                if(mapSettings.getCliffs() > 0) {            addCliffs(result, mapSettings.getCliffs());        }                /* Add the woods */        int count = mapSettings.getMinForestSpots();        if (mapSettings.getMaxForestSpots() > 0) {            count += Compute.randomInt(mapSettings.getMaxForestSpots());        }        count *= sizeScale;        for (int i = 0; i < count; i++) {            placeSomeTerrain(result, Terrains.WOODS, mapSettings.getProbHeavy() ,                    mapSettings.getMinForestSize(),                     mapSettings.getMaxForestSize(),                    reverseHex,                    true);        }        /* Add the rough */        count = mapSettings.getMinRoughSpots();        if (mapSettings.getMaxRoughSpots() > 0) {            count += Compute.randomInt(mapSettings.getMaxRoughSpots());        }        count *= sizeScale;        for (int i = 0; i < count; i++) {            placeSomeTerrain(result, Terrains.ROUGH, 0,                    mapSettings.getMinRoughSize(),                     mapSettings.getMaxRoughSize(),                    reverseHex,                    true);        }        /* Add the swamp */        count = mapSettings.getMinSwampSpots();        if (mapSettings.getMaxSwampSpots() > 0) {            count += Compute.randomInt(mapSettings.getMaxSwampSpots());        }        count *= sizeScale;        for (int i = 0; i < count; i++) {            placeSomeTerrain(result, Terrains.SWAMP, 0,                    mapSettings.getMinSwampSize(),                     mapSettings.getMaxSwampSize(),                    reverseHex,                    false); // can stack with woods or roughs        }        // Add the Fortified hexes        count = mapSettings.getMinFortifiedSpots();        if (mapSettings.getMaxFortifiedSpots() > 0) {            count += Compute.randomInt(mapSettings.getMaxFortifiedSpots());        }        count *= sizeScale;        for (int i = 0; i < count; i++) {            placeSomeTerrain(result, Terrains.FORTIFIED, 0,                    mapSettings.getMinFortifiedSize(),                     mapSettings.getMaxFortifiedSize(),                    reverseHex,                    false);         }        // Add the rubble        count = mapSettings.getMinRubbleSpots();        if (mapSettings.getMaxRubbleSpots() > 0) {            count += Compute.randomInt(mapSettings.getMaxRubbleSpots());        }        count *= sizeScale;        for (int i = 0; i < count; i++) {            placeSomeTerrain(result, Terrains.RUBBLE, 0,                    mapSettings.getMinRubbleSize(),                     mapSettings.getMaxRubbleSize(),                    reverseHex,                    true);        }        /* Add the water */        count = mapSettings.getMinWaterSpots();        if (mapSettings.getMaxWaterSpots() > 0) {             count += Compute.randomInt(mapSettings.getMaxWaterSpots());        }        count *= sizeScale;        for (int i = 0; i < count; i++) {            placeSomeTerrain(result, Terrains.WATER, mapSettings.getProbDeep() ,                    mapSettings.getMinWaterSize(),                     mapSettings.getMaxWaterSize(),                    reverseHex,                    true);        }        /* Add the pavements */        count = mapSettings.getMinPavementSpots();        if (mapSettings.getMaxPavementSpots() > 0) {            count += Compute.randomInt(mapSettings.getMaxPavementSpots());        }        count *= sizeScale;        for (int i = 0; i < count; i++) {            placeSomeTerrain(result, Terrains.PAVEMENT, 0,                    mapSettings.getMinPavementSize(),                     mapSettings.getMaxPavementSize(),                    reverseHex,                    true);         }                /* Add the ice */        count = mapSettings.getMinIceSpots();        if (mapSettings.getMaxIceSpots() > 0) {            count += Compute.randomInt(mapSettings.getMaxIceSpots());        }        count *= sizeScale;        for (int i = 0; i < count; i++) {            placeSomeTerrain(result, Terrains.ICE, 0,                    mapSettings.getMinIceSize(),                     mapSettings.getMaxIceSize(),                    reverseHex,                    true);         }                /* Add the craters */        if (Compute.randomInt(100) < mapSettings.getProbCrater()) {            addCraters(result, mapSettings.getMinRadius(), mapSettings.getMaxRadius(),                    (int)(mapSettings.getMinCraters()*sizeScale),                    (int)(mapSettings.getMaxCraters()*sizeScale));        }                /* Add the river */        if (Compute.randomInt(100)<mapSettings.getProbRiver()) {            addRiver(result, reverseHex);        }                /* Add special effects */        if(Compute.randomInt(100)<mapSettings.getProbFlood()) {            PostProcessFlood(nb, mapSettings.getFxMod());        }        if(Compute.randomInt(100)<mapSettings.getProbDrought()) {            PostProcessDrought(nb, mapSettings.getFxMod());        }        if(Compute.randomInt(100)<mapSettings.getProbFreeze()) {            PostProcessDeepFreeze(nb, mapSettings.getFxMod());        }        if(Compute.randomInt(100)<mapSettings.getProbForestFire()) {            PostProcessForestFire(nb, mapSettings.getFxMod());        }                /* Add the road */        boolean roadNeeded = false;        if (Compute.randomInt(100)<mapSettings.getProbRoad()) {            roadNeeded = true;        }        // add buildings         Vector buildings = mapSettings.getBoardBuildings();        CityBuilder cityBuilder = new CityBuilder(mapSettings, result);        if(buildings.size() == 0) {            buildings = cityBuilder.generateCity(roadNeeded);        }        for(int i=0; i<buildings.size();i++) {            placeBuilding(result, (BuildingTemplate)(buildings.elementAt(i)));        }        return result;    }    private static void placeBuilding(IBoard board, BuildingTemplate building) {        int type = building.getType();        int cf = building.getCF();        int height = building.getHeight();        ITerrainFactory tf = Terrains.getTerrainFactory();        Vector hexes = new Vector();        int level=0;        for(Enumeration i=building.getCoords();i.hasMoreElements();) {            Coords c = (Coords)i.nextElement();            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.addElement(hex);            level += hex.getElevation();        }        //set everything to the same level        for(int j=0;j<hexes.size();j++) {            ((IHex)(hexes.elementAt(j))).setElevation(level / hexes.size());        }    }    /**     * Places randomly some connected Woods.     * @param probHeavy The probability that a wood is a heavy wood (in %).     * @param maxWoods Maximum Number of Woods placed.     */    protected static void placeSomeTerrain(IBoard board, int terrainType, int probMore,int minHexes, int maxHexes, HashMap reverseHex, boolean exclusive) {        Point p = new Point(Compute.randomInt(board.getWidth()),Compute.randomInt(board.getHeight()));

⌨️ 快捷键说明

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