Prev: BE0F Up: Map Next: BF36
BEDF: Work out the percentage of the game done
Used by the routine at GAME_OVER.
Called by GAME_OVER after every game. A unit for every cell the knight has been in (every set bit of VISITED) and three for every villain record not holding a live villain (graphics 96-111): a destroyed villain's record is empty or dying. Each unit adds 10288 to a 16-bit fraction of which 65536 is one per cent, the carries counted in BCD; at the end 144 more, exactly what 637 units need to make 100. The town has 625 cells a knight can stand in, and three for each of four villains makes 637.
The carries are counted with DAA, which wraps from 99 to 0 and leaves its own carry to be lost: past 637 units the count starts again. A knight cannot be in more than 625 cells, so it never does in play; setting every bit of VISITED (1024) gave 62% (measured, stage 1).
PERCENTAGE BEDF LD HL,$0000 HL = how many cells he has been in: the set bits of VISITED
BEE2 LD DE,VISITED
BEE5 LD C,$80
PERCENTAGE_0 BEE7 LD A,(DE)
BEE8 INC DE
BEE9 LD B,$08
PERCENTAGE_1 BEEB RRCA
BEEC JR NC,PERCENTAGE_2
BEEE INC HL
PERCENTAGE_2 BEEF DJNZ PERCENTAGE_1
BEF1 DEC C
BEF2 JR NZ,PERCENTAGE_0
BEF4 LD IY,VILLAINS three more for each villain record without a live villain
BEF8 LD B,$04
PERCENTAGE_3 BEFA LD A,(IY+$00)
BEFD SUB $60
BEFF CP $10
BF01 JR C,PERCENTAGE_4
BF03 LD DE,$0003
BF06 ADD HL,DE
PERCENTAGE_4 BF07 LD DE,$0010
BF0A ADD IY,DE
BF0C DJNZ PERCENTAGE_3
BF0E LD BC,$2830 add 10288 for each unit, the carries counted in BCD in A
BF11 XOR A
BF12 EX DE,HL
BF13 LD HL,$0000
PERCENTAGE_5 BF16 ADD HL,BC
BF17 ADC A,$00
BF19 DAA
BF1A PUSH AF
BF1B DEC DE
BF1C LD A,E
BF1D OR D
BF1E JR Z,PERCENTAGE_6
BF20 POP AF
BF21 JR PERCENTAGE_5
PERCENTAGE_6 BF23 POP AF 144 more, rounding 637 units up to exactly a hundred
BF24 LD BC,$0090
BF27 ADD HL,BC
BF28 ADC A,$00
BF2A DAA
BF2B LD (PERCENT_LOW),A the tens and units, and the hundreds from the last carry
BF2E LD A,$00
BF30 ADC A,$00
BF32 LD (PERCENT),A
BF35 RET
Prev: BE0F Up: Map Next: BF36