(svn r13461) [NoAI] -Fix: add A* to regression, so we test it a bit (very basic test ;)) noai
authortruebrain
Wed, 11 Jun 2008 12:13:22 +0000
branchnoai
changeset 10910 85f71706f496
parent 10909 3c573e607b60
child 10912 48d072a192b5
(svn r13461) [NoAI] -Fix: add A* to regression, so we test it a bit (very basic test ;))
bin/ai/library/graph/aystar/main.nut
bin/ai/regression/regression.nut
bin/ai/regression/regression.txt
--- a/bin/ai/library/graph/aystar/main.nut	Tue Jun 10 23:30:02 2008 +0000
+++ b/bin/ai/library/graph/aystar/main.nut	Wed Jun 11 12:13:22 2008 +0000
@@ -39,7 +39,7 @@
 	local open = Queue();
 	/* Create the initial open list */
 	foreach (tile in sources) {
-		local new_path = AyStar.Path(null, tile, cost_callback);
+		local new_path = this.Path(null, tile, cost_callback);
 		open.Insert(new_path, new_path.GetCost() + estimate_callback(tile, goals));
 	}
 	while (open.Count() > 0) {
@@ -58,7 +58,7 @@
 		foreach (tile in neighbours) {
 			if (closed.HasItem(tile)) continue;
 			/* Calculate the new paths and add them to the open list */
-			local new_path = AyStar.Path(path, tile, cost_callback);
+			local new_path = this.Path(path, tile, cost_callback);
 			open.Insert(new_path, new_path.GetCost() + estimate_callback(tile, goals));
 		}
 	}
--- a/bin/ai/regression/regression.nut	Tue Jun 10 23:30:02 2008 +0000
+++ b/bin/ai/regression/regression.nut	Wed Jun 11 12:13:22 2008 +0000
@@ -1,5 +1,6 @@
 import("queue.priority_queue", "PQ", 2);
 import("queue.binary_heap", "BH", 1);
+import("graph.aystar", "AS", 1);
 
 class Regression extends AIController {
 	function Start();
@@ -371,6 +372,22 @@
 	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 Regression::Graph()
+{
+	print("--AyStar--");
+	print("  Fastest path:");
+	local as = AS();
+	local path = as.FindPath([1], [10], cost_callback, estimate_callback, neighbours_callback);
+	while (path != null) {
+		print("    Tile " + path.GetTile());
+		path = path.GetParent();
+	}
+}
+
 function Regression::Industry()
 {
 	local j = 0;
@@ -1322,6 +1339,7 @@
 	this.Engine();
 	this.EngineList();
 	this.Event();
+	this.Graph();
 	this.Industry();
 	this.IndustryList();
 	this.Map();
--- a/bin/ai/regression/regression.txt	Tue Jun 10 23:30:02 2008 +0000
+++ b/bin/ai/regression/regression.txt	Wed Jun 11 12:13:22 2008 +0000
@@ -4712,6 +4712,18 @@
   EnableEvent(1):    done
   GetNextEvent:      instance
   GetNextEvent:      null
+--AyStar--
+  Fastest path:
+    Tile 10
+    Tile 9
+    Tile 8
+    Tile 7
+    Tile 6
+    Tile 5
+    Tile 4
+    Tile 3
+    Tile 2
+    Tile 1
 
 --Industry--
   GetMaxIndustryID():  71