Prev: 56577 Up: Map Next: 56682
56616: Update Direction
Used by the routines at UpdateCreature and UpdateMonster.
Changes the direction that an object is facing when the timer for the direction has run out (the lowest five bits of the direction and timer) or the object has hit something. The object turns a quarter to the left or to the right, chosen from the random seed, or, if it has been flagged as chasing the player (bit 5 of the flags), turns towards the player. A new timer is set.
Input
IX Object
Change direction at once if the object has hit a wall.
UpdateDirection 56616 BIT 0,(IX+7) Jump to UpdateDirection_Random if the object has hit something.
56620 JR NZ,UpdateDirection_Random
Count down the timer, and return unless it has run out.
56622 DEC (IX+6) Count down the timer.
56625 LD A,(IX+6) Fetch the timer.
56628 AND %00011111 Keep the bottom five bits.
56630 RET NZ Return unless the timer has run out.
56631 BIT 5,(IX+7) Jump to UpdateDirection_Chase if the object chases the player.
56635 JR NZ,UpdateDirection_Chase
Turn left or right and set a new timer.
UpdateDirection_Random 56637 LD A,(48042) Fetch a random number.
56640 CP 128 Compare it with 128.
56642 LD A,(IX+6) Fetch the direction and timer.
56645 JR NC,UpdateDirection_Back Jump to UpdateDirection_Back to turn back if the random number is 128 or more.
56647 ADD A,64 Turn on a quarter.
Set a timer from the second random seed, set the direction and return.
UpdateDirection_Timer 56649 LD C,A Keep the new direction in C.
56650 LD A,(48043) Fetch another random number.
56653 AND %00111111 Keep the bottom six bits.
56655 OR C Combine it with the direction.
56656 LD (IX+6),A Store the new direction.
56659 RET Return.
The random number was 128 or more, so turn back.
UpdateDirection_Back 56660 SUB 64 Turn back a quarter.
56662 JR UpdateDirection_Timer Jump to UpdateDirection_Timer.
The object chases the player, so work out where the player is compared with the direction that the object is facing.
UpdateDirection_Chase 56664 CALL FindDirectionToPlayer Find the direction towards the player.
56667 LD C,A Keep it.
56668 LD A,(IX+6) Work out how far the object has to turn.
56671 RLCA
56672 RLCA
56673 SUB C
56674 AND 3
56676 LD BC,56682 Jump to the routine for the amount.
56679 JP JumpToTableRoutine
View the equivalent code in;
Prev: 56577 Up: Map Next: 56682