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

(function () {
  /*global adventurejs A*/
  "use strict";

  /**
   * @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="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",

    /**
     * @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, // TODO does a sound need to be known?
     *     //tangible: true, // TODO is a sound intangible?
     *     present: true,
     *   },
     *   accepts_preposition: true,
     *   require_preposition: true,
     *   accepts_these_prepositions: ["to"],
     * },
     */
    phrase1: {
      accepts_noun: true,
      noun_must_be: {
        known: true,
        present: true,
      },
      accepts_preposition: true,
      require_preposition: true,
      accepts_these_prepositions: ["to"],
    },

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

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

    doTry: function () {
      var input = this.game.getInput();
      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 currentRoom = this.game.getCurrentRoom();
      var player = this.game.getPlayer();
      var results, hook;
      var msg = "";

      if (player.is.deaf) {
        this.game.debug(`F1336 | ${this.name}.js | player.is.deaf `);
        msg += `$(We) can't hear anything at all. `;
        this.handleFailure(msg);
        return null;
      }

      if (!player.can.hear || !player.getNestOrPlaceAspect().player.can.hear) {
        this.game.debug(
          `F1598 | ${
            this.name
          }.js | player.can.hear is false or ${player.getNestOrPlaceAsset()}.aspects.${player.getNestOrPlacePreposition()} }.player.can.hear is false`
        );
        msg += `$(We) can't hear anything. `;
        this.handleFailure(msg);
        return null;
      }

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

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

      if (!direct_object.isDOV(this.name)) {
        this.game.debug(
          `F1599 | ${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(
          `F1413 | ${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 = direct_object.getDescription("sound");
      }

      // sentence structure: verb noun
      if (input.hasStructure("verb noun")) {
        // indirect object required?
        if (direct_object.DOVallowWithNothing(this.name)) {
          return true;
        }

        // indirect objects available?
        if (!direct_object.DOVhasIndirectObjects(this.name)) {
          this.game.debug(
            `F1783 | ${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, true);
        if (results.prompt) {
          this.game.debug(`F1784 | ${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";
        }
      } // 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.DOVallowWithAnything(this.name)) {
          return true;
        }

        // indirect object not required?
        if (direct_object.DOVallowWithNothing(this.name)) {
          this.game.debug(
            `F1785 | ${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.DOVallowWithAsset(this.name, indirect_object)) {
          this.game.debug(
            `F1786 | ${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(
            `F1902 | ${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.IOVallowOnce(this.name) &&
          indirect_object.IOVdidDo(this.name)
        ) {
          this.game.debug(
            `F1808 | ${this.name}.js | ${indirect_object.id}.iov.${
              this.name
            }.once and ${indirect_object.id}.iov.${this.name}.do_count is ${
              indirect_object.iov[this.name].do_count
            } `
          );
          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 direct_object = input.getAsset(1);
      var direct_preposition = input.getPreposition(1);
      var indirect_object = input.getAsset(2);
      var indirect_preposition = input.getPreposition(2);
      var player = this.game.getPlayer();
      var results;
      var msg = "";

      this.game.debug(`F1337 | ${this.name}.js | print doSuccess `);

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

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

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

      // 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
      this.handleSuccess(msg, direct_object);
      return true;
    },
  };
})();