-
Notifications
You must be signed in to change notification settings - Fork 8
/
Monkey-X - 2d Dungeon Generator 2 code example.monkey
432 lines (408 loc) · 10.7 KB
/
Monkey-X - 2d Dungeon Generator 2 code example.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
425
426
427
428
429
430
431
432
Import mojo
Const screenwidth:Int=640
Const screenheight:Int=480
Global mapwidth:Int=100
Global mapheight:Int=100
Global tilewidth:Float=640/Float(mapwidth)
Global tileheight:Float=480/Float(mapheight)
Const isnothing:Int=0
Const iswall:Int=1
Const isfloor:Int=2
Const isdoor:Int=3
Const minroomw:Int=5
Const minroomh:Int=5
Const maxroomw:Int=10
Const maxroomh:Int=10
Global map:Int[mapwidth][]
Global lightmap:Int[mapwidth][]
Global backmap:Int[mapwidth][]
Global rcount:Int=0
Class debug
Field x:Int
Field y:Int
Method New(_x:Int,_y:Int)
x=_x
y=_y
End Method
End Class
Class dooropenlist
Field x:Int
Field y:Int
Method New(_x:Int,_y:Int)
x=_x
y=_y
End Method
End Class
Class doorclosedlist
Field x:Int
Field y:Int
Method New(_x:Int,_y:Int)
x=_x
y=_y
End Method
End Class
Global dol:List<dooropenlist> = New List<dooropenlist>
Global dcl:List<doorclosedlist> = New List<doorclosedlist>
Global d:List<debug> = New List<debug>
Class MyGame Extends App
Method OnCreate()
SetUpdateRate(1)
Seed = Millisecs()
For Local i = 0 Until mapwidth
map[i] = New Int[mapheight]
lightmap[i] = New Int[mapheight]
backmap[i] = New Int[mapheight]
Next
createmap(25,mapwidth/2,mapheight/2)
createlightmap
createbackmap
End Method
Method OnUpdate()
createmap(Rnd(10,125),mapwidth/2,mapheight/2)
createlightmap
createbackmap
End Method
Method OnRender()
Cls 0,0,0
SetColor 255,255,255
drawmap
drawbackmap
drawlightmap
End Method
End Class
Function createmap:Bool(numrooms:Int,sx:Int,sy:Int)
For Local y=0 Until mapheight
For Local x=0 Until mapwidth
map[x][y] = isnothing
lightmap[x][y] = 0
backmap[x][y] = 0
Next
Next
d.Clear
dol.Clear
dcl.Clear
dol.AddLast(New dooropenlist(sx,sy))
Local roomcount:Int=0
Local tx:Int
Local ty:Int
While roomcount<numrooms And dol.IsEmpty() = False
Local founddoor:Bool=False
While founddoor=False
For Local i:=Eachin dol
If Rnd(100)<2
founddoor = True
tx = i.x
ty = i.y
Exit
End If
Next
Wend
If makeroomondoor(tx,ty) = True Then
roomcount+=1
removedoorfromopenlist(tx,ty)
dcl.AddLast(New doorclosedlist(tx,ty))
Else
removedoorfromopenlist(tx,ty)
End If
Wend
For Local i:=Eachin dcl
If i.x = sx And i.y = sy Then dcl.Remove i
Next
rcount = roomcount
For Local i:=Eachin dcl
map[i.x][i.y] = isdoor
Next
End Function
Function makeroomondoor:Bool(x:Int,y:Int)
Local makeroom:Bool=False
Local cnt:Int=0
Local x1:Int
Local y1:Int
Local w1:Int
Local h1:Int
Local facing:String
If x+maxroomw > mapwidth Then Return False
If y+maxroomh > mapheight Then Return False
If x-maxroomw < 0 Then Return False
If y-maxroomh < 0 Then Return False
If map[x+1][y]=isnothing Then facing = "right"
If map[x][y-1]=isnothing Then facing = "up"
If map[x][y+1]=isnothing Then facing = "down"
If map[x-1][y]=isnothing Then facing = "left"
If facing="" Then Return False
While cnt<100
w1 = Rnd(minroomw,maxroomw)
h1 = Rnd(minroomh,maxroomh)
Select facing
Case "left"
x1=x-w1
y1=y-Rnd(h1/2)
Case "right"
x1=x+1
y1=y-Rnd(h1/2)
Case "up"
x1=x-Rnd(w1/2)
y1=y-h1
Case "down"
x1=x-Rnd(w1/2)
y1=y+1
End Select
If spaceisempty(x1,y1,w1,h1) = True Then
For Local y2=0 Until h1
For Local x2=0 Until w1
map[x2+x1][y2+y1] = isfloor
If y2 = 0 Or x2 = 0 Or y2 = h1-1 Or x2 = w1-1 Then map[x2+x1][y2+y1] = 1 ' wall
Next
Next
' shift map
Select facing
Case "left"
For Local y2=0 Until h1
For Local x2=w1 Until 0 Step -1
map[x2+x1][y2+y1] = map[x2+x1-1][y2+y1]
Next
Next
For Local y2=0 Until h1
map[x1][y2+y1] = isnothing
Next
'make doors
makedoors(x1,y1,w1,h1,True,False,True,True)
Case "right"
For Local y2=0 Until h1
For Local x2=0 Until w1
map[x2+x1-1][y2+y1] = map[x2+x1][y2+y1]
Next
Next
For Local y2=0 Until h1
map[x1+w1-1][y2+y1] = isnothing
Next
'make doors
makedoors(x1-1,y1,w1,h1,False,True,True,True)
Case "up"
For Local y2=h1 Until 0 Step -1
For Local x2=0 Until w1
map[x2+x1][y2+y1] = map[x2+x1][y2+y1-1]
Next
Next
For Local x2=0 Until w1
map[x1+x2][y1] = isnothing
Next
'make doors
makedoors(x1,y1+1,w1,h1,True,True,True,False)
Case "down"
For Local y2=0 Until h1
For Local x2=0 Until w1
map[x2+x1][y2+y1-1] = map[x2+x1][y2+y1]
Next
Next
For Local x2=0 Until w1
map[x1+x2][y1+h1-1] = isnothing
Next
'make doors
makedoors(x1-1,y1-1,w1+1,h1,True,True,False,True)
End Select
Return True
End If
cnt+=1
Wend
Return False
End Function
Function makedoors:Void(x:Int,y:Int,w:Int,h:Int,l:Bool,r:Bool,u:Bool,d:Bool)
Local dx:Int
Local dy:Int
If l=True Then 'left side
dx = x+1
dy = y+Rnd(h-4)+2
dol.AddLast(New dooropenlist(dx,dy))
End If
If r=True Then 'right side
dx = x+w-1
dy = y+Rnd(h-4)+2
dol.AddLast(New dooropenlist(dx,dy))
End If
If u=True Then 'up side
dx = x+Rnd(w-4)+2
dy = y
dol.AddLast(New dooropenlist(dx,dy))
End If
If d=True Then ' down side
dx = x+Rnd(w-4)+2
dy = y+h-1
dol.AddLast(New dooropenlist(dx,dy))
End If
End Function
Function spaceisempty:Bool(x:Int,y:Int,w:Int,h:Int)
For Local y1=0 Until h
For Local x1=0 Until w
If map[x1+x][y1+y] <> isnothing Then Return False
Next
Next
Return True
End Function
Function makeroom(x:Int,y:Int,w:Int,h:Int)
For Local y1=0 Until h
For Local x1=0 Until w
map[x1+x][y1+y] = 2 ' floor
If y1 = 0 Or x1 = 0 Or y1 = h-1 Or x1 = w-1 Then map[x1+x][y1+y] = 1 ' wall
Next
Next
End Function
Function drawmap:Bool()
For Local y=0 Until mapheight
For Local x=0 Until mapwidth
Select map[x][y]
Case isnothing ;
Case iswall ; SetColor 150,150,150 ' wall
Case isfloor ; SetColor 50,50,50 ' floor
Case isdoor ; SetColor 170,170,170 ' door
End Select
If map[x][y]<>isnothing Then DrawRect x*tilewidth,y*tileheight,tilewidth,tileheight
Next
Next
'SetColor 255,255,0
'For Local i:=Eachin dcl
' DrawRect i.x*tilewidth,i.y*tileheight,tilewidth,tileheight
'Next
SetColor 255,255,255
DrawText "Number of rooms :"+rcount,0,0
#rem
SetColor 255,0,0
For Local i:=Eachin d
DrawRect i.x*tilewidth,i.y*tileheight,tilewidth,tileheight
Next
#End
End Function
Function removedoorfromopenlist:Void(x:Int,y:Int)
For Local i:=Eachin dol
If i.x = x And i.y = y Then
dol.Remove i
d.AddLast(New debug(x,y))
Return
End If
Next
End Function
Function distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
Return Abs(x2-x1)+Abs(y2-y1)
End Function
Function drawlightmap()
For Local y=0 Until mapheight
For Local x=0 Until mapwidth
Local col=lightmap[x][y]
If col>0
SetColor col,col,col
DrawRect x*tilewidth,y*tileheight,tilewidth,tileheight
End If
Next
Next
End Function
Function createlightmap()
For Local y=1 Until mapheight-1
For Local x=1 Until mapwidth-1
If map[x][y] = iswall
If map[x][y+1] = iswall
If map[x+1][y] = iswall
If map[x+1][y+1] = isfloor
Local col:Int=250
For Local y1=0 To 5
If y+y1 >= 0 And y+y1 <= mapheight
If map[x][y+y1] = iswall
lightmap[x][y+y1] = col
End If
End If
col-=22
Next
col = 250
For Local y1=0 To -5 Step -1
If y+y1 >= 0 And y+y1 <= mapheight
If map[x][y+y1] = iswall
lightmap[x][y+y1] = col
End If
End If
col-=22
Next
col = 250
For Local x1=0 To 5
If x+x1 >= 0 And x+x1<=mapwidth
If map[x+x1][y] = iswall
lightmap[x+x1][y] = col
End If
End If
col-=22
Next
col = 250
For Local x1=0 To -5 Step -1
If x+x1>=0 And x+x1<=mapwidth
If map[x+x1][y] = iswall
lightmap[x+x1][y] = col
End If
end if
col-=22
Next
End If
End If
End If
End If
'
If map[x][y] = isfloor
If map[x-1][y] = isfloor
If map[x+1][y] = iswall
If map[x][y+1] = iswall
If map[x+1][y+1] = iswall
For Local y1=-5 To 5
For Local x1=-5 To 5
If x+x1 >= 0 And y+y1 >= 0 And x+x1 < mapwidth And y+y1 <mapheight
Local l:Int=0
l+=distance(x+x1,y+y1,x,y)*3
If map[x+x1][y+y1] = isfloor
lightmap[x+x1][y+y1] = 30+l
End If
End if
Next
Next
End If
End If
End If
End If
End If
Next
Next
End Function
Function drawbackmap()
For Local y=0 Until mapheight
For Local x=0 Until mapwidth
Local val:Int=backmap[x][y]
If val > 0
SetColor val,val,val
DrawRect x*tilewidth,y*tileheight,tilewidth,tileheight
End If
Next
Next
End Function
Function createbackmap()
Local val=0
For Local x=0 Until mapwidth
For Local y=0 Until mapheight
If map[x][y] = 0
backmap[x][y] = val
Endif
If x+1 < mapwidth And x-1 >= 0 And y-1 >=0 And y+1 <mapheight
If map[x+1][y] = iswall Or
map[x-1][y] = iswall Or
map[x][y-1] = iswall Or
map[x][y+1] = iswall
If map[x][y] = isnothing
backmap[x][y] /= 2
Endif
End If
End If
Next
If x<mapwidth/2
val+=Rnd(0,4)
Else
val-=Rnd(0,4)
End If
Next
End Function
Function Main()
New MyGame()
End Function