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

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

  /**
   * @augments {adventurejs.Verb}
   * @class lie
   * @ajsnode game.dictionary.verbs.lie
   * @ajsconstruct MyGame.createVerb({ "name": "lie", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading PositionVerbs
   * @summary Verb meaning lie down in place or on 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; lie down</span>
   * You lie down, and let your mind drift. Though you would prefer they
   * not, your thoughts turn to the mademoiselle of Mairie Ste Cloud,
   * as they so often do. Perhaps you never will forget that
   * fateful kiss beneath the statue of Santos Dumont, stolen at
   * such great cost and never returned.
   * </pre>
   * <p>
   * <strong>Lie</strong> or more likely <strong>lie down</strong>
   * tries to put player into a lying
   * position wherever they happen to be. If player is not nested in a
   * {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset}, <strong>lie</strong> uses
   * the {@link adventurejs.Floor|Floor}. Requires that the
   * Asset has an {@link adventurejs.Aspect|Aspect} with its
   * player.can.lie
   * property set to true.
   * </p>
   * @ajsverbreactions
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   */
  A.Preverbs.lie = {
    name: "lie",
    prettyname: "lie",
    past_tense: "lay",
    //synonyms: ["lie down"],
    //verb_prep_noun: ["lie down"],

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

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

    /**
     * @memberof lie
     * @ajsverbphrase
     * phrase1:
     * {
     *   accepts_noun:true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     tangible: true,
     *     present: true,
     *     visible: true,
     *     reachable: true,
     *     not_direction: true,
     *     not_in_inventory: true,
     *   },
     *   accepts_preposition: true,
     *   requires_preposition: true,
     * },
     */
    phrase1: {
      accepts_noun: true,
      noun_must_be: {
        known: true,
        tangible: true,
        present: true,
        visible: true,
        reachable: true,
        not_direction: true,
        not_in_inventory: true,
      },
      accepts_preposition: true,
      accepts_preposition_without_noun: true,
      requires_preposition: true,
    },

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

    in_can_mean_on: true,

    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 nest_asset = player.getNestAsset();
      var nest_preposition = player.getNestPreposition();
      var msg = "";
      var results;

      //@todo add distinction for "lie to"
      // or even contextual clue for just "lie"
      // if player has been conversing

      // sentence structure: verb
      if (input.hasStructure("verb")) {
        // already lying
        if (player.is.lying) {
          this.game.debug(`F1010 | ${this.name}.js | player.is.lying`);
          msg += "$(We're) already lying! ";
          this.handleFailure(msg);
          return null;
        }
        direct_preposition = player.getNestOrPlacePreposition();
        input.setPreposition(1, direct_preposition);
        direct_object = player.getNestOrPlaceAsset();
        input.setStructure("verb preposition noun");
      } // verb

      // sentence structure: verb preposition
      if (input.hasStructure("verb preposition")) {
        if (direct_preposition === "down") {
          if (player.is.lying) {
            this.game.debug(`F1153 | ${this.name}.js | ${player.id}.is.lying`);
            msg += `$(We) roll over fitfully. `;
            this.handleFailure(msg);
            return null;
          }
          if (nest_asset) {
            direct_object = nest_asset;
            direct_preposition = nest_preposition;
          } else {
            // see if the room has its own floor
            direct_object = this.game.getCurrentRoomFloor();
            direct_preposition = "on";
          }
          if (direct_object) {
            input.setAsset(1, direct_object);
            input.setPreposition(1, direct_preposition);
            input.setAssumed(1, true);
            input.setStructure("verb preposition noun");
          }
        } else {
          this.game.debug(
            `F1240 | ${this.name}.js | no direct object received or inferred`
          );
          msg += `$(We) try to lie ${direct_preposition}, and fail. `;
          this.handleFailure(msg);
          return null;
        }
      } // verb preposition

      // sentence structure: verb preposition noun
      if (input.hasStructure("verb preposition noun")) {
        // is player allowed to lie here?
        if (
          !player.can.lie ||
          !direct_object.aspects[direct_preposition].player.can.lie
        ) {
          // nested in an object that can't be stood in
          this.game.debug(
            `F1011 | ${this.name}.js | player.can.lie is false or ${direct_object.id}.aspects.${direct_preposition} }.player.can.lie is false`
          );
          msg += `$(We) can't lie ${direct_preposition} ${direct_object.articlename}. `;
          this.handleFailure(msg);
          return null;
        }

        // can player reach it?
        if (nest_asset && nest_asset.id !== direct_object.id) {
          results = this.canPlayerGoThereFromNest(
            direct_preposition,
            direct_object
          );
          if (results.failure) {
            this.handleFailure(results.msg);
            return null;
          }
          if (results.return) return true;
        }

        // player is nested some other way with target
        // and needs to get up first
        if (
          nest_asset &&
          nest_asset.id === direct_object.id &&
          nest_preposition !== direct_preposition
        ) {
          this.game.debug(
            `F1013 | ${this.name}.js | player is otherwise nested ${nest_preposition} ${nest_asset.id}`
          );
          msg += `$(We) can't do that from $(our) position ${player.getPostureGerund()} ${nest_preposition} ${
            nest_asset.articlename
          }. `;
          this.handleFailure(msg);
          return null;
        }

        // player is already lying [preposition] target
        if (player.is.lying) {
          var already_on_it;

          if (
            nest_asset &&
            nest_asset.id === direct_object.id &&
            nest_preposition === direct_preposition
          )
            already_on_it = true;

          if (!nest_asset && direct_object instanceof adventurejs.Floor)
            already_on_it = true;

          if (already_on_it) {
            this.game.debug(
              `F1090 | ${this.name}.js | ${player.id}.is.lying ${direct_preposition} ${direct_object.id}`
            );
            msg += `$(We're) already lying ${direct_preposition} ${direct_object.articlename}. `;
            this.handleFailure(msg);
            return null;
          }
        }
      } // verb preposition noun

      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 nest_asset = player.getNestAsset();
      var nest_preposition = player.getNestPreposition();
      var do_nest, do_unnest;
      var fromfloor;
      var tofloor;
      var results;
      var msg = "";

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

      tofloor = direct_object instanceof adventurejs.Floor;
      fromfloor = !nest_asset || nest_asset instanceof adventurejs.Floor;

      // floor to floor
      if (fromfloor && tofloor) {
        do_nest = true;
        this.game.debug(`F1627 | ${this.name}.js | from floor to floor `);
        msg += `$(We) ${
          player.isOnFloor() ? "shift positions to" : ""
        } lie down on the floor. `;
      }

      // floor to nest or change positions on nest
      else if (
        (fromfloor && !tofloor) ||
        (nest_asset && nest_asset.id === direct_object.id)
      ) {
        do_nest = fromfloor && !tofloor;
        this.game.debug(
          `F1628 | ${this.name}.js | from floor to asset or change position on asset `
        );
        msg += `$(We) lie ${direct_preposition} ${direct_object.articlename}. `;
      }

      // nest_asset to floor - unnest
      else if (!fromfloor && tofloor) {
        do_unnest = true;
        this.game.debug(`F1629 | ${this.name}.js | from asset to floor `);
        msg += `$(We) hop ${player.getPrettyUnnestPreposition()} ${
          nest_asset.articlename
        } and lie ${direct_preposition} ${direct_object.articlename}. `;
      }

      // nest_asset to same nest_asset
      else if (!fromfloor && !tofloor && direct_object.id === nest_asset.id) {
        if (player.is.lying || player.is.sitting || player.is.kneeling) {
          this.game.debug(`F1630 | ${this.name}.js | change posture on floor `);
          msg += `$(We) lie down ${nest_preposition} ${direct_object.articlename}. `;
        }
      }

      // nest_asset to other nest_asset
      else if (!fromfloor && !tofloor && direct_object.id !== nest_asset.id) {
        do_nest = true;
        do_unnest = true;
        this.game.debug(
          `F1631 | ${this.name}.js | move from one asset to another `
        );
        msg += `$(We) climb over to ${direct_object.articlename} and lie down ${direct_preposition} it. `;
      }

      if (do_unnest && nest_asset) {
        results = player.onUnnestThisFromThat(nest_asset);
        if ("undefined" !== typeof results) return results;
      }

      if (do_nest && direct_object) {
        results = player.onNestThisToThat(direct_object, direct_preposition);
        if ("undefined" !== typeof results) return results;
      }

      player.posture = "lie";

      // print output
      this.handleSuccess(msg, direct_object);
      return true;
    },
  }; // END p.preverbs.push
})();