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

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

  /**
   * @augments {adventurejs.Verb}
   * @class hide
   * @ajsnode game.dictionary.verbs.hide
   * @ajsconstruct MyGame.createVerb({ "name": "hide", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading UtilityVerbs
   * @summary Summary.
   * @tutorial Scripting_VerbSubscriptions
   * @tutorial Verbs_VerbAnatomy
   * @tutorial Verbs_VerbProcess
   * @tutorial Verbs_ModifyVerbs
   * @tutorial Verbs_WriteVerbs
   * <pre class="display border outline">
   * <span class="ajs-player-input">&gt; hide under bed</span>
   * You crawl under the bed. No one will find you now!
   * </pre>
   * <pre class="display border outline">
   * <span class="ajs-player-input">&gt; hide amulet in chest</span>
   * You secrete the amulet in the chest. No one will find it now!
   * </pre>
   * <strong>Hide</strong> extends two verbs:
   * <a href="put.html">put</a> and
   * <a href="go.html">go</a>; and forwards
   * to one or the other depending on context.
   * @ajsverbphases
   */
  A.Preverbs.hide = {
    name: "hide",
    prettyname: "hide",
    past_tense: "hid",
    synonyms: ["hide"],
    extends: { go: true, put: true },
    gerund: "hiding",

    /**
     * @ajsverbstructures
     * @memberof hide
     */
    accepts_structures: [
      "verb noun",
      "verb preposition noun",
      "verb noun preposition noun",
    ],

    /**
     * @memberof verb
     * @ajsverbphrase
     * phrase1: {
     *   accepts_noun: true,
     *   noun_must_be: {
     *     known: true,
     *     not_global: true,
     *     not_scenery: true,
     *     not_exit: true,
     *     tangible: true,
     *     present: true,
     *     visible: true,
     *     reachable: true,
     *   },
     *   accepts_preposition: true,
     * },
     */

    phrase1: {
      accepts_noun: true,
      noun_must_be: {
        known: true,
        not_global: true,
        not_scenery: true,
        not_exit: true,
        tangible: true,
        present: true,
        visible: true,
        reachable: true,
      },
      accepts_preposition: true,
    },

    /**
     * @memberof verb
     * @ajsverbphrase
     * phrase1: {
     *   accepts_noun: true,
     *   accepts_preposition: true,
     *   requires_preposition: true,
     *   noun_must_be: {
     *     known: true,
     *     not_global: true,
     *     not_scenery: true,
     *     not_exit: true,
     *     tangible: true,
     *     present: true,
     *     visible: true,
     *     reachable: true,
     *   },
     * },
     */

    phrase2: {
      accepts_noun: true,
      accepts_preposition: true,
      requires_preposition: true,
      noun_must_be: {
        known: true,
        not_global: true,
        not_scenery: true,
        not_exit: true,
        tangible: true,
        present: true,
        visible: true,
        reachable: true,
      },
    },

    let_verb_handle_disambiguation: false,

    doTry: function () {
      var input = this.game.getInput();
      var subject = input.getSubject();
      var direct_object = input.getAsset(1);
      var direct_preposition = input.getPreposition(1);
      var indirect_object = input.getAsset(2);
      var indirect_preposition = input.getPreposition(2);
      var msg = "",
        target = "";

      // sentence structure: verb noun ----------
      if (input.hasStructure("verb noun")) {
        // is subject carrying direct object?
        if (!direct_object.isWithin(subject)) {
          this.game.debug(
            `D1408 | ${this.name}.js | ${direct_object.id} is not in ${subject.id}`
          );
          msg += `{We're} not carrying ${direct_object.articlename}. `;
          this.handleFailure(msg);
          return null;
        }
        return true;
      }

      // sentence structure: verb preposition noun ----------
      else if (input.hasStructure("verb preposition noun")) {
        // hide from?
        if (direct_preposition === "from") {
          return true;
        }
        //
        else {
          target = "go";
          this.game.log(
            "L1523",
            "log",
            "high",
            `[${this.name}.js] ${this.name} ${direct_preposition} ${direct_object.name}: infer ${target}`,
            "Verbs"
          );
        }
      }

      // sentence structure: verb noun preposition noun ----------
      else if (input.hasStructure("verb noun preposition noun")) {
        // hide from?
        if (indirect_preposition === "from") {
          // is subject carrying direct object?
          if (!direct_object.isWithin(subject)) {
            this.game.debug(
              `D1409 | ${this.name}.js | ${direct_object.id} is not in ${subject.id}`
            );
            msg += `{We're} not carrying ${direct_object.articlename}. `;
            this.handleFailure(msg);
            return null;
          }
          return true;
        }
        //
        else {
          target = "put";
          this.game.log(
            "L1524",
            "log",
            "high",
            `[${this.name}.js] ${this.name} ${direct_object.name} ${indirect_preposition} ${indirect_object.name}: infer ${target}`,
            "Verbs"
          );
        }
      }

      return this.game.dictionary.doVerb(target);
    },

    doSuccess: function () {
      var input = this.game.getInput();
      var subject = input.getSubject();
      var direct_object = input.getAsset(1);
      var direct_preposition = input.getPreposition(1);
      var indirect_object = input.getAsset(2);
      var indirect_preposition = input.getPreposition(2);
      var msg = "";

      if (input.hasStructure("verb noun")) {
        // --------------------------------------------------
        // compose output
        // --------------------------------------------------
        msg += `{We} ${this.agree()} ${direct_object.articlename}. `;
      }

      // sentence structure: verb preposition noun ----------
      else if (
        input.hasStructure("verb preposition noun") &&
        direct_preposition === "from"
      ) {
        // --------------------------------------------------
        // compose output
        // --------------------------------------------------
        msg += `{We} ${this.agree()} ${direct_preposition} ${direct_object.articlename}`;
        if (!direct_object.hasClass("Character")) {
          msg += ` (like ${direct_object.articlename} cares)`;
        }
        msg += `. `;
      }

      // sentence structure: verb noun preposition noun ----------
      else if (
        input.hasStructure("verb noun preposition noun") &&
        indirect_preposition === "from"
      ) {
        // --------------------------------------------------
        // compose output
        // --------------------------------------------------
        msg += `{We} ${this.agree()} ${direct_object.articlename} ${indirect_preposition}  ${indirect_object.articlename}. `;
      }

      // any other form will have been handled by go or put
      else {
        return this.handleSuccess();
      }

      // --------------------------------------------------
      // print output
      // --------------------------------------------------
      return this.handleSuccess(msg, direct_object);
    },
  };
})(); // hide