-
Notifications
You must be signed in to change notification settings - Fork 8
/
Generator - Cavetiles.monkey
424 lines (403 loc) · 10.9 KB
/
Generator - Cavetiles.monkey
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
Import mojo
Global sw:Int=640
Global sh:Int=480
Global mapwidth:Int=19
Global mapheight:Int=15
Global smooth1:Int=10
Global minsmooth1:Int=3
Global maxsmooth1:Int=30
Global smooth2:Int=20
Global minsmooth2:Int=3
Global maxsmooth2:Int=30
Global myseed:Int=300
Global tilewidth:Int=32
Global tileheight:Int=32
Global maxsize:Int=tilewidth/2
Global size:Int=tilewidth/2
Class map
Field image:Image[][]
Field imagepixels:Int[]
Field imagemap:Int[][]
Field map1:Int[][]
Field map2:Int[][]
Field map3:Int[][][]
Field mazex:Stack<Int> = New Stack<Int>
Field mazey:Stack<Int> = New Stack<Int>
Method New()
imagemap = New Int[tilewidth][]
For Local i=0 Until tilewidth
imagemap[i] = New Int[tileheight]
Next
map1 = New Int[mapwidth][]
For Local i=0 Until mapwidth
map1[i] = New Int[mapheight]
Next
map2 = New Int[mapwidth/2][]
map3 = New Int[mapwidth/2][][]
For Local i=0 Until mapwidth/2
map2[i] = New Int[mapheight/2]
map3[i] = New Int[mapheight/2][]
For Local z = 0 Until mapheight/2
map3[i][z] = New Int[4]
Next
Next
image = New Image[mapwidth][]
For Local i=0 Until mapwidth
image[i] = New Image[mapheight]
Next
For Local y=0 Until mapheight
For Local x=0 Until mapwidth
image[x][y] = CreateImage(tilewidth,tileheight)
Next
Next
imagepixels = New Int[tilewidth*tileheight]
Seed = myseed
makemaze()
tilesonmap()
'maketile()
'map1[0][0]=1
'finalizeimage(0,0)
End Method
Method tilesonmap()
Local top:Bool=False
Local right:Bool=False
Local bottom:Bool=False
Local left:Bool=False
For Local y=1 Until mapheight-1
For Local x=1 Until mapwidth-1
If map1[x][y] = 1
If map1[x][y-1] = 1 Then top=True Else top=False
If map1[x+1][y] = 1 Then right=True Else right=False
If map1[x][y+1] = 1 Then bottom=True Else bottom=False
If map1[x-1][y] = 1 Then left=True Else left = False
maketile(top,right,bottom,left)
finalizeimage(x,y)
End If
Next
Next
For Local x=1 Until mapwidth-1
top=False
If map1[x][0] = 1
If map1[x+1][0] = 1 Then right = True Else right=False
If map1[x][1] = 1 Then bottom = True Else bottom=False
If map1[x-1][0] = 1 Then left = True Else left=False
maketile(top,right,bottom,left)
finalizeimage(x,0)
End If
bottom=False
If map1[x][mapheight-1] = 1
If map1[x][mapheight-2] = 1 Then top=True Else top=False
If map1[x+1][mapheight-1] = 1 Then right=True Else right =False
If map1[x-1][mapheight-1] = 1 Then left = True Else left=False
maketile(top,right,bottom,left)
finalizeimage(x,mapheight-1)
End If
Next
For Local y=1 Until mapheight-1
left=False
If map1[0][0] = 1
If map1[0][y-1] = 1 Then top = True Else top=False
If map1[1][y] = 1 Then right=True Else right=False
If map1[0][y+1] = 1 Then bottom=True Else bottom=False
maketile(top,right,bottom,left)
finalizeimage(0,y)
End If
right=False
If map1[mapwidth-1][y] = 1
If map1[mapwidth-1][y-1] = 1 Then top=True Else top=False
If map1[mapwidth-1][y+1] = 1 Then bottom = True Else bottom=False
If map1[mapwidth-2][y] = 1 Then left=True Else left = False
maketile(top,right,bottom,left)
finalizeimage(mapwidth-1,y)
End If
Next
If map1[mapwidth-1][mapheight-1] = 1
bottom=False
right=False
If map1[mapwidth-2][mapheight-1] = 1 Then left=True Else left=False
If map1[mapwidth-1][mapheight-2] = 1 Then top=True Else top=False
maketile(top,right,bottom,left)
finalizeimage(mapwidth-1,mapheight-1)
End If
End Method
Method maketile(top:Bool=False,right:Bool=False,bottom:Bool=False,left:Bool=False)
For Local y=0 Until tileheight
For Local x=0 Until tilewidth
imagemap[x][y] = 0
Next
Next
smooth1 = Rnd(minsmooth1,maxsmooth1)
smooth2 = Rnd(minsmooth2,maxsmooth2)
If Rnd()<.8 Then smooth1 = Rnd(minsmooth1,10)
Local ang:Int=0
Local x:Int=tilewidth/2
Local y:Int=tileheight/2
Local d:Int=size-1
Local linex:Stack<Int> = New Stack<Int>
Local liney:Stack<Int> = New Stack<Int>
Local drawme:Stack<Bool> = New Stack<Bool>
Local ex:Bool=False
Local topset:Bool=False
Local rightset:Bool=False
Local bottomset:Bool=False
Local leftset:Bool=False
If right=True Then ang=17
linex.Push(x+Cos(ang)*d)
liney.Push(y+Sin(ang)*d)
drawme.Push(True)
While ang<(360-smooth2)
ang+=smooth2
If bottom=True And bottomset=False
If (ang>70 And ang<120) Then
bottomset=True
ang=90-20
linex.Push(x+Cos(ang)*d)'
liney.Push(y+tileheight/2)
drawme.Push(True)
ang=90+21
linex.Push(x+Cos(ang)*d)'
liney.Push(y+tileheight/2)
drawme.Push(False)
ang+=smooth2
End If
End If
If left=True And leftset=False
If (ang>160-smooth2 And ang<200) Then
leftset=True
ang=180-20
linex.Push(x-tilewidth/2)'
liney.Push(y+Sin(ang)*d)
drawme.Push(True)
ang=180+21
linex.Push(x-tilewidth/2)'
liney.Push(y+Sin(ang)*d)
drawme.Push(False)
ang+=smooth2
End If
End If
If top=True And topset=False
If (ang>250 And ang<290) Then
topset=True
ang=270-20
linex.Push(x+Cos(ang)*d)'
liney.Push(y-tileheight/2)
drawme.Push(True)
ang=270+21
linex.Push(x+Cos(ang)*d)'
liney.Push(y-tileheight/2)
drawme.Push(False)
ang+=smooth2
End If
End If
If right=True And rightset=False
If (ang>340-smooth2) Then
rightset=True
ang=360-20
linex.Push(x+tilewidth/2)'
liney.Push(y+Sin(ang)*d)
drawme.Push(True)
ang=21
linex.Push(x+tilewidth/2)'
liney.Push(y+Sin(ang)*d)
drawme.Push(False)
ang+=smooth2
ang=360
ex=True
End If
End If
If ex=False
d = size-1
Local d2:Int=d+Rnd(-d/smooth1,d/smooth1)
If d2>maxsize Then d2=maxsize
linex.Push(x+Cos(ang)*d2)
liney.Push(y+Sin(ang)*d2)
drawme.Push(True)
'linex.Push(x+Cos(ang)*d)'
'liney.Push(y+Sin(ang)*d)
End If
Wend
linex.Push(linex.Get(0))
liney.Push(liney.Get(0))
drawme.Push(True)
For Local i=1 Until linex.Length
Local x1:Int=linex.Get(i-1)
Local y1:Int=liney.Get(i-1)
Local x2:Int=linex.Get(i)
Local y2:Int=liney.Get(i)
If drawme.Get(i) = True Then
'If drawme.Get(i) = True
line x1,y1,x2,y2
'End if
End If
Next
'
floodimagemap(2) 'walkable tiles is no 2
End Method
Method floodimagemap(fillval:Int)
Local fx:Stack<Int> = New Stack<Int>
Local fy:Stack<Int> = New Stack<Int>
Local mx:Int[] = [0,1,0,-1]
Local my:Int[] = [-1,0,1,0]
fx.Push(tilewidth/2)
fy.Push(tileheight/2)
While fx.Length > 0
Local x1:Int=fx.Top
Local y1:Int=fy.Top
fx.Pop
fy.Pop
For Local i=0 Until 4
Local x2:Int=x1+mx[i]
Local y2:Int=y1+my[i]
If x2>=0 And x2<tilewidth And y2>=0 And y2<tileheight
If imagemap[x2][y2] = 0
imagemap[x2][y2] = fillval
fx.Insert(0,x2)
fy.Insert(0,y2)
End If
End If
Next
Wend
End Method
Method finalizeimage(tx:Int,ty:Int)
For Local i=0 Until tilewidth*tileheight
imagepixels[i] = 0
Next
Local cnt:Int=0
For Local y=0 Until tileheight
For Local x=0 Until tilewidth
If imagemap[x][y] = 1
imagepixels[cnt] = argb(255,255,255)
End If
If imagemap[x][y] = 2
imagepixels[cnt] = argb(55,55,55)
End If
cnt+=1
Next
Next
image[tx][ty].WritePixels(imagepixels, 0, 0, tilewidth, tileheight, 0)
End Method
Method makemaze()
Local ax:Int[] = [0,1,0,-1]
Local ay:Int[] = [-1,0,1,0]
mazex.Push(Rnd(mapwidth/2))
mazey.Push(Rnd(mapheight/2))
While mazex.IsEmpty = False
Local x:Int=mazex.Top
Local y:Int=mazey.Top
Local d:Int[] = New Int[4]
Local deadend:Bool=True
For Local i:=0 Until ax.Length
Local x2:Int=x+ax[i]
Local y2:Int=y+ay[i]
If x2>=0 And x2<mapwidth/2 And y2>=0 And y2<mapheight/2
If map2[x2][y2] = 0
d[i] = 1
deadend=False
End If
End If
Next
If deadend = False
Local eloop:Bool=False
While eloop = False
Local r:Int=Rnd(0,4)
If d[r] = 1 Then
eloop = True
Local nx:Int=x+ax[r]
Local ny:Int=y+ay[r]
map3[x][y][r] = 1
mazex.Push(nx)
mazey.Push(ny)
map2[x][y] = 1
map2[nx][ny] = 1
End If
Wend
Else ' if nothing happened than backtrace
mazex.Pop()
mazey.Pop()
End If
Wend
' convert the map into a tilemap
For Local y:=0 Until mapheight/2
For Local x:=0 Until mapwidth/2
Local x2:Int=x*2
Local y2:Int=y*2
If map2[x][y] = 1 Then map1[x2+1][y2+1] = 1
If map3[x][y][0] = 1 Then map1[x2+1][y2] = 1
If map3[x][y][1] = 1 Then map1[x2+2][y2+1] = 1
If map3[x][y][2] = 1 Then map1[x2+1][y2+2] = 1
If map3[x][y][3] = 1 Then map1[x2][y2+1] = 1
Next
Next
End Method
Method line:Void(x1:Int,y1:Int,x2:Int,y2:Int)
Local dx:Int, dy:Int, sx:Int, sy:Int, e:Int
dx = Abs(x2 - x1)
sx = -1
If x1 < x2 Then sx = 1
dy = Abs(y2 - y1)
sy = -1
If y1 < y2 Then sy = 1
If dx < dy Then
e = dx / 2
Else
e = dy / 2
End If
Local exitloop:Bool=False
While exitloop = False
If x1>=0 And x1<tilewidth And y1>=0 And y1<tileheight
imagemap[x1][y1] = 1
End If
If x1 = x2
If y1 = y2
exitloop = True
End If
End If
If dx > dy Then
x1 += sx ; e -= dy
If e < 0 Then e += dx ; y1 += sy
Else
y1 += sy ; e -= dx
If e < 0 Then e += dy ; x1 += sx
Endif
Wend
End Method
Function argb:Int(r:Int, g:Int, b:Int ,alpha:Int=255)
Return (alpha Shl 24) | (r Shl 16) | (g Shl 8) | b
End Function
Method draw()
SetColor 255,255,255
For Local y=0 Until mapheight
For Local x=0 Until mapwidth
If map1[x][y] = 1
DrawImage image[x][y],x*tilewidth,y*tileheight
End If
Next
Next
End Method
End Class
Global mymap:map
Class MyGame Extends App
Field cnt:Int=0
Method OnCreate()
SetUpdateRate(60)
myseed=1
mymap = New map
End Method
Method OnUpdate()
cnt+=1
If MouseDown(MOUSE_LEFT) Or cnt>120
cnt=0
myseed = Millisecs()
mymap = New map
End If
End Method
Method OnRender()
Cls 0,0,0
mymap.draw
SetColor 255,255,255
DrawText "Press mouse to draw new map or wait",0,0
End Method
End Class
Function Main()
New MyGame()
End Function