Pre-release
AdventureJS Docs Downloads Devlog
Score: 0 Moves: 0
// canPutThisInThat.js
(function () {
  /* global AdventureJS A */
  var p = AdventureJS.Assets.Entity.prototype;
  /**
   * <strong>canPutThisInThat</strong>
   * checks to see if one asset can be placed within
   * the specified aspect of another specified asset.
   * For example, "put sword in stone" and
   * "push stone into depression" would both be
   * tested with this function.
   * @memberOf AdventureJS.Dictionary.Verb
   * @method AdventureJS.Dictionary.Verb#canPutThisInThat
   * @param {String} preposition
   * @param {Object} indirect_object
   * @returns {Object}
   */
  p.canPutThisInThat = function Entity_canPutThisInThat(
    preposition,
    indirect_object
  ) {
    var response = { fail: false, msg: "", status: "", end_turn: false };
    var asset_aspect;
    var msg = "";
    var can = true;
    if ("string" === typeof indirect_object)
      indirect_object = this.game.getAsset(indirect_object);
    if (preposition === "at" && indirect_object?.default_aspect) {
      preposition = indirect_object.default_aspect;
    }
    if (
      !(this instanceof AdventureJS.Assets.Entity) ||
      !indirect_object ||
      !(indirect_object instanceof AdventureJS.Assets.Entity) ||
      !preposition ||
      !indirect_object.hasAspectAt(preposition)
    ) {
      this.game.debug(
        `D1670`,
        `Entity.js `,
        `can't move ${this.id || ""} into ${indirect_object.id || ""} `
      );
      if (!indirect_object.hasAspectAt(preposition)) {
        msg += `{We} can't put anything ${preposition} ${indirect_object.article_name}. `;
      } else {
        msg += this.game.settings.getUnparsedMessage(
          this.game.getInput().input
        );
      }
      response = {
        fail: true,
        msg: msg,
        status: "bad_request",
        end_turn: false,
      };
      return response;
    }

    // is indirect_object closed?
    if (
      "in" === preposition &&
      indirect_object.isDOV("close") &&
      indirect_object.is.closed
    ) {
      // @TODO see if it can be automatically opened
      let can_open = false;

      // does game allow auto opening?
      if (this.game.settings.auto_open_containers) {
        // does game require first use?
        if (!this.game.settings.auto_open_containers_after_first_use) {
          can_open = true;
        }
        // if so, has it been opened before?
        else if (indirect_object.didDoVerbs(["open"])) {
          can_open = true;
        }
      }

      // is it locked?
      if (indirect_object.isDOV("lock") && indirect_object.is.locked) {
        // does it need a key?
        // does player have a key?
        // has player used the key?
      }

      // is it sealed?
      if (indirect_object.isDOV("seal") && indirect_object.is.sealed) {
        // does it need a key?
        // does player have a key?
        // has player used the key?
      }

      if (!can_open) {
        this.game.debug(`D1671`, `Entity.js `, `indirect_object.id.is.closed `);
        msg += indirect_object.Article_name + " is closed.";
        // this.handleFailure(msg);
        // return false;
        response = {
          fail: true,
          msg: msg,
          status: "closed",
          end_turn: true,
        };
        return response;
      }
    }

    // does indirect object aspect limit what assets can be put in it?
    var with_classes = indirect_object.aspects[preposition].with_classes;
    if (with_classes.length > 0) {
      can = false;
      for (var i = 0; i < with_classes.length; i++) {
        var clas = with_classes[i];
        let c = AdventureJS[clas] || AdventureJS.Assets[clas];
        if (c && this instanceof c) {
          can = true;
          break;
        }
      }
      if (!can) {
        this.game.debug(
          `D1661`,
          `Entity.js `,
          `${this.id}.class ${this.class} is not among ${indirect_object.id}.aspects.${preposition}.with_classes `
        );
        msg += `${this.Article_name} can't be placed ${preposition} ${indirect_object.article_name}. `;
        response = {
          fail: true,
          msg: msg,
          status: "with_classes",
          end_turn: false,
        };
        return response;
      }
    }

    // does indirect object aspect limit what classes can be put in it?
    var with_assets = indirect_object.aspects[preposition].with_assets;
    if (with_assets.length > 0) {
      can = false;
      for (var i = 0; i < with_assets.length; i++) {
        if (this.id === with_assets[i]) {
          can = true;
          break;
        }
      }
      if (!can) {
        this.game.debug(
          `D1662`,
          `Entity.js `,
          `${this.id} is not among ${indirect_object.id}.aspects.${preposition}.with_assets `
        );
        msg += `${this.Article_name} can't be placed ${preposition} ${indirect_object.article_name}. `;
        response = {
          fail: true,
          msg: msg,
          status: "with_assets",
          end_turn: false,
        };
        return response;
      }
    }

    asset_aspect = indirect_object.getAspectAt(preposition);

    if (
      asset_aspect.contents_limits.width > -1 &&
      this.dimensions.width > asset_aspect.contents_limits.width
    ) {
      this.game.debug(
        `D1667`,
        `Entity.js `,
        `${this.id}.dimensions.width > ${indirect_object.id}.aspects.${preposition}.contents_limits.width `
      );
      msg += `${this.Article_name} doesn't fit ${preposition} ${indirect_object.article_name}. `;
      response = {
        fail: true,
        msg: msg,
        status: "maxwidth",
        end_turn: false,
      };
      return response;
    }

    if (
      asset_aspect.contents_limits.height > -1 &&
      this.dimensions.height > asset_aspect.contents_limits.height
    ) {
      this.game.debug(
        `D1127`,
        `Entity.js `,
        `${this.id}.dimensions.height > ${indirect_object.id}.aspects.${preposition}.contents_limits.height `
      );
      msg += `${this.Article_name} doesn't fit ${preposition} ${indirect_object.article_name}. `;
      response = {
        fail: true,
        msg: msg,
        status: "maxheight",
        end_turn: false,
      };
      return response;
    }

    if (
      asset_aspect.contents_limits.depth > -1 &&
      this.dimensions.depth > asset_aspect.contents_limits.depth
    ) {
      this.game.debug(
        `D1128`,
        `Entity.js `,
        `${this.id}.dimensions.depth > ${indirect_object.id}.aspects.${preposition}.contents_limits.depth `
      );
      msg += `${this.Article_name} doesn't fit ${preposition} ${indirect_object.article_name}. `;
      response = {
        fail: true,
        msg: msg,
        status: "maxdepth",
        end_turn: false,
      };
      return response;
    }

    if (
      asset_aspect.contents_limits.weight > -1 &&
      this.dimensions.weight > asset_aspect.contents_limits.weight
    ) {
      this.game.debug(
        `D1668`,
        `Entity.js `,
        `${this.id}.dimensions.weight > ${indirect_object.id}.aspects.${preposition}.contents_limits.weight `
      );
      msg += `${this.Article_name_is} too heavy to ${this.name} ${preposition} ${indirect_object.article_name}. `;
      response = {
        fail: true,
        msg: msg,
        status: "maxweight",
        end_turn: false,
      };
      return response;
    }

    if (
      asset_aspect.contents_limits.count > -1 &&
      asset_aspect.contents.length >= asset_aspect.contents_limits.count
    ) {
      this.game.debug(
        `D1669`,
        `Entity.js `,
        `${indirect_object.id}.aspects.${preposition}.contents.length >= ${indirect_object.id}.aspects.${preposition}.contents_limits.count `
      );
      msg += `Nothing ${asset_aspect.contents_limits.count > 0 ? "more " : ""} can be put ${preposition} ${indirect_object.article_name}. `;
      response = { fail: true, msg: msg, status: "maxcount", end_turn: true };
      return response;
    }

    return response;
  }; // canPutThisInThat
})();