Prev: 58056 Up: Map Next: 58140
58109: Clamp Entity Velocity
Adds a 16-bit signed delta (DE) to a velocity byte in H, sign-extending H to a full 16-bit value first, then clamps the result to the range 64512-1023. Returns the clamped result in HL. Called from Process_Tracked_Enemy_Entity and Process_Player_Tracking_Enemy when updating tracked enemy velocities.
Clamp_Entity_Velocity 58109 LD A,H Sign-extend H into a 16-bit value and add the delta in DE.
58110 AND 128
58112 LD H,0
58114 JP Z,Clamp_Entity_Velocity_0
58117 LD H,255 Positive result: check bit 7 and jump if the result is negative.
Clamp_Entity_Velocity_0 58119 ADD HL,DE
58120 LD A,H
58121 BIT 7,H Positive ceiling: return if within range; clamp to 1023 otherwise.
58123 JP NZ,Clamp_Entity_Velocity_Negative
58126 CP 4
58128 RET C
58129 LD HL,1023
58132 RET
Clamp_Entity_Velocity_Negative 58133 CP 252 Negative result: return if within range; clamp to 64512 otherwise.
58135 RET NC
58136 LD HL,64512
58139 RET
Prev: 58056 Up: Map Next: 58140