Stage 2

Introduction

At this point, you should have already built a simple MUD where players can move around the room. If you haven’t already done the extension from the last tutorial, you should definitely do it in this session: while you don’t need it to do this part, you will find it will make everything a lot easier for the next few tutorials.

By the end of this tutorial, you should have:

  • added items into your world
  • added the take command for putting items into the player’s inventory
  • introduced a the concept of keys, locked doors and unlocking them

Aim

We’ll be building on the world we created last session, so the map is mostly the same:

digraph {
    node [shape=box]

    entrance_courtyard [label="Entrance Courtyard\nitems: key"]
    throne_room [label="Throne Room\n(win)"]
    main_hall [label="Main Hall", color="blue"]
    dungeon [label="Dungeon\n(lose)"]

    main_hall -> entrance_courtyard [label="north"]
    entrance_courtyard -> main_hall [label="south"]

    {rank=same;
    throne_room -> main_hall [label="east\n(locked)", dir="back"]
    main_hall -> dungeon [label="west"]
    }
}

The changes we’ve made are that the exit from the Main Hall to the Throne Room is now locked (so you can’t win that easily!), and we’ve added a key in the Entrance Courtyard.

What this means is that you now have to do something like this to win:

You are standing in the main hall, a room with many pillars.
On the sides of the room, you see many sculptures of animals,
people and (strangely enough) boats.
There are exits west, east and north.
> north
You move north into the entrance courtyard
You are standing in the entrance courtyard, a place with a
crossroads of paved pathways, lined by miniature pine trees
with tulips surrounding them at the base.
There are exits south.
You see a brass key.

> take key
Taken a brass key

> south
You move south into the main hall
You are standing in the main hall, a room with many pillars.
On the sides of the room, you see many sculptures of animals,
people and (strangely enough) boats.
There are exits west, east and north.

> east
The door is locked.
Use a brass key? y
You unlock the door.
You move east into the throne room
You sneak into the throne room, and steal the crown!

Congratulations, you've won the game!

Data format

Note

For brevity, I’ve just included the bits of the world file which have changed. If you’re not sure what’s going on, please ask!

If you used the JSON format in the previous tutorial, there are just a few changes you’ll need to make to support this.

The first thing we’ll need to do is to add an "items" key for describing what items exist in the world.

{
    "items": {
        "key-1": {
            "type": "key",
            "description": "a brass key",
            "names": ["key", "brass key"]
        }
    }
}

The items section says that there is a single item in the world, uniquely identified as key-1 (this will be called an “item id”), which is a key, should be described as “a brass key”, and can be referred to by players as either “key” or “brass key”.

Note that it doesn’t describe where the object is, because that will be different for each player (at the moment, we’re just keeping the player’s state in memory, not in this world file). The starting position for the item is described by a new key we’re adding to rooms:

{
    "rooms": {
        "entrance courtyard": {
            "items": ["key-1"]
        }
    }
}

And finally, we’re locking the door between the Main Hall and the Throne Room by setting a special "locked-east" attribute on the room. Whenever a player wants to move in some direction X, we’ll check to see if the current room has a locked-X attribute, and if so, force them to use a key.

{
    "rooms": {
        "main hall": {
            "exits": {
                "east": "throne room"
            },
            "special": ["locked-east"]
        }
    }
}

Table Of Contents

Previous topic

Stage 1

Next topic

Stage 3

This Page