Prev: 33738 Up: Map Next: 33887
33770: Spawn Creature
Used by the routine at HandlerKnight.
Called by the handlers of the characters. When the player enters a new room a delay of 32 is started (see CreatureDelay). Once the delay has run out, and then at random about one time in sixteen, a creature is created in a free one of the first three 16-byte records at CreatureRecords. The new record is a copy of CreatureDefaults, in the current room and with a random position. It starts as a sparkle (see HandlerSparkle) that turns into the creature chosen by FRAMES from Creatures.
See if the player is in a different room to the last one a creature was made in.
SpawnCreature 33770 LD A,(24102) Fetch the room of the last creature from LastCreatureRoom and keep it in C.
33773 LD C,A
33774 LD A,(60049) Fetch the room of the player from PlayerRoom.
33777 CP C Jump to SpawnCreature_NewRoom if the player is in a different room.
33778 JR NZ,SpawnCreature_NewRoom
33780 LD HL,24103 Point HL at the creature delay at CreatureDelay.
33783 LD A,(HL) Fetch the creature delay.
33784 AND A Jump to SpawnCreature_Random if the delay is zero.
33785 JR Z,SpawnCreature_Random
33787 DEC (HL) Decrease the delay by one.
33788 RET NZ Return if the delay is not zero yet.
Look for a free record for the creature, in the first three of the 16-byte records.
SpawnCreature_Find 33789 LD HL,61024 Point HL at the creatures at CreatureRecords.
33792 LD DE,16 DE=0016 (the size of a record).
33795 LD B,3 B=3 (the number of records to look at).
SpawnCreature_FindLoop 33797 LD A,(HL) Fetch the sprite ID of the record.
33798 AND A Jump to SpawnCreature_Copy if the record is free.
33799 JR Z,SpawnCreature_Copy
33801 ADD HL,DE Move HL on to the next record.
33802 DJNZ SpawnCreature_FindLoop Decrease the record counter by one and loop back to SpawnCreature_FindLoop until all three records have been checked.
33804 RET Return.
Copy the default creature into the free record.
SpawnCreature_Copy 33805 EX DE,HL Swap DE and HL, so that DE is the free record.
33806 LD HL,35690 Point HL at the creature defaults at CreatureDefaults.
33809 LD BC,16 BC=0016 (the size of a record).
33812 PUSH DE Stash the record on the stack.
33813 LDIR Copy the default creature into the record.
33815 POP HL Restore the record from the stack.
33816 PUSH HL Stash the record on the stack again.
33817 INC HL Move HL on to the room of the creature.
33818 LD A,(60049) Fetch the room of the player from PlayerRoom.
33821 LD (HL),A Update the room of the creature.
33822 INC HL Move HL on to offset 2 of the creature.
33823 PUSH HL Stash it on the stack.
The creature that it turns into is chosen from the table using the bottom four bits of FRAMES.
33824 LD HL,35706 Point HL at the creatures at Creatures.
33827 LD A,(23672) Fetch FRAMES.
33830 AND %00001111 Keep only bits 0-3.
33832 LD E,A Set the offset in E to the bottom four bits of FRAMES.
33833 LD D,0 Clear the high byte of the offset in D.
33835 ADD HL,DE Find the entry in the table of creatures.
33836 LD A,(HL) Fetch the sprite ID of the creature to make.
33837 POP HL Restore the address of offset 2 from the stack.
33838 LD (HL),A Set the type of the creature at offset 2.
Choose a random X and Y position inside the room.
33839 LD DE,(24093) Fetch the size of the room from CurrentRoom_Width.
33843 LD B,E Set the limit for the X position in B to the width of the room.
33844 CALL GetRandomCoordinate Call GetRandomCoordinate to pick a random coordinate.
33847 LD (HL),A Update the X position of the creature.
33848 LD B,D Set the limit for the Y position in B to the height of the room.
33849 CALL GetRandomCoordinate Call GetRandomCoordinate to pick a random coordinate.
33852 LD (HL),A Update the Y position of the creature.
Give the creature a random velocity and draw it.
33853 POP DE Restore the address of the record from the stack.
33854 PUSH IX Stash the current entity on the stack.
33856 LD IX,0 Make IX point at the new creature (copy DE to IX).
33860 ADD IX,DE
33862 CALL RandomiseVelocity Call RandomiseVelocity to give the creature a new random velocity.
33865 CALL DrawEntity Call DrawEntity to draw the sparkle.
33868 POP IX Restore the current entity from the stack.
33870 RET Return.
The player is in a new room, so remember the room and start the delay.
SpawnCreature_NewRoom 33871 LD (24102),A Update the room of the last creature in LastCreatureRoom.
33874 LD A,32 Write 32 to CreatureDelay.
33876 LD (24103),A
33879 RET Return.
There is no delay, so only make a creature one time in sixteen.
SpawnCreature_Random 33880 LD A,R Fetch a random number (from the refresh register).
33882 AND %00001111 Keep only bits 0-3.
33884 RET NZ Return if the result is not zero.
33885 JR SpawnCreature_Find Jump back to SpawnCreature_Find to look at the next record.
Prev: 33738 Up: Map Next: 33887