Handling regions - ThePix/QuestJS GitHub Wiki

A region in this context is a collection of locations that have some basic similarity. One region could be a cave system, while another is a swamp and a third is a castle. Features of a region tend to be the same, so sounds and smells default to the same, and there may be some identical background items too.

This is all optional, but will make for a more polished game. Even for simple games setting up a single region allows you to handle the players doing EXAMINE WALLS in a slick manner.

I suggest splitting your game up into different files, with one file per region, but the important point is to have all locations for one region collected together. And at the start of that file, or that section of the file, you set up the region.

Here is an example that will set up a region called "castle":

setRegion('castle', {
  smell:'There is a faint musty smell, with just a hint of polish.',
  listen:'It is eerily quiet...',
  floor:"The floor is wooden, and well-polished.",
  wall:"The walls are all paneled in wood.",
  ceiling:"The ceiling is white, with simple decorations along each side.",  
})

All locations that are created subsequent to this (up until you set up the next region) will have their "_region" attribute set to "castle". It also sets default SMELL and LISTEN responses for the locations in this region, and descriptions for generic items.

Generic items

So now we need to create those generic items. You may need to add more for your game, but there really is very little to them. The BACKSCENE template does the work.

createItem("wall", BACKSCENE(), { 
  alias:"walls",
})

createItem("floor", BACKSCENE(), {
  synonyms:["ground"],
})

createItem("ceiling", BACKSCENE(), {
})

A BACKSCENE item will only be there in a region that has a description set for it.

Over-riding for specific locations

Sometimes you want one location in the region to be a bit different...

For LISTEN and SMELL, just give the location its own attribute.

You can also have descriptions for generic items in specific locations by giving the location an attribute named "examine_" plus the name of the item. For example:

createRoom("lounge", {
  // ...
  listen:'The clocks ticks...',
  examine_wall:'A fine example of a feature wall.',
})

The way it will work for walls, for example, is Quest will check if the location's region has a "wall" attribute. If it does not, it will report that there is no wall here. If there is a "wall" attribute for the region, Quest will now look at the location, and see if it has an "examine_wall" attribute. If it does, it will either run the function or print the string. If it does not, it will use the "wall" attribute for the location's region.

Dynamic Descriptions

Note that region data is not saved when the user saves the game - though the "_region" attribute of the location will be.

If you want to have dynamic descriptions, you can

  • use text processor directives in the strings
  • change the region attribute of the locations
  • use examine addendum strings

With the text processor

An example of the first, with the text depending on an attribute of the player, is seen here:

setRegion('castle', {
  smell:  "{if:player:afterFire:Everything smells of smoke.:There is a faint musty smell, with just a hint of polish.}",
  listen: "It is eerily quiet...",
  floor:  "{if:player:afterFire:The wooden floor has mostly survived, but is strew with ash.:The floor is wooden, and well-polished.}",
  wall:   "{if:player:afterFire:The wooden panels of the walls have burned to black ashes.:The walls are all paneled in wood.}",
  ceiling:"{if:player:afterFire:The ceiling is a dirty grey, with patches of black.:The ceiling is white, with simple decorations along each side.}",  
})

Changing regions

The second approach requires a dummy region. This is just like a normal region, except that there must be no locations defined after you create it before you create the next region.

setRegion('castleAfterFire', {
  smell:'Everything smells of smoke.',
  listen:'It is eerily quiet...',
  floor:"The wooden floor has mostly survived, but is strew with ash.",
  wall:"The wooden panels of the walls have burned to black ashes.",
  ceiling:"The ceiling is a dirty grey, with patches of black.",  
})

// Nothing here!!!

setRegion('castle', {
  smell:'There is a faint musty smell, with just a hint of polish.',
  listen:'It is eerily quiet...',
  floor:"The floor is wooden, and well-polished.",
  wall:"The walls are all paneled in wood.",
  ceiling:"The ceiling is white, with simple decorations along each side.",  
})

When the event happens, you need to change the "_region" attribute for each affected location. The advantage of this technique is that you can select which locations that is, rather than have them all affected.

The slick way to do it is put them in a list, and iterate.

for (const el of [w.lounge, w.drawing_room, w.study]) el._region = 'interiorAfterFire'

With addendum strings

You can give a location a string that will get added to the end of the description for a BACKSCENE item. The name of the attribute must be "addendum_examine_" plus the name of the item, for example "addendum_examine_wall".

Thus, if the player shoots the wall, you can do this:

currentLocation.addendum_examine_wall = 'There is a gun shot hole in the wall.'

There is a more complete example here.