(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
--- a/bin/ai/library/graph/aystar/library.nut Wed Jun 11 14:29:42 2008 +0000
+++ b/bin/ai/library/graph/aystar/library.nut Wed Jun 11 14:55:10 2008 +0000
@@ -4,7 +4,7 @@
function GetAuthor() { return "OpenTTD NoAI Developers Team"; }
function GetName() { return "AyStar"; }
function GetDescription() { return "An implementation of AyStar"; }
- function GetVersion() { return 2; }
+ function GetVersion() { return 3; }
function GetDate() { return "2008-06-11"; }
function CreateInstance() { return "AyStar"; }
}
--- a/bin/ai/library/graph/aystar/main.nut Wed Jun 11 14:29:42 2008 +0000
+++ b/bin/ai/library/graph/aystar/main.nut Wed Jun 11 14:55:10 2008 +0000
@@ -12,27 +12,41 @@
_cost_callback = null;
_estimate_callback = null;
_neighbours_callback = null;
+ _cost_callback_param = null;
+ _estimate_callback_param = null;
+ _neighbours_callback_param = null;
_open = null;
_closed = null;
_goals = null;
/**
* @param cost_callback A function that returns the cost of a path. It
- * should accept 2 parameters, old_path and new_node. old_path is an
- * instance of AyStar.Path, and new_node is the new node that is added to
- * that path. It should return the cost of the path including new_node.
+ * should accept three parameters, old_path, new_node and
+ * cost_callback_param. old_path is an instance of AyStar.Path, and
+ * new_node is the new node that is added to that path. It should return
+ * the cost of the path including new_node.
* @param estimate_callback A function that returns an estimate from a node
- * to the goal node. It should accept two parameters, node and goal_nodes.
- * It should return an estimate to the cost from the lowest cost between
- * node and any node out of goal_nodes. Note that this estimate is not
- * allowed to be higher than the real cost between node and any of
- * goal_nodes. A lower value is fine, however the closer it is to the real
- * value, the better the performance.
+ * to the goal node. It should accept three parameters, node, goal_nodes and
+ * estimate_callback_param. It should return an estimate to the cost from
+ * the lowest cost between node and any node out of goal_nodes. Note that
+ * this estimate is not allowed to be higher than the real cost between node
+ * and any of goal_nodes. A lower value is fine, however the closer it is to
+ * the real value, the better the performance.
* @param neighbours_callback A function that returns all neighbouring nodes
- * from a given node. It should accept two parameters, current_path and node,
- * and return an array containing all neighbouring nodes.
+ * from a given node. It should accept three parameters, current_path, node
+ * and neighbours_callback_param. It should return an array containing all
+ * neighbouring nodes.
+ * @param cost_callback_param This parameters will be passed to cost_callback
+ * as third parameter. Useful to send is an instance of an object.
+ * @param estimate_callback_param This parameters will be passed to
+ * estimate_callback as third parameter. Useful to send is an instance of an
+ * object.
+ * @param neighbours_callback_param This parameters will be passed to
+ * neighbours_callback as third parameter. Useful to send is an instance of
+ * an object.
*/
- constructor(cost_callback, estimate_callback, neighbours_callback)
+ constructor(cost_callback, estimate_callback, neighbours_callback, cost_callback_param = null,
+ estimate_callback_param = null, neighbours_callback_param = null)
{
if (typeof(cost_callback) != "function") throw("'cost_callback' has be a function-pointer.");
if (typeof(estimate_callback) != "function") throw("'estimate_callback' has be a function-pointer.");
@@ -41,6 +55,9 @@
this._cost_callback = cost_callback;
this._estimate_callback = estimate_callback;
this._neighbours_callback = neighbours_callback;
+ this._cost_callback_param = cost_callback_param;
+ this._estimate_callback_param = estimate_callback_param;
+ this._neighbours_callback_param = neighbours_callback_param;
}
/**
@@ -72,8 +89,8 @@
this._closed = AIList();
foreach (node in sources) {
- local new_path = this.Path(null, node, this._cost_callback);
- this._open.Insert(new_path, new_path.GetCost() + this._estimate_callback(node, goals));
+ local new_path = this.Path(null, node, this._cost_callback, this._cost_callback_param);
+ this._open.Insert(new_path, new_path.GetCost() + this._estimate_callback(node, goals, this._estimate_callback_param));
}
this._goals = goals;
@@ -98,12 +115,12 @@
}
this._closed.AddItem(cur_node, 0);
/* Scan all neighbours */
- local neighbours = this._neighbours_callback(path, cur_node);
+ local neighbours = this._neighbours_callback(path, cur_node, this._neighbours_callback_param);
foreach (node in neighbours) {
if (this._closed.HasItem(node)) continue;
/* Calculate the new paths and add them to the open list */
- local new_path = this.Path(path, node, this._cost_callback);
- this._open.Insert(new_path, new_path.GetCost() + this._estimate_callback(node, this._goals));
+ local new_path = this.Path(path, node, this._cost_callback, this._cost_callback_param);
+ this._open.Insert(new_path, new_path.GetCost() + this._estimate_callback(node, this._goals, this._estimate_callback_param));
}
}
@@ -131,11 +148,11 @@
_node = null;
_cost = null;
- constructor(old_path, new_node, cost_callback)
+ constructor(old_path, new_node, cost_callback, cost_callback_param)
{
this._prev = old_path;
this._node = new_node;
- this._cost = cost_callback(old_path, new_node);
+ this._cost = cost_callback(old_path, new_node, cost_callback_param);
};
/**
--- a/bin/ai/regression/regression.nut Wed Jun 11 14:29:42 2008 +0000
+++ b/bin/ai/regression/regression.nut Wed Jun 11 14:55:10 2008 +0000
@@ -1,6 +1,6 @@
import("queue.priority_queue", "PQ", 2);
import("queue.binary_heap", "BH", 1);
-import("graph.aystar", "AS", 2);
+import("graph.aystar", "AS", 3);
class Regression extends AIController {
function Start();
@@ -372,9 +372,9 @@
AIEventController.EnableAllEvents();
}
-function cost_callback(old_path, new_tile) { if (old_path == null) return 0; return old_path.GetCost() + 1; }
-function estimate_callback(tile, goals) { return goals[0] - tile; }
-function neighbours_callback(path, cur_tile) { return [cur_tile + 1]; }
+function cost_callback(old_path, new_tile, self) { if (old_path == null) return 0; return old_path.GetCost() + 1; }
+function estimate_callback(tile, goals, self) { return goals[0] - tile; }
+function neighbours_callback(path, cur_tile, self) { return [cur_tile + 1]; }
function Regression::Graph()
{