changeset 30:ce4bf9bde1f0

Decoded maze generator Fully decoded maze generator.
author William Astle <lost@l-w.ca>
date Fri, 26 Dec 2014 13:16:48 -0700
parents b431d0f1fbef
children 5a661416e322
files dod.s
diffstat 1 files changed, 164 insertions(+), 165 deletions(-) [+]
line wrap: on
line diff
--- a/dod.s	Fri Dec 26 12:22:34 2014 -0700
+++ b/dod.s	Fri Dec 26 13:16:48 2014 -0700
@@ -225,7 +225,6 @@
 
 ; the direct page
 		org $200
-V1F4		equ $1f4
 zero		rmb 2				; initialized to $0000
 V202		rmb 1
 allones		rmb 2				; initialized to $ffff
@@ -2129,52 +2128,52 @@
 		bne badcommandret		; brif not - error
 LCC46		ldx ,u				; fetch object pointer to X (and set Z if nothing)
 		rts
-LCC49		pshs a,b,x,u
-		deca
-		bsr LCC56
-		inca
+LCC49		pshs a,b,x,u			; save coordinates and registers
+		deca				; look at rooms to the NE, N, NW
 		bsr LCC56
-		inca
+		inca				; look at rooms to the E, W, <here>
 		bsr LCC56
-		puls a,b,x,u,pc
-LCC56		pshs a,b
-		decb
-		bsr LCC60
-		incb
+		inca				; look at rooms to the SE, S, SW
+		bsr LCC56
+		puls a,b,x,u,pc			; restore registers and return
+LCC56		pshs a,b			; save coordinates
+		decb				; look at room to W
 		bsr LCC60
-		incb
-		skip2
-LCC60		pshs a,b
-		bsr LCC8E
-		bne LCC6B
-		bsr LCC7B
-		lda ,x
-		skip2
-LCC6B		lda #$ff
-		sta ,u+
-		puls a,b,pc
-LCC71		getrandom
-		anda #$1f
-		tfr a,b
-		getrandom
-		anda #$1f
-LCC7B		pshs a,b
-		anda #$1f
+		incb				; look at room <here>
+		bsr LCC60
+		incb				; look at room E
+		skip2				; skip next instruction
+LCC60		pshs a,b			; save coordinates
+		bsr LCC8E			; did we fall off side of map?
+		bne LCC6B			; brif so
+		bsr LCC7B			; get pointer to room data
+		lda ,x				; fetch room data
+		skip2				; skip instruction
+LCC6B		lda #$ff			; flag no tunnel
+		sta ,u+				; save data for this room
+		puls a,b,pc			; save registers and return
+LCC71		getrandom			; get a random number
+		anda #$1f			; convert it to 0-31
+		tfr a,b				; save it
+		getrandom			; get another random number
+		anda #$1f			; also convert it to 0-31
+LCC7B		pshs a,b			; save coordinates
+		anda #$1f			; force coordinates to range 0-31
 		andb #$1f
-		tfr d,x
-		ldb #$20
-		mul
-		addd #mazedata
-		exg d,x
-		abx
-		puls a,b,pc
-LCC8E		pshs a,b
-		anda #$1f
-		cmpa ,s
-		bne LCC9A
-		andb #$1f
-		cmpb 1,s
-LCC9A		puls a,b,pc
+		tfr d,x				; save coordinates for later
+		ldb #32				; 32 rooms per row
+		mul				; calculate row offset
+		addd #mazedata			; convert to absolute pointer
+		exg d,x				; get pointer to X, get back coordinates
+		abx				; add offset within row
+		puls a,b,pc			; restore coordinates and return pointer in X
+LCC8E		pshs a,b			; save coordinates
+		anda #$1f			; modulo the Y coordinate
+		cmpa ,s				; does it match?
+		bne LCC9A			; brif not - fell off side
+		andb #$1f			; modulo the X coordinate
+		cmpb 1,s			; does it match? (set flags)
+LCC9A		puls a,b,pc			; return Z set if not falling off side of map
 ; This routine creates a maze for the specified level number.
 createmaze	ldx #mazedata			; get start address to set to $ff
 		ldu #mazedata+1024		; get end address
@@ -2186,120 +2185,120 @@
 		std randomseed			; set random seed
 		lda ,x				; fetch third byte of level seed
 		sta randomseed+2		; set random seed
-		ldy #V1F4
-		jsr LCC71
-		std V27C
+		ldy #500			; dig out 500 rooms
+		jsr LCC71			; fetch a random starting point
+		std V27C			; save starting pointer
 LCCBB		getrandom			; get random number
 		anda #3				; select only 4 directions
 		sta V28A			; save direction we're going
 		getrandom			; get random number
 		anda #7				; convert to value from 1-8
 		inca
