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

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

  /**
   * @augments {adventurejs.Verb}
   * @class tap
   * @ajsnode game.dictionary.verbs.tap
   * @ajsconstruct MyGame.createVerb({ "name": "tap", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading GesticulationVerbs
   * @summary Verb meaning tap, as in "tap keyboard".
   * @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; tap window</span>
   * You tap on the window in hopes of catching Nurse Emily's attention.
   * Unfortunately it's Doctor Fortescue who turns. You duck beneath the
   * window, now hoping to go unseen. Who knows what might happen if
   * you're caught out of bed. You might be branded a malingerer and sent
   * to the stockade...or worse, get sent back to the front lines.
   * </pre>
   * <p>
   * <strong>Tap</strong> a
   * {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset}.
   * Requires that the Asset to be tapped has
   * asset.dov.tap.enabled set to true.
   * By default, no special results will occur.
   * Authors wanting to make use of it may need to use a method such
   * as verb hooks. See
   * <a href="/doc/Scripting_VerbPhases.html">Verb Phases</a>
   * to learn more.
   * </p>
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   */
  A.Preverbs.tap = {
    name: "tap",
    prettyname: "tap",
    past_tense: "tapped",
    synonyms: ["tap"],
    gerund: "tapping",

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

    /**
     * @memberof tap
     * @ajsverbphrase
     * phrase1:
     * {
     *   accepts_noun:true,
     *   requires_noun: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     tangible: true,
     *     present: true,
     *     visible: true,
     *     reachable: true,
     *   },
     * },
     */
    phrase1: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        visible: true,
        reachable: true,
      },
    },

    /**
     * @memberof tap
     * @ajsverbphrase
     * phrase2:
     * {
     *   accepts_noun: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     tangible: true,
     *     present: true,
     *     visible: true,
     *     reachable: true,
     *   },
     *   accepts_preposition: true,
     *   requires_preposition: true,
     *   //accepts_these_prepositions: [ "with" ],
     * },
     */
    phrase2: {
      accepts_noun: true,
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        visible: true,
        reachable: true,
      },
      accepts_preposition: true,
      requires_preposition: true,
      //accepts_these_prepositions: ["with"],
    },

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

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

      // sentence structure: verb
      if (input.hasStructure("verb")) {
        this.game.debug(`D1995 | ${this.name}.js | no object given`);
        msg += `$(We) drum $(our) fingers on the nearest surface. `;
        this.handleFailure(msg);
        return null;
      }

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

      // single use direct object?
      if (
        direct_object.allowVerbOnce(this.name, "dov") &&
        direct_object.didVerb(this.name, "dov")
      ) {
        this.game.debug(
          `D2002 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.once and ${direct_object.id}.did.${this.name}.directly `
        );
        msg += `$(We've) already ${this.past_tense} ${direct_object.articlename} enough. `;
        this.handleFailure(msg);
        return false;
      }

      // sentence structure: verb noun
      if (input.hasStructure("verb noun")) {
        // can verb act without an indirect object?
        if (direct_object.allowVerbWithNothing(this.name, "dov")) {
          return true;
        }

        // indirect objects available?
        if (!direct_object.hasIndirectObjects(this.name)) {
          this.game.debug(
            `D2003 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.with_nothing is false `
          );
          msg += `$(We) don't know of a way to ${this.name} ${direct_object.articlename}. `;
          this.handleFailure(msg);
          return false;
        }

        // infer indirect object?
        results = this.tryToInferIndirectObject({
          direct_object: direct_object,
          context: player,
          handle_input: true,
        });
        if (results.prompt) {
          this.game.debug(`D2004 | ${this.name}.js | soft prompt for noun2 `);
          msg += `What would $(we) like to ${this.name} ${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
      if (input.hasStructure("verb preposition noun")) {
        if ("with" === direct_preposition) {
          // is player holding asset?
          if (!this.game.parser.selectInHands(direct_object.id).length) {
            this.game.debug(
              `D2001 | ${this.name}.js | ${direct_object.id}.$is("inhands") is false `
            );
            msg += `$(We're) not holding ${direct_object.articlename}. `;
            this.handleFailure(msg);
            return null;
          }
        }
      } // verb preposition noun

      // sentence structure: verb noun preposition noun
      if (input.hasStructure("verb noun preposition noun")) {
        if ("with" === indirect_preposition) {
          // is player holding asset?
          if (!this.game.parser.selectInHands(indirect_object.id).length) {
            this.game.debug(
              `D2008 | ${this.name}.js | ${indirect_object.id}.$is("inhands") is false `
            );
            msg += `$(We're) not holding ${indirect_object.articlename}. `;
            this.handleFailure(msg);
            return null;
          }
        }

        // 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}.dov.${this.name}.with_nothing `);
        //   msg += `$(We) can't ${this.name} ${direct_object.articlename} ${indirect_preposition} ${indirect_object.articlename}. `;
        //   this.handleFailure(msg);
        //   return null;
        // }

        // indirect object usable with direct object?
        if (
          !direct_object.allowVerbWithAsset(this.name, indirect_object, "dov")
        ) {
          this.game.debug(
            `D2005 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.with_assets/with_classes does not include ${indirect_object.id} `
          );
          msg += `$(We) can't ${this.name} ${direct_object.articlename} ${indirect_preposition} ${indirect_object.articlename}. `;
          this.handleFailure(msg);
          return null;
        }

        // can indirect object be used?
        if (!indirect_object.isIOV(this.name)) {
          this.game.debug(
            `D2006 | ${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(
            `D2007 | ${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 verb_phrase = input.verb_phrase;
      var player = this.game.getPlayer();
      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 = "";
      var results;

      // compose output
      msg +=
        `$(We) ${this.name}` +
        `${direct_preposition ? " " + direct_preposition : ""}` +
        `${direct_object ? " " + direct_object.articlename : ""}` +
        `${indirect_preposition ? " " + indirect_preposition : ""}` +
        `${indirect_object ? " " + indirect_object.articlename : ""}` +
        `. `;

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