| Routines

29361: Control a policeman (2)

The address of this primary command routine is placed into bytes 8 and 9 of a policeman's buffer by the routine at 29354. It makes the policeman continue patrol duty, and chase and pick up Sam when appropriate.
Input
H | 222 or 223 (policeman)
29361 | CALL 63128 | Exit now if the policeman is midstride
29364 | CALL 31998 | Is Sam wanted by the police at the moment?
29367 | RRA
29368 | JR NC,29476 | Jump if not
29370 | LD L,8 | Change the address of the primary command routine in bytes 8 and 9 of the policeman's buffer to 29374 (below)
29372 | LD (HL),190
This entry point is used when the policeman is not chasing Sam.
29374 | CALL 63128 | Exit now if the policeman is midstride
29377 | CALL 31998 | Is Sam wanted by the police at the moment?
29380 | RRA
29381 | JR C,29397 | Jump if so
29383 | LD L,29 | Clear any disguise identifier and timeout counter in byte 29 of the policeman's buffer
29385 | LD (HL),0
29387 | LD L,3 | Reset bit 7 of byte 3 of the policeman's buffer (no need to run if Sam's not wanted by the police)
29389 | RES 7,(HL)
29391 | LD L,8 | Change the address of the primary command routine in bytes 8 and 9 of the policeman's buffer to 29361
29393 | LD (HL),177
29395 | JR 29476 | Determine the policeman's next move
Sam is wanted by the police.
29397 | CALL 29559 | Make the policeman start chasing Sam if he spots him, or go to Sam's last known whereabouts if Sam's disguise has become known to the police
29400 | JR NC,29419 | Jump if the policeman is now doing either of those things
29402 | LD DE,32732 | 32732 holds the y-coordinate of the location the police should be heading for to get Sam
29405 | LD A,(DE) | Pick it up in A
29406 | AND A | Do the police know where they should be heading to get Sam?
29407 | JR Z,29476 | Jump if not to make the policeman continue to go about his normal business
29409 | DEC E | DE=32731
29410 | LD BC,3 | Copy the coordinates and location identifier of the spot to which the police should be heading to get Sam into byte 10-12 of the policeman's buffer, thus making it his destination
29413 | LD L,10
29415 | EX DE,HL
29416 | LDIR
29418 | EX DE,HL
29419 | LD L,3 | Set bit 7 of byte 3 of the policeman's buffer (to make him run)
29421 | SET 7,(HL)
29423 | LD L,8 | Change the address of the primary command routine in bytes 8 and 9 of the policeman's buffer to 29429 (below)
29425 | LD (HL),245
29427 | JR 29476 |
This entry point is used when the policeman is chasing Sam or heading to his last known whereabouts.
29429 | CALL 31998 | Should the police be chasing Sam at the moment?
29432 | RRA
29433 | JR NC,29374 | Jump if not
29435 | LD A,(32713) | Collect the ID of Sam's current disguise from 32713
29438 | ADD A,160 | B=160-167
29440 | LD B,A
29441 | EX DE,HL | Transfer the policeman's character number to D briefly
29442 | LD C,21 | Point BC at byte 21 of one of the pages 160-167, which hold the values 128, 64, 32, 16, 8, 4, 2 and 1
29444 | LD HL,32744 | The disguise status flags are stored at 32744
29447 | LD A,(BC) | Set the zero flag if Sam's disguise is unknown to the police
29448 | AND (HL)
29449 | EX DE,HL | Transfer the policeman's character number back to H
29450 | JR Z,29467 | Jump if Sam's disguise is unknown to the police
29452 | CALL 28942 | Is the policeman close enough to Sam to pick him up?
29455 | JR NC,29467 | Jump if not
29457 | XOR A | Clear the y-coordinate of the spot to which the police should run to get Sam at 32732 (Sam has been caught)
29458 | LD (32732),A
29461 | LD L,29 | Clear any disguise ID and timeout counter in byte 29 of the policeman's buffer
29463 | LD (HL),A
29464 | JP 31642 | End the game if Sam has less than $46, or make the policeman pick Sam up
Sam's disguise is unknown to the police, or the policeman is not close enough to Sam to pick him up.
29467 | CALL 63128 | Exit now if the policeman is midstride
29470 | CALL 29559 | Make the policeman start chasing Sam if his disguise becomes known
29473 | CALL 29052 | Make the policeman switch a light on if possible
29476 | CALL 60812 | Determine the next move the policeman should make to reach his destination
This entry point is used by the routine at 62976 with A=1-6.
29479 | CP 5 | Is the character standing behind or in front of a closed door?
29481 | JP NC,63100 | Jump if so
29484 | AND A | Is the character already at his destination?
29485 | JP NZ,63036 | Jump if not
The policeman has reached his destination.
29488 | LD L,8 | Collect the LSB of the primary command routine address from byte 8 of the policeman's buffer
29490 | LD A,(HL)
29491 | CP 245 | Is the primary command routine address 29429 (which is true if the policeman was either chasing Sam or heading towards his last known whereabouts)?
29493 | JR C,29536 | If not, send the policeman to a randomly chosen spot on the sidewalk or road
29495 | LD DE,32732 | Collect the y-coordinate of the location to which the police should run to get Sam from 32732
29498 | LD A,(DE)
29499 | AND A | Do the police know where to run to get Sam?
29500 | JR Z,29515 | Jump if not
29502 | LD L,2 | Point HL at byte 2 of the policeman's buffer
29504 | CP (HL) | Compare the policeman's y-coordinate with that of the spot to which the police should run to get Sam
29505 | DEC DE | DE=32731
29506 | JR NZ,29525 | Jump unless the policeman's y-coordinate matches
29508 | DEC L | L=1
29509 | LD A,(DE) | A=x-coordinate of the spot to which the police should run to get Sam
29510 | SUB (HL) | Does it match the policeman's x-coordinate?
29511 | JR NZ,29525 | Jump if not
29513 | INC E | Set 32732 to 0 now that a policeman has made it to the spot to which the police should run to get Sam
29514 | LD (DE),A
29515 | LD L,3 | Reset bit 7 of byte 3 of the policeman's buffer (to make him stop running)
29517 | RES 7,(HL)
29519 | LD L,8 | Change the address of the primary command routine in bytes 8 and 9 of the policeman's buffer to 29374 (above)
29521 | LD (HL),190
29523 | JR 29536 | Send the policeman to a randomly chosen spot on the sidewalk or road
29525 | LD L,10 | Copy the coordinates and location identifier of the spot to which the police should run to get Sam from 32731 into bytes 10-12 of the policeman's buffer, thus making it his destination
29527 | LD BC,3
29530 | EX DE,HL
29531 | LDIR
29533 | EX DE,HL | Point HL back at the policeman's buffer
29534 | JR 29476 | Determine the policeman's next move
This entry point is used by the routine at 29354. It sets the policeman's destination to a randomly chosen spot on the sidewalk or road.
29536 | CALL 61823 | Get a random number in A
29539 | OR 0 | The operand of this OR instruction is changed by the routine at 29952: to 0 (using the event entry at 24544), or to 192 (using the event entry at 24727) to keep the policeman close to Sam's office
29541 | AND 253 | Discard bit 1
29543 | LD E,A | Copy this x-coordinate to E
29544 | CALL 60165 | Set the carry flag if there is no sidewalk at this x-coordinate
29547 | ADC A,34 | A=34 (if there is sidewalk) or 35 (if there is no sidewalk)
29549 | SUB E
29550 | LD D,A | Copy this y-coordinate to D
29551 | LD C,0 | Clear bit 0 of C to indicate that the destination is not indoors
29553 | CALL 29076 | Set the policeman's destination to the coordinates in E, D and C
29556 | JR 29476 | Determine the policeman's next move