changeset 29:b431d0f1fbef

Identified maze generator and random number routine Decoded some aspects of maze generation. Also identified and labelled bits related to the random number generator and maze generator.
author William Astle <lost@l-w.ca>
date Fri, 26 Dec 2014 12:22:34 -0700
parents 81d1282860e5
children ce4bf9bde1f0
files dod.s
diffstat 1 files changed, 77 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/dod.s	Fri Dec 26 11:42:20 2014 -0700
+++ b/dod.s	Fri Dec 26 12:22:34 2014 -0700
@@ -96,7 +96,10 @@
 decodestr	macro noexpand
 		dod S06
 		endm
-
+; generate an 8 bit random number in A
+getrandom	macro noexpand
+		dod S07
+		endm
 ; clear graphics screen currently visible; return parameter pointer in U
 cleargfx1	macro noexpand
 		dod S08
@@ -283,9 +286,7 @@
 V262		rmb 1
 V263		rmb 2
 V265		rmb 6
-V26B		rmb 1
-V26C		rmb 1
-V26D		rmb 1
+randomseed	rmb 3				; random number generator seed value
 effectivelight	rmb 1
 V26F		rmb 1
 V270		rmb 3
@@ -424,15 +425,15 @@
 		rts				; return to caller
 LC053		pshs cc,a,b,x,y,u		; save registers and interrupt status
 		orcc #$10			; disable IRQ
-		ldx #V29F
-LC05A		clr ,x+
-		cmpx #V2AD
-		blo LC05A
+		ldx #V29F			; point to start of variables to clear for level creation
+LC05A		clr ,x+				; clear a byte
+		cmpx #V2AD			; are we finished clearing things?
+		blo LC05A			; brif not
 		ldx #V9FD
 		stx V2B9
-LC066		clr ,x+
-		cmpx #emptyhand
-		blo LC066
+LC066		clr ,x+				; clear more data
+		cmpx #emptyhand			; end of area to clear?
+		blo LC066			; brif not
 		ldy #LD7DC
 		dec V2BB
 		ldd #12
@@ -443,29 +444,29 @@
 		jsr LC21D
 		bra LC076
 LC084		puls cc,a,b,x,y,u,pc
-LC086		bsr LC053
-		ldu #LDA91
-		clra
-LC08C		ldb ,u
-		andb #$0f
-		stb V28C
-		ldb ,u+
-		lsrb
+LC086		bsr LC053			; initialize new level data
+		ldu #LDA91			; point to objects to create for demo game
+		clra				; initialize object type
+LC08C		ldb ,u				; get object info
+		andb #$0f			; get low nibble
+		stb V28C			; save number to create
+		ldb ,u+				; get object info again
+		lsrb				; get high nibble
 		lsrb
 		lsrb
 		lsrb
 		stb V28D
-LC09A		createobject
+LC09A		createobject			; create an object
 		dec 5,x
 		incb
 		cmpb #5
 		ble LC0A5
 		ldb V28D
