Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gopro2027.lwjgl.genertor;
- import java.util.Random;
- import org.lwjgl.util.vector.Vector3f;
- import com.gopro2027.lwjgl.Camera;
- import com.gopro2027.lwjgl.Start;
- import com.gopro2027.lwjgl.TextureHandler;
- import com.gopro2027.lwjgl.Triangle;
- import com.gopro2027.lwjgl.server.Message;
- import com.gopro2027.lwjgl.server.ObjectMessage;
- import com.gopro2027.lwjgl.server.ServerData;
- public class WorldGeneration {
- /** Source of entropy */
- private Random rand_;
- /** Amount of roughness */
- float roughness_;
- /** Plasma fractal grid */
- private float[][] grid_;
- private int startx = 0;
- private int starty = 0;
- /** Generate a noise source based upon the midpoint displacement fractal.
- *
- * @param rand The random number generator
- * @param roughness a roughness parameter
- * @param width the width of the grid
- * @param height the height of the grid
- */
- public WorldGeneration(Random rand, float roughness, int width, int height) {
- roughness_ = roughness / width;
- grid_ = new float[width][height];
- rand_ = (rand == null) ? new Random() : rand;
- }
- public WorldGeneration(Random rand, float roughness, int width, int height, int startx, int starty) {
- roughness_ = roughness / width;
- grid_ = new float[width][height];
- rand_ = (rand == null) ? new Random() : rand;
- this.startx = startx;
- this.starty = starty;
- }
- public void initialise() {
- int xh = grid_.length +startx - 1;
- int yh = grid_[0].length+starty - 1;
- // set the corner points
- grid_[0][0] = rand_.nextFloat() - 0.5f;
- grid_[0][yh] = rand_.nextFloat() - 0.5f;
- grid_[xh][0] = rand_.nextFloat() - 0.5f;
- grid_[xh][yh] = rand_.nextFloat() - 0.5f;
- // generate the fractal
- generate(0, 0, xh, yh);
- }
- // Add a suitable amount of random displacement to a point
- private float roughen(float v, int l, int h) {
- return v + roughness_ * (float) (rand_.nextGaussian() * (h - l));
- }
- // generate the fractal
- private void generate(int xl, int yl, int xh, int yh) {
- int xm = (xl + xh) / 2;
- int ym = (yl + yh) / 2;
- if ((xl == xm) && (yl == ym)) return;
- grid_[xm][yl] = 0.5f * (grid_[xl][yl] + grid_[xh][yl]);
- grid_[xm][yh] = 0.5f * (grid_[xl][yh] + grid_[xh][yh]);
- grid_[xl][ym] = 0.5f * (grid_[xl][yl] + grid_[xl][yh]);
- grid_[xh][ym] = 0.5f * (grid_[xh][yl] + grid_[xh][yh]);
- float v = roughen(0.5f * (grid_[xm][yl] + grid_[xm][yh]), xl + yl, yh + xh);
- grid_[xm][ym] = v;
- grid_[xm][yl] = roughen(grid_[xm][yl], xl, xh);
- grid_[xm][yh] = roughen(grid_[xm][yh], xl, xh);
- grid_[xl][ym] = roughen(grid_[xl][ym], yl, yh);
- grid_[xh][ym] = roughen(grid_[xh][ym], yl, yh);
- generate(xl, yl, xm, ym);
- generate(xm, yl, xh, ym);
- generate(xl, ym, xm, yh);
- generate(xm, ym, xh, yh);
- }
- public float[][] getMap() {
- return grid_;
- }
- /*public boolean[][] toBooleans() {
- int w = grid_.length;
- int h = grid_[0].length;
- boolean[][] ret = new boolean[w][h];
- for(int i = 0;i < w;i++) {
- for(int j = 0;j < h;j++) {
- ret[i][j] = grid_[i][j] < 0;
- }
- }
- return ret;
- }*/
- public static void makeMap(int size) {
- Random r = new Random();
- WorldGeneration n = new WorldGeneration(r, 100, size, size);
- n.initialise();
- float m = 50;
- float[][] map = n.getMap();
- for (int x = 0; x < size; x++) {
- for (int y = 0; y < size; y++) {
- try {
- Vector3f v1 = new Vector3f(x*m,map[x][y],y*m);
- Vector3f v2 = new Vector3f((x+1)*m,map[x+1][y],y*m);
- Vector3f v3 = new Vector3f((x+1)*m,map[x+1][y+1],(y+1)*m);
- Vector3f v4 = new Vector3f(x*m,map[x][y+1],(y+1)*m);
- Triangle t = new Triangle(v1,v2,v3,TextureHandler.getTexture(2),true);
- Triangle t1 = new Triangle(v3,v4,v1,TextureHandler.getTexture(2),true);
- t.addToGenList();
- t1.addToGenList();
- t.TextureNum = 2;
- t1.TextureNum = 2;
- t.add();
- t1.add();
- if (Start.hosting) {
- Message message = new Message("add");
- ObjectMessage objm = new ObjectMessage(t,message);
- ServerData.addMessage(objm);
- ObjectMessage objm1 = new ObjectMessage(t1,message);
- ServerData.addMessage(objm1);
- }
- } catch(Exception e){}
- }
- }
- }
- /**
- *
- * @param size the size of the map
- * @param height this should vary based on the dilation
- * @param dilation this is how big the triangles will be. The bigger,the less size you need and less lag
- * @param seed this is the seed. put -1 for random
- */
- public static void makeMapAdvanced(int size, float height, int dilation, long seed) {
- Random r;
- if (dilation < 20) {
- dilation = 20;
- }
- if (seed == -1) {
- r = new Random();
- } else {
- r = new Random(seed);
- }
- float dh = 50/height;
- float dd = (dilation)/20;
- height = size/dh*dd;
- WorldGeneration n = new WorldGeneration(r, height, size, size);
- n.initialise();
- float m = dilation;
- float[][] map = n.getMap();
- float[][] biomes = WorldGeneration.genArray(size,height,dilation,seed);
- float smallest = 10000;
- for (int x = 0; x < biomes.length; x++) {
- for (int y = 0; y < biomes[0].length; y++) {
- if (biomes[x][y] < smallest) {
- smallest = biomes[x][y];
- }
- }
- }
- for (int x = 0; x < biomes.length; x++) {
- for (int y = 0; y < biomes[0].length; y++) {
- biomes[x][y] = biomes[x][y] + Math.abs(smallest);
- }
- }
- for (int x = 0; x < biomes.length; x++) {
- for (int y = 0; y < biomes[0].length; y++) {
- biomes[x][y] = biomes[x][y]/Math.abs(smallest);
- }
- }
- for (int x = 0; x < biomes.length; x++) {
- for (int y = 0; y < biomes[0].length; y++) {
- biomes[x][y] = (int)(biomes[x][y]*3);
- }
- }
- boolean centerdone = false;
- for (int x = 0; x < size; x++) {
- for (int y = 0; y < size; y++) {
- try {
- if (x>map.length/2 && y>map.length/2 && !centerdone) {
- Camera.spawnPoint = new Vector3f(x*m,map[x][y]+5,y*m);
- centerdone = true;
- }
- Vector3f v1 = new Vector3f(x*m,map[x][y],y*m);//none
- Vector3f v2 = new Vector3f((x+1)*m,map[x+1][y],y*m);//x
- Vector3f v3 = new Vector3f((x+1)*m,map[x+1][y+1],(y+1)*m);//both
- Vector3f v4 = new Vector3f(x*m,map[x][y+1],(y+1)*m);//y
- int tex = 1;
- int b1 = (int) biomes[x][y];
- if (b1 == 1) {
- tex = 1;
- }
- if (b1 == 2) {
- tex = 3;
- }
- if (b1 > 2) {
- tex = 2;
- }
- Triangle t = new Triangle(v1,v2,v3,TextureHandler.getTexture(tex),true);
- Triangle t1 = new Triangle(v1,v4,v3,TextureHandler.getTexture(tex),true);
- t.addToGenList();
- t1.addToGenList();
- t.TextureNum = 2;
- t1.TextureNum = 2;
- t.add();
- t1.add();
- if (Start.hosting) {
- Message message = new Message("add");
- ObjectMessage objm = new ObjectMessage(t,message);
- ServerData.addMessage(objm);
- ObjectMessage objm1 = new ObjectMessage(t1,message);
- ServerData.addMessage(objm1);
- }
- } catch(Exception e){
- }
- }
- }
- }
- /**
- *
- * @param size the size of the map
- * @param height this should vary based on the dilation
- * @param dilation this is how big the triangles will be. The bigger,the less size you need and less lag
- * @param seed this is the seed. put -1 for random
- * @param startx x to start from
- * @param starty y to start from
- */
- public static float[][] genArray(int size, float height, int dilation, long seed) {
- Random r;
- if (dilation < 20) {
- dilation = 20;
- }
- if (seed == -1) {
- r = new Random();
- } else {
- r = new Random(seed);
- }
- float dh = 50/height;
- float dd = (dilation)/20;
- height = size/dh*dd;
- WorldGeneration n = new WorldGeneration(r, height, size, size);
- n.initialise();
- return n.getMap();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement