(svn r10564) [NoAI] -Add: added a AITileList valuator that checks for a NxM buildable spot with the entry from the AITileList as top-left tile noai
authortruelight
Sat, 14 Jul 2007 21:15:49 +0000
branchnoai
changeset 9657 f2c6e332d8bc
parent 9656 ddb4260f3051
child 9658 e7675771bca4
(svn r10564) [NoAI] -Add: added a AITileList valuator that checks for a NxM buildable spot with the entry from the AITileList as top-left tile
bin/ai/regression/regression.nut
bin/ai/regression/regression.txt
src/ai/ai_squirrel.cpp
src/ai/api/ai_tilelist_valuator.cpp
src/ai/api/ai_tilelist_valuator.hpp
src/ai/api/ai_tilelist_valuator.hpp.sq
--- a/bin/ai/regression/regression.nut	Sat Jul 14 14:56:26 2007 +0000
+++ b/bin/ai/regression/regression.nut	Sat Jul 14 21:15:49 2007 +0000
@@ -500,6 +500,11 @@
 	print("  KeepValue(1):        done");
 	print("  Count():             " + list.Count());
 
+	list.Valuate(AITileListBuildableRectangle(3, 3));
+	print("  BuildableRectangle(3, 3) ListDump:");
+	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
+		print("    " + i + " => " + list.GetValue(i));
+	}
 	list.Valuate(AITileListDistanceManhattanToTile(30000));
 	print("  DistanceManhattanToTile(30000) ListDump:");
 	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
--- a/bin/ai/regression/regression.txt	Sat Jul 14 14:56:26 2007 +0000
+++ b/bin/ai/regression/regression.txt	Sat Jul 14 21:15:49 2007 +0000
@@ -1027,6 +1027,40 @@
   Buildable():         done
   KeepValue(1):        done
   Count():             33
+  BuildableRectangle(3, 3) ListDump:
+    42415 => 1
+    42414 => 1
+    42413 => 1
+    42410 => 1
+    42409 => 1
+    42159 => 1
+    42158 => 1
+    42157 => 1
+    42156 => 1
+    42153 => 1
+    41903 => 1
+    41902 => 1
+    41901 => 1
+    41900 => 1
+    41899 => 1
+    41897 => 1
+    41647 => 1
+    41646 => 1
+    41645 => 1
+    41644 => 1
+    41643 => 1
+    41641 => 1
+    41391 => 1
+    41390 => 1
+    41389 => 1
+    41388 => 1
+    41387 => 1
+    42408 => 0
+    42407 => 0
+    42151 => 0
+    41895 => 0
+    41639 => 0
+    41383 => 0
   DistanceManhattanToTile(30000) ListDump:
     42415 => 175
     42414 => 174
--- a/src/ai/ai_squirrel.cpp	Sat Jul 14 14:56:26 2007 +0000
+++ b/src/ai/ai_squirrel.cpp	Sat Jul 14 21:15:49 2007 +0000
@@ -237,6 +237,7 @@
 	SQAIStationRegister(this->engine);
 	SQAIStationVehicleListRegister(this->engine);
 	SQAITestModeRegister(this->engine);
+	SQAITileListBuildableRectangleRegister(this->engine);
 	SQAITileListBuildableRegister(this->engine);
 	SQAITileListCargoAcceptanceRegister(this->engine);
 	SQAITileListDistanceManhattanToTileRegister(this->engine);
--- a/src/ai/api/ai_tilelist_valuator.cpp	Sat Jul 14 14:56:26 2007 +0000
+++ b/src/ai/api/ai_tilelist_valuator.cpp	Sat Jul 14 21:15:49 2007 +0000
@@ -10,6 +10,22 @@
 	return AITile::IsBuildable(tile);
 }
 
+int32 AITileListBuildableRectangle::Valuate(int32 tile) const
+{
+	uint tx, ty;
+
+	tx = AIMap::GetTileX(tile);
+	ty = AIMap::GetTileY(tile);
+
+	for (uint x = tx; x < this->width + tx; x++) {
+		for (uint y = ty; y < this->height + ty; y++) {
+			if (!AITile::IsBuildable(AIMap::GetTileIndex(x, y))) return false;
+		}
+	}
+
+	return true;
+}
+
 int32 AITileListSlope::Valuate(int32 tile) const
 {
 	return AITile::GetSlope(tile);
--- a/src/ai/api/ai_tilelist_valuator.hpp	Sat Jul 14 14:56:26 2007 +0000
+++ b/src/ai/api/ai_tilelist_valuator.hpp	Sat Jul 14 21:15:49 2007 +0000
@@ -24,6 +24,26 @@
 };
 
 /**
+ * Check if tiles are buildable in a rectangle around entries in an AITileList instance, with the entry in the list as top-left.
+ * @note resulting items are of the type bool (0 = not buildable, 1 = buildable)
+ * @note the input items are of the type TileIndex
+ */
+class AITileListBuildableRectangle : public AIAbstractList::Valuator {
+public:
+	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AITileListBuildableRectangle"; }
+
+	AITileListBuildableRectangle(uint width, uint height) { this->width = width; this->height = height; }
+
+private:
+	uint width, height;
+
+	int32 Valuate(int32 tile) const;
+};
+
+/**
  * Check how tiles in an AITileList instance are sloped.
  * @note resulting items are of the type int32 (0 = flat, > 1 = slope)
  * @note the input items are of the type TileIndex
--- a/src/ai/api/ai_tilelist_valuator.hpp.sq	Sat Jul 14 14:56:26 2007 +0000
+++ b/src/ai/api/ai_tilelist_valuator.hpp.sq	Sat Jul 14 21:15:49 2007 +0000
@@ -19,6 +19,24 @@
 }
 
 namespace SQConvert {
+	/* Allow AITileListBuildableRectangle to be used as Squirrel parameter */
+	template <> AITileListBuildableRectangle *GetParam(ForceType<AITileListBuildableRectangle *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AITileListBuildableRectangle *)instance; }
+	template <> AITileListBuildableRectangle &GetParam(ForceType<AITileListBuildableRectangle &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AITileListBuildableRectangle *)instance; }
+	template <> const AITileListBuildableRectangle *GetParam(ForceType<const AITileListBuildableRectangle *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AITileListBuildableRectangle *)instance; }
+	template <> const AITileListBuildableRectangle &GetParam(ForceType<const AITileListBuildableRectangle &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AITileListBuildableRectangle *)instance; }
+}; // namespace SQConvert
+
+void SQAITileListBuildableRectangleRegister(Squirrel *engine) {
+	DefSQClass <AITileListBuildableRectangle> SQAITileListBuildableRectangle("AITileListBuildableRectangle");
+	SQAITileListBuildableRectangle.PreRegister(engine);
+	SQAITileListBuildableRectangle.AddConstructor<void (AITileListBuildableRectangle::*)(uint width, uint height), 3>(engine, "xii");
+
+	SQAITileListBuildableRectangle.DefSQStaticMethod(engine, &AITileListBuildableRectangle::GetClassName, "GetClassName", 1, "x");
+
+	SQAITileListBuildableRectangle.PostRegister(engine);
+}
+
+namespace SQConvert {
 	/* Allow AITileListSlope to be used as Squirrel parameter */
 	template <> AITileListSlope *GetParam(ForceType<AITileListSlope *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AITileListSlope *)instance; }
 	template <> AITileListSlope &GetParam(ForceType<AITileListSlope &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AITileListSlope *)instance; }