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

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

  /**
   * @augments {adventurejs.Verb}
   * @class drop
   * @ajsnode game.dictionary.verbs.drop
   * @ajsconstruct MyGame.createVerb({ "name": "drop", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading ManipulationVerbs
   * @summary Verb meaning drop asset.
   * @ajssynonyms drop, put
   * @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; drop flugelhorn</span>
   * You drop the jewel encrusted flugelhorn. It emits a sad little blat.
   * </pre>
   * <p>
   * <strong>Drop</strong> a {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset}. Requires that the Asset is
   * in player's inventory.
   * </p>
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   */
  A.Preverbs.drop = {
    name: "drop",
    prettyname: "drop",
    past_tense: "dropped",
    synonyms: ["drop"],
    verb_prep_noun: ["put down"],
    enqueue_collections: true,

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

    /**
     * @memberof drop
     * @ajsverbphrase
     * phrase1:
     * {
     *   accepts_noun: true,
     *   requires_noun: true,
     *   accepts_plural_noun: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     in_inventory: true,
     *     not_worn_if_all: true,
     *     not_nested_inventory_if_all: true,
     *   },
     * },
     */
    phrase1: {
      accepts_noun: true,
      requires_noun: true,
      accepts_plural_noun: true,
      noun_must_be: {
        known: true,
        in_inventory: true,
        not_worn_if_all: true,
        not_nested_inventory_if_all: true,
      },
    },

    /**
     * @memberof drop
     * @ajsverbphrase
     * phrase2:
     * {
     *   accepts_noun:true,
     *   requires_noun: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     tangible: true,
     *     present: true,
     *     visible: true,
     *   accepts_preposition: true,
     *   requires_preposition: true,
     * },
     */
    phrase2: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        visible: true,
        singular: false,
      },
      accepts_preposition: true,
      requires_preposition: true,
    },

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

    doTry: function () {
      var input = this.game.getInput();
      var direct_object = input.getAsset(1);
      var player = this.game.getPlayer();
      var results;
      var msg = "";

      // sentence structure: verb noun preposition noun
      if (input.hasStructure("verb noun preposition noun")) {
        this.game.dictionary.doVerb("put");
        return null;
      }

      if (
        "all" === input.parsedNoun1.deserialized_input &&
        direct_object.is.worn
      ) {
        this.game.debug(
          `F1263 | ${this.name}.js | ${direct_object}.is.worn, not dropping it `
        );
        return null;
      }

      if (
        "all" === input.parsedNoun1.deserialized_input &&
        direct_object.getPlaceAssetId() !== this.game.world._player
      ) {
        this.game.debug(
          `F1264 | ${this.name}.js | ${direct_object} is not direct child of player, not dropping it `
        );
        return null;
      }

      // player is holding it, not carrying it, like a rope
      if (player.IOVisConnectedToAsset("hold", direct_object)) {
        this.game.debug(
          `F1265 | ${this.name}.js | player is holding ${direct_object}, infer release `
        );
        this.game.dictionary.doVerb("release");
        return null;
      }

      if (!direct_object.isIn(player)) {
        this.game.debug(
          `F1266 | ${this.name}.js | ${direct_object.id}+" is not child of player `
        );
        msg += `$(We're) not carrying ${direct_object.articlename}. `;
        this.handleFailure(msg);
        return null;
      }

      if (player.isNested() && player.getNestAsset().id === direct_object.id) {
        this.game.debug(
          `F1267 | ${this.name}.js | player is nested on ${direct_object.id}`
        );
        msg += `$(We) can't let go of ${
          direct_object.articlename
        } while $(we're) ${player.getPostureGerund()} ${player.getNestPreposition()} it. `;
        this.handleFailure(msg);
        return null;
      }

      return true;
    },

    doSuccess: function () {
      var input = this.game.getInput();
      var direct_object = input.getAsset(1);
      var indirect_object = input.getAsset(2);
      var player = this.game.getPlayer();
      var currentRoom = this.game.getCurrentRoom();
      var nest_asset = player.getNestAsset();
      var nest_preposition = player.getNestPreposition();
      var msg = "";
      var removedItem = false;
      var results;

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

      if (direct_object.is.worn) {
        results = direct_object.unfasten();
        msg += results ? `$(We) ${results}, remove, ` : `$(We) remove `;
        msg += `and drop ${direct_object.articlename}. `;
        direct_object.incrementDoVerbCount("remove", 1);
        direct_object.is.worn = false;
      } else {
        // compose output
        msg += `$(We) drop ${direct_object.articlename}. `;
      }

      // remove thing from player
      results = player.onRemoveThatFromThis(direct_object);
      if ("undefined" !== typeof results) return results;

      // set thing's new location to player's location
      // if nested, try to put there, otherwise in room
      if (nest_asset) {
        results = this.tryToPutThisInThatAspect(
          direct_object,
          player.getNestPreposition(),
          nest_asset
        );
      }
      if (nest_asset && !results.fail) {
        results = nest_asset.onMoveThatToThis(direct_object, nest_preposition);
        if ("undefined" !== typeof results) return results;
      } else {
        results = currentRoom.onMoveThatToThis(direct_object, "in");
        if ("undefined" !== typeof results) return results;
      }

      if (
        (1 < input.parsedNoun1.matches.qualified.length ||
          this.game.parser.isParsingMultiple()) &&
        -1 === input.output_class.indexOf("concatenate_output")
      ) {
        input.output_class += " concatenate_output ";
      }

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