forked from EricEve/adv3lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathfind.t
304 lines (249 loc) · 10.6 KB
/
pathfind.t
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#charset "us-ascii"
#include "advlite.h"
/* Abstract pathfinder */
class Pathfinder: object
/*
* When populated the pathsFound will contain a Vector of path Vectors,
* each path Vector comprising a series of two element lists, the first
* element describing the route taken and the second the destination
* arrived at (e.g. [northDir, hall] meaning go north to reach the hall).
*/
pathsFound = nil
/*
* The number of steps we have tried so far. We start with 1, being the
* null step to our starting point.
*/
steps = 1
/*
* A Vector containing all the nodes we have visited so far in our attempt
* to find a route. This enables us to cull paths that lead somewhere
* we've already been.
*/
nodesVisited = nil
findPath(start, target)
{
/*
* Initiate the search by setting up the Vectors we need and
* populating them with the null route to our starting point.
*/
cachedRoute = nil;
currentDestination = target;
pathsFound = new Vector(20);
nodesVisited = new Vector(20);
local newPath = new Vector(2);
newPath.append([nil, start]);
pathsFound.append(newPath);
nodesVisited.append(start);
steps = 1;
if(start == target)
return newPath;
/*
* To find the path we take a step out from our starting point through
* all available routes. We note the route we took and where we
* arrived at as a set of new paths building on our existing paths. We
* then discard all paths that are shorter than the number of steps we
* have now taken and look for one among the remainder that arrives at
* our target destination. If we find one, we return it. If not, we
* remove all paths that lead to destinations we have visited before,
* and then try taking another step, noting the destinations to which
* it leads. Repeat until we either find a path to our target or we
* run out of new paths to try.
*/
while(pathsFound.length > 0)
{
takeOneStep();
/* cull all paths that are shorter than steps long */
pathsFound = pathsFound.subset({x: x.length == steps});
/* see if any of the paths we've found lead to our target */
local pathFound = pathsFound.valWhich({x: x[steps][2] == target} );
if(pathFound != nil)
{
cachedRoute = pathFound;
return pathFound;
}
/* remove all paths that end in nodes we've already visited */
pathsFound = pathsFound.subset({x: nodesVisited.indexOf(x[steps][2])
== nil});
/* note which nodes have now been visited */
foreach(local cur in pathsFound)
nodesVisited.append(cur[steps][2]);
}
return nil;
}
takeOneStep()
{
/* Note that we've taken another step out from our starting point */
steps ++;
/*
* Copy the existing paths into a temporary Vector, since we're about
* to add to them and we only want to iterate over the existing list.
*/
local temp = new Vector(pathsFound);
/*
* For each existing route, see what happens if we advance one more
* step in every available direction and add the new routes to our
* list of paths.
*/
foreach(local cur in temp)
findDestinations(cur);
}
/* Find all the destinations one step away from cur */
findDestinations(cur)
{
/* Specific instances must define how this is done */
}
/* The most recently calculated route */
cachedRoute = nil
/* The destination of the most recently calculated route. */
currentDestination = nil
;
/*
* A Pathfinder specialized for finding a route through the game map. Note
* that this can only find a route through TravelConnector objects (which
* includes direction properties attached to Rooms, Doors and other
* TravelConnectors).
*/
routeFinder: Pathfinder
findDestinations(cur)
{
/* Note the location our current path leads to */
local loc = cur[steps - 1][2];
/* See what leads in every available direction from this location */
for(local dir = firstObj(Direction); dir != nil ; dir = nextObj(dir,
Direction))
{
local newPath = new Vector(cur);
/*
* If the direction property points to an object, see if it points
* to a valid path.
*/
if(loc.propType(dir.dirProp) == TypeObject)
{
local obj = loc.(dir.dirProp);
/*
* if the object is a locked door and we want to exclude
* locked doors, or if there's some other reason the actor
* cannot pass this way, we can't use this path.
*/
if(excludeLockedDoors &&
(obj.isLocked
|| obj.canTravelerPass(gActor) == nil
|| valToList(obj.travelBarriers).indexWhich(
{ b: !b.canTravelerPass(gActor, obj)}) != nil))
return;
/*
* If it leads to a non-nil destination note the path to this
* object. This will be the path that got us to this location
* plus the one additional step.
*/
local dest = loc.(dir.dirProp).getDestination(loc);
if(dest != nil)
{
newPath.append([dir, dest]);
pathsFound.append(newPath);
}
}
/*
* if the direction property points to code, see if it provides a
* valid path.
*/
if(loc.propType(dir.dirProp) == TypeCode)
{
/* first look up the destination this code takes the actor to */
local dest = libGlobal.extraDestInfo[[loc, dir]];
/*
* the destination is only of interest if it's not nowhere,
* the default unknown destination, or the location we're
* trying to leave.
*
*
* if it's none of these, add it to the list of possible paths
*
*/
if(dest not in (nil, loc, unknownDest_, varDest_))
{
newPath.append([dir, dest]);
pathsFound.append(newPath);
}
}
}
}
excludeLockedDoors = true
;
/*
* The pcRouteFinder works exactly the same as the more general routeFinder
* except that it finds routes only through TravelConnectors whose
* destinations are known.
*/
pcRouteFinder: Pathfinder
findDestinations(cur)
{
/* Note the location our current path leads to */
local loc = cur[steps - 1][2];
/* See what leads in every available direction from this location */
for(local dir = firstObj(Direction); dir != nil ; dir = nextObj(dir,
Direction))
{
local newPath = new Vector(cur);
/*
* If the direction property points to an object, see if it points
* to a valid path.
*/
if(loc.propType(dir.dirProp) == TypeObject)
{
local conn = loc.(dir.dirProp);
/*
* If it leads to a non-nil destination that the pc knowns,
* note the path to this object. This will be the path that
* got us to this location plus the one additional step.
*/
local dest = conn.getDestination(loc);
/*
* if both the location (loc) and the destination (dest) lie
* in the same familiar region, then assume the pc knows
* his/her way between the two rooms and so set
* isDestinationKnown to true
*/
if(!conn.isDestinationKnown &&
loc.regionsInCommonWith(dest).indexWhich(
{x: x.familiar}) != nil)
conn.isDestinationKnown = true;
/*
* if the connector leads to a known destination then add the
* direction and its destination to a new path
*/
if(dest != nil && conn.isDestinationKnown)
{
newPath.append([dir, dest]);
pathsFound.append(newPath);
}
}
/*
* if the direction property points to code, see if it provides a
* valid path.
*/
if(loc.propType(dir.dirProp) == TypeCode)
{
/* first look up the destination this code takes the actor to */
local dest = libGlobal.extraDestInfo[[loc, dir]];
/*
* the destination is only of interest if it's not nowhere,
* the default unknown destination, or the location we're
* trying to leave.
*
*
* if it's none of these, add it to the list of possible paths
* (The fact that it's none of these implies that the
* destination is known so we don't need to apply any further
* tests to check that).
*
*/
if(dest not in (nil, loc, unknownDest_, varDest_))
{
newPath.append([dir, dest]);
pathsFound.append(newPath);
}
}
}
}
;