Items that can change size - ThePix/QuestJS GitHub Wiki

For The House of Highfield Lane, I create a system that would allow the player to shrink or grow any item. This is a complex system, and I thought it worth looking at his it was done.

The system comprises two rooms; a big one and a little. Drop something in the little room, go to the big room and the item is now about ten times the size. This means that pretty much anything in the game that can be picked up can change its size, and can do so repeatedly. There is a natural limit to do big or small a thing can get. Ultimately it will be too heavy to pick up or just to smaller to grab.

The very basics of the system: I am going to use the "size" attribute to track size, and all items will start at 5, and will typically go down to 3 and up to 7, but will vary - these will be stored as "minSize" and "maxSize".

A very important consideration in a system like this is making it easy to use for the author. Do all the tricky stuff once, test it well, and then apply it to all those items. The best way to do that is often a template. But before we get to that, we want to think about the bare minimum we want to put in our items.

Here, as an example, is a phone. We will need t give it the SIZE_CHANGING, and it will need a number of descriptions, but we do not want to have to do anything besides that. Let the template do the rest.

createItem("mobile_phone", SIZE_CHANGING(), {
  loc:"school_bag",
  synonyms:['cell phone', 'buttons'],
  use:function() {
    //...
  },
  desc5:"Mandy looks at her phone. It is soooo old. It even has buttons!",
  desc4:"Mandy's phone is now tiny.",
  desc3:"Mandy's phone is so small she could hardly see it.",
  desc6:"Mandy's phone is now not only way out of date, but also too big to easily carry.",
})

So now we can create a template. This starts from TAKEABLE, and sets some defaults. The "afterCreation" function runs after the item is created, and is used because the template itself has no access to the attributes set by the author. This function, on the other hand, does, so can check what descriptions are set, and set minSize and maxSize based on that.

The template also overrides "take" and "examine", and also defines new "shrink" and "grow" functions.

const SIZE_CHANGING = function() {
  const res = Object.assign({}, TAKEABLE_DICTIONARY)
  res.size_changing = true
  res.size = 5
  res.minsize = 4
  res.maxsize = 6
  
  res.afterCreation = function(o) {
    o.basealias = o.alias
    if (!o.desc5) log("WARNING: Size changer " + o.name + " has no desc5.")
    if (o.desc4) o.minsize = 3
    if (o.desc3) o.minsize = 2
    if (o.desc2) o.minsize = 1
    if (o.desc1) o.minsize = 0
    if (o.desc6) o.maxsize = 7
    if (o.desc7) o.maxsize = 8
    if (o.desc8) o.maxsize = 9
    if (o.desc9) o.maxsize = 10
  }
  
  res.examine = function(options) {
    if (this.size === this.minsize) {
      msg("{nv:item:be:true} too tiny to see properly!", {item:this})
    }
    else if (this.size === this.maxsize) {
      msg("{nv:item:be:true} of gigantic proportions!", {item:this})
    }
    else {
      msg(this['desc' + this.size])
    }
    return true
  }

  res.take = function(options) {
    if (this.isAtLoc(options.char.name)) {
      return falsemsg(lang.already_have, options)
    }
    if (!options.char.canManipulate(this, "take")) return false
    
    if (this.size === this.maxsize) {
      return falsemsg("{nv:item:be:true} far too big to pick up.", {item:this})
    }
    else if (this.size === this.minsize) {
      return falsemsg("Mandy tries to pick up {nm:item:the}, but {pv:item:be} too tiny for her fingers to grasp.", {item:this})
    }    
    
    msg(this.msgTake, options)
    this.moveToFrom(options, "name", "loc")
    if (this.scenery) this.scenery = false
    return true
  }

  res.shrink = function() {
    this.size--
    this.setAlias(this.size === 5 ? this.basealias : sizeAdjectives[this.size] + ' ' + this.basealias)
    if (this.afterSizeChange) this.afterSizeChange()
  }

  res.grow = function() {
    this.size++
    this.setAlias(this.size === 5 ? this.basealias : sizeAdjectives[this.size] + ' ' + this.basealias)
    if (this.afterSizeChange) this.afterSizeChange()
  }
    
  return res;
}

Here are the tests I created.

  test.title("Shrink/grow phone")
  test.assertEqual(true, w.mobile_phone.size_changing)
  test.assertEqual(7, w.mobile_phone.maxsize)
  test.assertEqual(2, w.mobile_phone.minsize)
  w.mobile_phone.shrink()
  test.assertEqual('small mobile phone', w.mobile_phone.alias)
  test.assertOut(["Mandy's phone is now tiny."], function() {
    w.mobile_phone.examine(player, {})
  })
  test.assertOut(["Mandy looks at her shrunken phone. Maybe it was a bit optimistic thinking it would now be charged, just because it is so much smaller."], function() {
    w.mobile_phone.use(player, {})
  })
  w.mobile_phone.shrink()
  test.assertEqual('tiny mobile phone', w.mobile_phone.alias)
  test.assertOut(["Her stupid phone is now too small to use!"], function() {
    w.mobile_phone.use(player, {})
  })
  w.mobile_phone.grow()
  w.mobile_phone.grow()
  test.assertEqual('mobile phone', w.mobile_phone.alias)
  test.assertOut(["Mandy looks at her phone. 'Shit.' No charge left. She only charged it last night... No, wait, she had found it on her bedroom floor this morning. 'Shit,' she says again."], function() {
    w.mobile_phone.use(player, {})
  })
  w.mobile_phone.grow()
  test.assertEqual('big mobile phone', w.mobile_phone.alias)
  test.assertOut(["Her stupid phone is now too big to use!"], function() {
    w.mobile_phone.use(player, {})
  })
  w.mobile_phone.shrink()