Prev: 83CA Up: Map Next: 845F
83EA: 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 20 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 83EA LD A,($5E26) Fetch the room of the last creature from LastCreatureRoom and keep it in C.
83ED LD C,A
83EE LD A,($EA91) Fetch the room of the player from PlayerRoom.
83F1 CP C Jump to SpawnCreature_NewRoom if the player is in a different room.
83F2 JR NZ,SpawnCreature_NewRoom
83F4 LD HL,$5E27 Point HL at the creature delay at CreatureDelay.
83F7 LD A,(HL) Fetch the creature delay.
83F8 AND A Jump to SpawnCreature_Random if the delay is zero.
83F9 JR Z,SpawnCreature_Random
83FB DEC (HL) Decrease the delay by one.
83FC 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 83FD LD HL,$EE60 Point HL at the creatures at CreatureRecords.
8400 LD DE,$0010 DE=0010 (the size of a record).
8403 LD B,$03 B=03 (the number of records to look at).
SpawnCreature_FindLoop 8405 LD A,(HL) Fetch the sprite ID of the record.
8406 AND A Jump to SpawnCreature_Copy if the record is free.
8407 JR Z,SpawnCreature_Copy
8409 ADD HL,DE Move HL on to the next record.
840A DJNZ SpawnCreature_FindLoop Decrease the record counter by one and loop back to SpawnCreature_FindLoop until all three records have been checked.
840C RET Return.
Copy the default creature into the free record.
SpawnCreature_Copy 840D EX DE,HL Swap DE and HL, so that DE is the free record.
840E LD HL,$8B6A Point HL at the creature defaults at CreatureDefaults.
8411 LD BC,$0010 BC=0010 (the size of a record).
8414 PUSH DE Stash the record on the stack.
8415 LDIR Copy the default creature into the record.
8417 POP HL Restore the record from the stack.
8418 PUSH HL Stash the record on the stack again.
8419 INC HL Move HL on to the room of the creature.
841A LD A,($EA91) Fetch the room of the player from PlayerRoom.
841D LD (HL),A Update the room of the creature.
841E INC HL Move HL on to offset 02 of the creature.
841F 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.
8420 LD HL,$8B7A Point HL at the creatures at Creatures.
8423 LD A,($5C78) Fetch FRAMES.
8426 AND %00001111 Keep only bits 0-3.
8428 LD E,A Set the offset in E to the bottom four bits of FRAMES.
8429 LD D,$00 Clear the high byte of the offset in D.
842B ADD HL,DE Find the entry in the table of creatures.
842C LD A,(HL) Fetch the sprite ID of the creature to make.
842D POP HL Restore the address of offset 02 from the stack.
842E LD (HL),A Set the type of the creature at offset 02.
Choose a random X and Y position inside the room.
842F LD DE,($5E1D) Fetch the size of the room from CurrentRoom_Width.
8433 LD B,E Set the limit for the X position in B to the width of the room.
8434 CALL GetRandomCoordinate Call GetRandomCoordinate to pick a random coordinate.
8437 LD (HL),A Update the X position of the creature.
8438 LD B,D Set the limit for the Y position in B to the height of the room.
8439 CALL GetRandomCoordinate Call GetRandomCoordinate to pick a random coordinate.
843C LD (HL),A Update the Y position of the creature.
Give the creature a random velocity and draw it.
843D POP DE Restore the address of the record from the stack.
843E PUSH IX Stash the current entity on the stack.
8440 LD IX,$0000 Make IX point at the new creature (copy DE to IX).
8444 ADD IX,DE
8446 CALL RandomiseVelocity Call RandomiseVelocity to give the creature a new random velocity.
8449 CALL DrawEntity Call DrawEntity to draw the sparkle.
844C POP IX Restore the current entity from the stack.
844E RET Return.
The player is in a new room, so remember the room and start the delay.
SpawnCreature_NewRoom 844F LD ($5E26),A Update the room of the last creature in LastCreatureRoom.
8452 LD A,$20 Write 20 to CreatureDelay.
8454 LD ($5E27),A
8457 RET Return.
There is no delay, so only make a creature one time in sixteen.
SpawnCreature_Random 8458 LD A,R Fetch a random number (from the refresh register).
845A AND %00001111 Keep only bits 0-3.
845C RET NZ Return if the result is not zero.
845D JR SpawnCreature_Find Jump back to SpawnCreature_Find to look at the next record.
Prev: 83CA Up: Map Next: 845F