Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.jtjj222.BasicTerrain;
- import java.util.Random;
- import org.bukkit.Material;
- import org.bukkit.World;
- import org.bukkit.generator.*;
- import org.bukkit.util.noise.SimplexOctaveGenerator;
- public class BasicChunkGenerator extends ChunkGenerator {
- @Override
- /**
- * @param world
- * The world the chunk belongs to
- * @param rand
- * Don't use this, make a new random object using the world seed (world.getSeed())
- * @param biome
- * Use this to set/get the current biome
- * @param ChunkX and ChunkZ
- * The x and z co-ordinates of the current chunk.
- */
- public byte[][] generateBlockSections(World world, Random rand, int chunkX,
- int chunkZ, BiomeGrid biome) {
- //where we will store our blocks
- byte[][] chunk = new byte[world.getMaxHeight() / 16][];
- SimplexOctaveGenerator randomGenerator = new SimplexOctaveGenerator(world,8);
- randomGenerator.setScale(1/256.0);
- double magnitude = 1/64.0;
- int topLeft = (int) (randomGenerator.noise(chunkX, chunkZ, 0.5, 0.5) * magnitude);
- int topRight = (int) (randomGenerator.noise(chunkX, chunkZ + 16, 0.5, 0.5) * magnitude);
- int bottomLeft = (int) (randomGenerator.noise(chunkX + 16, chunkZ, 0.5, 0.5) * magnitude);
- int bottomRight = (int) (randomGenerator.noise(chunkX + 16, chunkZ + 16, 0.5, 0.5) * magnitude);
- double[][] values = new double[16][16];
- values[0][0] = topLeft;
- values[0][15] = topRight;
- values[15][0] = bottomLeft;
- values[15][15] = bottomRight;
- values = fillNoiseArray(16,values);
- for (int x = 0; x < 16; x++) {
- for (int z = 0; z < 16; z++) {
- for (int y = 0; y < values[x][z] + 64; y++) {
- ChunkArrayManipulator.setBlock(x, y, z, chunk, Material.STONE);
- }
- }
- }
- return chunk;
- }
- public double[][] fillNoiseArray(int finalNoiseSize, double[][] finalNoiseArray) throws ArrayIndexOutOfBoundsException {
- //Seed all the values
- double diamondError = 1.6;
- double squareError = 1.2;
- double topLeft = finalNoiseArray[0][0];
- double topRight = finalNoiseArray[0][finalNoiseSize - 1];
- double bottomLeft = finalNoiseArray[finalNoiseSize - 1][0];
- double bottomRight = finalNoiseArray[finalNoiseSize - 1][finalNoiseSize - 1];
- double[][] thisLevelNoiseValues =
- getCornerNosieValues(topLeft, topRight, bottomLeft,
- bottomRight, diamondError, squareError);
- //if it's odd, there isn't much work to be done
- if (finalNoiseSize%2 == 1) { //odd
- int middle = finalNoiseSize/2;
- finalNoiseArray[0][middle] = thisLevelNoiseValues[0][1]; //top middle
- finalNoiseArray[finalNoiseSize - 1][middle] = thisLevelNoiseValues[2][1]; //bottom middle
- finalNoiseArray[middle][0] = thisLevelNoiseValues[1][0]; //left middle
- finalNoiseArray[middle][finalNoiseSize - 1] = thisLevelNoiseValues[1][2]; //right middle
- finalNoiseArray[middle][middle] = thisLevelNoiseValues[1][1]; //center
- }
- else { //even
- double middle = finalNoiseSize/2; //it's gonna be something + .5
- int topMiddle = (int) (middle - 0.5);
- int bottomMiddle = (int) (middle + 0.5);
- int leftMiddle = (int) (middle - 0.5);
- int rightMiddle = (int) (middle - 0.5);
- finalNoiseArray[0][leftMiddle] = thisLevelNoiseValues[0][1]; //top middle
- finalNoiseArray[0][rightMiddle] = thisLevelNoiseValues[0][1]; //top middle
- finalNoiseArray[finalNoiseSize - 1][leftMiddle] = thisLevelNoiseValues[2][1]; //bottom middle
- finalNoiseArray[finalNoiseSize - 1][rightMiddle] = thisLevelNoiseValues[2][1]; //bottom middle
- finalNoiseArray[topMiddle][0] = thisLevelNoiseValues[1][0]; //left middle
- finalNoiseArray[bottomMiddle][0] = thisLevelNoiseValues[1][0]; //left middle
- finalNoiseArray[topMiddle][finalNoiseSize - 1] = thisLevelNoiseValues[1][2]; //right middle
- finalNoiseArray[bottomMiddle][finalNoiseSize - 1] = thisLevelNoiseValues[1][2]; //right middle
- finalNoiseArray[leftMiddle][topMiddle] = thisLevelNoiseValues[1][1]; //center
- finalNoiseArray[rightMiddle][topMiddle] = thisLevelNoiseValues[1][1]; //center
- finalNoiseArray[leftMiddle][bottomMiddle] = thisLevelNoiseValues[1][1]; //center
- finalNoiseArray[rightMiddle][bottomMiddle] = thisLevelNoiseValues[1][1]; //center
- }
- //now that we have seeded the values, we can find all the smaller detail levels
- if (finalNoiseSize <= 3) {
- return finalNoiseArray; //lowest possible detail level
- }
- else //needs to be split up
- {
- int newSize = getSubdivisionSize(finalNoiseSize);
- double[][] topLeftArray = fillNoiseArray(newSize,getSubdivision(finalNoiseSize,0,0, finalNoiseArray));
- double[][] topRightArray = fillNoiseArray(newSize,getSubdivision(finalNoiseSize,0,newSize, finalNoiseArray));
- double[][] bottomLeftArray = fillNoiseArray(newSize,getSubdivision(finalNoiseSize,newSize,0, finalNoiseArray));
- double[][] bottomRightArray = fillNoiseArray(newSize,getSubdivision(finalNoiseSize,newSize,newSize, finalNoiseArray));
- return addNoiseArrays(topLeftArray,topRightArray,bottomLeftArray,bottomRightArray, newSize, finalNoiseSize);
- }
- }
- public double[][] addNoiseArrays(double[][] topLeftArray,
- double[][] topRightArray, double[][] bottomLeftArray,
- double[][] bottomRightArray, int oldSize, int finalSize) throws ArrayIndexOutOfBoundsException {
- double[][] newArray = new double[finalSize][finalSize];
- //do the top left
- for (int i = 0; i < oldSize; i++) {
- for (int j = 0; j < oldSize; j++) {
- newArray[i][j] = topLeftArray[i][j];
- }
- }
- //then the top right
- for (int i = 0; i < oldSize; i++) {
- for (int j = 0; j < oldSize; j++) {
- newArray[i][j + oldSize] = topRightArray[i][j];
- }
- }
- //then the bottom left
- for (int i = 0; i < oldSize; i++) {
- for (int j = 0; j < oldSize; j++) {
- newArray[i + oldSize][j] = bottomRightArray[i][j];
- }
- }
- //then the bottom right
- for (int i = 0; i < oldSize; i++) {
- for (int j = 0; j < oldSize; j++) {
- newArray[i + oldSize][j + oldSize] = bottomRightArray[i][j];
- }
- }
- return newArray;
- }
- public double[][] getSubdivision(int noiseSize, int startX, int startZ, double[][] noiseArray) {
- int newSize = getSubdivisionSize(noiseSize);
- double[][] subdivision = new double[newSize][newSize];
- for (int i = startX; i < newSize; i++) {
- for (int j = startZ; j < newSize; j++) {
- subdivision[i][j] = noiseArray[i][j];
- }
- }
- return subdivision;
- }
- public int getSubdivisionSize(int noiseSize) {
- int newSize = 0;
- if (noiseSize%2 == 0) newSize = noiseSize/2;
- else newSize = (int)(noiseSize / 2 + 0.5);
- return newSize;
- }
- /**
- * @author jtjj222
- * @param topLeft
- * The top left seeded value for the square
- * @param topRight
- * The top right seeded value for the square
- * @param bottumLeft
- * The bottom left seeded value for the square
- * @param bottumRight
- * The bottom right seeded value for the square
- * @param diamondError
- * The error applied to the center of the square.
- * @param squareError
- * The error applied to the sides of the square. If this value is high on the biggest
- * "square", there will be more volatile terrain.
- * @return an array with the results from the diamond square
- * algorithm, at the following array locations:
- * (0,0)(0,1)(0,2)
- * (1,0)(1,1)(1,2)
- * (2,0)(2,1)(2,2)
- */
- public double[][] getCornerNosieValues(double topLeft, double topRight, double bottumLeft, double bottumRight, double diamondError, double squareError) {
- double[][] noiseArray = new double[3][3];
- noiseArray[0][0] = topLeft;
- noiseArray[0][2] = topRight;
- double topMiddle = mean(topLeft,topRight) + squareError;
- noiseArray[0][1] = topMiddle;
- noiseArray[2][0] = bottumLeft;
- noiseArray[2][2] = bottumRight;
- double bottumMiddle = mean(bottumLeft,bottumRight) + squareError;
- noiseArray[2][1] = bottumMiddle;
- double centre = mean(topLeft,topRight,bottumLeft,bottumRight) + diamondError;
- noiseArray[1][1] = centre;
- double leftMiddle = mean(topLeft,bottumLeft) + squareError;
- noiseArray[1][0] = leftMiddle;
- double rightMiddle = mean(topRight,bottumRight) + squareError;
- noiseArray[1][2] = rightMiddle;
- return noiseArray;
- }
- public double mean(double a, double b, double c, double d) {
- return (a+b+c+d)/4;
- }
- public double mean(double a, double b) {
- return (a+b)/2;
- }
- public double mean(double a, double b, double c, double d, double e, double f, double g, double h, double i) {
- return (a+b+c+d+e+f+g+h+i)/9;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement