Prev: AF56 Up: Map Next: B079
AFDA: Spawn Flying Creature
Used by the routine at Run_Game.
Lets a flying creature into the room every now and then. Nothing happens unless the sum of the tick counter and the frame counter is a multiple of eight.
If one of the four creature records that start at First_Creature_Record is free, it is filled in with a type that comes from bits 2 and 4 of the tick counter: 30 (eagle), 34 (gargoyle), 40 (vulture) or 44 (jellyfish). Only the vultures and the jellyfish appear until the flag at Creature_Spawn_Flag has been set, which happens when Sabreman kills a guardian.
The creature starts in a room next to the one that Sabreman is in. It is on the left or the right if the room has a gap in that wall (the choice is made from bit 0 of the tick counter), and it is above or below otherwise. Its position inside the room, and its attribute, come from the frame counter.
Handle_Flying_Creature then makes the creature fly into the room that Sabreman is in.
Spawn_Flying_Creature AFDA LD A,($8ED6) Fetch the tick counter from GameTimer and keep it in B.
AFDD LD B,A
AFDE LD A,($5C78) Fetch the low byte of FRAMES.
AFE1 ADD A,B Add the tick counter to make a pseudo-random number.
AFE2 AND %00000111 Keep only the low three bits.
AFE4 RET NZ Return unless the result is zero (a creature can only appear on one pass in eight).
AFE5 LD HL,$8FDA Point at the first of the four creature records (First_Creature_Record).
AFE8 LD DE,$0010 Set the size of a record to 10.
AFEB LD B,$04 Set the number of records to 04.
Spawn_Flying_Creature_Find_Record AFED LD A,(HL) Fetch the type of the record.
AFEE AND A Jump to Spawn_Flying_Creature_Choose_Type if the record is not in use (the type is zero).
AFEF JR Z,Spawn_Flying_Creature_Choose_Type
AFF1 ADD HL,DE Move on to the next record.
AFF2 DJNZ Spawn_Flying_Creature_Find_Record Loop back until all of the records have been tried.
AFF4 RET Return as all of the records are in use.
Spawn_Flying_Creature_Choose_Type AFF5 LD A,($AAAC) Fetch the flags of the room that Sabreman is in.
AFF8 LD E,A Store them in E.
AFF9 LD A,($8EF9) Fetch the creature spawn flag.
AFFC AND A Test the flag.
AFFD LD A,($8ED6) Fetch the tick counter.
B000 JR NZ,Spawn_Flying_Creature_Store_Type Jump to Spawn_Flying_Creature_Store_Type if the flag is set.
B002 OR %00010000 Set bit 4, which limits the choice to the vulture and the jellyfish.
Spawn_Flying_Creature_Store_Type B004 PUSH AF Stash the tick counter on the stack.
B005 AND %00010100 Keep only bits 2 and 4.
B007 ADD A,$30 Add 30 to make the type.
B009 LD (HL),A Store the type in the record.
B00A POP AF Restore the tick counter.
B00B INC HL Move on to the x position.
B00C BIT 0,A Jump to Spawn_Flying_Creature_Test_Left if bit 0 of the tick counter is set.
B00E JR NZ,Spawn_Flying_Creature_Test_Left
Bit 0 of the tick counter is reset, so use the right of the room if it has no right wall, and the left otherwise.
B010 BIT 2,E Jump to Spawn_Flying_Creature_From_Right if the room has no right wall.
B012 JR Z,Spawn_Flying_Creature_From_Right
B014 BIT 6,E Jump to Spawn_Flying_Creature_Vertically if the room has a left wall too.
B016 JR NZ,Spawn_Flying_Creature_Vertically
The creature comes in from the room on the left.
Spawn_Flying_Creature_From_Left B018 AND %01111111 Keep only the low seven bits of the tick counter.
B01A ADD A,$48 Add 48 to make the x position.
B01C LD (HL),A Store the x position.
B01D INC HL Move on to the room x.
B01E LD A,($8F1C) Fetch the room x of Sabreman.
B021 DEC A Subtract one to get the room on the left.
Spawn_Flying_Creature_Store_Room_X B022 LD (HL),A Store the room x.
B023 INC HL Move on to the y position.
B024 LD A,($5C78) Fetch the low byte of the frame counter.
B027 AND %00111111 Keep only the low six bits.
B029 ADD A,$40 Add 40 to make the y position.
B02B LD (HL),A Store the y position.
B02C INC HL Move on to the room y.
B02D LD A,($8F1E) Fetch the room y of Sabreman.
B030 LD (HL),A Store it as the room y of the creature.
B031 INC HL Move on to the attribute.
B032 LD A,($5C78) Fetch the low byte of the frame counter.
B035 AND %00000011 Keep only the low two bits.
B037 ADD A,$42 Add 42 to make a bright attribute.
B039 LD (HL),A Store the attribute of the creature.
B03A RET Return.
Spawn_Flying_Creature_Test_Left B03B BIT 6,E Jump to Spawn_Flying_Creature_From_Left if the room has no left wall.
B03D JR Z,Spawn_Flying_Creature_From_Left
B03F BIT 2,E Jump to Spawn_Flying_Creature_Vertically if the room has a right wall.
B041 JR NZ,Spawn_Flying_Creature_Vertically
The creature comes in from the room on the right.
Spawn_Flying_Creature_From_Right B043 AND %00111111 Keep only the low six bits of the tick counter.
B045 ADD A,$40 Add 40 to make the x position.
B047 LD (HL),A Store the x position.
B048 INC HL Move on to the room x.
B049 LD A,($8F1C) Fetch the room x of Sabreman.
B04C INC A Add one to get the room on the right.
B04D JR Spawn_Flying_Creature_Store_Room_X Jump to Spawn_Flying_Creature_Store_Room_X to store the room x.
The creature comes in from the room above or below.
Spawn_Flying_Creature_Vertically B04F AND %01111111 Keep only the low seven bits of the tick counter.
B051 ADD A,$48 Add 48 to make the x position.
B053 LD (HL),A Store the x position.
B054 INC HL Move on to the room x.
B055 LD A,($8F1C) Fetch the room x of Sabreman.
B058 LD (HL),A Store it as the room x of the creature.
B059 INC HL Move on to the y position.
B05A LD A,($5C78) Fetch the low byte of the frame counter.
B05D AND %00111111 Keep only the low six bits.
B05F ADD A,$40 Add 40 to make the y position.
B061 LD (HL),A Store the y position.
B062 INC HL Move on to the room y.
B063 LD A,($8F1E) Fetch the room y of Sabreman.
B066 BIT 0,E Jump to Spawn_Flying_Creature_From_Above if the room has no ceiling.
B068 JR Z,Spawn_Flying_Creature_From_Above
B06A INC A Add one to get the room below.
B06B JR Spawn_Flying_Creature_Store_Room_Y Jump to Spawn_Flying_Creature_Store_Room_Y to store the room y.
Spawn_Flying_Creature_From_Above B06D DEC A Subtract one to get the room above.
Spawn_Flying_Creature_Store_Room_Y B06E LD (HL),A Store the room y.
B06F INC HL Move on to the attribute.
B070 LD A,($5C78) Fetch the low byte of the frame counter.
B073 AND %00000011 Keep only the low two bits.
B075 ADD A,$42 Add 42 to make a bright attribute.
B077 LD (HL),A Store the attribute of the creature.
B078 RET Return.
Prev: AF56 Up: Map Next: B079