-		sta V27E
-		bra LCCD2
-LCCCA		ldd V288
-		std V27C
-		dec V27E
-		beq LCCBB
-LCCD2		ldd V27C
-		jsr LD11B
-		bsr LCC8E
-		bne LCCBB
-		std V288
-		tst ,x
-		beq LCCCA
-		ldu #V9F4
-		jsr LCC49
-		lda 3,u
-		adda ,u
-		adda 1,u
-		beq LCCBB
-		lda 1,u
-		adda 2,u
-		adda 5,u
-		beq LCCBB
-		lda 5,u
-		adda 8,u
-		adda 7,u
-		beq LCCBB
-		lda 7,u
-		adda 6,u
-		adda 3,u
-		beq LCCBB
-		clr ,x
-		leay -1,y
-		bne LCCCA
-		clr V27C
+		sta V27E			; save number of steps to dig out
+		bra LCCD2			; go dig the tunnel
+LCCCA		ldd V288			; get current coordinate
+		std V27C			; save it as starting position
+		dec V27E			; have we gone far enough?
+		beq LCCBB			; brif so - select a new direction
+LCCD2		ldd V27C			; fetch maze coordinates
+		jsr LD11B			; apply direction to coordinates
+		bsr LCC8E			; did we fall off the side of the map?
+		bne LCCBB			; brif so - select a new direction
+		std V288			; save new coordinate
+		tst ,x				; is this room open?
+		beq LCCCA			; brif so - move to next
+		ldu #V9F4			; point to temporary storage area
+		jsr LCC49			; set bytes to FF or 00 depending on whether the rooms in the 3x3 area are open
+		lda 3,u				; get W room
+		adda ,u				; add data for NW room
+		adda 1,u			; add data for N room
+		beq LCCBB			; brif all open - get new direction
+		lda 1,u				; get data for N room
+		adda 2,u			; add data for NE room
+		adda 5,u			; add data for E room
+		beq LCCBB			; brif all open - get new direction
+		lda 5,u				; get data for E room
+		adda 8,u			; add data for SE room
+		adda 7,u			; add data for S room
+		beq LCCBB			; brif all open - get new direction
+		lda 7,u				; get data for S room
+		adda 6,u			; add data for SW room
+		adda 3,u			; add data for W room
+		beq LCCBB			; brif all open - get new direction
+		clr ,x				; mark this room open
+		leay -1,y			; have we dug out enough rooms?
+		bne LCCCA			; brif not - keep digging
+		clr V27C			; set coordinates to top left
 		clr V27D
