DEADSOFTWARE

Implement DI for menu and refactor #13
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / WorldGen.java
1 package ru.deadsoftware.cavedroid.game;
3 import com.badlogic.gdx.math.RandomXS128;
4 import com.badlogic.gdx.utils.TimeUtils;
6 import javax.annotation.CheckForNull;
8 class WorldGen {
10 @CheckForNull
11 private static RandomXS128 rand;
12 private static long seed;
14 @CheckForNull
15 private static int[][] foreMap, backMap;
16 @CheckForNull
17 private static int[] hMap;
18 @CheckForNull
19 private static int[] bMap; //biomes, 0-plains, 1-desert
21 public static long getSeed() {
22 return seed;
23 }
25 static int[] genLandscape(int width, int mid, int min, int max) {
26 int[] res = new int[width];
27 bMap = new int[width];
28 int t;
29 res[0] = mid;
30 for (int i = 1; i < width; i++) {
31 t = rand.nextInt(7) - 3;
32 if (t > -3 && t < 3) {
33 t = 0;
34 } else {
35 t /= Math.abs(t);
36 }
37 if (i > width - (max - min)) {
38 if (res[i - 1] + t < res[0]) {
39 t = Math.abs(t);
40 } else if (res[i - 1] + t > res[0]) {
41 t = -Math.abs(t);
42 }
43 }
44 res[i] = res[i - 1] + t;
45 if (res[i] < min) {
46 res[i] = min;
47 }
48 if (res[i] > max) {
49 res[i] = max;
50 }
51 bMap[i] = 0;
52 // if (i >= width / 2) {
53 // bMap[i] = 1;
54 // if (res[i] < 60) res[i] = 60;
55 // } else {
56 // bMap[i] = 0;
57 // }
58 }
59 if (res[0] < res[width - 1]) {
60 res[width - 1] = res[0];
61 }
62 return res;
63 }
65 private static void genCactus(int x, int y) {
66 assert foreMap != null;
67 foreMap[x][y] = 59;
68 foreMap[x][y - 1] = 59;
69 foreMap[x][y - 2] = 59;
70 }
72 private static void genOak(int x, int y) {
73 assert foreMap != null && backMap != null;
74 backMap[x][y] = 15;
75 backMap[x][y - 1] = 15;
76 backMap[x][y - 2] = 15;
77 backMap[x][y - 3] = 15;
78 backMap[x][y - 4] = 16;
79 backMap[x][y - 5] = 16;
80 backMap[x - 1][y - 3] = 16;
81 backMap[x - 1][y - 4] = 16;
82 backMap[x + 1][y - 3] = 16;
83 backMap[x + 1][y - 4] = 16;
84 foreMap[x][y - 3] = 16;
85 foreMap[x][y - 4] = 16;
86 foreMap[x][y - 5] = 16;
87 foreMap[x - 1][y - 3] = 16;
88 foreMap[x - 1][y - 4] = 16;
89 foreMap[x + 1][y - 3] = 16;
90 foreMap[x + 1][y - 4] = 16;
91 }
93 static void genWorld(int width, int height) {
94 genWorld(width, height, TimeUtils.millis());
95 }
97 static void genWorld(int width, int height, long worldseed) {
98 int dirtH;
99 seed = worldseed;
100 rand = new RandomXS128(seed);
101 foreMap = new int[width][height];
102 backMap = new int[width][height];
103 hMap = genLandscape(width, height / 4, height / 8, height / 2);
104 for (int x = 0; x < width; x++) {
105 dirtH = 4 + rand.nextInt(2);
106 for (int y = height - hMap[x]; y < height; y++) {
107 if (y == height - hMap[x]) {
108 switch (bMap[x]) {
109 case 0:
110 foreMap[x][y] = 2;
111 backMap[x][y] = 2;
112 break;
113 case 1:
114 foreMap[x][y] = 10;
115 backMap[x][y] = 10;
116 break;
118 } else if (y < height - hMap[x] + dirtH) {
119 switch (bMap[x]) {
120 case 0:
121 foreMap[x][y] = 3;
122 backMap[x][y] = 3;
123 break;
124 case 1:
125 foreMap[x][y] = 10;
126 backMap[x][y] = 10;
127 break;
129 } else if (bMap[x] == 1 && y < height - hMap[x] + dirtH + 3) {
130 foreMap[x][y] = 21;
131 backMap[x][y] = 21;
132 } else if (y < height - 1) {
133 foreMap[x][y] = 1;
134 backMap[x][y] = 1;
135 } else {
136 foreMap[x][y] = 7;
137 backMap[x][y] = 7;
140 for (int y = height - 60; y < height - 1; y++) {
141 if (foreMap[x][y] == 0 && bMap[x] != 1) {
142 foreMap[x][y] = 8;
143 if (bMap[x] == 0) {
144 if (y == height - 60) {
145 backMap[x][y] = 2;
146 } else {
147 backMap[x][y] = 3;
150 if (y == height - hMap[x] - 1) {
151 foreMap[x][y + 1] = 3;
152 backMap[x][y + 1] = 3;
156 if (x > 2 && x < width - 2) {
157 if (foreMap[x][height - hMap[x] - 1] == 0 && foreMap[x][height - hMap[x]] == 2) {
158 switch (rand.nextInt(50)) {
159 case 0:
160 genOak(x, height - hMap[x] - 1);
161 break;
162 case 1:
163 foreMap[x][height - hMap[x] - 1] = 26;
164 break;
165 case 2:
166 foreMap[x][height - hMap[x] - 1] = 29;
167 break;
168 case 3:
169 foreMap[x][height - hMap[x] - 1] = 30;
170 break;
171 case 4:
172 foreMap[x][height - hMap[x] - 1] = 31;
173 break;
174 case 5:
175 foreMap[x][height - hMap[x] - 1] = 32;
176 break;
179 if (foreMap[x][height - hMap[x] - 1] == 0 && foreMap[x][height - hMap[x]] == 10) {
180 switch (rand.nextInt(20)) {
181 case 0:
182 genCactus(x, height - hMap[x] - 1);
183 break;
184 case 1:
185 foreMap[x][height - hMap[x] - 1] = 27;
186 break;
193 static int[][] getForeMap() {
194 assert foreMap != null;
195 return foreMap;
198 static int[][] getBackMap() {
199 assert backMap != null;
200 return backMap;
203 static void clear() {
204 hMap = null;
205 bMap = null;