Prev: 48537 Up: Map Next: 48718
48589: Add Entity
Adds an entity to the draw list, if it can be seen. The view is centred on the robot (the first entity), and an entity can only be seen if it is within about 16 cells of it. The distances between the entity and the robot in the two directions are projected onto the screen: the sum of them gives the height of the entity above the bottom of the buffer and the difference gives its horizontal position, both in units of one sixteenth of a cell. If it is added then bit 0 of (IX+15) is set. The draw list entry that is added is made of the type of the entity, the horizontal position and the height.
Input
IX Entity
Output
F Carry set if the entity was added
AddEntity 48589 LD BC,1344 Set the offset that puts the robot five and a quarter cells from the start of the view.
48592 RES 0,(IX+15) Mark the entity as not visible.
48596 LD L,(IX+1) Fetch the horizontal position of the entity.
48599 LD H,(IX+2)
48602 LD DE,(38545) Fetch the horizontal position of the robot.
48606 AND A Find the distance from the robot...
48607 SBC HL,DE
48609 ADD HL,BC ...and add the offset.
48610 LD A,H Keep it within the width of the map (64 cells).
48611 AND 63
48613 CP 16 Return, as the entity cannot be seen, if it is further than 15 cells from the start of the view.
48615 RET NC
48616 LD H,A Keep the cell number.
48617 PUSH HL Save the horizontal distance.
48618 LD L,(IX+3) Fetch the vertical position of the entity.
48621 LD H,(IX+4)
48624 LD DE,(38547) Fetch the vertical position of the robot.
48628 AND A Find the distance from the robot.
48629 SBC HL,DE
48631 LD A,H Keep it within the height of the map (64 cells).
48632 AND 63
48634 LD H,A
48635 EX DE,HL Move the vertical distance into DE.
48636 POP HL Restore the horizontal distance, keeping a copy of it too.
48637 PUSH HL
48638 ADD HL,DE Add the vertical distance.
48639 CALL DivideBySixteen Divide it by sixteen using DivideBySixteen, to find the height.
48642 LD A,H Jump to throw the entity away if the height does not fit in a byte.
48643 AND 3
48645 JR NZ,AddEntity_Hidden
48647 LD A,L Jump to throw the entity away if the height is more than 167.
48648 CP 168
48650 JR NC,AddEntity_Hidden
48652 LD IY,(38442) Fetch the address of the next free entry of the draw list.
48656 LD (IY+2),A Store the height in it.
48659 POP HL Restore the horizontal distance.
48660 AND A Subtract the vertical distance...
48661 SBC HL,DE
48663 CALL DivideBySixteen ...and divide it by sixteen, to find the horizontal position.
48666 LD A,L Add eight to it...
48667 ADD A,8
48669 LD L,A
48670 LD A,H ...carrying on into the high byte.
48671 ADC A,0
48673 AND 3 Return, as the entity cannot be seen, if it is out of range.
48675 RET NZ
48676 LD A,L Return, as the entity cannot be seen, if it is further right than 183.
48677 CP 184
48679 RET NC
48680 SUB 8 Take the eight off again. If that goes below zero then subtract 64.
48682 JR NC,AddEntity_StoreX
48684 SUB 64
AddEntity_StoreX 48686 LD (IY+1),A Store the horizontal position in the entry.
48689 LD A,(IX+0) Copy the type of the entity into the entry.
48692 LD (IY+0),A
48695 INC IY Move on to the next entry.
48697 INC IY
48699 INC IY
48701 LD (IY+0),0 End the list after it.
48705 LD (38442),IY Store the address of the next free entry.
48709 SET 0,(IX+15) Mark the entity as visible.
48713 SCF Set the carry flag to show that the entity was added, and return.
48714 RET
AddEntity_Hidden 48715 POP HL Throw away the horizontal distance.
48716 AND A Clear the carry flag, and return.
48717 RET
Prev: 48537 Up: Map Next: 48718