Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// selectKnown.js

(function () {
  /* global adventurejs A */

  var p = adventurejs.Parser.prototype;

  /**
   * Exclude from a list of assets all assets that are unknown by player.
   * @method adventurejs.Parser#selectKnown
   * @memberOf adventurejs.Parser
   * @param {Array} list
   * @returns {Array}
   */
  p.selectKnown = function Parser_selectKnown(list) {
    if ("string" === typeof list) list = [list];
    if (!Array.isArray(list)) {
      this.game.log(
        "L1282",
        "error",
        "critical",
        "[selectKnown.js] selectKnown() received non-array",
        "Parser"
      );
      return [];
    }
    this.game.log(
      "L1112",
      "log",
      "high",
      `[selectKnown.js] selectKnown() receive:\n${list}`,
      "Parser"
    );
    var foundObjects = [];
    var character = this.game.getSubject();
    for (var i = 0; i < list.length; i++) {
      var object = this.game.getAsset(list[i]);

      // is it a substance in a container we haven't examined?
      // this only works if a prior selection has identified containers
      // but, as selectKnown runs first, it will only determine whether
      // the substance itself is known
      if ("string" === typeof list[i]) {
        var sub = list[i].split(":"); // asset:aspect:substance
        if (3 === sub.length) {
          var known = this.game.getAsset(sub[0])?.aspects[sub[1]]?.vessel?.is
            .known;
          if (!known) {
            continue;
          }
        }
      }
      /**
       * Objects become known once they're "seen" by player
       * or if player learns about them by other method.
       * Omit unknown objects.
       */
      if (!character.knowsAbout(object)) {
        continue;
      }

      /**
       * Also omit object if any of its
       * anscestors are unknown. It's possible for a known
       * object to be inside an unknown object, but this state
       * is unlikely to happen by accident, so we'll take unknown
       * anscestors to mean the object was deliberately hidden.
       */
      // if (object.areAnscestorsUnknown && object.areAnscestorsUnknown()) {
      //   continue;
      // }

      foundObjects.push(list[i]);
    }

    return foundObjects;
  };
})();