(svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount noai
authortruelight
Fri, 04 May 2007 22:59:59 +0000
branchnoai
changeset 9617 df9cedf12aab
parent 9616 ca607e3077dd
child 9618 a370e3f3bd70
(svn r9786) [NoAI] -Fix: NeighbourRoad -> NeighbourRoadCount
[NoAI] -Fix: move AITileListValuators self-defined checks to AITile so we can call it per tile (instead of only via AITileList)
bin/ai/regression/regression.nut
bin/ai/regression/regression.txt
source.list
src/ai/ai_squirrel.cpp
src/ai/api/ai_road.cpp
src/ai/api/ai_road.hpp
src/ai/api/ai_road.hpp.sq
src/ai/api/ai_tile.cpp
src/ai/api/ai_tile.hpp
src/ai/api/ai_tile.hpp.sq
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	Fri May 04 21:46:28 2007 +0000
+++ b/bin/ai/regression/regression.nut	Fri May 04 22:59:59 2007 +0000
@@ -230,6 +230,7 @@
 	print("    BuildRoad():                   " + road.BuildRoad(33411, 33414));
 	print("    AreRoadTilesConnected():       " + road.AreRoadTilesConnected(33412, 33413));
 	print("    IsRoadTile():                  " + road.IsRoadTile(33411));
+	print("    GetNeighbourRoadCount():       " + road.GetNeighbourRoadCount(33412));
 	print("    RemoveRoad():                  " + road.RemoveRoad(33411, 33411));
 	print("    RemoveRoad():                  " + road.RemoveRoad(33411, 33412));
 	print("    RemoveRoad():                  " + road.RemoveRoad(19590, 19590));
@@ -349,9 +350,9 @@
 		print("    " + i + " => " + list.GetValue(i));
 	}
 
-	list.Valuate(AITileListNeighbourRoad());
+	list.Valuate(AITileListNeighbourRoadCount());
 	list.KeepValue(1);
-	print("  NeighbourRoad():     done");
+	print("  NeighbourRoadCount():done");
 	print("  KeepValue(1):        done");
 	print("  Count():             " + list.Count());
 	print("  ListDump:");
--- a/bin/ai/regression/regression.txt	Fri May 04 21:46:28 2007 +0000
+++ b/bin/ai/regression/regression.txt	Fri May 04 22:59:59 2007 +0000
@@ -624,6 +624,7 @@
     BuildRoad():                   true
     AreRoadTilesConnected():       true
     IsRoadTile():                  true
+    GetNeighbourRoad():            2
     RemoveRoad():                  true
     RemoveRoad():                  true
     RemoveRoad():                  true
--- a/source.list	Fri May 04 21:46:28 2007 +0000
+++ b/source.list	Fri May 04 22:59:59 2007 +0000
@@ -330,6 +330,7 @@
 ai/api/ai_settings.hpp
 ai/api/ai_sign.hpp
 ai/api/ai_testmode.hpp
+ai/api/ai_tile.hpp
 ai/api/ai_tilelist.hpp
 ai/api/ai_tilelist_valuator.hpp
 ai/api/ai_town.hpp
@@ -357,6 +358,7 @@
 ai/api/ai_settings.cpp
 ai/api/ai_sign.cpp
 ai/api/ai_testmode.cpp
+ai/api/ai_tile.cpp
 ai/api/ai_tilelist.cpp
 ai/api/ai_tilelist_valuator.cpp
 ai/api/ai_town.cpp
--- a/src/ai/ai_squirrel.cpp	Fri May 04 21:46:28 2007 +0000
+++ b/src/ai/ai_squirrel.cpp	Fri May 04 22:59:59 2007 +0000
@@ -36,6 +36,7 @@
 #include "api/ai_settings.hpp.sq"
 #include "api/ai_sign.hpp.sq"
 #include "api/ai_testmode.hpp.sq"
