Prev: 48638 Up: Map Next: 48950
48863: 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 128 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 10288 and divided by 65536, so that visiting about 637 cells (or a few less when monsters have been killed) gives 100 percent.
Count the visited cells.
CalculatePercentage 48863 LD HL,0 Start the count of cells at zero.
48866 LD DE,48142 Point at the bitmap of visited cells.
48869 LD C,128 Set the number of bytes in the bitmap to 128.
CalculatePercentage_ByteLoop 48871 LD A,(DE) Fetch a byte of the bitmap and move on to the next.
48872 INC DE
48873 LD B,8 Set a counter for the eight bits in the byte.
CalculatePercentage_BitLoop 48875 RRCA Check the next bit of the bitmap, and move it into the carry flag.
48876 JR NC,CalculatePercentage_NextBit
48878 INC HL Count the visited cell.
CalculatePercentage_NextBit 48879 DJNZ CalculatePercentage_BitLoop Loop back until all eight bits have been counted.
48881 DEC C Loop back until every byte of the bitmap has been counted.
48882 JR NZ,CalculatePercentage_ByteLoop
Add three for every monster that has been killed.
48884 LD IY,48478 Start with the first of the four monsters.
48888 LD B,4 Set a counter for the four monsters.
CalculatePercentage_MonsterLoop 48890 LD A,(IY+0) Fetch the type of the monster and subtract 96.
48893 SUB 96
48895 CP 16 Jump to CalculatePercentage_NextMonster if the monster is still alive (its type is 96 to 111).
48897 JR C,CalculatePercentage_NextMonster
48899 LD DE,3 Add three to the count of cells.
48902 ADD HL,DE
CalculatePercentage_NextMonster 48903 LD DE,16 Set the size of a monster's entry in the object table to 16 bytes.
48906 ADD IY,DE Move on to the next monster.
48908 DJNZ CalculatePercentage_MonsterLoop Loop back until all four monsters have been counted.
Multiply the count by 10288 and divide by 65536, using BCD arithmetic for the high byte.
48910 LD BC,10288 Set the value to add on each pass.
48913 XOR A Start the BCD result at zero.
48914 EX DE,HL Make the count of cells the number of times to add.
48915 LD HL,0 Start the running total at zero.
CalculatePercentage_MultiplyLoop 48918 ADD HL,BC Add the value to the total.
48919 ADC A,0 Add any carry to the BCD result.
48921 DAA Adjust the BCD result.
48922 PUSH AF Stash the result on the stack.
48923 DEC DE Decrease the count.
48924 LD A,E Jump to CalculatePercentage_Finish if the count has reached zero.
48925 OR D
48926 JR Z,CalculatePercentage_Finish
48928 POP AF Restore the result from the stack.
48929 JR CalculatePercentage_MultiplyLoop Jump to CalculatePercentage_MultiplyLoop.
CalculatePercentage_Finish 48931 POP AF Restore the result from the stack.
48932 LD BC,144 Add 144 to the total, which rounds the percentage.
48935 ADD HL,BC Add any carry to the BCD result.
48936 ADC A,0
48938 DAA
48939 LD (48126),A Set the tens and units of the percentage.
48942 LD A,0 Set the hundreds digit to one if the percentage has overflowed.
48944 ADC A,0
48946 LD (48125),A Set the hundreds digit of the percentage.
48949 RET Return.
Prev: 48638 Up: Map Next: 48950