DEADSOFTWARE

Minor enhancements
[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;
14 private static int[] bMap; //biomes, 0-plains, 1-desert
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 bMap = new int[width];
23 int t;
24 res[0] = mid;
25 for (int i=1; i<width; i++) {
26 t = rand.nextInt(7)-3;
27 if (t>-3 && t<3) t=0; else t/=Math.abs(t);
28 if (i>width-(max-min)) {
29 if (res[i-1]+t<res[0]) t=Math.abs(t);
30 else if (res[i-1]+t>res[0]) t=-Math.abs(t);
31 }
32 res[i] = res[i-1] + t;
33 if (res[i]<min) res[i] = min;
34 if (res[i]>max) res[i] = max;
35 if (i>=width/2) {
36 bMap [i] = 1;
37 if (res[i] < 60) res[i] = 60;
38 } else {
39 bMap[i] = 0;
40 }
41 }
42 if (res[0]<res[width-1]) res[width-1] = res[0];
43 return res;
44 }
46 private static void genCactus(int x, int y) {
47 foreMap[x][y] = 59;
48 foreMap[x][y-1] = 59;
49 foreMap[x][y-2] = 59;
50 }
52 private static void genOak(int x, int y) {
53 backMap[x][y] = 15;
54 backMap[x][y-1] = 15;
55 backMap[x][y-2] = 15;
56 backMap[x][y-3] = 15;
57 backMap[x][y-4] = 16;
58 backMap[x][y-5] = 16;
59 backMap[x-1][y-3] = 16;
60 backMap[x-1][y-4] = 16;
61 backMap[x+1][y-3] = 16;
62 backMap[x+1][y-4] = 16;
63 foreMap[x][y-3] = 16;
64 foreMap[x][y-4] = 16;
65 foreMap[x][y-5] = 16;
66 foreMap[x-1][y-3] = 16;
67 foreMap[x-1][y-4] = 16;
68 foreMap[x+1][y-3] = 16;
69 foreMap[x+1][y-4] = 16;
70 }
72 static void genWorld(int width, int height) {
73 genWorld(width, height, TimeUtils.millis());
74 }
76 static void genWorld(int width, int height, long worldseed) {
77 int dirtH;
78 seed = worldseed;
79 rand = new RandomXS128(seed);
80 foreMap = new int[width][height];
81 backMap = new int[width][height];
82 hMap = genLandscape(width, height/4, height/8, height/2);
83 for (int x=0; x<width; x++) {
84 dirtH = 4+rand.nextInt(2);
85 for (int y = height- hMap[x]; y<height; y++) {
86 if (y==height- hMap[x]) {
87 switch (bMap[x]) {
88 case 0:
89 foreMap[x][y] = 2;
90 backMap[x][y] = 2;
91 break;
92 case 1:
93 foreMap[x][y] = 10;
94 backMap[x][y] = 10;
95 break;
96 }
97 } else if (y<height-hMap[x]+dirtH) {
98 switch (bMap[x]) {
99 case 0:
100 foreMap[x][y] = 3;
101 backMap[x][y] = 3;
102 break;
103 case 1:
104 foreMap[x][y] = 10;
105 backMap[x][y] = 10;
106 break;
108 } else if (bMap[x]==1 && y<height-hMap[x]+dirtH+3) {
109 foreMap[x][y] = 21;
110 backMap[x][y] = 21;
111 } else if (y<height-1){
112 foreMap[x][y] = 1;
113 backMap[x][y] = 1;
114 } else {
115 foreMap[x][y] = 7;
116 backMap[x][y] = 7;
119 for (int y = height-60; y<height-1; y++) {
120 if (foreMap[x][y]==0 && bMap[x]!=1){
121 foreMap[x][y] = 8;
122 if (bMap[x] == 0) {
123 if (y==height-60) {
124 backMap[x][y] = 2;
125 } else {
126 backMap[x][y] = 3;
129 if (y==height-hMap[x]-1) {
130 foreMap[x][y+1] = 3;
131 backMap[x][y+1] = 3;
135 if (x>2 && x<width-2){
136 if (foreMap[x][height-hMap[x]-1]==0 && foreMap[x][height-hMap[x]]==2) {
137 switch (rand.nextInt(50)) {
138 case 0:
139 genOak(x, height - hMap[x] - 1);
140 break;
141 case 1:
142 foreMap[x][height-hMap[x]-1] = 26;
143 break;
144 case 2:
145 foreMap[x][height-hMap[x]-1] = 29;
146 break;
147 case 3:
148 foreMap[x][height-hMap[x]-1] = 30;
149 break;
150 case 4:
151 foreMap[x][height-hMap[x]-1] = 31;
152 break;
153 case 5:
154 foreMap[x][height-hMap[x]-1] = 32;
155 break;
158 if (foreMap[x][height-hMap[x]-1]==0 && foreMap[x][height-hMap[x]]==10) {
159 switch(rand.nextInt(20)) {
160 case 0:
161 genCactus(x,height-hMap[x]-1);
162 break;
163 case 1:
164 foreMap[x][height-hMap[x]-1] = 27;
165 break;
172 static int[][] getForeMap() {
173 return foreMap;
176 static int[][] getBackMap() {
177 return backMap;
180 static void clear() {
181 foreMap = null;
182 backMap = null;
183 hMap = null;
184 bMap = null;