forked from textadventures/questkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.scopes.js
143 lines (122 loc) · 3.84 KB
/
core.scopes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* jshint quotmark: single */
(function () {
'use strict';
questkit.scopeCommands = function () {
var result = [];
var povParent = questkit.povParent();
questkit.allCommands().forEach(function (cmd) {
var parent = get(cmd, 'parent');
if (!parent || parent == povParent) {
result.push(cmd);
}
});
return result;
};
questkit.scopeVisible = function () {
return questkit.scopeVisibleNotHeld().concat(questkit.scopeInventory());
};
questkit.scopeVisibleNotHeld = function () {
var povParent = questkit.povParent();
return questkit.scopeReachableNotHeldForLocation(povParent).concat(questkit.scopeVisibleNotReachableForLocation(povParent));
};
questkit.scopeReachableNotHeldForLocation = function (location) {
var result = [];
var pov = get('pov');
questkit.getAllChildObjects(location).forEach(function (object) {
if (questkit.containsReachable(location, object) && object != pov && !questkit.contains(pov, object)) {
result.push(object);
}
});
if (location == get(pov, 'parent')) {
result.push(pov);
}
return result;
};
var getChildObjects = function (object, recurse) {
var result = [];
questkit.allObjects().forEach(function (child) {
if (get(child, 'parent') == object) {
result.push(child);
if (recurse) result = result.concat(getChildObjects(child, true));
}
});
return result;
};
questkit.getAllChildObjects = function (object) {
return getChildObjects(object, true);
};
questkit.getDirectChildren = function (object) {
return getChildObjects(object, false);
};
questkit.containsVisible = function (parent, search) {
return containsAccessible(parent, search, false);
};
questkit.containsReachable = function (parent, search) {
return containsAccessible(parent, search, true);
};
var containsAccessible = function (parent, search, onlyReachable) {
var searchParent = get(search, 'parent');
if (!searchParent) return false;
if (get(search, 'visible') === false) return false;
if (get(parent, 'darklevel') && !get(search, 'lightsource')) return false;
if (searchParent == parent) return true;
var canAdd;
if (onlyReachable) {
canAdd = questkit.canReachThrough(searchParent);
}
else {
canAdd = questkit.canSeeThrough(searchParent);
}
if (canAdd) {
return (questkit.containsAccessible(parent, searchParent, onlyReachable));
} else {
return false;
}
};
questkit.canSeeThrough = function (object) {
return (get(object, 'transparent') || questkit.canReachThrough(object)) && !get(object, 'hidechildren');
};
questkit.canReachThrough = function (object) {
return get(object, 'isopen') && !get(object, 'hidechildren');
};
questkit.contains = function (parent, search) {
var searchParent = get(search, 'parent');
if (!searchParent) return false;
if (searchParent == parent) return true;
return questkit.contains(parent, searchParent);
};
questkit.scopeVisibleNotReachableForLocation = function (location) {
// TODO
return [];
};
questkit.scopeInventory = function () {
var result = [];
var pov = get('pov');
questkit.getAllChildObjects(pov).forEach(function (object) {
if (questkit.containsVisible(pov, object)) {
result.push(object);
}
});
return result;
};
questkit.scopeExits = function () {
return questkit.scopeExitsForLocation(questkit.povParent());
};
questkit.scopeExitsForLocation = function (location) {
var result = [];
questkit.allExits().forEach(function (exit) {
if (get(exit, 'parent') != location) return;
if (get(exit, 'visible') === false) return;
if (get(location, 'darklevel') && !get(exit, 'lightsource')) return;
result.push(exit);
});
return result;
};
questkit.getNonTransparentParent = function (object) {
if (get(object, 'transparent')) {
var parent = get(object, 'parent');
if (parent) return questkit.getNonTransparentParent(parent);
}
return object;
};
})();