NPCs moving between rooms within a region

I’m trying to have a NPC randomly move between rooms in order to search for the player. The only catch is the NPC is restricted to a specific region.

I’ve got this code:

Dragon Accessable is a region. Denied is a region.

TestingCenter is a room in Dragon Accessable. TestingNorth is a room in Dragon Accessable. It is north of TestingCenter. TestingEast is a room in Dragon Accessable. It is east of TestingCenter. TestingSouth is a room in Denied. It is south of TestingCenter. TestingWest is a room in Denied. It is west of TestingCenter. TestingNorth is northwest of TestingEast. TestingEast is northeast of TestingSouth. TestingSouth is southeast of TestingWest. TestingWest is southwest of TestingNorth.

[m300/m349]Erdeth is a man in TestingCenter. The description of Erdeth is "The great red dragon Erdeth, grown too large to escape his lair and having eaten everything he can reach, now spends most of his time hibernating and waiting for food to come to him." Before printing the name of Erdeth when looking, say "the great dragon ". Understand "great dragon" or "dragon" as Erdeth.

Every turn (this is the hungry dragon rule):
	If the location of Erdeth is the location of the player:
		say "[one of]Erdeth snuffles around, trying to find where the delicious scent of food is coming from.[or]Erdeth turns over a nearby rock, hoping to find something - or someone - to eat.[or]Erdeth lashes his tail angrily, narrowly missing your head.[then at random]";
		hunt the player;
	Otherwise:
		hunt the player.

To hunt the player:
	let origin be the location of Erdeth;
	let nextspace be a random room in Dragon Accessable which is adjacent to origin;
	let way be the best route from the origin to nextspace;
	say "debug - origin is [origin].";
	say "debug - nextspace is [nextspace].";
	say "debug - way is [way].";
	If Erdeth is visible:
		say "With a frustrated whine, Erdeth moves [way], trying to find someone to eat.";
	try Erdeth going way;
	If Erdeth is visible:
		say "Erdeth screams in rage as he spots you!".

It works fine if I take out the ‘in Dragon Accessable’ bit in the hunt the player routine… but then the dragon goes wandering into rooms he’s not allowed into. What can I do to make this work properly?

Also I wish Inform had a spellcheck function… I had no idea ‘accessible’ was spelled with an i…

What goes wrong with the code you have posted? You only said what goes wrong if you delete “in Dragon Accessible”.

It won’t pick a room for Erdeth to go to, instead he tries to move to nowhere. :confused:

I get this output:

Inform doesn’t parse this line as expected: let nextspace be a random room in Dragon Accessable which is adjacent to origin; I can’t tell why, nor if it is to be considered a bug or not. The solution is to break the line down and impose the restrictions you want to put on the choice of nextspace in sequential chunks that Inform can swallow:

To hunt the player:
	let origin be the location of Erdeth;
[	say "debug - origin is [origin].";]
	let nextspace be a random room which is adjacent to origin;
[	say "debug - nextspace is [nextspace].";]
	while nextspace is not in Dragon Accessable:
		let nextspace be a random room which is adjacent to origin;
[		say "debug - nextspace is [nextspace].";]
	let way be the best route from the origin to nextspace;
[	say "debug - way is [way].";]
	if Erdeth is visible:
		say "With a frustrated whine, Erdeth moves [way], trying to find someone to eat.";
	try Erdeth going way;
	if Erdeth is visible:
		say "Erdeth screams in rage as he spots you!".

You could also define an adjective:

Definition: A room is dragon-friendly if it is in Dragon Accessable.

and then write:

let nextspace be a random dragon-friendly room which is adjacent to origin;

Thanks! I went with setting an adjective, and it works now.

Belatedly, it is a bug, 632 it’s a case like 645 where Inform reads ``which is adjacent to origin’’ as describing Dragon Accessible, not the random room. That should be a bug for applying a room-to-room relation to a region; I’ll see if we have it on file.