Advertisement
jtjj222

Diamond square test

Sep 12th, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.47 KB | None | 0 0
  1. package me.jtjj222.BasicTerrain;
  2.  
  3. import java.util.Random;
  4.  
  5. import org.bukkit.Material;
  6. import org.bukkit.World;
  7. import org.bukkit.generator.*;
  8. import org.bukkit.util.noise.SimplexOctaveGenerator;
  9.  
  10. public class BasicChunkGenerator extends ChunkGenerator {
  11.  
  12.     @Override
  13.     /**
  14.      * @param world
  15.      * The world the chunk belongs to
  16.      * @param rand
  17.      * Don't use this, make a new random object using the world seed (world.getSeed())
  18.      * @param biome
  19.      * Use this to set/get the current biome
  20.      * @param ChunkX and ChunkZ
  21.      * The x and z co-ordinates of the current chunk.
  22.      */
  23.     public byte[][] generateBlockSections(World world, Random rand, int chunkX,
  24.             int chunkZ, BiomeGrid biome) {
  25.         //where we will store our blocks
  26.         byte[][] chunk = new byte[world.getMaxHeight() / 16][];
  27.  
  28.        
  29.         SimplexOctaveGenerator randomGenerator = new SimplexOctaveGenerator(world,8);
  30.         randomGenerator.setScale(1/256.0);
  31.         double magnitude = 1/64.0;
  32.        
  33.         int topLeft = (int) (randomGenerator.noise(chunkX, chunkZ, 0.5, 0.5) * magnitude);
  34.         int topRight = (int) (randomGenerator.noise(chunkX, chunkZ + 16, 0.5, 0.5) * magnitude);
  35.         int bottomLeft = (int) (randomGenerator.noise(chunkX + 16, chunkZ, 0.5, 0.5) * magnitude);
  36.         int bottomRight = (int) (randomGenerator.noise(chunkX + 16, chunkZ + 16, 0.5, 0.5) * magnitude);
  37.        
  38.         double[][] values = new double[16][16];
  39.         values[0][0] = topLeft;
  40.         values[0][15] = topRight;
  41.         values[15][0] = bottomLeft;
  42.         values[15][15] = bottomRight;
  43.         values = fillNoiseArray(16,values);
  44.         for (int x = 0; x < 16; x++) {
  45.             for (int z = 0; z < 16; z++) {
  46.                 for (int y = 0; y < values[x][z] + 64; y++) {
  47.                     ChunkArrayManipulator.setBlock(x, y, z, chunk, Material.STONE);
  48.                 }
  49.             }
  50.         }
  51.        
  52.         return chunk;
  53.     }
  54.    
  55.     public double[][] fillNoiseArray(int finalNoiseSize, double[][] finalNoiseArray) throws ArrayIndexOutOfBoundsException {
  56.        
  57.         //Seed all the values
  58.         double diamondError = 1.6;
  59.         double squareError = 1.2;
  60.        
  61.         double topLeft = finalNoiseArray[0][0];
  62.         double topRight = finalNoiseArray[0][finalNoiseSize - 1];
  63.         double bottomLeft = finalNoiseArray[finalNoiseSize - 1][0];
  64.         double bottomRight = finalNoiseArray[finalNoiseSize - 1][finalNoiseSize - 1];
  65.        
  66.         double[][] thisLevelNoiseValues =
  67.                 getCornerNosieValues(topLeft, topRight, bottomLeft,
  68.                 bottomRight, diamondError, squareError);
  69.        
  70.         //if it's odd, there isn't much work to be done
  71.         if (finalNoiseSize%2 == 1) { //odd
  72.             int middle = finalNoiseSize/2;
  73.            
  74.             finalNoiseArray[0][middle] = thisLevelNoiseValues[0][1]; //top middle
  75.             finalNoiseArray[finalNoiseSize - 1][middle] = thisLevelNoiseValues[2][1]; //bottom middle
  76.             finalNoiseArray[middle][0] = thisLevelNoiseValues[1][0]; //left middle
  77.             finalNoiseArray[middle][finalNoiseSize - 1] = thisLevelNoiseValues[1][2]; //right middle
  78.             finalNoiseArray[middle][middle] = thisLevelNoiseValues[1][1]; //center
  79.         }
  80.         else { //even
  81.            
  82.             double middle = finalNoiseSize/2; //it's gonna be something + .5
  83.            
  84.             int topMiddle = (int) (middle - 0.5);
  85.             int bottomMiddle = (int) (middle + 0.5);
  86.             int leftMiddle = (int) (middle - 0.5);
  87.             int rightMiddle = (int) (middle - 0.5);
  88.            
  89.             finalNoiseArray[0][leftMiddle] = thisLevelNoiseValues[0][1]; //top middle
  90.             finalNoiseArray[0][rightMiddle] = thisLevelNoiseValues[0][1]; //top middle
  91.            
  92.             finalNoiseArray[finalNoiseSize - 1][leftMiddle] = thisLevelNoiseValues[2][1]; //bottom middle
  93.             finalNoiseArray[finalNoiseSize - 1][rightMiddle] = thisLevelNoiseValues[2][1]; //bottom middle
  94.            
  95.             finalNoiseArray[topMiddle][0] = thisLevelNoiseValues[1][0]; //left middle
  96.             finalNoiseArray[bottomMiddle][0] = thisLevelNoiseValues[1][0]; //left middle
  97.            
  98.             finalNoiseArray[topMiddle][finalNoiseSize - 1] = thisLevelNoiseValues[1][2]; //right middle
  99.             finalNoiseArray[bottomMiddle][finalNoiseSize - 1] = thisLevelNoiseValues[1][2]; //right middle
  100.            
  101.             finalNoiseArray[leftMiddle][topMiddle] = thisLevelNoiseValues[1][1]; //center
  102.             finalNoiseArray[rightMiddle][topMiddle] = thisLevelNoiseValues[1][1]; //center
  103.             finalNoiseArray[leftMiddle][bottomMiddle] = thisLevelNoiseValues[1][1]; //center
  104.             finalNoiseArray[rightMiddle][bottomMiddle] = thisLevelNoiseValues[1][1]; //center
  105.         }
  106.        
  107.         //now that we have seeded the values, we can find all the smaller detail levels
  108.         if (finalNoiseSize <= 3)  {
  109.             return finalNoiseArray; //lowest possible detail level
  110.         }
  111.         else //needs to be split up
  112.         {
  113.             int newSize = getSubdivisionSize(finalNoiseSize);
  114.            
  115.             double[][] topLeftArray = fillNoiseArray(newSize,getSubdivision(finalNoiseSize,0,0, finalNoiseArray));
  116.             double[][] topRightArray = fillNoiseArray(newSize,getSubdivision(finalNoiseSize,0,newSize, finalNoiseArray));
  117.             double[][] bottomLeftArray = fillNoiseArray(newSize,getSubdivision(finalNoiseSize,newSize,0, finalNoiseArray));
  118.             double[][] bottomRightArray = fillNoiseArray(newSize,getSubdivision(finalNoiseSize,newSize,newSize, finalNoiseArray));
  119.            
  120.             return addNoiseArrays(topLeftArray,topRightArray,bottomLeftArray,bottomRightArray, newSize, finalNoiseSize);
  121.  
  122.         }      
  123.     }
  124.  
  125.     public double[][] addNoiseArrays(double[][] topLeftArray,
  126.             double[][] topRightArray, double[][] bottomLeftArray,
  127.             double[][] bottomRightArray, int oldSize, int finalSize) throws ArrayIndexOutOfBoundsException {
  128.        
  129.         double[][] newArray = new double[finalSize][finalSize];
  130.        
  131.         //do the top left
  132.         for (int i = 0; i < oldSize; i++) {
  133.             for (int j = 0; j < oldSize; j++) {
  134.                 newArray[i][j] = topLeftArray[i][j];
  135.             }
  136.         }
  137.         //then the top right
  138.         for (int i = 0; i < oldSize; i++) {
  139.             for (int j = 0; j < oldSize; j++) {
  140.                 newArray[i][j + oldSize] = topRightArray[i][j];
  141.             }
  142.         }
  143.         //then the bottom left
  144.         for (int i = 0; i < oldSize; i++) {
  145.             for (int j = 0; j < oldSize; j++) {
  146.                 newArray[i + oldSize][j] = bottomRightArray[i][j];
  147.             }
  148.         }
  149.         //then the bottom right
  150.         for (int i = 0; i < oldSize; i++) {
  151.             for (int j = 0; j < oldSize; j++) {
  152.                 newArray[i + oldSize][j + oldSize] = bottomRightArray[i][j];
  153.             }
  154.         }
  155.        
  156.         return newArray;
  157.     }
  158.  
  159.     public double[][] getSubdivision(int noiseSize, int startX, int startZ, double[][] noiseArray) {
  160.        
  161.         int newSize = getSubdivisionSize(noiseSize);
  162.        
  163.         double[][] subdivision = new double[newSize][newSize];
  164.        
  165.         for (int i = startX; i < newSize; i++) {
  166.             for (int j = startZ; j < newSize; j++) {
  167.                 subdivision[i][j] = noiseArray[i][j];
  168.             }
  169.         }
  170.        
  171.         return subdivision;
  172.     }
  173.     public int getSubdivisionSize(int noiseSize) {
  174.         int newSize = 0;
  175.        
  176.         if (noiseSize%2 == 0) newSize = noiseSize/2;
  177.         else newSize = (int)(noiseSize / 2 + 0.5);
  178.         return newSize;
  179.     }
  180.  
  181.     /**
  182.      * @author jtjj222
  183.      * @param topLeft
  184.      * The top left seeded value for the square
  185.      * @param topRight
  186.      * The top right seeded value for the square
  187.      * @param bottumLeft
  188.      * The bottom left seeded value for the square
  189.      * @param bottumRight
  190.      * The bottom right seeded value for the square
  191.      * @param diamondError
  192.      * The error applied to the center of the square.
  193.      * @param squareError
  194.      * The error applied to the sides of the square. If this value is high on the biggest
  195.      * "square", there will be more volatile terrain.
  196.      * @return an array with the results from the diamond square
  197.      *         algorithm, at the following array locations:
  198.      * (0,0)(0,1)(0,2)
  199.      * (1,0)(1,1)(1,2)
  200.      * (2,0)(2,1)(2,2)
  201.      */
  202.     public double[][] getCornerNosieValues(double topLeft, double topRight, double bottumLeft, double bottumRight, double diamondError, double squareError) {
  203.         double[][] noiseArray = new double[3][3];
  204.        
  205.         noiseArray[0][0] = topLeft;    
  206.         noiseArray[0][2] = topRight;
  207.        
  208.         double topMiddle = mean(topLeft,topRight) + squareError;
  209.         noiseArray[0][1] = topMiddle;
  210.        
  211.         noiseArray[2][0] = bottumLeft;
  212.         noiseArray[2][2] = bottumRight;
  213.        
  214.         double bottumMiddle = mean(bottumLeft,bottumRight) + squareError;
  215.         noiseArray[2][1] = bottumMiddle;
  216.        
  217.         double centre = mean(topLeft,topRight,bottumLeft,bottumRight) + diamondError;
  218.         noiseArray[1][1] = centre;
  219.        
  220.         double leftMiddle = mean(topLeft,bottumLeft) + squareError;
  221.         noiseArray[1][0] = leftMiddle;
  222.        
  223.         double rightMiddle = mean(topRight,bottumRight) + squareError;
  224.         noiseArray[1][2] = rightMiddle;
  225.        
  226.         return noiseArray;
  227.     }
  228.    
  229.     public double mean(double a, double b, double c, double d) {
  230.         return (a+b+c+d)/4;
  231.     }
  232.    
  233.     public double mean(double a, double b) {
  234.         return (a+b)/2;
  235.     }
  236.    
  237.     public double mean(double a, double b, double c, double d, double e, double f, double g, double h, double i) {
  238.         return (a+b+c+d+e+f+g+h+i)/9;
  239.     }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement