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

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

  var p = adventurejs.Game.prototype;

  /**
   * <p>
   * Global assets are a bit (perhaps overly) complicated.
   * They exist as singular objects in the game world, and
   * they can be enabled/disabled and have their descriptions
   * overridden per room and per zone.
   * </p>
   * <p>
   * For example: <br/>
   *
   * <li><code>MyGame.world.global_air</code> is an asset</li>
   *
   * <li><code>MyGame.world.room.room_scenery.global_air</code>
   * is a simple object that can be used to determine whether
   * a global asset is available in a given room. </li>
   *
   * <li><code>MyGame.world.zone.zone_scenery.global_air</code>
   * is a simple object that can be used to determine whether
   * a global asset is available in a given zone. </li>
   *
   * Though global asset is singular, its description can be
   * overridden by descriptions set in room_scenery and zone_scenery.
   * </p>
   * @memberOf adventurejs.Game
   * @method adventurejs.Game#getGlobalAssetDescription
   * @param {Object|String} asset
   * @param {String} identifier
   * @returns {String} A description of the specified asset id.
   */
  p.getGlobalAssetDescription = function Game_getGlobalAssetDescription(
    asset,
    identifier = "look"
  ) {
    //          defined  enabled  described
    //   room   _        _        _           all slots can be
    //   zone   _        _        _           true, false, or
    // global   _        _        _           null/undefined

    // if defined is null we check next level
    // if defined is false at any stage we stop
    // if any level is enabled but not described,
    //   try to get description from next level

    if ("string" === typeof asset) asset = this.game.getAsset(asset);
    if (!(asset instanceof adventurejs.Asset)) return "";

    console.warn("getGlobalAssetDescription", asset.id);

    var currentRoom = this.game.getCurrentRoom();

    var defined_in_room;
    var enabled_in_room; // true, false, or null/undefined
    var described_in_room;

    var defined_in_zone;
    var enabled_in_zone; // true, false, or null/undefined
    var described_in_zone;

    var defined_in_game;
    var enabled_in_game; // true, false, or null/undefined
    var described_in_game;

    // get room settings
    defined_in_room = currentRoom.room_scenery[asset.id];
    if ("undefined" !== typeof defined_in_room) {
      enabled_in_room = defined_in_room.enabled;
      described_in_room = this.game.getModifiedDescription({
        asset: defined_in_room,
        identifier: identifier,
        find_modifiers: true,
        fallback_base: true,
      });
    }

    // get zone settings
    if ("undefined" !== typeof this.game.world[currentRoom.zone]) {
      defined_in_zone =
        this.game.world[currentRoom.zone].zone_scenery[asset.id];
    }
    if (defined_in_zone) {
      enabled_in_zone = defined_in_zone.enabled;
      described_in_zone = this.game.getModifiedDescription({
        asset: defined_in_zone,
        identifier: identifier,
        find_modifiers: true,
        fallback_base: true,
      });
    }

    // get global settings
    // no need to ask if undefined because we received the global asset
    enabled_in_game = asset.enabled;
    described_in_game = this.game.getModifiedDescription({
      asset: asset,
      identifier: identifier,
      find_modifiers: true,
      fallback_base: true,
    });

    // console.warn("enabled_in_room", enabled_in_room);
    // console.warn("enabled_in_zone", enabled_in_zone);
    // console.warn("enabled_in_game", enabled_in_game);

    // console.warn("described_in_room", described_in_room);
    // console.warn("described_in_zone", described_in_zone);
    // console.warn("described_in_game", described_in_game);

    // if room explicitly says no, exit
    if (false === enabled_in_room) {
      return "";
    }
    // if room explicitly says yes, use room or zone or global
    else if (true === enabled_in_room) {
      return described_in_room || described_in_zone || described_in_game;
    }
    // if zone explicitly says no, exit
    else if (false === enabled_in_zone) {
      return "";
    }
    // if zone explicitly says yes, use zone or global
    else if (true === enabled_in_zone) {
      return described_in_zone || described_in_game;
    }
    // if game explicitly says no, exit
    else if (false === enabled_in_game) {
      return "";
    }
    // if game explicitly says yes, use global
    else if (true === enabled_in_game) {
      return described_in_game;
    }

    // if room nor zone nor game has no opinion
    // but there is a description, print it
    if (
      "undefined" === typeof enabled_in_room &&
      "undefined" === typeof enabled_in_zone &&
      "undefined" === typeof enabled_in_game &&
      (described_in_room || described_in_zone || described_in_game)
    ) {
      return described_in_room || described_in_zone || described_in_game;
    }

    // ideally we never arrive here
    return `${asset.Articlename_is} indescribable. `;
  };
})();