(svn r9625) [NoAI] -Add: added AITileList() and one valuator for now: AITileListBuildable() noai
authortruelight
Sat, 14 Apr 2007 20:01:25 +0000
branchnoai
changeset 9592 c5c09cfde63a
parent 9591 cd88f4b7cba0
child 9593 012f29f59906
(svn r9625) [NoAI] -Add: added AITileList() and one valuator for now: AITileListBuildable()
bin/ai/regression/regression.nut
bin/ai/regression/regression.txt
projects/openttd.vcproj
projects/openttd_vs80.vcproj
source.list
src/ai/ai_squirrel.cpp
src/ai/api/ai_tilelist.cpp
src/ai/api/ai_tilelist.hpp
src/ai/api/ai_tilelist_valuator.cpp
src/ai/api/ai_tilelist_valuator.hpp
--- a/bin/ai/regression/regression.nut	Sat Apr 14 19:20:17 2007 +0000
+++ b/bin/ai/regression/regression.nut	Sat Apr 14 20:01:25 2007 +0000
@@ -303,6 +303,25 @@
 	print("  GetSignCount():    " + sign.GetSignCount());
 }
 
+function Regression::TileList()
+{
+	local list = AITileList();
+
+	print("");
+	print("--TileList--");
+	print("  Count():             " + list.Count());
+	list.AddRectangle(33410 - 256 * 2, 256 * 2 + 33410 + 8);
+	print("  Count():             " + list.Count());
+	list.Valuate(AITileListBuildable());
+	list.KeepValue(1);
+	print("  KeepAboveValue(1):   done");
+	print("  Count():             " + list.Count());
+	print("  ListDump:");
+	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
+		print("    " + i + " => " + list.GetValue(i));
+	}
+}
+
 function Regression::Town()
 {
 	local town = AITown();
@@ -418,6 +437,7 @@
 	this.List();
 	this.Road();
 	this.Sign();
+	this.TileList();
 	this.Town();
 	this.TownList();
 	this.Vehicle();
--- a/bin/ai/regression/regression.txt	Sat Apr 14 19:20:17 2007 +0000
+++ b/bin/ai/regression/regression.txt	Sat Apr 14 20:01:25 2007 +0000
@@ -698,6 +698,50 @@
   Valid Signs:       1
   GetSignCount():    1
 
+--TileList--
+  Count():             0
+  Count():             45
+  KeepAboveValue(1):   done
+  Count():             37
+  ListDump:
+    33930 => 1
+    33929 => 1
+    33928 => 1
+    33927 => 1
+    33926 => 1
+    33925 => 1
+    33924 => 1
+    33923 => 1
+    33922 => 1
+    33674 => 1
+    33673 => 1
+    33672 => 1
+    33671 => 1
+    33670 => 1
+    33669 => 1
+    33666 => 1
+    33418 => 1
+    33417 => 1
+    33410 => 1
+    33162 => 1
+    33161 => 1
+    33160 => 1
+    33159 => 1
+    33158 => 1
+    33157 => 1
+    33156 => 1
+    33155 => 1
+    33154 => 1
+    32906 => 1
+    32905 => 1
+    32904 => 1
+    32903 => 1
+    32902 => 1
+    32901 => 1
+    32900 => 1
+    32899 => 1
+    32898 => 1
+
 --Town--
   GetMaxTownID():    31
   GetTownCount():    28
--- a/projects/openttd.vcproj	Sat Apr 14 19:20:17 2007 +0000
+++ b/projects/openttd.vcproj	Sat Apr 14 20:01:25 2007 +0000
@@ -1072,6 +1072,12 @@
 				RelativePath=".\..\src\ai\api\ai_testmode.hpp">
 			</File>
 			<File
+				RelativePath=".\..\src\ai\api\ai_tilelist.hpp">
+			</File>
+			<File
+				RelativePath=".\..\src\ai\api\ai_tilelist_valuator.hpp">
+			</File>
+			<File
 				RelativePath=".\..\src\ai\api\ai_town.hpp">
 			</File>
 			<File
@@ -1136,6 +1142,12 @@
 				RelativePath=".\..\src\ai\api\ai_testmode.cpp">
 			</File>
 			<File
+				RelativePath=".\..\src\ai\api\ai_tilelist.cpp">
+			</File>
+			<File
+				RelativePath=".\..\src\ai\api\ai_tilelist_valuator.cpp">
+			</File>
+			<File
 				RelativePath=".\..\src\ai\api\ai_town.cpp">
 			</File>
 			<File
--- a/projects/openttd_vs80.vcproj	Sat Apr 14 19:20:17 2007 +0000
+++ b/projects/openttd_vs80.vcproj	Sat Apr 14 20:01:25 2007 +0000
@@ -1648,6 +1648,14 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\ai\api\ai_tilelist.hpp"
+				>
+			</File>
+			<File
+				RelativePath=".\..\src\ai\api\ai_tilelist_valuator.hpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\ai\api\ai_town.hpp"
 				>
 			</File>
@@ -1732,6 +1740,14 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\ai\api\ai_tilelist.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\..\src\ai\api\ai_tilelist_valuator.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\ai\api\ai_town.cpp"
 				>
 			</File>
--- a/source.list	Sat Apr 14 19:20:17 2007 +0000
+++ b/source.list	Sat Apr 14 20:01:25 2007 +0000
@@ -329,6 +329,8 @@
 ai/api/ai_settings.hpp
 ai/api/ai_sign.hpp
 ai/api/ai_testmode.hpp
+ai/api/ai_tilelist.hpp
+ai/api/ai_tilelist_valuator.hpp
 ai/api/ai_town.hpp
 ai/api/ai_townlist.hpp
 ai/api/ai_townlist_valuator.hpp
@@ -351,6 +353,8 @@
 ai/api/ai_settings.cpp
 ai/api/ai_sign.cpp
 ai/api/ai_testmode.cpp
+ai/api/ai_tilelist.cpp
+ai/api/ai_tilelist_valuator.cpp
 ai/api/ai_town.cpp
 ai/api/ai_townlist.cpp
 ai/api/ai_townlist_valuator.cpp
--- a/src/ai/ai_squirrel.cpp	Sat Apr 14 19:20:17 2007 +0000
+++ b/src/ai/ai_squirrel.cpp	Sat Apr 14 20:01:25 2007 +0000
@@ -35,6 +35,8 @@
 #include "api/ai_settings.hpp"
 #include "api/ai_sign.hpp"
 #include "api/ai_testmode.hpp"
+#include "api/ai_tilelist.hpp"
+#include "api/ai_tilelist_valuator.hpp"
 #include "api/ai_town.hpp"
 #include "api/ai_townlist.hpp"
 #include "api/ai_townlist_valuator.hpp"
@@ -207,6 +209,8 @@
 	SQAISettingsRegister(this->engine);
 	SQAISignRegister(this->engine);
 	SQAITestModeRegister(this->engine);
+	SQAITileListBuildableRegister(this->engine);
+	SQAITileListRegister(this->engine);
 	SQAITownListGetLocationRegister(this->engine);
 	SQAITownListGetPopulationRegister(this->engine);
 	SQAITownListRegister(this->engine);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_tilelist.cpp	Sat Apr 14 20:01:25 2007 +0000
@@ -0,0 +1,58 @@
+#include "ai_tilelist.hpp"
+
+void AITileList::FixRectangleSpan(TileIndex &t1, TileIndex &t2)
+{
+	uint x1 = ::TileX(t1);
+	uint x2 = ::TileX(t2);
+
+	uint y1 = ::TileY(t1);
+	uint y2 = ::TileY(t2);
+
+	if (x1 >= x2) ::Swap(x1, x2);
+	if (y1 >= y2) ::Swap(y1, y2);
+
+	t1 = ::TileXY(x1, y1);
+	t2 = ::TileXY(x2, y2);
+}
+
+void AITileList::AddRectangle(TileIndex t1, TileIndex t2)
+{
+	if (!IsValidTile(t1) || !IsValidTile(t2)) return;
+
+	this->FixRectangleSpan(t1, t2);
+
+	uint w = TileX(t2) - TileX(t1) + 1;
+	uint h = TileY(t2) - TileY(t1) + 1;
+
+	BEGIN_TILE_LOOP(t, w, h, t1) {
+		this->AddItem(t);
+	} END_TILE_LOOP(t, w, h, t1)
+}
+
+void AITileList::AddTile(TileIndex tile)
+{
+	if (!IsValidTile(tile)) return;
+
+	this->AddItem(tile);
+}
+
+void AITileList::RemoveRectangle(TileIndex t1, TileIndex t2)
+{
+	if (!IsValidTile(t1) || !IsValidTile(t2)) return;
+
+	this->FixRectangleSpan(t1, t2);
+
+	uint w = TileX(t2) - TileX(t1) + 1;
+	uint h = TileY(t2) - TileY(t1) + 1;
+
+	BEGIN_TILE_LOOP(t, w, h, t1) {
+		this->RemoveItem(t);
+	} END_TILE_LOOP(t, w, h, t1)
+}
+
+void AITileList::RemoveTile(TileIndex tile)
+{
+	if (!IsValidTile(tile)) return;
+
+	this->RemoveItem(tile);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_tilelist.hpp	Sat Apr 14 20:01:25 2007 +0000
@@ -0,0 +1,84 @@
+/* $Id$ */
+
+/** @file ai_tilelist.hpp a simple tilelist which you can manipulate */
+
+#ifndef AI_TILELIST_HPP
+#define AI_TILELIST_HPP
+
+#include "ai_list.hpp"
+
+/**
+ * Class that creates a simple list of tiles which you can manipulate.
+ */
+class AITileList : public AIList {
+public:
+	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AITileList"; }
+
+private:
+	/**
+	 * Make sure t1.x is smaller than t2.x and t1.y is smaller than t2.y.
+	 * They are swapped to ensure they are after calling this function.
+	 */
+	void FixRectangleSpan(TileIndex &t1, TileIndex &t2);
+
+public:
+
+	/**
+	 * Adds the rectangle between tile_from and tile_to to the to-be-evaluated tiles.
+	 * @param tile_from one corner of the tiles to add.
+	 * @param tile_to the other corner of the tiles to add.
+	 * @pre tile_from is below AIMap::GetMapSize().
+	 * @pre tile_to is below AIMap::GetMapSize().
+	 */
+	void AddRectangle(TileIndex tile_from, TileIndex tile_to);
+
+	/**
+	 * Add a tile to the to-be-evaluated tiles.
+	 * @param tile the tile to add.
+	 * @pre tile is below AIMap::GetMapSize().
+	 */
+	void AddTile(TileIndex tile);
+
+	/**
+	 * Remove the tiles inside the rectangle between tile_from and tile_to form the list.
+	 * @param tile_From one corner of the tiles to remove.
+	 * @param tile_to the other corner of the files to remove.
+	 * @pre tile_from is below AIMap::GetMapSize().
+	 * @pre tile_to is below AIMap::GetMapSize().
+	 */
+	void RemoveRectangle(TileIndex tile_from, TileIndex tile_to);
+
+	/**
+	 * Remove a tile from the list.
+	 * @param tile the tile to remove.
+	 * @pre tile is below AIMap::GetMapSize().
+	 */
+	void RemoveTile(TileIndex tile);
+};
+
+#ifdef DEFINE_SQUIRREL_CLASS
+namespace SQConvert {
+	/* Allow AITileList to be used as Squirrel parameter */
+	template <> AITileList *GetParam(ForceType<AITileList *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AITileList *)instance; }
+}; // namespace SQConvert
+
+void SQAITileListRegister(Squirrel *engine) {
+	DefSQClass <AITileList> SQAITileList("AITileList");
+	SQAITileList.PreRegister(engine, "AIList");
+	SQAITileList.AddConstructor(engine);
+
+	SQAITileList.DefSQStaticMethod(engine, &AITileList::GetClassName, "GetClassName", 1, "x");
+
+	SQAITileList.DefSQMethod(engine, &AITileList::AddRectangle,    "AddRectangle",    3, "xii");
+	SQAITileList.DefSQMethod(engine, &AITileList::AddTile,         "AddTile",         2, "xi");
+	SQAITileList.DefSQMethod(engine, &AITileList::RemoveRectangle, "RemoveRectangle", 3, "xii");
+	SQAITileList.DefSQMethod(engine, &AITileList::RemoveTile,      "RemoveTile",      2, "xi");
+
+	SQAITileList.PostRegister(engine);
+}
+#endif /* DEFINE_SQUIRREL_CLASS */
+
+#endif /* AI_TILELIST_HPP */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_tilelist_valuator.cpp	Sat Apr 14 20:01:25 2007 +0000
@@ -0,0 +1,14 @@
+#include "ai_tilelist_valuator.hpp"
+#include "../../tile.h"
+
+int32 AITileListBuildable::Valuate(int32 tile) const
+{
+	switch (::GetTileType(tile)) {
+		default: return 1;
+		case MP_VOID:
+		case MP_HOUSE:
+		case MP_STATION:
+		case MP_INDUSTRY:
+		case MP_UNMOVABLE: return 0;
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_tilelist_valuator.hpp	Sat Apr 14 20:01:25 2007 +0000
@@ -0,0 +1,43 @@
+/* $Id$ */
+
+/** @file ai_tilelist.hpp all the valuators for tilelist */
+
+#ifndef AI_TILELIST_VALUATOR_HPP
+#define AI_TILELIST_VALUATOR_HPP
+
+#include "ai_list.hpp"
+
+/**
+ * Check if tiles are buildable for entries in an AITownList instance.
+ * @note resulting items are of the type bool (0 = not buildable, 1 = buildable)
+ * @note the input items are of the type TileIndex
+ */
+class AITileListBuildable : public AIList::Valuator {
+public:
+	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AITileListBuildable"; }
+
+private:
+	int32 Valuate(int32 tile) const;
+};
+
+#ifdef DEFINE_SQUIRREL_CLASS
+namespace SQConvert {
+	/* Allow AITileListBuildable to be used as Squirrel parameter */
+	template <> AITileListBuildable *GetParam(ForceType<AITileListBuildable *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AITileListBuildable *)instance; }
+}; // namespace SQConvert
+
+void SQAITileListBuildableRegister(Squirrel *engine) {
+	DefSQClass <AITileListBuildable> SQAITileListBuildable("AITileListBuildable");
+	SQAITileListBuildable.PreRegister(engine);
+	SQAITileListBuildable.AddConstructor(engine);
+
+	SQAITileListBuildable.DefSQStaticMethod(engine, &AITileListBuildable::GetClassName, "GetClassName", 1, "x");
+
+	SQAITileListBuildable.PostRegister(engine);
+}
+#endif /* DEFINE_SQUIRREL_CLASS */
+
+#endif /* AI_TILELIST_VALUATOR_HPP */