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

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

  /**
   * @augments {adventurejs.Verb}
   * @class turn
   * @ajsnode game.dictionary.verbs.turn
   * @ajsconstruct MyGame.createVerb({ "name": "turn", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading ManipulationVerbs
   * @summary Verb meaning turn, as in "turn faucet handle".
   * @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; turn sparrow</span>
   * You turn the carven sparrow that caps the newel post
   * of the teak balustrade. The little statue rotates smoothly
   * and clicks neatly into position at 45 degrees from its original
   * position. A bass rumble emanates from somewhere deep in the house.
   * </pre>
   * <pre class="display border outline">
   * <span class="input">&gt; turn off timer</span>
   * You turn off the bomb timer with only three seconds to spare!
   * </pre>
   * <pre class="display border outline">
   * <span class="input">&gt; turn on washing machine</span>
   * You turn on the washing machine. It immediately starts to
   * buck across the bar's cement floor. The other machine leaps
   * out ahead of yours. The race is on!
   * </pre>
   * <p>
   * <strong>Turn</strong> requires that the
   * {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset} to be turned has
   * asset.dov.turn.enabled set to true.
   * Turn offers a couple of distinct contexts. "Turn on" and "turn off"
   * do as you might expect. Just plain "turn" requires not only the
   * verb subscription but also that asset.can.turn is true.
   * Authors wanting to make use of turn may wish to use verb hooks. See
   * <a href="/doc/Scripting_VerbPhases.html">Verb Phases</a>
   * to learn more.
   * </p>
   * @ajsverbreactions
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   */
  A.Preverbs.turn = {
    name: "turn",
    prettyname: "turn",
    past_tense: "turned",
    synonyms: ["turn"],

    /**
     * @ajsverbstructures
     * @memberof remove
     */
    accepts_structures: [
      "verb noun", // turn statue
      "verb preposition noun", // turn on faucet
      "verb noun preposition", // turn faucet on
      "verb noun preposition noun", // turn faucet with wrench, turn statue to east
      "verb preposition noun preposition noun", // turn on faucet with wrench
    ],

    /**
     * @memberof turn
     * @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 turn
     * @ajsverbphrase
     * phrase2:
     * {
     *   accepts_noun: true,
     *   requires_noun: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     tangible: true,
     *     present: true,
     *     visible: true,
     *     reachable: true,
     *   },
     * },
     */
    phrase2: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        visible: true,
        reachable: true,
      },
      accepts_preposition: true,
      requires_preposition: true,
    },

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

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

      if (!direct_object.isDOV(this.name)) {
        this.game.debug(
          `F1455 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.enabled is unset `
        );
        msg += `${direct_object.Articlename} can't be ${this.past_tense}${
          direct_preposition ? " " + direct_preposition : ""
        }. `;
        this.handleFailure(msg);
        return null;
      }

      // single use direct object?
      if (
        direct_object.DOVallowOnce(this.name) &&
        direct_object.DOVdidDo(this.name)
      ) {
        this.game.debug(
          `F1933 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.once and ${direct_object.id}.dov.${this.name}.did_do `
        );
        msg += `${direct_object.Articlename} has already been ${this.past_tense}. `;
        this.handleFailure(msg);
        return false;
      }

      if (input.hasStructure("verb noun")) {
        if (!direct_object.can.turn) {
          this.game.debug(
            `F1183 | ${this.name}.js | ${direct_object.id}.can.${this.name} is unset `
          );
          msg += `${direct_object.Articlename} can't be ${this.past_tense}. `;
          this.handleFailure(msg);
          return null;
        }
        // indirect object required?
        if (direct_object.DOVallowWithNothing(this.name)) {
          return true;
        }

        // indirect objects available?
        if (!direct_object.DOVhasIndirectObjects(this.name)) {
          this.game.debug(
            `F1934 | ${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 null;
        }

        // infer indirect object?
        results = this.tryToInferIndirectObject(direct_object, true);
        if (results.prompt) {
          this.game.debug(`F1935 | ${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";
        }
      } // verb noun

      // sentence structure: verb noun preposition
      if (input.hasStructure("verb noun preposition")) {
        if (["on", "off"].indexOf(indirect_preposition) > -1) {
          input.setPreposition(1, indirect_preposition);
          input.deletePhrase(2);
          input.setStructure("verb preposition noun");
        } else {
          this.game.debug(
            `F1068 | ${this.name}.js | ${this.name} ${indirect_preposition} not handled `
          );
          msg += `$(We) don't know how to ${this.name} ${direct_object.articlename} ${indirect_preposition}. `;
          this.handleFailure(msg);
          return null;
        }
      } // verb noun preposition

      // sentence structure: verb preposition noun
      // ex: turn on radio, turn off computer
      // @TODO turn in thief
      if (input.hasStructure("verb preposition noun")) {
        if (["on", "off"].indexOf(direct_preposition) === -1) {
          this.game.debug(
            `F1070 | ${this.name}.js | ${this.name} ${direct_preposition} not handled `
          );
          msg += `$(We) don't know how to ${this.name} ${direct_preposition} ${direct_object.articlename}. `;
          this.handleFailure(msg);
          return null;
        }
      } // verb preposition noun

      // sentence structure: verb noun preposition noun
      // ex: turn statue toward window, turn statue with lever
      if (input.hasStructure("verb noun preposition noun")) {
        if (["to", "from", "with"].indexOf(indirect_preposition) === -1) {
          this.game.debug(
            `F1072 | ${this.name}.js | ${this.name} ${direct_object} ${indirect_preposition} ${indirect_object} not handled `
          );
          msg += `$(We) don't know how to ${this.name} ${direct_object} ${indirect_preposition} ${indirect_object}. `;
          this.handleFailure(msg);
          return null;
        }
      } // verb noun preposition noun

      // sentence structure: verb preposition noun preposition noun
      // ex: turn on faucet with wrench, turn off faucet with wrench
      // @TODO turn in thief to police?
      if (input.hasStructure("verb preposition noun preposition noun")) {
        if (
          ["on", "off"].indexOf(direct_preposition) === -1 ||
          indirect_preposition !== "with"
        ) {
          this.game.debug(
            `F1499 | ${this.name}.js | ${this.name} ${direct_object} ${indirect_preposition} ${indirect_object} not handled `
          );
          msg += `$(We) don't know how to ${this.name} ${direct_preposition} ${direct_object} ${indirect_preposition} ${indirect_object}. `;
          this.handleFailure(msg);
          return null;
        }
      } // verb preposition noun preposition noun

      if (
        input.hasStructure("verb noun preposition noun") ||
        (input.hasStructure("verb preposition noun preposition noun") &&
          "with" === indirect_preposition)
      ) {
        // 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(
            `F1597 | ${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.DOVallowWithAsset(this.name, indirect_object)) {
          this.game.debug(
            `F1930 | ${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(
            `F1499 | ${this.name}.js | ${indirect_object.id}.iov.${this.name} is unset `
          );
          msg += `${indirect_object.Articlename} can't be used to ${this.name} anything. `;
          this.handleFailure(msg);
          return null;
        }

        // single use indirect object?
        if (
          indirect_object.IOVallowOnce(this.name) &&
          indirect_object.IOVdidDo(this.name)
        ) {
          this.game.debug(
            `F1932 | ${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;
        }
      }

      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(`F1456 | ${this.name}.js | print doSuccess `);

      if (direct_preposition === "on") direct_object.is.on = true;
      if (direct_preposition === "off") direct_object.is.on = false;

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

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