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

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

  var p = AdventureJS.Parser.prototype;

  /**
   * Exclude from a list of assets all assets in subject's inventory.
   * @method AdventureJS.Parser#selectNotNestedIfAll
   * @memberOf AdventureJS.Parser
   * @param {Array} list
   * @param {Object} context An optional context asset, for "take from asset"
   * @returns {Array}
   */
  p.selectNotNestedIfAll = function Parser_selectNotNestedIfAll(
    list,
    context = null
  ) {
    if ("string" === typeof list) list = [list];
    if (!Array.isArray(list)) {
      this.game.log(
        "L1242",
        "error",
        "critical",
        [
          "[selectNotNestedIfAll.js] selectNotNestedIfAll() received non-array",
          list,
        ],
        "Parser"
      );
      return [];
    }
    this.game.log(
      "L1177",
      "log",
      "high",
      `[selectNotNestedIfAll.js] selectNotNestedIfAll() receive:\n${list}`,
      "Parser"
    );
    const input = this.game.getInput();
    const subject = input.getSubject();
    const foundObjects = [];
    const room = this.game.getRoom();

    for (let i = 0; i < list.length; i++) {
      const object = this.game.getAsset(list[i]);

      // if it hasn't got a place it can't be in inventory
      // not originally intended but this applies directly to substances
      if (!object.getPlaceAsset) continue;
      const place = object.getPlaceAsset();
      if (!place) continue;

      // exclude held things
      // @TODO should this be subject to context?
      if (subject.isConnectedToAsset("hold", object, "to_dov")) {
        continue;
      }

      // was a context specified?
      if (context && context.id === place.id) {
        // allow this
        foundObjects.push(list[i]);
        continue;
      } else if (context && context.id !== place.id) {
        // not an immediate child of context
        continue;
      }
      // everything else
      else if (!context) {
        // exclude inventory nested in subject
        if (object.isWithin(subject) && place.id !== subject.id) {
          continue;
        }

        // only take things from the same place as subject
        const subject_place = subject.getPlaceAsset();
        if (place.id !== subject_place.id) {
          continue;
        }
        // @TODO also include things from reachable assets
      }

      foundObjects.push(list[i]);
    }

    this.game.log(
      "L1662",
      "log",
      "high",
      `[selectNotNestedIfAll.js] selectNotNestedIfAll() return:\n${foundObjects}`,
      "Parser"
    );

    return foundObjects;
  };
})();