changeset 46:e8eee008ad42

Decoded creature handler. Decoded the routine that handles creature movement and other actions.
author William Astle <lost@l-w.ca>
date Sat, 27 Dec 2014 13:07:49 -0700
parents 8af580d56e9f
children 90d91aa004d5
files dod.s
diffstat 1 files changed, 146 insertions(+), 146 deletions(-) [+]
line wrap: on
line diff
--- a/dod.s	Sat Dec 27 01:27:00 2014 -0700
+++ b/dod.s	Sat Dec 27 13:07:49 2014 -0700
@@ -2669,110 +2669,110 @@
 		inc a,x				; bump creature count for that type
 LD03D		ldd #$0508			; reschedule for 5 minutes
 		rts				; return to caller
-LD041		ldy 5,u
-		tst creaturefreeze
-		bne LD06A
-		ldb 12,y
-		bne LD04D
-		rts
-LD04D		lda 13,y
-		cmpa #6
-		beq LD06D
-		cmpa #10
-		bge LD06D
-		ldd 15,y
-		clr V291
-		jsr LCF53
-		beq LD06D
-		ldd 8,y
-		stx 8,y
-		std ,x
-		dec 5,x
-		updatedungeon
-LD06A		jmp LD103
-LD06D		ldd 15,y
-		cmpd playerloc
-		bne LD0B2
-		lda 13,y
-		ldb #$ff
-		playsound
-		ldd #$8080
-		ldx lefthand
-		bsr LD09E
-		ldx righthand
-		bsr LD09E
-		sta V21A
-		stb V21C
-		tfr y,x
-		ldu #powerlevel
-		jsr attack
-		bmi LD099
-		playsoundimm $13
-		jsr damage
-LD099		checkdamage
-		jmp LD10F
-LD09E		pshs a,b,x
-		beq LD0B0
-		lda 10,x
-		cmpa #3
-		bne LD0B0
-		ldx 6,x
-		cmpx ,s
-		bhs LD0B0
-		stx ,s
-LD0B0		puls a,b,x,pc
-LD0B2		cmpa playerloc
-		bne LD0C3
-		lda 16,y
-		ldb #1
-		suba playerloc+1
-		bmi LD0D0
-		ldb #3
-		bra LD0D0
-LD0C3		ldd 15,y
-		cmpb playerloc+1
-		bne LD0E4
-		ldb #2
-		suba playerloc
-		bmi LD0D0
-		clrb
-LD0D0		stb V28A
-		ldd 15,y
-LD0D4		bsr LD136
-		bne LD0E4
-		cmpd playerloc
-		bne LD0D4
-		ldb V28A
-		stb 14,y
-		clrb
-		bra LD101
-LD0E4		ldx #LD114
-		getrandom
-		tsta
-		bmi LD0EE
-		leax 3,x
-LD0EE		anda #3
-		bne LD0F4
-		leax 1,x
-LD0F4		lda #3
-LD0F6		ldb ,x+
-		bsr LD14F
-		beq LD103
-		deca
-		bne LD0F6
-		ldb #2
-LD101		bsr LD14F
-LD103		lda 6,y
-		ldx 15,y
-		cmpx playerloc
-		bne LD111
-		updatedungeon
-		clr V2B5
-LD10F		lda 7,y
-LD111		ldb #4
-		rts
-LD114		fcb $00,$03,$01,$00
-		fcb $01,$03,$00		
+; This is the routine that handles creature movement, etc.
+LD041		ldy 5,u				; get creature data pointer
+		tst creaturefreeze		; are creatures frozen (after the Wizard is beaten)?
+		bne LD06A			; brif so
+		ldb 12,y			; is the creature alive?
+		bne LD04D			; brif so
+		rts				; return to caller
+LD04D		lda 13,y			; get the creature type
+		cmpa #6				; is it a scorpion?
+		beq LD06D			; brif so
+		cmpa #10			; is it the wizard's image or wizard?
+		bge LD06D			; brif so
+		ldd 15,y			; fetch room location
+		clr V291			; reset object iterator
+		jsr LCF53			; fetch first object in room
+		beq LD06D			; brif no object in room
+		ldd 8,y				; get creature inventory pointer
+		stx 8,y				; save room object as head of inventory list
+		std ,x				; save inventory list as next item
+		dec 5,x				; mark object as carried
+		updatedungeon			; update the dungeon view
+LD06A		jmp LD103			; go reschedule
+LD06D		ldd 15,y			; get cerature location
+		cmpd playerloc			; is it in the room with the player?
+		bne LD0B2			; brif not
+		lda 13,y			; get creature type
+		ldb #$ff			; maximum sound volume
+		playsound			; go make the creature sound (always makes on attack)
+		ldd #$8080			; base defense modifiers
+		ldx lefthand			; get object in left hand
+		bsr LD09E			; set modifiers if shield
+		ldx righthand			; get object in right hand
+		bsr LD09E			; set modifiers if shield
+		sta V21A			; save magical defense value for player
+		stb V21C			; save physical defense value for player
+		tfr y,x				; put the creature as the attacker
+		ldu #powerlevel			; put the player as defender
+		jsr attack			; calculate an attack
+		bmi LD099			; brif attack failed
+		playsoundimm $13		; play the hit sound
+		jsr damage			; go damage the player
+LD099		checkdamage			; check damage levels
+		jmp LD10F			; go reschedule
+LD09E		pshs a,b,x			; save registers
+		beq LD0B0			; brif no object
+		lda 10,x			; get object type
+		cmpa #3				; is it a shield?
+		bne LD0B0			; brif not
+		ldx 6,x				; get magical and physical defense values
+		cmpx ,s				; is it higher (magic has precedence)
+		bhs LD0B0			; brif so - less good
+		stx ,s				; save new defense multipliers
+LD0B0		puls a,b,x,pc			; restore registers and return
+LD0B2		cmpa playerloc			; are we in the same horizontal line as the player?
+		bne LD0C3			; brif not
+		lda 16,y			; get vertical coordinate for creature
+		ldb #1				; assume east
+		suba playerloc+1		; calculate distance to player
+		bmi LD0D0			; brif negative - player is east
+		ldb #3				; player is actually west
+		bra LD0D0			; go check movement
+LD0C3		ldd 15,y			; get creature location
+		cmpb playerloc+1		; are we in the same column as the player?
+		bne LD0E4			; brif not
+		ldb #2				; assume south
+		suba playerloc			; calculate difference to player
+		bmi LD0D0			; brif player is south
+		clrb				; set north
+LD0D0		stb V28A			; save direction
+		ldd 15,y			; get creature location
+LD0D4		bsr LD136			; calculate new coordinates
+		bne LD0E4			; brif not a room
+		cmpd playerloc			; is the new room the player's place?
+		bne LD0D4			; brif not
+		ldb V28A			; get direction to move
+		stb 14,y			; set last movement direction to player direction
+		clrb				; select a last ditch direction
+		bra LD101			; go try the movement and continue
+LD0E4		ldx #LD114			; point to direction selections
+		getrandom			; fetch a random value
+		tsta				; set flags
+		bmi LD0EE			; brif negative
+		leax 3,x			; select alternative direction sets
+LD0EE		anda #3				; normalize direction to 0-3
+		bne LD0F4			; brif nonzero
+		leax 1,x			; move to next value
+LD0F4		lda #3				; try 3 times for a movement
+LD0F6		ldb ,x+				; get direction modifier
+		bsr LD14F			; go handle movement
+		beq LD103			; brif movement succeeded
+		deca				; have we tried enough times?
+		bne LD0F6			; brif not
+		ldb #2				; try one more last ditch option
+LD101		bsr LD14F			; do movement
+LD103		lda 6,y				; get movement tick rate
+		ldx 15,y			; get creature location
+		cmpx playerloc			; does it match the player?
+		bne LD111			; brif not - use movement rate
+		updatedungeon			; update the dungeon display immediately
+		clr V2B5			; mark dungeon update not required
+LD10F		lda 7,y				; get attack tick rate
+LD111		ldb #4				; and schedule for the 10Hz timer
+		rts				; return to caller
+LD114		fcb $00,$03,$01,$00,$01,$03,$00	; direction rotations for movement choices
 LD11B		pshs a,b			; save coordinates
 		ldb V28A			; get direction to move
 		andb #3				; force it to 0-3
