This will be explored more in subsequent examples, but one of the things we can do with carry out rules is extend the function of existing commands so that they do more, or have special effects in specific situations. For instance, suppose we want to have a class of electric light:
"The Dark Ages Revisited"
An electric light is a kind of device. Carry out switching on an electric light: now the noun is lit. Carry out switching off an electric light: now the noun is unlit.
This will not affect the behavior of any other devices when switched; it will also not change the way in which switching lights on and off is reported. The player will still see "You switch the sodium lamp on." or the like. In this case that is probably what we want. If we wanted a special way of describing turning on electric lights as opposed to all other devices, we could also add an after rule for the electric light class. Adding this rule to the carry out train does guarantee, though, that in no case will we manage to make the lamp lit without actually making it switched on (or vice versa).
The Stooped Corridor is a room. "A low, square-cut corridor, running north to south, stooping you over."
The sodium lamp is an electric light in the Stooped Corridor. "[if switched on]The sodium lamp squats on the ground, burning away.[otherwise]The sodium lamp squats heavily on the ground.[end if]". The description is "It is a heavy-duty archaeologist's lamp, [if switched off]currently off.[otherwise]blazing with brilliant yellow light.[end if]"
Instead of burning the sodium lamp, try switching on the lamp.
So far so easy. Since we've built the description of its light or darkness into the lamp's description, though, we may want to get rid of the "...is switched on" line that automatically follows when we look at something. For this we do need to borrow from a later chapter:
The examine devices rule is not listed in the carry out examining rules.
Test me with "x lamp / switch lamp on / look / x lamp".
Suppose we intend a game in which the player needs to cut things open on a regular basis. We'll want to check whether he has the proper tools handy, and deal graciously with commands such as CUT [something] when no tool is specified. So:
"Paddington"
A blade is a kind of thing.
Understand "cut [something] with [something]" as cutting it with.
Instead of cutting something:
if a blade (called the edge) is held by the player,
try cutting the noun with the edge;
otherwise say "Your fingernails are not sharp enough."
Cutting it with is an action applying to two things.
Check cutting it with:
if the noun is a person, say "That would hurt." instead;
if the second noun is not a blade, say "[The second noun] has not got enough of a blade." instead.
Carry out cutting it with:
increment the count of rips of the noun.
Report cutting it with:
say "You slash [the noun] with [the second noun]."
We'll need a way to account for all these cuts and rips.
Definition: a thing is ripped if the count of rips of it > 0. A thing has a number called the count of rips. After examining something ripped, say "You see [the count of rips of the noun in words] rip[s] in [the noun][if something is in the noun], revealing [a list of things in the noun][end if]."
Moreover, because open containers normally list their contents when examined but we'd prefer Paddington's to be mentioned in the ripping paragraph:
The examine containers rule does nothing when examining the teddy bear.
So far, so good. But suppose that we'd like cutting also to make containers be permanently open and impossible to close again. We could write an "instead" rule, but that would mean that only our instead instructions would take effect, overriding the normal cutting it with rules entirely. Better would be to add a second carry out rule:
Carry out cutting a container with something:
now the noun is open;
now the noun is unopenable.
Now our rule will occur whenever a container is cut, but play will still go on to the reporting stage. And indeed we can add more of these, of varying degrees of specificity:
Carry out cutting something which is part of something with something:
move the noun to the player.
Carry out cutting the quilt with something:
now the description of the quilt is "Horribly tattered."
For that matter, we might want to add a report rule as well, to occur after the "You slash..." rule, so that every time the player cuts something open which has contents, the contents will be listed.
Report cutting it with:
if the noun is open and the noun contains something,
say "Visible within [is-are a list of things in the noun]."
This time we do not add the condition to the rule (i.e., Report cutting an open noun...) If we did, this report rule would be more specific than the general report rule, and would occur first.
The Safehouse is a room.
The teddy bear is a closed thing in the Safehouse. The description is "Fluffy[if the head is part of the bear], with an outsized head[otherwise], but headless[end if]." The head is a closed part of the teddy bear. In the bear is a large wad of stuffing. In the head are a small wad of stuffing and a packet of smuggled diamonds.
The quilt is in the Safehouse. The description is "An old but comforting quilt."
The player carries a blade called a switchblade.
Here is a final nicety to get rid of the "which is closed" statement on our closed unopenable teddy bear, using an "activity" rule:
After printing the name of a closed unopenable container:
omit contents in listing.
Test me with "cut quilt with bear / cut quilt with switchblade / examine quilt / cut bear with switchblade / again / examine bear / cut head with switchblade / get diamonds / mourn loss of innocence".
In some cases, we may want to add new stages to action processing. One possibility is a stage where we check the sanity of what the player is trying to do before executing any of the other commands; so that we avoid, for instance
>EAT ROCK
(first taking the rock)
That's plainly inedible.
Here is how we might insert such a stage in our action processing, using rulebook manipulation.
"Delicious, Delicious Rocks"
Section 1 - Procedure
The sanity-check rules are a rulebook.
This is the sanity-check stage rule:
abide by the sanity-check rules.
The sanity-check stage rule is listed after the before stage rule in the action-processing rules.
Section 2 - Scenario
Candyland is a room. The lollipop tree is an edible thing in Candyland. The genuine rock is a thing in Candyland.
Sanity-check eating an inedible thing:
say "Your digestion is so delicate -- you're sure [the noun] wouldn't agree with you." instead.
Test me with "eat lollipop / eat rock".
Notice that now Inform does not try taking the rock before rejecting the player's attempt to eat it.
It is of course possible to get the same effect with
Before eating an inedible thing:
say "Your digestion is so delicate -- you're sure [the noun] wouldn't agree with you." instead.
...and in a small game with few rules, there's not much reason to add an extra stage. The ability to modify the stages of action processing becomes useful when we have a fairly large game with sophisticated modeling and want to be sure that some kinds of message (such as the sanity-check) are always handled before other things that we might be doing at the before stage (such as generating implicit actions like opening doors before going through them).
Suppose the current sequence of action handling is not quite enough for us: we'd also like to have a stage after reporting, where other characters can react to the player character's behavior after it has already happened and been reported on screen. Having such a stage is unlike using "after", because after occurs before reports and prevents them from being printed. So, for instance, we could allow the player to do any of a range of different actions that make loud noises, and have a nervous bird that reacts to all of them by flying away afterward.
To do this, we can add a new rule into the specific action-processing rules. (For a list of these, see the Rules index.) Moving rules around and adding new ones requires syntax that we will learn in the chapter on Rulebooks, but the present example is fairly straightforward:
"Noisemaking"
Section 1 - Procedure
The other-player response rule is listed after the report stage rule in the specific action-processing rules.
This is the other-player response rule:
follow the observation rules.
The observation rules is a rulebook.
Section 2 - Scenario
Country Lane is a room. West of Country Lane is Outside the Farmhouse. East of Country Lane is Village Center. North of Country Lane is Open Field.
The player carries a drum.
The black crow is an animal in Country Lane.
Singing is an action applying to nothing. Understand "sing" as singing.
Report singing:
say "You hum a little ditty."
Singing is a loud action.
Attacking the drum is a loud action.
The block attacking rule is not listed in any rulebook.
Report attacking something:
say "THWACK!"
An observation rule for loud action in the presence of the black crow:
let N be a random adjacent room;
if N is a room, move the black crow to N;
say "The crow, startled, flies off to [N]."
Test me with "sing / g / n / hit drum".