Welcome to Adventure: Lesson 5 (Changing Existing Actions)

Introduction

This is the fifth 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.

Overview: The Bird and the Snake

As you head into Colossal Cave, you discover a bird singing to itself. The bird is a brave little fowl that can drive off the giant snake you encounter later, but only if you catch it in the cage.

the bird and the snake

However, the bird is frightened by the rod, so you can only catch it when you aren’t carrying the rod.

the bird and the rod

This lesson will cover creating all four of these objects (the bird, the snake, the cage, and the rod) and then cover all alterations to the bird’s behavior to produce the results above.

Before continuing, create the key rooms – In Debris Room, Orange River Chamber, Hall of the Mountain King, and at least one passage beyond Hall of the Mountain King, such as Low N/S Passage. For convenience, I am cutting out all the intervening rooms:

There is a room called In Cobble Crawl.
There is a room called Orange River Chamber. Orange River Chamber is west of In Cobble Crawl.
There is a room called Hall of the Mountain King. Hall of the Mountain King is west of Orange River Chamber.
There is a room called Low N/S Passage. Low N/S Passage is north of Hall of the Mountain King.

This is obviously a very bad implementation of Adventure, but it will suffice for these purposes.

Creating People

People are a kind of thing in Inform 7. They inherit all the innate properties of things, but they have additional properties that are specific to people.

In addition to the generic “person”, the people category is broken down into men, women, and animals (which can be themselves male or female.)

The bird and the snake are animals. You can create them just like creating a thing, but you must specify that they are animals.

A cheerful little bird is an animal. The cheerful little bird is in Orange River Chamber. The initial appearance of a cheerful little bird is “A cheerful little bird is sitting here singing.”

A huge green fierce snake is an animal. The huge green fierce snake is in Hall of the Mountain King. The initial appearance of a huge green snake is “A huge green fierce snake bars the way!”

The Rod and the Cage

Before continuing, let’s create the rod and the cage. I’m putting the rod in In Cobble Crawl for convenience.

There’s something interesting about the rod, which is that its description is longer than just a few words. Even in your inventory, the rod is “a black rod with a rusty star on the end”. Not only is this a pain to type repeatedly, but it confuses the system badly enough that it won’t compile.

rod won't compile

To handle this properly, you can build the black rod with a much shorter name and then change its printed name property. Here’s the source:

A black rod is in In Cobble Crawl. The printed name of the black rod is “black rod with a rusty star on the end”.

…and here’s the result, examined with the debug command SHOWME.

showme on black rod

From the parser’s perspective, the only words applying to the rod are “black” and “rod”. The printed name property only affects the display. But the player could reasonably type in a number of other things, such as “rusty” or “star”. These synonyms can be added with “Understand” in the same way that synonyms were added to the forest back in Lesson 2:

Understand “rusty/star” as the black rod.

Creating the cage was also covered in Lesson 2, so I’ll pick up the source from there and move on.

A small wicker cage is an open openable container. A small wicker cage is in In Cobble Crawl. The initial appearance of a small wicker cage is “There is a small wicker cage discarded nearby.” The description of the cage is “It’s a small wicker cage.”

Getting the Bird

can't pick up the bird

As shown above, Inform won’t normally allow you to get a person (or in this case, an animal.) Let’s change that.

The first thing we need to do is look up what action we are using, through Index -> Actions and then checking the alphabetical list. This is important because Inform implements many synonyms for the same command.

what command is get actually

The command for getting something is actually taking.

If you look back at Lesson 4, you’ll find that actions are broken into three sets of rules:

  • check rules (verifying all requirements for the action)
  • carry out rules (actually executing the action),
  • report rules (messaging the player about the execution of the action).

Clicking on the magnifying glass beside taking provides a detailed look at the structure of the taking verb, broken into those three parts.

taking verb

Reading this list, it’s clear that we’re being blocked by the can’t take other people rule. But what if that wasn’t obvious? The debug command RULES will show a list of all rules being executed in a turn.

rules list for can't take other people

If we want to interfere with an existing action, there are three ways to do that. The three ways to interfere with existing actions are before rules, instead of rules, and after rules.

  • Before rules precede the check rules.
  • Instead of rules come after the check rules, but before the carry out rules.
  • After rules come after the carry out rules and replace the report rules.

In this case, we need to intervene before the can’t take other people rule can shut down the action. This is a before rule.

Before taking the cheerful little bird:
    if the player is carrying the black rod:
        say “The bird was unafraid when you entered, but as you approach it becomes disturbed and you cannot catch it.”;
        stop the action;
    else if the player is carrying the small wicker cage:
        now the cheerful little bird is inside the small wicker cage;
        say “You catch the bird in the wicker cage.”;
        stop the action;
    else:
        say “You can catch the bird, but you cannot carry it.”;
        stop the action.

