DEADSOFTWARE

Add logic to some blocks and fluids
[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 return res;
43 }
45 private static void genCactus(int x, int y) {
46 foreMap[x][y] = 59;
47 foreMap[x][y-1] = 59;
48 foreMap[x][y-2] = 59;
49 }
51 private static void genOak(int x, int y) {
52 backMap[x][y] = 15;
53 backMap[x][y-1] = 15;
54 backMap[x][y-2] = 15;
55 backMap[x][y-3] = 15;
56 backMap[x][y-4] = 16;
57 backMap[x][y-5] = 16;
58 backMap[x-1][y-3] = 16;
59 backMap[x-1][y-4] = 16;
60 backMap[x+1][y-3] = 16;
61 backMap[x+1][y-4] = 16;
62 foreMap[x][y-3] = 16;
63 foreMap[x][y-4] = 16;
64 foreMap[x][y-5] = 16;
65 foreMap[x-1][y-3] = 16;
66 foreMap[x-1][y-4] = 16;
67 foreMap[x+1][y-3] = 16;
68 foreMap[x+1][y-4] = 16;
69 }
71 static void genWorld(int width, int height) {
72 genWorld(width, height, TimeUtils.millis());
73 }
75 static void genWorld(int width, int height, long worldseed) {
76 int dirtH;
77 seed = worldseed;
78 rand = new RandomXS128(seed);
79 foreMap = new int[width][height];
80 backMap = new int[width][height];
81 hMap = genLandscape(width, height/4, height/8, height/2);
82 for (int x=0; x<width; x++) {
83 dirtH = 4+rand.nextInt(2);
84 for (int y = height- hMap[x]; y<height; y++) {
85 if (y==height- hMap[x]) {
86 switch (bMap[x]) {
87 case 0:
88 foreMap[x][y] = 2;
89 backMap[x][y] = 2;
90 break;
91 case 1:
92 foreMap[x][y] = 10;
93 backMap[x][y] = 10;
94 break;
95 }
96 } else if (y<height-hMap[x]+dirtH) {
97 switch (bMap[x]) {
98 case 0:
99 foreMap[x][y] = 3;
100 backMap[x][y] = 3;
101 break;
102 case 1:
103 foreMap[x][y] = 10;
104 backMap[x][y] = 10;
105 break;
107 } else if (bMap[x]==1 && y<height-hMap[x]+dirtH+3) {
108 foreMap[x][y] = 21;
109 backMap[x][y] = 21;
110 } else if (y<height-1){
111 foreMap[x][y] = 1;
112 backMap[x][y] = 1;
113 } else {
114 foreMap[x][y] = 7;
115 backMap[x][y] = 7;
118 for (int y = height-60; y<height-1; y++) {
119 if (foreMap[x][y]==0 && bMap[x]!=1){
120 foreMap[x][y] = 8;
121 if (bMap[x] == 0) {
122 if (y==height-60) {
123 backMap[x][y] = 2;
124 } else {
125 backMap[x][y] = 3;
128 if (y==height-hMap[x]-1) {
129 foreMap[x][y+1] = 3;
130 backMap[x][y+1] = 3;
134 if (x>2 && x<width-2 && rand.nextInt(100)<5){
135 if (foreMap[x][height-hMap[x]-1]==0 && foreMap[x][height-hMap[x]]==2) {
136 genOak(x,height-hMap[x]-1);
138 if (foreMap[x][height-hMap[x]-1]==0 && foreMap[x][height-hMap[x]]==10) {
139 genCactus(x,height-hMap[x]-1);
145 static int[][] getForeMap() {
146 return foreMap;
149 static int[][] getBackMap() {
150 return backMap;
153 static void clear() {
154 foreMap = null;
155 backMap = null;
156 hMap = null;
157 bMap = null;