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

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

  /**
   * @augments {adventurejs.Verb}
   * @class dip
   * @ajsnode game.dictionary.verbs.dip
   * @ajsconstruct MyGame.createVerb({ "name": "dip", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading ManipulationVerbs
   * @summary Verb meaning dip one asset into another.
   * @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; dip doughnut in coffee</span>
   * You dip the doughnut in the cup of coffee.
   * </pre>
   * <p>
   * <strong>Dip</strong> one {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset} in another.
   * Requires that the Asset to be dipped has
   * <code>asset.dov.dip.enabled</code>
   * set to true, and that the Asset to be dipped in has
   * <code>asset.iov.dip.enabled</code>
   * set to true.
   * </p>
   * @ajsverbreactions
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   */
  A.Preverbs.dip = {
    name: "dip" /* @todo handle 'in' via prep */,
    prettyname: "dip",
    past_tense: "dipped",
    synonyms: ["dip", "dunk"],
    // verb_noun_prep_noun: ["dip in", "dunk in"],
    gerund: "dipping",

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

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

    /**
     * @memberof dip
     * @ajsverbphrase
     * phrase2:
     * {
     *   accepts_noun: true,
     *   requires_noun: true,
     *   noun_must_be:
     *   {
     *     matter: true,
     *     present: true,
     *     reachable: true,
     *     visible: true,
     *   },
     *   accepts_preposition: true,
     *   requires_preposition: true,
     *   accepts_these_prepositions: [ 'in' ],
     * },
     */
    phrase2: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        matter: true,
        present: true,
        reachable: true,
        visible: true,
      },
      accepts_preposition: true,
      requires_preposition: true,
      accepts_these_prepositions: ["in"],
    },

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

    doTry: function () {
      var input = this.game.getInput();
      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_substance = input.getSubstance(2);
      var indirect_container;
      var msg = "";
      var containers, container;
      var aspect;

      if (verb_phrase === "dip in" || verb_phrase === "dunk in") {
        //
      }

      if (indirect_substance) {
        indirect_container = indirect_object;
        indirect_object = indirect_substance;
      }

      if (!direct_object.isDOV(this.name)) {
        this.game.debug(
          `D1840 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.enabled is false `
        );
        msg += `$(We) can't ${this.name} ${direct_object.articlename}. `;
        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(
          `D2123 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.once and ${direct_object.id}.did.${this.name}.directly `
        );
        msg += `$(We) can't ${this.name} ${direct_object.articlename} again. `;
        this.handleFailure(msg);
        return false;
      }

      // sentence structure: verb noun
      if (input.hasStructure("verb noun")) {
        // can verb act without an indirect object?
        if (direct_object.allowVerbWithNothing(this.name, "dov")) {
          return true;
        }
      }

      // sentence structure: verb noun preposition noun
      if (input.hasStructure("verb noun preposition noun")) {
        // can indirect object be used?
        if (!indirect_object.isIOV(this.name)) {
          this.game.debug(
            `D2124 | ${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(
            `D2125 | ${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 += `$(We've) already ${this.past_tense} into ${indirect_object.articlename} enough. `;
          this.handleFailure(msg);
          return null;
        }
      }

      // sentence structure: verb noun preposition noun
      if (
        input.hasStructure("verb noun preposition noun") &&
        indirect_object.hasClass("Tangible")
      ) {
        // check locations for substances
        // aspect probably always going to be "in"
        // but we've made it possible to store substances
        // in any aspect, so we gotta check
        aspect = indirect_object.getVesselPreposition();

        if (aspect == "in" && indirect_object.is.closed) {
          this.game.debug(
            `D1842 | ${this.name}.js | ${indirect_object.id}.is.closed`
          );
          msg += `${indirect_object.Articlename_is} closed. `;
          this.handleFailure(msg);
          return null;
        }
      }

      // sentence structure: verb noun preposition noun
      if (
        input.hasStructure("verb noun preposition noun") &&
        indirect_object.hasClass("Substance")
      ) {
      }

      return true;
    },

    doSuccess: function () {
      var input = this.game.getInput();
      var verb_phrase = input.verb_phrase;
      var direct_object = input.getAsset(1);
      var direct_preposition = input.getPreposition(1);
      var indirect_object = input.getAsset(2);
      var indirect_container; // = input.getContainerAsset(2) || indirect_object;
      var indirect_preposition = input.getPreposition(2);
      var indirect_substance = input.getSubstance(2);
      var indirect_vessel;
      var msg = "";
      var dampened = false;
      var results;

      if (!indirect_substance && indirect_object.containsAnySubstance()) {
        indirect_substance = indirect_object.getVessel().getSubstance();
      }

      if (indirect_substance) {
        indirect_container = indirect_object;
        indirect_object = indirect_substance;
      }

      if (indirect_container) {
        indirect_vessel = indirect_container.getVesselAt(
          indirect_container.getVesselPreposition()
        );
      }

      // apply state changes
      if (
        indirect_vessel?.substance_id &&
        indirect_vessel.getSubstance().hasClass("Liquid") &&
        direct_object.is.absorbent &&
        direct_object.absorption_quantity &&
        !direct_object.is.damp
      ) {
        let newvol =
          indirect_vessel.getVolume() - direct_object.absorption_quantity;
        indirect_vessel.setVolume(newvol);
        direct_object.is.damp = true;
        dampened = true;
        results = direct_object.onMoistenThisWithThat(
          indirect_vessel.getSubstance()
        );
        if ("undefined" !== typeof results) return results;
      }

      // compose output
      msg +=
        `$(We) ${this.name}` +
        `${direct_preposition ? " " + direct_preposition : ""}` +
        `${direct_object ? " " + direct_object.articlename : ""}` +
        `${indirect_preposition ? " " + indirect_preposition : ""}` +
        `${indirect_container ? " " + indirect_container.articlename : ""}` +
        `. ` +
        `${dampened ? direct_object.Articlename + " comes away damp. " : ""}`;

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