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

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

  /**
   * @augments {adventurejs.Verb}
   * @class flick
   * @ajsnode game.dictionary.verbs.flick
   * @ajsconstruct MyGame.createVerb({ "name": "flick", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading DestructionVerbs
   * @summary Verb meaning flick an asset.
   * @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; flick wyvern's snout</span>
   * You flick the wyvern's snout. It sneezes out a tiny fireball.
   * </pre>
   * <p>
   * <strong>Flick</strong> requires that the
   * {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset} to be flicked has
   * asset.dov.flick.enabled
   * set to true. No special logic is provided with the verb.
   * 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
   */
  A.Preverbs.flick = {
    name: "flick",
    prettyname: "flick",
    past_tense: "flicked",
    synonyms: ["flick"],

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

    /**
     * @memberof flick
     * @ajsverbphrase
     * phrase1:
     * {
     *   accepts_noun:true,
     *   requires_noun:true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     matter: true,
     *     present_if_tangible: true,
     *     reachable_if_tangible: true,
     *     visible_if_tangible: true,
     *   },
     * },
     */
    phrase1: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        known: true,
        // tangible: true,
        // present: true,
        // visible: true,
        // reachable: true,
        matter: true,
        present_if_tangible: true,
        reachable_if_tangible: true,
        visible_if_tangible: true,
      },
    },

    /**
     * @memberof flick
     * @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 flick
     * @ajsverbparams
     * with_params: {},
     */
    with_params: {},

    doTry: function () {
      var input = this.game.getInput();
      var player = this.game.getPlayer();
      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 results;
      var msg = "";
      var containers;
      var inhands;
      var direct_object_vessel;

      // verb enabled?
      if (!direct_object.isDOV("flick")) {
        this.game.debug(
          `F1281 | ${this.name}.js | ${direct_object.id}.dov.flick.enabled is false`
        );
        msg += `$(We) can't flick ${direct_object.articlename}. `;
        this.handleFailure(msg);
        return null;
      }

      // single use direct object?
      if (
        direct_object.DOVallowOnce(this.name) &&
        direct_object.DOVdidDo(this.name)
      ) {
        this.game.debug(
          `F1823 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.once and ${direct_object.id}.dov.${this.name}.did_do `
        );
        msg += `${direct_object.Articlename} has already been ${this.past_tense}. `;
        this.handleFailure(msg);
        return false;
      }

      // did player input something like "throw sand" ?
      // if so it must come from a substance container that
      // is either in hands, or in a reachable substance body
      if (direct_object instanceof adventurejs.Substance) {
        // get a list of available vessels
        // this includes only things in inventory and nearby bodies of substance

        containers = this.game.findSubstanceBodyOrHeld(direct_object.id);

        switch (containers.length) {
          case 0:
            this.game.debug(` | ${this.name}.js | no valid vessel found `);
            msg += `There doesn't appear to be any ${
              this.game.getAsset(direct_object.id).name
            } to throw. `;
            this.handleFailure(msg);
            return null;
          default:
            // use the first container, regardless of how many are found
            direct_object_vessel = this.game.getAsset(containers[0]);
            input.setAssumed(1, true);
            input.setVessel(1, direct_object_vessel);
            break;
        } // switch containers.length
      } // direct_object is substance // not substance
      // else {
      //   // we bypassed the usual in_hands check to allow
      //   // substance handling so we need to check that now
      //   inhands = this.game.parser.selectInHands(direct_object.id);
      //   if (!inhands.length) {
      //     this.game.debug(
      //       ` | ${this.name}.js | ${direct_object.id} not in player's hands `
      //     );
      //     msg += `$(We're) not holding ${direct_object.articlename}. `;
      //     this.handleFailure(msg);
      //     return null;
      //   }
      // }

      // parsed sentence structure: verb noun
      if (input.hasStructure("verb noun")) {
        // indirect object required?
        if (direct_object.DOVallowWithNothing(this.name)) {
          return true;
        }

        // indirect objects available?
        if (!direct_object.DOVhasIndirectObjects(this.name)) {
          this.game.debug(
            `F1949 | ${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, true);
        if (results.prompt) {
          this.game.debug(`F1950 | ${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";
        }
      } // verb noun

      // parsed sentence structure: verb noun preposition noun
      if (input.hasStructure("verb noun preposition noun")) {
        if (indirect_preposition === "with" && !indirect_object.isIn(player)) {
          this.game.debug(
            `F1947 | ${this.name}.js | ${direct_object.id} is not in player `
          );
          msg += indirect_object.isDOV("take")
            ? `$(We're) not carrying ${indirect_object.articlename}. `
            : `$(We) can't use ${indirect_object.articlename} that way. `;
          this.handleFailure(msg);
          return null;
        }

        // works with any indirect object?
        if (direct_object.DOVallowWithAnything(this.name)) {
          return true;
        }

        // indirect object not required?
        if (direct_object.DOVallowWithNothing(this.name)) {
          this.game.debug(
            `F1951 | ${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.DOVallowWithAsset(this.name, indirect_object)) {
          this.game.debug(
            `F1952 | ${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(
            `F1953 | ${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.IOVallowOnce(this.name) &&
          indirect_object.IOVdidDo(this.name)
        ) {
          this.game.debug(
            `F1954 | ${this.name}.js | ${indirect_object.id}.iov.${
              this.name
            }.once and ${indirect_object.id}.iov.${this.name}.do_count is ${
              indirect_object.iov[this.name].do_count
            } `
          );
          msg += `${indirect_object.Articlename} has already been used to ${this.name} something. `;
          this.handleFailure(msg);
          return null;
        }
      }

      return true;
    },

    doSuccess: function () {
      var input = this.game.getInput();
      var player = this.game.getPlayer();
      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 direct_object_vessel = this.game.getAsset(input.getVessel(1));
      var currentRoom = this.game.getCurrentRoom();
      var results;
      var msg = "";

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

      // sentence structure: verb noun
      if (input.hasStructure("verb noun")) {
        // flick thing
        if (direct_object_vessel) {
          msg += `$(We) flick a bit of ${direct_object.articlename} from ${direct_object_vessel.articlename}. `;
        } else {
          msg += `$(We) flick ${direct_object.articlename}. `;
        }
      } // verb noun

      // sentence structure: verb noun preposition noun
      if (input.hasStructure("verb noun preposition noun")) {
        if (direct_object_vessel) {
          msg += `$(We) flick a bit of ${direct_object.articlename} from ${direct_object_vessel.articlename} ${indirect_preposition} ${indirect_object.articlename}. `;
        } else {
          if ("with" !== indirect_preposition) {
            results = player.onRemoveThatFromThis(direct_object);
            if ("undefined" !== typeof results) return results;

            results = currentRoom.onMoveThatToThis(direct_object, "in");
            if ("undefined" !== typeof results) return results;
          }
          msg += `$(We) flick ${direct_object.articlename} ${indirect_preposition} ${indirect_object.articlename}. `;
        }
      } // verb noun preposition noun

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