Prev: BDFE Up: Map Next: BF36
BEDF: Calculate Percentage Complete
Used by the routine at GameOver.
Works out how much of the game has been completed and stores it as a percentage in PercentageHundreds (the hundreds digit) and PercentageTensAndUnits (the tens and units, as BCD).
Every bit set in the 80 bytes at VisitedCells is a cell of the village that has been visited. Every monster that has been killed counts as three more cells. The total is multiplied by 2830 and divided by 10000, so that visiting about 027D cells (or a few less when monsters have been killed) gives 64 percent.
Count the visited cells.
CalculatePercentage BEDF LD HL,$0000 Start the count of cells at zero.
BEE2 LD DE,$BC0E Point at the bitmap of visited cells.
BEE5 LD C,$80 Set the number of bytes in the bitmap to 80.
CalculatePercentage_ByteLoop BEE7 LD A,(DE) Fetch a byte of the bitmap and move on to the next.
BEE8 INC DE
BEE9 LD B,$08 Set a counter for the eight bits in the byte.
CalculatePercentage_BitLoop BEEB RRCA Check the next bit of the bitmap, and move it into the carry flag.
BEEC JR NC,CalculatePercentage_NextBit
BEEE INC HL Count the visited cell.
CalculatePercentage_NextBit BEEF DJNZ CalculatePercentage_BitLoop Loop back until all eight bits have been counted.
BEF1 DEC C Loop back until every byte of the bitmap has been counted.
BEF2 JR NZ,CalculatePercentage_ByteLoop
Add three for every monster that has been killed.
BEF4 LD IY,$BD5E Start with the first of the four monsters.
BEF8 LD B,$04 Set a counter for the four monsters.
CalculatePercentage_MonsterLoop BEFA LD A,(IY+$00) Fetch the type of the monster and subtract 60.
BEFD SUB $60
BEFF CP $10 Jump to CalculatePercentage_NextMonster if the monster is still alive (its type is 60 to 6F).
BF01 JR C,CalculatePercentage_NextMonster
BF03 LD DE,$0003 Add three to the count of cells.
BF06 ADD HL,DE
CalculatePercentage_NextMonster BF07 LD DE,$0010 Set the size of a monster's entry in the object table to 10 bytes.
BF0A ADD IY,DE Move on to the next monster.
BF0C DJNZ CalculatePercentage_MonsterLoop Loop back until all four monsters have been counted.
Multiply the count by 2830 and divide by 10000, using BCD arithmetic for the high byte.
BF0E LD BC,$2830 Set the value to add on each pass.
BF11 XOR A Start the BCD result at zero.
BF12 EX DE,HL Make the count of cells the number of times to add.
BF13 LD HL,$0000 Start the running total at zero.
CalculatePercentage_MultiplyLoop BF16 ADD HL,BC Add the value to the total.
BF17 ADC A,$00 Add any carry to the BCD result.
BF19 DAA Adjust the BCD result.
BF1A PUSH AF Stash the result on the stack.
BF1B DEC DE Decrease the count.
BF1C LD A,E Jump to CalculatePercentage_Finish if the count has reached zero.
BF1D OR D
BF1E JR Z,CalculatePercentage_Finish
BF20 POP AF Restore the result from the stack.
BF21 JR CalculatePercentage_MultiplyLoop Jump to CalculatePercentage_MultiplyLoop.
CalculatePercentage_Finish BF23 POP AF Restore the result from the stack.
BF24 LD BC,$0090 Add 90 to the total, which rounds the percentage.
BF27 ADD HL,BC Add any carry to the BCD result.
BF28 ADC A,$00
BF2A DAA
BF2B LD ($BBFE),A Set the tens and units of the percentage.
BF2E LD A,$00 Set the hundreds digit to one if the percentage has overflowed.
BF30 ADC A,$00
BF32 LD ($BBFD),A Set the hundreds digit of the percentage.
BF35 RET Return.
Prev: BDFE Up: Map Next: BF36