An Encyclopedia - ThePix/QuestJS GitHub Wiki

If your game is set in some other world, you might want to give the player access to an encyclopedia, so the user can look up places, people, etc. that form the backdrop to the universe. This works best in a sci-fi setting, where the player might have access to Space-Google on her space phone.

This is adapted from a similar system. It should work but if you run into problems, let me know.

The Data

A big issue is the hassle of typing all the entries, so a major concern when designing this system is make that as easy as possible. We will start, then, by creating a few entries.

const encyclopedia = {
  "SS Star Quest":"The SS Star Quest is an {i:Intrepid} class warship, launched {time:-195341} from Newport, in Mars orbit.",
  "The Admiralty":"The Space Admiralty, or more often just the Admiralty, is the administration for Earth-based space military.",
  "Stardate":"The \"Stardate\" has been adopted as a universal time system across [[Union]] space.",
  "Alliance":"Currently thirty four species spread across 3962 planets make up the alliance. Its aim is to promote peace and prosperity throughout the galaxy. The latest species to join the Alliance was the [[Chal]], five years ago.",
  "Terran Union of Planets":"Of the 84 planets that are considered to be terran (earth and its colonies), 68 are part of the Terran Union of Planets.|The Terran Union of Planets is a subset of the [[Alliance]].",
}

Each entry is a name-value pair in a dictionary. Usually I would restrict the names in a dictionary to just letters, numbers and underscores as that allows you to access them using the dot notation and you do not need quote marks, but JavaScript will actually allow any string if you do use quotes, which is great for us here.

Note that we can use the text processor in the value part. Paragraph breaks are denoted by vertical bars. Also, links to other entries are flagged using double square brackets.

The Command

Actually, we have two commands; one is used when the player follows a link.

commands.unshift(new Cmd("LookUp", {
  regex:/(?:look up|search for|search|google) (.+)$/,
  objects:[
    {special:'text'},
  ],
  script:function(objects) {
    log(objects[0])
    w.phone.askDiag(objects[0]) 
    return world.SUCCESS
  },
}))

commands.unshift(new Cmd("EncyclopediaLink", {
  regex:/encyclopedia-link (.+)$/,
  objects:[
    {special:'text'},
  ],
  script:function(objects) {
    log(objects[0])
    w.phone.askDiag(objects[0], true) 
    return world.SUCCESS
  },
}))

All the commands do is pass the search text to w.phone.askDiag, so we next need to set up the phone.

The Phone

You need a couple of functions to do the work, and these are probably best put on an item. I have assumed this is on an item called "phone"; adjust accordingly. There is also a "verbsHeld" attribute set.

  verbsHeld:['Search from'],
  askDiag:function(s, fromLink) {
    if (s.length === 0) return
    if (s.length < 3) {
      msg("On your phone you search for \"" + s + "\", but get over a billion hits. Perhaps search for something a few more characters long?")
      return
    }
    const regex = RegExp(s, 'i')
    for (const key in encyclopedia) {
      if (regex.test(key)) {
        if (!fromLink) msg("On your PAGE you search for \"" + s + "\".")
        let strs = []
        const paras = encyclopedia[key].split('|')
        let flag = true
        for (const s of paras) {
          if (flag) {
            strs.push("<b>" + key + "</b> " + w.phone.expandRefs(s))
            flag = false
          }
          else {
            strs.push(w.phone.expandRefs(s))
          }
        }
        msgDiv(strs, {}, 'encyclopedia')
        return true
      }
    }
    if (fromLink) {
      msg("The link seems to be broken; you wonder if you should report it to someone....")
    }
    else {
      msg("On your phone you search for \"" + s + "\", but find nothing of interest.")
    }
    return false      
  },
  expandRefs:function(s) {
    let match = s.match(/\[\[(.+)\]\]/)
    while (match) {
      log(match)
      const link = '<span onclick="runCmd(\'encyclopedia ' + match[1] + '\')" class="encycLink">' + match[1] + '</span>'
      log(match[0])
      log(link)
      s = s.replace(match[0], link)
      log(s)
      match = s.match(/\[\[.+\]\]/)
    }
    return s
  },

The sidepane

If the phone (or whatever) can be used via the side pane, you need a text input box to appear. For that to work you need a third command

commands.unshift(new Cmd("LookUpOnPhone", {
  regex:/search from phone$/,
  objects:[
  ],
  script:function(objects) {
    askDiag("Search the web", w.phone.askDiag, "Submit")
    return world.SUCCESS
  },
}))
⚠️ **GitHub.com Fallback** ⚠️