[i7] Random direction in a say message

How could i do this:

say “You hear a noise coming from [random exitable direction].”

This seems to work (assuming you haven’t already defined “exitable”):

[code]Definition: A direction (called the way) is exitable if the room-or-door the way from the location is not nothing.

Every turn: say “You hear a noise coming from the [random exitable direction].”
[/code]

I haven’t tested it on doors, though, and if you can go up or down you’ll need to figure out a way to get that to work (you don’t want it to say “You hear a noise coming from the up.”)

Also, this only picks up map connections that are really in the source code; if you have some trickery like “Instead of going east from the Portal Room: move the player to a random room” then east won’t show up as exitable from the Portal Room.

This should get you started:

[code]“Strolling” by “Victor Gijsbers”

Laboratory is a room.
Park is south of Laboratory.
Grove is east of Park.
Grove is southeast of Laboratory.

Every turn:
if at least one room is adjacent to the location of the player:
let home be the location of the player; [needed because of a bug in Inform 7]
let place be a random room which is adjacent to home;
let way be the best route from home to place;
say “You could go [way].”.[/code]
Edit: though I guess that Matt’s solutions is actually more elegant.

Well, except I forgot to include a check that at least one direction is exitable from the location, so if you ever run my rule in a room with no exits you’ll get a runtime error. Always check before you use “a random (something)” unless you’re very sure it exists!

EDIT: That said, I believe my solution picks up doors and Victor’s solution does not (a room on the other side of a door is not considered “adjacent,” per 6.14 of the documentation).

I’d suggest something like this.

[code]Definition: A direction (called the way) is exitable:
if the room-or-door way from the location of the player is a room begin;
decide yes;
otherwise if the room-or-door way from the location of the player is a door;
let chosen door be the door way from the location of the player;
let chosen room be the other side of the chosen door from the location of the player;
if the chosen room is not nothing begin;
decide yes;
otherwise;
decide no;
end if;
otherwise;
decide no;
end if.

Every turn: say “You hear a noise coming from the [random exitable direction].”.[/code]

This will stop directions leading to dead end doors from being listed as exitable.

Hope this helps.

OK if I use

Definition: A direction (called the way) is exitable if the room-or-door the way from the location is not nothing and it is not up and it is not down.

and

say "You feel a [one of]draft[or]chill[or]breeze[purely at random] coming from [the random exitable direction], making you [one of]shudder[or]shiver[or]tremble[or]cringe[or]quiver[purely at random].";

In a room with only an up exit, I get:

You feel a draft coming from nothing, making you tremble.

It otherwise seems to prevent the code from executing on up/down exits. Ideas? It looks like it should work.

There is not exitable direction, so your token [the random exitable direction] evaluates to nothing. As Matt said in his second post, you should add a check whether there are any exitable directions before using a random one.

You can fix that with something like this.

[code]Definition: A direction (called the way) is exitable:
if the way is up or the way is down begin;
decide no;
otherwise if the room-or-door way from the location of the player is a room;
decide yes;
otherwise if the room-or-door way from the location of the player is a door;
let chosen door be the door way from the location of the player;
let chosen room be the other side of the chosen door from the location of the player;
if the chosen room is not nothing begin;
decide yes;
otherwise;
decide no;
end if;
otherwise;
decide no;
end if.

Every turn when there is at least one exitable direction: say “You feel a [one of]draft[or]chill[or]breeze[purely at random] coming from [the random exitable direction], making you [one of]shudder[or]shiver[or]tremble[or]cringe[or]quiver[purely at random].”.[/code]

Hope this helps.

In fairness, that probably would make me tremble.

The Draft from Nothing is, I believe, Lovecraft’s classic tale of uncanny authorship.

Well, he’d know all about that :slight_smile:

You can also make it work with “up” and “down” by defining a slightly more complex text substitution, like this:

[code]Laboratory is a room.
Park is south of Laboratory.
Grove is east of Park.
Grove is southeast of Laboratory.
Treetops is above Grove.
Lake is a room.
The wrought gate is a door. The wrought gate is east of Grove and west of Lake.

Definition: A direction (called the way) is exitable if the room-or-door the way from the location is not nothing.

Every turn when there is an exitable direction: say “You hear a noise coming [from the random exitable direction].”

To say from the (way - a direction):
if the way is:
– up: say “from above”;
– down: say “from below”;
– otherwise: say “from [the way]”.[/code]

NB: If you’re worried about dead-end doors, you might want to use climbingstars’ more complex definition of “exitable,” though it depends partly on if you have dead-end doors, and why.

Oooh. That’s crafty. I think I might try that.

*Edit: this was the most elegant solution. Thanks everyone!