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

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

  /**
   * @augments {adventurejs.Verb}
   * @class talk
   * @ajsnode game.dictionary.verbs.talk
   * @ajsconstruct MyGame.createVerb({ "name": "talk", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading ConversationVerbs
   * @summary Verb that means talk, as in "talk to robot".
   * @tutorial BasicVerbs_Subscriptions
   * @tutorial AdvancedVerbs_VerbAnatomy
   * @tutorial AdvancedVerbs_VerbProcess
   * @tutorial AdvancedVerbs_ModifyVerbs
   * @tutorial AdvancedVerbs_ModifyVerbs
   * @classdesc
   * <pre class="display border outline">
   * <span class="ajs-player-input">&gt; talk to robot</span>
   * You make advances on the robot. Meep morp!
   * </pre>
   * <p>
   * <strong>Talk</strong> can be intransitive,
   * meaning it takes no object. Talk doesn't
   * provide any special logic.
   * Authors wanting to make use of it may need
   * to use a method such as verb hooks. See
   * <a href="/doc/BasicVerbs_PhaseHooks.html">Verb Phases</a>
   * to learn more.
   * </p>
   * @todo talk to character, talk to character through thing
   * @ajsverbreactions
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   */
  A.Preverbs.talk = {
    name: "talk",
    past_tense: "talked",
    synonyms: [],
    gerund: "talking",
    can_try: true,

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

    /**
     * @memberof talk
     * @ajsverbphrase
     * phrase1:
     * {
     *   accepts_noun: true,
     *   accepts_preposition: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *   },
     *   accepts_preposition: true,
     *   preposition_must_be: [ "to", "at", "through", "in" ],
     * },
     */
    phrase1: {
      accepts_noun: true,
      noun_must_be: { known: true },
      accepts_preposition: true,
      preposition_must_be: ["to", "at", "through", "in"],
    },

    /**
     * @memberof talk
     * @ajsverbphrase
     * phrase2:
     * {
     *   accepts_noun: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *   },
     *   accepts_preposition: true,
     *   requires_preposition: true,
     * },
     */
    phrase2: {
      accepts_noun: true,
      noun_must_be: { known: true },
      accepts_preposition: true,
      requires_preposition: true,
      preposition_must_be: ["to", "at", "through", "in"],
    },

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

    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 room = subject.getRoom();
      var msg = "";

      if (subject.is.mute) {
        this.game.debug(`D1653`, `${this.name}.js `, ` ${subject.id}.is.mute `);
        msg += `{We} can't talk at all. `;
        this.handleFailure(msg);
        return null;
      }

      if (room.is.vacuum) {
        this.game.debug(`D1656`, `${this.name}.js `, ` ${room.id}.is.vacuum `);
        msg += `In space, no one can hear you scream. `;
        this.handleFailure(msg);
        return null;
      }

      if (input.hasStructure("verb noun")) {
        direct_preposition = "to";
        input.setStructure("verb preposition noun");
      }

      if (input.hasStructure("verb noun preposition noun")) {
        direct_preposition = "to";
        input.setStructure("verb preposition 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 room = subject.getRoom();
      var results;
      var msg = "";

      // compose output
      if (input.hasStructure("verb")) {
        msg += `{We} mumble a few words to {ourself}. `;
      } else {
        msg +=
          `{We} ${this.agree()}` +
          (direct_preposition ? " " + direct_preposition : "") +
          (" " + direct_object.article_name) +
          (indirect_preposition ? " " + indirect_preposition : "") +
          (indirect_object ? " " + indirect_object.article_name : "") +
          `. `;
      }

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