DEADSOFTWARE

More blocks, trees
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / WorldGen.java
1 package ru.deadsoftware.cavecraft.game;
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.math.RandomXS128;
5 import com.badlogic.gdx.utils.TimeUtils;
7 public class WorldGen {
9 private static RandomXS128 rand;
10 private static long seed;
12 private static int[][] foreMap, backMap;
13 private static int[] hMap;
15 public static long getSeed() {
16 return seed;
17 }
19 static int[] genLandscape(int width, int mid, int min, int max) {
20 int[] res = new int[width];
21 int t;
22 res[0] = mid;
23 for (int i=1; i<width; i++) {
24 t = rand.nextInt(3)-1;
25 res[i] = res[i-1] + t;
26 if (res[i]<min) res[i] = min;
27 if (res[i]>max) res[i] = max;
28 }
29 return res;
30 }
32 private static void genOak(int x, int y) {
33 backMap[x][y] = 15;
34 backMap[x][y-1] = 15;
35 backMap[x][y-2] = 15;
36 backMap[x][y-3] = 15;
37 backMap[x][y-4] = 16;
38 backMap[x][y-5] = 16;
39 backMap[x-1][y-3] = 16;
40 backMap[x-1][y-4] = 16;
41 backMap[x+1][y-3] = 16;
42 backMap[x+1][y-4] = 16;
43 foreMap[x][y-3] = 16;
44 foreMap[x][y-4] = 16;
45 foreMap[x][y-5] = 16;
46 foreMap[x-1][y-3] = 16;
47 foreMap[x-1][y-4] = 16;
48 foreMap[x+1][y-3] = 16;
49 foreMap[x+1][y-4] = 16;
50 }
52 static void genWorld(int width, int height) {
53 genWorld(width, height, TimeUtils.millis());
54 }
56 static void genWorld(int width, int height, long s) {
57 int dirtH;
58 seed = s;
59 rand = new RandomXS128(seed);
60 foreMap = new int[width][height];
61 backMap = new int[width][height];
62 hMap = genLandscape(width, height/2, height/4, height/4*3);
63 for (int x=0; x<width; x++) {
64 dirtH = 4+rand.nextInt(2);
65 for (int y = height- hMap[x]; y<height; y++) {
66 if (y==height- hMap[x]) {
67 foreMap[x][y] = 2;
68 backMap[x][y] = 2;
69 } else if (y<height-hMap[x]+dirtH) {
70 foreMap[x][y] = 3;
71 backMap[x][y] = 3;
72 } else if (y<height-1){
73 foreMap[x][y] = 1;
74 backMap[x][y] = 1;
75 } else {
76 foreMap[x][y] = 7;
77 backMap[x][y] = 7;
78 }
79 }
80 for (int y = height/4*3; y<height- hMap[x]; y++) {
81 if (foreMap[x][y]==0){
82 foreMap[x][y] = 8;
83 backMap[x][y] = 8;
84 }
85 }
86 if (x>2 && x<width-2 && rand.nextInt(100)<5) genOak(x,height-hMap[x]-1);
87 }
88 }
90 static int[][] getForeMap() {
91 return foreMap;
92 }
94 static int[][] getBackMap() {
95 return backMap;
96 }
98 static void clear() {
99 foreMap = null;
100 backMap = null;