| Routines

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

Used by the routines at 31648, 62938 and 63374. Returns with one of the following values in 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
Input
DE | Target character's coordinates
H | Follower's character number (152-172)
31452 | LD L,97 | Byte 97 of the follower's buffer holds his y-coordinate
31454 | LD A,(HL) | Pick this up in A
31455 | CP 155 | Is the follower on the top floor?
31457 | JR Z,31480 | Jump if so
31459 | CP 162 | Is the follower on the middle floor?
31461 | JR Z,31480 | Jump if so
31463 | CP 169 | Is the follower on the bottom floor?
31465 | JR Z,31480 | Jump if so
The follower is on a staircase. Find out whether he's going up or down and set A accordingly to make him continue.
31467 | LD L,98 | Byte 98 of the follower's buffer holds his x-coordinate
31469 | LD A,(HL) | Pick this up in A
31470 | LD L,96 | Byte 96 holds the follower's animatory state
31472 | ADD A,80 | Set bit 7 of A if the follower is on a staircase at the right of the skool
31474 | XOR (HL) | A=1 if the follower is going upstairs, 2 if he's going downstairs
31475 | AND 128
31477 | RLCA
31478 | INC A
31479 | RET |
The follower is not on a staircase. Which way should he go?
31480 | CP D | Is the follower on the same floor as the target?
31481 | JR NZ,31549 | Jump if not
The follower is on the same floor as the target. However, there may be a wall between them.
31483 | INC L | Pick up the follower's x-coordinate in A
31484 | LD A,(HL)
31485 | SUB E | Is the follower in the same spot as his target?
31486 | RET Z | Return if so
31487 | DEC L | Pick up the follower's y-coordinate in A
31488 | LD A,(HL)
31489 | CP 169 | Is the follower on the bottom floor?
31491 | JR NZ,31501 | Jump if not
The follower and target are both on the bottom floor, so the follower should simply move either left or right.
31493 | INC HL | L=98
31494 | LD A,(HL) | A=follower's x-coordinate
31495 | CP E | Set the carry flag if the follower should go right
31496 | LD A,3 | A=3 if the follower should go left, 4 if he should go right
31498 | ADC A,0
31500 | RET |
The follower and target are both on the middle floor or both on the top floor.
31501 | CP 162 | Set the zero flag if the follower and target are on the middle floor
31503 | INC HL | L=98
31504 | LD A,57 | This is the x-coordinate of the wall between the Reading and Map Rooms
31506 | JR NZ,31510 | Jump if the follower and target are on the top floor
31508 | LD A,38 | This is the x-coordinate of the wall between the White and Exam Rooms
31510 | CP (HL) | Compare the follower's x-coordinate with that of the wall
31511 | JR C,31531 | Jump if the follower is to the right of the wall
31513 | CP E | Is the target character to the left of the wall?
31514 | JR NC,31494 | Jump if so
The follower is on the left side of the skool and must go downstairs to get to the target on the other side of the skool.
31516 | LD A,(HL) | A=follower's x-coordinate
31517 | CP 12 | This is the x-coordinate of the tops of the staircases at the left of the skool
31519 | JR NZ,31496 | Jump unless the follower is at the top of a staircase
31521 | LD L,96 | Byte 96 of the follower's buffer holds his animatory state
31523 | LD A,4 | A=4 will make the follower turn round if facing left
31525 | BIT 7,(HL) | Is the follower facing left?
31527 | RET Z | Return if so
31528 | LD A,2 | A=2 will make the follower go down the stairs
31530 | RET |
The follower is on the right side of the skool. Is the target there too?
31531 | CP E | Is the target character to the right of the wall?
31532 | JR C,31494 | Jump if so
31534 | LD A,(HL) | A=follower's x-coordinate
31535 | CP 77 | This is the x-coordinate of the tops of the staircases at the right of the skool
31537 | JR NZ,31496 | Jump unless the follower is at the top of a staircase
31539 | LD L,96 | Byte 96 of the follower's buffer holds his animatory state
31541 | LD A,3 | A=3 will make the follower turn round if facing right
31543 | BIT 7,(HL) | Is the follower facing right?
31545 | RET NZ | Return if so
31546 | LD A,2 | A=2 will make the follower go down the stairs
31548 | RET |
The follower and target are on different floors.
31549 | JR NC,31565 | Jump if the target is on a floor above the follower
31551 | CP 162 | Set the zero flag if the follower is on the middle floor
31553 | INC HL | L=98
31554 | LD A,57 | This is the x-coordinate of the wall between the Reading and Map Rooms
31556 | JR NZ,31560 | Jump if the follower is on the top floor
31558 | LD A,38 | This is the x-coordinate of the wall between the White and Exam Rooms
31560 | CP (HL) | Compare the follower's x-coordinate with that of the wall
31561 | JR NC,31516 | Jump if the follower is to the left of the wall (and so needs to go to the top of a staircase on the left)
31563 | JR C,31534 | Jump if the follower is to the right of the wall (and so needs to go to the top of a staircase on the right)
The target is on a floor above the follower. Which staircase should the follower use?
31565 | LD A,D | A=target's y-coordinate
31566 | LD D,155 | This is the y-coordinate of the top floor
31568 | CP 162 | This is the y-coordinate of the middle floor
31570 | LD A,57 | This is the x-coordinate of the wall between the Reading and Map Rooms
31572 | JR C,31578 | Jump if the target is above the middle floor
31574 | LD D,162 | This is the y-coordinate of the middle floor
31576 | LD A,38 | This is the x-coordinate of the wall between the White and Exam Rooms
31578 | CP E | Compare the target's x-coordinate with that of the wall
31579 | LD E,97 | Signal: target is to the right of the wall
31581 | JR C,31585 | Jump if the target is to the right of the wall
31583 | LD E,1 | Signal: target is to the left of the wall
31585 | LD A,(HL) | A=follower's y-coordinate
31586 | CP 169 | Set the zero flag if the follower is on the bottom floor
31588 | INC HL | L=98
31589 | JR NZ,31622 | Jump if the follower is on the middle floor
31591 | LD A,19 | This is the x-coordinate of the bottoms of the staircases at the left of the skool
31593 | DEC E | Is the target to the left of the wall?
31594 | JR Z,31598 | Jump if so
31596 | LD A,70 | This is the x-coordinate of the bottoms of the staircases at the right of the skool
31598 | CP (HL) | Compare this with the follower's x-coordinate
31599 | CCF | Set the carry flag if the follower should go right
31600 | JR NZ,31496 | Jump if the follower is not at the bottom of a staircase
31602 | LD L,96 | Byte 96 of the follower's buffer holds his animatory state
31604 | CP 19 | This is the x-coordinate of the bottoms of the staircases at the left of the skool
31606 | LD A,1 | A=1 will make the follower go upstairs
31608 | JR Z,31616 | Jump if the follower should go up a staircase at the left of the skool
31610 | BIT 7,(HL) | Is the follower facing right?
31612 | RET NZ | Jump if so
31613 | LD A,4 | A=3 will make the follower turn round and face right
31615 | RET |
31616 | BIT 7,(HL) | Is the follower facing left?
31618 | RET Z | Return (with A=1) if so
31619 | LD A,3 | A=3 will make the follower turn round and face left
31621 | RET |
The follower is on the middle floor, and the target is on the top floor.
31622 | LD A,38 | This is the x-coordinate of the wall between the White and Exam Rooms
31624 | CP (HL) | Compare this with the follower's x-coordinate
31625 | LD A,97 | Signal: follower is to the right of the wall
31627 | JR C,31631 | Jump if the follower is to the right of the wall
31629 | LD A,1 | Signal: follower is to the left of the wall
31631 | CP E | Is the target on the same side of the skool?
31632 | JR Z,31591 | Jump if so (follower must go up the nearest staircase)
31634 | CCF | Set the carry flag if the follower must go down a staircase at the right of the skool, and reset it if he must go down a staircase at the left of the skool
31635 | JR 31561 |