// selectReachable.js
(function () {
/* global adventurejs A */
var p = adventurejs.Parser.prototype;
/**
* Exclude from a list of assets all assets that are not reachable by subject.
* @method adventurejs.Parser#selectReachable
* @memberOf adventurejs.Parser
* @param {Array} list
* @returns {Array}
*/
p.selectReachable = function Parser_selectReachable(list) {
if ("string" === typeof list) list = [list];
if (!Array.isArray(list)) {
this.game.log(
"L1096",
"error",
"critical",
["[selectReachable.js] selectReachable() received non-array", list],
"Parser"
);
return [];
}
this.game.log(
"L1183",
"log",
"high",
`[selectReachable.js] selectReachable() receive:\n${list}`,
"Parser"
);
var input = this.game.getInput();
var subject = input.getSubject();
var room = this.game.getRoom();
var nest = subject.getNest();
var nest_asset = subject.getNestAsset();
var nest_ancestor = this.game.getAsset(subject.getNestAnscestorId());
var foundObjects = [];
var roomObjects = [];
var containers = [];
for (var i = 0; i < list.length; i++) {
var object_parent, object_ancestor, object_ancestor;
var object = this.game.getAsset(list[i]);
if (!object) continue;
// abstractions are always reachable
if (object.is.abstract) {
foundObjects.push(list[i]);
continue;
}
// check if input is unparsed substance
if (object instanceof adventurejs.Substance) {
// foundObjects.push(list[i]);
// continue;
// console.warn( " - selectReachable " + object.id + " is substance" );
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;
containers.push(
roomObject.id + ":" + preposition + ":" + object.id
);
}
}
if (containers.length) {
containers = this.selectReachable(containers);
if (containers.length) {
foundObjects = foundObjects.concat(containers);
}
}
continue;
}
// if object is a plug, test against its parent
if (object.isConnectedToAsset("plug", object.getPlaceAsset(), "to_dov")) {
object = object.getPlaceAsset();
}
object_parent = object.getPlaceAsset();
object_ancestor = this.game.getAsset(object.getAncestorId());
// subject is carrying it, ok
if (object.isWithin(subject)) {
foundObjects.push(list[i]);
continue;
}
// subject is in it, ok
if (nest && object.id === nest_asset.id) {
foundObjects.push(list[i]);
continue;
}
// @WATCH
// directions are always reachable...
// ...and let tryTravel handle reachability?
if (object.direction) {
foundObjects.push(list[i]);
continue;
}
// if subject is nested and trying to get up/down
if ((nest && object.direction === "down") || object.direction === "up") {
foundObjects.push(list[i]);
continue;
}
// if subject is in/on/under/behind something
if (
nest &&
// subject is not nested in object
object.id !== nest_asset.id &&
// it's not top of the nest
object.id !== nest_ancestor.id &&
// it's not in inventory
//&& object_ancestor.id !== subject.id
// it's not nested in same container as subject
object_parent.id !== nest_asset.id &&
// it's not nested in same container as subject
object_ancestor.id !== nest_ancestor.id &&
// it's not explicitly reachable from thing subject is nested in
!nest.canSubjectReachAssetFromNest(object) &&
// it's not inside another thing that is
// explicitly reachable from thing subject is nested in
!nest.canSubjectReachAssetFromNest(object_ancestor)
) {
// console.warn("reachable excluded", object.id);
continue;
}
// is subject on the floor, but object not on the floor?
// this check might be too restrictive
if (
!nest &&
subject.isOnFloor() &&
!(object instanceof adventurejs.Floor) &&
object.getPlaceAssetId() !== subject.getPlaceAssetId()
) {
continue;
}
// is subject at different y height?
// figure out y overlap
var range = object.getYRange();
// is subject close enough on y?
if (subject.position.y < range.min || subject.position.y > range.max) {
continue;
}
/*
// TODO selectReachable
on top of a tall object?
behind glass?
inside something that's closed?
*/
foundObjects.push(list[i]);
}
//console.warn( "selectReachable returned " + JSON.stringify(foundObjects) );
return foundObjects;
};
})();