DEADSOFTWARE

Fix codestyle
[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 class WorldGen {
8 private static RandomXS128 rand;
9 private static long seed;
11 private static int[][] foreMap, backMap;
12 private static int[] hMap;
13 private static int[] bMap; //biomes, 0-plains, 1-desert
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 bMap = new int[width];
22 int t;
23 res[0] = mid;
24 for (int i = 1; i < width; i++) {
25 t = rand.nextInt(7) - 3;
26 if (t > -3 && t < 3) {
27 t = 0;
28 } else {
29 t /= Math.abs(t);
30 }
31 if (i > width - (max - min)) {
32 if (res[i - 1] + t < res[0]) {
33 t = Math.abs(t);
34 } else if (res[i - 1] + t > res[0]) {
35 t = -Math.abs(t);
36 }
37 }
38 res[i] = res[i - 1] + t;
39 if (res[i] < min) {
40 res[i] = min;
41 }
42 if (res[i] > max) {
43 res[i] = max;
44 }
45 bMap[i] = 0;
46 // if (i >= width / 2) {
47 // bMap[i] = 1;
48 // if (res[i] < 60) res[i] = 60;
49 // } else {
50 // bMap[i] = 0;
51 // }
52 }
53 if (res[0] < res[width - 1]) {
54 res[width - 1] = res[0];
55 }
56 return res;
57 }
59 private static void genCactus(int x, int y) {
60 foreMap[x][y] = 59;
61 foreMap[x][y - 1] = 59;
62 foreMap[x][y - 2] = 59;
63 }
65 private static void genOak(int x, int y) {
66 backMap[x][y] = 15;
67 backMap[x][y - 1] = 15;
68 backMap[x][y - 2] = 15;
69 backMap[x][y - 3] = 15;
70 backMap[x][y - 4] = 16;
71 backMap[x][y - 5] = 16;
72 backMap[x - 1][y - 3] = 16;
73 backMap[x - 1][y - 4] = 16;
74 backMap[x + 1][y - 3] = 16;
75 backMap[x + 1][y - 4] = 16;
76 foreMap[x][y - 3] = 16;
77 foreMap[x][y - 4] = 16;
78 foreMap[x][y - 5] = 16;
79 foreMap[x - 1][y - 3] = 16;
80 foreMap[x - 1][y - 4] = 16;
81 foreMap[x + 1][y - 3] = 16;
82 foreMap[x + 1][y - 4] = 16;
83 }
85 static void genWorld(int width, int height) {
86 genWorld(width, height, TimeUtils.millis());
87 }
89 static void genWorld(int width, int height, long worldseed) {
90 int dirtH;
91 seed = worldseed;
92 rand = new RandomXS128(seed);
93 foreMap = new int[width][height];
94 backMap = new int[width][height];
95 hMap = genLandscape(width, height / 4, height / 8, height / 2);
96 for (int x = 0; x < width; x++) {
97 dirtH = 4 + rand.nextInt(2);
98 for (int y = height - hMap[x]; y < height; y++) {
99 if (y == height - hMap[x]) {
100 switch (bMap[x]) {
101 case 0:
102 foreMap[x][y] = 2;
103 backMap[x][y] = 2;
104 break;
105 case 1:
106 foreMap[x][y] = 10;
107 backMap[x][y] = 10;
108 break;
110 } else if (y < height - hMap[x] + dirtH) {
111 switch (bMap[x]) {
112 case 0:
113 foreMap[x][y] = 3;
114 backMap[x][y] = 3;
115 break;
116 case 1:
117 foreMap[x][y] = 10;
118 backMap[x][y] = 10;
119 break;
121 } else if (bMap[x] == 1 && y < height - hMap[x] + dirtH + 3) {
122 foreMap[x][y] = 21;
123 backMap[x][y] = 21;
124 } else if (y < height - 1) {
125 foreMap[x][y] = 1;
126 backMap[x][y] = 1;
127 } else {
128 foreMap[x][y] = 7;
129 backMap[x][y] = 7;
132 for (int y = height - 60; y < height - 1; y++) {
133 if (foreMap[x][y] == 0 && bMap[x] != 1) {
134 foreMap[x][y] = 8;
135 if (bMap[x] == 0) {
136 if (y == height - 60) {
137 backMap[x][y] = 2;
138 } else {
139 backMap[x][y] = 3;
142 if (y == height - hMap[x] - 1) {
143 foreMap[x][y + 1] = 3;
144 backMap[x][y + 1] = 3;
148 if (x > 2 && x < width - 2) {
149 if (foreMap[x][height - hMap[x] - 1] == 0 && foreMap[x][height - hMap[x]] == 2) {
150 switch (rand.nextInt(50)) {
151 case 0:
152 genOak(x, height - hMap[x] - 1);
153 break;
154 case 1:
155 foreMap[x][height - hMap[x] - 1] = 26;
156 break;
157 case 2:
158 foreMap[x][height - hMap[x] - 1] = 29;
159 break;
160 case 3:
161 foreMap[x][height - hMap[x] - 1] = 30;
162 break;
163 case 4:
164 foreMap[x][height - hMap[x] - 1] = 31;
165 break;
166 case 5:
167 foreMap[x][height - hMap[x] - 1] = 32;
168 break;
171 if (foreMap[x][height - hMap[x] - 1] == 0 && foreMap[x][height - hMap[x]] == 10) {
172 switch (rand.nextInt(20)) {
173 case 0:
174 genCactus(x, height - hMap[x] - 1);
175 break;
176 case 1:
177 foreMap[x][height - hMap[x] - 1] = 27;
178 break;
185 static int[][] getForeMap() {
186 return foreMap;
189 static int[][] getBackMap() {
190 return backMap;
193 static void clear() {
194 foreMap = null;
195 backMap = null;
196 hMap = null;
197 bMap = null;