Prev: B671 Up: Map Next: B6B0
B674: The other object must be drawn first
Reached through DEPTH_ORDER. IY is behind the candidate, so it becomes the candidate instead -- unless it is already in the chain of objects that have been candidates since the last draw (CANDIDATE_CHAIN), in which case the order has gone round in a circle and IY is drawn straight away.
Nothing checks the chain's length: CANDIDATE_CHAIN has room for fifteen numbers and the $FF, twice Knight Lore's, and a sixteenth link would put its $FF on the first byte of FILL_RECT. Measured over every room's first turn: the longest chain is 9, in the four start rooms, and four rooms go past Knight Lore's 7 -- so Knight Lore's eight bytes would have overflowed here (that this is why it was doubled is an inference).
IY_GOES_FIRST B674 LD HL,(SORT_SECOND) C = IY's number, from its entry in the list
B677 DEC HL
B678 LD C,(HL)
B679 LD DE,CANDIDATE_CHAIN search the chain for it
IY_GOES_FIRST_0 B67C LD A,(DE)
B67D CP $FF
B67F JR Z,IY_BECOMES_CANDIDATE
B681 CP C
B682 JR Z,BREAK_ORDER_CYCLE
B684 INC DE
B685 JR IY_GOES_FIRST_0
IY_BECOMES_CANDIDATE B687 LD A,C not there: add it, with a new $FF after it
B688 LD (DE),A
B689 INC DE
B68A LD A,$FF
B68C LD (DE),A
B68D PUSH IY IX = IY, and SORT_FIRST = SORT_SECOND
B68F POP IX
B691 LD HL,(SORT_SECOND)
B694 LD (SORT_FIRST),HL
B697 LD DE,DRAW_LIST compare it with the whole list from the top, since it need not be the first undrawn entry
B69A JP COMPARE_NEXT_OBJ
BREAK_ORDER_CYCLE B69D LD HL,DRAW_LIST a circle: find IY's entry in the list (its number is certain to be there, so the exit to a new pass is never taken)
IY_GOES_FIRST_1 B6A0 LD A,(HL)
B6A1 INC HL
B6A2 CP $FF
B6A4 JP Z,SORT_PASS
B6A7 CP C
B6A8 JR NZ,IY_GOES_FIRST_1
B6AA PUSH IY IX = IY; draw it, HL just past its entry
B6AC POP IX
B6AE JR DRAW_AND_NEXT_PASS
Prev: B671 Up: Map Next: B6B0