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

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

  /**
   * @augments {adventurejs.Verb}
   * @class read
   * @ajsnode game.dictionary.verbs.read
   * @ajsconstruct MyGame.createVerb({ "name": "read", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading CompositionVerbs
   * @summary Verb meaning read asset, as in "read scroll".
   * @tutorial Scripting_VerbSubscriptions
   * @tutorial Verbs_VerbAnatomy
   * @tutorial Verbs_VerbProcess
   * @tutorial Verbs_ModifyVerbs
   * @tutorial Verbs_WriteVerbs
   * @ajsdemo WritingDemo, Office, Playroom, Classroom, Library, Scorecard
   * @ajscss Styles
   * @classdesc
   * <pre class="display border outline">
   * <span class="ajs-player-input">&gt; read grimoire</span>
   * You read the ancient grimoire. Instantly, your head begins to fill with
   * ancient spells. Lead to gold! Levitation! Spiritual transformation?
   * Your mind expands with a rush of incoming knowledge. Or does it?
   * Do minds have a maximum capacity? Does it feel as if the new
   * spells are overwriting your own memories? You try to recall a childhood
   * memory, and turn up a spell to purify water. You think back to your
   * first kiss, and find a spell to transform yourself into an eagle. You...
   * no, there is no you. There is only the spellbook, and the timeless, immortal
   * spells within it.
   * </pre>
   * <p>
   * <strong>Read</strong> requires that the
   * {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset} to be read has
   * asset.dov.read.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.read = {
    name: "read",
    prettyname: "read",
    past_tense: "read",
    synonyms: ["read"],
    gerund: "reading",

    /**
     * @ajsverbstructures
     * @memberof read
     */
    accepts_structures: [
      "verb noun", // read book
      "verb noun preposition noun", // read book with magnifying glass, read writing on wall
    ],

    /**
     * @memberof read
     * @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 read
     * @ajsverbphrase
     * 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,
     * },
     */
    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 read
     * @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 indirect_preposition = input.getPreposition(2);
      var indirect_inferred;
      var msg = "";
      var results;

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

      if (
        direct_object.must.hold_to_read &&
        subject.id !== direct_object.getPlaceAssetId()
      ) {
        this.game.debug(
          `D1389 | ${this.name}.js | ${direct_object.id}.must.hold_to_read `
        );
        msg += `{We} have to be holding ${direct_object.articlename} in order to read it. `;
        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(
          `D1604 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.once and ${direct_object.id}.did.${this.name}.directly `
        );
        msg += `{We've} already ${this.name} ${direct_object.articlename} enough. `;
        this.handleFailure(msg);
        return false;
      }

      if (input.hasStructure("verb noun")) {
        // indirect objects available?
        if (
          !direct_object.hasIndirectObjects(this.name) &&
          !direct_object.allowVerbWithNothing(this.name, "dov")
        ) {
          this.game.debug(
            `D1605 | ${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: subject,
          handle_input: true,
        });
        if (results.prompt) {
          // can verb act without an indirect object?
          if (direct_object.allowVerbWithNothing(this.name, "dov")) {
            return true;
          }

          this.game.debug(`D1606 | ${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 noun preposition noun
      if (input.hasStructure("verb noun preposition noun")) {
        if (indirect_preposition === "with") {
          // is subject holding asset?
          if (!this.game.parser.selectInHands(indirect_object.id).length) {
            this.game.debug(
              `D1608 | ${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 usable with direct object?
        if (
          !direct_object.allowVerbWithAsset({
            verb: this.name,
            asset: indirect_object,
            ov: "dov",
          })
        ) {
          this.game.debug(
            `D1609 | ${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(
            `D2111 | ${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(
            `D2112 | ${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 = "";

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

      if (direct_object.hasDescription("read")) {
        msg += this.game.getDescription({
          asset: direct_object,
          identifier: "read",
        });
      }

      if (direct_object.appearance.scribbles) {
        msg += `A few scribbles have been drawn on it. `;
      }

      if (direct_object.written_strings?.length) {
        if (direct_object.written_strings.length === 1) {
          msg += `A phrase has been written there in ${
            this.game.settings.apply_color_classes_to_written_strings
              ? direct_object.written_strings[0].color
              : ""
          } ${direct_object.written_strings[0].class.toLowerCase()}: `;
        } else {
          msg += `Some phrases have been written there: `;
        }

        for (let i = 0; i < direct_object.written_strings.length; i++) {
          msg += `<br> <span class="string ${direct_object.written_strings[i].class.toLowerCase()} ${
            this.game.settings.apply_color_classes_to_written_strings
              ? direct_object.written_strings[i].color
              : ""
          }">"${direct_object.written_strings[i].data}"</span>`;
        }
      }

      if (direct_object.typed_strings?.length) {
        msg += `${
          direct_object.typed_strings.length > 1
            ? "Some phrases have"
            : "A phrase has"
        } been typed there: `;
        for (let i = 0; i < direct_object.typed_strings.length; i++) {
          msg += `<br> <span class="string">"${direct_object.typed_strings[i]}"</span> `;
        }
      }

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