@@ -2799,46 +2799,46 @@
 		lda #1				; set so we get Z=1 on return
 LD14C		deca				; set flags for success/fail
 LD14D		puls a,b,x,y,u,pc		; restore registers and return
-LD14F		pshs a,b,x
-		addb 14,y
-		andb #3
-		stb V28A
-		ldd 15,y
-		bsr LD136
-		bne LD199
-		jsr LCF82
-		bne LD199
-		std 15,y
-		ldb V28A
-		stb 14,y
-		ldd 15,y
-		suba playerloc
-		bpl LD16F
-		nega
-LD16F		subb playerloc+1
-		bpl LD174
-		negb
-LD174		stb accum0
-		cmpa accum0
-		bge LD17C
-		exg a,b
-LD17C		sta accum0
-		cmpa #8
-		bgt LD198
-		cmpb #2
-		bgt LD198
-		getrandom
-		bita #1
-		beq LD196
-		lda accum0
-		ldb #$1f
-		mul
-		comb
-		lda 13,y
-		playsound
-LD196		dec V2B5
-LD198		clra
-LD199		puls a,b,x,pc
+LD14F		pshs a,b,x			; save registers
+		addb 14,y			; add selected rotation to current movement direction
+		andb #3				; normalize to 0-3
+		stb V28A			; save new direction
+		ldd 15,y			; get creature location
+		bsr LD136			; calculate new coordinates
+		bne LD199			; brif no room there
+		jsr LCF82			; get creature in room
+		bne LD199			; brif there's a creature there - can't go
+		std 15,y			; save new creature location
+		ldb V28A			; get direction
+		stb 14,y			; save as last moved direction
+		ldd 15,y			; get new location
+		suba playerloc			; get distance from player (Y)
+		bpl LD16F			; brif positive
+		nega				; invert msb (absolute value)
+LD16F		subb playerloc+1		; get distance from player (X)
+		bpl LD174			; brif positive
+		negb				; invert lsb (absolute value)
+LD174		stb accum0			; save X distance
+		cmpa accum0			; is the Y distance more? 
+		bge LD17C			; brif so
+		exg a,b				; use the Y distance then
+LD17C		sta accum0			; save calculated distance
+		cmpa #8				; more than 8 rooms away in long distance?
+		bgt LD198			; brif so
+		cmpb #2				; more than 2 rooms away in short distance?
+		bgt LD198			; brif so
+		getrandom			; get a random value
+		bita #1				; do we need to make a sound?
+		beq LD196			; brif we won't make a sound
+		lda accum0			; get distance
+		ldb #$1f			; multplier for distance
+		mul				; calculate distance volume modifier
+		comb				; invert it so closer is louder
+		lda 13,y			; get creature number
+		playsound			; go make the creature's sound
+LD196		dec V2B5			; mark dungeon update required
+LD198		clra				; set Z for movement happened
+LD199		puls a,b,x,pc			; restore registers and return
 ; This is the routine that ticks down the torch.
 LD19B		ldu curtorch			; get currently burning torch
 		beq LD1BC			; brif no torch in use
@@ -2924,11 +2924,11 @@
 		leau 1,u			; move past the "object type" flag
 		delay				; wait a bit
 		skip2				; skip next instruction
-LD235		bsr LD24C
+LD235		bsr LD24C			; go handle input character
 		lda ,u+				; fetch a character from the decoded string
 		bpl LD235			; brif not end of string
 		clra				; code for a space
-		bsr LD24C			
+		bsr LD24C			; go handle input character
 		decb				; have we consumed all the words in this command?
 		bne LD229			; brif not - get another
 		lda #$1f			; code for carriage return