Prev: 48571 Up: Map Next: 48961
48641: Add Object Entity
Used by the routine at DrawObject.
Called by DrawObject for an object that can be an entity. Checks that the object is in view and, if it is of kind 2 3 or 4 makes an entity for it in the pool of four entities at 46704. The entity is of type 12 for kind 2 and type 16 for the other two. Any object that is in view is also recorded with RecordObject.
Input
IX The object, which is three bytes
HL The screen column of the object
Check that the object is not off the left or right edge of the view.
AddObjectEntity 48641 BIT 7,H Jump to AddObjectEntity_RightEdge if bit 7 of the high byte of the screen column is set.
48643 JR NZ,AddObjectEntity_RightEdge
48645 LD BC,40 BC is 40 the margin on the left of the view.
48648 ADD HL,BC Add the margin to the screen column.
48649 RL H Rotate bit 7 of the high byte into the carry flag.
48651 RET NC Return if the object is off the left edge of the view.
48652 JR AddObjectEntity_Position Jump to AddObjectEntity_Position.
AddObjectEntity_RightEdge 48654 LD BC,32992 BC is 32992 the right edge of the view.
48657 SBC HL,BC Subtract it from the screen column.
48659 RET NC Return if the object is off the right edge of the view.
Work out the coordinates of the object. The x coordinate is the column of the cell and the upper four bits of the first byte of the object. The y coordinate is the row of the cell and the lower four bits, multiplied by sixteen. They are kept on the stack for the entity.
AddObjectEntity_Position 48660 LD A,(23738) Fetch the row of the cell and keep it in D.
48663 LD D,A
48664 LD A,(IX+0) Fetch the first byte of the object.
48667 ADD A,A Multiply it by sixteen, to move the lower four bits to the top...
48668 ADD A,A
48669 ADD A,A
48670 ADD A,A
48671 LD E,A ...and keep the result in E, as the low byte of the y coordinate.
48672 LD HL,(23736) Fetch the column of the cell into H.
48675 LD A,(IX+0) Fetch the first byte of the object.
48678 AND %11110000 Keep the upper four bits.
48680 LD L,A Keep them in L, as the low byte of the x coordinate.
48681 PUSH DE Save the y coordinate.
48682 PUSH HL Save the x coordinate.
Work out the screen row of the object. This is half of the difference between its x and y coordinates, plus the offset to the view, less the height of the object.
48683 SRL D Halve the y coordinate.
48685 RR E
48687 SRL H Halve the x coordinate.
48689 RR L
48691 SBC HL,DE Subtract half of the y coordinate from half of the x coordinate.
48693 LD DE,(49249) Fetch the offset to the view from the drawing code.
48697 ADD HL,DE Add the offset.
48698 LD A,(IX+1) Fetch the second byte of the object.
48701 AND %01110000 Keep the height in bits 4-6.
48703 LD E,A Keep it in E.
48704 LD A,(IX+2) Fetch the third byte of the object.
48707 ADD A,A Move bit 6, the top bit of the height, to bit 7...
48708 AND %10000000 ...and keep it.
48710 ADD A,E Add the other bits of the height to find the height of the object.
48711 LD E,A Keep the low byte of the height in E.
48712 LD D,0 Clear D.
48714 SBC HL,DE Subtract the height from the screen row.
Check that the object is not above or below the view.
48716 BIT 7,H Jump to AddObjectEntity_TopMargin if bit 7 of the high byte of the screen row is not set.
48718 JR Z,AddObjectEntity_TopMargin
48720 LD DE,32932 DE is 32932 the bottom of the view.
48723 SBC HL,DE Subtract it from the screen row.
48725 POP BC Restore the x coordinate.
48726 POP DE Restore the y coordinate.
48727 RET NC Return if the object is below the view.
48728 JR AddObjectEntity_Pool Jump to AddObjectEntity_Pool.
AddObjectEntity_TopMargin 48730 LD DE,40 DE is 40 the margin at the top of the view.
48733 ADD HL,DE Add the margin to the screen row.
48734 RL H Rotate bit 7 of the high byte into the carry flag.
48736 POP BC Restore the x coordinate.
48737 POP DE Restore the y coordinate.
48738 RET NC Return if the object is above the view.
Only objects of kind 2 3 and 4 that are not hidden have an entity. Record all of the others.
AddObjectEntity_Pool 48739 LD IY,46704 Point IY at the pool of four entities for objects.
48743 LD A,(IX+2) Fetch the third byte of the object.
48746 BIT 7,A Jump to RecordObject to record the object if it has one.
48748 JP NZ,RecordObject
48751 AND %00011111 Keep the kind of the object.
48753 CP 3 Jump to AddObjectEntity_Search if it is kind 3.
48755 JR Z,AddObjectEntity_Search
48757 CP 4 Jump to AddObjectEntity_Search if it is kind 4.
48759 JR Z,AddObjectEntity_Search
48761 CP 2 Jump to RecordObject to record the object if it is not kind 2.
48763 JP NZ,RecordObject
Search the pool for an entity that is not in use.
AddObjectEntity_Search 48766 LD A,4 Set the number of entities left to try to 4.
48768 EX DE,HL Put the y coordinate into HL.
48769 LD DE,38 Set the size of an entity, 38.
AddObjectEntity_Next 48772 EX AF,AF' Save the number of entities left.
48773 LD A,(IY+0) Fetch the type of the entity.
48776 AND A Test it.
48777 JP NZ,AddObjectEntity_NextEntity Jump to AddObjectEntity_NextEntity if the entity is in use.
Make the entity. It starts as type 12 which is kind 2.
48780 LD (IY+2),B Store the high byte of the x coordinate in field 2 of the entity.
48783 LD (IY+3),C Store the low byte of the x coordinate in field 3.
48786 LD (IY+5),H Store the high byte of the y coordinate in field 5.
48789 LD A,L Fetch the low byte of the y coordinate.
48790 ADD A,4 Add 4 to put the entity in the middle of the cell.
48792 LD L,A Keep it in L.
48793 LD (IY+6),L Store it in field 6.
48796 LD (IY+28),0 Set the animation counter in field 28 to 0.
48800 LD (IY+29),32 Set the first and last frames in field 29 to 32.
48804 LD (IY+0),12 Set the type of the entity to 12.
48808 LD (IY+11),12 Set the size in field 11 to 12.
48812 LD (IY+12),12 Set the size in field 12 to 12.
48816 LD (IY+13),1 Set field 13 to 1.
48820 LD A,(IX+2) Fetch the third byte of the object.
48823 AND %00011111 Keep the kind of the object.
48825 CP 2 Jump to AddObjectEntity_Fields if it is kind 2.
48827 JR Z,AddObjectEntity_Fields
48829 LD (IY+0),16 Change the type of the entity to 16.
48833 LD (IY+29),0 Set the first and last frames in field 29 to 0.
48837 LD (IY+13),10 Set field 13 to 10.
48841 CP 3 Jump to AddObjectEntity_Fields if it is kind 3.
48843 JR Z,AddObjectEntity_Fields
48845 LD (IY+29),17 Set the first and last frames in field 29 to 17 for kind 4.
48849 LD (IY+13),16 Set field 13 to 16.
Work out the z coordinate of the entity from the height of the object, less one.
AddObjectEntity_Fields 48853 LD A,(IX+1) Fetch the second byte of the object.
48856 AND %01110000 Keep the height in bits 4-6.
48858 LD E,A Keep it in E.
48859 LD (IY+8),1 Set the high byte of the z coordinate in field 8 to 1.
48863 LD A,(IX+2) Fetch the third byte of the object.
48866 ADD A,A Move bit 6, the top bit of the height, to bit 7...
48867 AND %10000000 ...and keep it.
48869 ADD A,E Add the other bits of the height.
48870 SUB 1 Take away one.
48872 LD (IY+9),A Store it in field 9 as the low byte of the z coordinate.
48875 JR NC,AddObjectEntity_SetFrame Jump to AddObjectEntity_SetFrame if it did not go below zero.
48877 DEC (IY+8) Decrease the high byte of the z coordinate by one.
AddObjectEntity_SetFrame 48880 LD A,(IY+29) Fetch the first frame of the entity from field 29.
48883 AND %00001111 Keep the frame.
48885 OR %10000000 Set bit 7 to hide the entity until it is known to be in view.
48887 LD (IY+1),A Store it as the frame of the entity in field 1.
Remember the address of the object in fields 26 and 27 of the entity.
48890 PUSH IX Put the address of the object into DE, via the stack.
48892 POP DE
48893 LD (IY+26),D Store the high byte in field 26.
48896 LD (IY+27),E Store the low byte in field 27.
Check that the new entity is in view, and free it again if it is not.
48899 EXX Switch to the alternate registers, to keep the coordinates.
48900 PUSH IX Save the address of the object.
48902 PUSH IY Put the address of the new entity into IX, via the stack.
48904 POP IX
48906 LD (IX+22),0 Set the width of the sprite in field 22 to 0.
48910 LD (IX+23),24 Set the height of the sprite in field 23 to 24.
48914 LD BC,16 Set the margin to 16.
48917 CALL TestEntityVisible Test whether the entity is in view.
48920 JR NZ,AddObjectEntity_Visible Jump to AddObjectEntity_Visible if it is.
48922 LD (IX+0),0 Set the type of the entity to 0 to free it.
48926 LD (IX+1),128 Set the frame of the entity to 128 so that it is hidden.
48930 POP IX Restore the address of the object.
48932 JR AddObjectEntity_Done Jump to AddObjectEntity_Done.
The entity is in view. Work out where it is on the screen and which sprite it has, and mark the object as having an entity so that it is not made again.
AddObjectEntity_Visible 48934 CALL ProjectEntity Work out the screen position of the entity.
48937 CALL SetEntitySize Fetch the sprite of the entity.
48940 POP IX Restore the address of the object.
48942 POP HL Take the return address off the stack.
48943 POP DE Take the screen column of the object off the stack.
48944 POP BC Take the second and third bytes of the object off the stack.
48945 SET 7,B Set bit 7 of the third byte, to show that the object has an entity.
48947 PUSH BC Put the bytes of the object back on the stack...
48948 PUSH DE ...then the screen column...
48949 PUSH HL ...and then the return address.
AddObjectEntity_Done 48950 EXX Switch back to the main registers.
48951 JR AddObjectEntity_Record Jump to AddObjectEntity_Record to record the object.
AddObjectEntity_NextEntity 48953 ADD IY,DE Move on to the next entity of the pool.
48955 EX AF,AF' Restore the number of entities left.
48956 DEC A Decrease the number of entities left.
48957 JP NZ,AddObjectEntity_Next Jump back to AddObjectEntity_Next until all four have been tried.
AddObjectEntity_Record 48960 EX DE,HL Put the y coordinate back into DE, and carry on into RecordObject to record the object.
Continue on to RecordObject.
Prev: 48571 Up: Map Next: 48961