-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom-mazes.8o
453 lines (401 loc) · 7.21 KB
/
random-mazes.8o
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
: main
clear
randomize-map
clear
render-map
loop
again
#####################
# Level definitions
# We can only have one for obvious space constraints :(
# 0 = nothing
# 1 = wall
: map
0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01
0x01 0x01 0x00 0x00 0x00 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x00 0x00 0x00 0x01
0x01 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x01 0x00 0x00 0x01 0x00 0x01
0x01 0x00 0x01 0x01 0x01 0x01 0x00 0x01 0x01 0x00 0x01 0x00 0x01 0x00 0x00 0x01
0x01 0x00 0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x01 0x00 0x01 0x01
0x01 0x00 0x00 0x00 0x00 0x01 0x01 0x00 0x01 0x01 0x01 0x00 0x00 0x00 0x00 0x01
0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x00 0x01 0x01 0x01 0x01 0x00 0x01 0x01 0x01
0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01
0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01
: player
0 0 0
:alias X vA
:alias Y vB
:alias FRONTIER vC
:alias RUN vD
: randomize-map
:monitor map 144
# Clear the map (make it all walls)
i := map
v1 := 0
v0 := 1
: rm-clear-loop
save v0
#if SCHIP
# i += v0
#end
v1 += 1
if v1 < 128 then jump rm-clear-loop
# Choose a random starting position
v0 := 7
rand
X := v0
X <<= X
X += 1
v0 := 3
rand
Y := v0
Y <<= Y
Y += 1
v0 := 4
rand
v2 := v0
v0 := X
v1 := Y
i := player
save v2
RUN := 0
FRONTIER := 0
: rm-mark-spot
# Mark spot as new hallway
v0 := 0
map-write
# Mark the new frontier
if Y > 2 begin # Valid coordinate?
Y -= 2
map-read
if v0 != 0 begin
if v0 == 1 then FRONTIER += 1 # Is this a new frontier?
v1 := 0x80
v0 |= v1
map-write
end
Y += 2
end
if X > 2 begin
X -= 2
map-read
if v0 != 0 begin
if v0 == 1 then FRONTIER += 1 # Is this a new frontier?
v1 := 0x40
v0 |= v1
map-write
end
X += 2
end
if X < 13 begin
X += 2
map-read
if v0 != 0 begin
if v0 == 1 then FRONTIER += 1 # Is this a new frontier?
v1 := 0x20
v0 |= v1
map-write
end
X -= 2
end
if Y < 6 begin
Y += 2
map-read
if v0 != 0 begin
if v0 == 1 then FRONTIER += 1 # Is this a new frontier?
v1 := 0x10
v0 |= v1
map-write
end
Y -= 2
end
# Are we done yet?
if FRONTIER == 0 begin
v0 := 7 # Mark the exit
map-write
return
end
# Show progress
RUN += 1
v0 := 0
i := hex v0
v0 := RUN
v0 <<= v0
v0 += 2
v1 := 20
sprite v0 v1 5
clear
render-map
# Pick a new frontier to expand
v0 := FRONTIER
rand
v4 := v0
X := 1
Y := 1
v2 := 0xF0
loop
map-read
v5 := v0
v0 &= v2
if v0 != 0 begin
if v4 == 0 begin
FRONTIER -= 1
# In how many directions can this frontier connect?
#v4 := 0
loop
v0 <<= v0
if vF == 1 then v4 += 1
if v0 != 0 then again
# v4 = number of possible directions
# Pick a random direction to connect to
v0 := v4
rand # destroys v1, v2, v3
v4 := 0x80
v1 := v5
loop
v1 <<= v1
if vF == 1 begin
if v0 == 0 then jump rm-found-direction
v0 -= 1
end
v4 >>= v4
again
: rm-found-direction
# v4 = chosen direction
v0 := 0
if v4 == 0x80 begin
Y += 1
map-write
Y -= 1
end
if v4 == 0x40 begin
X += 1
map-write
X -= 1
end
if v4 == 0x20 begin
X -= 1
map-write
X += 1
end
if v4 == 0x10 begin
Y -= 1
map-write
Y += 1
end
jump rm-mark-spot
end
v4 -= 1
end
X += 1
if X == 16 begin
X := 0
Y += 1
# if Y == 7 then return # This would be an error state
end
again
#####################
# Get a random number
# Input: v0 is upper bound (exclusive)
# Output: v0 is random number
# Destroys: v1, v2, v3
: rand
# Create bitmask
v2 := 0
v3 := v0
: rand-mask
v3 >>= v3
v2 <<= v2
v2 += 1
if v3 != 0 then jump rand-mask
loop
v1 := random 255
v1 &= v2
if v1 < v0 begin
v0 := v1
return
end
again
#####################
# Read map at X,Y into v0
# Destroys i
: map-read
i := map
i += X
v0 := Y
v0 <<= v0
v0 <<= v0
v0 <<= v0
v0 <<= v0
i += v0
load v0
return
#####################
# Write v0 into map at X,Y
# Destroys v1, i
: map-write
i := map
i += X
v1 := Y
v1 <<= v1
v1 <<= v1
v1 <<= v1
v1 <<= v1
i += v1
save v0
return
# Blocking "press any key" routine
# Returns pressed key in v5
: wait-key-press
vA := 0
: wait-key-press-loop
if vA key then jump wait-key-release
vA += 1
if vA != 16 then jump wait-key-press-loop
jump wait-key-press
: wait-key-release
vA := 0
: wait-key-release-loop
if vA key then jump wait-key-release-loop
vA += 1
if vA != 16 then jump wait-key-release-loop
return
: render-map
:alias X v6
:alias Y v7
X := 0
Y := 0
: render-map-loop
# Look up value at X,Y
v0 := X
v1 := Y
map-get
# Don't show tile if its hidden
v1 := 0b00001000
v2 := 0b00000111
v1 &= v0
v2 &= v0
if v1 == 0b00001000 begin
# Hidden normal stuff looks like hallway
v0 := 0x00
# Hidden hallways look like FRONTIER
if v2 == 0x00 then v0 := 0x01
# Hidden coins look like FRONTIER
if v2 == 0x06 then v0 := 0x01
end
# Mask out three relevant bits
v1 := 0b00000111
v0 &= v1
# Get pointer to right sprite
# Sprites are 8 bytes, so sprite = tiles + map value * 4
i := long top-down-tiles
v0 <<= v0
v0 <<= v0
v0 <<= v0
i += v0
# Get coordinates in display space (x4)
v4 := X
v4 <<= v4
v4 <<= v4
v5 := Y
v5 <<= v5
v5 += Y
# Render
sprite v4 v5 3
X += 1
if X < 16 then jump render-map-loop
# Next row
X := 0
Y += 1
if Y < 8 then jump render-map-loop
# Done
return
#####################
# Query the map for the value at a given position
# Input: X in v0, Y in v1 (destructive)
# Output: map value in v0
# Destroys: v0 - v3, i
: map-get
i := map
i += v0
v1 <<= v1
v1 <<= v1
v1 <<= v1
v1 <<= v1
i += v1
load v0
return
#####################
# Tiles for the mini-map
: top-down-tiles
# empty
0b11110000
0b11110000
0b11110000
0b11110000
0b00000000
0b00000000
0b00000000
0b00000000
# wall
0b00000000
0b00000000
0b00000000
0b00000000
0b01010000
0b10100000
0b01010000
0b10100000
# player starting position
0b00000000
0b00000000
0b00000000
0b01100000
0b00000000
0b01100000
0b01100000
0b00000000
# finish
0b00000000
0b00010000
0b00010000
0b00000000
0b01100000
0b11110000
0b10010000
0b10010000
# button
0b00000000
0b10000000
0b10000000
0b00000000
0b00000000
0b01100000
0b01100000
0b00000000
# snake
0b01100000
0b00000000
0b00000000
0b00000000
0b00100000
0b01000000
0b00100000
0b01000000
# Coins are not visible on the map
0b11110000
0b10110000
0b11010000
0b11110000
0b00000000
0b00000000
0b00000000
0b00000000
# Coins are not visible on the map
0b11110000
0b11010000
0b10110000
0b11110000
0b00000000
0b00000000
0b00000000
0b00000000