(svn r13415) [NoAI] -Add: added AITileList_StationType, giving all the tiles on which a station of your requested type is, for a given station noai
authortruebrain
Sun, 08 Jun 2008 13:56:35 +0000
branchnoai
changeset 10864 9a6616a1dce6
parent 10863 7b31d67e1553
child 10865 c4b3ddd2e9da
(svn r13415) [NoAI] -Add: added AITileList_StationType, giving all the tiles on which a station of your requested type is, for a given station
bin/ai/regression/regression.nut
bin/ai/regression/regression.txt
src/ai/api/ai_controller.cpp
src/ai/api/ai_tilelist.cpp
src/ai/api/ai_tilelist.hpp
src/ai/api/ai_tilelist.hpp.sq
--- a/bin/ai/regression/regression.nut	Sun Jun 08 12:45:11 2008 +0000
+++ b/bin/ai/regression/regression.nut	Sun Jun 08 13:56:35 2008 +0000
@@ -980,6 +980,15 @@
 	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
 		print("    " + i + " => " + list.GetValue(i));
 	}
+
+	local list = AITileList_StationType(3, AIStation.STATION_BUS_STOP);
+	print("");
+	print("--TileList_StationType--");
+	print("  Count():             " + list.Count());
+	print("  Location ListDump:");
+	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
+		print("    " + i + " => " + list.GetValue(i));
+	}
 }
 
 function Regression::Town()
--- a/bin/ai/regression/regression.txt	Sun Jun 08 12:45:11 2008 +0000
+++ b/bin/ai/regression/regression.txt	Sun Jun 08 13:56:35 2008 +0000
@@ -6295,6 +6295,14 @@
     44360 => 1
     44351 => 1
 
+--TileList_StationType--
+  Count():             4
+  Location ListDump:
+    33668 => 0
+    33416 => 0
+    33414 => 0
+    33412 => 0
+
 --Town--
   GetMaxTownID():    31
   GetTownCount():    28
--- a/src/ai/api/ai_controller.cpp	Sun Jun 08 12:45:11 2008 +0000
+++ b/src/ai/api/ai_controller.cpp	Sun Jun 08 13:56:35 2008 +0000
@@ -129,6 +129,7 @@
 	SQAITileList_Register(this->engine);
 	SQAITileList_IndustryAccepting_Register(this->engine);
 	SQAITileList_IndustryProducing_Register(this->engine);
+	SQAITileList_StationType_Register(this->engine);
 	SQAITown_Register(this->engine);
 	SQAITownList_Register(this->engine);
 	SQAITransactionMode_Register(this->engine);
--- a/src/ai/api/ai_tilelist.cpp	Sun Jun 08 12:45:11 2008 +0000
+++ b/src/ai/api/ai_tilelist.cpp	Sun Jun 08 13:56:35 2008 +0000
@@ -11,6 +11,8 @@
 #include "../../map_func.h"
 #include "../../tile_map.h"
 #include "../../industry_map.h"
+#include "../../station_base.h"
+#include "../../station_map.h"
 
 void AITileList::FixRectangleSpan(TileIndex &t1, TileIndex &t2)
 {
@@ -148,3 +150,18 @@
 		this->AddTile(cur_tile);
 	} END_TILE_LOOP(cur_tile, i->width + radius * 2, i->height + radius * 2, i->xy - ::TileDiffXY(radius, radius))
 }
+
+AITileList_StationType::AITileList_StationType(StationID station_id, AIStation::StationType station_type)
+{
+	if (!AIStation::IsValidStation(station_id)) return;
+
+	const StationRect *rect = &::GetStation(station_id)->rect;
+	int station_type_value = ::FindFirstBit(station_type);
+
+	BEGIN_TILE_LOOP(cur_tile, rect->right - rect->left + 1, rect->bottom - rect->top + 1, ::TileXY(rect->left, rect->top)) {
+		if (!::IsTileType(cur_tile, MP_STATION)) continue;
+		if (::GetStationIndex(cur_tile) != station_id) continue;
+		if (::GetStationType(cur_tile) != station_type_value) continue;
+		this->AddTile(cur_tile);
+	} END_TILE_LOOP(cur_tile, rect->right - rect->left + 1, rect->bottom - rect->top + 1, ::TileXY(rect->left, rect->top))
+}
--- a/src/ai/api/ai_tilelist.hpp	Sun Jun 08 12:45:11 2008 +0000
+++ b/src/ai/api/ai_tilelist.hpp	Sun Jun 08 13:56:35 2008 +0000
@@ -6,6 +6,7 @@
 #define AI_TILELIST_HPP
 
 #include "ai_abstractlist.hpp"
+#include "ai_station.hpp"
 
 /**
  * Creates an empty list, in which you can add tiles.
@@ -90,4 +91,20 @@
 	AITileList_IndustryProducing(IndustryID industry_id, uint radius);
 };
 
+/**
+ * Creates a list of tiles which have the requested StationType of the
+ *  StationID.
+ * @ingroup AIList
+ */
+class AITileList_StationType : public AITileList {
+public:
+	static const char *GetClassName() { return "AITileList_StationType"; }
+
+	/**
+	 * @param station_id The station to create the AITileList for.
+	 * @param station_type The StationType to create the AIList for.
+	 */
+	AITileList_StationType(StationID station_id, AIStation::StationType station_type);
+};
+
 #endif /* AI_TILELIST_HPP */
--- a/src/ai/api/ai_tilelist.hpp.sq	Sun Jun 08 12:45:11 2008 +0000
+++ b/src/ai/api/ai_tilelist.hpp.sq	Sun Jun 08 13:56:35 2008 +0000
@@ -64,3 +64,22 @@
 
 	SQAITileList_IndustryProducing.PostRegister(engine);
 }
+
+namespace SQConvert {
+	/* Allow AITileList_StationType to be used as Squirrel parameter */
+	template <> AITileList_StationType *GetParam(ForceType<AITileList_StationType *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AITileList_StationType *)instance; }
+	template <> AITileList_StationType &GetParam(ForceType<AITileList_StationType &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AITileList_StationType *)instance; }
+	template <> const AITileList_StationType *GetParam(ForceType<const AITileList_StationType *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AITileList_StationType *)instance; }
+	template <> const AITileList_StationType &GetParam(ForceType<const AITileList_StationType &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AITileList_StationType *)instance; }
+	template <> int Return<AITileList_StationType *>(HSQUIRRELVM vm, AITileList_StationType *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "AITileList_StationType", res, NULL, DefSQDestructorCallback<AITileList_StationType>); return 1; }
+}; // namespace SQConvert
+
+void SQAITileList_StationType_Register(Squirrel *engine) {
+	DefSQClass <AITileList_StationType> SQAITileList_StationType("AITileList_StationType");
+	SQAITileList_StationType.PreRegister(engine, "AITileList");
+	SQAITileList_StationType.AddConstructor<void (AITileList_StationType::*)(StationID station_id, AIStation::StationType station_type), 3>(engine, "xii");
+
+	SQAITileList_StationType.DefSQStaticMethod(engine, &AITileList_StationType::GetClassName, "GetClassName", 1, "x");
+
+	SQAITileList_StationType.PostRegister(engine);
+}