Prev: D5DF Up: Map Next: D682
D647: Test Overlap
Used by the routine at TestEntityPair.
Tests whether two ranges overlap. The carry is set when they do, and L is the amount of the overlap. No overlap leaves the carry clear.
Input
HL The start of the first range
DE The start of the second range
B The size of the first range
C The size of the second range
Output
F The carry flag is set if they overlap
L The amount of the overlap
B is the size of the first range and C the size of the second. The result is the smaller amount by which they cross.
TestOverlap D647 XOR A Subtract the second start from the first.
D648 SBC HL,DE
D64A JR C,TestOverlap_Before The second range starts first.
D64C OR H No overlap when the starts are a byte or more apart.
D64D RET NZ
D64E LD A,L No overlap when the gap is at least the first size.
D64F SUB B
D650 RET NC
D651 NEG The overlap is the first size less that gap.
D653 ADD HL,DE
D654 LD B,$00
D656 ADD HL,BC
D657 SBC HL,DE
D659 LD H,$00
D65B CP L Keep the smaller of the two amounts.
D65C JR NC,TestOverlap_Overlap
D65E LD L,A Return that overlap.
D65F RET
TestOverlap_Overlap D660 LD H,$FF Return the overlap that reached the other size.
D662 SCF
D663 RET
TestOverlap_Before D664 LD A,B Take the first size and clear the carry.
D665 LD B,$00
D667 AND A
D668 PUSH HL Remember where the ranges stood.
D669 ADC HL,BC Add the second size to the first start.
D66B LD C,A
D66C LD A,L
D66D POP HL
D66E RET NC No overlap when the second size falls short.
D66F JP Z,TestRangeOverlap_Reset The ranges meet on an edge.
D672 ADD HL,DE Measure how far the first range reaches back.
D673 EX DE,HL
D674 ADD HL,BC
D675 SBC HL,DE
D677 LD H,$FF
D679 CP L Keep the smaller of the two amounts.
D67A JR NC,TestOverlap_Inside
D67C LD L,A Return that overlap.
D67D RET
TestOverlap_Inside D67E LD H,$00 Return the overlap that reached the other size.
D680 SCF
D681 RET
Prev: D5DF Up: Map Next: D682