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

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

  var p = adventurejs.Parser.prototype;

  /**
   * Exclude from a list of assets all assets that are not in player's hands,
   * unless asset is a reservoir (such as "throw sand" while standing in desert).
   * @method adventurejs.Parser#selectInHandsUnlessReservoir
   * @memberOf adventurejs.Parser
   * @param {Array} list
   * @returns {Array}
   */
  p.selectInHandsUnlessReservoir = function Parser_selectInHandsUnlessReservoir(
    list
  ) {
    // we already know it's present and visible and reachable
    if ("string" === typeof list) list = [list];
    if (!Array.isArray(list)) {
      this.game.log(
        "L1241",
        "warn",
        "critical",
        "selectInHandsUnlessReservoir.js > received non-array",
        "Parser"
      );
      return [];
    }
    var foundObjects = [];
    var player = this.game.getPlayer();
    for (var i = 0; i < list.length; i++) {
      var object = this.game.getAsset(list[i]);

      // if (object instanceof adventurejs.Substance) {
      //   continue;
      // }

      if (object.$is("reservoir")) {
        // automatically return substances as present
        foundObjects.push(list[i]);
        continue;
      }

      // if it hasn't got a parent it can't be in hands
      // not originally intended but this applies directly to substances
      if (!object.getPlaceAsset) {
        continue;
      }
      var parent = object.getPlaceAsset();
      if (!parent) {
        continue;
      }

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

      if (parent.id === player.id && object.is.worn) {
        continue;
      }

      foundObjects.push(list[i]);
    }

    return foundObjects;
  };
})();