-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateTerrain.java
222 lines (175 loc) · 5.43 KB
/
GenerateTerrain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import java.util.Random;
public class GenerateTerrain {
/*
* Amount of smoothing done by algorithm. Larger number = less smoothing.
*/
static final int TEMP_DROP = 4, VEIN_SIZE = 20, BLOCK_SIZE = 4,
TEMP_INCREASE = 1, TEMP_RANGE = 12, MAX_VEINS = 60,
HEIGHT_RANGE = 12, STEEP_AMT = 4;
final Game game;
Random r = new Random();
public GenerateTerrain(Game g) {
game = g;
}
public void generate() {
for (int i = 0; i < Game.GAME_SIZE; i++) {
for (int i1 = 0; i1 < Game.GAME_SIZE; i1++) {
game.terrainMap[i][i1] = new Terrain(game, i, i1, 0, 0);
}
}
for (int i = 0; i < r.nextInt(MAX_VEINS); i++) {
int temp = r.nextInt(TEMP_RANGE), addX, addY;
if (r.nextBoolean()) {
addX = BLOCK_SIZE;
} else {
addX = -BLOCK_SIZE;
}
if (r.nextBoolean()) {
addY = BLOCK_SIZE;
} else {
addY = -BLOCK_SIZE;
}
createVein(r.nextInt(Game.GAME_SIZE), r.nextInt(Game.GAME_SIZE),
addX, addY, temp, r.nextInt(VEIN_SIZE - 1) + 1,
r.nextBoolean());
}
for (int i = 0; i < r.nextInt(MAX_VEINS); i++) {
int temp = -r.nextInt(TEMP_RANGE), addX, addY;
if (r.nextBoolean()) {
addX = BLOCK_SIZE;
} else {
addX = -BLOCK_SIZE;
}
if (r.nextBoolean()) {
addY = BLOCK_SIZE;
} else {
addY = -BLOCK_SIZE;
}
createVein(r.nextInt(Game.GAME_SIZE), r.nextInt(Game.GAME_SIZE),
addX, addY, temp, r.nextInt(VEIN_SIZE - 1) + 1,
r.nextBoolean());
}
for (int i = 0; i < r.nextInt(MAX_VEINS); i++) {
int height = r.nextInt(HEIGHT_RANGE * 2) - HEIGHT_RANGE;
createAlt(r.nextInt(Game.GAME_SIZE), r.nextInt(Game.GAME_SIZE),
height, r.nextInt(STEEP_AMT), r.nextInt(VEIN_SIZE - 1) + 1,
new boolean[Game.GAME_SIZE][Game.GAME_SIZE]);
}
}
/*
* createVein creates a "vein" of temperature in a random location on the
* map, automatically smoothing in the process. x and y are the starting
* point for the vein. temp is the temperature of the vein. kill is an
* accumulator for recursion. Expects 0 < kill increase decides whether temp
* should increase as vein progresses.
*/
protected void createVein(int x, int y, int addX, int addY, int temp,
int kill, boolean increase) {
if (Game.validPos(x, y) && kill > 1) {
if ((temp > 0 && game.terrainMap[x][y].temp >= 0)
|| (temp < 0 && game.terrainMap[x][y].temp <= 0)) {
makeTempBlock(x, y, temp);
smoothTerrain(x, y);
}
if (increase && r.nextInt(20) == 0) {
if (temp + TEMP_INCREASE <= TEMP_RANGE)
temp += TEMP_INCREASE;
else
increase = false;
}
if (!increase && r.nextInt(20) == 0) {
if (temp - TEMP_INCREASE >= -TEMP_RANGE)
temp -= TEMP_INCREASE;
else
increase = true;
}
if (r.nextInt(20) == 0) {
addX *= -1;
}
if (r.nextInt(20) == 0) {
addY *= -1;
}
if (r.nextInt(kill) != 0) {
/*
* int i1 = r.nextInt(4);
*
* switch (i1) { case 0: createVein(x + BLOCK_SIZE, y, temp,
* kill - 1, increase); break; case 1: createVein(x -
* BLOCK_SIZE, y, temp, kill - 1, increase); break; case 2:
* createVein(x, y - BLOCK_SIZE, temp, kill - 1, increase);
* break; case 3: createVein(x, y + BLOCK_SIZE, temp, kill - 1,
* increase); break; }
*/
createVein(x + addX, y + addY, addX, addY, temp, kill - 1, increase);
createVein(x, y + addY, addX, addY, temp, kill - 1, increase);
}
}
}
protected void makeTempBlock(int x, int y, int temp) {
for (int i = -BLOCK_SIZE; i < BLOCK_SIZE; i++) {
for (int i1 = -BLOCK_SIZE; i1 < BLOCK_SIZE; i1++) {
int newX = x + i, newY = y + i1;
if (Game.validPos(newX, newY)) {
game.terrainMap[newX][newY].setTemperature(temp);
}
}
}
}
protected void smoothTerrain(int x, int y) {
int temperature = game.terrainMap[x][y].temp;
boolean warm;
if (temperature > 0) {
warm = true;
} else {
warm = false;
}
for (int i = -BLOCK_SIZE; i <= BLOCK_SIZE; i+= BLOCK_SIZE) {
for (int i1 = -BLOCK_SIZE; i1 <= BLOCK_SIZE; i1+= BLOCK_SIZE) {
int posX = x + i, posY = y + i1;
if (Game.validPos(posX, posY)) {
if (warm
&& game.terrainMap[posX][posY].temp < temperature
- TEMP_DROP) {
makeTempBlock(posX, posY, temperature - TEMP_DROP);
smoothTerrain(posX, posY);
} else if (!warm
&& game.terrainMap[posX][posY].temp > temperature
+ TEMP_DROP) {
game.terrainMap[posX][posY].setTemperature(temperature
+ TEMP_DROP);
makeTempBlock(posX, posY, temperature + TEMP_DROP);
}
}
}
}
}
protected void createAlt(int x, int y, int height, int steepness, int kill,
boolean visited[][]) {
if (Game.validPos(x, y) && kill > 1 && !visited[x][y]) {
kill--;
visited[x][y] = true;
makeAltBlock(x, y, height);
if (Math.abs(height - steepness) < steepness)
kill = 1;
if (height > 0) {
height -= steepness;
} else {
height += steepness;
}
createAlt(x + BLOCK_SIZE, y, height, steepness, kill - 1, visited);
createAlt(x - BLOCK_SIZE, y, height, steepness, kill - 1, visited);
createAlt(x, y - BLOCK_SIZE, height, steepness, kill - 1, visited);
createAlt(x, y + BLOCK_SIZE, height, steepness, kill - 1, visited);
}
}
protected void makeAltBlock(int x, int y, int alt) {
for (int i = -BLOCK_SIZE; i < BLOCK_SIZE; i++) {
for (int i1 = -BLOCK_SIZE; i1 < BLOCK_SIZE; i1++) {
int newX = x + i, newY = y + i1;
if (Game.validPos(newX, newY)) {
game.terrainMap[newX][newY].setAltitude(alt);
}
}
}
}
}