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

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

  var p = adventurejs.Parser.prototype;

  /**
   * Exclude from a list of assets all assets that are not takeable from player inventory.
   * @method adventurejs.Parser#selectInInventoryIfTakeable
   * @memberOf adventurejs.Parser
   * @param {Array} list
   * @returns {Array}
   */
  p.selectInInventoryIfTakeable = function Parser_selectInInventoryIfTakeable(
    list
  ) {
    if ("string" === typeof list) list = [list];
    if (!Array.isArray(list)) {
      this.game.log(
        "L1227",
        "warn",
        "critical",
        "selectInInventoryIfTakeable.js > received non-array",
        "Parser"
      );
      return [];
    }
    var player = this.game.getPlayer();
    var foundObjects = [];
    for (var i = 0; i < list.length; i++) {
      var object = this.game.getAsset(list[i]);

      if (!object.isDOV("take")) {
        foundObjects.push(list[i]);
        continue;
      }

      if (
        "undefined" !== typeof object.areAnscestorsUnknown &&
        object.areAnscestorsUnknown()
      ) {
        continue;
      }

      if (
        !object.isWithin(player) &&
        !player.isConnectedToAsset("hold", object, "to_dov")
      ) {
        continue;
      }

      foundObjects.push(list[i]);

      // TODO but is it nested in a closed object in player?
      // and is it known? could be unseen inside an unopened thing
    }
    //console.warn("selectInInventoryIfTakeable returned " + JSON.stringify(foundObjects));
    return foundObjects;
  };
})();