-LCD11		ldd V27C
-		jsr LCC7B
-		lda ,x
-		inca
-		beq LCD41
-		ldd V27C
-		ldu #V9F4
-		jsr LCC49
-		lda ,x
-		ldb #$ff
-		cmpb 1,u
-		bne LCD2D
-		ora #3
-LCD2D		cmpb 3,u
-		bne LCD33
-		ora #$c0
-LCD33		cmpb 5,u
-		bne LCD39
-		ora #$0c
-LCD39		cmpb 7,u
-		bne LCD3F
-		ora #$30
-LCD3F		sta ,x
-LCD41		ldb #$20
-		inc V27D
-		cmpb V27D
-		bne LCD11
-		clr V27D
-		inc V27C
-		cmpb V27C
-		bne LCD11
-		ldb #$46
-		ldu #LCDAA
-LC056		bsr LCD6D
-		decb
-		bne LC056
-		ldb #$2d
-		ldu #LCDAE
-LCD60		bsr LCD6D
-		decb
-		bne LCD60
-		ldb V297
-LCD67		getrandom
-		decb
-		bne LCD67
-		rts
-LCD6D		pshs a,b,x,y,u
-		ldy #LCDA6
-LCD73		jsr LCC71
-		std V288
-		ldb ,x
-		cmpb #$ff
-		beq LCD73
-		getrandom
-		anda #3
-		sta V28A
-		bitb a,y
-		bne LCD73
-		orb a,u
-		stb ,x
-		ldd V288
-		jsr LD11B
-		ldb V28A
-		addb #2
+LCD11		ldd V27C			; get current coordinates
+		jsr LCC7B			; convert to pointer
+		lda ,x				; get room data
+		inca				; is ot open?
+		beq LCD41			; brif not
+		ldd V27C			; get coordinates
+		ldu #V9F4			; point to temp area
+		jsr LCC49			; calculate neighbors
+		lda ,x				; get room data at current room
+		ldb #$ff			; data for "no room"
+		cmpb 1,u			; is there a room N?
+		bne LCD2D			; brif so
+		ora #3				; flag as no exit N
+LCD2D		cmpb 3,u			; is there a room W?
+		bne LCD33			; brif so
+		ora #$c0			; flag as no exit W
+LCD33		cmpb 5,u			; is there a room E
+		bne LCD39			; brif so
+		ora #$0c			; flag as no exit E
+LCD39		cmpb 7,u			; is there a room S?
+		bne LCD3F			; brif so
+		ora #$30			; flag as no exit S
+LCD3F		sta ,x				; save adjusted room data
+LCD41		ldb #32				; 32 rooms per row
+		inc V27D			; bump X coordinate
+		cmpb V27D			; did we wrap?
+		bne LCD11			; brif not
+		clr V27D			; reset to left edge
+		inc V27C			; bump Y coordinate
+		cmpb V27C			; did we wrap?
+		bne LCD11			; brif not - fix another room's exits
+		ldb #70				; create 70 doors
+		ldu #doormasks			; pointer to routine to make a normal door
+LC056		bsr LCD6D			; go create a door
+		decb				; are we done yet?
+		bne LC056			; brif not
+		ldb #$2d			; create 45 magic doors
+		ldu #mdoormasks			; pointer to routine to make a magic door
+LCD60		bsr LCD6D			; go create a door
+		decb				; done yet?
+		bne LCD60			; brif not
+		ldb V297			; get number of times to spin the random number generator
+LCD67		getrandom			; fetch a random number
+		decb				; have we done enough randoms?
+		bne LCD67			; brif not, do another
+		rts				; return to caller
+LCD6D		pshs a,b,x,y,u			; save registers
+		ldy #dirmasks			; point to direction masks
+LCD73		jsr LCC71			; get a random location
+		std V288			; save coordinates
+		ldb ,x				; get room data at location
+		cmpb #$ff			; is there a room?
+		beq LCD73			; brif not - try again
+		getrandom			; get random number
+		anda #3				; normalize to direction
+		sta V28A			; save direction
+		bitb a,y			; is there a door or wall at that direction?
+		bne LCD73			; brif so - try again
+		orb a,u				; mark the direction as having a door of desired type
+		stb ,x				; save new room data
+		ldd V288			; get back coordinates
+		jsr LD11B			; get pointer to neighbor
+		ldb V28A			; get direction back
+		addb #2				; calculate opposite direction
 		andb #3
-		lda ,x
-		ora b,u
-		sta ,x
-		puls a,b,x,y,u,pc
+		lda ,x				; get data at neighboring room
+		ora b,u				; set it to the right type of door
+		sta ,x				; save new neighbor data
+		puls a,b,x,y,u,pc		; restore data and return
 ; These are the random seeds for the level mazes. Note that the seeds overlap by two
 ; bytes. The actual seed values are:
 ; Level 1: 73c75d
@@ -2308,9 +2307,9 @@
 ; Level 4: 97f313
 ; Level 5: f31387
 levelseeds	fcb $73,$c7,$5d,$97,$f3,$13,$87
-LCDA6		fcb $03,$0c,$30,$c0
-LCDAA		fcb $01,$04,$10,$40
-LCDAE		fcb $02,$08,$20,$80
+dirmasks	fcb $03,$0c,$30,$c0		; direction masks
+doormasks	fcb $01,$04,$10,$40		; direction masks to create doors
+mdoormasks	fcb $02,$08,$20,$80		; direction masks to create magic doors
 LCDB2		ldu screendraw
 		ldd #$1f1f
 		std V27C
@@ -2712,19 +2711,19 @@
 		rts
 LD114		fcb $00,$03,$01,$00
 		fcb $01,$03,$00		
-LD11B		pshs a,b
-		ldb V28A
-		andb #3
-		lslb
-		ldx #LD12E
-		ldd b,x
-		adda ,s+
-		addb ,s+
-		jmp LCC7B
-LD12E		fdb $ff00
-		fdb 1
-		fdb $100
-		fdb $ff
+LD11B		pshs a,b			; save coordinates
+		ldb V28A			; get direction to move
+		andb #3				; force it to 0-3
+		lslb				; two bytes per direction adjuster
+		ldx #LD12E			; point to direction adjusters
+		ldd b,x				; get adjuster
+		adda ,s+			; apply north/south adjustment
+		addb ,s+			; apply east/west adjustment
+		jmp LCC7B			; convert to pointer in X
+LD12E		fdb $ff00			; move north (-1, 0)
+		fdb 1				; move east (0, +1)
+		fdb $100			; move south (+1, 0)
+		fdb $ff				; move west (0, -1)
 LD136		pshs a,b,x,y,u
 		bsr LD11B
 		jsr LCC8E