RPG Library: Spawning monsters and items - ThePix/QuestJS GitHub Wiki

Because of issue with how games are saved, you cannot create NPCs or other items during game play; instead you need to clone existing ones. The best way to do this is to create a set of prototypes, and a function to clone the prototype at a given location.

Here is an example for a zombie and a knife. Note that Quest expects the name to end "_prototype" - and when you spawn it, you leave that off the name. You could set up any number of prototypes, all using the same "spawn" function.

createItem("zombie_prototype", RPG_CORPOREAL_UNDEAD(), {
  alias:'zombie',
  damage:"2d4",
  health:20,
  signalGroups:['zombies'],
  ex:"A shambling corpse.",
})

createItem("knife_prototype", WEAPON("d4+2"), {
  offensiveBonus:1,
})

To create a zombie at the current location:

spawn('zombie')

We can add some variety. The "mutate" function is called when the monster is spawned. This could give them different attacks, hits, treasure drops, etc. As it has "options" passed to it, you could have this change depending on the situation - for example, in a hospital a large proportion of the zombies might be in scrubs, nurses uniforms, etc. You might want to change the alias too, if there will be several zombies at one location.

This new zombie prototype has two sets of alternative descriptions, "stdExs" and "hospitalExs". The latter is used half the time, if this is the hospital zone. Also, these are set up so they will also change the alias.

createItem("zombie_prototype", RPG_CORPOREAL_UNDEAD(), {
  alias:'zombie',
  damage:"2d4",
  health:20,
  signalGroups:['zombies'],
  ex:"A shambling corpse.",
  stdExs:[
    "A very decayed shambling corpse, wearing the remains of a {random:Dead Pixies:Metallica:Sugababes} tee-shirt.",
    "A shambling corpse, missing an {random:eye:ear:arm}.",
  ],
  hospitalExs:[
    {name:'nurse', ex:"A shambling corpse in a nurse's uniform."},
    {name:'doctor', ex:"A slow-moving corpse in a lab coat."},
  ],
  mutate:function(options) {
    if (options.zone === 'hospital' && random.chance(50)) {
      const data = random.fromArray(this.hospitalExs)
      this.ex = processText(data.ex)
      this.setAlias('zombie ' + data.name)
    }
    else {
      this.ex = processText(random.fromArray(this.stdExs))
    }
  },
})

To create five zombies on North Road, outside the hospital:

for (let i = 0; i < 5; i++) spawn('zombie', 'north_road', {zone:'hospital'})

There is a huge scope here; you could have the zombie doctors armed with scalpels, or patients that explode on death, or anything you can imagine. One limitation to be aware of is to only set attributes to Boolean, integer string or string list values, as others will not be saved. For your scalpel-wielding zombie, change its "skillOptions" attribute, rather than giving it special code.