Prev: 39742 Up: Map Next: 40227
40134: Print Percentage Explored
Used by the routine at GameOver_1UP_Messaging.
Works out how much of the jungle the player has explored and prints the result as a percentage on the game over screen.
The number of rooms visited (the bits set in the bitmap at VisitedRooms) plus the number of amulet pieces collected (the bits set in the low four bits of 38669) is converted into a percentage of 0260 (the 256 rooms plus the four amulet pieces). The result is held as two BCD bytes in Percentage_Hundreds (the hundreds) and PreviousGameOptions (the tens and units) before being printed.
Before
percentage-before
After PrintRoomsVisited
percentage-after
Count the number of rooms which have been visited.
PrintRoomsVisited 40134 LD DE,0 Set the count of rooms visited and amulet pieces collected in DE to 0.
40137 LD BC,2080 Set the bit counter in B to 8 and the byte counter in C to 32.
40140 LD HL,38591 Point HL at the visited rooms bitmap at VisitedRooms.
PrintRoomsVisited_CountByte 40143 PUSH BC Stash the counters on the stack.
40144 LD A,(HL) Fetch the next byte of the visited rooms bitmap.
40145 INC HL Move onto the next byte of the bitmap.
PrintRoomsVisited_CountBit 40146 RRCA Rotate the bit for the next room into the carry flag.
40147 JR NC,PrintRoomsVisited_NextBit Jump to PrintRoomsVisited_NextBit if the room has not been visited.
40149 INC DE Add the room to the count in DE.
PrintRoomsVisited_NextBit 40150 DJNZ PrintRoomsVisited_CountBit Decrease the bit counter by one and loop back to PrintRoomsVisited_CountBit until all eight rooms in the byte have been checked.
40152 POP BC Restore the counters from the stack.
40153 DEC C Decrease the byte counter by one and jump back to PrintRoomsVisited_CountByte until all 32 bytes of the bitmap have been counted.
40154 JR NZ,PrintRoomsVisited_CountByte
Add the number of amulet pieces which have been collected.
40156 LD A,(38669) Fetch the amulet pieces collected from 38669.
40159 LD B,4 Set the amulet piece counter in B to 4.
PrintRoomsVisited_CountPiece 40161 RRCA Rotate the flag for the next amulet piece into the carry flag.
40162 JR NC,PrintRoomsVisited_NextPiece Jump to PrintRoomsVisited_NextPiece if the amulet piece has not been collected.
40164 INC DE Add the amulet piece to the count in DE.
PrintRoomsVisited_NextPiece 40165 DJNZ PrintRoomsVisited_CountPiece Decrease the amulet piece counter by one and loop back to PrintRoomsVisited_CountPiece until all four pieces have been checked.
Multiply the count by 25206 (65536 * 100 / 0260) by repeated addition. Every time the sum in HL overflows the carry is added to A, which is kept in BCD, so A ends up holding the percentage.
40167 LD BC,25206 Set the multiplier in BC to 25206.
40170 LD HL,0 Clear the running total in HL.
40173 XOR A Clear the percentage in A.
PrintRoomsVisited_Multiply 40174 ADD HL,BC Add the multiplier to the running total.
40175 ADC A,0 Add any carry to the percentage.
40177 DAA Adjust the percentage to be a BCD number.
40178 DEC DE Decrease the count of rooms and amulet pieces by one.
40179 PUSH AF Stash the percentage so far on the stack.
40180 LD A,E Jump to PrintRoomsVisited_Store if the count has run out.
40181 OR D
40182 JR Z,PrintRoomsVisited_Store
40184 POP AF Restore the percentage so far from the stack.
40185 JR PrintRoomsVisited_Multiply Jump back to PrintRoomsVisited_Multiply to keep adding.
Round the result up and store the percentage as BCD.
PrintRoomsVisited_Store 40187 POP AF Restore the percentage from the stack.
40188 LD BC,40 Set the rounding value in BC to 40.
40191 ADD HL,BC Add the rounding value to the running total.
40192 ADC A,0 Add any carry to the percentage.
40194 DAA Adjust the percentage to be a BCD number.
40195 LD (38583),A Store the tens and units of the percentage in PreviousGameOptions.
40198 LD A,0 Set the hundreds in A to 0.
40200 ADC A,0 Add the carry flag (the percentage is 256 if it is set).
40202 DAA Adjust the hundreds to be a BCD number.
40203 LD (38582),A Store the hundreds of the percentage in Percentage_Hundreds.
Print the percentage, leaving out the hundreds digit if it is zero.
40206 LD HL,18680 Set the print position in HL to 18680 in the screen buffer.
40209 LD DE,38582 Point DE at the percentage at Percentage_Hundreds.
40212 LD B,1 Set the number of bytes to print in B to 1.
40214 LD A,(DE) Jump to PrintRoomsVisited_SkipHundreds if the hundreds are zero.
40215 AND A
40216 JR Z,PrintRoomsVisited_SkipHundreds
40218 INC B Print both bytes of the percentage.
40219 JP PrintScore_LowDigit Jump to PrintScore_LowDigit to print the low digit of the first byte and then the digits of the second byte.
PrintRoomsVisited_SkipHundreds 40222 INC HL Move the print position on past the unused hundreds digit.
40223 INC DE Move onto the tens and units of the percentage.
40224 JP PrintScore_Loop Jump to PrintScore_Loop to print both digits of the byte.
View the equivalent code in;
Prev: 39742 Up: Map Next: 40227