Controlling movement of wandering NPC

In my game I have a stray cat that wanders around. I have the following code below but would like to adjust it so that:

  1. Instead of every turn is every 1-4 turns chosen randomly
  2. The Say text says which direction Bob has wandered off in if the player is in the location of Bob when he wanders off.
  3. I ‘think’ inform will report Bob entering a location if that location is where the player is?

destination is a room that varies

Bob is a person.

Bob is in chester street

Every turn:
Now destination is a random adjacent room;
if the location of bob is location of player:
Say “Bob wanders off”;
Now bob is in destination;

For the timing thing, do you want it to always be 1-4 turns, or is a random 1 in 4 chance good enough? Also, do you want Bob to only wander away from the player rather than doing so when the player isn’t present? If so, this should work:

Every turn:
	If a random chance of 1 in 4 succeeds and if the location of Bob is the location of the player:
		let the path be the best route from the location of Bob to a random room adjacent to the location of Bob;
		Try Bob going the path.

This will also solve your reporting issues – by default, Inform will tell the player if an NPC leaves or enters the room they’re in, but since your code was teleporting Bob rather than having him take a movement action, those defaults weren’t kicking in.

If you want to customize the rather-generic reporting of Bob entering or leaving, you can do that by modifying the relevant Responses – see WWI 7.6 (you’d probably be most interested in changing the describe room gone into rule responses C and L).

If you do want a more fixed timer, you’d need to think through how you want it to work (like, does it pause or does it reset if the player leaves Bob’s location before it fires?) but should be pretty easy to implement as a timing variable that counts down as part of the every turn rule, with the Bob-wandering part only firing once the variable reaches 0 (at which point you’d re-initialize it to a random number).

Hope this is helpful!

1 Like

perfect, thanks!

This seems to work fine but sometimes throws up the error:

You’ll have to say which compass direction to go in.
I didn’t understand that instruction.

Hmm, I’m not seeing that in the little sample I threw together to test the code. Is it happening when Bob is in a particular location? And can you use the RULES command to track down exactly which rule is giving off the error?

1 Like

Actually it’s not too much a problem, I made the error of starting Bob off at a tramstop that has no adjacent room except for when a tram arrives (then ‘inside’ is an option)

It’s hard to say without seeing your code, but the simple answer is probably that there are a number of reasons why a room might be adjacent to another room, but still not possible for the algorithm to find a path between the two: for instance, if there is a locked door in between. You could in theory use the best route ... using doors or ... using even locked doors to solve this, but opening a locked door with a key would be unusual behavior for a cat. The ... using doors might make things easier, but it might try to send Bob through a door, and if you’ve set up some way of generating implicit door-opening events, as you might have by using Emily Short’s Locksmith extension, for instance, then Bob may try opening those doors himself: again, pretty unusual behavior for a cat.

In any case, if you say the best route from ... to ... and there is no next move towards the selected that Inform can calculate using the parameters you specified, then the result of the the best route phrase is the special object 'nothing`, which is not a direction. You might try adapting the code more or less like this:

Every turn:
	If a random chance of 1 in 4 succeeds and if the location of Bob is the location of the player:
		let the path be the best route from the location of Bob to a random room adjacent to the location of Bob;
		if the path is a direction, try Bob going the path.

That way, the attempt to move will just fail silently if there is no viable path, instead of printing an error message for the player to see.

But this also means that Bob will just not move sometimes when he otherwise would, so you might also want to adjust if a random chance of 1 in 4 succeeds to ... chance of 1 in 3 or 1 in 2 or 2 in 5 or whatever, depending on how interconnected your rooms are.

EDIT. Code contained a silly typo, now fixed.


Ignore that direct is obviously direction! Doh! Works now.

1 Like

Thanks for catching that, Patrick! And necrodeath, I think what you’re seeing is “direct” should be “direction”.

EDIT: ninja’d :slight_smile:


Thanks both, Bob is now free to roam and will occasionally catch the tram, ace!