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

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

  /**
   * @augments {adventurejs.Verb}
   * @class draw
   * @ajsnode game.dictionary.verbs.draw
   * @ajsconstruct MyGame.createVerb({ "name": "draw", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading CompositionVerbs
   * @summary Verb that means draw, as in "draw on blackboard".
   * @tutorial Verbs_Subscriptions
   * @tutorial AdvancedVerbs_VerbAnatomy
   * @tutorial AdvancedVerbs_VerbProcess
   * @tutorial AdvancedVerbs_ModifyVerbs
   * @tutorial AdvancedVerbs_ModifyVerbs
   * @ajsdemo WritingDemo, Office, Playroom, Classroom, Library, Scorecard
   * @ajscss Styles
   * @classdesc
   * <pre class="display border outline">
   * <span class="ajs-player-input">&gt; draw on sidewalk with chalk</span>
   * You draw a tiny house with a rainbow over it.
   * </pre>
   * <p>
   * <strong>Draw</strong> on a {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset}.
   * Requires that the drawing Asset has
   * <code>asset.iov.draw.enabled</code>
   * set to true and that the target has
   * <code>asset.dov.draw.enabled</code>
   * set to true.
   * <strong>Draw</strong> saves a record
   * of things drawn on an Asset to its
   * <a class="code" href="/doc/adventurejs.Tangible.html#property_drawn_things">drawn_things</a>
   * property, which can be appended to the Asset's description.
   * </p>
   * @ajsverbreactions
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   */
  A.Preverbs.draw = {
    name: "draw",
    prettyname: "draw on",
    past_tense: "drew",
    synonyms: ["draw"],
    gerund: "drawing",

    allow_iov_on_iov: true,

    // draw on x
    //verb_prep_noun: [ "draw on" ],

    // draw on x with y
    //verb_prep_noun_prep_noun: [ "draw on with" ],

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

    /**
     * @memberof draw
     * @ajsverbphrase
     * phrase1:
     * {
     *   accepts_noun: true,
     *   requires_noun: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     present: true,
     *     visible: true,
     *     reachable: true,
     *   },
     *   accepts_preposition: true,
     *   preposition_must_be: ["on", "with"],
     * },
     */
    phrase1: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        known: true,
        // present: true,
        // visible: true,
        // reachable: true,
      },
      accepts_preposition: true,
      preposition_must_be: ["on", "with"],
    },

    /**
     * @memberof draw
     * @ajsverbphrase
     * phrase2:
     * {
     *   accepts_noun: true,
     *   requires_noun: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     present: true,
     *     visible: true,
     *     reachable: true,
     *     prefer_carried_if_ambiguous: true,
     *   },
     *   accepts_preposition: true,
     *   requires_preposition: true,
     *   preposition_must_be: ["on", "with"],
     * },
     */
    phrase2: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        known: true,
        present: true,
        visible: true,
        reachable: true,
        prefer_carried_if_ambiguous: true,
      },
      accepts_preposition: true,
      requires_preposition: true,
      preposition_must_be: ["in", "on", "with"],
    },

    /**
     * @memberof draw
     * @ajsverbphrase
     * phrase3:
     * {
     *   accepts_noun: true,
     *   requires_noun: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     present: true,
     *     visible: true,
     *     reachable: true,
     *     prefer_carried_if_ambiguous: true,
     *   },
     *   accepts_preposition: true,
     *   requires_preposition: true,
     *   preposition_must_be: ["on", "with"],
     * },
     */
    phrase3: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        known: true,
        present: true,
        visible: true,
        reachable: true,
        prefer_carried_if_ambiguous: true,
      },
      accepts_preposition: true,
      requires_preposition: true,
      preposition_must_be: ["in", "on", "with"],
    },

    /**
     * Draw breaks our usual direct / indirect pattern
     * because technically, the thing being drawn is
     * the direct object, even if no thing is specified.
     * So for example "draw foo on paper" treats foo as the
     * direct object, and the paper as the indirect object.
     * Whereas "draw on paper", even though no information
     * object has been provided, will infer one, leading
     * to the same result.
     *
     * "draw foo on paper with pencil" treats both the paper
     * and the pencil as indirect objects. So, though it may
     * seem that "draw on paper" should treat paper as the
     * direct object, in fact it does not.
     *
     * Usually, we can use isDOV() and isIOV() to determine which
     * asset acts on another - as with lock and key - but here,
     * in order to distinguish direct from indirect object, we add
     * a custom verb param to make the relationship explicit.
     * @memberof draw
     * @ajsverbparams
     * with_params: {
     *   // tool is a drawing implement
     *   tool: false,
     *   // target is a drawing surface
     *   target: false,
     * },
     */
    with_params: {
      // tool is a drawing implement
      tool: false,

      // target is a drawing surface
      target: false,
    },

    doTry: function () {
      var input = this.game.getInput();
      var subject = input.getSubject();
      var msg = "";

      var subject_asset,
        tool_asset,
        tool_preposition,
        tool_asset_inferred,
        target_asset,
        target_preposition;
      var results;

      /**
       * Draw doesn't follow our usual direct / indirect pattern.
       * Player can input "draw 'foo'" or "draw 'foo' on board"
       * or "draw 'foo' on board with chalk"
       * or "draw with chalk" or "draw on board"
       * We need to do some extra work to figure out what's what
       * and save the results in the verb_params object for doSuccess to use.
       * All we're saving is the phrase number, not the actual phrase.
       */

      // @TODO
      // if(input.hasStructure("noun preposition") && input.getAsset(1).isDOV(this.name)) {
      //   // we don't accept "draw on subject"
      // }

      let count = input.getPhraseCount();

      for (let i = count; i > 0; i--) {
        const asset = input.getAsset(i);
        const preposition = input.getPreposition(i);

        // is asset not iov ?
        console.warn(
          `${asset.id}.isDOV(draw) && !${asset.id}.isIOV(draw) `,
          asset?.isDOV(this.name) && !asset.isIOV(this.name)
        );
        if (asset?.isDOV(this.name) && !asset.isIOV(this.name)) {
          if (subject_asset) {
            this.game.debug(
              `D1527 | ${this.name}.js | can't handle multiple drawn items`
            );
            msg += `{We} don't know how to ${input.input}. `;
            this.handleFailure(msg);
            return null;
          }
          // we've done minimal noun/preposition gates in phrases
          // to allow for input flexibility, but information asset
          // shouldn't have a preposition, so check that now
          if (preposition) {
            this.game.debug(
              `D1528 | ${this.name}.js | no handling for "${preposition} ${asset.id}" `
            );
            msg += `{We} don't know how to ${this.name} ${preposition} ${asset.name}. `;
            this.handleFailure(msg);
            return false;
          }

          subject_asset = input.verb_params.subject_asset = asset;
          continue;
        }

        // is asset a tool?
        // if (preposition === "with") {
        if (asset?.isIOV(this.name) && asset.iov[this.name].with_params.tool) {
          if (tool_asset) {
            this.game.debug(
              `D1535 | ${this.name}.js | can't handle multiple tool assets`
            );
            msg += `{We} don't know how to ${input.input}. `;
            this.handleFailure(msg);
            return null;
          }
          if (preposition !== "with") {
            this.game.debug(
              `D1602 | ${this.name}.js | can't handle preposition`
            );
            msg += `{We} don't know how to ${this.name} ${preposition} ${asset.articlename}. `;
            this.handleFailure(msg);
            return null;
          }
          // it's our drawing implement
          tool_asset = input.verb_params.tool_asset = asset;
          tool_preposition = input.verb_params.tool_preposition = preposition;
          continue;
        }

        // is asset a target?
        // else if (preposition === "on" || preposition === "in") {
        if (
          asset?.isIOV(this.name) &&
          asset.iov[this.name].with_params.target
        ) {
          if (target_asset) {
            this.game.debug(
              `D1620 | ${this.name}.js | can't handle multiple target assets`
            );
            msg += `{We} don't know how to ${input.input}. `;
            this.handleFailure(msg);
            return null;
          }
          if (!["in", "on"].includes(preposition)) {
            this.game.debug(
              `D1621 | ${this.name}.js | can't handle preposition`
            );
            msg += `{We} don't know how to ${this.name} ${preposition} ${asset.articlename}. `;
            this.handleFailure(msg);
            return null;
          }
          // it's our drawing surface
          // draw on paper is different from draw in book
          // maybe need to add a "quirks.draw_on_means_draw_in" property?
          target_asset = input.verb_params.target_asset = asset;
          target_preposition = input.verb_params.target_preposition =
            preposition;
          continue;
        }

        // is asset none of these things?
        this.game.debug(
          `D1622 | ${this.name}.js | unusable asset ${asset.id} found`
        );
        msg += `{We} don't know how to ${this.name} ${preposition ? preposition : ""} ${asset ? asset.articlename : ""}. `;
        this.handleFailure(msg);
        return null;
      }

      // if we didn't receive one of these three things
      // then we got nothin' to work with
      if (!subject_asset && !target_asset && !tool_asset) {
        this.game.debug(`D1623 | ${this.name}.js | no relevant assets found`);
        msg += `{We} don't know how to ${input.input}. `;
        this.handleFailure(msg);
        return null;
      }

      // if no subject_asset we need to make one for consistency with dov/iov draw
      if (!subject_asset) {
        subject_asset = input.verb_params.subject_asset =
          this.game.getAsset("global_drawing");
        input.shiftPhrase({ asset: subject_asset });
      }

      if (!target_asset) {
        var index = input.getPhraseCount() + 1;
        input.setPreposition(index, "on");
        input.setSoftPrompt({
          index: index,
          type: "noun",
          [`noun${index}`]: true,
          // [`preposition${index}`]: "on",
          // verb: 'draw'
        });
        this.game.debug(
          `D1624 | ${this.name}.js | soft prompt for target asset `
        );
        msg += `What would {we} like to draw on? `;
        this.handleFailure(msg);
        return null;
      }

      // is subject holding the drawing implement?
      // in most verbs would be handled by NounMustBe,
      // but draw's noun_must_be is a little more complicated
      // because the order of words can vary
      if (
        tool_asset &&
        !this.game.parser.selectInHands([tool_asset.id]).length
      ) {
        this.game.debug(
          `D1624 | ${this.name}.js | ${tool_asset.id}.place is not ${subject.id} `
        );
        msg += `{We're} not holding ${tool_asset.articlename}. `;
        this.handleFailure(msg);
        return null;
      }

      // can drawing object draw on surface object?
      if (
        tool_asset &&
        !tool_asset.allowVerbWithAsset({
          verb: this.name,
          asset: target_asset,
          ov: "iov",
        }) &&
        !tool_asset.allowVerbWithAnything(this.name, "iov")
      ) {
        this.game.debug(
          `D1625 | ${this.name}.js | neither ${tool_asset.id} nor ${target_asset.id} lists the other in dov/iov.${this.name}.with_assets or dov/iov.${this.name}.with_classes `
        );
        msg += `${target_asset.Articlename} doesn't present a good drawing surface for ${tool_asset.articlename}. `;
        this.handleFailure(msg);
        return null;
      }

      // can we infer a drawing object?
      if (!tool_asset && this.game.settings.infer_objects) {
        results = this.tryToInferIndirectObject({
          direct_object: target_asset,
          context: subject,
          handle_input: false,
          infer_first_use: true,
        });
        if (results.fail) {
          /* do nothing */
        } else if (results.prompt) {
          /* do nothing here, we'll handle later */
        } else if (results.success) {
          // tool_asset = results.indirect_object;
          tool_asset = input.verb_params.tool_asset = results.indirect_object;
          tool_preposition = input.verb_params.tool_preposition = "with";
          var index = input.pushPhrase({
            asset: tool_asset,
            preposition: "with",
          });
          tool_asset_inferred = true;
          this.game.printInferred(`with ${tool_asset.articlename}`);
        }
      }

      // no drawing object after all
      if (!tool_asset) {
        var index = input.hasPhrase(2) ? 3 : 2;
        // we're inferring an indirect object so restructure sentence for next turn
        input.setSoftPrompt({
          index: index,
          type: "noun",
          ["noun" + index]: true,
          structure: input.getStructure() + " preposition noun",
        });
        input.setPreposition(index, "with");
        this.game.debug(`D1627 | ${this.name}.js | soft prompt for noun2 `);
        msg += `What would {we} like to draw with? `;
        this.handleFailure(msg);
        return false;
      }

      // reorder input if needed
      // (1) subject_asset (2) tool_asset (3) target_asset

      let phrase_count = input.getPhraseCount();

      let asset_in_info_slot = input.getAsset(1);
      if (
        subject_asset &&
        asset_in_info_slot &&
        subject_asset.id !== asset_in_info_slot.id
      ) {
        // move subject_asset to slot 1
        let info_found_in_slot;
        for (let i = 1; i <= phrase_count; i++) {
          if (input.getAsset(i).id === subject_asset.id) {
            info_found_in_slot = i;
            break;
          }
        }
        input.swapPhrases(info_found_in_slot, 1);
      }

      let asset_in_target_slot = input.getAsset(2);
      if (
        target_asset &&
        asset_in_target_slot &&
        target_asset.id !== asset_in_target_slot.id
      ) {
        // move target_asset to slot 2
        let target_found_in_slot;
        for (let i = 1; i <= phrase_count; i++) {
          if (input.getAsset(i).id === target_asset.id) {
            target_found_in_slot = i;
            break;
          }
        }
        if (target_found_in_slot) input.swapPhrases(target_found_in_slot, 2);
      }

      let asset_in_tool_slot = input.getAsset(phrase_count);
      if (
        tool_asset &&
        asset_in_tool_slot &&
        tool_asset.id !== asset_in_tool_slot.id
      ) {
        // move tool_asset to last slot [phraseCount]
        let tool_found_in_slot;
        for (let i = 1; i <= phrase_count; i++) {
          let asset = input.getAsset(i);
          if (asset && asset.id === tool_asset.id) {
            tool_found_in_slot = i;
            break;
          }
        }
        if (tool_found_in_slot)
          input.swapPhrases(tool_found_in_slot, phrase_count);
      }

      return true;
    },

    doSuccess: function () {
      var input = this.game.getInput();
      var results;
      var msg = "";

      let {
        subject_asset,
        info_preposition,
        target_asset,
        target_preposition,
        tool_asset,
        tool_preposition,
      } = input.verb_params;

      // if (
      //   input.verb_params.tool_phrase &&
      //   input.getInferred(input.verb_params.tool_phrase)
      // ) {
      //   msg += `<span class='ajs-inferred'>(with ${tool_asset.articlename})</span>`;
      // }

      msg += "{We} draw ";

      if (target_asset) {
        if (subject_asset) {
          let data = subject_asset.indefinite_name;
          data.replace(/^"(.*)"$/, "$1");
          msg += `<span class="drawing ${tool_asset.class.toLowerCase()}">${data}</span> `;
          target_asset.drawn_things.push({
            class: tool_asset.class,
            erasable: tool_asset.is.erasable,
            data,
            color: tool_asset.appearance.color || "",
          });
        } else {
          msg += " a doodle ";
          target_asset.appearance.scribbles = true;
        }
        msg += target_asset.hasQuirk("draw_on_means_draw_in") ? "in " : "on ";
        msg += target_asset.articlename;
      } else {
        msg += " a doodle in the air";
      }

      if (tool_asset) {
        msg += " with " + tool_asset.articlename;
      }

      msg += ". ";

      // --------------------------------------------------
      // print output
      // --------------------------------------------------
      return this.handleSuccess(msg, target_asset);
    },
  };
})(); // draw