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

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

  /**
   * @augments {adventurejs.Verb}
   * @class dig
   * @ajsnode game.dictionary.verbs.dig
   * @ajsconstruct MyGame.createVerb({ "name": "dig", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading DestructionVerbs
   * @summary Verb meaning dig asset.
   * @ajssynonyms dig, attack
   * @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; dig hole in ground</span>
   * You dig a hole in the ground. Unfortunately, the supermarket
   * staff are less than pleased about this.
   * </pre>
   * <p>
   * <strong>Dig</strong> a
   * {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset}.
   * Requires that the Asset to be dig has
   * asset.dov.dig.enabled
   * set to true.  By default, no special results will occur.
   * Authors wanting to make use of it may need to use a method such
   * as verb hooks. See
   * <a href="/doc/Scripting_VerbPhases.html">Verb Phases</a>
   * to learn more.
   * </p>
   * @ajsverbreactions
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   * </div>
   */
  A.Preverbs.dig = {
    name: "dig",
    prettyname: "dig",
    past_tense: "dug",
    synonyms: ["dig"],
    gerund: "digging",

    /**
     * @ajsverbstructures
     * @memberof dig
     */
    accepts_structures: [
      "verb", // dig
      "verb noun", // dig hole
      "verb preposition noun", // dig in sand, dig up chest
      "verb noun preposition noun", // dig hole with shovel
      "verb preposition noun preposition noun", // dig in sand with shovel
      "verb noun preposition noun preposition noun", // dig hole in sand with shovel
    ],

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

    /**
     * @memberof dig
     * @ajsverbphrase
     * phrase2:
     * {
     *   accepts_noun: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     tangible: true,
     *     present: true,
     *     visible: true,
     *     reachable: true,
     *   },
     *   accepts_preposition: true,
     *   requires_preposition: true,
     * },
     */
    phrase2: {
      accepts_noun: true,
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        visible: true,
        reachable: true,
      },
      accepts_preposition: true,
      requires_preposition: true,
    },

    /**
     * @memberof dig
     * @ajsverbphrase
     * phrase3:
     * {
     *   accepts_noun: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     tangible: true,
     *     present: true,
     *     visible: true,
     *     reachable: true,
     *   },
     *   accepts_preposition: true,
     *   requires_preposition: true,
     * },
     */
    phrase3: {
      accepts_noun: true,
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        visible: true,
        reachable: true,
      },
      accepts_preposition: true,
      requires_preposition: true,
    },

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

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

      let hole_asset,
        hole_preposition,
        hole_position,
        target_asset,
        target_preposition,
        target_aspect,
        target_substance,
        target_container,
        target_is_hole,
        tool_asset,
        tool_preposition;

      for (let i = input.getPhraseCount(); i > 0; i--) {
        const phrase = input.getPhrase(i);
        const asset = input.getAsset(i);
        const preposition = input.getPreposition(i);
        const substance = input.getSubstance(i);
        const hole = phrase.noun === "hole";

        if (asset.id === "global_hole") {
          // hole
          hole_position = i;
          hole_asset = input.verb_params.hole_asset = asset;
          hole_preposition = input.verb_params.hole_preposition = preposition;
          continue;
        } else if (hole) {
          target_asset = input.verb_params.target_asset = asset;
          target_is_hole = input.verb_params.target_is_hole = true;
          target_preposition = input.verb_params.target_preposition =
            preposition;
          continue;
        }

        if (preposition === "with") {
          // tool
          tool_asset = input.verb_params.tool_asset = asset;
          tool_preposition = input.verb_params.tool_preposition = preposition;
          continue;
        }

        if (["in", "under", "behind"].includes(preposition)) {
          // target
          target_asset = input.verb_params.target_asset = asset;
          target_preposition = input.verb_params.target_preposition =
            preposition;
          target_substance = input.verb_params.target_substance = substance;
          // target_aspect = target_asset.get
          continue;
        }

        // if (["up", "out"].includes(preposition)) {
        //   // ??
        //   // target_asset = asset;
        //   // target_preposition = preposition;
        //   continue;
        // }

        // if we get here, we don't know how to handle it
        this.game.debug(
          `D1190 | ${this.name}.js | ${this.name} doesn't handle ${input.input}`
        );
        msg += `{We} don't know how to ${input.input}. `;
        this.handleFailure(msg);
        return null;
      }

      // if player input "dig hole in thing" we captured global_hole
      // as direct_object but it's not actually an object
      // we delete it so that doSuccess works on the right assets
      if (hole_position) input.deletePhrase(hole_position);

      // did player input "dig hole" with no target? use player location
      if (!target_asset) {
        // is there a reservoir?
        let reservoirs = this.game
          .getRoom()
          .findNestedAssetsWithProperty("aspects.in.vessel.is.reservoir");
        if (reservoirs.length) {
          for (let item in reservoirs) {
            let reservoir = reservoirs[item];
            if (reservoir.aspects.in.vessel.substance.hasClass("Solid")) {
              // found a target for dig
              target_asset = input.verb_params.target_asset = reservoir;
              target_preposition = input.verb_params.target_preposition = "in";
              target_aspect = input.verb_params.target_aspect =
                reservoir.aspects.in;
              target_substance = input.verb_params.target_substance =
                target_aspect.vessel.substance;
            }
          }
        }
        // if no reservoir was found, dig in room
        if (!target_asset) {
          target_asset = input.verb_params.target_asset =
            player.getNestOrPlaceAsset();
          target_preposition = input.verb_params.target_preposition =
            player.getNestOrPlacePreposition();
          target_aspect = input.verb_params.target_aspect =
            player.getNestOrPlaceAspect();
          if (target_aspect.vessel?.substance) {
            target_substance = input.verb_params.target_substance =
              target_aspect.vessel.substance;
          }
        }
        input.pushPhrase({
          asset: target_asset,
          preposition: target_preposition,
        });
      }

      // if player input "dig with shovel in sand"
      // we prefer "dig in sand with shovel"
      if (
        tool_asset?.id === input.getAsset(1).id &&
        target_asset?.id === input.getAsset(2).id
      ) {
        input.swapPhrases(1, 2);
      }

      // verb enabled for target object?
      if (!target_asset.isDOV(this.name)) {
        this.game.debug(
          `D1192 | ${this.name}.js | ${target_asset.id}.dov.${this.name}.enabled is false `
        );
        msg += `{We} can't ${this.name} ${
          target_preposition ? target_preposition : ""
        } ${target_asset.articlename}. `;
        this.handleFailure(msg);
        return null;
      }

      // single use target object?
      if (
        target_asset &&
        target_asset.allowVerbOnce(this.name, "dov") &&
        target_asset.didVerb(this.name, "dov")
      ) {
        this.game.debug(
          `D1196 | ${this.name}.js | ${target_asset.id}.dov.${this.name}.once and ${target_asset.id}.did.${this.name}.directly `
        );
        msg += `${target_asset.Articlename} has already been ${this.past_tense}. `;
        this.handleFailure(msg);
        return false;
      }

      // can verb act on target without an indirect object?
      if (
        target_asset &&
        !tool_asset /* &&
        !target_asset.allowVerbWithNothing(this.name, "dov") */
      ) {
        // indirect objects available?
        if (
          !target_asset.hasIndirectObjects(this.name) &&
          !target_asset.allowVerbWithNothing(this.name, "dov")
        ) {
          this.game.debug(
            `D1337 | ${this.name}.js | ${target_asset.id}.dov.${this.name}.with_nothing is false `
          );
          msg += `{We} {don't} know of a way to ${this.name} ${target_asset.articlename}. `;
          this.handleFailure(msg);
          return false;
        }

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

          this.game.debug(`D1206 | ${this.name}.js | no tool found `);
          msg += `{We} don't appear to have anything to ${this.name} ${target_preposition ? target_preposition : ""} ${target_asset.articlename} with. `;
          this.handleFailure(msg);
          return false;
        } else if (results.success) {
          tool_asset = input.verb_params.tool_asset = results.indirect_object;
          tool_preposition = input.verb_params.tool_preposition = "with";
          // tool_inferred = true;
          // input.setAsset(2, tool_asset);
          // input.setPreposition(2, tool_preposition);
          // input.setStructure("verb noun preposition noun");
          this.game.printInferred(
            `${tool_preposition} ${tool_asset.articlename}`
          );
        }
      } // verb noun aka target no tool

      // verb enabled for tool asset?
      if (tool_asset && !tool_asset.isIOV(this.name)) {
        this.game.debug(
          `D1319 | ${this.name}.js | ${tool_asset.id}.iov.${this.name}.enabled is false `
        );
        msg += `{We} can't ${this.name} anything ${
          tool_preposition ? tool_preposition : "with"
        } ${tool_asset.articlename}. `;
        this.handleFailure(msg);
        return null;
      }

      // is subject holding asset?
      if (tool_asset && !this.game.parser.selectInHands(tool_asset.id).length) {
        this.game.debug(
          `D1335 | ${this.name}.js | ${tool_asset.id}.$is("inhands") is false `
        );
        msg += `{We're} not holding ${tool_asset.articlename}. `;
        this.handleFailure(msg);
        return null;
      }

      // single use tool asset?
      if (
        tool_asset &&
        tool_asset.allowVerbOnce(this.name, "iov") &&
        tool_asset.didVerb(this.name, "dov")
      ) {
        this.game.debug(
          `D1193 | ${this.name}.js | ${tool_asset.id}.dov.${this.name}.once and ${tool_asset.id}.did.${this.name}.directly `
        );
        msg += `${tool_asset.Articlename} has already been used to ${this.name}. `;
        this.handleFailure(msg);
        return false;
      }

      // works with any indirect object?
      if (tool_asset && target_asset?.allowVerbWithAnything(this.name, "dov")) {
        return true;
      }

      return true;
    },

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

      let {
        hole_asset,
        hole_preposition,
        target_asset,
        target_preposition,
        target_aspect,
        target_substance,
        target_container,
        target_is_hole,
        tool_asset,
        tool_preposition,
      } = input.verb_params;

      // compose output
      msg += `{We} ${this.name}`;
      if (target_preposition) msg += ` ${target_preposition}`;
      if (target_substance && target_asset && target_asset.hasClass("Room")) {
        msg += ` ${target_substance.articlename} `;
      }
      if (target_substance && target_asset && !target_asset.hasClass("Room")) {
        msg += ` ${target_substance.articlename} of ${target_asset.articlename} `;
      }
      if (!target_substance && target_asset) {
        msg += target_asset.hasClass("Room")
          ? " the ground"
          : " " + target_asset.articlename;
      }
      if (tool_preposition) msg += ` ${tool_preposition}`;
      if (tool_asset) msg += ` ${tool_asset}`;
      msg = msg.trim();
      msg += `. `;

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