Inform 7 Home Page / Documentation
§19.9. Basis of a rulebook
Every rulebook works on a value supplied to it, though it doesn't always look that way. The kind of the value is called its "basis"; for example, if a rulebook works on a number, it's called a "number based rulebook". Most of the rulebooks seen up to now have been action based rulebooks:
Instead of eating the cake: ...
"Instead" is an action based rulebook, and the action it works on is the one currently being processed. Besides before, after and instead, other action based rulebooks include the check, carry out, and report rules; general rulebooks such as every turn rules, the visibility rules, the turn sequence rules; and rules specially for dealing with the actions of other characters, such as the persuasion and unsuccessful attempt rules. But we have also seen object based rulebooks:
Rule for reaching inside the flask: ...
"Reaching inside" is an object based rulebook, and here we're giving it a rule which applies if the object is the flask. Inform would reject something like:
Rule for reaching inside 100: ...
because 100 has the wrong kind to fit - it's a number, not an object. There are many object based rulebooks, because most activities built-in to Inform act on objects. For example, the "printing the name of" activity has three rulebooks attached to it: before printing the name of, for printing the name of, after printing the name of. All of these are object based rulebooks.
Finally, we've also seen scene based rulebooks (which is how rules like "when a recurring scene ends" worked, in the Scenes chapter).
If a rulebook is declared like so:
Marvellous reasoning is a rulebook.
then it is an action based rulebook. If we want something different, we must write something like this:
Grading is a number based rulebook.
Grading 5: say "It's five. What can I say?" instead.
Grading an odd number (called N): say "There's something odd about [N]." instead.
Grading a number (called N): say "Just [N]." instead.
When play begins:
repeat with N running from 1 to 10:
say "Grading [N]: ";
follow the grading rulebook for N.
which produces:
Grading 1: There's something odd about 1.
Grading 2: Just 2.
Grading 3: There's something odd about 3.
Grading 4: Just 4.
Grading 5: It's five. What can I say?
Grading 6: Just 6.
Grading 7: There's something odd about 7.
Grading 8: Just 8.
Grading 9: There's something odd about 9.
Grading 10: Just 10.
Here we needed a variation on "follow" which supplies the value to apply to:
follow (values based rule producing values) for (value)
This phrase causes the rule to be obeyed immediately (rather than simply at predetermined times such as when a particular action is being tried, or at the end of every turn, and such), and applies it to the value given, which must be of a matching kind. Example:
follow the reaching inside rulebook for the electrified cage;
And here is an example based on objects:
The flotation rules are an object based rulebook.
A flotation rule for the cork: rule succeeds.
A flotation rule for an inflated thing: rule succeeds.
A flotation rule: rule fails.
And we might use the flotation rules in a circumstance like this:
After inserting something into the well:
follow the flotation rules for the noun;
if the rule succeeded:
say "[The noun] bobs on the surface.";
otherwise:
now the noun is nowhere;
say "[The noun] sinks out of sight."
|
ExampleFlotation
Objects that can sink or float in a well, depending on their own properties and the state of the surrounding environment.
|
|
Here we want a rulebook to determine whether objects float or sink, so we create an object-based rulebook for the purpose. The more specific rules here, pertaining to corks and to inflated things, will be consulted first; then, as a default, the general flotation rule.
We also want a switch that can turn flotation off at will. The rule about the big switch will be observed before the others because the when... clause makes it more specific than the other rules in the flotation rulebook.
If we wanted, we could also put these rules into a rulebook in an explicit order, overriding Inform's automatic sorting by specificity.
"Flotation"
The Pumping House is a room.
A well is a fixed in place container in the Pumping House.
Instead of examining the well:
say "[if something is in the well]On the surface of the water you can see [a list of things in the well][otherwise]There is nothing on the surface of the water, nor can you see into the depths[end if]."
The well bottom is a container.
The cork, the rubber ring and a lead ingot are in the Pumping House.
A big switch is a fixed in place device in the Pumping House. "A big switch labelled 'MAKE EVERYTHING SINK' is mounted on one wall[if switched on]. It crackles with electricity[otherwise]. It is currently switched off and silent[end if]."
A thing can be inflated or uninflated. A thing is usually uninflated. Before printing the name of an inflated thing: say "inflated ".
The rubber ring is inflated.
The flotation rules are an object-based rulebook.
A flotation rule for the cork: rule succeeds.
A flotation rule for an inflated thing: rule succeeds.
A flotation rule when the big switch is switched on: rule fails.
After inserting something into the well:
follow the flotation rules for the noun;
if the rule succeeded:
say "[The noun] bobs on the surface.";
otherwise:
move the noun to the well bottom;
say "[The noun] sinks out of sight."
A thing can be sinking, rising, or static. A thing is usually static.
Definition: a thing is wet:
if it is in the well, yes;
if it is in the well bottom, yes;
no.
Every turn:
now every thing is static;
repeat with item running through wet things:
follow the flotation rules for the item;
if the rule failed and the item is in the well, now the item is sinking;
if the rule succeeded and the item is in the well bottom, now the item is rising;
now every rising thing is in the well;
now every sinking thing is in the well bottom;
if something is rising, say "[The list of rising things] rise[if the number of rising things is 1]s[end if] to the surface of the well.";
if something is sinking, say "[The list of sinking things] sink[if the number of sinking things is 1]s[end if] out of sight."
And finally a few description rules to make things look prettier:
Rule for writing a paragraph about the well when the well contains something:
say "The chief feature of the room is a concrete-sided well in which there float[if the number of things in the well is 1]s[end if] [a list of things in the well]."
Rule for writing a paragraph about the well:
say "The chief feature of the room is a concrete-sided well full of water."
As we recall from the chapter on activities, "writing a paragraph about..." is an activity; activities are themselves structured as sets of object-based rulebooks. The activity "writing a paragraph about" uses three object-based rulebooks (before writing..., for writing..., after writing...). We could have made a flotation activity as well, but in general it is overkill to make an activity to make success/failure decisions. For that purpose an object-based rulebook is sufficient.
Test me with "get all / put cork in well / put ring in well / put ingot in well / x well / get cork / get ring / switch switch on / put cork in well / put ring in well / x well / switch switch off / switch switch on".