Prev: DD01 Up: Map Next: DD6A
DD28: 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 DD28 BIT 0,(IX+$07) Jump to UpdateDirection_Random if the object has hit something.
DD2C JR NZ,UpdateDirection_Random
Count down the timer, and return unless it has run out.
DD2E DEC (IX+$06) Count down the timer.
DD31 LD A,(IX+$06) Fetch the timer.
DD34 AND %00011111 Keep the bottom five bits.
DD36 RET NZ Return unless the timer has run out.
DD37 BIT 5,(IX+$07) Jump to UpdateDirection_Chase if the object chases the player.
DD3B JR NZ,UpdateDirection_Chase
Turn left or right and set a new timer.
UpdateDirection_Random DD3D LD A,($BBAA) Fetch a random number.
DD40 CP $80 Compare it with 80.
DD42 LD A,(IX+$06) Fetch the direction and timer.
DD45 JR NC,UpdateDirection_Back Jump to UpdateDirection_Back to turn back if the random number is 80 or more.
DD47 ADD A,$40 Turn on a quarter.
Set a timer from the second random seed, set the direction and return.
UpdateDirection_Timer DD49 LD C,A Keep the new direction in C.
DD4A LD A,($BBAB) Fetch another random number.
DD4D AND %00111111 Keep the bottom six bits.
DD4F OR C Combine it with the direction.
DD50 LD (IX+$06),A Store the new direction.
DD53 RET Return.
The random number was 80 or more, so turn back.
UpdateDirection_Back DD54 SUB $40 Turn back a quarter.
DD56 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 DD58 CALL FindDirectionToPlayer Find the direction towards the player.
DD5B LD C,A Keep it.
DD5C LD A,(IX+$06) Work out how far the object has to turn.
DD5F RLCA
DD60 RLCA
DD61 SUB C
DD62 AND $03
DD64 LD BC,$DD6A Jump to the routine for the amount.
DD67 JP JumpToTableRoutine
View the equivalent code in;
Prev: DD01 Up: Map Next: DD6A