Prev: CF95 Up: Map Next: D014
CFA7: Update Drift
Used by the routine at MoveEntity.
Adds the amount in A to the drift in field 20. Bits 0-1 are the amount and bit 2 makes it negative. A zero amount is handled by DecayDrift. An eighth of the resulting drift is added to the displacement in HL, which is then clamped to the limit in the high nibble of B.
Input
A The amount, in bits 0-1, with bit 2 for the direction
HL The displacement
IX The entity
Output
HL The limited displacement
UpdateDrift CFA7 LD C,A Keep the amount aside.
CFA8 AND %00000011 Use bits 0-1 as the amount.
CFAA JP Z,DecayDrift A zero amount is handled by the decay.
CFAD ADD A,A Double the amount.
CFAE BIT 2,C Bit 2 of the original amount makes it negative.
CFB0 JR Z,UpdateDrift_Apply
CFB2 NEG Negate the amount.
This entry point is used by the routine at DecayDrift.
UpdateDrift_Apply CFB4 LD C,A Keep the amount.
CFB5 LD A,(IX+$20) Read the drift.
CFB8 AND $7F Use bits 0-6 of the drift.
CFBA LD E,A
CFBB AND %00011111 Use the low five bits as its magnitude.
CFBD BIT 7,(IX+$20) The top bit makes that magnitude negative.
CFC1 JR Z,UpdateDrift_Add
CFC3 NEG Negate the magnitude.
UpdateDrift_Add CFC5 ADD A,C Add the amount to the drift.
CFC6 BIT 7,A A negative result flips the sign.
CFC8 JR Z,UpdateDrift_Cap
CFCA NEG Negate it and set the sign.
CFCC SET 7,E
UpdateDrift_Cap CFCE AND %00011111 Use the low five bits as the new magnitude.
CFD0 LD D,A Keep the new magnitude.
CFD1 LD A,E Shift the old limit down.
CFD2 RRA
CFD3 RRA
CFD4 AND %00011000 Use bits 3-4 as that limit.
CFD6 CP D Keep the new magnitude when it is within the limit.
CFD7 JR NC,UpdateDrift_Store
CFD9 LD D,A
UpdateDrift_Store CFDA LD A,E Use the sign and the old limit bits.
CFDB AND $E0
CFDD OR D Combine them with the magnitude and keep the drift.
CFDE LD (IX+$20),A
CFE1 RLA A negative drift is subtracted from the displacement.
CFE2 JR NC,UpdateDrift_Shift
CFE4 LD A,D Negate the magnitude.
CFE5 NEG
CFE7 LD D,A
UpdateDrift_Shift CFE8 LD E,$00 Add an eighth of the drift to the displacement.
CFEA SRA D
CFEC RR E
CFEE SRA D
CFF0 RR E
CFF2 SRA D
CFF4 RR E
CFF6 ADD HL,DE
CFF7 LD A,B Shift the limit in B down by four.
CFF8 RRA
CFF9 RRA
CFFA RRA
CFFB RRA
CFFC AND %00001111 Use the low four bits as the limit.
CFFE LD B,A Keep the limit.
CFFF LD A,H
D000 BIT 7,A A negative displacement is clamped to it.
D002 LD A,B
D003 JR Z,UpdateDrift_Positive
D005 NEG Compare the displacement with the negative limit.
D007 CP H
D008 RET C Leave it when it is inside the limit.
D009 LD H,A Clamp the displacement to that limit.
D00A LD L,$00
D00C RET
UpdateDrift_Positive D00D DEC A A positive displacement is cleared only when the limit is zero.
D00E RET NC
D00F INC A Clear the displacement.
D010 LD H,A
D011 LD L,$00
D013 RET
Prev: CF95 Up: Map Next: D014