-LC0A5		dec V28C
-		bne LC09A
-		inca
-		cmpu #LDAA3
-		blo LC08C
+LC0A5		dec V28C			; created enough objects?
+		bne LC09A			; brif not
+		inca				; move to next object type
+		cmpu #LDA91+18			; end of table?
+		blo LC08C			; brif not - go to next entry
 		ldu #V388			; point to text parameters for status area
 		dec V2B7			; indicate nonstandard text area
 		clearstatus			; blank status line (where we'll put the copyright notice)
@@ -1044,10 +1045,11 @@
 LC4CC		ldb ,x+				; fetch input data - already aligned
 		rts				; return to caller
 ; swi 7 routine
+; Generate a random number based on seed in randomseed, return 8 bit value in A
 LC4CF		ldx #8
 LC4D2		clrb
 		ldy #8
-		lda V26D
+		lda randomseed+2
 		anda #$e1
 LC4DB		lsla
 		bcc LC4DF
@@ -1055,14 +1057,14 @@
 LC4DF		leay -1,y
 		bne LC4DB
 		lsrb
-		rol V26B
-		rol V26C
-		rol V26D
+		rol randomseed
+		rol randomseed+1
+		rol randomseed+2
 		leax -1,x
 		bne LC4D2
-		lda V26B
-		sta 3,s
-		rts
+		lda randomseed
+		sta 3,s				; save 8 bit random value for return
+		rts				; return to caller
 ; swi 8 routine - clear first graphics screen
 LC4F3		ldu screenvis			; point to first screen parameter block
 		skip2				; skip next instruction
@@ -1413,18 +1415,18 @@
 		ldx #V3D4			; get start address to clear
 		ldu #mazedata			; get end address to clear
 		clearblock			; go clear area to zeros
-		jsr LC053
-		jsr LCC9C
-		ldu creaturecountptr
-		lda #11
-LC783		ldb a,u
-		beq LC78D
+		jsr LC053			; initialize data for new level
+		jsr createmaze			; create the maze
+		ldu creaturecountptr		; point to creature counts for this level
+		lda #11				; offset for wizard
+LC783		ldb a,u				; get number of creatures required
+		beq LC78D			; brif none
 LC787		jsr LCFA5
-		decb
-		bne LC787
-LC78D		deca
-		bpl LC783
-		ldu #V3D4-17
+		decb				; created enough creatures?
+		bne LC787			; brif not
+LC78D		deca				; move on to next creature
+		bpl LC783			; brif not finished all creatures
+		ldu #V3D4-17			; point to creature table
 		clr V291
 LC795		jsr LCF63
 		beq LC7B6
@@ -2151,10 +2153,10 @@
 LCC6B		lda #$ff
 		sta ,u+
 		puls a,b,pc
-LCC71		dod S07
+LCC71		getrandom
 		anda #$1f
 		tfr a,b
-		dod S07
+		getrandom
 		anda #$1f
 LCC7B		pshs a,b
 		anda #$1f
@@ -2173,24 +2175,25 @@
 		andb #$1f
 		cmpb 1,s
 LCC9A		puls a,b,pc
-LCC9C		ldx #mazedata			; get start address to set to $ff
-		ldu #V9F4			; get end address
+; 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
 		setblock			; go set block to $ff
-		ldx #LCD9F
-		ldb currentlevel
-		abx
-		ldd ,x++
-		std V26B
-		lda ,x
-		sta V26D
+		ldx #levelseeds			; point to level seeds table
+		ldb currentlevel		; fetch current level
+		abx				; offset into table (the seeds overlap!)
+		ldd ,x++			; fetch first two bytes of level seed
+		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
-LCCBB		dod S07
-		anda #3
-		sta V28A
-		dod S07
-		anda #7
+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
@@ -2270,7 +2273,7 @@
 		decb
 		bne LCD60
 		ldb V297
-LCD67		dod S07
+LCD67		getrandom
 		decb
 		bne LCD67
 		rts
@@ -2281,7 +2284,7 @@
 		ldb ,x
 		cmpb #$ff
 		beq LCD73
-		dod S07
+		getrandom
 		anda #3
 		sta V28A
 		bitb a,y
@@ -2297,8 +2300,14 @@
 		ora b,u
 		sta ,x
 		puls a,b,x,y,u,pc
-LCD9F		fcb $73,$c7,$5d,$97
-		fcb $f3,$13,$87
+; 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
+; Level 2: c75d97
+; Level 3: 5d97f3
+; 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
@@ -2593,7 +2602,7 @@
 		bpl LD02C
 		cmpa #$20
 		bhs LD03D
-		dod S07
+		getrandom
 		anda #7
 		adda #2
 		inc a,x
@@ -2677,7 +2686,7 @@
 		clrb
 		bra LD101
 LD0E4		ldx #LD114
-		dod S07
+		getrandom
 		tsta
 		bmi LD0EE
 		leax 3,x
@@ -2757,7 +2766,7 @@
 		bgt LD198
 		cmpb #2
 		bgt LD198
-		dod S07
+		getrandom
 		bita #1
 		beq LD196
 		lda V2C1
@@ -2954,7 +2963,7 @@
 		lda 9,y				; get torch type
 		cmpa #$18			; is it "DEAD"?
 		bne LD31F			; brif not
-LD319		dod S07				; get random number
+LD319		getrandom			; get random number
 		anda #3				; 1 in 4 chance of a hit in the dark
 		bne LD375			; brif we didn't hit
 LD31F		playsoundimm $12		; play the "HIT" sound
@@ -3065,7 +3074,7 @@
 LD3FB		lda #10
 		mul
 LD3FE		std ,--s
-		dod S07
+		getrandom
 		tfr a,b
 		clra
 		addd ,s++