-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_terrain.py
244 lines (208 loc) · 6.93 KB
/
gen_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
import pygame
import sys, math
from random import randint, random
pygame.init()
width = 250
height = 250
screen = pygame.display.set_mode((width,height), pygame.HWSURFACE|pygame.DOUBLEBUF)
clock = pygame.time.Clock()
left = False
right = False
up = False
down = False
def interpolate(x0, x1, alpha):
return (1.0 - alpha) * x0 + alpha * x1
#enddef
class Map:
def __init__(self, w, h):
self.w = w
self.h = h
self.bmp = pygame.Surface((w, h)).convert()
self.generate()
#self.generateOld(0,0,w,h,128)
self.addWater()
#enddef
def generate(self):
octaveCount = 6
persistance = 0.6
w = self.w
h = self.h
smooth = []
baseNoise = self.generateBaseNoise(w, h)
for i in xrange(0, octaveCount):
smooth.append(self.generateSmoothNoise(baseNoise, octaveCount-i, w, h))
#endfor
perlinNoise = []
for i in xrange(0, w):
ln = []
for j in xrange(0, h):
ln.append(0.0)
perlinNoise.append(ln)
#endfor
amplitude = 1.0
#blend noise together
for k in xrange(0, octaveCount):
amplitude *= persistance
for i in xrange(0, w):
for j in xrange(0, h):
perlinNoise[i][j] += smooth[k][i][j] * amplitude
#endfor
#endfor
# normalisation
maxVal = 0.0
for col in perlinNoise:
for val in col:
if val > maxVal:
maxVal = val
for i in xrange(0, w):
for j in xrange(0, h):
perlinNoise[i][j] /= maxVal
#endfor
# draw
for i in xrange(0, w):
for j in xrange(0, h):
c = int(255*perlinNoise[i][j])
self.bmp.set_at((i,j), (c,c,c))
#endfor
#enddef
def generateBaseNoise(self, w, h):
baseNoise = []
for i in xrange(0, w):
ln = []
for j in xrange(0, h):
ln.append(random())
baseNoise.append(ln)
#endfor
return baseNoise
#endfor
def generateSmoothNoise(self, baseNoise, k, w, h):
smooth = []
for i in xrange(0, w):
ln = []
for j in xrange(0, h):
ln.append(0.0)
smooth.append(ln)
#endfor
samplePeriod = 1 << k # calculates 2 ^ k
sampleFrequency = 1.0 / samplePeriod
for i in xrange(0, w):
#calculate the horizontal sampling indices
sample_i0 = (i / samplePeriod) * samplePeriod
sample_i1 = (sample_i0 + samplePeriod) % w #wrap around
horizontal_blend = (i - sample_i0) * sampleFrequency
for j in xrange(0, h):
#calculate the vertical sampling indices
sample_j0 = (j / samplePeriod) * samplePeriod
sample_j1 = (sample_j0 + samplePeriod) % h #wrap around
vertical_blend = (j - sample_j0) * sampleFrequency
#blend the top two corners
top = interpolate(baseNoise[int(sample_i0)][int(sample_j0)],\
baseNoise[int(sample_i1)][int(sample_j0)], horizontal_blend)
#blend the bottom two corners
bottom = interpolate(baseNoise[int(sample_i0)][int(sample_j1)],\
baseNoise[int(sample_i1)][int(sample_j1)], horizontal_blend)
#final blend
smooth[i][j] = interpolate(top, bottom, vertical_blend)
#endfor
#endfor
return smooth
# draw
for i in xrange(0, w):
for j in xrange(0, h):
c = int(255*smooth[i][j])
self.bmp.set_at((i,j), (c,c,c))
self.show()
#enddef
def addWater(self, level = 128):
for x in xrange(0,self.w):
for y in xrange(0,self.h):
c = self.bmp.get_at((x,y))
if c[0] < level:
self.bmp.set_at((x,y), (c[0],c[1],250))
#enddef
def addRivers(self):
spillProb = 2
seeds = []
for i in xrange(0,(self.w*self.h)/500):
seeds.append((randint(0,self.w-1), randint(0,self.h-1)))
while len(seeds)>0:# and len(seeds)<20000:
seeds2 = []
for (x,y) in seeds:
#print s
c1 = self.bmp.get_at((x,y))
if c1[2]==255:
continue
self.bmp.set_at((x,y), (c1[0],c1[1],255))
spill = randint(0,10) < spillProb
if x>0:
c2 = self.bmp.get_at((x-1,y))
if c2[2]!=255 and (c2[0]<c1[0] or (c2[0]==c1[0] and spill)):
seeds2.append((x-1,y))
spill = randint(0,10) < spillProb
if y>0:
c2 = self.bmp.get_at((x,y-1))
if c2[2]!=255 and (c2[0]<c1[0] or (c2[0]==c1[0] and spill)):
seeds2.append((x,y-1))
spill = randint(0,10) < spillProb
if x+1<self.w:
c2 = self.bmp.get_at((x+1,y))
if c2[2]!=255 and (c2[0]<c1[0] or (c2[0]==c1[0] and spill)):
seeds2.append((x+1,y))
spill = randint(0,10) < spillProb
if y+1<self.h:
c2 = self.bmp.get_at((x,y+1))
if c2[2]!=255 and (c2[0]<c1[0] or (c2[0]==c1[0] and spill)):
seeds2.append((x,y+1))
#endfor
seeds = seeds2
# dbg
#print len(seeds)
self.show()
#endwhile
#enddef
def show(self):
global screen
self.draw(screen)
pygame.display.flip()
#enddef
def draw(self, screen):
screen.blit(self.bmp, (0,0))
#enddef
#endclass
map = Map(width, height)
tickCnt = 0
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit(0)
if event.type == pygame.KEYDOWN:
key = event.key
if key==27: sys.exit(0) #self.exit = True
if key==273: up = True
if key==274: down = True
if key==276: left = True
if key==275: right = True
# if key==305: self.fire = True
if event.type == pygame.KEYUP:
key = event.key
if key==273: up = False
if key==274: down = False
if key==276: left = False
if key==275: right = False
if up: points[ctrP].my -= 1
if down: points[ctrP].my += 1
if left: points[ctrP].mx -= 1
if right: points[ctrP].mx += 1
#screen.fill((0,0,0))
map.draw(screen)
pygame.display.flip()
clock.tick(30)
tickCnt += 1
if tickCnt > 100:
map.generate()
map.addWater(randint(50,200))
#map.addRivers()
# for p in points:
# p.behave()
tickCnt = 0
#endif
#endwhile