How to spit - ThePix/QuestJS GitHub Wiki
How might we implement spitting out an item, such as some gum?
We will create the gum first. The important bit is flagging it as spittable.
createItem("gum", TAKEABLE(), {
loc:"lounge",
spittable:true,
examine: "Just some gum",
pronouns:lang.pronouns.massnoun,
})
The player can put the gum in her mouth, so we need a mouth object, and it needs to be a container. We can also make it a component of the player object (which is called "me"). Here is a first go:
createItem("mouth", COMPONENT('me'), CONTAINER(), {
loc:"me",
owner:'me',
examine: "Teeth and a tongue.",
})
We need to refine that so only spittable things can go in the moth, and only one at a time. This version therefore has a "testDropIn" function. I have also added a "content" function, which will give us what is currently in there, or null
if there is nothing, as this is something we will want to check a few times.
createItem("mouth", COMPONENT('me'), CONTAINER(), {
loc:"me",
owner:'me',
examine: "Teeth and a tongue.",
content:function() {
const contents = this.getContents()
return contents.length > 0 ? contents[0] : null
},
testDropIn:function(options) {
if (!options.item.spittable) {
return falsemsg("{nv:char:cannot:true} put {nm:item:the} in your mouth.", options)
}
options.otherItem = this.content()
if (options.otherItem) {
options.otherItem = contents[0]
return falsemsg("{nv:char:have:true} {nm:otherItem:the} in {pa:char} mouth already.", options)
}
return true
},
})
We then need to update the inventory command. How do we want to do that? Do we want to have a bit tagged on the end if there is something in the players mouth? If so, we can just modify the existing script.
findCmd('Inv').script = function() {
const listOfOjects = player.getContents(world.INVENTORY)
msg(lang.inventory_prefix + " " + formatList(listOfOjects, {article:INDEFINITE, lastSep:lang.list_and, modified:true, nothing:lang.list_nothing, loc:player.name}) + ".", {char:player})
const options = {item:this.content(), char:player}
if (options.item) msg("{nv:char:have:true} {nm:item:a} in {pa:char} mouth.", options)
return settings.lookCountsAsTurn ? world.SUCCESS : world.SUCCESS_NO_TURNSCRIPTS
}
Finally, we need a spit command. Note that a Regex is "non-greedy" as there is a question mark after the plus sign. This means the bare minimum will be matched and stops the OUT getting added to the item name.
In the objects list, "attName" is used to tell the parser to give priority to items that are flagged as spittable.
In the script, we just check the item is in the mouth, and if so, proceed to spit.
new Cmd('Spit', {
regex:/^spit(?: out|) (.+?)(?: out|)$/,
objects:[
{scope:parser.isPresent, attName:"spittable"}
],
script:function(objects) {
const item = objects[0][0]
if (item.loc !== 'mouth') return failedmsg("You can't spit something that is not in your mouth")
msg("You spit out {nm:item:the}.", {item:item})
item.loc = player.loc
return world.SUCCESS
},
})
An alternative approach would be to have a SPITTABLE template. We could then handle most of the command in there. We can also combine it with the TAKEABLE template, as everything that the player can spit can also be taken - and there should be no conflicts with other templates that also combine with TAKEABLE.
Here is the template:
const SPITTABLE = function() {
const res = Object.assign({}, TAKEABLE_DICTIONARY)
res.spittable = true
res.spit = function(options) {
if (this.loc !== 'mouth') return failedmsg("You can't spit something that is not in your mouth")
msg("{nv:char:spit:true} out {nm:item:the}.", options)
this.loc = player.loc
return true
}
return res
}
Our command is not just this:
new Cmd('Spit', {
regex:/^spit(?: out|) (.+?)(?: out|)$/,
objects:[
{scope:parser.isPresent, attName:"spittable"}
],
defmsg:"That's not something you can spit.",
})
And the gum is flagged as spittable like this:
createItem("gum", SPITTABLE(), {
loc:"lounge",
examine: "Just some gum",
pronouns:lang.pronouns.massnoun,
})
What if you want your NPCs to be spitting gum? You would have to give every NPC their own mouth object.
Or we could just have one mouth object and fake it...