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

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

  /**
   * @augments {adventurejs.Verb}
   * @class knock
   * @ajsnode game.dictionary.verbs.knock
   * @ajsconstruct MyGame.createVerb({ "name": "knock", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading UtilityVerbs
   * @summary Summary.
   * @tutorial Scripting_VerbSubscriptions
   * @tutorial Verbs_VerbAnatomy
   * @tutorial Verbs_VerbProcess
   * @tutorial Verbs_ModifyVerbs
   * @tutorial Verbs_WriteVerbs
   * @classdesc Verbs start out as generic objects, which are converted
   * to verb class instances at runtime.
   * <br/><br/>
   * Verbs have access to the global Input object ( game.getInput() ),
   * which contains parsed player input.
   * @ajsverbreactions
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   */
  A.Preverbs.knock = {
    name: "knock",
    prettyname: "knock",
    past_tense: "knocked",
    synonyms: ["knock"],
    gerund: "knocking",

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

    /**
     * @memberof knock
     * @ajsverbphrase
     * phrase1:
     * {
     *   accepts_noun: true,
     *   requires_noun: true,
     *   noun_must_be: {
     *     known: true,
     *     tangible: true,
     *     present: true,
     *     reachable: true,
     *     visible: true,
     *   },
     *   accepts_preposition: true,
     *   accepts_these_prepositions: [ 'on', 'over' ],
     * },
     */

    phrase1: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        reachable: true,
        visible: true,
      },
      accepts_preposition: true,
      //accepts_these_prepositions: ["on", "over", "down", "off"],
    },

    /**
     * @memberof knock
     * @ajsverbphrase
     * phrase2:
     * {
     *   accepts_noun: true,
     *   requires_noun: true,
     *   noun_must_be: {
     *     known: true,
     *     tangible: true,
     *     present: true,
     *     reachable: true,
     *     visible: true,
     *   },
     *   accepts_preposition: true,
     *   accepts_these_prepositions: [ 'on', 'over' ],
     * },
     */

    phrase2: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        reachable: true,
        visible: true,
      },
      accepts_preposition: true,
      accepts_these_prepositions: ["with", "on", "against"],
    },

    let_verb_handle_disambiguation: false,

    doTry: 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_preposition = input.getPreposition(2);
      var player = this.game.getPlayer();
      var results;
      var msg = "";

      if (input.hasStructure("verb")) {
        return true;
      }

      // verb enabled?
      if (!direct_object.isDOV(this.name)) {
        this.game.debug(
          `D2087 | ${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(
          `D2088 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.once and ${direct_object.id}.did.${this.name}.directly `
        );
        msg += `$(We've) ${this.past_tense} ${
          direct_preposition ? direct_preposition : "on"
        } ${direct_object.articlename} enough. `;
        this.handleFailure(msg);
        return false;
      }

      if (input.hasStructure("verb noun")) {
        return true;
        // this.game.debug(` | ${this.name}.js | sentence structure 'verb'`);
        // msg += this.game.parser.getUnparsedMessage(input.getInput());
        // this.handleFailure(msg);
        // return false;
      }

      // sentence structure: verb preposition noun
      if (input.hasStructure("verb preposition noun")) {
        if (
          direct_object.allowVerbWithPreposition(
            this.name,
            direct_preposition,
            "dov"
          )
        ) {
          return true;
        } else if (direct_preposition === "off") {
          // is direct_object on something we can knock it off of?
        } else if (
          direct_preposition === "on" ||
          ((direct_preposition === "in" ||
            direct_preposition === "under" ||
            direct_preposition === "behind") &&
            direct_object.hasAspectAt(direct_preposition))
        ) {
          // ex: knock on door, knock under table - just do it
          return true;
        } else {
          //if (!direct_object.dov.knock.with_prepositions[direct_preposition]) {
          if (
            !direct_object.allowVerbWithPreposition(
              this.name,
              direct_preposition,
              "dov"
            )
          ) {
            this.game.debug(
              `D1948 | ${this.name}.js | ${this.name} ${direct_preposition} not handled `
            );
            msg += `$(We) don't know how to ${this.name} ${direct_preposition} ${direct_object.articlename}. `;
            this.handleFailure(msg);
            return null;
          }
        }
      } // verb preposition noun

      // sentence structure: verb noun preposition noun
      if (input.hasStructure("verb noun preposition noun")) {
        if (
          indirect_preposition === "on" ||
          indirect_preposition === "against"
        ) {
          // ex: knock cane on door
          // change to knock on door with cane
          input.swapPhrases(1, 2);
          input.setPreposition(1, "on");
          input.setPreposition(2, "with");
          input.setStructure("verb preposition noun preposition noun");
          direct_object = input.getAsset(1);
          direct_preposition = input.getPreposition(1);
          indirect_object = input.getAsset(2);
          indirect_preposition = input.getPreposition(2);
        }
      }

      // sentence structure: verb noun preposition noun
      if (
        input.hasStructure("verb noun preposition noun") ||
        input.hasStructure("verb preposition noun preposition noun")
      ) {
        if (indirect_preposition === "with" && !indirect_object.isIn(player)) {
          this.game.debug(
            `D2089 | ${this.name}.js | ${indirect_object.id} is not in player `
          );
          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;
        }

        // indirect object not required?
        if (direct_object.allowVerbWithNothing(this.name, "dov")) {
          this.game.debug(
            `D2093 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.with_nothing `
          );
          msg += `$(We) don't need to use ${indirect_object.articlename} to ${
            this.name
          } ${direct_preposition ? direct_preposition : ""} ${
            direct_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(
            `D2090 | ${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_preposition ? direct_preposition : ""
          } ${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(
            `D2091 | ${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(
            `D2092 | ${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;
        }
      }

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

      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_preposition = input.getPreposition(2);
      var player = this.game.getPlayer();
      var results;
      var msg = "";

      // apply state changes
      //this.setState(direct_object,false);

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

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