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

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

  /**
   * @augments {adventurejs.Verb}
   * @class turnIn
   * @ajsnode game.dictionary.verbs.turnIn
   * @ajsconstruct MyGame.createVerb({ "name": "turnIn", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading ManipulationVerbs
   * @ajsstate turnedOn
   * @summary Verb meaning turn in, as in "turn in thief".
   * @tutorial Scripting_VerbSubscriptions
   * @tutorial Verbs_VerbAnatomy
   * @tutorial Verbs_VerbProcess
   * @tutorial Verbs_ModifyVerbs
   * @tutorial Verbs_WriteVerbs
   * @classdesc
   * <pre class="display border outline">
   * <span class="ajs-player-input">&gt; turn in thief</span>
   * You turn in the thief. He immediately escapes.
   * Oh no, he stole your lantern!
   * </pre>
   * <p>
   * <strong>TurnOn</strong> requires that the
   * {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset} to be turned on has
   * asset.dov.turnIn.enabled set to true.
   * "Turn in" is a distinct verb from "turn".
   * Authors wanting to make use of turn in
   * 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.turnIn = {
    name: "turnIn",
    prettyname: "turn in",
    past_tense: "turned in",
    verb_noun_prep: ["turn in"], // turn thief in
    verb_prep_noun: ["turn in"], // turn in thief
    synonyms: [],
    gerund: "turning in",

    /**
     * @ajsadverbs
     * @memberof turnIn
     */
    accepts_adverbs: [],

    /**
     * @ajsverbstructures
     * @memberof turnIn
     */
    accepts_structures: [
      "verb", // turnIn means sleep
      "verb noun", // turnIn thief
      "verb noun preposition noun", // turnIn thief with ?
    ],

    /**
     * @memberof turnIn
     * @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 turnIn
     * @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,
     *   preposition_must_be: ["with"],
     * },
     */
    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,
      preposition_must_be: ["with"],
    },

    /**
     * @memberof turnIn
     * @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 direct_preposition = input.getPreposition(1);
      var indirect_preposition = input.getPreposition(2);
      var indirect_inferred;
      var adverb = input.getAdverb();
      var msg = "";
      var results;

      if (input.hasStructure("verb")) {
        if (input.verb_phrase === "turn in" && this.game.hasVerb("sleep")) {
          return this.game.dictionary.doVerb("sleep");
        } else {
          input.setSoftPrompt({
            index: 1,
            type: "noun",
            verb: input.verb_phrase,
            noun1: true,
            structure: "verb noun",
          });
          this.game.debug(`D1068 | ${this.name}.js | soft prompt for noun2 `);
          msg += `What would {we} like to ${input.verb_phrase || input.input_verb}? `;
          this.handleFailure(msg);
          return null;
        }
      }

      if (!direct_object.isDOV(this.name)) {
        this.game.debug(
          `D1157 | ${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.allowVerbOnce(this.name, "dov") &&
        direct_object.didVerb(this.name, "dov")
      ) {
        this.game.debug(
          `D1183 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.once and ${direct_object.id}.did.${this.name}.directly `
        );
        msg += `${direct_object.Articlename} has already been ${this.past_tense}. `;
        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(
            `D1411 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.with_nothing is false `
          );
          msg += `{We} {don't} know of a way to ${this.prettyname} ${direct_object.articlename}. `;
          this.handleFailure(msg);
          return null;
        }

        // infer indirect object?
        results = this.tryToInferIndirectObject({
          direct_object: direct_object,
          context: subject,
          handle_input: true,
        });
        if (results.prompt) {
          // indirect object required?
          if (direct_object.allowVerbWithNothing(this.name, "dov")) {
            return true;
          }

          this.game.debug(`D1414 | ${this.name}.js | soft prompt for noun2 `);
          msg += `What would {we} like to ${this.prettyname} ${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
      // ex: turn nut with wrench
      if (input.hasStructure("verb noun preposition noun")) {
        // 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.prettyname} ${direct_object.articlename} ${indirect_preposition} ${indirect_object.articlename}. `;
        //   this.handleFailure(msg);
        //   return null;
        // }

        // indirect object usable with direct object?
        if (
          !direct_object.allowVerbWithAsset({
            verb: this.name,
            asset: indirect_object,
            ov: "dov",
          })
        ) {
          this.game.debug(
            `D1416 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.with_assets/with_classes does not include ${indirect_object.id} `
          );
          msg += `{We} can't ${this.prettyname} ${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(
            `D1418 | ${this.name}.js | ${indirect_object.id}.iov.${this.name} is unset `
          );
          msg += `${indirect_object.Articlename} can't be used to ${this.prettyname} anything. `;
          this.handleFailure(msg);
          return null;
        }

        // single use indirect object?
        if (
          indirect_object.allowVerbOnce(this.name, "iov") &&
          indirect_object.iDidVerb(this.name, "iov")
        ) {
          this.game.debug(
            `D1424 | ${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.prettyname} something. `;
          this.handleFailure(msg);
          return null;
        }
      }

      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 adverb = input.getAdverb();
      var results;
      var msg = "";

      // apply state changes
      // this.setState(direct_object, true);

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

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