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

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

  /**
   * @augments {adventurejs.Verb}
   * @class listen
   * @ajsnode game.dictionary.verbs.listen
   * @ajsconstruct MyGame.createVerb({ "name": "listen", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading SensationVerbs
   * @summary Verb meaning listen to asset or room.
   * @todo Finish with room tone handling.
   * @tutorial Scripting_VerbSubscriptions
   * @tutorial Verbs_VerbAnatomy
   * @tutorial Verbs_VerbProcess
   * @tutorial Verbs_ModifyVerbs
   * @tutorial Verbs_WriteVerbs
   * @classdesc
   * <pre class="display border outline">
   * <span class="ajs-player-input">&gt; listen</span>
   * You hold still and listen. The chain falls
   * down the stone well, bouncing endlessly from side to side,
   * creating a wince inducing amount of noise. Each time you
   * think it may have reached bottom, there is another loud
   * rattle and clunk... and finally, a splash. You wait, ears
   * straining to catch any residual sounds. After a long pregnant
   * moment, a moan begins far below, slowly rising into a shriek,
   * followed by chants and screams. Uh oh.
   * </pre>
   * <p>
   * <strong>Listen</strong> tries to find a sound description.
   * If no direct object is provided, it looks first at the current
   * room.descriptions.sound and then at the global_sound.description.
   * If a direct object is provided, it looks for
   * asset.descriptions.sound.
   * </p>
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   */
  A.Preverbs.listen = {
    name: "listen",
    synonyms: ["listen"],
    past_tense: "listened",
    gerund: "listening",

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

    /**
     * @memberof listen
     * @ajsverbphrase
     * phrase1:
     * {
     *   accepts_noun: true,
     *   //requires_noun: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     present: true,
     *   },
     *   accepts_preposition: true,
     *   require_preposition: true,
     *   preposition_must_be: ["to"],
     * },
     */
    phrase1: {
      accepts_noun: true,
      noun_must_be: { known: true, present: true },
      accepts_preposition: true,
      require_preposition: true,
      preposition_must_be: ["to"],
    },

    /**
     * @memberof close
     * @ajsverbphrase
     * phrase2:
     * {
     *   accepts_noun: true,
     *   noun_must_be:
     *   {
     *     in_inventory: true,
     *     known: true,
     *   },
     *   accepts_preposition: true,
     *   requires_preposition: true,
     *   preposition_must_be: ["with"],
     * },
     */
    phrase2: {
      accepts_noun: true,
      noun_must_be: { in_inventory: true, known: true },
      accepts_preposition: true,
      requires_preposition: true,
      preposition_must_be: ["with"],
    },

    /**
     * @memberof listen
     * @ajsverbparams
     * with_params: {},
     */
    with_params: {},

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

      if (!subject.can.hear) {
        this.game.debug(
          `D1336 | ${this.name}.js | ${subject.id}.can.hear is false `
        );
        msg += `{We} can't hear anything at all. `;
        this.handleFailure(msg);
        return null;
      }

      if (!subject.getNestOrPlaceAspect().canCharacter("hear")) {
        this.game.debug(
          `D1598 | ${this.name}.js | ${
            subject.getNestOrPlaceAsset().id
          }.aspects.${subject.getNestOrPlacePreposition()}.nest.can.hear is false`
        );
        msg += `{We} can't hear anything here. `;
        this.handleFailure(msg);
        return null;
      }

      // sentence structure: verb
      if (input.hasStructure("verb")) {
        // does current room have sound?
        if (room.hasDescription("sound")) {
          input.verb_params.source = room.descriptions.sound;
        } else {
          // try to get global_sound
          input.verb_params.source =
            this.game.getGlobalAssetDescription("global_sound");
        }
        if (input.verb_params.source) {
          return true;
        } else {
          this.game.debug(`D1782 | ${this.name}.js | no sound found `);
          msg += `{We} {don't} hear any particular sound. `;
          this.handleFailure(msg);
          return null;
        }
      } // verb

      // sentence structure: verb noun
      if (input.hasStructure("verb noun")) {
      } // verb noun

      if (!direct_object.isDOV(this.name)) {
        this.game.debug(
          `D1599 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.enabled is false `
        );
        msg += `{We} can't hear ${direct_object.articlename}. `;
        this.handleFailure(msg);
        return null;
      }

      if (!direct_object.hasDescription("sound")) {
        this.game.debug(
          `D1413 | ${this.name}.js | ${direct_object.id}.descriptions.sound is unset `
        );
        msg += `{We} can't hear any particular sound from ${direct_object.articlename}. `;
        this.handleFailure(msg);
        return null;
      } else {
        input.verb_params.source = this.game.getDescription({
          asset: direct_object,
          identifier: "sound",
        });
      }

      // sentence structure: verb noun
      if (input.hasStructure("verb noun")) {
        // indirect objects available?
        if (
          !direct_object.hasIndirectObjects(this.name) &&
          !direct_object.allowVerbWithNothing(this.name, "dov")
        ) {
          this.game.debug(
            `D1783 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.with_nothing is false `
          );
          msg += `{We} {don't} have any way to ${this.name} to ${direct_object.articlename}. `;
          this.handleFailure(msg);
          return null;
        }

        // infer indirect object?
        results = this.tryToInferIndirectObject({
          direct_object: direct_object,
          context: subject,
          handle_input: true,
        });
        if (results.prompt) {
          // indirect object required?
          if (direct_object.allowVerbWithNothing(this.name, "dov")) {
            return true;
          }

          this.game.debug(`D1784 | ${this.name}.js | soft prompt for noun2 `);
          msg += `What would {we} like to ${this.name} to ${direct_object.articlename} with? `;
          this.handleFailure(msg);
          return false;
        } else if (results.success) {
          indirect_object = results.indirect_object;
          indirect_preposition = "with";
          indirect_inferred = true;
          input.setAsset(2, indirect_object);
          input.setPreposition(2, indirect_preposition);
          input.setStructure("verb noun preposition noun");
          this.game.printInferred(
            `${indirect_preposition} ${indirect_object.articlename}`
          );
        }
      } // verb noun

      // sentence structure: verb preposition noun preposition noun
      if (input.hasStructure("verb preposition noun preposition noun")) {
        // works with any indirect object?
        if (direct_object.allowVerbWithAnything(this.name, "dov")) {
          return true;
        }

        // indirect object not required?
        // if (direct_object.allowVerbWithNothing(this.name, "dov")) {
        //   this.game.debug(
        //     ` | ${this.name}.js | ${direct_object.id} can't be ${this.state} by ${indirect_object.id} `
        //   );
        //   msg += `{We} can't ${this.name} ${direct_preposition} ${direct_object.articlename} ${indirect_preposition} ${indirect_object.articlename}. `;
        //   this.handleFailure(msg);
        //   return null;
        // }

        // indirect object usable with direct object?
        if (
          !direct_object.allowVerbWithAsset({
            verb: this.name,
            asset: indirect_object,
            ov: "dov",
          })
        ) {
          this.game.debug(
            `D1786 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.with_assets does not include ${indirect_object.id} `
          );
          msg += `${indirect_object.Articlename} can't be used to ${this.name} to ${direct_object.articlename}. `;
          this.handleFailure(msg);
          return null;
        }

        // can indirect object be used?
        if (!indirect_object.isIOV(this.name)) {
          this.game.debug(
            `D1902 | ${this.name}.js | ${indirect_object.id}.iov.${this.name}.enabled is false `
          );
          msg += `{We} can't ${this.name} anything ${indirect_preposition} ${indirect_object.articlename}. `;
          this.handleFailure(msg);
          return false;
        }

        // single use indirect object?
        if (
          indirect_object.allowVerbOnce(this.name, "iov") &&
          indirect_object.iDidVerb(this.name, "iov")
        ) {
          this.game.debug(
            `D1808 | ${this.name}.js | ${indirect_object.id}.iov.${
              this.name
            }.once and ${indirect_object.id}.did.${this.name}.indirectly is ${
              indirect_object.did[this.name].indirectly
            } `
          );
          msg += `${indirect_object.Articlename} has already been used to ${this.name} something. `;
          this.handleFailure(msg);
          return null;
        }
      } // verb noun preposition noun

      return true;
    },

    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 results;
      var msg = "";

      if (subject.id !== this.game.getPlayer().id) {
        msg += `${subject.articlename} cocks {our} head to listen. `;
        return this.handleSuccess(msg, direct_object);
      }

      // compose output
      msg += direct_object
        ? `{We} tilt an ear toward ${direct_object.articlename}`
        : ``;
      msg += indirect_object
        ? `${indirect_preposition} ${indirect_object.articlename}}`
        : ``;
      msg += direct_object ? `. ` : ``;
      msg += input.verb_params.source
        ? A.getSAF.call(this.game, input.verb_params.source)
        : `{We} {don't} hear anything in particular. `;

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