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

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

  /**
   * @augments {adventurejs.Verb}
   * @class erase
   * @ajsnode game.dictionary.verbs.erase
   * @ajsconstruct MyGame.createVerb({ "name": "erase", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading CompositionVerbs
   * @summary Verb meaning erase asset.
   * @todo Everything. Copied this from write_on and haven't modified yet. Need erase_noun1_with_noun2.
   * @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; erase paper</span>
   * You erase the sheet of paper.
   * </pre>
   * <p>
   * <strong>Erase</strong> erases a
   * {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset}
   * of anything written on it.
   * Requires that the Asset to be erased has
   * asset.dov.erase.enabled
   * set to true and that subject is holding an
   * {@link adventurejs.Eraser|Eraser} Asset
   * (aka a Tangible Asset with its
   * asset.iov.erase.enabled set to true).
   * </p>
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   */
  A.Preverbs.erase = {
    name: "erase",
    prettyname: "erase",
    past_tense: "erased",
    synonyms: ["erase"],
    // verb_noun_prep_noun: ["erase with"],
    gerund: "erasing",

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

    /**
     * @memberof erase
     * @ajsverbphrase
     * phrase2:
     * {
     *   accepts_noun: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     in_inventory: true,
     *   },
     *   accepts_preposition: true,
     *   preposition_must_be: ["with"],
     * },
     */
    phrase2: {
      accepts_noun: true,
      noun_must_be: {
        known: true,
        in_inventory: true,
        // prefer_carried_if_ambiguous: true,
      },
      accepts_preposition: true,
      preposition_must_be: ["with"],
    },

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

    doTry: function () {
      var input = this.game.getInput();
      var subject = input.getSubject();
      var verb_phrase = input.verb_phrase;
      var direct_object = input.getAsset(1);
      var indirect_object = input.getAsset(2);
      var indirect_preposition = input.getPreposition(2);
      var indirect_inferred;
      var results;
      var msg = "";
      var allow = true;

      if (verb_phrase === "erase with") {
        //
      }

      if (!direct_object.isDOV("erase")) {
        this.game.debug(
          `D1516 | ${this.name}.js | ${direct_object.id}.dov.erase.enabled is false `
        );
        msg += `${direct_object.Articlename} can't be erased. `;
        this.handleFailure(msg);
        return false;
      }

      if (input.hasStructure("verb noun")) {
        results = this.tryToInferIndirectObject({
          direct_object: direct_object,
          context: subject,
          handle_input: true,
          infer_first_use: true,
        });
        if (results.prompt) {
          // no asset needed?
          if (direct_object.allowVerbWithNothing(this.name, "dov")) {
            return true;
          }

          this.game.debug(`D1755 | ${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

      if (input.hasStructure("verb noun preposition noun")) {
        // if (direct_object.allowVerbWithNothing(this.name, "dov")) {
        //   // it doesn't need a tool
        //   this.game.debug(
        //     ` | ${this.name}.js | ${direct_object.id}.dov.erase.with_nothing `
        //   );
        //   msg += `{We} can't ${this.name} ${direct_object.articlename} ${indirect_preposition} ${indirect_object.articlename}. `;
        //   this.handleFailure(msg);
        //   return false;
        // }

        if (!indirect_object.isIOV("erase")) {
          this.game.debug(
            `D1519 | ${this.name}.js | ${indirect_object.id}.iov.erase.enabled is false `
          );
          msg += `${indirect_object.Articlename} can't be used as an eraser. `;
          this.handleFailure(msg);
          return null;
        }

        if (
          !direct_object.allowVerbWithAsset({
            verb: this.name,
            asset: indirect_object,
            ov: "dov",
          })
        ) {
          this.game.debug(
            `D1361 | ${this.name}.js | neither ${direct_object.id} nor ${indirect_object.id} lists the other in dov/iov.${this.name}.with_assets or dov/iov.${this.name}.with_classes `
          );
          msg += `{We} can't ${this.name} ${direct_object.articlename} with ${indirect_object.articlename}. `;
          this.handleFailure(msg);
          return null;
        }
      } // verb noun preposition noun

      return true;
    },

    doSuccess: function () {
      var input = this.game.getInput();
      var subject = input.getSubject();
      var verb_phrase = input.verb_phrase;
      var direct_object = input.getAsset(1);
      var indirect_object = input.getAsset(2);
      var msg = "";
      var results;

      // apply state changes
      const oldcount = direct_object.written_strings.length;
      for (let i = direct_object.written_strings.length - 1; i > -1; i--) {
        if (direct_object.written_strings[i].erasable) {
          direct_object.written_strings.splice(i, 1);
        }
      }
      const newcount = direct_object.written_strings.length;

      // compose output
      msg += `{We} ${
        oldcount === 0 || newcount > 0 ? "try to" : ""
      } erase ${direct_object.articlename} with 
        ${
          indirect_object
            ? indirect_object.articlename
            : "the flat of {our} hand"
        }. `;

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