📄 boardutilities.java
字号:
// Walk through the current data array and build a new one. int newX; int newY; IHex tempHex; ITerrain terr; for ( int oldX = 0; oldX < stopX; oldX++ ) { // Calculate the new X position of the flipped hex. if (horiz) { newX = width - oldX - 1; } else { newX = oldX; } for ( int oldY = 0; oldY < stopY; oldY++ ) { // Calculate the new Y position of the flipped hex. if (vert) { newY = height - oldY - 1; } else { newY = oldY; } // Swap the old hex for the new hex. tempHex = board.getHex(oldX, oldY); board.setHex(oldX, oldY, board.getHex(newX, newY)); board.setHex(newX, newY,tempHex); IHex newHex = board.getHex(newX, newY); IHex oldHex = board.getHex(oldX, oldY); // Update the road exits in the swapped hexes. terr = newHex.getTerrain(Terrains.ROAD); if ( null != terr ) { terr.flipExits(horiz, vert); } terr = oldHex.getTerrain(Terrains.ROAD); if ( null != terr ) { terr.flipExits(horiz, vert); } // Update the building exits in the swapped hexes. terr = newHex.getTerrain( Terrains.BUILDING ); if ( null != terr ) { terr.flipExits( horiz, vert ); } terr = oldHex.getTerrain(Terrains.BUILDING); if ( null != terr ) { terr.flipExits( horiz, vert ); } // Update the fuel tank exits in the swapped hexes. terr = newHex.getTerrain(Terrains.FUEL_TANK); if (null != terr) { terr.flipExits(horiz, vert); } terr = oldHex.getTerrain(Terrains.FUEL_TANK); if (null != terr) { terr.flipExits(horiz, vert); } // Update the bridge exits in the swapped hexes. terr = newHex.getTerrain( Terrains.BRIDGE ); if ( null != terr ) { terr.flipExits( horiz, vert ); } terr = oldHex.getTerrain( Terrains.BRIDGE ); if ( null != terr ) { terr.flipExits( horiz, vert ); } } } } /** * one of the landscape generation algorithms */ protected static void cutSteps(int hilliness, int width, int height, int elevationMap[][]) { Point p1, p2; int sideA, sideB; int type; p1 = new Point(0,0); p2 = new Point(0,0); for (int step = 0; step < hilliness * 20; step++) { /* select which side should be decremented, and which increemented */ sideA = (Compute.randomInt(2) == 0)? -1 : 1; sideB =- sideA; type = Compute.randomInt(6); /* 6 different lines in rectangular area from border to border possible */ switch (type) { case 0: /* left to upper border */ p1.setLocation(0, Compute.randomInt(height)); p2.setLocation(Compute.randomInt(width), height-1); markSides(p1, p2, sideB, sideA, elevationMap, height); markRect(p2.x, width, sideA, elevationMap, height); break; case 1: /* upper to lower border */ p1.setLocation(Compute.randomInt(width), 0); p2.setLocation(Compute.randomInt(width), height-1); if (p1.x < p2.x) { markSides(p1, p2, sideA, sideB, elevationMap, height); } else { markSides(p2, p1, sideB, sideA, elevationMap, height); } markRect(0, p1.x, sideA, elevationMap, height); markRect(p2.x, width, sideB, elevationMap, height); break; case 2: /* upper to right border */ p1.setLocation(Compute.randomInt(width), height-1); p2.setLocation(width, Compute.randomInt(height)); markSides(p1, p2, sideB, sideA, elevationMap, height); markRect(0, p1.x, sideA, elevationMap, height); break; case 3: /* left to right border */ p1.setLocation(0, Compute.randomInt(height)); p2.setLocation(width, Compute.randomInt(height)); markSides(p1, p2, sideA, sideB, elevationMap, height); break; case 4: /* left to lower border */ p1.setLocation(0, Compute.randomInt(height)); p2.setLocation(Compute.randomInt(width), 0); markSides(p1, p2, sideB, sideA, elevationMap, height); markRect(p2.x, width, sideB, elevationMap, height); break; case 5: /* lower to right border */ p1.setLocation(Compute.randomInt(width), 0); p2.setLocation(width, Compute.randomInt(height)); markSides(p1, p2, sideB, sideA, elevationMap, height); markRect(0, p1.x, sideB, elevationMap, height); break; } } } /** * Helper function for the map generator * increased a heightmap my a given value */ protected static void markRect(int x1, int x2, int inc, int elevationMap [][], int height) { for (int x = x1; x < x2; x++) { for (int y = 0; y < height; y++) { elevationMap[x][y] += inc; } } } /** * Helper function for map generator * inreases all of one side and decreased on other side */ protected static void markSides(Point p1, Point p2, int upperInc, int lowerInc, int elevationMap [][], int height) { for (int x = p1.x; x < p2.x; x++) { for (int y = 0; y < height; y++) { int point =(p2.y - p1.y) / (p2.x - p1.x) * (x - p1.x) + p1.y; if (y > point) { elevationMap[x][y] += upperInc; } else if (y < point) { elevationMap[x][y] += lowerInc; } } } } /** * midpoint algorithm for landscape generartion */ protected static void midPoint(int hilliness, int width, int height, int elevationMap[][]) { int size; int steps = 1; int tmpElevation[][]; size = (width > height) ? width : height; while (size > 0) { steps++; size /= 2; } size = (1 << steps) + 1; tmpElevation = new int[size + 1][size + 1]; /* init elevation map with 0 */ for (int w = 0; w < size; w++) for (int h = 0; h < size; h++) if ((w < width) && (h < height)) { tmpElevation[w][h] = elevationMap[w][h]; } else { tmpElevation[w][h] = 0; } for (int i = steps; i > 0; i--) { midPointStep((double)hilliness/100, size, 100, tmpElevation, i, true); } for (int w = 0; w < width; w++) { for (int h = 0; h < height; h++) { elevationMap[w][h] = tmpElevation[w][h]; } } } /** * Helper function for landscape generation */ protected static void midPointStep(double fracdim, int size, int delta, int elevationMap[][], int step, boolean newBorder) { int d1, d2; int delta5; int x,y; d1 = size >> (step - 1); d2 = d1 / 2; fracdim = (1.0 - fracdim) / 2.0; delta = (int)(delta * Math.exp(-0.6931 * fracdim * (2.0 * step - 1))); delta5 = delta << 5; x = d2; do { y = d2; do { elevationMap[x][y] = middleValue(elevationMap[x + d2][y + d2], elevationMap[x + d2][y - d2], elevationMap[x - d2][y + d2], elevationMap[x - d2][y - d2], delta5); y += d1; } while (y < size - d2); x += d1; } while (x < size - d2); delta = (int)(delta * Math.exp(-0.6931 * fracdim )); delta5 = delta << 5; if (newBorder) { x = d2; do { y = x; elevationMap[0][x] = middleValue(elevationMap[0][x + d2], elevationMap[0][x - d2], elevationMap[d2][x], delta5); elevationMap[size][x] = middleValue(elevationMap[size - 1][x + d2], elevationMap[size - 1][x - d2], elevationMap[size - d2 - 1][x], delta5); y = 0; elevationMap[x][0] = middleValue(elevationMap[x + d2][0], elevationMap[x - d2][0], elevationMap[x][d2], delta5); elevationMap[x][size] = middleValue(elevationMap[x + d2][size - 1], elevationMap[x - d2][size - 1], elevationMap[x][size - d2 - 1], delta5); x += d1; } while (x < size - d2); } diagMid(new Point(d2, d1), d1, d2, delta5, size, elevationMap); diagMid(new Point(d1, d2), d1, d2, delta5, size, elevationMap); } /** * calculates the diagonal middlepoints with new values * @param p Starting point. */ protected static void diagMid(Point p, int d1, int d2, int delta, int size, int elevationMap[][]) { int x = p.x; int y; int hx = x + d2; int hy; while ((x < size - d2) && (hx < size)) { y = p.y; hy = y + d2; while ( (y < size-d2) && (hy < size)) { elevationMap[x][y] = middleValue(elevationMap[x][hy], elevationMap[x][y - d2], elevationMap[hx][y], elevationMap[x - d2][y], delta); y += d1; hy = y + d2; } x += d1; hx = x + d2; } } /** * calculates the arithmetic medium of 3 values and add random * value in range of delta. */ protected static int middleValue(int a, int b, int c, int delta) { int result=(((a + b + c) / 3) + normRNG(delta)); return result; } /** * calculates the arithmetic medium of 4 values and add random * value in range of delta. */ protected static int middleValue(int a, int b, int c, int d, int delta) { int result = (((a + b + c + d) / 4) + normRNG(delta)); return result; } /** * Gives a normal distributed Randomvalue, with mediumvalue from * 0 and a Varianz of factor. * @param factor varianz of of the distribution. * @return Random number, most times in the range -factor .. +factor, * at most in the range of -3*factor .. +3*factor. */ private static int normRNG(int factor) { factor++; return (2 * (Compute.randomInt(factor) + Compute.randomInt(factor) + Compute.randomInt(factor)) - 3 * (factor - 1)) / 32; } protected static class Point { public int x; public int y; public Point(int x, int y) { this.x = x; this.y = y; } public Point(Point other) { this.x = other.x; this.y = other.y; } /** * Set the location * @param x x coordinate * @param y y coordinate */ public void setLocation(int x, int y) { this.x = x; this.y = y; } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -