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

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

  /**
   * @augments {adventurejs.Verb}
   * @class release
   * @ajsnode game.dictionary.verbs.release
   * @ajsconstruct MyGame.createVerb({ "name": "release", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading ManipulationVerbs
   * @summary Verb meaning release or let go of, as in "let go of rope".
   * @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; let go of pride</span>
   * You let go of your pride. With it, you feel a swell
   * of emotion: loss, grief, regret. Joy. You sob and laugh
   * with an intense, almost hysterical feeling of release,
   * tinged with a hint of bitterness at all the time you've lost.
   * </pre>
   * <p>
   * <strong>Let go of</strong> a
   * {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset}.
   * Operates in several ways. If player is hanging, as if from a rope,
   * redirects to {@link go_off}. If player is holding but not nested,
   * as if with an end of a rope, player lets go. If player is carrying
   * Asset, player drops it.
   * </p>
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   */
  A.Preverbs.release = {
    name: "release",
    prettyname: "release",
    synonyms: ["release"],
    verb_prep_noun: ["let go"],
    verb_prep_prep_noun: ["let go of"],

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

    /**
     * @memberof release
     * @ajsverbphrase
     * phrase1:
     * {
     *   accepts_noun:true,
     *   noun_must_be:
     *   {
     *     tangible: true,
     *     known: true,
     *     present: true,
     *     visible: true,
     *     reachable: true,
     *     //in_inventory: true,
     *     // If we were carrying we'd limit to in_inventory
     *     // but "let go of" can also apply to things player
     *     // is holding on to.
     *   },
     * },
     */
    phrase1: {
      accepts_noun: true,
      noun_must_be: {
        tangible: true,
        known: true,
        present: true,
        visible: true,
        reachable: true,
      },
    },

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

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

      if (input.hasStructure("verb")) {
        // parsed sentence structure: verb

        if ("release" === input.input_verb) {
          // we think of "release" as meaning drop
          // and we don't accept it as intransitive
          // in the way that we accept "let go" where
          // context should make direct object clear

          // prompt for a direct object
          input.setSoftPrompt({ noun1: true, input_verb: input.input_verb });
          this.game.debug(
            `F1355 | ${this.name}.js | no noun provided or inferrable, soft prompt noun1`
          );
          msg += `What did $(we) want to release? `;
          this.handleFailure(msg);
          return null;
        }

        // is player nested in something they can drop from?
        if (player.isNested() && nest_asset.quirks.let_go_of_means_go_off) {
          asset = nest_asset;
        }

        // is player holding one thing?
        else if (1 === player.IOVgetConnectionCount("hold")) {
          asset = this.game.getAsset(player.IOVgetConnections("hold")[0]);
        }

        if (!asset) {
          this.game.debug(
            `F1709 | ${this.name}.js | no noun supplied or inferred, soft prompt noun1 `
          );
          input.setSoftPrompt({ noun1: true, verb: "release" });
          msg += "Let go of what? ";
          this.handleFailure(msg);
          return null;
        }

        direct_object = asset;
        input.setAsset(1, asset);
        input.setAssumed(1);
        input.setStructure("verb noun");
      } // verb

      // sentence structure: verb noun
      // example: let go of rope (holding)
      if (input.hasStructure("verb noun")) {
        // example: let go of branch (hanging from)
        // might result in falling to ground
        // might also result in tryTravel to lower room

        // @TODO
        // what if player is in space?
        // or in rushing water?

        // author can do these in doAfterSuccess

        // is object takeable and in player inventory? do drop
        if (direct_object.isIn(player) && direct_object.isDOV("drop")) {
          this.game.debug(
            `F1710 | ${this.name}.js | infer 'drop ${direct_object.name}', doVerb drop`
          );
          this.game.dictionary.doVerb("drop");
          return null;
        }

        // is player nested in an object where letGo means goOff?
        if (
          player.isNested() &&
          direct_object.id === nest_asset.id &&
          direct_object.quirks.let_go_of_means_go_off
        ) {
          this.game.debug(
            `F1711 | ${this.name}.js | infer 'go off', doVerb go`
          );
          if (player.IOVisConnectedToAsset("hold", direct_object)) {
            // break "held" connection
            player.IOVunsetConnection("hold", direct_object);
          }
          input.setVerb("go");
          input.setPreposition(1, "off");
          input.setStructure("verb preposition noun");
          this.game.dictionary.doVerb("go");
          return null;
        }

        // is player nested in an object where letGo means goDown?
        if (
          player.isNested() &&
          direct_object.id === nest_asset.id &&
          direct_object.quirks.let_go_of_means_go_down
        ) {
          this.game.debug(
            `F1712 | ${this.name}.js | infer 'go down', doVerb down`
          );
          if (player.IOVisConnectedToAsset("hold", direct_object)) {
            // break "held" connection
            player.IOVunsetConnection("hold", direct_object);
          }
          this.game.tryTravel("down");
          return null;
        }

        // not holding object
        if (!player.IOVisConnectedToAsset("hold", direct_object)) {
          msg += `$(We're) not holding ${direct_object.articlename}. `;
          this.handleFailure(msg);
          return null;
        }
      } // verb noun

      return true;
    },

    doSuccess: function () {
      var input = this.game.getInput();
      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 msg = "";
      var results;

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

      // apply state changes
      if (player.IOVisConnectedToAsset("hold", direct_object)) {
        player.IOVunsetConnection("hold", direct_object);
      }

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

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