DEADSOFTWARE

Gameplay enhancements
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / WorldGen.java
1 package ru.deadsoftware.cavecraft.game;
3 import com.badlogic.gdx.math.RandomXS128;
4 import com.badlogic.gdx.utils.ArrayMap;
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 for (int y=0; y<height; y++) {
65 }
66 dirtH = 4+rand.nextInt(2);
67 for (int y = height- hMap[x]; y<height; y++) {
68 if (y==height- hMap[x]) {
69 foreMap[x][y] = 2;
70 backMap[x][y] = 2;
71 } else if (y<height-hMap[x]+dirtH) {
72 foreMap[x][y] = 3;
73 backMap[x][y] = 3;
74 } else if (y<height-1){
75 foreMap[x][y] = 1;
76 backMap[x][y] = 1;
77 } else {
78 foreMap[x][y] = 7;
79 backMap[x][y] = 7;
80 }
81 }
82 for (int y = height/4*3; y<height- hMap[x]; y++) {
83 if (foreMap[x][y]==0){
84 foreMap[x][y] = 8;
85 backMap[x][y] = 8;
86 }
87 }
88 if (x>2 && x<width-2 && rand.nextInt(100)<5){
89 genOak(x,height-hMap[x]-1);
90 }
91 }
92 }
94 static int[][] getForeMap() {
95 return foreMap;
96 }
98 static int[][] getBackMap() {
99 return backMap;
102 static void clear() {
103 foreMap = null;
104 backMap = null;