// selectReachableIfTangible.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#selectReachableIfTangible
* @memberOf adventurejs.Parser
* @param {Array} list
* @returns {Array}
*/
p.selectReachableIfTangible = function Parser_selectReachableIfTangible(
list
) {
if ("string" === typeof list) list = [list];
if (!Array.isArray(list)) {
this.game.log(
"L1259",
"error",
"critical",
[
"[selectReachableIfTangible.js] selectReachableIfTangible() received non-array",
list,
],
"Parser"
);
return [];
}
this.game.log(
"L1185",
"log",
"high",
`[selectReachableIfTangible.js] selectReachableIfTangible() receive:\n${list}`,
"Parser"
);
var input = this.game.getInput();
var subject = input.getSubject();
var nest = subject.getNest();
var nest_asset = subject.getNestAsset();
var nest_ancestor = this.game.getAsset(subject.getNestAnscestorId());
var foundObjects = [];
for (var i = 0; i < list.length; i++) {
var object, object_parent, object_ancestor, object_ancestor;
object = this.game.getAsset(list[i]);
if (!object) continue;
if (object instanceof adventurejs.Substance) {
// automatically return substances as present
foundObjects.push(list[i]);
continue;
}
// abstractions are always reachable
if (object.is.abstract) {
foundObjects.push(list[i]);
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 ) ) {
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()
) {
// console.warn(
// "Exclude " + object.name + " because subject is on the floor."
// );
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) {
// console.warn(
// "Exclude " + object.name + " because subject at different y."
// );
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;
};
})();