Prev: BA14 Up: Map Next: BA5E
BA16: Find Group
Used by the routines at DrawLevelObjects and TestGroupObjects.
Finds the group of objects at a position of the level, using the level map at the address in LevelHeaderAddress and the table of groups at GroupTable.
Input
H The vertical position, which starts at 20
L The horizontal position, which starts at 20
Output
IY The address of the group
F Carry set if there is no group, or it has no objects
Check that the position is inside the map, and find the cell of the map.
FindGroup BA16 LD IY,($5CFE) Fetch the address of the header of the level into IY.
BA1A LD A,L Copy the horizontal position into A.
BA1B SUB $20 Take away the border of 20 to make it a column of the map.
BA1D RET C Return if the position is left of the map.
BA1E LD C,A Keep the column in C.
BA1F CP (IY+$05) Compare the column with the width of the map.
BA22 JR NC,SetCarryAndReturn Return with no group if the position is right of the map.
BA24 LD A,H Copy the vertical position into H.
BA25 SUB $20 Take away the border of 20 to make it a row of the map.
BA27 RET C Return if the position is above the map.
BA28 LD B,A Keep the row in B.
BA29 JR Z,FindGroup_Row Jump to FindGroup_Row if it is the first row.
BA2B CP (IY+$06) Compare the row with the height of the map.
BA2E JR NC,SetCarryAndReturn Return with no group if the position is below the map.
Find the number of the cell by adding the width of the map once for each row, and then add that number of cells to the start of the map.
BA30 LD A,C Start with the column.
BA31 LD D,(IY+$05) Fetch the width of the map.
FindGroup_Loop BA34 ADD A,D Add the width of the map.
BA35 DJNZ FindGroup_Loop Decrease the row counter and loop back to FindGroup_Loop until all the rows have been added.
BA37 LD C,A Keep the number of the cell in C.
FindGroup_Row BA38 SLA C Double it, as there are two bytes for each cell.
BA3A ADD IY,BC Add it to the address of the header, to reach the cell.
Read the cell. Its first byte is returned as an offset, and its second is the number of the group.
BA3C LD E,B Clear E.
BA3D LD A,(IY+$07) Fetch the first byte of the cell, which starts 07 bytes after the header.
BA40 LD D,A Keep it in D.
BA41 ADD A,$02 Add 02 which sets the carry flag if the byte is FE or FF.
BA43 RET C Return with no group if the cell has none.
BA44 LD C,(IY+$08) Fetch the number of the group from the second byte of the cell.
BA47 SLA C Double it, as the table has two bytes for each group.
BA49 LD HL,$8BB1 Point HL at GroupTable.
BA4C ADD HL,BC Add the offset to find the entry for the group.
BA4D LD C,(HL) Fetch the low byte of the address of the group.
BA4E INC HL Move onto the high byte.
BA4F LD B,(HL) Fetch the high byte of the address of the group.
BA50 PUSH BC Put the address of the group in BC onto the stack...
BA51 POP IY ...and take it off into IY.
BA53 SRL D Multiply the first byte of the cell by 80 by shifting it...
BA55 RR E ...across DE, ready to return it as an offset.
BA57 LD A,(IY+$00) Fetch the first byte of the group.
BA5A AND A Test it.
BA5B RET NZ Return with the carry flag reset if the group has objects.
BA5C SCF Set the carry flag as the group is empty.
BA5D RET Return.
Prev: BA14 Up: Map Next: BA5E