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="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 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 verb_phrase = input.verb_phrase;
      if (input.getAsset(1).id === "global_hole") {
        input.deletePhrase(1);
      }
      var direct_object = input.getAsset(1);
      var direct_substance = input.getSubstance(1);
      var direct_container;
      var direct_preposition = input.getPreposition(1);
      var indirect_object = input.getAsset(2);
      var indirect_preposition = input.getPreposition(2);
      var indirect_substance = input.getSubstance(2);
      var indirect_container;
      var indirect_inferred;
      var indirect_object2 = input.getAsset(3);
      var indirect_preposition2 = input.getPreposition(3);
      var player = this.game.getPlayer();
      var results;
      var msg = "";

      // dig hole with shovel in sand?
      // we prefer dig hole in sand with shovel
      if (indirect_preposition === "with" && indirect_preposition2 === "in") {
        input.swapPhrases(2, 3);
        indirect_object = input.getAsset(2);
        indirect_preposition = input.getPreposition(2);
        indirect_object2 = input.getAsset(3);
        indirect_preposition2 = input.getPreposition(3);
      }

      // dig with shovel in sand?
      // we prefer dig in sand with shovel
      if (direct_preposition === "with" && indirect_preposition === "in") {
        input.swapPhrases(1, 2);
        direct_object = input.getAsset(1);
        direct_preposition = input.getPreposition(1);
        indirect_object = input.getAsset(2);
        indirect_preposition = input.getPreposition(2);
      }

      if (direct_substance) {
        direct_container = direct_object;
        direct_object = direct_substance;
      }

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

      // direct object checks

      // verb enabled for direct object?
      if (!direct_object.isDOV(this.name)) {
        this.game.debug(
          `D1841 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.enabled is false `
        );
        msg += `$(We) can't ${this.name} ${
          direct_preposition ? direct_preposition : ""
        } ${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(
          `D2128 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.once and ${direct_object.id}.did.${this.name}.directly `
        );
        msg += `${direct_object.Articlename} has already been ${this.past_tense}. `;
        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;
        }

        // indirect objects available?
        if (!direct_object.hasIndirectObjects(this.name)) {
          this.game.debug(
            `D2130 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.with_nothing is false `
          );
          msg += `$(We) don't know of a way to ${this.name} ${direct_object.articlename}. `;
          this.handleFailure(msg);
          return false;
        }

        // infer indirect object?
        results = this.tryToInferIndirectObject({
          direct_object: direct_object,
          context: player,
          handle_input: true,
        });
        if (results.prompt) {
          this.game.debug(`D2131 | ${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

      // indirect object 2 checks
      if (indirect_object2) {
        // verb enabled for indirect object 2?
        if (!indirect_object2.isIOV(this.name)) {
          this.game.debug(
            `D2144 | ${this.name}.js | ${indirect_object2.id}.iov.${this.name}.enabled is false `
          );
          msg += `$(We) can't ${this.name} anything ${
            indirect_preposition2 ? indirect_preposition2 : "with"
          } ${indirect_object2.articlename}. `;
          this.handleFailure(msg);
          return null;
        }

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

        if (indirect_preposition2 !== "with") {
          this.game.debug(
            `D1647 | ${this.name}.js | ${indirect_object2.id}.iov.${this.name}.enabled is false `
          );
          msg += `$(We) don't know how to ${this.name} ${
            direct_object.articlename
          } ${indirect_preposition ? indirect_preposition : ""}  ${
            indirect_object ? indirect_object.articlename : ""
          } ${indirect_preposition2 ? indirect_preposition2 : "with"} ${
            indirect_object2.articlename
          }. `;
          this.handleFailure(msg);
          return null;
        }

        // single use indirect object 2?
        if (
          indirect_object2.allowVerbOnce(this.name, "iov") &&
          indirect_object2.iDidVerb(this.name, "iov")
        ) {
          this.game.debug(
            `D2146 | ${this.name}.js | ${indirect_object2.id}.iov.${this.name}.once and ${indirect_object2.id}.did.${this.name}.indirectly `
          );
          msg += `${indirect_object2.Articlename} has already been used to ${this.name} with. `;
          this.handleFailure(msg);
          return false;
        }
      } // indirect_object2

      // indirect object checks
      if (indirect_object) {
        if ("with" === indirect_preposition) {
          // is player holding asset?
          if (!this.game.parser.selectInHands(indirect_object.id).length) {
            this.game.debug(
              `D2132 | ${this.name}.js | ${indirect_object.id}.$is("inhands") is false `
            );
            msg += `$(We're) not holding ${indirect_object.articlename}. `;
            this.handleFailure(msg);
            return null;
          }
        }

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

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

        // single use indirect object?
        if (
          indirect_preposition === "with" &&
          indirect_object.allowVerbOnce(this.name, "iov") &&
          indirect_object.iDidVerb(this.name, "iov")
        ) {
          this.game.debug(
            `D2145 | ${this.name}.js | ${indirect_object.id}.iov.${this.name}.once and ${indirect_object.id}.did.${this.name}.indirectly `
          );
          msg += `${indirect_object.Articlename} has already been used to ${this.name} with. `;
          this.handleFailure(msg);
          return false;
        }

        // indirect object not required?
        // if( direct_object.allowVerbWithNothing(this.name, "dov") )
        // {
        //   this.game.debug(` | ${this.name}.js | ${direct_object.id}.dov.${this.name}.with_nothing `);
        //   msg += `$(We) can't ${this.name} ${direct_object.articlename} ${indirect_preposition} ${indirect_object.articlename}. `;
        //   this.handleFailure(msg);
        //   return null;
        // }

        // indirect object usable with direct object?
        // if (!direct_object.allowVerbWithAsset(this.name, indirect_object, "dov")) {
        //   this.game.debug(
        //     ` | ${this.name}.js | ${direct_object.id}.dov.${this.name}.with_assets/with_classes does not include ${indirect_object.id} `
        //   );
        //   msg += `$(We) can't ${this.name} ${direct_object.articlename} ${indirect_preposition} ${indirect_object.articlename}. `;
        //   this.handleFailure(msg);
        //   return null;
        // }

        // can indirect object be used?
        // if (!indirect_object.isIOV(this.name)) {
        //   this.game.debug(
        //     ` | ${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(
        //     ` | ${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 += `${indirect_object.Articlename} has already been used to ${this.name} something. `;
        //   this.handleFailure(msg);
        //   return null;
        // }
      } // indirect object

      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 direct_substance = input.getSubstance(1);
      var indirect_object = input.getAsset(2);
      var indirect_preposition = input.getPreposition(2);
      var indirect_substance = input.getSubstance(2);
      var indirect_object2 = input.getAsset(3);
      var indirect_preposition2 = input.getPreposition(3);
      var player = this.game.getPlayer();
      var results;
      var msg = "";

      // compose output
      msg += `$(We) try ${this.gerund}`;
      msg += `${direct_preposition ? " " + direct_preposition : ""}`;
      if (direct_substance) {
        msg += ` ${direct_substance.articlename} of `;
      }
      msg += `${direct_object ? " " + direct_object.articlename : ""}`;
      msg += `${indirect_preposition ? " " + indirect_preposition : ""}`;
      // if (indirect_substance) {
      //   msg += ` ${indirect_substance.articlename} of `;
      // }
      msg += `${
        indirect_object
          ? indirect_object.hasClass("Room")
            ? " the ground"
            : " " + indirect_object.articlename
          : ""
      }`;
      msg += `${indirect_preposition2 ? " " + indirect_preposition2 : ""}`;
      msg += `${
        indirect_object2
          ? indirect_object2.hasClass("Room")
            ? " the ground"
            : " " + indirect_object2.articlename
          : ""
      }`;
      msg += `. `;

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