Template ‐ COUNTABLE - ThePix/QuestJS GitHub Wiki
Often in a text adventure there are several instances of the same thing. Perhaps there are arrows the player can shoot or sticks the player can build with or healing potions the player can use. To track all the instances of one type, Quest 6 uses the COUNTABLE template.
NOTE: If the user does not specify a number, Quest assumes she wants to TAKE all that are there unless there is an infinite number there, in which case only one is taken. However, for DROP just one will be assumed.
NOTE: Do not add the TAKEABLE template to a countable item; that is done automatically.
Behind the scenes, Quest uses some trickery to make one object appear to be any number in several places, so when you create your countable, you just create a single item, but you have to state where it is and in what quantity.
Here is a simple example:
createItem("brick", COUNTABLE({lounge:7, dining_room:1}), {
examine:"A brick is a brick.",
})
Note that the COUNTABLE template needs to be given a list (actually a dictionary) of locations and numbers. This means there is no need to give it a "loc" attribute.
The other complicated bit is the "regex" attribute. Quest uses this attribute to match against alternative names. In this case we want to match "1 BRICK" or "23 BRICKS" or whatever. If you want, you can just replace "bricks" with the plural of your countable item name, and skip the next section.
The Regex
When the user types a command, the parser will try to match objects using regular expressions, and the "regex" attribute. Quest will set this for you, but it will only work properly if the plural is the singular plus the letter s, as is the case for brick above.
Here is the regex Quest will create for the brick, so we can look in detail:
/^(\d+ )?bricks?$/
The slashes at the start and end just signify that this is a regular expression, just like quote marks fir a string. The ^
and $
match the start and end of a string. This leaves two parts:
(\d+ )?
bricks?
The first part matches numbers - \d
indicates a digit, and \d+ indicates one or more digits. We wrap that in brackets and append a ?
to indicate that it is optional.
The bricks
just matches that word, but the ?
on the end indicates the s
is optional.
Infinity
You can use the string "infinity" to indicate there is an inexhaustible supply at a location.
Functions
There are three functions that facilitate using countables.
countAtLoc(loc)
- Returns the number at the given location.
takeFrom(loc, count)
- Removes the given number from the location.
giveTo(loc, count)
- Adds the given number to the location.
Names
There are extra options for lang.getName
too. Use the item name, followed by "_count" to tell lang.getName
how many it should say. You can use the string "infinity" to indicate an indefinite, but large number. Use "loc" to indicate a room to use the count from. Set "article" to count to have "one" used, rather than the/an.
lang.getName(w.brick, {brick_count:5, capital:true, article:INDEFINITE})
lang.getName(w.brick, {brick_count:'infinity', article:INDEFINITE})
lang.getName(w.brick, {loc:'lounge', article:INDEFINITE})
lang.getName(w.brick, {brick_count:1, capital:true, article:COUNT})
These can also be done with the text processor.
"{nm:item:a:true}", {item:w.brick, brick_count:5}
"{nm:item:a}", {item:w.brick, item_count:'infinity'}
"{nm:item:a}", {item:w.brick, brick_count_loc:'lounge'}
"{nm:item:count:true}", {item:w.brick, brick_count:1}
An example
Here is a full example for leaves, which is made more complicated because of the plural. Note that there are an infinite number of leaves in the woods. If an NPC, Mary, is present the player cannot pick them up.
createItem("leaf", COUNTABLE({woods:'infinity'}), {
regex:/^(\d+ )?(leaf|leaves)?$/,
scenery:true,
pluralAlias:"leaves",
infinity:"lots of",
examine:"There are leaves everywhere. You are in some woods in Autumn, so no surprise.",
testTakeRestrictions:function(options) {
if (w.Mary.isAtLoc("woods")) {
msg("You pick up " + lang.getName({article:DEFINITE, count:options.count}) + ". 'Why do you want a bunch of leaves?' asks Mary.");
msg("'I don't' You quickly drop " + (options.count === 1 ? "it" : "them") + ".");
return false;
}
return true;
},
})