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

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

  var p = adventurejs.Game.prototype;

  /**
   *
   * @method adventurejs.Game#listCurrentRoomContents
   * @memberOf adventurejs.Game
   * @param {Object} room
   * @returns {String}
   */
  p.listRoomContents = function Game_listRoomContents(room) {
    this.game.log("log", 1, "Game.listRoomContents", "Game");
    var roomContentsByID = A.clone.call(this.game, room.getAllContents()); // array of strings
    var listableObjects = [];
    var objectsWithListableContents = [];
    var objectsTiedToOtherThings = [];
    var output = "";

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

    // exclude exits from list ?

    // TODO needs to check against lighting and visibility

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

      if (!object) {
        this.game.log(
          "warn",
          "critical",
          "listRoomContents found an undefined object in room " +
            room.name +
            ".",
          "Game"
        );
        continue;
      }

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

      object.setKnown();
      object.setSeen();

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

      if (object.hasListableContents()) {
        if (object.is.listed_in_room || object.is.unlisted_but_list_children) {
          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;

      output += " ";

      var name = A.getSAF.call(this, object.name);
      /* TODO
          should check for descriptions.exits
          before defaulting to name

          2023.10.02 - is name a suitable field for getSAF?
      */

      if (object.print_bold) name = "<strong>" + name + "</strong>";
      if (object.print_italic) name = "<em>" + name + "</em>";
      output += name;

      if (object.dov.tie?.with_params.connections.length > 0) {
        objectsTiedToOtherThings.push(object);
        output +=
          " (tied to " +
          this.game.getPrintableObjectList({
            objects: object.dov.tie.with_params.connections,
          }) +
          ")";
      }

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

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

    return output;
  };
})();