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

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

  var p = adventurejs.Game.prototype;

  /**
   *
   * @method adventurejs.Game#printRoomContents
   * @memberOf adventurejs.Game
   * @param {Object} room A room to be printed. Defaults to current room.
   */
  p.printRoomContents = function Game_printRoomContents(
    room = this.world[this.world._room]
  ) {
    this.game.log(
      "L1050",
      "log",
      1,
      `[printRoomContents.js] print ${this.world._room} contents`,
      "Game"
    );

    var roomContentsByID = A.clone.call(this.game, room.getAllContents()); // array of strings
    var listableObjects = [];
    var objectsWithListableContents = [];
    var objectsTiedToOtherThings = [];
    var output = "";
    var player = this.game.getPlayer();

    // exclude player from list
    roomContentsByID.splice(roomContentsByID.indexOf(this.world._player), 1);

    // exclude exits from list ?

    // @TODO consider lighting / visibility

    for (let i = roomContentsByID.length - 1; i > -1; i--) {
      const object = this.game.getAsset(roomContentsByID[i]);
      //console.log( object );

      if (!object) {
        this.game.log(
          "L1053",
          "warn",
          "critical",
          `[printRoomContents.js] found an undefined object in room ${room.name}`,
          "Game"
        );
        continue;
      }

      if (object.is.hidden) {
        continue;
      }

      if (object.is.listed) {
        listableObjects.push(object);
      }

      if (object.hasListableContents()) {
        objectsWithListableContents.push(object);
      }
    } // for

    // if the list is empty return
    if (!listableObjects.length && !objectsWithListableContents.length) {
      return false;
    }

    if (0 < listableObjects.length) {
      output += "{We} can see ";
    }

    for (var i = 0; i < listableObjects.length; i++) {
      var object = listableObjects[i];

      if (listableObjects.length > 1 && i === listableObjects.length - 1) {
        output += " and ";
      }

      output += `${object.indefinite_article} `;

      let name = A.getSAF.call(this, object.name);
      if (object.print_bold) name = `<strong>${name}</strong>`;
      if (object.print_italic) name = `<em>${name}</em>`;
      if (object.print_style) {
        name = `<span class="${object.print_style}">${name}</span>`;
      }
      output += name;

      if (object.getVerbConnectionCount("tie", "to_iov")) {
        objectsTiedToOtherThings.push(object);
        output +=
          " (tied to " +
          this.game.getPrintableObjectList({
            objects: object.is.connected_by.tie.to_iov,
          }) +
          ")";
      }

      if (i === listableObjects.length - 1) {
        output += " here. ";
      } else {
        output += ", ";
      }
    }

    if (objectsWithListableContents.length > 0) {
      for (var i = 0; i < objectsWithListableContents.length; i++) {
        output += objectsWithListableContents[i].getPrintableListOfContents({
          caller: "room",
        });
      }
    }

    if (output) this.print(output);
  };
})();