Witnessed 1

Batteries, swappable between devices
Example 139
★★★

A kind of battery which can be put into different devices, and which will lose power after extended use.

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:

"Witnessed"
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.
Warning about failure of something is an activity.
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."
Putting out something is an activity.
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 "turn on [device]" as switching on.
Understand "turn off [device]" as switching off.
Understand "open [openable closed thing]" as opening.
Understand "close [openable open thing]" as closing.
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.
Definition: a battery is discharged if its charge < 1.
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."
The red cellophane is part of the flashlight.
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 me with "test first / test second".
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".
Test me with "test first / test second".
Thirtieth Street Station
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.

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.

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.

>(Testing.)

>[1] test first
(Testing.)

>[2] i
You are carrying:
a D battery
a backpack (being worn and open)
a flashlight
a cassette recorder

>[3] open flashlight compartment
You open the flashlight's battery compartment.

>[4] put battery in it
(the D battery in the flashlight's battery compartment)
You put the D battery into the flashlight's battery compartment.

>[5] turn on flashlight
You switch the flashlight on.

>[6] take d battery
Taken.

The flashlight loses power and switches off!

>[7] open cassette compartment
You open the cassette recorder's battery compartment.

>[8] turn on cassette
Nothing happens, perhaps because there isn't a charged battery in the cassette recorder.

>[9] put battery in cassette compartment
(the D battery in the cassette recorder's battery compartment)
You put the D battery into the cassette recorder's battery compartment.

>[10] turn on cassette
You switch the cassette recorder on.

The cassette recorder hisses faintly.

>[11] z
Time passes.

The cassette recorder hisses faintly.

>[12] z
Time passes.

The cassette recorder hisses faintly.

>[13] z
Time passes.

The cassette recorder hisses faintly.

>[14] z
Time passes.

The cassette recorder hisses faintly.

>[15] test second
(Testing.)

>[16] get d battery
Taken.

The cassette recorder loses power and switches off!

>[17] put d battery in flashlight compartment
You put the D battery into the flashlight's battery compartment.

>[18] turn on flashlight
You switch the flashlight on.

>[19] z
Time passes.

>[20] z
Time passes.

>[21] z
Time passes.

>[22] z
Time passes.

>[23] z
Time passes.

>[24] z
Time passes.

The flashlight is obviously going to go out quite soon.

>[25] turn off flashlight
You switch the flashlight off.

>[26] z
Time passes.

>[27] z
Time passes.

>[28] turn on flashlight
You switch the flashlight on.

>[29] z
Time passes.

The flashlight loses power and switches off!