Is there a way to create an NPC who walks along a set path indefinitely (but stops when interacting with the player)? There’s an example in the documentation for setting a path for an interactive NPC using a table, but the NPC cycles through it once and then stops; I want it to keep going, and I don’t want it moving at random either (it’s a patrolling robot).
Have you looked at the extension Patrollers by Michael Callaghan? (To download, right-click on “raw” and choose “save as.”)
The example you found is probably “Oddyssey,” #274 in the Recipe Book. It uses a table to keep track of the NPC (Athena):
Table of Athena's Movement destination Thebes Delphi [...] Every turn when Athena is active: repeat through the Table of Athena's Movement: let last space be the location of Athena; if Athena can be seen by the player, say "Athena heads to [the destination entry]."; move Athena to destination entry; if Athena can be seen by the player, say "Athena arrives from [the last space]."; blank out the whole row; break.
The example blanks out the table as Athena moves. You can modify it to have her cycle through the destinations by using the row number.
Table of Athena's Movement destination Athens [have to include her initial location for the code below to work well] Thebes Delphi Thebes Athens Corinth Mycenae N is a number that varies. N is usually 1. Every turn when Athena is active: let last space be the location of Athena; if the number of rows in the Table of Athena's Movement is N: [it's the end of the table, go back to 1st row next] now N is 0; increase N by 1; if Athena can be seen by the player, say "Athena heads to [the destination in row N of the Table of Athena's Movement]."; move Athena to the destination in row N of the Table of Athena's Movement; if Athena can be seen by the player, say "Athena arrives from [the last space].".
Caution: if the NPC can’t teleport around, make sure the whole chain is from adjacent room to adjacent room. Also carry over the example’s active/passive handling so your NPC doesn’t wander off while the PC’s trying to interact. For the code snippet I gave, include the room where the NPC starts in the table of destinations if you’re doing a simple circular patrol.
It’d be a small fix to check teleporting (though in some cases it might not be bad, like if there’s a secret passage) :
unless the last space is adjacent to the location of athena:
say "Whoops, someone teleported."
If you store a series of directions instead of rooms, and use “try [NPC] going [direction]”, the library will take care of reporting their movements and checking for passibility.
You can also use the “try going” approach with stored destinations rather than stored directions, with a bit of pathfinding trickery:
[code]The Red Room is a room. The Red Room is north of the Purple Room.
The Orange Room is a room. The Orange Room is east of the Red Room.
The Yellow Room is a room. The Yellow Room is east of the Orange Room.
The Green Room is a room. The Green Room is south of the Yellow Room.
The Blue Room is a room. The Blue Room is west of the Green Room.
The Purple Room is a room. The Purple Room is west of the Blue Room.
Table of Robot Destinations
The Red Room
The Yellow Room
The Blue Room
N is a number that varies. N is usually 1.
if the location of the robot is the destination in row N of the Table of Robot Destinations:
increase N by 1;
if N is greater than the number of rows in the Table of Robot Destinations:
now N is 1;
let the heading be the best route from the location of the robot to the destination in row N of the Table of Robot Destinations;
try the robot going the heading.
The robot is a person. The robot is in the Red Room.
The player is in the Red Room.[/code]
In this example, the robot should do a complete circuit of the rooms every six turns. Since the row index is only incremented upon successful arrival, the destinations need not be adjacent, as long as there exists some valid path between them. (Note, however, that the robot will always follow the shortest path, so complex room layouts may yield unexpected results unless you’re careful to set intermediate waypoints.)
I’ve got it working now. Thanks for the help, everyone!