Now is a word that changes the game state. This was covered briefly in Lesson 4, where we looked at the syntax “now the player is in…” to move the player to another room.

The logic system if and else was also covered in Lesson 4, when we looked at teleporting the player from place to place.

Stop the action does exactly what it sounds like: shuts down all action processing from that point forward. Since all routes through “take bird” end in “stop the action”, this has effectively turned off all the check, carry out, and report verbs whenever taking the bird.

There’s one clause missing, however. What if the player is already carrying the bird? Normally, the can’t take what’s already taken rule would intervene, but our new before rule precedes all check rules.

get bird repeatedly

We need to see if the player is already carrying the bird before any of the rest of this happens. Here’s the updated source code:

Before taking the cheerful little bird:
    if the player encloses the cheerful little bird:
        say “You already have the little bird. If you take it out of the cage it will likely fly away from you.”;
        stop the action;
    else if the player is carrying the black rod:
        say “The bird was unafraid when you entered, but as you approach it becomes disturbed and you cannot catch it.”;
        stop the action;
    else if the player is carrying the small wicker cage:
        now the cheerful little bird is inside the small wicker cage;
        say “You catch the bird in the wicker cage.”;
        stop the action;
    else:
        say “You can catch the bird, but you cannot carry it.”;
        stop the action.

Observe the specific syntax used: “if the player encloses”, rather than “if the player is carrying”. If the player is carrying the wicker cage, and the bird is inside the wicker cage, then the player is not carrying the bird. Checking for enclosure will check through all layers instead of just the immediate inventory layer.

Blocking the Way with the Snake

The player cannot pass the snake in the Hall of the Mountain King without frightening it away first. We can use another before rule to stop the player from moving past.

Before going north:
    if the location of the player is Hall of the Mountain King and the location of the snake is Hall of the Mountain King:
        say “You can’t get by the snake.”;
        stop the action.

Note that this rule is triggered whenever the player goes north, no matter where the player is. However, if the player is not in Hall of the Mountain King with the snake, then the rule will have no effect and the normal check, carry out, and report rules will follow.

This behavior pattern works with before rules, but it does not work with instead of rules or after rules. Instead of rules and after rules replace everything following them – “do this instead of this” – unless there is an explicit continue the action that follows.

In our rule above, we only have one blocked exit implemented – north. In the real Adventure, however, all directions are blocked except for the way we came (up, in the original; east, here). To recreate this, the before rule should cover going globally, and then check the direction to see whether or not the player is retreating.

Before going:
    if not going east:
        if the location of the player is Hall of the Mountain King and the location of the snake is Hall of the Mountain King:
            say “You can’t get by the snake.”;
            stop the action.

Dropping the Bird

Normally, the player needs to be holding something in order to drop it, due to the check rule can’t drop what’s not held. To avoid this rule, you can again use a before rule to avoid the normal check rules.

Before dropping the bird:
    if the player does not enclose the bird:
        say “The little bird is already here.”;
        stop the action;
    if the location of the player is Hall of the Mountain King and the location of the snake is Hall of the Mountain King:
        say “The little bird attacks the green snake, and in an astounding flurry drives the snake away. “;
        remove the snake from play;
    else:
        say “The little bird flies free.”;
    now the bird is in the location of the player;
    stop the action.

Remove the snake from play is exactly what it sounds like – the location of the snake is now “nowhere” and the snake appears in no room.

Further reading

This is supposed to be a quick-start guide, but this lesson has done even less justice than most to Inform 7’s extremely deep systems. Reading all of chapter 7 is highly recommended for a better understanding of the before/instead/after rules and Inform’s associated toolset.

§3.17. Men, women and animals
§7.1. Actions
§7.2. Instead rules
§7.3. Before rules
§7.5. After rules
§8.10. Removing things from play
§8.11. Now…
§8.13. Checking on whereabouts
§13.4. To carry, to wear, to have

Also, see Example 19 (“Laura”) for more about printed names.

Bookmark the permalink.

4 Comments

  1. I have some free time lately, so I’ve been working through Inform tutorials. Having been out of the loop for a while, I was surpised to see that some of the old materials from just a few years ago were no longer up to date, including Aaron A. Reed’s book. I’m happy to find this tutorial! It’s very frustrating to work with the room names with leading prepositions. I often inadvertently leave them off when typing in the source code, typing “End of Road” or “Cobble Crawl,” which creates Problems. This has been a lesson to avoid using prepositions at the start of room names. Too confusing for Inform.

  2. I’m so glad I finally found some tutorials I can actually understand!! These lessons are perfect for an absolute beginner like myself.

    I did have a question about this particular lesson, though. After having completed all the source text and testing it out, I’ve found that once I drop drop the bird to remove the snake, the bird is still in my inventory. Am I doing something wrong?

Leave a Reply to Ted Cancel reply

Your email address will not be published. Required fields are marked *