Inline Hyperlinks

version 2/101030 by Erik Temple

  • Home page
  • Beginning
  • Previous



  • Example: ** Systematic Derangement of the Inner Compass - There may be times when we want a hyperlink to do something other than paste a straightforward command. In this example, which builds on the Maze example above, we set aside a few hyperlink ID numbers for manual use, and write a new hyperlink processing rule that will respond when hyperlinks with these IDs are activated. Other hyperlinks will react normally.

    In this example, the player can eat a pill, which temporarily thwarts her attempts to move in a given direction; instead of moving the direction indicated, a random direction is substituted. The easiest way to have done this would have been to simply change the going action, but I've changed the hyperlink mechanism for illustrative purposes (along with the going action, in order to handle typed directions).

    The player eventually recovers from the disorienting effects of the drug, and we track this using a variable called derangement. Because most of our movement occurs in situations that do not trigger the every turn and time advancement machinery, we hack the tracking of this variable into the routines where it is needed via the derangement abation rule.

         "Systematic Derangement of the Inner Compass"

        Include Basic Screen Effects by Emily Short.
        Include Inline Hyperlinks by Erik Temple.

        Yourself can be deranged. The player is not deranged.

        The derangement is a number variable. The derangement is 5.

        This is the derangement abation rule:
            decrease derangement by 1;
            if derangement < 1:
                player recovers.
        
        Every turn when the player is deranged:
            follow the derangement abation rule.
            
        Instead of going nowhere when the player is deranged:
            say "You collide with a wall.";
            follow the derangement abation rule;
            rule succeeds.
            
        To player recovers:
            say "[line break]The odd feeling fades a bit.[paragraph break][bracket]Press any key[close bracket]";
            wait for any key;
            clear screen;
            say "You shake your head, feeling it clear. All is right again!";
            now the player is not deranged;
            silently try looking.

    So much for the basics of derangement. Moving on to the hyperlink-specific stuff...

    First, we reserve the hyperlink ID numbers 1 through 12 for manual use. This simply assigns an empty text to the first 12 hyperlink commands, i.e. "". If these are clicked outside of their intended use, they will thus have no effect.

    We then revisit the exit lister from the Maze example above, and implement it in two different ways. The first represents the player while "straight", and is the same as in the Maze example. The second example, used when the player is "deranged," assigns random link IDs to the listed directions.

        After starting the virtual machine:
            reserve 12 slots at the beginning of the hyperlink list.
        
        To say exits:
            let count be the number of adjacent rooms;
            if count is greater than 0:
                repeat with destination running through adjacent rooms:
                    let the way be the best route from the location to the destination, using even locked doors;
                    let dir be indexed text;
                    let dir be "[way]";
                    let dir be "[dir]";
                    say "[link][way][as]go [dir][end link]";
                    let count be count minus 1;
                    if count > 0:
                        say ", ";
                say ". "

        To say skewed exits:
            let count be the number of adjacent rooms;
            if count is greater than 0:
                repeat with destination running through adjacent rooms:
                    let the way be the best route from the location to the destination, using even locked doors;
                    let dir be indexed text;
                    let dir be "[way]";
                    let dir be "[dir]";
                    say "[set link (a random number between 1 and 12)][way][terminate link]";
                    let count be count minus 1;
                    if count > 0:
                        say ", ";
                say ". "
            
    We then write a new hyperlink processing rule that will supersede the default command-pasting rule. This rule takes effect only when the player is deranged, and when the hyperlink that was clicked has an ID in the set of 12 that we reserved above. The rule pastes text to the command line that indicates the PC's confusion ("go ???"). It is important to note, however, that this command is not entered; if it were, Inform would not understand it, since ??? is not a valid direction. Instead, we look the hyperlink ID number up in a table of stored actions and we directly "try" that action.

    Note that, in this example, we clear the screen to ensure that the player will never be able to click on our special links during her normal state, nor on the normal links during her deranged state. However, if she were to click on one of the 12 special links while not deranged, there would be no result, because the accompanying commands are all empty texts, i.e. "".

    Since we are shortcutting normal processes with our new hyperlink processing rule, we also print a new command prompt, as well as follow the derangement abation rule, to be sure that the player can eventually emerge from her unfortunate state.

    A companion rule for input redirects the player's typed direction command in the same way. (A better way to do this would be to change the "direction understood" to a random direction before going, but we choose to lie in the hole we've dug ourselves...
        
        Hyperlink processing rule when the player is deranged and the current hyperlink ID is less than 13:
            say "[input-style-for-glulx]go ???[roman type]";
            if there is an ID of current hyperlink ID in the Table of Going Actions:
                choose row with ID of current hyperlink ID in the Table of Going Actions;
                let X be a stored action;
                let X be the go-action entry;
                try X;
                follow the derangement abation rule;
                say "[command prompt][run paragraph on]";
            now glulx replacement command is "";
            rule succeeds.
        
        After reading a command when the player is deranged:
            if the player's command matches "[direction]":
                say "Confusion... ";
                choose a random row in the Table of Going Actions;
                let X be a stored action;
                let X be the go-action entry;
                try X;
                rule succeeds.


        Table of Going Actions
      ID  go-action  
      number  stored action  
      1  going north  
      2  going east  
      3  going south  
      4  going west  
      5  going northeast  
      6  going southeast  
      7  going southwest  
      8  going northwest  
      9  going up  
      10  going down  
      11  going inside  
      12  going outside  


        The printed name of a room is "Maze". The description of a room is usually "A maze of twisty little passages, all alike. Exits: [if the player is deranged][skewed exits][otherwise][exits][end if]".

        There is a red pill in R01. The red pill is edible. "A [link]red pill[as]eat red pill[end link] lies on the floor here. Perhaps this will give you the mental acuity necessary to see through the maze!"

        Understand "take [something edible]" as eating when the player holds the noun.

        Instead of eating the red pill:
            say "You pop the red pill with great anticipation...[paragraph break][bracket]Press any key[close bracket]";
            wait for any key;
            clear screen;
            say "You begin to feel odd indeed. Like the synapses aren't properly connected. Your body has been divorced from your inner compass!";
            remove the red pill from play;
            now the player is deranged;
            silently try looking.

        R01 is a room. R02 is north of R01. R03 is east of R01. R04 is north of R02 and northwest of R03. R05 is northeast of R02. R06 is north of R05. R07 is west of R06. R08 is southwest of R04 and northwest of R02. R09 is east of R06. R10 is northeast of R06. R11 is east of R10. R12 is southeast of R09. R13 is south of R12. R14 is northeast of R12. R14 is southeast of R11. R15 is up from R03. R16 is east of R15. R17 is north of R16. R18 is down from R17. The description of R18 is "A maze of twisty little passages, all alike. A breeze blows from the northeast. Exits: [exits]"

        Exit is northeast of R18. Exit is outside from R18. The printed name of Exit is "Outside". "You emerge into sunlight." Southwest from Exit is nowhere. Inside from Exit is nowhere. After looking in Exit, end the game in victory.