+#include "api/ai_tile.hpp.sq"
 #include "api/ai_tilelist.hpp.sq"
 #include "api/ai_tilelist_valuator.hpp.sq"
 #include "api/ai_town.hpp.sq"
@@ -214,10 +215,11 @@
 	SQAITestModeRegister(this->engine);
 	SQAITileListBuildableRegister(this->engine);
 	SQAITileListCargoAcceptanceRegister(this->engine);
-	SQAITileListNeighbourRoadRegister(this->engine);
+	SQAITileListNeighbourRoadCountRegister(this->engine);
 	SQAITileListRegister(this->engine);
 	SQAITileListRoadTileRegister(this->engine);
 	SQAITileListSlopeRegister(this->engine);
+	SQAITileRegister(this->engine);
 	SQAITownListLocationRegister(this->engine);
 	SQAITownListPopulationRegister(this->engine);
 	SQAITownListRegister(this->engine);
--- a/src/ai/api/ai_road.cpp	Fri May 04 21:46:28 2007 +0000
+++ b/src/ai/api/ai_road.cpp	Fri May 04 22:59:59 2007 +0000
@@ -57,6 +57,21 @@
 	return HASBIT(r1, dir_1) && HASBIT(r2, dir_2);
 }
 
+int32 AIRoad::GetNeighbourRoadCount(TileIndex tile)
+{
+	/* Outside of the map */
+	if (tile >= _map_size) return false;
+
+	int32 neighbour = 0;
+
+	if (::IsTileType(tile + ::TileDiffXY(-1, 0), MP_STREET) && ::GetRoadTileType(tile + ::TileDiffXY(-1, 0)) != ROAD_TILE_DEPOT) neighbour++;
+	if (::IsTileType(tile + ::TileDiffXY( 1, 0), MP_STREET) && ::GetRoadTileType(tile + ::TileDiffXY( 1, 0)) != ROAD_TILE_DEPOT) neighbour++;
+	if (::IsTileType(tile + ::TileDiffXY( 0,-1), MP_STREET) && ::GetRoadTileType(tile + ::TileDiffXY( 0,-1)) != ROAD_TILE_DEPOT) neighbour++;
+	if (::IsTileType(tile + ::TileDiffXY( 0, 1), MP_STREET) && ::GetRoadTileType(tile + ::TileDiffXY( 0, 1)) != ROAD_TILE_DEPOT) neighbour++;
+
+	return neighbour;
+}
+
 TileIndex AIRoad::GetRoadDepotFrontTile(TileIndex depot)
 {
 	if (!this->IsRoadDepotTile(depot)) return INVALID_TILE;
--- a/src/ai/api/ai_road.hpp	Fri May 04 21:46:28 2007 +0000
+++ b/src/ai/api/ai_road.hpp	Fri May 04 22:59:59 2007 +0000
@@ -76,6 +76,14 @@
 	bool AreRoadTilesConnected(TileIndex t1, TileIndex t2);
 
 	/**
+	 * Count how many neighbours are road.
+	 * @pre tile is always positive and smaller than AIMap::GetMapSize().
+	 * @param tile the tile to check on.
+	 * @return 0 means no neighbour road; max value is 4.
+	 */
+	static int32 GetNeighbourRoadCount(TileIndex tile);
+
+	/**
 	 * Gets the tile in front of a road depot.
 	 * @param depot the road depot tile.
 	 * @pre IsRoadDepotTile(depot).
--- a/src/ai/api/ai_road.hpp.sq	Fri May 04 21:46:28 2007 +0000
+++ b/src/ai/api/ai_road.hpp.sq	Fri May 04 22:59:59 2007 +0000
@@ -13,7 +13,8 @@
 	SQAIRoad.PreRegister(engine);
 	SQAIRoad.AddConstructor<void (AIRoad::*)()>(engine, 1, "x");
 
-	SQAIRoad.DefSQStaticMethod(engine, &AIRoad::GetClassName, "GetClassName", 1, "x");
+	SQAIRoad.DefSQStaticMethod(engine, &AIRoad::GetClassName,          "GetClassName",          1, "x");
+	SQAIRoad.DefSQStaticMethod(engine, &AIRoad::GetNeighbourRoadCount, "GetNeighbourRoadCount", 2, "xi");
 
 	SQAIRoad.DefSQMethod(engine, &AIRoad::IsRoadTile,                    "IsRoadTile",                    2, "xi");
 	SQAIRoad.DefSQMethod(engine, &AIRoad::IsRoadDepotTile,               "IsRoadDepotTile",               2, "xi");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_tile.cpp	Fri May 04 22:59:59 2007 +0000
@@ -0,0 +1,44 @@
+/* $Id$ */
+
+/** @file ai_tile.cpp handles the functions of the AITile class */
+
+#include "ai_tile.hpp"
+#include "../../tile.h"
+#include "../../variables.h"
+#include "../../station.h"
+
+bool AITile::IsBuildable(TileIndex tile)
+{
+	/* Outside of the map */
+	if (tile >= _map_size) return false;
+
+	switch (::GetTileType(tile)) {
+		default: return 1;
+		case MP_VOID:
+		case MP_HOUSE:
+		case MP_STATION:
+		case MP_INDUSTRY:
+		case MP_UNMOVABLE: return 0;
+	}
+}
+
+int32 AITile::GetSlope(TileIndex tile)
+{
+	/* Outside of the map */
+	if (tile >= _map_size) return 0;
+
+	return GetTileSlope(tile, NULL);
+}
+
+int32 AITile::GetCargoAcceptance(TileIndex tile, CargoID cargo_type)
+{
+	/* Outside of the map */
+	if (tile >= _map_size) return false;
+
+	/* TODO -- Make it an enum via constructor, for now it assumes RoadVehicle Station */
+	uint rad = 3;
+
+	AcceptedCargo accepts;
+	GetAcceptanceAroundTiles(accepts, tile, 1, 1, _patches.modified_catchment ? rad : 4);
+	return accepts[cargo_type];
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_tile.hpp	Fri May 04 22:59:59 2007 +0000
@@ -0,0 +1,48 @@
+/* $Id$ */
+
+/** @file ai_tile.hpp tile related functions */
+
+#ifndef AI_TILE_HPP
+#define AI_TILE_HPP
+
+#include "ai_abstractlist.hpp"
+
+/**
+ * Class that handles all tile related functions.
+ */
+class AITile : public AIObject {
+public:
+	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AITile"; }
+
+	/**
+	 * Check if this tile is buildable (e.g.: no things on it that needs removing).
+	 * @note Road and rail are considered buildable.
+	 * @pre tile is always positive and smaller than AIMap::GetMapSize().
+	 * @param tile the tile to check on.
+	 * @return true if it is buildable, false if not.
+	 */
+	static bool IsBuildable(TileIndex tile);
+
+	/**
+	 * Get the slope of a tile.
+	 * @pre tile is always positive and smaller than AIMap::GetMapSize().
+	 * @param tile the tile to check on.
+	 * @return 0 means flat, others indicate internal state of slope.
+	 */
+	static int32 GetSlope(TileIndex tile);
+
+	/**
+	 * Check how much cargo this tile accepts.
+	 *  It creates a radius around the tile and adds up all acceptance of this
+	 *   cargo and returns that value.
+	 * @pre tile is always positive and smaller than AIMap::GetMapSize().
+	 * @param tile the tile to check on.
+	 * @return value below 8 means no acceptance; the more the better.
+	 */
+	static int32 GetCargoAcceptance(TileIndex tile, CargoID cargo_type);
+};
+
+#endif /* AI_TILE_HPP */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_tile.hpp.sq	Fri May 04 22:59:59 2007 +0000
@@ -0,0 +1,22 @@
+#include "ai_tile.hpp"
+
+namespace SQConvert {
+	/* Allow AITile to be used as Squirrel parameter */
+	template <> AITile *GetParam(ForceType<AITile *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AITile *)instance; }
+	template <> AITile &GetParam(ForceType<AITile &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AITile *)instance; }
+	template <> const AITile *GetParam(ForceType<const AITile *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AITile *)instance; }
+	template <> const AITile &GetParam(ForceType<const AITile &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AITile *)instance; }
+}; // namespace SQConvert
+
+void SQAITileRegister(Squirrel *engine) {
+	DefSQClass <AITile> SQAITile("AITile");
+	SQAITile.PreRegister(engine);
+	SQAITile.AddConstructor<void (AITile::*)()>(engine, 1, "x");
+
+	SQAITile.DefSQStaticMethod(engine, &AITile::GetClassName,       "GetClassName",       1, "x");
+	SQAITile.DefSQStaticMethod(engine, &AITile::IsBuildable,        "IsBuildable",        2, "xi");
+	SQAITile.DefSQStaticMethod(engine, &AITile::GetSlope,           "GetSlope",           2, "xi");
+	SQAITile.DefSQStaticMethod(engine, &AITile::GetCargoAcceptance, "GetCargoAcceptance", 3, "xii");
+
+	SQAITile.PostRegister(engine);
+}
--- a/src/ai/api/ai_tilelist_valuator.cpp	Fri May 04 21:46:28 2007 +0000
+++ b/src/ai/api/ai_tilelist_valuator.cpp	Fri May 04 22:59:59 2007 +0000
@@ -1,40 +1,22 @@
 #include "ai_tilelist_valuator.hpp"
