DEADSOFTWARE

8c0f4ca7eec7cd7ae6d038ce688fb32872d9fc74
[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 bMap[i] = i/(width/2);
27 t = rand.nextInt(7)-3;
28 if (t>-3 && t<3) t=0; else t/=Math.abs(t);
29 if (i>width-(max-min)) {
30 if (res[i-1]+t<res[0]) t=Math.abs(t);
31 else if (res[i-1]+t>res[0]) t=-Math.abs(t);
32 }
33 res[i] = res[i-1] + t;
34 if (res[i]<min) res[i] = min;
35 if (res[i]>max) res[i] = max;
36 }
37 return res;
38 }
40 private static void genCactus(int x, int y) {
41 foreMap[x][y] = 59;
42 foreMap[x][y-1] = 59;
43 foreMap[x][y-2] = 59;
44 }
46 private static void genOak(int x, int y) {
47 backMap[x][y] = 15;
48 backMap[x][y-1] = 15;
49 backMap[x][y-2] = 15;
50 backMap[x][y-3] = 15;
51 backMap[x][y-4] = 16;
52 backMap[x][y-5] = 16;
53 backMap[x-1][y-3] = 16;
54 backMap[x-1][y-4] = 16;
55 backMap[x+1][y-3] = 16;
56 backMap[x+1][y-4] = 16;
57 foreMap[x][y-3] = 16;
58 foreMap[x][y-4] = 16;
59 foreMap[x][y-5] = 16;
60 foreMap[x-1][y-3] = 16;
61 foreMap[x-1][y-4] = 16;
62 foreMap[x+1][y-3] = 16;
63 foreMap[x+1][y-4] = 16;
64 }
66 static void genWorld(int width, int height) {
67 genWorld(width, height, TimeUtils.millis());
68 }
70 static void genWorld(int width, int height, long worldseed) {
71 int dirtH;
72 seed = worldseed;
73 rand = new RandomXS128(seed);
74 foreMap = new int[width][height];
75 backMap = new int[width][height];
76 hMap = genLandscape(width, height/8*3, height/8, height/2);
77 for (int x=0; x<width; x++) {
78 dirtH = 4+rand.nextInt(2);
79 for (int y = height- hMap[x]; y<height; y++) {
80 if (y==height- hMap[x]) {
81 switch (bMap[x]) {
82 case 0:
83 foreMap[x][y] = 2;
84 backMap[x][y] = 2;
85 break;
86 case 1:
87 foreMap[x][y] = 10;
88 backMap[x][y] = 10;
89 break;
90 }
91 } else if (y<height-hMap[x]+dirtH) {
92 switch (bMap[x]) {
93 case 0:
94 foreMap[x][y] = 3;
95 backMap[x][y] = 3;
96 break;
97 case 1:
98 foreMap[x][y] = 10;
99 backMap[x][y] = 10;
100 break;
102 } else if (bMap[x]==1 && y<height-hMap[x]+dirtH+3) {
103 foreMap[x][y] = 21;
104 backMap[x][y] = 21;
105 } else if (y<height-1){
106 foreMap[x][y] = 1;
107 backMap[x][y] = 1;
108 } else {
109 if (bMap[x]==0) {
110 foreMap[x][y] = 7;
111 backMap[x][y] = 7;
115 for (int y = height-60; y<height-1; y++) {
116 if (foreMap[x][y]==0 && bMap[x]!=1){
117 foreMap[x][y] = 8;
118 backMap[x][y] = 8;
119 if (y==height-hMap[x]-1) {
120 foreMap[x][y+1] = 3;
124 if (x>2 && x<width-2 && rand.nextInt(100)<5){
125 if (foreMap[x][height-hMap[x]-1]==0 && foreMap[x][height-hMap[x]]==2) {
126 genOak(x,height-hMap[x]-1);
128 if (foreMap[x][height-hMap[x]-1]==0 && foreMap[x][height-hMap[x]]==10) {
129 genCactus(x,height-hMap[x]-1);
135 static int[][] getForeMap() {
136 return foreMap;
139 static int[][] getBackMap() {
140 return backMap;
143 static void clear() {
144 foreMap = null;
145 backMap = null;