Prev: 47120 Up: Map Next: 47195
47146: Update Direction
Used by the routines at HandleOutlaw and HandleBystander.
Makes a figure choose a new direction every so often. The bottom six bits of byte 6 count down the frames until the next change, and bits 6 and 7 are the direction. The direction is turned a quarter to the left or the right, and the counter is set to a new random number. A figure which has bumped into a wall (bit 0 of the flags) changes direction straight away, and a figure with bit 5 of the flags set never changes direction on its own.
Input
IX The address of the object
Change direction at once if the figure has hit a wall.
UpdateDirection 47146 BIT 0,(IX+7) Jump to UpdateDirection_Random if the figure has bumped into a wall.
47150 JR NZ,UpdateDirection_Random
47152 DEC (IX+6) Count down the counter.
47155 LD A,(IX+6) Fetch the counter.
47158 AND %00011111 Keep the bottom five bits.
47160 RET NZ Return unless the counter is at a multiple of thirty two.
47161 BIT 5,(IX+7) Jump to UpdateDirection_Finish to return if the figure keeps its direction.
47165 JR NZ,UpdateDirection_Finish
Turn left or right and set a new counter.
UpdateDirection_Random 47167 LD A,(41496) Fetch a random number.
47170 CP 128 Compare it with 128.
47172 LD A,(IX+6) Fetch the direction and counter.
47175 JR NC,UpdateDirection_TurnLeft Jump to UpdateDirection_TurnLeft to turn left if the random number is 128 or more.
47177 ADD A,64 Turn right.
UpdateDirection_Timer 47179 LD C,A Keep the new direction in C.
47180 LD A,(41497) Fetch another random number.
47183 AND %00111111 Keep the bottom six bits.
47185 OR C Combine it with the direction.
47186 LD (IX+6),A Store the new direction.
47189 RET Return.
The random number was 128 or more, so turn left.
UpdateDirection_TurnLeft 47190 SUB 64 Turn left.
47192 JR UpdateDirection_Timer Go back to store the direction and counter.
UpdateDirection_Finish 47194 RET Return.
View the equivalent code in;
Prev: 47120 Up: Map Next: 47195