-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaillett_terrain.py
264 lines (213 loc) · 8 KB
/
maillett_terrain.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
# maillett_terrain.py
# *mostly not my code, this file was provided by professor
# I added lines 192-195 and changed 204-207 to allow for a green gradient
# instead of a gray one. Modifications can be made to the RGB values
# for different colors.
import tkinter as tk
import random
def d2TerrainGen(terrain, tl, br, noise):
if br[0]-tl[0]<=1 and br[1]-tl[1]<=1:
return
elif br[0]-tl[0]<=1 and br[1]-tl[1] > 1: # Y only
mid_y = (tl[1]+br[1])//2
mid_x = tl[0]
if terrain[tl[0]][mid_y]==0:
val = (
terrain[tl[0]][tl[1]] +
terrain[tl[0]][br[1]]
) / 2 + random.uniform(-noise/2,noise/2)
terrain[tl[0]][mid_y] = val
if terrain[br[0]][mid_y] == 0:
val = (
terrain[br[0]][tl[1]] +
terrain[br[0]][br[1]]
) / 2 + random.uniform(-noise/2,noise/2)
terrain[br[0]][mid_y] = val
val = (
terrain[tl[0]][tl[1]] +
terrain[tl[0]][br[1]] +
terrain[br[0]][tl[1]] +
terrain[br[0]][br[1]]
) / 4 + random.uniform(-noise/2,noise/2)
terrain[mid_x][mid_y] = val
# Recursive calls
d2TerrainGen(terrain, tl, (mid_x,mid_y), noise/2)
d2TerrainGen(terrain, (mid_x,mid_y), br, noise/2)
elif br[0]-tl[0] > 1 and br[1]-tl[1]<=1: # x only
mid_x = (tl[0]+br[0])//2
mid_y = tl[1]
if terrain[mid_x][tl[1]] == 0:
val = (
terrain[tl[0]][tl[1]] +
terrain[br[0]][tl[1]]
) / 2 + random.uniform(-noise/2,noise/2)
terrain[mid_x][tl[1]] = val
if terrain[mid_x][br[1]] == 0:
val = (
terrain[tl[0]][br[1]] +
terrain[br[0]][br[1]]
) / 2 + random.uniform(-noise/2,noise/2)
terrain[mid_x][br[1]] = val
val = (
terrain[tl[0]][tl[1]] +
terrain[tl[0]][br[1]] +
terrain[br[0]][tl[1]] +
terrain[br[0]][br[1]]
) / 4 + random.uniform(-noise/2,noise/2)
terrain[mid_x][mid_y] = val
# Recursive calls
d2TerrainGen(terrain, tl, (mid_x,mid_y), noise/2)
d2TerrainGen(terrain, (mid_x,mid_y), br, noise/2)
else: # 4 case
mid_x = (tl[0]+br[0])//2
mid_y = (tl[1]+br[1])//2
if terrain[tl[0]][mid_y]==0:
val = (
terrain[tl[0]][tl[1]] +
terrain[tl[0]][br[1]]
) / 2 + random.uniform(-noise/2,noise/2)
terrain[tl[0]][mid_y] = val
if terrain[br[0]][mid_y] == 0:
val = (
terrain[br[0]][tl[1]] +
terrain[br[0]][br[1]]
) / 2 + random.uniform(-noise/2,noise/2)
terrain[br[0]][mid_y] = val
if terrain[mid_x][tl[1]] == 0:
val = (
terrain[tl[0]][tl[1]] +
terrain[br[0]][tl[1]]
) / 2 + random.uniform(-noise/2,noise/2)
terrain[mid_x][tl[1]] = val
if terrain[mid_x][br[1]] == 0:
val = (
terrain[tl[0]][br[1]] +
terrain[br[0]][br[1]]
) / 2 + random.uniform(-noise/2,noise/2)
terrain[mid_x][br[1]] = val
val = (
terrain[tl[0]][tl[1]] +
terrain[tl[0]][br[1]] +
terrain[br[0]][tl[1]] +
terrain[br[0]][br[1]]
) / 4 + random.uniform(-noise/2,noise/2)
terrain[mid_x][mid_y] = val
# Recursive calls
d2TerrainGen(terrain, tl, (mid_x,mid_y), noise/2)
d2TerrainGen(terrain, (mid_x, tl[1]), (br[0], mid_y), noise/2)
d2TerrainGen(terrain, (tl[0], mid_y), (mid_x, br[1]), noise/2)
d2TerrainGen(terrain, (mid_x, mid_y), br, noise/2)
def GetElevationMap(map_size, seed=None):
random.seed(seed)
# Ma constants
noise = 1024.0
size=map_size
terrain = []
for i in range(size):
inner = []
for j in range(size):
inner.append(0.0)
terrain.append(inner)
# Init corners with random vals
terrain[0][0] = random.uniform(-noise,noise)
terrain[0][size-1] = random.uniform(-noise,noise)
terrain[size-1][0] = random.uniform(-noise,noise)
terrain[size-1][size-1] = random.uniform(-noise,noise)
# RUN & PLOT
d2TerrainGen(terrain, (0,0), (size-1,size-1), noise)
max_val = -100000
min_val = 100000
for x in terrain:
for y in x:
if y > max_val:
max_val = y
elif y < min_val:
min_val = y
min_val = abs(min_val)
max_val += min_val
# Squash values to (0,1)
for x in range(len(terrain)):
for y in range(len(terrain[x])):
terrain[x][y] = (terrain[x][y]+min_val) / (max_val)
# Set the random seed back to None.
random.seed(None)
return terrain
def DisplayTerrain(terrain):
scale = 8
size = len(terrain)
colors = []
for i in range(1,99,1):
colors.append('gray'+str(i))
root = tk.Tk()
canvas = tk.Canvas(root, bg='white', height=size*scale, width=size*scale)
for x in range(len(terrain)):
for y in range(len(terrain[x])):
_x = x*scale
_y = y*scale
try:
if int(terrain[x][y]*(len(colors)-1)) < 0:
print(terrain[x][y])
c = colors[int(terrain[x][y]*(len(colors)-1))]
canvas.create_rectangle(_x,_y,_x+scale,_y+scale,fill=c)
except IndexError:
print(terrain[x][y]*len(colors))
canvas.pack()
root.mainloop()
def from_rgb(rgb): # translates rgb so that tkinter can accept
rgb = tuple(rgb)
return "#%02x%02x%02x" % rgb # I found this on stackoverflow: https://tinyurl.com/z475z7t4
def DisplayTerrainWithOverlays(elevation_map, *args):
if len(args)%2 != 0:
print("For every overlay, you must provide a color.")
scale = 8
size = len(elevation_map)
colors = []
rgb = [0, 0, 0]
for i in range(1,99,1):
colors.append(from_rgb(rgb))
rgb[1] += 2
root = tk.Tk()
frame=tk.Frame(root, width=1000, height=1000)
canvas = tk.Canvas(
frame,
bg='white',
height=1000,
width=1000,
scrollregion=(0,0,size*scale,size*scale)
)
xsbar = tk.Scrollbar(frame, orient=tk.HORIZONTAL)
ysbar = tk.Scrollbar(frame, orient=tk.VERTICAL)
xsbar.config(command=canvas.xview)
ysbar.config(command=canvas.yview)
canvas.config(xscrollcommand=xsbar.set,yscrollcommand=ysbar.set)
for x in range(len(elevation_map)):
for y in range(len(elevation_map[x])):
_x = x*scale
_y = y*scale
try:
if int(elevation_map[x][y]*(len(colors)-1)) < 0:
print(elevation_map[x][y])
c = colors[int(elevation_map[x][y]*(len(colors)-1))]
canvas.create_rectangle(_x,_y,_x+scale,_y+scale,fill=c)
except IndexError:
print(elevation_map[x][y]*len(colors))
for i in range(0,len(args),2):
for x in range(len(args[i])):
for y in range(len(args[i][x])):
_x = x*scale
_y = y*scale
if args[i][x][y]==1:
canvas.create_rectangle(_x,_y,_x+scale,_y+scale,fill=args[i+1])
frame.pack(expand=True, fill=tk.BOTH)
xsbar.pack(side=tk.BOTTOM, fill=tk.X)
ysbar.pack(side=tk.RIGHT, fill=tk.Y)
canvas.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)
root.mainloop()
def GenerateEmptyOverlay(map_size):
overlay = []
for i in range(map_size):
inner = []
for j in range(map_size):
inner.append(0)
overlay.append(inner)
return overlay