// selectPresent.js
(function () {
/* global adventurejs A */
var p = adventurejs.Parser.prototype;
/**
* Exclude from a list of assets all assets that are not present in current room.
* @method adventurejs.Parser#selectPresent
* @memberOf adventurejs.Parser
* @param {Array} list
* @returns {Array}
*/
p.selectPresent = function Parser_selectPresent(list) {
if ("string" === typeof list) list = [list];
if (!Array.isArray(list)) {
this.game.log(
"L1240",
"error",
"critical",
["[selectPresent.js] selectPresent() received non-array", list],
"Parser"
);
return [];
}
this.game.log(
"L1114",
"log",
"high",
`[selectPresent.js] selectPresent() receive:\n${list}`,
"Parser"
);
var input = this.game.getInput();
var subject = input.getSubject();
var room = this.game.getRoom();
var foundObjects = [];
var roomObjects = [];
for (var i = 0; i < list.length; i++) {
var object;
object = this.game.getAsset(list[i]);
if (!object) continue;
// abstractions are always present
if (object.is.abstract) {
foundObjects.push(list[i]);
continue;
}
// check if input is unparsed substance
// I've commented and uncommented this a couple of times.
// Latest uncomment on 2023.04.08
// Needed in order to handle "water" as a substance
// specifically in the case of "pour water" while player
// is holding a bowl full of water.
if (object instanceof adventurejs.Substance) {
// this version automatically returns substances as present
// foundObjects.push( list[i] );
// continue;
// this version looks for substance containers
if (!roomObjects.length) {
roomObjects = [room.id].concat(room.getAllNestedContents());
}
for (var j = 0; j < roomObjects.length; j++) {
var roomObject = this.game.getAsset(roomObjects[j]);
if (!subject.knowsAbout(roomObject)) continue;
var preposition = roomObject.containsSubstance(object.id);
if (preposition) {
// becomes for ex: bowl:in:water which can be handled by verbs
if (!subject.knowsAbout(`${roomObject.id}|${preposition}|vessel`))
continue;
foundObjects.push(
roomObject.id + ":" + preposition + ":" + object.id
);
}
}
}
// walls, floor, ceiling, scenery
// all prefixed with "global_"
if (object.is.global) {
if (
object instanceof adventurejs.GlobalString ||
object instanceof adventurejs.GlobalNumber
) {
foundObjects.push(list[i]);
continue;
}
if (object instanceof adventurejs.Floor) {
if (this.game.getRoom().findNestedAssetsWithClass("Floor").length)
continue;
}
// room says yes
if (
"undefined" !== typeof room.room_scenery[object.id] &&
true === room.room_scenery[object.id].enabled
) {
foundObjects.push(list[i]);
// console.warn(
// "selectPresent > true === room.room_scenery[ object.id ].enabled"
// );
continue;
}
// room says no
if (
"undefined" !== typeof room.room_scenery[object.id] &&
false === room.room_scenery[object.id].enabled
) {
console.warn(
"selectPresent > false === room.room_scenery[ object.id ].enabled"
);
continue;
}
// room says neither true nor false
// if room has no true / false setting, check for zone
// zone says yes
if (
"undefined" !== typeof this.game.world[room.zone] &&
"undefined" !==
typeof this.game.world[room.zone].zone_scenery[object.id] &&
true === this.game.world[room.zone].zone_scenery[object.id].enabled
) {
foundObjects.push(list[i]);
console.warn(
"selectPresent > true === this.game.world[ room.zone ].zone_scenery[ object.id ].enabled"
);
continue;
}
// zone says no
if (
"undefined" !== typeof this.game.world[room.zone] &&
"undefined" !==
typeof this.game.world[room.zone].zone_scenery[object.id] &&
false === this.game.world[room.zone].zone_scenery[object.id].enabled
) {
console.warn(
"selectPresent > false === room.zone.zone_scenery[ object.id ].enabled"
);
continue;
}
// zone says neither true nor false
if (object.enabled) {
console.warn("selectPresent > true === object.enabled");
foundObjects.push(list[i]);
}
continue;
}
// exclude global exit if there is a local exit with its direction
if (
object instanceof adventurejs.Exit &&
object.is.global &&
"undefined" !== typeof room.exits[object.direction]
) {
continue;
}
// include global exit if there is no local exit with its direction
if (
object instanceof adventurejs.Exit &&
object.is.global &&
"undefined" === typeof room.exits[object.direction]
) {
foundObjects.push(list[i]);
continue;
}
// never return exits from other rooms
if (
object instanceof adventurejs.Exit &&
!object.is.global &&
object.getPlaceAssetId() !== this.game.world._room
) {
continue;
}
if (object.id === this.game.world._room) {
foundObjects.push(list[i]);
continue;
}
if (object.isWithin && object.isWithin(this.game.getRoom())) {
foundObjects.push(list[i]);
continue;
}
// for substances
// examine every item in the room for substance container
//if( object.contains_substance)
// if there's a global object, list that
// if( -1 < object.id.indexOf( "global_" ) ) {
// //console.log( "selectPresent found " + object.name );
// foundObjects.push( list[i] );
// continue;
// }
}
//console.warn("selectPresent returned " + JSON.stringify(foundObjects));
return foundObjects;
};
})();