DEADSOFTWARE

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