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

(function () {
  /*global adventurejs A*/

  /**
   * @augments {adventurejs.Verb}
   * @class kneel
   * @ajsnode game.dictionary.verbs.kneel
   * @ajsconstruct MyGame.createVerb({ "name": "kneel", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading PositionVerbs
   * @summary Verb meaning kneel 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; kneel</span>
   * You kneel on the floor.
   * </pre>
   * <p>
   * If input without an object,
   * <strong>kneel</strong> tries to put subject into a kneeling
   * position wherever they happen to be. If subject is not nested in a
   * {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset}, <strong>kneel</strong> uses
   * the {@link adventurejs.Floor|Floor}. Requires that the
   * Asset has an
   * {@link adventurejs.Aspect|Aspect} with its
   * aspect.nest.can.kneel</a>
   * property set to true.
   * </p>
   * @ajsverbreactions doNestThatToThis, doNestThisToThat, doUnnestThatFromThis, doUnnestThisFromThat
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   */
  A.Preverbs.kneel = {
    name: "kneel",
    prettyname: "kneel",
    past_tense: "knelt",
    gerund: "kneeling",

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

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

    /**
     * @memberof kneel
     * @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,
     *   // accepts_these_prepositions: ["on"],
     * },
     */
    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 kneel
     * @ajsverbparams
     * with_params: {},
     */
    with_params: {},

    in_can_mean_on: true,

    doTry: function () {
      var input = this.game.getInput();
      var subject = input.getSubject();
      var verb_phrase = input.verb_phrase;
      var direct_object = input.getAsset(1);
      var direct_preposition = input.getPreposition(1);
      var nest_asset = subject.getNestAsset();
      var nest_preposition = subject.getNestPreposition();
      var target;
      var msg = "";
      var results;

      // sentence structure: verb
      if (input.hasStructure("verb")) {
        // is subject already kneeling?
        if (subject.is.kneeling) {
          this.game.debug(
            `D1326 | ${this.name}.js | ${subject.id}.is.kneeling `
          );
          msg += `$(We're) already kneeling! `;
          this.handleFailure(msg);
          return null;
        }
        if (nest_asset) {
          direct_preposition = subject.getNestOrPlacePreposition();
          direct_object = nest_asset;
        } else {
          direct_preposition = "on";
          direct_object = this.game.getCurrentRoomFloor();
        }
        input.setAsset(1, direct_object);
        input.setPreposition(1, direct_preposition);
        input.setStructure("verb preposition noun");
        this.game.printInferred(
          `${direct_preposition} ${direct_object.articlename}`
        );
      } // verb

      // sentence structure: verb preposition
      if (input.hasStructure("verb preposition")) {
        if (direct_preposition === "down") {
          if (subject.is.kneeling) {
            this.game.debug(
              `D1088 | ${this.name}.js | ${subject.id}.is.kneeling`
            );
            msg += `$(We) shift weight from knee to knee. `;
            this.handleFailure(msg);
            return null;
          }
          // kneel down is still an option but we need a direct object
          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.setInferred(1, true);
            input.setStructure("verb preposition noun");
            this.game.printInferred(
              `${direct_preposition} ${direct_object.articlename}`
            );
          }
        } else {
          this.game.debug(
            `D1021 | ${this.name}.js | no direct object received or inferred`
          );
          msg += `$(We) try to ${this.name} ${direct_preposition}, and fail. `;
          this.handleFailure(msg);
          return null;
        }
      } // verb preposition

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

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

        // subject 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(
            `D1614 | ${this.name}.js | subject is otherwise nested ${nest_preposition} ${nest_asset.id} `
          );
          msg += `$(We) can't do that from $(our) position ${subject.getPostureGerund()} ${nest_preposition} ${
            nest_asset.articlename
          }. `;
          this.handleFailure(msg);
          return null;
        }

        if (subject.is.kneeling) {
          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(
              `D1613 | ${this.name}.js | ${subject.id}.is.kneeling ${direct_preposition} ${direct_object.id} `
            );
            msg += `$(We're) already kneeling ${direct_preposition} ${direct_object.articlename}. `;
            this.handleFailure(msg);
            return null;
          }
        }
      } // verb preposition noun

      target = direct_object;
      if (target?.placePreventsNesting(subject)) {
        this.game.debug(
          `D1164 | ${this.name}.js | ${target.id}.is.deep_nest is false `
        );
        msg += `$(We) can't do that while ${target.articlename_is} 
          ${target.getPrettyPlacePreposition()}
          ${target.getPlaceAsset().articlename}. `;
        this.handleFailure(msg);
        return null;
      }

      return true;
    },

    doSuccess: function () {
      var input = this.game.getInput();
      var subject = input.getSubject();
      var verb_phrase = input.verb_phrase;
      var direct_object = input.getAsset(1);
      var direct_preposition = input.getPreposition(1);
      var indirect_object = input.getAsset(2);
      var nest_asset = subject.getNestAsset();
      var nest_preposition = subject.getNestPreposition();
      var do_nest, do_unnest;
      var fromfloor;
      var tofloor;
      var results;
      var msg = "";

      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.log(
          "L1378",
          "log",
          "high",
          `${this.name}.js > from floor to floor`,
          "verbs"
        );
        msg += `$(We) ${subject.isOnFloor() ? "shift positions to" : ""} ${
          this.name
        } 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.log(
          "L1379",
          "log",
          "high",
          `${this.name}.js > from floor to asset or change position on asset`,
          "verbs"
        );
        msg += `${
          fromfloor && subject.posture !== "stand"
            ? "$(We) rise from the floor and "
            : "$(We) "
        } ${this.name} ${direct_preposition} ${direct_object.articlename}. `;
      }

      // nest_asset to floor - unnest
      else if (!fromfloor && tofloor) {
        do_unnest = true;
        this.game.log(
          "L1380",
          "log",
          "high",
          `${this.name}.js > from asset to floor `,
          "verbs"
        );
        msg += `$(We) get ${subject.getPrettyUnnestPreposition()} ${
          nest_asset.articlename
        } and ${this.name} ${direct_preposition} ${
          direct_object.articlename
        }. `;
      }

      // nest_asset to same nest_asset
      else if (!fromfloor && !tofloor && direct_object.id === nest_asset.id) {
        if (subject.is.lying || subject.is.sitting || subject.is.standing) {
          this.game.debug(` | ${this.name}.js | change posture on floor `);
          msg += `$(We) ${this.agree()} ${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.log(
          "L1381",
          "log",
          "high",
          `${this.name}.js > move from one asset to another `,
          "verbs"
        );
        msg += `$(We) move over to ${direct_object.articlename} and ${this.name} ${direct_preposition} it. `;
      }

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

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

      subject.posture = this.name;

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