Prev: 27870 Up: Map Next: 28048
27871: Handle Butterfly
Used by the routine at Handler_RedBird.
Controls the bonus butterfly collectible. Butterflies flutter around specific rooms and can be collected by Percy for 2 bonus points. They appear only on certain room/level combinations defined in the table at Butterfly_Room_Table. When collected, bit 7 of the table entry is set to prevent recollection until the lives counter changes or Percy respawns.
Input
IX Pointer to the butterfly sprite data at Butterfly_X_Position
Set up the butterfly sprite and check if any collected butterflies need resetting.
Handle_Butterfly 27871 LD IX,56036 Point IX at Butterfly_X_Position (butterfly sprite data).
27875 LD (IX+3),0 Write 0 to *IX+3 (clear the butterfly sprite frame).
27879 LD A,(24510) Call Reset_Butterfly_Flags to reset collected flags if *Lives_Backup (lives backup) is unset.
27882 OR A
27883 CALL NZ,Reset_Butterfly_Flags
27886 LD A,(24499) Fetch the lives display character from *LivesDisplayCharacter into B.
27889 LD B,A
27890 LD A,(27843) Call Reset_Butterfly_Flags to reset collected flags if *Lives_Display_Previous (previous lives count) does not equal B (lives count has changed).
27893 CP B
27894 CALL NZ,Reset_Butterfly_Flags
Search the butterfly table for an entry matching the current room and level.
27897 LD A,(24517) Load the current room from *CurrentRoom into C.
27900 LD C,A
27901 LD A,(24497) Load the current level from *CurrentLevel into B.
27904 LD B,A
27905 LD HL,27846 Point HL at Butterfly_Room_Table (butterfly room/level table).
Search_Butterfly_Table 27908 LD A,(HL) Fetch the room number from *HL.
27909 CP 255 Return if the terminator byte has been found (end of table).
27911 RET Z
27912 CP C Jump to Skip_Butterfly_Entry_02 if the room number does not match C.
27913 JR NZ,Skip_Butterfly_Entry_02
27915 INC HL Advance to the level byte.
27916 LD A,(HL) Fetch the level number from *HL.
27917 CP B Jump to Skip_Butterfly_Entry_01 if the level does not match B.
27918 JR NZ,Skip_Butterfly_Entry_01
27920 BIT 7,(HL) Jump to Skip_Butterfly_Entry_01 if bit 7 of *HL is set (butterfly already collected).
27922 JR NZ,Skip_Butterfly_Entry_01
27924 JR Animate_Butterfly Jump to Animate_Butterfly (matching entry found).
Skip_Butterfly_Entry_02 27926 INC HL Advance HL by two bytes to skip this entry.
Skip_Butterfly_Entry_01 27927 INC HL Advance HL to skip this entry.
27928 JR Search_Butterfly_Table Jump back to Search_Butterfly_Table.
Matching butterfly found; animate and move it.
Animate_Butterfly 27930 PUSH HL Stash the table entry pointer on the stack.
27931 LD A,25 Set the border to yellow (25 output to port 254).
27933 OUT (254),A
27935 LD A,(24507) Call Randomise_Butterfly_Position to randomise the butterfly position if *RespawnFlag (respawn flag) is non-zero.
27938 OR A
27939 CALL NZ,Randomise_Butterfly_Position
Update the butterfly animation frame with a wing-flapping effect.
27942 LD A,(27870) Fetch the butterfly colour from *Butterfly_Direction into B.
27945 LD B,A
27946 LD A,(27868) Toggle bit 0 of *Butterfly_Wing_Toggle (wing flap toggle) and write back.
27949 XOR 1
27951 LD (27868),A
27954 ADD A,3 Add 3 to get the base butterfly frame.
27956 BIT 2,B If bit 2 of B is set, jump to Set_Butterfly_Frame; otherwise add 2 for the alternate colour variant.
27958 JR Z,Set_Butterfly_Frame
27960 ADD A,2
Set_Butterfly_Frame 27962 LD (IX+3),A Write the frame to *IX+3.
Decrement the movement delay and pick a new direction when it expires.
27965 LD HL,27869 Point HL at Butterfly_Movement_Delay (butterfly movement delay).
27968 DEC (HL) Decrement the delay counter.
27969 JR NZ,Move_Butterfly Jump to Move_Butterfly if the delay has not reached zero.
Handle_Butterfly_0 27971 CALL GenerateRandomNumber Call GenerateRandomNumber to generate a random number.
27974 PUSH AF Stash the random value on the stack.
27975 AND %00000111 Mask with 7 to get a direction.
27977 LD (27870),A Write the direction to *Butterfly_Direction.
27980 POP AF Restore the random value from the stack.
27981 LD HL,27869 Point HL at Butterfly_Movement_Delay (butterfly movement delay).
27984 RRCA Rotate right three times.
27985 RRCA
27986 RRCA
27987 AND %00001111 Mask with 15.
27989 OR %00000001 Set bit 0 to ensure a minimum delay of 1.
27991 LD (HL),A Write the new delay to *HL.
Move the butterfly in its current direction.
Move_Butterfly 27992 LD A,(27870) Fetch the direction from *Butterfly_Direction.
27995 ADD A,A Double it to form a jump table index.
27996 LD (27593),A Write the direction index to *27593.
27999 LD HL,27845 Point HL at Butterfly_Movement_Boundary (butterfly movement boundary data).
28002 LD C,(IX+0) Load the butterfly's X position into C and Y position into B from *IX+0 and *IX+1.
28005 LD B,(IX+1)
28008 CALL Direction_Jump_Table Call Direction_Jump_Table to move the butterfly in the current direction.
28011 JR C,Handle_Butterfly_0 Jump to Handle_Butterfly_0 if the move was invalid (carry set) and pick a new direction.
Check if the butterfly has collided with Percy.
28013 LD IY,56036 Point IY at Butterfly_X_Position (butterfly sprite data).
28017 LD IX,56000 Point IX at Percy_X_Position (Percy sprite data).
28021 CALL CheckEggCollision Call CheckEggCollision to check for collision with Percy.
28024 JR C,Butterfly_No_Collision Jump to Butterfly_No_Collision if carry is set (no collision).
Butterfly collected by Percy.
Butterfly_Collected 28026 POP HL Restore the table entry pointer from the stack.
28027 SET 7,(HL) Set bit 7 of *HL to mark this butterfly as collected.
28029 LD B,96 Set the sound loop counter to 96 in B.
Butterfly_Sound_Loop 28031 PUSH BC Stash the sound counter on the stack.
28032 CALL UpdateEnergyBar_Refill_Shift Call UpdateEnergyBar_Refill_Shift to play one step of the collection sound.
28035 POP BC Restore the sound counter from the stack.
28036 DJNZ Butterfly_Sound_Loop Decrease the counter and loop back to Butterfly_Sound_Loop until the sound is complete.
28038 LD A,2 Call AddToScore to add 2 points to the score.
28040 CALL AddToScore
28043 LD B,9 Set B to 9.
28045 RET Return.
Butterfly_No_Collision 28046 POP HL Restore the table entry pointer from the stack.
28047 RET Return.
Prev: 27870 Up: Map Next: 28048