-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTerrainMap.py
357 lines (312 loc) · 14.3 KB
/
TerrainMap.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
__author__ = 'Ian'
from PIL import Image
from MapPixel import MapPixel
import random
import math
from Queue import Queue
import Generators
import time
class TerrainMap:
"""A map, duh"""
MAX_ITERATIONS = 10000
WIND_DIRECTIONS = [0, 180, 90, 270, 90, 0, 180]
def __init__(self, height_array, sea_level):
self.heightArray = height_array
self.resolution = len(height_array)
self.seaLevel = sea_level
self.mapArray = list()
self.fill_map_array()
print "Map created"
self.print_map_info()
def in_bounds(self, id):
(x, y) = id
return 0 <= x < self.resolution and 0 <= y < self.resolution
def neighbors(self, id):
(x, y) = id
# results = [(x, y+1), (x-1, y), (x+1, y), (x, y-1)]
results = [(x-1, y+1), (x, y+1), (x+1, y+1), (x-1, y), (x+1, y), (x-1, y-1), (x, y-1), (x+1, y-1)]
if (x + y) % 2 == 0: results.reverse()
results = filter(self.in_bounds, results)
return results
def cost(self, a, b):
heightA = self.mapArray[a[0]][a[1]].height
heightB = self.mapArray[b[0]][b[1]].height
return (heightB - heightA)
def generate_rivers(self, start_height, frequency):
print "Generating rivers..."
start_time = time.time()
river_count = 0
for x in range(len(self.mapArray)):
for y in range(len(self.mapArray)):
if self.mapArray[x][y].height >= start_height:
if random.random() <= frequency:
river_count += 1
came_from, goal = self.find_river_path((x, y))
path = self.reconstruct_path(came_from, (x, y), goal)
for location in path:
self.mapArray[location[0]][location[1]].isWater = True
print "River generation completed. Elapsed time: " + str(time.time() - start_time)
print str(river_count) + " rivers created."
def calculate_wind_direction(self, bands=3, distortion=10):
print "Calculating wind directions..."
start_time = time.time()
randomOffset = (random.randint(-500, 500), random.randint(-500, 500))
distortionMap = Generators.generateNoise(self.resolution, randomOffset, 5, 128.0)
distortedValues = list()
startIndex = random.randint(0, len(self.WIND_DIRECTIONS)-bands)
start = self.WIND_DIRECTIONS[startIndex]
spacing = self.resolution / (bands - 1)
bandList = list()
nearestBand = 0
val = 0
for b in range(bands):
bandList.append((b * spacing, self.WIND_DIRECTIONS[startIndex + b]))
for x in range(len(self.mapArray)):
for z in range(bands):
if x >= bandList[z][0]:
nearestBand = z
distanceBetweenBands = bandList[nearestBand+1][0] - bandList[nearestBand][0]
distanceFromBand = x - bandList[nearestBand][0]
percent = float(distanceFromBand) / float(distanceBetweenBands)
valueDifference = bandList[nearestBand+1][1] - bandList[nearestBand][1]
val = int(valueDifference * percent) + bandList[nearestBand][1]
if val < 0:
val += 360
# print "Between " + str(bandList[nearestBand][1]) + " and " + str(bandList[nearestBand + 1][1])
# print str(val)
for y in range(len(self.mapArray)):
self.mapArray[y][x].windDirection = val
for x in range(len(self.mapArray)):
distortedValues.append(list())
for y in range(len(self.mapArray)):
remappedValue = (distortionMap[x][y] * 2.0) - 1.0
distortedOffset = int(remappedValue * distortion)
clampedOffset = max(0, min(self.resolution-1, y+distortedOffset))
distortedValues[x].append(self.mapArray[x][clampedOffset].windDirection)
for x in range(len(self.mapArray)):
for y in range(len(self.mapArray)):
self.mapArray[x][y].windDirection = distortedValues[x][y]
for x in range(len(self.mapArray)):
for y in range(len(self.mapArray)):
val = self.mapArray[x][y].windDirection
clampedVal = val
if (val % 45) < 5:
clampedVal = val - (val % 45)
elif (val % 45) > 40:
clampedVal = val + (45 - (val % 45))
elif random.randint(0, 45) > (val % 45):
clampedVal = val - (val % 45)
else:
clampedVal = val + (45 - (val % 45))
self.mapArray[x][y].windDirection = clampedVal
print "Wind directions calculated. Elapsed time: " + str(time.time() - start_time)
def calculate_rainfall_from_wind(self):
print "Calculating rainfall..."
start_time = time.time()
for x in range(len(self.mapArray)):
for y in range(len(self.mapArray)):
if not self.mapArray[x][y].isWater:
val = self.find_distance_to_water((x, y))
self.mapArray[x][y].rainfall = 1.0 / (val / 5.0)
print "Rainfall calculated. Elapsed time: " + str(time.time() - start_time)
def calculate_biomes(self):
print "Calculating biomes..."
start_time = time.time()
for x in range(len(self.mapArray)):
for y in range(len(self.mapArray)):
self.mapArray[x][y].calculate_biome()
print "Biomes calculated. Elapsed time: " + str(time.time() - start_time)
def calculate_temperature(self):
print "Calculating temperature..."
start_time = time.time()
#simple north = cold model (boring)
for x in range(len(self.mapArray)):
for y in range(len(self.mapArray)):
self.mapArray[x][y].temperature = float(y) / float(self.resolution)
self.mapArray[x][y].temperature *= 1 - (self.mapArray[x][y].height * 0.9)
print "Temperature calculated. Elapsed time: " + str(time.time() - start_time)
def calculate_rainfall(self):
print "Calculating rainfall..."
start_time = time.time()
area = 40
amount = 0.04
for x in range(len(self.mapArray)):
for y in range(len(self.mapArray)):
if not self.mapArray[x][y].isWater:
for a in range(x - (area / 2), x + (area / 2)):
if 0 <= a < self.resolution:
for b in range(y - (area / 2), y + (area / 2)):
if 0 <= b < self.resolution:
if self.mapArray[a][b].isWater:
radialAmount = math.pow(math.pow(a - x, 2) + math.pow(b - y, 2), 0.5)
radialAmount = 1.0 / radialAmount
radialAmount *= amount
if self.mapArray[a][b].height <= self.seaLevel:
radialAmount *= 0.02
self.mapArray[x][y].rainfall += radialAmount
for x in range(len(self.mapArray)):
for y in range(len(self.mapArray)):
self.mapArray[x][y].rainfall = min(self.mapArray[x][y].rainfall, 1.0)
print "Rainfall calculated. Elapsed time: " + str(time.time() - start_time)
def fill_map_array(self):
for x in range(len(self.heightArray)):
self.mapArray.append(list())
for y in range(len(self.heightArray[0])):
self.mapArray[x].append(MapPixel((x, y), self.heightArray[x][y], self.seaLevel))
def get_height_map_image(self):
img = Image.new('RGB', (len(self.mapArray), len(self.mapArray)), "black")
pixels = img.load()
for x in range(len(self.mapArray)):
for y in range(len(self.mapArray)):
pixels[x, y] = self.mapArray[x][y].get_greyscale_color()
return img
def get_wind_direction_map_image(self):
img = Image.new('RGB', (len(self.mapArray), len(self.mapArray)), "black")
pixels = img.load()
for x in range(len(self.mapArray)):
for y in range(len(self.mapArray)):
pixels[x, y] = self.mapArray[x][y].get_wind_direction_map_color()
return img
def get_water_map_image(self):
img = Image.new('RGB', (len(self.mapArray), len(self.mapArray)), "black")
pixels = img.load()
for x in range(len(self.mapArray)):
for y in range(len(self.mapArray)):
pixels[x, y] = (0, 0, 0) if self.mapArray[x][y].isWater else (255, 255, 255)
return img
def get_biome_map_image(self):
img = Image.new('RGB', (len(self.mapArray), len(self.mapArray)), "black")
pixels = img.load()
for x in range(len(self.mapArray)):
for y in range(len(self.mapArray)):
pixels[x, y] = self.mapArray[x][y].get_biome_map_color()
return img
def get_rainfall_map_image(self):
img = Image.new('RGB', (len(self.mapArray), len(self.mapArray)), "black")
pixels = img.load()
for x in range(len(self.mapArray)):
for y in range(len(self.mapArray)):
pixels[x, y] = self.mapArray[x][y].get_rainfall_map_color()
return img
def get_temperature_map_image(self):
img = Image.new('RGB', (len(self.mapArray), len(self.mapArray)), "black")
pixels = img.load()
for x in range(len(self.mapArray)):
for y in range(len(self.mapArray)):
pixels[x, y] = self.mapArray[x][y].get_temperature_map_color()
return img
def get_terrain_map_image(self):
img = Image.new('RGB', (len(self.mapArray),len(self.mapArray)), "black")
pixels = img.load()
for x in range(len(self.mapArray)):
for y in range(len(self.mapArray)):
pixels[x, y] = self.mapArray[x][y].get_rgb_color()
return img
def print_map_info(self):
print "**************************************"
print "Input heightmap resolution: " + str(len(self.heightArray)) + "x" + str(len(self.heightArray[0]))
print "Sea level: " + str(self.seaLevel)
print "Map size: " + str(len(self.mapArray)) + "x" + str(len(self.mapArray[0]))
print "**************************************"
def load_height_map(self, path):
self.heightArray = list()
height_map = Image.open(path)
pixels = height_map.load()
for x in range(height_map.size[0]):
self.heightArray.append(list())
for y in range(height_map.size[1]):
self.heightArray[x].append(float(pixels[x, y][0]) / 255.0)
self.resolution = len(self.heightArray)
self.mapArray = list()
self.fill_map_array()
print "Height map loaded."
def load_water_map(self, path):
water_map = Image.open(path)
pixels = water_map.load()
for x in range(self.resolution):
for y in range(self.resolution):
water = True if pixels[x, y][0] < 100 else False
self.mapArray[x][y].isWater = water
print "Water map loaded."
def load_rainfall_map(self, path):
rainfall_map = Image.open(path)
pixels = rainfall_map.load()
for x in range(self.resolution):
for y in range(self.resolution):
self.mapArray[x][y].rainfall = float(pixels[x, y][0]) / 255.0
print "Rainfall map loaded."
def load_temperature_map(self, path):
temperature_map = Image.open(path)
pixels = temperature_map.load()
for x in range(self.resolution):
for y in range(self.resolution):
self.mapArray[x][y].temperature = float(pixels[x, y][0]) / 255.0
print "Temperature map loaded."
def find_distance_to_water(self, start):
(a, b) = start
print str(start)
iterations = 0.0
while iterations < self.MAX_ITERATIONS and not self.mapArray[a][b].isWater:
iterations += 1.0
(a, b) = self.get_wind_source((a, b))
if not self.in_bounds((a, b)):
iterations = 1000.0
break
# print str(iterations)
return iterations
def get_wind_source(self, id):
(x, y) = id
direction = self.mapArray[x][y].windDirection
sourceDirection = (x, y)
if direction == 0 or direction == 360:
sourceDirection = (x, y-1)
elif direction == 45:
sourceDirection = (x+1, y-1)
elif direction == 90:
sourceDirection = (x+1, y)
elif direction == 135:
sourceDirection = (x+1, y+1)
elif direction == 180:
sourceDirection = (x, y+1)
elif direction == 225:
sourceDirection = (x-1, y+1)
elif direction == 270:
sourceDirection = (x-1, y)
else:
sourceDirection = (x-1, y-1)
return sourceDirection
def find_river_path(self, start):
frontier = Queue()
frontier.put(start, 0)
came_from = {}
cost_so_far = {}
came_from[start] = None
cost_so_far[start] = 0
iterations = 0
while iterations < self.MAX_ITERATIONS and not frontier.empty():
current = frontier.get()
self.mapArray[current[0]][current[1]].isWater = True
if self.mapArray[current[0]][current[1]].height <= self.seaLevel:
break
for next in self.neighbors(current):
new_cost = cost_so_far[current] + self.cost(current, next)
if next not in cost_so_far or new_cost < cost_so_far[next]:
#self.mapArray[next[0]][next[1]].isWater = True
cost_so_far[next] = new_cost
priority = new_cost
frontier.put(next, priority)
came_from[next] = current
iterations += 1
return came_from, current
def reconstruct_path(self, came_from, start, goal):
current = goal
path = [current]
iterations = 0
while current != start:
iterations += 1
if iterations >= self.MAX_ITERATIONS:
break
current = came_from[current]
path.append(current)
path.reverse()
return path