Prev: 86E5 Up: Map Next: 876B
86F9: Start Level
Used by the routine at RestoreBottles.
Finds the data of the current level and, when it is entered at StartLevel, places the player at the start of the level (or at the position where the player last restarted) and sets the view to match.
Input
C 01 to place the player, otherwise only find the level data
StartLevel 86F9 LD C,$01 Set the placement flag in C to 01.
This entry point is used by the routines at SwitchPlayer and CollectBottle.
StartLevel_FindData 86FB LD A,($DEDF) Fetch the number of the current level from LevelNumber.
86FE ADD A,A Multiply it by four, as there are four bytes for each level in the table at LevelTable.
86FF ADD A,A
8700 LD E,A Move it into E.
8701 LD D,$00 Clear D.
8703 LD HL,$86E5 Point HL at LevelTable.
8706 ADD HL,DE Add the offset of the level to find its entry.
8707 LD E,(HL) Fetch the low byte of the address of the data of the level.
8708 INC HL Move onto the high byte.
8709 LD D,(HL) Fetch the high byte of the address of the data of the level.
870A LD ($5CFE),DE Store the address in LevelHeaderAddress.
870E DEC C Decrease the placement flag by one.
870F RET NZ Return unless the player is to be placed.
Place the player at the start position of the level, or at the position of the last restart.
8710 PUSH DE Copy the address of the start position of the level into IX (using the stack).
8711 POP IX
8713 PUSH DE Stash the address of the start position of the level on the stack.
8714 LD A,($DEF7) Fetch the restart flag from DEF7.
8717 AND A Test it.
8718 JR Z,StartLevel_Place Jump to StartLevel_Place if the player has not restarted.
871A LD IX,$DEF5 Point IX at the position of the last restart at RestartPosition instead.
StartLevel_Place 871E LD A,(IX+$00) Fetch the first byte of the start position.
8721 AND %11110000 Keep the upper four bits.
8723 LD H,A Copy them into H.
8724 LD L,$01 L is 01.
8726 LD C,H Copy the upper four bits into C.
8727 LD B,$00 Clear B.
8729 LD ($B548),HL Write HL in the player entity at B548.
872C LD A,(IX+$00) Fetch the first byte of the start position again.
872F AND %00001111 Keep the lower four bits.
8731 ADD A,$20 Add 20.
8733 LD L,A Copy the result into L.
8734 LD H,(IX+$01) Fetch the second byte of the start position into H.
8737 LD ($B542),HL Write HL in the player entity at B542.
873A LD A,L L moves into A.
873B LD L,H H moves into L.
873C LD H,A A moves into H.
873D LD DE,$FF98 DE is FF98 (minus 68).
8740 ADD HL,DE Subtract 68 from the swapped position.
8741 AND A Subtract the upper four bits of the first byte of the start position.
8742 SBC HL,BC
8744 LD ($5CE2),HL Store the result in ViewPosition.
8747 LD L,(IX+$02) Fetch the third byte of the start position into L.
874A LD H,(IX+$03) Fetch the fourth byte of the start position into H.
874D LD ($B545),HL Write HL in the player entity at B545.
8750 LD A,L L moves into A.
8751 LD L,H H moves into L.
8752 LD H,A A moves into H.
8753 ADD HL,DE Subtract 68 from the swapped position.
8754 ADD HL,BC Add the upper four bits of the first byte of the start position.
8755 LD ($5CE4),HL Store the result in 5CE4.
8758 LD A,($B546) Fetch the byte of the player entity at B546.
875B ADD A,$04 Add 04 to it.
875D LD ($B546),A Store it again.
8760 LD A,($B543) Fetch the byte of the player entity at B543.
8763 ADD A,$02 Add 02 to it.
8765 LD ($B543),A Store it again.
8768 POP IX Bring back IX from the stack.
876A RET Return.
Prev: 86E5 Up: Map Next: 876B