author | truebrain |
Thu, 12 Jun 2008 19:47:02 +0000 | |
branch | noai |
changeset 10942 | cd3f2d07199f |
parent 10918 | 317f736e5fc6 |
child 11039 | a45899beee2a |
permissions | -rw-r--r-- |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
1 |
/* $Id$ */ |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
2 |
|
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
3 |
/** |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
4 |
* An AyStar implementation. |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
5 |
* It solves graphs by finding the fastest route from one point to the other. |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
6 |
*/ |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
7 |
class AyStar |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
8 |
{ |
10942
cd3f2d07199f
(svn r13496) [NoAI] -Fix: if a library depends on an other library, the import became globally known, which defeats the idea of imports. They are now restricted to their scope, and 'import' returns the class of import (if any)
truebrain
parents:
10918
diff
changeset
|
9 |
_queue_class = import("queue.binary_heap", "", 1); |
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
10 |
_cost_callback = null; |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
11 |
_estimate_callback = null; |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
12 |
_neighbours_callback = null; |
10918
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
13 |
_cost_callback_param = null; |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
14 |
_estimate_callback_param = null; |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
15 |
_neighbours_callback_param = null; |
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
16 |
_open = null; |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
17 |
_closed = null; |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
18 |
_goals = null; |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
19 |
|
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
20 |
/** |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
21 |
* @param cost_callback A function that returns the cost of a path. It |
10918
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
22 |
* should accept three parameters, old_path, new_node and |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
23 |
* cost_callback_param. old_path is an instance of AyStar.Path, and |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
24 |
* new_node is the new node that is added to that path. It should return |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
25 |
* the cost of the path including new_node. |
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
26 |
* @param estimate_callback A function that returns an estimate from a node |
10918
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
27 |
* to the goal node. It should accept three parameters, node, goal_nodes and |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
28 |
* estimate_callback_param. It should return an estimate to the cost from |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
29 |
* the lowest cost between node and any node out of goal_nodes. Note that |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
30 |
* this estimate is not allowed to be higher than the real cost between node |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
31 |
* and any of goal_nodes. A lower value is fine, however the closer it is to |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
32 |
* the real value, the better the performance. |
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
33 |
* @param neighbours_callback A function that returns all neighbouring nodes |
10918
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
34 |
* from a given node. It should accept three parameters, current_path, node |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
35 |
* and neighbours_callback_param. It should return an array containing all |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
36 |
* neighbouring nodes. |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
37 |
* @param cost_callback_param This parameters will be passed to cost_callback |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
38 |
* as third parameter. Useful to send is an instance of an object. |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
39 |
* @param estimate_callback_param This parameters will be passed to |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
40 |
* estimate_callback as third parameter. Useful to send is an instance of an |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
41 |
* object. |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
42 |
* @param neighbours_callback_param This parameters will be passed to |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
43 |
* neighbours_callback as third parameter. Useful to send is an instance of |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
44 |
* an object. |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
45 |
*/ |
10918
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
46 |
constructor(cost_callback, estimate_callback, neighbours_callback, cost_callback_param = null, |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
47 |
estimate_callback_param = null, neighbours_callback_param = null) |
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
48 |
{ |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
49 |
if (typeof(cost_callback) != "function") throw("'cost_callback' has be a function-pointer."); |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
50 |
if (typeof(estimate_callback) != "function") throw("'estimate_callback' has be a function-pointer."); |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
51 |
if (typeof(neighbours_callback) != "function") throw("'neighbours_callback' has be a function-pointer."); |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
52 |
|
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
53 |
this._cost_callback = cost_callback; |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
54 |
this._estimate_callback = estimate_callback; |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
55 |
this._neighbours_callback = neighbours_callback; |
10918
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
56 |
this._cost_callback_param = cost_callback_param; |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
57 |
this._estimate_callback_param = estimate_callback_param; |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
58 |
this._neighbours_callback_param = neighbours_callback_param; |
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
59 |
} |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
60 |
|
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
61 |
/** |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
62 |
* Initialize a path search between sources and goals. |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
63 |
* @param sources The source nodes. |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
64 |
* @param goals The target nodes. |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
65 |
*/ |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
66 |
function InitializePath(sources, goals); |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
67 |
|
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
68 |
/** |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
69 |
* Try to find the path as indicated with InitializePath with the lowest cost. |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
70 |
* @param iterations After how many iterations it should abort for a moment. |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
71 |
* This value should either be -1 for infinite, or > 0. Any other value |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
72 |
* aborts immediatly and will never find a path. |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
73 |
* @return A route if one was found, or false if the amount of iterations was |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
74 |
* reached, or null if no path was found. |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
75 |
* You can call this function over and over as long as it returns false, |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
76 |
* which is an indication it is not yet done looking for a route. |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
77 |
*/ |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
78 |
function FindPath(iterations); |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
79 |
} |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
80 |
|
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
81 |
function AyStar::InitializePath(sources, goals) |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
82 |
{ |
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
83 |
if (typeof(sources) != "array" || sources.len() == 0) throw("sources has be a non-empty array."); |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
84 |
if (typeof(goals) != "array" || goals.len() == 0) throw("goals has be a non-empty array."); |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
85 |
|
10942
cd3f2d07199f
(svn r13496) [NoAI] -Fix: if a library depends on an other library, the import became globally known, which defeats the idea of imports. They are now restricted to their scope, and 'import' returns the class of import (if any)
truebrain
parents:
10918
diff
changeset
|
86 |
this._open = this._queue_class(); |
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
87 |
this._closed = AIList(); |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
88 |
|
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
89 |
foreach (node in sources) { |
10918
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
90 |
local new_path = this.Path(null, node, this._cost_callback, this._cost_callback_param); |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
91 |
this._open.Insert(new_path, new_path.GetCost() + this._estimate_callback(node, goals, this._estimate_callback_param)); |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
92 |
} |
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
93 |
|
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
94 |
this._goals = goals; |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
95 |
} |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
96 |
|
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
97 |
function AyStar::FindPath(iterations) |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
98 |
{ |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
99 |
if (this._open == null) throw("can't execute over an uninitialized path"); |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
100 |
|
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
101 |
while (this._open.Count() > 0 && (iterations == -1 || iterations-- > 0)) { |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
102 |
/* Get the path with the best score so far */ |
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
103 |
local path = this._open.Pop(); |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
104 |
local cur_node = path.GetNode(); |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
105 |
/* Make sure we didn't already passed it */ |
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
106 |
if (this._closed.HasItem(cur_node)) continue; |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
107 |
/* Check if we found the end */ |
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
108 |
foreach (node in this._goals) { |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
109 |
if (cur_node == node) { |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
110 |
this._CleanPath(); |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
111 |
return path; |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
112 |
} |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
113 |
} |
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
114 |
this._closed.AddItem(cur_node, 0); |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
115 |
/* Scan all neighbours */ |
10918
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
116 |
local neighbours = this._neighbours_callback(path, cur_node, this._neighbours_callback_param); |
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
117 |
foreach (node in neighbours) { |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
118 |
if (this._closed.HasItem(node)) continue; |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
119 |
/* Calculate the new paths and add them to the open list */ |
10918
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
120 |
local new_path = this.Path(path, node, this._cost_callback, this._cost_callback_param); |
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
121 |
this._open.Insert(new_path, new_path.GetCost() + this._estimate_callback(node, this._goals, this._estimate_callback_param)); |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
122 |
} |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
123 |
} |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
124 |
|
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
125 |
if (this._open.Count() > 0) return false; |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
126 |
this._CleanPath(); |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
127 |
return null; |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
128 |
} |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
129 |
|
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
130 |
function AyStar::_CleanPath() |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
131 |
{ |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
132 |
this._closed = null; |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
133 |
this._open = null; |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
134 |
this._goals = null; |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
135 |
} |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
136 |
|
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
137 |
/** |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
138 |
* The path of the AyStar algorithm. |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
139 |
* It is reversed, that is, the first entry is more close to the goal-nodes |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
140 |
* than his GetParent(). You can walk this list to find the whole path. |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
141 |
* The last entry has a GetParent() of null. |
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
142 |
*/ |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
143 |
class AyStar.Path |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
144 |
{ |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
145 |
_prev = null; |
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
146 |
_node = null; |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
147 |
_cost = null; |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
148 |
|
10918
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
149 |
constructor(old_path, new_node, cost_callback, cost_callback_param) |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
150 |
{ |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
151 |
this._prev = old_path; |
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
152 |
this._node = new_node; |
10918
317f736e5fc6
(svn r13470) [NoAI] -Change [Library CHANGE]: allow in graph.aystar to give a custom param to the callbacks, so you can send in an instance of yourself
truebrain
parents:
10912
diff
changeset
|
153 |
this._cost = cost_callback(old_path, new_node, cost_callback_param); |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
154 |
}; |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
155 |
|
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
156 |
/** |
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
157 |
* Return the node where this (partial-)path ends. |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
158 |
*/ |
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
159 |
function GetNode() { return this._node; } |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
160 |
|
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
161 |
/** |
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
162 |
* Return an instance of this class leading to the previous node. |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
163 |
*/ |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
164 |
function GetParent() { return this._prev; } |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
165 |
|
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
166 |
/** |
10912
48d072a192b5
(svn r13463) [NoAI] -Change [Library CHANGE]: AyStar is now more object oriented, and you can indicate the amount of iterations FindPath should do in one go (tnx to Yexo and TrueBrain)
truebrain
parents:
10910
diff
changeset
|
167 |
* Return the cost of this (partial-)path from the beginning up to this node. |
10909
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
168 |
*/ |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
169 |
function GetCost() { return this._cost; } |
3c573e607b60
(svn r13460) [NoAI] -Add [Library]: added graph.aystar, an A* implementation (Yexo)
truebrain
parents:
diff
changeset
|
170 |
} |