DEADSOFTWARE

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