Prev: 52105 Up: Map Next: 52233
52139: Create Creature
Used by the routine at RunGame.
Makes a random creature, when the countdown has finished, and there is room for it. It is called once for each frame.
The routine does not make a creature in a room that has a special object in it. Otherwise it counts down the timer at SpawnTimer (which is set by SetCreatureTimer) and, when that is over, tries once for every four frames (judging by the random seed) to make a creature in one of the two object records reserved for creatures. If both are being used, nothing is done.
The creature that is made comes from the template of an object at CreatureTemplate, which is copied into the free record. After that the template is changed to a new creature of a random type, in a random place that is high above the floor, ready for the next time. If it is found to overlap another object there, its type is set to zero instead, which makes the object that is copied the next time one that does not exist.
Return if the room has a special object or the timer is still running.
CreateCreature 52139 LD A,(42818) Fetch the special object flag.
52142 AND A Return if there is a special object in the room.
52143 RET NZ
52144 LD HL,42813 Take the creature timer, SpawnTimer.
52147 DEC (HL) Decrease the timer.
52148 RET NZ Return if it is not zero.
52149 LD (HL),1 Set the timer to one, so that it is zero again after the next frame unless it is changed.
Try once every four frames, then find a free creature slot.
52151 LD A,(42765) Fetch the random seed.
52154 AND %00000011 Keep the lowest two bits.
52156 RET NZ Return unless they are zero.
52157 CALL SetCreatureTimer Call SetCreatureTimer to set the timer.
52160 LD HL,42991 Start at the first of the objects for creatures, ObjectsTable_Creatures.
52163 LD BC,32 Set the size of an object, 32, in BC.
52166 LD DE,52241 Take the template of the creature, CreatureTemplate.
52169 LD A,(HL) Fetch the type of the object.
52170 AND A Jump to CreateCreature_Copy if it is free.
52171 JR Z,CreateCreature_Copy
52173 ADD HL,BC Move on to the second object for creatures.
52174 LD A,(HL) Fetch the type of the object.
52175 AND A Return if it is not free either.
52176 RET NZ
Copy the template, then pick a new creature for next time.
CreateCreature_Copy 52177 EX DE,HL Put the template against the free object.
52178 PUSH HL Take the template as the object being filled in (using the stack).
52179 POP IX
52181 LDIR Copy the template into the free object.
52183 LD A,(42765) Fetch the random seed.
52186 AND %00101111 Keep bits 0-3 and bit 5.
52188 ADD A,104 Add the start of the area for creatures, 104.
52190 LD (IX+1),A Set the x position of the creature in the template.
52193 LD A,R Fetch a random number from the refresh register.
52195 AND %00101111 Keep bits 0-3 and bit 5.
52197 ADD A,104 Add the start of the area for creatures, 104.
52199 LD (IX+2),A Set the y position of the creature in the template.
52202 LD A,(42765) Fetch the random seed.
52205 RRA Move bits 3-5 to bits 0-2.
52206 RRA
52207 RRA
52208 AND %00000111 Keep them.
52210 LD L,A Put the offset in L.
52211 LD H,0 Clear H.
52213 LD DE,52233 Take the types of creatures, CreatureTypes.
52216 ADD HL,DE Add the offset.
52217 LD A,(HL) Fetch the type of creature.
52218 LD (IX+0),A Set the type of the creature in the template.
52221 CALL CalculatePixelCoordinates Call CalculatePixelCoordinates to work out where the creature is on the screen.
52224 CALL TestObjectIntersections Call TestObjectIntersections to find out if the creature overlaps an object where it is.
52227 RET NC Return if it does not.
Clear the template if the new place overlaps another object.
52228 LD (IX+0),0 Set the type in the template to zero.
52232 RET Return.
Prev: 52105 Up: Map Next: 52233