It can be handy to develop simplistic NPCs that can wander aimlessly (in computing terms, “the Drunken Walk” algorithm), or follow other NPCs or the player. After adapting a few other examples into something that works, I hereby present “Drunken Master.” The player can WANDER or FOLLOW WONG (who is wandering) or FOLLOW DOG (who is following Wong). Anyone can copy or mix into their projects, or into the documentation or recipe book for Inform 7. If you spot a bug, please let me know. I’m sure there’s some weirdness around doors I didn’t spot, but it handles loopback paths okay.
"Drunken Master" by Ed Halley Section 1 - Following and Wandering [Moving from room to room in such a way that you can see and follow them.] To move (someone - a person) obviously to (there - a room): let here be the holder of someone; if here is not there begin; [there is not here, begin;] change the last location of someone to here; if someone is visible and someone is not the player, say "[Someone] heads to [there]."; move someone to there; if someone is visible and someone is not the player, say "[Someone] arrives from [here]."; end if. [Following] A person has a room called last location. Understand "follow [any person]" as following. Understand the commands "chase" and "pursue" as "follow". Following is an action applying to one visible thing. Check following: if the noun is not a person, say "You can't follow [the noun]." instead; if the noun is the actor, say "Whom are you asking to follow you?" instead; if the noun is in the location of the actor, say "[The noun] is right next to you." instead; if the last location of the noun is not the location of the actor, say "It isn't clear where [the noun] has gone." instead. Carry out an actor following: let the destination be the location of the noun; if the destination is not a room, say "You can not follow [the noun] into [destination]." instead; let aim be the best route from the location of the actor to the destination; if aim is not nothing begin; if the actor is the player then say "(heading [aim])[line break]"; if the actor is not the player and the actor is visible then say "[The actor] follows [the noun] away."; change the last location of the actor to the location of the actor; if the actor is not the player then silently try the actor going aim; otherwise try the actor going aim; if the actor is not the player and the actor is visible then say "[The actor] follows [the noun] here."; end if. [Wandering] Wandering is an action applying to nothing. Understand "wander" as wandering. Carry out an actor wandering: let here be the location of the actor; let there be a random room which is adjacent to here; if here is not there then move the actor obviously to there. Section 2 - The Setting The description of a room is "A gritty, grungy place with no redeeming value." Village Street is a room. Vacant Corner is a room. It is north of Village Street. Desolate End is a room. It is northeast of Village Street and east of Vacant Corner. Dirty Road is a room. It is south of Desolate End and east of Village Street. Section 3 - Cast of Characters Wong Fei-Hung is a man. Wong Fei-Hung is proper-named. The description of Wong Fei-Hung is "Wong appears too drunk to fight." When play begins, move Wong Fei-Hung to Desolate End. Every turn: if a random chance of 1 in 2 succeeds begin; try Wong Fei-Hung wandering; otherwise; if Wong Fei-Hung is visible, say "Wong Fei-Hung takes a sloppy drink of something smelly from a large gourd."; end if. Gou is an animal. Gou is proper-named. Understand "dog" as Gou. The description of Gou is "Gou the mutt was probably born in an alley and raised on scraps." When play begins, move Gou to Dirty Road. Every turn: try Gou following Wong Fei-Hung. Test me with "wander / g / g / follow Wong / g / g / follow Gou / g / g / z / z".