DEADSOFTWARE

Fix bugs
[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.ArrayMap;
6 import com.badlogic.gdx.utils.TimeUtils;
8 public class WorldGen {
10 private static RandomXS128 rand;
11 private static long seed;
13 private static int[][] foreMap, backMap;
14 private static int[] hMap;
16 public static long getSeed() {
17 return seed;
18 }
20 static int[] genLandscape(int width, int mid, int min, int max) {
21 int[] res = new int[width];
22 int t;
23 res[0] = mid;
24 for (int i=1; i<width; i++) {
25 t = rand.nextInt(3)-1;
26 res[i] = res[i-1] + t;
27 if (res[i]<min) res[i] = min;
28 if (res[i]>max) res[i] = max;
29 }
30 return res;
31 }
33 private static void genOak(int x, int y) {
34 backMap[x][y] = 15;
35 backMap[x][y-1] = 15;
36 backMap[x][y-2] = 15;
37 backMap[x][y-3] = 15;
38 backMap[x][y-4] = 16;
39 backMap[x][y-5] = 16;
40 backMap[x-1][y-3] = 16;
41 backMap[x-1][y-4] = 16;
42 backMap[x+1][y-3] = 16;
43 backMap[x+1][y-4] = 16;
44 foreMap[x][y-3] = 16;
45 foreMap[x][y-4] = 16;
46 foreMap[x][y-5] = 16;
47 foreMap[x-1][y-3] = 16;
48 foreMap[x-1][y-4] = 16;
49 foreMap[x+1][y-3] = 16;
50 foreMap[x+1][y-4] = 16;
51 }
53 static void genWorld(int width, int height) {
54 genWorld(width, height, TimeUtils.millis());
55 }
57 static void genWorld(int width, int height, long worldseed) {
58 int dirtH;
59 seed = worldseed;
60 rand = new RandomXS128(seed);
61 foreMap = new int[width][height];
62 backMap = new int[width][height];
63 hMap = genLandscape(width, height/4, height/8, height/2);
64 for (int x=0; x<width; x++) {
65 dirtH = 4+rand.nextInt(2);
66 for (int y = height- hMap[x]; y<height; y++) {
67 if (y==height- hMap[x]) {
68 foreMap[x][y] = 2;
69 backMap[x][y] = 2;
70 } else if (y<height-hMap[x]+dirtH) {
71 foreMap[x][y] = 3;
72 backMap[x][y] = 3;
73 } else if (y<height-1){
74 foreMap[x][y] = 1;
75 backMap[x][y] = 1;
76 } else {
77 foreMap[x][y] = 7;
78 backMap[x][y] = 7;
79 }
80 }
81 for (int y = height-64; y<height-1; y++) {
82 if (foreMap[x][y]==0){
83 foreMap[x][y] = 8;
84 backMap[x][y] = 8;
85 if (y==height-hMap[x]-1) {
86 foreMap[x][y+1] = 3;
87 }
88 }
89 }
90 if (x>2 && x<width-2 && rand.nextInt(100)<5){
91 if (foreMap[x][height-hMap[x]]-1==0) genOak(x,height-hMap[x]-1);
92 }
93 }
94 }
96 static int[][] getForeMap() {
97 return foreMap;
98 }
100 static int[][] getBackMap() {
101 return backMap;
104 static void clear() {
105 foreMap = null;
106 backMap = null;