Adventure Book

version 1/110101 by Edward Griffiths

  • Home page
  • Beginning
  • Previous
  • Next



  • Example: **** Combat System - Using the "run inform code for..." phrase to add functionality.

    Experienced Inform 7 programmers can add a lot of extra behavior to the Adventure Book system by creating custom game commands and overriding the "run inform code for..." phrase.

    Some traditional adventure books, such as the Fighting Fantasy series, added a combat system and encounters with monsters to make the story feel more like an RPG. This is a very simple example of how we can make special pages that know how to begin a monster encounter and a combat menu that can do more than just turn the player to a different page.

        "Dungeon Deep" by Edward Griffiths
        
        Include Adventure Book by Edward Griffiths
        
        Chapter 1 - A Simple Combat System
        
    Because this is just an example, we keep the system simple. Attack increased your chances of hitting, hit points represent your life force.

        Section 1 - Character Stats
        
        People have a number called hit points. People have a number called attack.
        
        The hit points of the player is 10. The attack of the player is 5.
        
        A monster is a kind of person.
        
        Section 2 - Combat

    Here, we create a special kind of page called a combat setting. It keeps track of a lot of information that will be useful when we run combat. The monsterless page is where we turn if there are no monsters currently occupying this page. The victory page is where we turn at the end of a successful combat, where we may grant the player treasures or other boons. The defeat page is where we turn if the player fails in this combat. It is usually The Last Page, but it doesn't have to be; we can create special encounters where combat isn't fatal if the player fails by making it turn to a different page. Finally, the escape page allows us to give a page where the player can turn if he tries to escape. Notice that, if no such page is defined, the player simply bounces back into combat.
        
        A combat setting is a kind of page. A combat setting has a page called the monsterless page. A combat setting has a page called the victory page. A combat setting has a page called the defeat page. A combat setting usually has defeat page The Last Page. A combat setting has a page called the escape page. A combat setting usually has escape page NoEscape. A combat setting is usually special.
        
        NoEscape is a page.
        "You can't escape!"
        It is a dead end.
        
        Current Battle is a combat setting that varies. Current Opponent is a monster that varies.

    We define monsters separately from the combat setting where they are found. This would allow us to dynamically change where monsters appear throughout the game.

        Inhabiting relates various monsters to one combat setting (called the home). The verb to inhabit (it inhabits, they inhabit, it inhabited, it is inhabited, it is inhabiting) implies the inhabiting relation. The verb to house (it houses, they house, it housed, it is housed, it is housing) implies the reversed inhabiting relation.

    Whenever we turn to a combat setting, we first check to see if monsters are about. If not, we go to the monsterless page. If so, we set up our combat variables and go to the combat menu.
        
        To run inform code for (x - a combat setting):
            if x is not inhabited by something:
                change pages to the monsterless page of x;
            otherwise:
                now Current Battle is x;
                now Current Opponent is a random monster that inhabits x;
                read x;
                change pages to CombatMenu;

    CombatMenu is a pretty normal page. CombatAttack is where the real fun happens. Whenever we turn to that page, we roll dice for the player and the monster, add attack bonuses, and compare the results. The winner does 2 points of damage to the loser, and nothing happens in the event of a tie. If either side was defeated, we tell the game to branch to the appropriate page. Otherwise, we jump back to the CombatMenu to try again.
        
        CombatMenu is a page.
        "Your hit points: [hit points of the player][line break] [Current Opponent]'s hit points: [hit points of the Current Opponent][paragraph break] What will you do?"
        A choice called combatmenua is for CombatMenu. "Attack". It triggers CombatAttack.
        A choice called combatmenub is for CombatMenu. "Retreat". It triggers CombatRetreat.
        
        CombatAttack and CombatRetreat are special pages.
        
        To run inform code for (x - CombatAttack):
            let playerroll be a random number from 1 to 6;
            let playerroll be playerroll plus a random number from 1 to 6;
            let playerroll be playerroll plus the attack of the player;
            let monsterroll be a random number from 1 to 6;
            let monsterroll be monsterroll plus a random number from 1 to 6;
            let monsterroll be monsterroll plus the attack of the Current Opponent;
            if playerroll is greater than monsterroll:
                say "You hit for 2 points.";
                now the hit points of the Current Opponent is the hit points of the current opponent minus 2;
            if playerroll is less than monsterroll:
                say "You were hit for 2 points.";
                now the hit points of the player is the hit points of the player minus 2;
            if playerroll is monsterroll:
                say "You parry each other's attacks.";
            if the hit points of the player is less than 1:
                say "You were defeated.";
                change pages to the defeat page of the Current Battle;
                rule succeeds;
            if the hit points of the Current Opponent is less than 1:
                say "You have defeated [the Current Opponent].";
                now the Current Opponent inhabits nothing;
                if the Current Battle is inhabited by something:
                    change pages to the Current Battle;
                otherwise:
                    change pages to the victory page of the Current Battle;
                rule succeeds;
            change pages to CombatMenu;
        
        To run inform code for (x - CombatRetreat):
            change pages to the escape page of the Current Battle.
        
    We may want to make the player gain or lose stamina as a result of events in the story beyond combat. So we create properties to keep track of what the page can do to the player's stamina and "run inform code for..." definitions that can carry them out.

        Section 3 - Traps
        
        Pages have a number called hit points. A page usually has hit points 0. A page can be trapped. A page is usually not trapped. A page can be healing. A page is usually not healing.
        
        To run inform code for (x - a trapped page):
            now the hit points of the player is the hit points of the player minus the hit points of the current page;
            if the hit points of the player is less than 1:
                now the hit points of the player is 0;
                read the current page;
                change pages to The Last Page;
        
        To run inform code for (x - a healing page):
            now the hit points of the player is the hit points of the player plus the hit points of the current page;
            if the hit points of the player is greater than 10:
                now the hit points of the player is 10;
        
    Finally, we define a new command, "status", that allows the player to view his hit points at any time.

        Section 4 - Status
        
        Table of Commands (continued)
      topic  
      "status"  
        
        Checking status is an action applying to nothing. Understand "status" as checking status.
        
        Carry out checking status:
            say "[status][paragraph break]";
            
        To say status:
            say "Current hit points: [the hit points of the player]";
        
    And now, an example of how it all comes together. We have one combat setting with two monsters, one room that does damage, and one room that heals.

        Chapter 2 - A sample game
        
        The key is an inventory item.
        
        The First Page is a page.
        "You're deep in a gloomy dungeon. A nasty smell comes from the passage to your left. The passage to the right smells like fresh air. There's a sound of running water behind you. Where will you go?"
        A choice called tfpa is for The First Page. "Go left." It triggers OrcBattle.
        A choice called tfpb is for The First Page. "Go right." It triggers TrapRoom.
        A choice called tfpc is for The First Page. "Explore the darkness in the back of the room." It triggers FountainRoom.
        
        OrcBattle is a combat setting.
        "Suddenly, you're attacked by an ugly monster!"
        It has victory page FindKey. It has monsterless page MonsterKilled. It has escape page The First Page.
        
        The Orc is a monster. It has hit points 5. It has attack 5. It inhabits OrcBattle.
        
        The Kobold is a monster. It has hit points 3. It has attack 3. It inhabits OrcBattle.
        
        FindKey is a page.
        "Exploring the room, you find a small brass key."
        It gives the key. It is followed by MonsterKilled.
        
        MonsterKilled is a page.
        "You're in an empty room. It looks like the scene of a recent melee."
        A choice called monsterkilleda is for MonsterKilled. "Go back." It triggers The First Page.
        
        TrapRoom is a page.
        "FWIP! A poison dart shoots from a hidden spot in the wall. You take one point of damage.[paragraph break][status]".
        It is trapped. It has hit points 1. It is followed by NearExit.
        
        NearExit is a page.
        "The way out is just up ahead!"
        A choice called nearexita is for NearExit. "Escape". It triggers Victory. It requires the key.
        A choice called nearexitb is for NearExit. "Escape". It triggers NoKey. It is cancelled by the key.
        A choice called nearexitc is for NearExit. "Go Back". It triggers The First Page.
        A use called nearexitd is for NearExit. It uses the key. It triggers Victory.
        
        NoKey is a page.
        "You try the door, but it won't open!"
        It is a dead end.
        
        Victory is a page.
        "You use the brass key to open the door. At last, you've escaped from the dungeon!"
        It is followed by The Last Page.
        
        FountainRoom is a page.
        "You find a fountain of healing and drink your fill.[paragraph break][status]".
        It is healing. It has hit points 10. It is a dead end.