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

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

  /**
   * @augments {adventurejs.Verb}
   * @class ride
   * @ajsnode game.dictionary.verbs.ride
   * @ajsconstruct MyGame.createVerb({ "name": "ride", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading LocomotionVerbs
   * @summary Verb meaning ride, as in "ride horse".
   * @todo Ride NPCs (like a horse). Will NPC be a SimpleVehicle?
   * @todo What about things like rafts that aren't under player's control?
   * @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; ride hippogryph</span>
   * You ride the hippogryph, bearing in mind that this is not one of your
   * classical type hippogryphs - you know, half horse, half gryphon - but
   * is in fact a full-on hippopotamus named Griff. As such, Griff has
   * mixed feelings about the arrangement of being ridden. On the one hand,
   * you could kick your heels and go "yah!" and he could carry you across
   * the lake. On the other, he could roll over and crush you. Griff
   * casually regurgitates some cud and reswallows it while he considers the
   * situation. (Unlike other ruminants, hippos don't chew their cud.
   * THE MORE YOU KNOW!)
   * </pre>
   * <p>
   * <strong>Ride</strong> a
   * {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset}, optionally in a direction.
   * Requires that the Asset to be ridden has
   * asset.dov.ride.enabled
   * set to true. If player isn't on Asset, verb will
   * redirect to {@link go}. If player is already on Asset
   * and a direction is also provided, player will try to travel.
   * </p>
   * <p>
   * It's assumed that anything that has
   * dov.ride will be a
   * {@link adventurejs.SimpleVehicle|SimpleVehicle}.
   * See
   * <a href="/doc/Tangibles_SimpleVehicles.html">Simple Vehicles</a>
   * to learn more.
   * </p>
   * @ajsverbreactions
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   */
  A.Preverbs.ride = {
    name: "ride",
    prettyname: "ride",
    past_tense: "rode",
    synonyms: ["ride", "peddle", "skate"],
    verb_prep_noun: ["ride on"],

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

    player_must_be: {
      not_constrained: true,
      not_on_floor: true,
      not_nested_elsewhere: true,
      able_to_ride: true,
    },

    /**
     * @memberof ride
     * @ajsverbphrase
     * phrase1:
     * {
     *   accepts_noun:true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     tangible: true,
     *     present: true,
     *     visible: true,
     *     singular: false,
     *     //not_in_inventory: true,
     *   },
     * },
     */
    phrase1: {
      accepts_noun: true,
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        visible: true,
        singular: false,
      },
    },

    /**
     * @memberof ride
     * @ajsverbphrase
     * phrase2:
     * {
     *   accepts_noun:true,
     *   noun_must_be:
     *   {
     *     direction: true,
     *   },
     *   accepts_preposition: true,
     *   // accepts_these_prepositions: ["to"], // ride onto? ride into?
     * },
     */
    phrase2: {
      accepts_noun: true,
      noun_must_be: {
        direction: true,
      },
      accepts_preposition: true,
    },

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

    doTry: function () {
      var input = this.game.getInput();
      var input_verb = input.input_verb;
      var direct_object = input.getAsset(1);
      var indirect_object = input.getAsset(2);
      var player = this.game.getPlayer();
      var nest_asset = player.getNestAsset();
      var currentRoom = this.game.getCurrentRoom();
      var msg = "";

      if (!direct_object) {
        if (nest_asset && nest_asset.is.rideable) {
          // was ride called without noun?
          direct_object = nest_asset;
          input.setAsset(2, direct_object);
          this.game.debug(
            `F1213 | ${this.name}.js | infer ${direct_object.id} as direct_object `,
          );
        } else {
          // can't do it
          input.setSoftPrompt({ noun1: true, verb: "ride" });
          this.game.debug(
            `F1211 | ${this.name}.js | no direct_object received or inferred, soft prompt noun1 `,
          );
          msg += `What would $(we) like to ${input_verb}? `;
          this.handleFailure(msg);
          return null;
        }
      }

      // did player input something like "ride east" ?
      if (
        direct_object.direction &&
        !indirect_object &&
        nest_asset &&
        nest_asset.is.rideable
      ) {
        // set nest as direct object
        indirect_object = direct_object;
        direct_object = nest_asset;
        input.setPhrase(2, input.getPhrase(1));
        input.setAsset(1, nest_asset);
        input.setStructure("verb noun noun");
      }

      if ("peddle" === input_verb && !direct_object.is.peddleable) {
        this.game.debug(
          `F1214 | ${this.name}.js | ${direct_object.id}.is.peddleable is false `,
        );
        msg += `${direct_object.Articlename} can't be peddled. `;
        this.handleFailure(msg);
        return null;
      }

      if ("skate" === input_verb && !direct_object.is.skateable) {
        this.game.debug(
          `F1215 | ${this.name}.js | ${direct_object.id}.is.skateable is false `,
        );
        msg += `$(We) can't skate on ${direct_object.articlename}. `;
        this.handleFailure(msg);
        return null;
      }

      if (!direct_object.isDOV("ride")) {
        this.game.debug(
          `F1216 | ${this.name}.js | ${direct_object.id}.dov.ride.enabled is false `,
        );
        msg += `${direct_object.Articlename} can't be ridden. `;
        this.handleFailure(msg);
        return null;
      }

      if (nest_asset.id !== direct_object.id) {
        if (indirect_object) {
          this.game.debug(
            `F1217 | ${this.name}.js | player must be on ${direct_object.id} `,
          );
          msg += `$(We're) not on ${direct_object.articlename}. `;
          this.handleFailure(msg);
          return null;
        }
        input.setVerb("go");
        input.setPreposition(1, "on");
        input.setStructure("verb preposition noun");
        this.game.dictionary.doVerb("go");
        return null;
      }

      if (!player.can.ride || !player.getNestOrPlaceAspect().player.can.ride) {
        this.game.debug(
          `F1854 | ${
            this.name
          }.js | player.can.ride is false or ${player.getNestOrPlaceAsset()}.aspects.${player.getNestOrPlacePreposition()} }.player.can.ride is false`,
        );
        msg += `$(We) can't ride ${direct_object.articlename} here. `;
        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 nest_asset = player.getNestAsset();
      var msg = "";
      var results;

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

      if (indirect_object && indirect_object.direction) {
        results = this.game.tryTravel(indirect_object.direction, {
          with: [direct_object.id],
        });
        if (A.isFalseOrNull(results)) return results;
        // no msg because tryTravel handled it
      } else {
        msg += `$(We) ride ${direct_object.articlename} around the room. `;
      }

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