Terrain scripts and groups

When the player searches the chest, the goblin should jump out of it. To do this, we need to use a terrain script. Terrain scripts are yet another type of script that Blades of Avernum uses. To place a terrain script on a location, click the "Place Terrain Script" button, then click on the terrain you want the script to be linked to. After doing so, you'll see the settings for the script on the bottom of the Editor (you can always bring these setting up by using the "Select/edit placed object" tool, and you can delete terrain scripts using the "Delete an object" tool).


To specify the terrain script you want to link to, click where it says "Script: dummy" and change "dummy" to the name of your script. We could write our own script from scratch, but in this case we can use one of the default scripts that come included with all scenarios. specobj.txt is the filename of the script, so replace "dummy" with "specobj". Then set "Memory Cell 0" to 11. What does this do? Let's read the comments at the beginning of specobj.txt:

// specobj.txt - This very simple script waits until this object is searched. When it
// is, calls a given special node in the current town's script.

// Memory Cells - 
//   0 - Number of a state in the town script. This is the state that is called when the item is used.
//   1,2 - Coordinates for a stuff done flag. If these are 0 and 0, ignored. Otherwise,
//     is the given flag is non-zero, nothing happens when the object is used.

Remember how we used memory cells to set the initial talk node for creatures? This is very similar. The specobj script overrides how terrain behaves when it is searched. When the player uses the look action on the terrain, the town script state specified by Memory Cell 1 is run (feel free to read specobj.txt if you want to see the full implementation). Now let's write state 11 in z1hideout.txt:

beginstate 11;
  if (get_flag(1, 2) == 0) {
    set_flag(1, 2, 1);
    set_terrain(8, 5, 0);
    message_dialog("You open the chest. Out springs the goblin!", "");

This follows our usual pattern of "if SDF not set, then set SDF and display a message". set_terrain changes the terrain at the given coordinates. In this case, it puts terrain 0 (the empty terrain) at (8,5), effectively erasing the chest. activate_hidden_group activates all creatures in the given group. What's a hidden group? In the Editor, you're able to assign creatures to groups. Those creatures won't appear until activate_hidden_group is called in a script. Place a Pesky Goblin creature on the same square as the chest. Click on "Hidden Class" and set it to 1.


specobj.txt is a terrain script that will be used in nearly all scenarios. Another one is door.txt, the script that controls all doors in Blades of Avernum. Make a room in the hideout (filled with slightly nicer items than the dead end had), and place a closed door (terrain number 48) where a wall would normally go. Doors come with terrain script "door" already set. If you want the door to be locked, set "Memory Cell 0" with the door's difficulty. If you want the game to keep track of when a door gets unlocked (almost always), set "Memory Cell 1" and "Memory Cell 2" with the coordinates of an unused SDF. Read the comments in door.txt to see what the other memory cells do).


Now the player is able to fight and kill the last goblin. But nothing actually happens when it dies. Let's change that.