// 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 = subject.getRoom();
var nest = subject.getNest();
var nest_asset = subject.getNestAsset();
// var nest_ancestor = this.game.getAsset(subject.getNestAnscestorId());
var nest_ancestor_id = subject.getNestAnscestorId();
var foundObjects = [];
var roomObjects = [];
var containers = [];
var dispensers = [];
for (var i = 0; i < list.length; i++) {
var object_parent, object_ancestor_id;
var object = this.game.getAsset(list[i]);
if (!object) continue;
// if it's platonic, look for dispensers
// we must ask this first because platonic assets are missing
// most asset methods and may throw errors in response to
// some of the other questions
if (object.is.platonic && object.hasDispensers()) {
for (const i in object.fungible.dispensers) {
const dispenser = object.fungible.dispensers[i];
const [dispenser_aspect, dispenser_id] = Object.entries(dispenser)[0];
const select = this.selectReachable([dispenser_id]);
if (select.length) {
const quad = `fungible:${object.fungible.dispense_class}:${dispenser_aspect}:${dispenser_id}`;
foundObjects.push(quad);
}
}
continue;
}
// abstractions are always reachable
if (object.is.abstract) {
foundObjects.push(list[i]);
continue;
}
// check if input is unparsed substance
if (object instanceof AdventureJS.Assets.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:substance which can be handled by verbs
if (!subject.knowsAbout(`${roomObject.id}|${preposition}|vessel`)) {
continue;
}
containers.push(
`substance:${object.id}:${preposition}:${roomObject.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());
object_ancestor_id = 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) {
const object_is_nest = object.id === nest_asset.id;
const object_contains_nest = object.id === nest_ancestor_id;
const object_parent_is_nest = object_parent.id === nest_asset.id;
const common_anscestor =
object_ancestor_id &&
nest_ancestor_id &&
object_ancestor_id === nest_ancestor_id;
const can_reach_object = nest.canSubjectReachAssetFromNest(object);
const can_reach_anscestor =
object_ancestor_id &&
nest.canSubjectReachAssetFromNest(object_ancestor_id);
if (
// subject is not nested in object
!object_is_nest &&
// it's not top of the nest
!object_contains_nest &&
// it's not in inventory
//&& object_ancestor.id !== subject.id
// it's not nested in same container as subject
!object_parent_is_nest &&
// it's not nested in same container as subject
// object_ancestor.id !== nest_ancestor_id &&
!common_anscestor &&
// it's not explicitly reachable from thing subject is nested in
!can_reach_object &&
// it's not inside another thing that is
// explicitly reachable from thing subject is nested in
!can_reach_anscestor
) {
// 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.Assets.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]);
}
this.game.log(
"L1643",
"log",
"high",
`[selectReachable.js] selectReachable() return:\n${foundObjects}`,
"Parser"
);
return foundObjects;
};
})();