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

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

  /**
   * @augments {adventurejs.Verb}
   * @class where
   * @ajsnode game.dictionary.verbs.where
   * @ajsconstruct MyGame.createVerb({ "name": "where", [...] });
   * @ajsconstructedby adventurejs.Dictionary#createVerb
   * @hideconstructor
   * @ajsinstanceof Verb
   * @ajsnavheading UtilityVerbs
   * @summary Verb that returns location of an object.
   * @todo consider "last saw" and how this applies to NPCs who move between rooms
   * @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; where is hendrickson</span>
   * You last saw Ms. Hendrickson in the Supply Room.
   * </pre>
   * <p>
   * <strong>Where is</strong> a
   * {@link adventurejs.Tangible|Tangible}
   * {@link adventurejs.Asset|Asset} will return the name of the
   * {@link adventurejs.Room|Room} that the Asset is in. Intended
   * chiefly for debugging, but may also be useful in game play.
   * </p>
   * tryWhereThis
   * doWhereThis
   * @ajsverbphases doBeforeTry, doAfterTry, doBeforeSuccess, doAfterSuccess
   */
  A.Preverbs.where = {
    name: "where",
    prettyname: "where is",
    synonyms: [],
    verb_prep_noun: ["where is"],

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

    /**
     * @memberof where
     * @ajsverbphrase
     * phrase1:
     * {
     *   accepts_noun: true,
     *   requires_noun: true,
     *   noun_must_be:
     *   {
     *     known: true,
     *     tangible: true,
     *   },
     * },
     */
    phrase1: {
      accepts_noun: true,
      requires_noun: true,
      noun_must_be: {
        known: true,
        tangible: true,
      },
    },

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

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

      if ("Room" === direct_object.class) {
        this.game.debug(
          `F1500 | where.js | " + direct_object.id + ".class is Room `
        );
        msg += `Maybe $(we) should draw a map? `;
        this.handleFailure(msg);
        return null;
      }

      if (!direct_object.areAnscestorsKnown()) {
        this.game.debug(
          `F1501 | where.js | ${direct_object.id} has unknown ancestors `
        );
        msg += `$(We) don't know where ${direct_object.articlename} is. `;
        this.handleFailure(msg);
        return null;
      }

      if (!room) {
        this.game.debug(`F1502 | where.js | ${direct_object.id} has no room `);
        msg += `$(We) don't know where ${direct_object.articlename} is. `;
        this.handleFailure(msg);
        return null;
      }

      if (room === this.game.world._currentRoom) {
        this.game.debug(
          `F1503 | where.js | ${direct_object.id} is in current room `
        );
        msg += `${direct_object.Articlename} is right here in the room with $(us)! `;
        this.handleFailure(msg);
        return null;
      }

      return true;
    },

    doSuccess: function () {
      var input = this.game.getInput();
      var player = this.game.getPlayer();
      var direct_object = input.getAsset(1);
      var room = this.game.getAsset(direct_object.getRoomId());
      var results;
      var msg = "";

      // TODO: handle characters
      this.game.debug(`F1504 | where.js | print doSuccess `);
      msg += `$(We) last saw ${direct_object.articlename} in ${
        room.use_definite_article_in_lists ? room.definite_article + " " : ""
      } ${room.name}. `;

      this.handleSuccess(msg, direct_object);

      return true;
    },
  };
})(); // where