+#include "ai_tile.hpp"
+#include "ai_road.hpp"
 #include "../../tile.h"
 #include "../../road_map.h"
-#include "../../variables.h"
-#include "../../station.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;
-	}
+	return AITile::IsBuildable(tile);
 }
 
 int32 AITileListSlope::Valuate(int32 tile) const
 {
-	return GetTileSlope(tile, NULL);
+	return AITile::GetSlope(tile);
 }
 
-int32 AITileListNeighbourRoad::Valuate(int32 tile) const
+int32 AITileListNeighbourRoadCount::Valuate(int32 tile) const
 {
-	int32 neighbour = 0;
-
-	if (::IsTileType(tile + ::TileDiffXY(-1, 0), MP_STREET) && ::GetRoadTileType(tile + ::TileDiffXY(-1, 0)) != ROAD_TILE_DEPOT)
-		neighbour++;
-	if (::IsTileType(tile + ::TileDiffXY( 1, 0), MP_STREET) && ::GetRoadTileType(tile + ::TileDiffXY( 1, 0)) != ROAD_TILE_DEPOT)
-		neighbour++;
-	if (::IsTileType(tile + ::TileDiffXY( 0,-1), MP_STREET) && ::GetRoadTileType(tile + ::TileDiffXY( 0,-1)) != ROAD_TILE_DEPOT)
-		neighbour++;
-	if (::IsTileType(tile + ::TileDiffXY( 0, 1), MP_STREET) && ::GetRoadTileType(tile + ::TileDiffXY( 0, 1)) != ROAD_TILE_DEPOT)
-		neighbour++;
-
-	return neighbour;
+	return AIRoad::GetNeighbourRoadCount(tile);
 }
 
 int32 AITileListRoadTile::Valuate(int32 tile) const
