Prev: 43455 Up: Map Next: 43618
43501: Create Cactus
Used by the routine at CreateTownObjects.
Called every frame in town. Cacti stand in eight fixed cells of the town (see CactusPositions). When the sheriff comes within two cells of one, and the seventh object slot is free, the routine puts a cactus (an object of type 19) in the slot.
Return if the seventh slot is in use.
CreateCactus 43501 LD A,(41693) Fetch the type of the object in the seventh slot.
43504 AND A Return if the slot is in use.
43505 RET NZ
Find a cactus that is within two cells of the sheriff.
43506 LD A,(41599) Fetch the horizontal cell of the sheriff and keep it in E.
43509 LD E,A
43510 LD A,(41601) Fetch the vertical cell of the sheriff.
43513 RLCA Shift it into the high nibble.
43514 RLCA
43515 RLCA
43516 RLCA
43517 AND %11110000 Keep the high nibble.
43519 LD D,A Keep the vertical cell in the high nibble of D.
43520 LD HL,43638 Point HL at CactusPositions.
43523 LD B,(HL) Fetch the number of cacti.
CreateCactus_Next 43524 INC HL Move on to the next cactus.
CreateCactus_Loop 43525 LD A,(HL) Fetch the position of the cactus.
43526 AND %00001111 Keep the horizontal cell.
43528 SUB E Subtract the horizontal cell of the sheriff.
43529 JR NC,CreateCactus_CompareHorizontal Jump to CreateCactus_CompareHorizontal unless the difference is negative.
43531 NEG Make the difference positive.
CreateCactus_CompareHorizontal 43533 CP 2 Jump to CreateCactusSkip if the cactus is two or more cells away horizontally.
43535 JR NC,CreateCactusSkip
43537 LD A,(HL) Fetch the position of the cactus again.
43538 AND %11110000 Keep the vertical cell.
43540 SUB D Subtract the vertical cell of the sheriff.
43541 JR NC,CreateCactus_CompareVertical Jump to CreateCactus_CompareVertical unless the difference is negative.
43543 NEG Make the difference positive.
CreateCactus_CompareVertical 43545 CP 32 Jump to CreateCactusSkip if the cactus is two or more cells away vertically.
43547 JR NC,CreateCactusSkip
The sheriff is close to this cactus, so make it.
CreateCactusFound 43549 LD A,(HL) Fetch the position of the cactus.
43550 PUSH AF Stash it on the stack.
43551 LD DE,41693 Point DE at ObjectSlot_07.
43554 PUSH DE Copy DE to IY (using the stack).
43555 POP IY
43557 LD HL,43618 Point HL at CactusTemplate.
43560 LD BC,16 Set the size of an object slot in BC.
43563 LDIR Copy the template into the slot.
43565 POP AF Retrieve the position of the cactus.
43566 LD C,A Keep the cactus position in C.
43567 AND %00001111 Keep the horizontal cell.
43569 LD (IY+2),A Set the horizontal cell of the cactus.
43572 LD A,C Fetch the position again.
43573 RRCA Shift the vertical cell into the bottom of the byte.
43574 RRCA
43575 RRCA
43576 RRCA
43577 AND %00001111 Keep the vertical cell.
43579 LD (IY+4),A Set the vertical cell of the cactus.
43582 LD A,C Fetch the position again.
43583 RLCA Swap the two halves of the position.
43584 RLCA
43585 RLCA
43586 RLCA
43587 AND %01110000 Keep three bits which came from the horizontal cell.
43589 ADD A,64 Add 64 to give the horizontal position within the cell.
43591 LD (IY+1),A Set the horizontal position of the cactus within its cell.
43594 LD A,C Fetch the position again.
43595 CPL Invert it.
43596 AND %01110000 Keep three bits which came from the vertical cell.
43598 ADD A,64 Add 64 to give the vertical position within the cell.
43600 LD (IY+3),A Set the vertical position of the cactus within its cell.
43603 XOR (IY+1) Combine it with the horizontal position.
43606 RLCA Rotate bit 4 up into bit 6.
43607 RLCA
43608 AND %01000000 Keep bit 6, which chooses whether the sprite is flipped.
43610 LD (IY+7),A Set the flags of the cactus.
43613 RET Return.
CreateCactusSkip 43614 INC HL Move on to the next cactus.
43615 DJNZ CreateCactus_Loop Loop back until every cactus has been checked.
43617 RET Return.
Prev: 43455 Up: Map Next: 43618