Prev: AC9A Up: Map Next: AD02
ACAB: Set Up Line
Used by the routine at Draw_Line.
Works out what is needed to draw a line that goes up the screen from the first point (the start) to the second point, one pixel row at a time. The number of steps is the difference between the y positions, and the amount that the x position changes for each step is the difference between the x positions multiplied by 0100 and divided by the number of steps (see Divide).
The step count is stored in Line_Step_Count and the direction flags in Line_Direction_Flags (see there). The slope is made negative when the line goes to the left, and it is halved once for each room that the rope is long (the high byte of the length multiplied by two), which scales it to the length of the rope.
Input
HL Start of the line (x position in L, y position in H)
DE End of the line (E is the x position and D is the y position)
Output
A Low byte of the slope
B Number of steps
C Direction of the y position (01 or FF)
DE Slope (x position change per step, multiplied by 0100)
HL Start of the line
Find the difference between the y positions (in B) and between the x positions, and the direction flags.
Set_Up_Line ACAB LD C,$00 Clear the direction flags.
ACAD LD A,H Fetch the y position of the start.
ACAE SUB D Jump to Set_Up_Line_Store_Y_Difference if the difference is positive.
ACAF JR NC,Set_Up_Line_Store_Y_Difference
ACB1 NEG Make the difference positive.
ACB3 SET 0,C Set bit 0 of the direction flags.
Set_Up_Line_Store_Y_Difference ACB5 LD B,A Copy the difference to B.
ACB6 LD A,L Fetch the x position of the start.
ACB7 SUB E Jump to Set_Up_Line_Store_X_Difference if the difference is positive.
ACB8 JR NC,Set_Up_Line_Store_X_Difference
ACBA NEG Make the difference positive.
ACBC SET 1,C Set bit 1 of the direction flags.
Set_Up_Line_Store_X_Difference ACBE EX AF,AF' Stash the difference between the x positions in AF'.
ACBF LD A,C Fetch the direction flags.
ACC0 LD ($AA94),A Store the direction flags.
ACC3 LD A,B Fetch the difference between the y positions.
ACC4 LD ($AA93),A Store the number of steps.
ACC7 EX AF,AF' Restore the difference between the x positions from AF'.
Divide the difference between the x positions by the number of steps.
ACC8 PUSH HL Stash the start of the line on the stack.
ACC9 LD E,B Copy the number of steps to E.
ACCA LD D,$00 Make D zero.
ACCC LD L,D Clear L.
ACCD LD H,A Copy the difference between the x positions to H.
ACCE CALL Divide Divide the difference by the number of steps (the result is the slope).
ACD1 LD A,($AA94) Fetch the direction flags.
ACD4 BIT 1,A Jump to Set_Up_Line_Check_Vertical_Direction if bit 1 is set (the line goes to the right).
ACD6 JR NZ,Set_Up_Line_Check_Vertical_Direction
ACD8 CALL NegateHL Make the slope negative.
Set_Up_Line_Check_Vertical_Direction ACDB LD A,($AA94) Fetch the direction flags.
ACDE BIT 0,A Test bit 0 of the direction flags.
ACE0 LD C,$01 Set the vertical step of the line to 01.
ACE2 JR NZ,Set_Up_Line_Store_Slope Jump to Set_Up_Line_Store_Slope if bit 0 is set.
ACE4 LD C,$FF Set the vertical step of the line to FF.
Set_Up_Line_Store_Slope ACE6 EX DE,HL Move the slope to DE and the remainder to HL.
ACE7 POP HL Restore the start of the line from the stack.
ACE8 LD A,($AA93) Fetch the number of steps.
ACEB LD B,A Copy it to B.
ACEC PUSH BC Stash the number of steps and the direction on the stack.
Halve the slope once for each room that the rope is long.
ACED LD BC,($AAA5) Fetch the length of the rope.
ACF1 RL C Double the length.
ACF3 RL B
ACF5 LD A,B Copy the high byte to A.
ACF6 AND A Jump to Set_Up_Line_Finish if the rope is not longer than a room.
ACF7 JR Z,Set_Up_Line_Finish
Set_Up_Line_Halve_Slope_Loop ACF9 SRA D Halve the slope.
ACFB RR E
ACFD DJNZ Set_Up_Line_Halve_Slope_Loop Decrease the room counter and loop back until the slope has been halved for each room.
Set_Up_Line_Finish ACFF POP BC Restore the number of steps and the direction from the stack.
AD00 LD A,E Copy the low byte of the slope to A.
AD01 RET Return.
Prev: AC9A Up: Map Next: AD02