Main      Site Guide    
Smash Tutorial

Sample Adventure: The Trainer

The "rock.sma" File


This file handles the "rock" location. Initially, there is a rock on the ground that you can pick up. Paths lead in a couple different directions, and there's a cliff rising up to the east that you can try to climb. The player is only successful climbing it, however, if he has the rope, and if he's tied a loop in the rope.

. You are at the bottom of a short cliff face. The cliff is roughly forty feet high and jagged with rocky outcroppings. Paths lead away to the north and west. c d:rock , There is a small rock on the pathway that catches your eye.

The above section of code provides a description of the area. If the player hasn't picked up the rock yet, we also mention that there is a rock on the path.

^ n Go north. g tree ^ w Go west. g kobold

These two options are pretty self-explanatory. The player is provided with the option to go north, which puts him at the tree, or west, which puts him at the cave entrance, where, initially, the kobold is guarding the entrance.

But now things get interesting:

^ u Climb the cliff. c a:rope p You throw the rope up the cliff to aid you in your climb. c tied_loop P The loop at the end of the rope hooks around a rock. You climb up. g dragon C P But it falls back down. You try to climb the cliff without it, but you don't get very far. C p You try to climb up the cliff, but it's too steep for you to make it.

We provide the player with the option of attempting to climb the cliff. Depending on how things are, this could result in one of three different ways. If the player doesn't have the rope yet, we skip the first conditional and drop down to the last C and print a message saying that the cliff is too steep.

If the player has the rope, the text "You throw the rope up the cliff to aid you in your climb." is displayed. But if he hasn't tied a loop in the rope, "But it falls back down. [etc]" is appended to the action description text. It is only if the player has the rope and has tied a loop in the rope that the climb is successful, and the "g dragon" statement is executed, which puts the player at the top of the cliff.

So how does the game know if the player has tied a loop in the rope or not? That information is stored in the "tied_loop" variable, which starts out being 0 (no loop), and, as we shall soon see, becomes 1 when the loop is tied. The conditional statement c tied_loop might be more readably written c tied_loop = 1, but remember that a conditional succeeds if the expression following the c (or C) evaluates to a nonzero value. So c tied_loop will succeed when tied_loop is 1 and fail when it is 0, which is exactly the behavior we are looking for.

c a:rope , !tied_loop * 1 Tie a loop in the rope. p Ok. There is now a loop in the rope. s tied_loop = 1

Here is where we manage the tying of the rope. If the player has the rope and has not yet tied a loop in the rope, then (and only then) he is provided with an option to tie a loop in the rope. If he selects that option, the state variable "tied_loop" is set to 1. We could have used a global variable instead of a state variable, but since we do not need to use "tied_loop" outside this location, it makes sense to use a state variable.

c a:rock * 2 Throw the rock up the cliff. p You toss it upwards. It travels half the way up the cliff, then bounces back down again. d rock C * 3 Pick up the rock. c !rock p Ok. It's a really neat rock. C p Ok. s rock = 1 a rock

The above section of code provides one of two different options, depending on whether or not the player has the rock at the moment. If the player has the rock, he can try to throw it up the cliff. This doesn't result in anything except the rock falling back onto the path again. If the player doesn't have the rock, he can pick it up.

This code is fairly self-explanatory except for the state variable called "rock". This is not to be confused with the inventory item "rock", nor the location named "rock" -- these are three different things!

We could most certainly write all of the above code without using the state variable "rock" -- but using it provides a little cosmetic difference. When we first pick up the rock, the state variable rock is set to 0. We hit the conditional c !rock (this is equivalent to c rock = 0), and we print "Ok. It's a really neat rock." Then we set "rock" to 1.

If the rock is subsequently lost by throwing it up the cliff and we try to pick it up again, "rock" is already set to 1, so we only get the message "Ok." The effect is that "It's a really neat rock" -- which seems like an observation that would only be made once, not every time -- is only displayed the first time the rock is picked up.

The following section of code completes the rock.sma file:

* 4 Shout, "Hey up there!" p You hear a faint rumbling in response, but that's it.