@@ -44,10 +26,5 @@
 
 int32 AITileListCargoAcceptance::Valuate(int32 tile) const
 {
-	/* TODO -- Make it an enum via constructor, for now it assumes RoadVehicle Station */
-	uint rad = 3;
-
-	AcceptedCargo accepts;
-	GetAcceptanceAroundTiles(accepts, tile, 1, 1, _patches.modified_catchment ? rad : 4);
-	return accepts[this->cargo_type];
+	return AITile::GetCargoAcceptance(tile, this->cargo_type);
 }
--- a/src/ai/api/ai_tilelist_valuator.hpp	Fri May 04 21:46:28 2007 +0000
+++ b/src/ai/api/ai_tilelist_valuator.hpp	Fri May 04 22:59:59 2007 +0000
@@ -40,11 +40,11 @@
 };
 
 /**
- * Check if one of the neighbour tiles in AITileList has a piece of road.
+ * Count the neighbour tiles which have a piece of road of tiles in AITileList.
  * @note resulting items are of the type int32 (the amount of neighbour road tiles)
  * @note the input items are of the type TileIndex
  */
-class AITileListNeighbourRoad : public AIAbstractList::Valuator {
+class AITileListNeighbourRoadCount : public AIAbstractList::Valuator {
 public:
 	/**
 	 * The name of the class, needed by several sub-processes.
--- a/src/ai/api/ai_tilelist_valuator.hpp.sq	Fri May 04 21:46:28 2007 +0000
+++ b/src/ai/api/ai_tilelist_valuator.hpp.sq	Fri May 04 22:59:59 2007 +0000
@@ -37,21 +37,21 @@
 }
 
 namespace SQConvert {
-	/* Allow AITileListNeighbourRoad to be used as Squirrel parameter */
-	template <> AITileListNeighbourRoad *GetParam(ForceType<AITileListNeighbourRoad *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AITileListNeighbourRoad *)instance; }
-	template <> AITileListNeighbourRoad &GetParam(ForceType<AITileListNeighbourRoad &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AITileListNeighbourRoad *)instance; }
-	template <> const AITileListNeighbourRoad *GetParam(ForceType<const AITileListNeighbourRoad *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AITileListNeighbourRoad *)instance; }
-	template <> const AITileListNeighbourRoad &GetParam(ForceType<const AITileListNeighbourRoad &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AITileListNeighbourRoad *)instance; }
+	/* Allow AITileListNeighbourRoadCount to be used as Squirrel parameter */
+	template <> AITileListNeighbourRoadCount *GetParam(ForceType<AITileListNeighbourRoadCount *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AITileListNeighbourRoadCount *)instance; }
+	template <> AITileListNeighbourRoadCount &GetParam(ForceType<AITileListNeighbourRoadCount &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AITileListNeighbourRoadCount *)instance; }
+	template <> const AITileListNeighbourRoadCount *GetParam(ForceType<const AITileListNeighbourRoadCount *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AITileListNeighbourRoadCount *)instance; }
+	template <> const AITileListNeighbourRoadCount &GetParam(ForceType<const AITileListNeighbourRoadCount &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AITileListNeighbourRoadCount *)instance; }
 }; // namespace SQConvert
 
