-
Notifications
You must be signed in to change notification settings - Fork 0
/
terragen.py
246 lines (167 loc) · 5.77 KB
/
terragen.py
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# creates randomly generated map
from __future__ import division
from random import random
from Perlin import PerlinGrid
class Biomes:
DeepOcean = "A"
Ocean = 4
Sand = 3
Grass = 0
Trees = 2
Dirt = 1
Hill = 5
Gap = 6
Mountain = 7
Snow = 8
Lava = 9
BiomeGrid = [
#low #high
[Biomes.Trees, Biomes.Trees, Biomes.Trees, Biomes.Dirt, Biomes.Gap, Biomes.Gap, Biomes.Snow, Biomes.Snow ], #wet
[Biomes.Trees, Biomes.Trees, Biomes.Trees, Biomes.Dirt, Biomes.Hill, Biomes.Hill, Biomes.Snow, Biomes.Snow ],
[Biomes.Grass, Biomes.Grass, Biomes.Grass, Biomes.Dirt, Biomes.Hill, Biomes.Hill, Biomes.Snow, Biomes.Snow ],
[Biomes.Grass, Biomes.Grass, Biomes.Grass, Biomes.Dirt, Biomes.Hill, Biomes.Hill, Biomes.Mountain, Biomes.Mountain ],
[Biomes.Grass, Biomes.Grass, Biomes.Grass, Biomes.Dirt, Biomes.Hill, Biomes.Hill, Biomes.Mountain, Biomes.Mountain ],
[Biomes.Grass, Biomes.Grass, Biomes.Grass, Biomes.Dirt, Biomes.Dirt, Biomes.Hill, Biomes.Mountain, Biomes.Mountain ],
[Biomes.Sand, Biomes.Sand, Biomes.Sand, Biomes.Sand, Biomes.Dirt, Biomes.Hill, Biomes.Mountain, Biomes.Mountain ],
[Biomes.Sand, Biomes.Sand, Biomes.Sand, Biomes.Sand, Biomes.Dirt, Biomes.Hill, Biomes.Mountain, Biomes.Lava ] #dry
]
class Tile():
global BiomeGrid
def __init__(self, x, y):
self.x = x
self.y = y
self.height = 0
self.wetness = 0
self.biome = Biomes.Ocean
def CalcBiome(tile, waterLevel):
if tile.height <= waterLevel/2:
tile.biome = Biomes.DeepOcean
return
if tile.height <= waterLevel:
tile.biome = Biomes.Ocean
return
scaledHeight = (tile.height - waterLevel) / (1.0001-waterLevel)
heightGroup = min(int(scaledHeight*8), 7)
wetGroup = min(int(tile.wetness*8), 7)
#print tile.height,scaledHeight,heightGroup
tile.biome = BiomeGrid[wetGroup][heightGroup]
def Neighbors(tile, map):
for x,y in ((-1,-1),(0,-1),(1,-1),(-1,0),(1,0),(-1,1),(0,1),(1,1)):
nx = tile.x + x
ny = tile.y + y
if nx >= 0 and nx < map.width and ny >= 0 and ny < map.height:
yield map.grid[ny][nx]
class Map():
def __init__(self, width, height, waterApproach):
self.width = width
self.height = height
self.InitGrid(width, height)
# GREP DEBUG
#self.grid[140][10].height = 1
#self.grid[140][10].wetness = 0
self.Normalize()
self.Deform()
self.Normalize()
self.NormalizeWetness()
self.waterLevel = self.CalcWaterLevel(waterApproach)
self.ClassifyBiomes(self.waterLevel)
self.Enforce(Biomes.Lava, [], Biomes.Mountain)
self.Enforce(Biomes.Snow, [], Biomes.Mountain)
self.Enforce(Biomes.Mountain, [Biomes.Snow, Biomes.Lava], Biomes.Hill)
self.CullEdge(Biomes.Gap, Biomes.Hill)
self.Enforce(Biomes.Hill, [Biomes.Mountain, Biomes.Gap], Biomes.Dirt)
self.Enforce(Biomes.Dirt, [Biomes.Hill], Biomes.Grass)
self.CullEdge(Biomes.Trees, Biomes.Grass)
self.Enforce(Biomes.Grass, [Biomes.Dirt,Biomes.Trees], Biomes.Sand)
self.CullEdge(Biomes.DeepOcean, Biomes.Ocean)
self.Enforce(Biomes.Sand, [Biomes.Grass, Biomes.Ocean, Biomes.DeepOcean], Biomes.Sand)
#self.Enforce(Biomes.Ocean, [Biomes.DeepOcean,Biomes.Sand], Biomes.Sand)
#self.CullEdge(Biomes.Snow, Biomes.Mountain)
def InitGrid(self, width, height):
#perlinSpaceL = PerlinGrid(width, height, width/53, height/53)
#perlinSpace = PerlinGrid(width, height, width/13, height/13)
perlinSpaceH = PerlinGrid(width, height, width/7, height/7)
perlinSpaceRain = PerlinGrid(width, height, width/21, height/15)
self.grid = []
for y in range(height):
row = []
for x in range(width):
tile = Tile(x, y)
#noise = perlinSpaceL.get(x,y)/2 + perlinSpace.get(x,y)/4 + perlinSpaceH.get(x,y)/8
noise = perlinSpaceH.get(x,y)
tile.height = noise
tile.wetness = (perlinSpaceRain.get(x,y)+1)/2
# GREP DEBUG
#tile.height = 0
#tile.wetness = 1
row.append(tile)
self.grid.append(row)
def Normalize(map):
min = 1.0
max = -1.0
for row in map.grid:
for tile in row:
if tile.height < min:
min = tile.height
if tile.height > max:
max = tile.height
scale = 1/(max - min)
for row in map.grid:
for tile in row:
heightIndex = (tile.height - min) * scale
tile.height = heightIndex
def NormalizeWetness(map):
min = 1.0
max = -1.0
for row in map.grid:
for tile in row:
if tile.wetness < min:
min = tile.wetness
if tile.wetness > max:
max = tile.wetness
scale = 1/(max - min)
for row in map.grid:
for tile in row:
tile.wetness = (tile.wetness - min) * scale
def Deform(map):
for row in map.grid:
for tile in row:
falloff = (map.height - tile.y)/map.height
falloff = 1-abs(2*falloff - 1)
h = tile.height
h = h*(falloff+1) + falloff
h = h**2
tile.height = h
def ClassifyBiomes(map, waterLevel):
for row in map.grid:
for tile in row:
tile.CalcBiome(waterLevel)
def CalcWaterLevel(map, approach):
maxLevel = 0
for row in range(map.height - approach, map.height):
for col in range(0, map.width):
tile = map.grid[row][col]
if(tile.height > maxLevel):
maxLevel = tile.height
return maxLevel
def CullEdge(map, centerBiome, edgeBiome):
for row in map.grid:
for tile in row:
if tile.biome != centerBiome:
continue
edge = False
for neighbor in tile.Neighbors(map):
if neighbor.biome not in (centerBiome, edgeBiome):
edge = True
if edge:
tile.biome = edgeBiome
def Enforce(map, checkBiome, legalEdges, changeTo):
legal = [checkBiome]
legal += legalEdges
for row in map.grid:
for tile in row:
if tile.biome != checkBiome:
continue
for neighbor in tile.Neighbors(map):
if neighbor.biome not in legal:
neighbor.biome = changeTo