Inform 7 Home Page / Documentation
§9.5. Every turn
The passage of time in interactive fiction is broken up into a succession of turns, in each of which the player types a request and is given a response. Usually each such request triggers one action, but sometimes a whole sequence are fired off, as when the player types "get all" in a cluttered room.
As we've seen, the variable "turn count" holds the number of turns of play so far. By convention turn number 0 is the time when Inform prints up the banner and any initial text; it becomes turn number 1 when the player's first command is typed.
One of the last things to happen in each turn is that Inform will apply any rules which have been set to occur "every turn", like so:
Every turn, say "The summer breeze shakes the apple-blossom."
This is equivalent to writing:
An every turn rule: say "The summer breeze shakes the apple-blossom."
Note that the text about blossom, which will quickly become tiresome, is said at the end of every turn, not at the beginning, and in particular not before the player's first opportunity to type a command.
As usual when defining rules, we can add stipulations: any condition can be attached using "when".
Every turn when the location is the Orchard, say "The summer breeze shakes the apple-blossom."
Every turn when the player can see the rotting fish, say "Your nose twitches involuntarily."
ExampleWitnessed 1 |
The following example makes fairly ample use of material that we haven't seen yet, but gives some idea of the flexibility of every turn rules. Suppose we want to have a number of electrical devices, all of which may be powered by a set of batteries. The batteries will all need to be discharged as they are used (regardless of what device they happen to be controlling at the moment). So:
A battery is a kind of thing. A battery has a number called charge. The charge of a battery is usually 15.
Every turn:
repeat with hollow running through battery compartments:
if the hollow is part of a switched on device (called the machine):
if a battery (called cell) is in the hollow:
decrement the charge of the cell;
carry out the warning about failure activity with the machine;
if the cell is discharged, carry out the putting out activity with the machine;
otherwise:
carry out the putting out activity with the machine.
Rule for warning about failure of a device (called the machine):
if a random battery compartment which is part of the machine contains a battery (called the power source):
if the charge of the power source is 2, say "[The machine] is obviously going to go out quite soon."
Rule for putting out a device (called the machine):
say "[The machine] loses power and switches off![line break]";
silently try switching off the machine.
A battery compartment is a kind of container. A battery compartment is usually closed and openable. One battery compartment is part of every device. Instead of inserting something which is not a battery into a battery compartment, say "Only batteries should go in a battery compartment."
And to get rid of annoying messages like "Which would you like to close, the flashlight or the flashlight's battery compartment?" when only the compartment is closable, we might add some understanding instructions:
Understand "put [something] in [container]" as inserting it into.
Instead of opening a device, try opening a random battery compartment which is part of the noun. Instead of closing a device, try closing a random battery compartment which is part of the noun. Instead of inserting a battery into a device, try inserting the noun into a random battery compartment which is part of the second noun.
Instead of switching on an empty device:
say "Nothing happens, perhaps because there isn't a charged battery in [the noun]."
Instead of switching on a battery compartment which is part of a device (called the power user), try switching on the power user.
Definition: a device is empty:
if a random battery compartment which is part of it contains a battery (called the power source):
if the power source is discharged, yes;
no;
yes.
A light source is a kind of device. Carry out switching on a light source: now the noun is lit. Carry out switching off a light source: now the noun is unlit.
The flashlight is a light source. A D battery is a battery carried by the player.
The cassette recorder is a device. Every turn: if the cassette recorder is switched on, say "The cassette recorder hisses faintly."
Rule for warning about failure of the cassette recorder:
if a random battery compartment which is part of the cassette recorder contains a battery (called the power source):
if the charge of the power source is 2, say "The hiss from [the cassette recorder] begins to warble ominously."
The player wears a backpack. The backpack is openable. In the backpack is the flashlight and the cassette recorder.
The description of the cassette recorder is "Useful both for recording your notes and for capturing any odd ghostly sounds you may hear."
The description of the backpack is "An old familiar pack, which you know so well that you can find all its pockets and take things in and out of it in pitch darkness. To avoid it showing up oddly in photographs, it is entirely black, with no shiny or metallic attachments."
The description of the flashlight is "You bought a new one just for this occasion, because you were worried about bringing something too small or light. This is a heavy-duty flashlight with an adjustable-focus beam. The case is made of metal, rather than plastic, and there is a spare light-bulb inside as well. You've put a band of masking tape around the handle and written in your initials in red marker.
There is a piece of red cellophane attached to the business end of the flashlight to keep it from being overly bright."
Instead of doing something to the red cellophane: say "You need the cellophane on the flashlight so that using it does not completely destroy your night vision."
Thirtieth Street Station is a room. "A huge, high, rectangular room with coffered ceilings, which looks grand but mostly makes you feel lonely and small. There are long benches in rows down the middle of the room, and an information desk with the train times, and a series of ticket windows, none of which matters very much at the moment."
The benches are an enterable supporter. They are scenery in the Station. The information desk is scenery in the Station. Some ticket windows are scenery in the Station. Instead of examining scenery in the Station: say "You're fairly sure that whatever is going on here has nothing to do with [the noun]." Understand "window" as ticket windows.
The mural is fixed in place in Thirtieth Street. "At the north side of the station is a particularly pointless and empty annex to the main room. It is dominated by a huge relief of sorts, and this is what you remember." Understand "metal" or "relief" or "huge" as the mural. The description of the mural is "It is both stylized and confusing, but you think it might be supposed to represent the various tasks and occupations of Philadelphia's population. The portions closer to the ground look as though they have recently been subjected to a light dusting of talcum powder. No unusual prints are evident."
The wind chimes are fixed in place in Thirtieth Street. "Carefully attached to the wall with a piece of duct tape and a hook is a light-weight set of wind chimes. Someone else has been here before you, it seems." The description is "Several of your friends use wind chimes as a sort of ghost alarm, since ghosts sometimes cause very localized movements of air when there is no natural breeze."
And this last bit, borrowed from the chapter on Understanding, adds some special instructions to help Inform decide when the player is likely to be referring to a compartment and when he's likely to be referring to the device itself.
Does the player mean doing something other than searching to a battery compartment: it is unlikely. [We discourage Inform from choosing a compartment when the player uses just the name of a device or the word 'battery'.]
We also need to deal with commands like PUT BATTERY IN FLASHLIGHT, where Inform might construe BATTERY as the D battery, the flashlight's battery compartment, or the cassette recorder's battery compartment -- and might also construe FLASHLIGHT as either the flashlight's battery compartment or the flashlight itself.
Does the player mean inserting into a battery compartment:
if the noun is nothing:
it is very likely;
otherwise:
make no decision.
Does the player mean inserting a battery compartment into: it is very unlikely.
Does the player mean inserting something into a device: it is unlikely.
Does the player mean searching a battery compartment: it is very likely.
Test first with "i / open flashlight compartment / put battery in it / turn on flashlight / take d battery / open cassette compartment / turn on cassette / put battery in cassette compartment / turn on cassette / z / z / z / z".
Test second with "get d battery / put d battery in flashlight compartment / turn on flashlight / z / z / z / z / z / z / turn off flashlight / z / z / turn on flashlight / z".
ExampleText Foosball |
Suppose we want a game of foosball in which our opponent acts every turn, but does different things depending on where the ball currently lies. We can put together a sequence of every-turn rules to account for this, as follows:
The Lounge is a room. "The Lounge is appointed with everything necessary to rest and relaxation: a vending machine, a potted palm, a stack of Entertainment Weekly issues from 1993, and -- your pride and joy -- a foosball game."
The foosball game is scenery in the Lounge. Understand "table" or "football" or "foozball" or "fussball" or "soccer" as the foosball game. The game is a supporter. On the game is a small white ball. The ball can be still, approaching, receding, or unreachable. The description of the ball is "Currently [small white ball condition]."
After printing the name of the small white ball, say " ([small white ball condition])".
When play begins:
now left hand status line is "You: [score]";
now right hand status line is "Joey: [Joey's score]".
Some tiny men on sticks are part of the game. Understand "handles" as the tiny men. The description is "Okay, a couple of the tiny men have had their feet broken off, and the table surface itself is a bit warped, and the ball resembles a quail egg in respect of shape and color. This makes for a game of unusual randomness, but skill is overrated."
Instead of attacking or pulling or pushing the game when the ball is unreachable:
say "You give the table a good shove, and the ball moves ever-so-slightly.";
now the ball is still.
Instead of taking the white ball:
say "You'd forfeit the game if you did that."
Instead of turning the tiny men when the ball is unreachable:
say "The ball has somehow gotten to a mystical point on the table where it cannot be reached, no matter what. Close inspection reveals that this point has been marked in chalk with a tiny X. Not that that does any good."
Instead of turning the tiny men when the ball is approaching:
if a random chance of 2 in 3 succeeds:
if a random chance of 1 in 2 succeeds, now the ball is receding;
otherwise now the ball is still;
say "[if the ball is still]Thunk. [otherwise]Thwack! [end if]You keep the ball from reaching its goal! Now it is [small white ball condition].";
otherwise:
let Joey score.
To let Joey score:
now the ball is still;
now Joey's score is Joey's score + 1;
say "The ball rolls neatly into your goal, despite your efforts. ";
if Joey's score < score, say "You put the ball back in the center with a snap. No reason to worry yet; you're still ahead. Joey looks determined, though.";
otherwise say "After allowing a moment or two for Joey's gloating to pass, you replace it at the center."
Instead of turning the tiny men when a random chance of 1 in 13 succeeds:
if the ball is unreachable, continue the action;
now the ball is unreachable;
say "You hit the ball off-center and it rolls sluggishly into a little dip in the surface of the foosball table. ";
if Joey's score > 7, say "'You did that on purpose!' Joey exclaims indignantly.";
otherwise say "You and Joey exchange glances. This is never good."
Instead of turning the tiny men:
say "You madly rotate the tiny men on sticks! ";
if a random chance of 1 in 2 succeeds:
say "Hoorah! You hit the ball!";
now the ball is receding;
otherwise:
say "Somehow you fail to bring your monopodal player into contact with the ball."
Joey is a man in the Lounge. "Joey is hunkered over the foosball handles on his side of the table." Joey can be active or inactive.
Every turn when the ball is approaching and Joey is active:
let total be Joey's score + score;
if total > 9, make no decision;
now Joey is inactive;
let Joey score;
rule succeeds.
Every turn when the ball is unreachable and Joey is active:
let total be Joey's score + score;
if total > 9, make no decision;
now Joey is inactive;
say "Joey glares angrily at the stuck ball."
Every turn when the ball is receding and Joey is active:
let total be Joey's score + score;
if total > 9, make no decision;
if the ball is unreachable, make no decision;
now Joey is inactive;
if a random chance of 1 in 2 succeeds:
if a random chance of 1 in 2 succeeds, now the ball is still;
otherwise now the ball is approaching;
say "Joey connects with your shot. Now the ball is [small white ball condition]!";
otherwise:
now the ball is still;
say "Joey tries to block, but misses! Back it goes in the center, where it is [small white ball condition].";
increment the score.
Every turn when the ball is still and Joey is active:
let total be Joey's score + score;
if total > 9, make no decision;
if the ball is unreachable, make no decision;
now Joey is inactive;
if a random chance of 1 in 2 succeeds:
now the ball is approaching;
say "Joey hits the ball solidly down towards your goal. Now it is [small white ball condition].";
otherwise:
say "Joey fails to hit the ball in your direction. It remains [small white ball condition]."
Every turn:
let total be Joey's score + score;
if total > 9:
if Joey's score > score, end the story saying "Rats! Joey wins!";
if Joey's score < score, end the story finally saying "Victory is yours!";
if Joey's score is score, end the story saying "A perfect tie."