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

(function () {
  /*global adventurejs A*/
  "use strict";

  /**
   * @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: [],
    verb_noun_prep_noun: ["dip in", "dunk in"],

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

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

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

    doTry: function () {
      var input = this.game.getInput();
      var direct_object = input.getAsset(1);
      var indirect_object = input.getAsset(2);
      var msg = "";

      // 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
      var aspect = indirect_object.getAspectWithVessel();

      if (
        !aspect ||
        !direct_object.isDOV("dip") ||
        !indirect_object.isIOV("dip")
      ) {
        this.game.debug(
          `F1840 | ${this.name}.js | ${direct_object.id}.dov.dip.enabled is false or ${indirect_object.id}.iov.dip.enabled is false or doesn't have aspects.${aspect} aspect`,
        );
        msg += `$(We) can't dip ${direct_object.articlename} in ${indirect_object.articlename}. `;
        this.handleFailure(msg);
        return null;
      }

      if (0 === indirect_object.aspects[aspect].vessel.getVolume()) {
        this.game.debug(
          `F1841 | ${this.name}.js | ${indirect_object.id}.${aspect}.vessel.getVolume is 0`,
        );
        msg += `${indirect_object.Articlename} is empty. `;
        this.handleFailure(msg);
        return null;
      }

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

      return true;
    },

    doSuccess: function () {
      var input = this.game.getInput();
      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 msg = "";
      var aspect = indirect_object.getAspectWithVessel();

      this.game.debug(`F1843 | ${this.name}.js | print doSuccess `);

      // apply state changes
      if (direct_object.is.absorbent && !direct_object.is.damp) {
        var newvol =
          indirect_object.aspects[aspect].vessel.getVolume() -
          direct_object.absorption_quantity;
        indirect_object.aspects[aspect].vessel.setVolume(newvol);
        direct_object.is.damp = true;
      }

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

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