Prev: C745 Up: Map Next: C833
C785: Draw the listed objects, back to front
Used by the routine at RENDER_DYNAMIC_OBJECTS.
Called by RENDER_DYNAMIC_OBJECTS every turn. This is where the game decides what is in front of what; the code is Knight Lore's and Pentagram's. Every listed object is a box in the room: centre U and V with half-sizes at +$04 and +$05, a base Z with its whole height at +$06. The far side of anything is towards smaller U, larger V and lower Z (measured in Pentagram, whose code this is).
It takes the first object in DRAW_LIST not yet drawn as the candidate (IX) and compares it with every other undrawn one (IY). If some IY has to be drawn before the candidate, IY becomes the candidate and the comparison starts again from the top of the list; a candidate that survives a whole pass is drawn, marked done, and the whole thing starts over. So each object drawn has nothing undrawn behind it.
The comparison classifies the two boxes on each axis -- IX clear on one side, overlapping, IY clear on one side -- and adds the three into an index 0-26 into DEPTH_ORDER. The order found need not be consistent (three boxes can each be behind the next), so the chain of candidates is kept in CANDIDATE_CHAIN, and meeting an object already in it breaks the circle by drawing that object at once.
DRAW_WORK counts the objects drawn; RENDER_DYNAMIC_OBJECTS adds the rectangles it wiped, and the main loop waits less the more work a turn has done. SORT_FIRST and SORT_SECOND point just past the candidate's entry and the compared object's.
Output
IX kept
IY kept
SORT_AND_DRAW C785 XOR A No objects drawn yet; keep IX and IY
C786 LD (DRAW_WORK),A
C789 PUSH IX
C78B PUSH IY
This entry point is used by the routines at IY_GOES_FIRST and DRAW_CANDIDATE.
SORT_PASS C78D LD DE,DRAW_LIST A pass: from the top of the list
SORT_AND_DRAW_0 C790 LD A,(DE) The end of the list: everything is drawn
C791 INC DE
C792 CP $FF
C794 JP Z,ALL_DRAWN
C797 BIT 7,A Bit 7: already drawn
C799 JR NZ,SORT_AND_DRAW_0
C79B CALL GET_PTR_OBJECT IX = the candidate; SORT_FIRST just past its entry
C79E LD (SORT_FIRST),DE
C7A2 PUSH HL
C7A3 POP IX
This entry point is used by the routines at ORDER_UNCONSTRAINED, CANDIDATE_ALREADY_FIRST, IY_GOES_FIRST and BOXES_INTERSECT.
COMPARE_NEXT_OBJ C7A5 LD A,(DE) The end of the list: nothing is behind the candidate, so draw it
C7A6 INC DE
C7A7 CP $FF
C7A9 JP Z,DRAW_CANDIDATE
C7AC BIT 7,A Already drawn: skip
C7AE JR NZ,COMPARE_NEXT_OBJ
C7B0 CALL GET_PTR_OBJECT IY = this object; SORT_SECOND just past its entry
C7B3 LD (SORT_SECOND),DE
C7B7 PUSH HL
C7B8 POP IY
C7BA PUSH IX The candidate itself: skip
C7BC POP BC
C7BD AND A
C7BE SBC HL,BC
C7C0 JR Z,COMPARE_NEXT_OBJ
C7C2 LD C,$00 Z: code 0 if the candidate's base is at or above IY's top
C7C4 LD A,(IY+$03)
C7C7 ADD A,(IY+$06)
C7CA LD L,A
C7CB LD A,(IX+$03)
C7CE SUB L
C7CF JR NC,COMPARE_ALONG_V
C7D1 LD A,(IX+$03) Code 2 if IY's base is at or above the candidate's top, 1 if they overlap
C7D4 ADD A,(IX+$06)
C7D7 LD L,A
C7D8 LD A,(IY+$03)
C7DB SUB L
C7DC JR C,SORT_AND_DRAW_1
C7DE INC C
SORT_AND_DRAW_1 C7DF INC C
COMPARE_ALONG_V C7E0 LD A,(IY+$02) V: add 0 if the candidate's low-V edge is at or beyond IY's high-V edge, the candidate lying wholly further back
C7E3 ADD A,(IY+$05)
C7E6 LD L,A
C7E7 LD A,(IX+$02)
C7EA SUB (IX+$05)
C7ED SUB L
C7EE JR NC,COMPARE_ALONG_U
C7F0 LD A,(IX+$02) Add 6 if IY lies wholly further back, 3 if they overlap
C7F3 ADD A,(IX+$05)
C7F6 LD L,A
C7F7 LD A,(IY+$02)
C7FA SUB (IY+$05)
C7FD SUB L
C7FE LD A,C
C7FF JR C,SORT_AND_DRAW_2
C801 ADD A,$03
SORT_AND_DRAW_2 C803 ADD A,$03
C805 LD C,A
COMPARE_ALONG_U C806 LD A,(IY+$01) U: add 0 if the candidate's low-U edge is at or beyond IY's high-U edge, IY lying wholly further back
C809 ADD A,(IY+$04)
C80C LD L,A
C80D LD A,(IX+$01)
C810 SUB (IX+$04)
C813 SUB L
C814 JR NC,ACT_ON_COMPARISON
C816 LD A,(IX+$01) Add 18 if the candidate lies wholly further back, 9 if they overlap
C819 ADD A,(IX+$04)
C81C LD L,A
C81D LD A,(IY+$01)
C820 SUB (IY+$04)
C823 SUB L
C824 LD A,C
C825 JR C,SORT_AND_DRAW_3
C827 ADD A,$09
SORT_AND_DRAW_3 C829 ADD A,$09
C82B LD C,A
ACT_ON_COMPARISON C82C LD L,C Jump through DEPTH_ORDER, as the main loop jumps through the update table
C82D LD BC,DEPTH_ORDER
C830 JP JUMP_THROUGH_TABLE
Prev: C745 Up: Map Next: C833