This is the ninth and final entry in a series of quick-start Inform 7 tutorials using examples from Colossal Cave Adventure. More information about this tutorial series can be found here: A Quick-Start Guide to Inform 7.
This lesson will cover the darkness of the cave and Adventure’s iconic lamp.
It is now pitch dark.
Inform 7 has a basic light and darkness system. Rooms can be “dark” or “lighted”, and having an object with the “lit” property will enable the player to see in dark rooms. Without a “lit” object, actions requiring visibility will be blocked with the default message “It is pitch dark, and you can’t see a thing.”
Which actions are blocked in darkness? By default, only examine, look under, and search are blocked. You can check the list for your own game by going to the Index tab, then choosing Actions, and then scrolling down to the Alphabetic listing, where the words “requiring light” will appear beside any actions requiring light.
Using the “kinds” system (discussed in lesson 7), we can set up caves as a kind of room that is dark by default.
A cave is a kind of room. A cave is usually dark.
There is a cave called Low N/S Passage.
Now every cave in the game will be dark by default.
Customizing the darkness
The regular darkness message in Adventure is different than the regular darkness message in Inform 7.
What we currently see is “It is pitch dark, and you can’t see a thing.” This is perfect for messages relating to examining and searching, but it isn’t the correct room description. What we need there is “It is now pitch dark. If you proceed you will likely fall into a pit.”
…and we need pits.
First things first: in the Inform handbook, §18.18 through §18.22 deal with the standard behavior of the dark/light system. These sections provide the names of five key rules:
- Rule for printing a refusal to act in the dark
- Rule for printing the announcement of darkness
- Rule for printing the announcement of light
- Rule for printing the name of a dark room
- Rule for printing the description of a dark room
We can overwrite any of the standard behaviors within our source code by explicitly naming the rule we want to overwrite. For example, we can edit the default description for a dark room like so:
Rule for printing the description of a dark room:
say “It is now pitch black. If you proceed you will likely fall into a pit.” instead.
The pits in Adventure are not actual objects that can be examined and interacted with. Consequently, we don’t need to create them as things. The appropriate messaging can be constructed with an “after going” rule, like so:
After going when the location is a dark room:
if a random chance of 1 in 4 succeeds:
say “You fell into a pit and broke every bone in your body!”;
end the story saying “Ow.”;
continue the action.
(Of course, the Adventure death system is more complex than “ow”, but this section has already gotten somewhat off topic.)
Identifying the rules you want to change
The Inform 7 handbook explicitly talks about the dark/light system, which makes it easy to find the rules we need to change. But it is sometimes more difficult to figure out what rules are producing a given result, especially when debugging.
Technique #1 – Debug commands
Inform 7 games run by stepping through a series of prioritized rules. The debugging command RULES ON will show you a step-by-step view of everything your game is doing. For example, here’s the rule-by-rule view for getting the lamp…
This is a bit overwhelming, but it does show that the “standard report taking rule” is responsible for printing the word “Taken”, and when.
A related technique is ACTIONS ON, which shows every action being executed.
Technique #2 – Accessing the IDE’s index
Check the Index for the specific action in question. The magnifying glass icon beside each action will take you to a detailed view of the action.
Critical actions like “look” have some notes at the top, but when you scroll past them, you’ll find details about exactly how the action works – what rules control it, what named values it includes, and what the default messages associated are.
The speech bubbles here indicate the presence of default responses that are built into the game. If we were still trying to alter the darkness messaging, we could check here for those default darkness messages by clicking the speech bubbles.
And here are the two darkness messages again!
Note the button marked “set” beside each message. This button will add code to your game that allows you to change the default message. We could have changed the darkness text this way instead of changing the activity, and this would also have been a valid solution.
The shiny brass lamp
Inform 7 has a built-in kind called the device, which can be switched on or switched off. This is extremely convenient for making, say, shiny brass lamps.
A shiny brass lamp is a device in Inside Building.
After switching on the lamp:
now the lamp is lit;
continue the action.
After switching off the lamp:
now the lamp is unlit;
continue the action.
The lamp is battery-powered (despite all my childhood impressions of something Aladdin-esque) and can only endure for so many turns. Every turn, it loses a little more power, and that phrase “every turn” is absolutely a hint as to how this can be implemented.
The shiny brass lamp has a number called the power. The power of the shiny brass lamp is 500.
Every turn when the shiny brass lamp is lit:
now the power of the shiny brass lamp is the power of the shiny brass lamp – 1;
if the power of the shiny brass lamp < 1:
say “Your lamp has run out of power.”;
now the lamp is unlit.
There are many more details that could be added to this implementation – the low battery warning message, the ability to add new batteries, the varying text when the lamp is on the ground lit or unlit. But we’ve already covered techniques for each of these…
- Low battery warning message – write an appropriate “if” statement in the every turn rule
- Adding new batteries – write a new “instead” rule for inserting batteries into the lamp
- Varying text based on the lamp’s state – write an [if the lamp is lit]/[otherwise] rule into the description
…so walking through them would be redundant.
Part of this lesson addressed the concept of rules, which are the most basic machinery of Inform 7. For the most part, it is not necessary to understand rulebooks work in order to build an Inform 7 game. To peek further under the hood, see Chapter 19: Rulebooks.
§3.15. Light and darkness
§4.2 Using new kinds
§18.18 Printing a refusal to act in the dark
§18.19 Printing the announcement of darkness
§18.20 Printing the announcement of light
§18.21 Printing the name of a dark room
§18.22 Printing the description of a dark room
§24.3. High-level debugging commands