-void SQAITileListNeighbourRoadRegister(Squirrel *engine) {
-	DefSQClass <AITileListNeighbourRoad> SQAITileListNeighbourRoad("AITileListNeighbourRoad");
-	SQAITileListNeighbourRoad.PreRegister(engine);
-	SQAITileListNeighbourRoad.AddConstructor<void (AITileListNeighbourRoad::*)()>(engine, 1, "x");
+void SQAITileListNeighbourRoadCountRegister(Squirrel *engine) {
+	DefSQClass <AITileListNeighbourRoadCount> SQAITileListNeighbourRoadCount("AITileListNeighbourRoadCount");
+	SQAITileListNeighbourRoadCount.PreRegister(engine);
+	SQAITileListNeighbourRoadCount.AddConstructor<void (AITileListNeighbourRoadCount::*)()>(engine, 1, "x");
 
-	SQAITileListNeighbourRoad.DefSQStaticMethod(engine, &AITileListNeighbourRoad::GetClassName, "GetClassName", 1, "x");
+	SQAITileListNeighbourRoadCount.DefSQStaticMethod(engine, &AITileListNeighbourRoadCount::GetClassName, "GetClassName", 1, "x");
 
-	SQAITileListNeighbourRoad.PostRegister(engine);
+	SQAITileListNeighbourRoadCount.PostRegister(engine);
 }
 
 namespace SQConvert {