| Routines

25843: Determine the next move of a character following another character

Used by the routines at 25944 and 62483. Returns with one of the following values in bits 0-2 of A depending on the relative locations of the follower and his target:
A | Meaning
--- | ---
0 | Follower is at the same coordinates as the target
1 | Follower should go (or continue going) upstairs
2 | Follower should go (or continue going) downstairs
3 | Follower should go left
4 | Follower should go right
In addition, bit 3 of A will be set if the follower is facing a closed door, and C will hold the identifier of the closed door (see 28736).
Input
DE | Target character's coordinates
H | Follower's character number (200-204, 210)
25843 | PUSH DE | Save the target coordinates temporarily
25844 | CALL 28242 | Is the follower on a staircase?
25847 | JR NC,25860 | Jump if not
The follower is on a staircase. At this point, A=0 if the staircase goes up and to the left, 128 (bit 7 set) if it goes up and to the right.
25849 | LD L,0 | Point HL at byte 0 of the character's buffer
25851 | XOR (HL) | Compare the staircase's direction with the follower's
25852 | RLCA | Set the carry flag if the character is descending the staircase
25853 | LD A,1 | A=1 if the follower is ascending the staircase, 2 if descending
25855 | ADC A,0
25857 | POP DE | Restore the target coordinates to DE
25858 | SCF | Signal: follower is on a staircase (this is ignored by both callers)
25859 | RET |
The follower is not on a staircase.
25860 | CALL 25554 | Get the region identifier for the follower's current location
25863 | LD L,A | Copy this to L
25864 | POP DE | Restore the target coordinates to DE
25865 | CALL 25554 | Get the region identifier for the target coordinates
25868 | CP L | Compare the current and target regions
25869 | LD D,L | D=region ID for the follower's current location
25870 | LD L,1 | Point HL at byte 1 of the follower's buffer
25872 | JR NZ,25909 | Jump if the follower is not in the same region as his target
25874 | LD A,E | A=target x-coordinate
25875 | SUB (HL) | Is the follower in exactly the same location as his target?
25876 | RET Z | Return if so (with the carry flag reset)
The follower is in the same region as his target, but still has some walking to do. At this point the carry flag is set if the follower is to the right of his target, and reset if he's to the left.
25877 | SBC A,A | A=3 if the follower must go left, 4 if right to reach the target
25878 | ADD A,4
25880 | PUSH AF | Save this value briefly
25881 | LD L,0 | Point HL at byte 0 of the follower's buffer
25883 | RRCA | Set bit 7 of A if the follower is facing the right way to reach his target
25884 | XOR (HL)
25885 | RLCA | Push bit 7 of A into the carry flag
25886 | JR C,25891 | Jump if the follower is facing the right way to reach his target
25888 | POP AF | A=3 (go left) or 4 (go right)
25889 | AND A | Clear the carry flag (the follower is not on a staircase)
25890 | RET |
The follower is in the same region as his target, and is facing the right way to reach him. But are there any doors in the way?
25891 | CALL 28767 | Check for closed doors in front of the follower
25894 | JR NC,25888 | Jump if there are none
25896 | LD A,H | A=follower's character number
25897 | CP 210 | Is this ERIC (in demo mode)?
25899 | JR NZ,25905 | Jump if not
25901 | POP AF | A=3 (go left) or 4 (go right)
25902 | XOR 7 | 3 (go left) becomes 4 (go right) and vice versa for ERIC
25904 | RET | Return with the carry flag reset and C holding the door identifier
25905 | POP AF | A=3 (go left) or 4 (go right)
25906 | ADD A,8 | Set bit 3 (A=11, 12): closed door ahead
25908 | RET | Return with the carry flag reset and C holding the door identifier
The follower is not in the same region as his target. This means at least one visit to a staircase first.
25909 | SUB 129 | A=identifier of the top or bottom of the staircase (see 25700)
25911 | LD E,A
25912 | LD A,(DE)
25913 | LD D,A | A=x-coordinate of the top or bottom of the staircase the follower will have to go to first in order to reach his target
25914 | LD E,68
25916 | LD A,(DE)
25917 | CP (HL) | Is the follower standing at this staircase already?
25918 | JR NZ,25875 | Jump if not
The follower is at the top or bottom of the first staircase he must negotiate in order to reach his target. Is he facing the right way to begin his ascent or descent?
25920 | INC E | Point DE at the staircase direction indicator
25921 | DEC L | Point HL at byte 0 of the character's buffer
25922 | LD A,(DE) | A=0 if the follower must face left to negotiate the stairs, 128 (bit 7 set) if right
25923 | XOR (HL) | Set the carry flag if the follower is facing the wrong way
25924 | RLCA
25925 | JR NC,25934 | Jump if the follower is facing the right way to negotiate the stairs
25927 | LD A,(HL) | A=follower's animatory state
25928 | RLCA | A=3 if the follower's facing right, 4 if facing left
25929 | SBC A,A
25930 | ADD A,4
25932 | AND A | Clear the carry flag (the follower is not on a staircase)
25933 | RET |
The follower is facing the right way to begin his ascent or descent of the staircase.
25934 | INC E | A=LSB of the routine to make the follower ascend (56/25656) or descend (77/25677) the stairs
25935 | LD A,(DE)
25936 | AND 1 | A=1 if the follower must ascend the stairs, 2 if he must descend
25938 | INC A
25939 | RET | Return with the carry flag reset