Suppose we have a complete Encyclopedia in our game. The player is allowed to pick up the whole set (there must not be too many volumes), but also to do things with individual volumes, and indeed to scatter these volumes all over the place. Putting a volume back in the same place as the rest of the Encyclopedia should, however, restore it to the collective. We will start out by defining general rules for collectives like this:
"AARP-Gnosis"
Fitting relates various things to one thing (called the home). The verb to fit means the fitting relation. Definition: a thing is missing if it is not part of the home of it.
A collective is a kind of thing.
Before doing something to something which is part of a collective:
let space be the holder of the home of the noun;
move the noun to the space.
Instead of examining a collective:
say "[The noun] consists of [the list of things which are part of the noun]."
Now the real work begins. One reason to make this an activity is that we might easily want to override it for specific objects; for instance, the generic collecting activity here would not deal properly with collectives of clothing where some items might be worn and others not. In that case, we would want to write another, more specific "collecting" activity to handle the complexities of fashion.
Collecting something is an activity.
Every turn:
repeat with item running through collectives:
carry out the collecting activity with the item.
To remove (item - a thing) when empty:
let space be the holder of the item;
if the number of things which are part of the item is 0:
now the item is nowhere;
if the number of things which are part of the item is 1:
let the last thing be a random thing which is part of the item;
move the last thing to the space;
now the item is nowhere.
Before collecting a thing (called the item):
remove item when empty;
let space be the holder of the item;
if space is not a thing and space is not a room:
if something (called the other space) contains at least two things which fit the item, move item to the other space;
if a room (called the other space) contains at least two things which fit the item, move item to the other space;
if someone (called the owner) carries at least two things which fit the item, move item to the owner.
Rule for collecting a thing (called the item):
let space be the holder of the item;
if space is a thing or space is a room:
repeat with component running through things held by the space:
if the component fits the item, now the component is part of the item;
remove item when empty.
And now for a cheerful scenario:
The Boise Memorial Library is a room. "A concrete box of a room, roughly eight feet by fourteen, which contains all the fallout shelter has to offer by way of entertainment. Someone with a grim sense of humor has tacked a READ! literacy poster to the door, as though there were anything else to do while you await the calming of the Geiger counters." The shelf is a supporter in the Library. "A battered utility shelf stands against the south wall."
The New Idahoan Encyclopedia Set is a collective. Volume A-Aalto fits the Encyclopedia. It is part of the Set. Volume AAM-Aardvark fits the Encyclopedia. It is part of the Set. Volume Aarhus-Aaron fits the Encyclopedia. It is part of the Set. Volume AARP-Gnosis fits the Encyclopedia. It is part of the Set. Volume Gnu-Zygote fits the Encyclopedia. It is part of the Set. The Set is on the shelf.
Let's have the Encyclopedia describe itself differently depending on whether it's all in one place:
After printing the name of the Set when something missing fits the Set:
say " (missing [a list of missing things which fit the Set])"
Before printing the name of the Set when the number of missing things which fit the set is 0:
say "complete ".
Test me with "get aarhus-aaron / look / inventory / get aam-aardvark / look / get gnu-zygote / look / get aarp-gnosis / look / inventory / drop set / look / get set / get a-aalto / inventory".
The built-in behavior of Inform is to print a line after a device is examined, saying whether the item is on or off. This is often inappropriate, and we could simply turn off that behavior in general by instructing Inform to ignore the "examine described devices rule" (see the chapter on rulebooks).
Perhaps, though, we would like continue to have a short passage about the action of any switched on device; we'd just like a little more control over what it says from time to time. And in that case, we might change the rule to give a new activity control over that portion of the description:
"Aftershock"
Section 1 - Showing actions
Showing action of something is an activity.
Rule for showing action of something (called item):
if the item is switched on, say "[The item] is switched on.";
otherwise say "[The item] is switched off."
Borrowing from the rulebooks chapter, we can replace the standard "examine described devices" rule with something that uses this activity.
The new described devices rule is listed instead of the examine devices rule in the carry out examining rules.
This is the new described devices rule:
if the noun is a device:
carry out the showing action activity with the noun;
now examine text printed is true.
Thus far we have essentially replicated the original behavior, but we've made it possible to write specialized behavior for devices, and to invoke that behavior in other places:
Report switching on something:
say "You flip a switch. ";
carry out the showing action activity with the noun instead.
This might be useful for an electric lamp kind:
Section 2 - Electric Lamps
An electric lamp is a kind of device.
Rule for showing action of an electric lamp (called item):
if the item is switched on, say "[The item] is lit[if the number of visible lit things is greater than 1], competing with [the list of visible lit things which are not the item][end if].";
otherwise say "[The item] is dark."
Carry out switching on an electric lamp: now the noun is lit. Carry out switching off an electric lamp: now the noun is unlit.
Section 2 - The Scenario
The time of day is 3:47 AM. When play begins, now the right hand status line is "[time of day]".
The Downstairs Hallway is a dark room. "The only room in the house with no furniture and almost nothing on the walls. At times like this you always notice the crack in the plaster, originating near the light fixture and running almost all the way to the wall."
A plastic jug of filtered water is in the Downstairs Hallway. The description is "Five gallons, not that that will last you very long, hot as it has been lately."
The crack is scenery in the Hallway. The description is "No, the ceiling isn't going to fall on you today."
The light fixture is an electric lamp in the Hallway. It is switched on, lit, and scenery. The description is "A plain globe of frosted glass containing the light bulb. Nothing special, and you never think about it except when, as now, you are forced to spend hours in this room."
The flashlight is an electric lamp carried by the player. The description is "A shiny red flashlight." The portable radio is a device carried by the player. The description is "A small battery-operated radio which you received for free with your subscription to US News & World Report. It has served you well through many earthquakes past."
And with our activity, we can override the flashlight's electric lamp behavior with new behavior:
Rule for showing action of the flashlight:
if the flashlight is switched on, say "A strong, narrow beam of light shines from the flashlight.";
otherwise say "It is currently switched off."
...or give special actions for the radio:
Rule for showing action of the radio:
if the radio is switched on, say "Through the static, you pick up pieces of discussion: a 6.7 on the Richter scale, epicenter... something about Topanga... but it crackles out again.";
otherwise say "The radio is silent. You're saving the batteries."
Instead of listening in the presence of the switched on radio:
carry out the showing action activity with the radio instead.
Test me with "examine light / switch light off / switch flashlight on / switch radio on / examine radio / examine flashlight".
Suppose we want to add rules so that any time we examine a charred object (and most of our objects can be charred), a line about the charring is appended to the end of the object description. We could use "after examining...", but perhaps we would prefer for the sentence about the charring not to appear in its own paragraph.
This is an ideal occasion for a new activity. We look at the action index for "examining" to identify the rule that causes the old behavior (in this case, the "standard examining rule"); replace this with a new rule that calls our activity; and write our "printing the description" activity in such a way that it uses an object's description without forcing a paragraph return afterward.
Then we will use "after printing the description" to add our line about charring, and make sure that the paragraph return does occur before the prompt.
So:
"Crusoe"
Section 1 - Creating our New Activity
The fancy examining rule is listed instead of the standard examining rule in the carry out examining rules.
This instruction replaces a normal piece of the examine action, the standard examining rule, with another one of our own devising. (The replacement of the standard examining rule will be explained in more detail in the chapter on rulebooks.)
Printing the description of something is an activity.
This is the fancy examining rule:
carry out the printing the description activity with the noun;
rule succeeds.
All we have done here is enclose what is usually just a rule inside an activity. This means that we can now write before and after rules for the activity, and also add special instructions like "Rule for printing the name of something while printing the description of something" -- this may not be likely to arise often, but Inform now has the concept of "printing the description of something" as a separate context of action. Next we add the modification that lets us append to the description without a new line:
Rule for printing the description of something (called item):
if the description of the item is not "":
say "[description of item] [run paragraph on]";
otherwise:
say "You see nothing special about [the item]. [run paragraph on]".
"run paragraph on" here will mean that we do not get a paragraph break following the description, even if it ends with a period. We also insert a space, so that our follow-on comments will be properly punctuated.
After printing the description of something charred:
say "It is charred." instead.
The instead at the end of this line stops Inform for going on with any other "after printing the description of..." rules.
The standard library also has rules for printing additional text about containers and supporters with visible contents, and devices that are switched on; with this current system, we could add those as "after printing the description" rules as well, building up a complete paragraph if we wanted. But for simplicity we won't exemplify all of that here. The effects would be much the same as with the "charred" line.
Now, because we want to make sure that we always do get a paragraph break after our description, we add this rule last after all the other rules. "Last" and "first" rules are covered in more detail in the chapter on rulebooks.
Last after printing the description of something:
say paragraph break.
Section 2 - The Scenario
The Desert Isle is a room. "A pale expanse of sand, here and there developing into hillocks of grass, and a small clump of palms. The water is shallow here, and there are other islands within swimming distance -- or even wading distance, perhaps -- but none of them is any larger than your island, so it doesn't seem worth the trouble of visiting.
A few hundred feet out, the water turns darker blue, the sea floor drops away, and there is nothing to be seen all the way down to the horizon, except a couple of fluffy clouds, and an occasional bird.
The remains of your fire smolder in the stone-lined pit."
A thing can be charred or whole. A thing is usually whole. Instead of burning something: say "You hold [the noun] to the fire until it flares and chars."; now the noun is charred.
The player carries a stick. The description of the stick is "A strip of palm from the woodiest part of the leaf, about a foot and a half long."
The player carries a glass bottle and a piece of paper. The description of the paper is "A single blank sheet." In the glass bottle is a grain of sand. The glass bottle is openable and open. Instead of burning the glass bottle: say "You hold the bottle to the flame, but it grows uncomfortably warm."
Instead of burning the grain of sand: say "You drop the grain into the fire pit, where it becomes indistinguishable from all the others."; now the grain of sand is nowhere. Instead of dropping the grain of sand: now the grain of sand is nowhere; say "You return the grain of sand to its brethren."
The player's description is handled in an unusual way, and this will produce a space paragraph break there where it should not. Instead, therefore, we will add an instead for examining the player (probably a good idea anyway):
Instead of examining the player:
say "You are sunburned and there is sand in cracks you didn't know existed."
Test me with "i / x stick / x bottle / x sand / x paper / x me / burn stick / x stick / burn paper / x paper".
The "printing a description" activity may be useful for other games, and can be imported just by lifting section 1.