Prev: 53141 Up: Map Next: 53268
53159: Update Drift
Used by the routine at MoveEntity.
Adds the amount in A to the drift in field 32. 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 53159 LD C,A Keep the amount aside.
53160 AND %00000011 Use bits 0-1 as the amount.
53162 JP Z,DecayDrift A zero amount is handled by the decay.
53165 ADD A,A Double the amount.
53166 BIT 2,C Bit 2 of the original amount makes it negative.
53168 JR Z,UpdateDrift_Apply
53170 NEG Negate the amount.
This entry point is used by the routine at DecayDrift.
UpdateDrift_Apply 53172 LD C,A Keep the amount.
53173 LD A,(IX+32) Read the drift.
53176 AND 127 Use bits 0-6 of the drift.
53178 LD E,A
53179 AND %00011111 Use the low five bits as its magnitude.
53181 BIT 7,(IX+32) The top bit makes that magnitude negative.
53185 JR Z,UpdateDrift_Add
53187 NEG Negate the magnitude.
UpdateDrift_Add 53189 ADD A,C Add the amount to the drift.
53190 BIT 7,A A negative result flips the sign.
53192 JR Z,UpdateDrift_Cap
53194 NEG Negate it and set the sign.
53196 SET 7,E
UpdateDrift_Cap 53198 AND %00011111 Use the low five bits as the new magnitude.
53200 LD D,A Keep the new magnitude.
53201 LD A,E Shift the old limit down.
53202 RRA
53203 RRA
53204 AND %00011000 Use bits 3-4 as that limit.
53206 CP D Keep the new magnitude when it is within the limit.
53207 JR NC,UpdateDrift_Store
53209 LD D,A
UpdateDrift_Store 53210 LD A,E Use the sign and the old limit bits.
53211 AND 224
53213 OR D Combine them with the magnitude and keep the drift.
53214 LD (IX+32),A
53217 RLA A negative drift is subtracted from the displacement.
53218 JR NC,UpdateDrift_Shift
53220 LD A,D Negate the magnitude.
53221 NEG
53223 LD D,A
UpdateDrift_Shift 53224 LD E,0 Add an eighth of the drift to the displacement.
53226 SRA D
53228 RR E
53230 SRA D
53232 RR E
53234 SRA D
53236 RR E
53238 ADD HL,DE
53239 LD A,B Shift the limit in B down by four.
53240 RRA
53241 RRA
53242 RRA
53243 RRA
53244 AND %00001111 Use the low four bits as the limit.
53246 LD B,A Keep the limit.
53247 LD A,H
53248 BIT 7,A A negative displacement is clamped to it.
53250 LD A,B
53251 JR Z,UpdateDrift_Positive
53253 NEG Compare the displacement with the negative limit.
53255 CP H
53256 RET C Leave it when it is inside the limit.
53257 LD H,A Clamp the displacement to that limit.
53258 LD L,0
53260 RET
UpdateDrift_Positive 53261 DEC A A positive displacement is cleared only when the limit is zero.
53262 RET NC
53263 INC A Clear the displacement.
53264 LD H,A
53265 LD L,0
53267 RET
Prev: 53141 Up: Map Next: 53268