Advertisement
Guest User

Java random terrain generation

a guest
Feb 4th, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.78 KB | None | 0 0
  1. package com.gopro2027.lwjgl.genertor;
  2.  
  3. import java.util.Random;
  4.  
  5. import org.lwjgl.util.vector.Vector3f;
  6.  
  7. import com.gopro2027.lwjgl.Camera;
  8. import com.gopro2027.lwjgl.Start;
  9. import com.gopro2027.lwjgl.TextureHandler;
  10. import com.gopro2027.lwjgl.Triangle;
  11. import com.gopro2027.lwjgl.server.Message;
  12. import com.gopro2027.lwjgl.server.ObjectMessage;
  13. import com.gopro2027.lwjgl.server.ServerData;
  14.  
  15. public class WorldGeneration {
  16.     /** Source of entropy */
  17.     private Random rand_;
  18.  
  19.     /** Amount of roughness */
  20.     float roughness_;
  21.  
  22.     /** Plasma fractal grid */
  23.     private float[][] grid_;
  24.    
  25.     private int startx = 0;
  26.     private int starty = 0;
  27.  
  28.  
  29.     /** Generate a noise source based upon the midpoint displacement fractal.
  30.      *
  31.      * @param rand The random number generator
  32.      * @param roughness a roughness parameter
  33.      * @param width the width of the grid
  34.      * @param height the height of the grid
  35.      */
  36.     public WorldGeneration(Random rand, float roughness, int width, int height) {
  37.         roughness_ = roughness / width;
  38.         grid_ = new float[width][height];
  39.         rand_ = (rand == null) ? new Random() : rand;
  40.     }
  41.    
  42.     public WorldGeneration(Random rand, float roughness, int width, int height, int startx, int starty) {
  43.         roughness_ = roughness / width;
  44.         grid_ = new float[width][height];
  45.         rand_ = (rand == null) ? new Random() : rand;
  46.         this.startx = startx;
  47.         this.starty = starty;
  48.     }
  49.  
  50.  
  51.     public void initialise() {
  52.         int xh = grid_.length +startx - 1;
  53.         int yh = grid_[0].length+starty - 1;
  54.  
  55.         // set the corner points
  56.         grid_[0][0] = rand_.nextFloat() - 0.5f;
  57.         grid_[0][yh] = rand_.nextFloat() - 0.5f;
  58.         grid_[xh][0] = rand_.nextFloat() - 0.5f;
  59.         grid_[xh][yh] = rand_.nextFloat() - 0.5f;
  60.  
  61.         // generate the fractal
  62.         generate(0, 0, xh, yh);
  63.     }
  64.  
  65.  
  66.     // Add a suitable amount of random displacement to a point
  67.     private float roughen(float v, int l, int h) {
  68.         return v + roughness_ * (float) (rand_.nextGaussian() * (h - l));
  69.     }
  70.  
  71.  
  72.     // generate the fractal
  73.     private void generate(int xl, int yl, int xh, int yh) {
  74.         int xm = (xl + xh) / 2;
  75.         int ym = (yl + yh) / 2;
  76.         if ((xl == xm) && (yl == ym)) return;
  77.  
  78.         grid_[xm][yl] = 0.5f * (grid_[xl][yl] + grid_[xh][yl]);
  79.         grid_[xm][yh] = 0.5f * (grid_[xl][yh] + grid_[xh][yh]);
  80.         grid_[xl][ym] = 0.5f * (grid_[xl][yl] + grid_[xl][yh]);
  81.         grid_[xh][ym] = 0.5f * (grid_[xh][yl] + grid_[xh][yh]);
  82.  
  83.         float v = roughen(0.5f * (grid_[xm][yl] + grid_[xm][yh]), xl + yl, yh + xh);
  84.         grid_[xm][ym] = v;
  85.         grid_[xm][yl] = roughen(grid_[xm][yl], xl, xh);
  86.         grid_[xm][yh] = roughen(grid_[xm][yh], xl, xh);
  87.         grid_[xl][ym] = roughen(grid_[xl][ym], yl, yh);
  88.         grid_[xh][ym] = roughen(grid_[xh][ym], yl, yh);
  89.  
  90.         generate(xl, yl, xm, ym);
  91.         generate(xm, yl, xh, ym);
  92.         generate(xl, ym, xm, yh);
  93.         generate(xm, ym, xh, yh);
  94.     }
  95.    
  96.     public float[][] getMap() {
  97.         return grid_;
  98.     }
  99.     /*public boolean[][] toBooleans() {
  100.         int w = grid_.length;
  101.         int h = grid_[0].length;
  102.         boolean[][] ret = new boolean[w][h];
  103.         for(int i = 0;i < w;i++) {
  104.             for(int j = 0;j < h;j++) {
  105.                 ret[i][j] = grid_[i][j] < 0;
  106.             }
  107.         }
  108.         return ret;
  109.     }*/
  110.    
  111.     public static void makeMap(int size) {
  112.         Random r = new Random();
  113.         WorldGeneration n = new WorldGeneration(r, 100, size, size);
  114.         n.initialise();
  115.         float m = 50;
  116.         float[][] map = n.getMap();
  117.         for (int x = 0; x < size; x++) {
  118.             for (int y = 0; y < size; y++) {
  119.                 try {
  120.                     Vector3f v1 = new Vector3f(x*m,map[x][y],y*m);
  121.                     Vector3f v2 = new Vector3f((x+1)*m,map[x+1][y],y*m);
  122.                     Vector3f v3 = new Vector3f((x+1)*m,map[x+1][y+1],(y+1)*m);
  123.                     Vector3f v4 = new Vector3f(x*m,map[x][y+1],(y+1)*m);
  124.                     Triangle t = new Triangle(v1,v2,v3,TextureHandler.getTexture(2),true);
  125.                     Triangle t1 = new Triangle(v3,v4,v1,TextureHandler.getTexture(2),true);
  126.                     t.addToGenList();
  127.                     t1.addToGenList();
  128.                     t.TextureNum = 2;
  129.                     t1.TextureNum = 2;
  130.                     t.add();
  131.                     t1.add();
  132.                    
  133.                     if (Start.hosting) {
  134.                         Message message = new Message("add");
  135.                         ObjectMessage objm = new ObjectMessage(t,message);
  136.                         ServerData.addMessage(objm);
  137.                         ObjectMessage objm1 = new ObjectMessage(t1,message);
  138.                         ServerData.addMessage(objm1);
  139.                     }
  140.                 } catch(Exception e){}
  141.             }
  142.         }
  143.     }
  144.    
  145.     /**
  146.      *
  147.      * @param size the size of the map
  148.      * @param height this should vary based on the dilation
  149.      * @param dilation this is how big the triangles will be. The bigger,the less size you need and less lag
  150.      * @param seed this is the seed. put -1 for random
  151.      */
  152.     public static void makeMapAdvanced(int size, float height, int dilation, long seed) {
  153.         Random r;
  154.         if (dilation < 20) {
  155.             dilation = 20;
  156.         }
  157.         if (seed == -1) {
  158.             r = new Random();
  159.         } else {
  160.             r = new Random(seed);
  161.         }
  162.         float dh = 50/height;
  163.         float dd = (dilation)/20;
  164.         height = size/dh*dd;
  165.         WorldGeneration n = new WorldGeneration(r, height, size, size);
  166.         n.initialise();
  167.         float m = dilation;
  168.         float[][] map = n.getMap();
  169.         float[][] biomes = WorldGeneration.genArray(size,height,dilation,seed);
  170.         float smallest = 10000;
  171.         for (int x = 0; x < biomes.length; x++) {
  172.             for (int y = 0; y < biomes[0].length; y++) {
  173.                 if (biomes[x][y] < smallest) {
  174.                     smallest = biomes[x][y];
  175.                 }
  176.             }
  177.         }
  178.         for (int x = 0; x < biomes.length; x++) {
  179.             for (int y = 0; y < biomes[0].length; y++) {
  180.                 biomes[x][y] = biomes[x][y] + Math.abs(smallest);
  181.             }
  182.         }
  183.         for (int x = 0; x < biomes.length; x++) {
  184.             for (int y = 0; y < biomes[0].length; y++) {
  185.                 biomes[x][y] = biomes[x][y]/Math.abs(smallest);
  186.             }
  187.         }
  188.         for (int x = 0; x < biomes.length; x++) {
  189.             for (int y = 0; y < biomes[0].length; y++) {
  190.                 biomes[x][y] = (int)(biomes[x][y]*3);
  191.             }
  192.         }
  193.         boolean centerdone = false;
  194.         for (int x = 0; x < size; x++) {
  195.             for (int y = 0; y < size; y++) {
  196.                 try {
  197.                     if (x>map.length/2 && y>map.length/2 && !centerdone) {
  198.                         Camera.spawnPoint = new Vector3f(x*m,map[x][y]+5,y*m);
  199.                         centerdone = true;
  200.                     }
  201.                     Vector3f v1 = new Vector3f(x*m,map[x][y],y*m);//none
  202.                     Vector3f v2 = new Vector3f((x+1)*m,map[x+1][y],y*m);//x
  203.                     Vector3f v3 = new Vector3f((x+1)*m,map[x+1][y+1],(y+1)*m);//both
  204.                     Vector3f v4 = new Vector3f(x*m,map[x][y+1],(y+1)*m);//y
  205.                     int tex = 1;
  206.                     int b1 = (int) biomes[x][y];
  207.                     if (b1 == 1) {
  208.                         tex = 1;
  209.                     }
  210.                     if (b1 == 2) {
  211.                         tex = 3;
  212.                     }
  213.                     if (b1 > 2) {
  214.                         tex = 2;
  215.                     }
  216.                     Triangle t = new Triangle(v1,v2,v3,TextureHandler.getTexture(tex),true);
  217.                     Triangle t1 = new Triangle(v1,v4,v3,TextureHandler.getTexture(tex),true);
  218.                     t.addToGenList();
  219.                     t1.addToGenList();
  220.                     t.TextureNum = 2;
  221.                     t1.TextureNum = 2;
  222.                     t.add();
  223.                     t1.add();
  224.                     if (Start.hosting) {
  225.                         Message message = new Message("add");
  226.                         ObjectMessage objm = new ObjectMessage(t,message);
  227.                         ServerData.addMessage(objm);
  228.                         ObjectMessage objm1 = new ObjectMessage(t1,message);
  229.                         ServerData.addMessage(objm1);
  230.                     }
  231.                 } catch(Exception e){
  232.                 }
  233.             }
  234.         }
  235.     }
  236.    
  237.    
  238.    
  239.     /**
  240.      *
  241.      * @param size the size of the map
  242.      * @param height this should vary based on the dilation
  243.      * @param dilation this is how big the triangles will be. The bigger,the less size you need and less lag
  244.      * @param seed this is the seed. put -1 for random
  245.      * @param startx x to start from
  246.      * @param starty y to start from
  247.      */
  248.     public static float[][] genArray(int size, float height, int dilation, long seed) {
  249.         Random r;
  250.         if (dilation < 20) {
  251.             dilation = 20;
  252.         }
  253.         if (seed == -1) {
  254.             r = new Random();
  255.         } else {
  256.             r = new Random(seed);
  257.         }
  258.         float dh = 50/height;
  259.         float dd = (dilation)/20;
  260.         height = size/dh*dd;
  261.         WorldGeneration n = new WorldGeneration(r, height, size, size);
  262.         n.initialise();
  263.         return n.getMap();
  264.     }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement