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

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

  /**
   * @augments {adventurejs.Verb}
   * @class hold
   * @ajsnode game.dictionary.verbs.hold
   * @ajsconstruct MyGame.createVerb({ "name": "hold", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading ManipulationVerbs
   * @summary Verb meaning hold 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; hold railing</span>
   * You hold the woven rope railing. It doesn't make the rotted
   * slat bridge look any less frightening.
   * </pre>
   * <p>
   * <strong>Hold</strong> a
   * {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset}.
   * Requires that the Asset to be held has
   * asset.dov.hold.enabled set to true.
   * <strong>Hold</strong> exists as a variation of
   * <strong>take</strong> that doesn't move the held
   * Asset into player inventory. This is intended for
   * holding onto fixed Assets such as railings or
   * {@link adventurejs.Rope|Ropes}. Player is fixed in
   * place while holding a stationary Asset and must
   * {@link drop} or {@link release}
   * the Asset to move. Though hold takes an indirect object,
   * technically the player is the indirect object. If an
   * indirect object is provided, we expect to be able to
   * treat it like a worn item, as in "hold pan with oven mitt".
   * Verb connections will be drawn between held item and
   * player.
   * </p>
   * @ajsverbreactions
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   */
  A.Preverbs.hold = {
    name: "hold",
    prettyname: "hold",
    past_tense: "held",
    synonyms: ["hold", "grab"],
    verb_prep_prep_noun: ["hold on to", "grab on to"],

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

    /**
     * @memberof hold
     * @ajsverbphrase
     * phrase1:
     * {
     *   accepts_noun:true,
     *   requires_noun:true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     tangible: 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,
      requires_noun: true,
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        visible: true,
        reachable: true,
      },
    },

    /**
     * @memberof close
     * @ajsverbphrase
     * phrase2:
     * {
     *   accepts_noun: true,
     *   noun_must_be:
     *   {
     *     in_inventory: true,
     *   },
     *   accepts_preposition: true,
     *   requires_preposition: true,
     *   accepts_these_prepositions: ["with"],
     * },
     */
    phrase2: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        in_inventory: true,
      },
      accepts_preposition: true,
      requires_preposition: true,
      accepts_these_prepositions: ["with"],
    },

    /**
     * @memberof hold
     * @ajsverbparams
     * with_params: {
     *   connections: [],
     *   max_connections: 1,
     * },
     */
    with_params: {
      connections: [],
      max_connections: 1,
    },

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

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

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

      // example: hold pen (take)
      // example: hold railing (hold on to)

      // is object takeable? do take
      // be careful here because we also want take to redirect to hold
      // don't want to get caught in a loop
      if (!direct_object.isDOV("hold") && direct_object.isDOV("take")) {
        this.game.debug(
          `F1311 | ${this.name}.js | inferring take, doVerb take `,
        );
        this.game.dictionary.doVerb("take");
        return null;
      }

      // can't hold it
      if (!direct_object.isDOV("hold")) {
        this.game.debug(
          `F1313 | ${this.name}.js | ${direct_object.id}.dov.hold.enabled is false `,
        );
        msg += `$(We) can't hold on to ${direct_object.articlename}. `;
        this.handleFailure(msg);
        return null;
      }

      // already holding object
      if (player.IOVisConnectedtoAsset(this.name, direct_object)) {
        this.game.debug(
          `F1314 | ${this.name}.js | ${player.id}.iov.hold.with_params.connections contains ${direct_object.id} `,
        );
        msg += `$(We're) already holding ${direct_object.articlename}. `;
        this.handleFailure(msg);
        return null;
      }

      // already holding max
      if (player.IOVhasMaxConnections(this.name)) {
        this.game.debug(
          `F1315 | ${this.name}.js | ${player.id}.iov.${this.name}.with_params.connections.length >= ${player.id}.iov.${this.name}.with_params.max_connections `,
        );
        msg += `$(We're) holding on to as many things as $(we) can. `;
        this.handleFailure(msg);
        return null;
      }

      // 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(
            `F1787 | ${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 null;
        }

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

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

        // 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(
            `F1790 | ${this.name}.js | ${direct_object.id} can't be ${this.state} by ${indirect_object.id} `,
          );
          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(
            `F1789 | ${this.name}.js | ${direct_object.id}.dov.${this.name}.with_assets 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(
            `F1901 | ${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(
            `F1807 | ${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;
        }
      } // verb noun preposition noun

      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 msg = "";
      var results;

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

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

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

      // state change
      this.setVerbSubscriptionConnection(direct_object, player);
      //player.IOVsetConnection(this.name, direct_object);

      // compose output
      msg +=
        "take" === input.input_verb
          ? `$(We) can't take ${direct_object.articlename}, but $(we) can hold it, so $(we) do that instead`
          : `$(We) grab hold of ${direct_object.articlename}`;
      msg += indirect_object ? `, using ${indirect_object}` : ``;
      msg += `. `;

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