Prev: BD99 Up: Map Next: BE4E
BDCD: 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 10 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+0F) 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 BDCD LD BC,$0540 Set the offset that puts the robot five and a quarter cells from the start of the view.
BDD0 RES 0,(IX+$0F) Mark the entity as not visible.
BDD4 LD L,(IX+$01) Fetch the horizontal position of the entity.
BDD7 LD H,(IX+$02)
BDDA LD DE,($9691) Fetch the horizontal position of the robot.
BDDE AND A Find the distance from the robot...
BDDF SBC HL,DE
BDE1 ADD HL,BC ...and add the offset.
BDE2 LD A,H Keep it within the width of the map (40 cells).
BDE3 AND $3F
BDE5 CP $10 Return, as the entity cannot be seen, if it is further than 0F cells from the start of the view.
BDE7 RET NC
BDE8 LD H,A Keep the cell number.
BDE9 PUSH HL Save the horizontal distance.
BDEA LD L,(IX+$03) Fetch the vertical position of the entity.
BDED LD H,(IX+$04)
BDF0 LD DE,($9693) Fetch the vertical position of the robot.
BDF4 AND A Find the distance from the robot.
BDF5 SBC HL,DE
BDF7 LD A,H Keep it within the height of the map (40 cells).
BDF8 AND $3F
BDFA LD H,A
BDFB EX DE,HL Move the vertical distance into DE.
BDFC POP HL Restore the horizontal distance, keeping a copy of it too.
BDFD PUSH HL
BDFE ADD HL,DE Add the vertical distance.
BDFF CALL DivideBySixteen Divide it by sixteen using DivideBySixteen, to find the height.
BE02 LD A,H Jump to throw the entity away if the height does not fit in a byte.
BE03 AND $03
BE05 JR NZ,AddEntity_Hidden
BE07 LD A,L Jump to throw the entity away if the height is more than A7.
BE08 CP $A8
BE0A JR NC,AddEntity_Hidden
BE0C LD IY,($962A) Fetch the address of the next free entry of the draw list.
BE10 LD (IY+$02),A Store the height in it.
BE13 POP HL Restore the horizontal distance.
BE14 AND A Subtract the vertical distance...
BE15 SBC HL,DE
BE17 CALL DivideBySixteen ...and divide it by sixteen, to find the horizontal position.
BE1A LD A,L Add eight to it...
BE1B ADD A,$08
BE1D LD L,A
BE1E LD A,H ...carrying on into the high byte.
BE1F ADC A,$00
BE21 AND $03 Return, as the entity cannot be seen, if it is out of range.
BE23 RET NZ
BE24 LD A,L Return, as the entity cannot be seen, if it is further right than B7.
BE25 CP $B8
BE27 RET NC
BE28 SUB $08 Take the eight off again. If that goes below zero then subtract 40.
BE2A JR NC,AddEntity_StoreX
BE2C SUB $40
AddEntity_StoreX BE2E LD (IY+$01),A Store the horizontal position in the entry.
BE31 LD A,(IX+$00) Copy the type of the entity into the entry.
BE34 LD (IY+$00),A
BE37 INC IY Move on to the next entry.
BE39 INC IY
BE3B INC IY
BE3D LD (IY+$00),$00 End the list after it.
BE41 LD ($962A),IY Store the address of the next free entry.
BE45 SET 0,(IX+$0F) Mark the entity as visible.
BE49 SCF Set the carry flag to show that the entity was added, and return.
BE4A RET
AddEntity_Hidden BE4B POP HL Throw away the horizontal distance.
BE4C AND A Clear the carry flag, and return.
BE4D RET
Prev: BD99 Up: Map Next: BE4E