(svn r9489) [NoAI] -Add: functions to query existance and direction of road stations and depots. noai
authorrubidium
Mon, 26 Mar 2007 23:51:18 +0000
branchnoai
changeset 9551 d015a5b4b0a8
parent 9550 31e558ce0c04
child 9552 8e383f873776
(svn r9489) [NoAI] -Add: functions to query existance and direction of road stations and depots.
src/ai/api/ai_road.cpp
src/ai/api/ai_road.hpp
--- a/src/ai/api/ai_road.cpp	Mon Mar 26 23:42:00 2007 +0000
+++ b/src/ai/api/ai_road.cpp	Mon Mar 26 23:51:18 2007 +0000
@@ -12,7 +12,69 @@
 	/* Outside of the map */
 	if (tile >= _map_size) return false;
 
-	return IsTileType(tile, MP_STREET) && GetRoadTileType(tile) != ROAD_TILE_DEPOT;
+	return ::IsTileType(tile, MP_STREET) && ::GetRoadTileType(tile) != ROAD_TILE_DEPOT;
+}
+
+bool AIRoad::IsRoadDepotTile(TileIndex tile)
+{
+	/* Outside of the map */
+	if (tile >= _map_size) return false;
+
+	return ::IsTileType(tile, MP_STREET) && ::GetRoadTileType(tile) == ROAD_TILE_DEPOT;
+}
+
+bool AIRoad::IsRoadStationTile(TileIndex tile)
+{
+	/* Outside of the map */
+	if (tile >= _map_size) return false;
+
+	return ::IsRoadStopTile(tile);
+}
+
+bool AIRoad::IsDriveThroughRoadStationTile(TileIndex tile)
+{
+	/* Outside of the map */
+	if (tile >= _map_size) return false;
+
+	return ::IsDriveThroughStopTile(tile);
+}
+
+bool AIRoad::AreRoadTilesConnected(TileIndex t1, TileIndex t2)
+{
+	/* Outside of the map */
+	if (t1 >= _map_size || t2 >= _map_size) return false;
+
+	/* Tiles not neighbouring */
+	if ((abs((int)::TileX(t1) - (int)::TileX(t2)) + abs((int)::TileY(t1) - (int)::TileY(t2))) != 1) return false;
+
+	RoadBits r1 = ::GetAnyRoadBits(t1);
+	RoadBits r2 = ::GetAnyRoadBits(t2);
+
+	DiagDirection dir_1 = (DiagDirection)((::TileX(t1) == ::TileX(t2)) ? (::TileY(t1) < ::TileY(t2) ? 2 : 0) : (::TileX(t1) < ::TileX(t2) ? 1 : 3));
+	DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
+
+	return HASBIT(r1, dir_1) && HASBIT(r2, dir_2);
+}
+
+TileIndex AIRoad::GetRoadDepotFrontTile(TileIndex depot)
+{
+	if (!this->IsRoadDepotTile(depot)) return INVALID_TILE;
+
+	return depot + ::TileOffsByDiagDir(::GetRoadDepotDirection(depot));
+}
+
+TileIndex AIRoad::GetRoadStationFrontTile(TileIndex station)
+{
+	if (!this->IsRoadStationTile(station)) return INVALID_TILE;
+
+	return station + ::TileOffsByDiagDir(::GetRoadStopDir(station));
+}
+
+TileIndex AIRoad::GetDriveThroughBackTile(TileIndex station)
+{
+	if (!this->IsDriveThroughRoadStationTile(station)) return INVALID_TILE;
+
+	return station + ::TileOffsByDiagDir(::ReverseDiagDir(::GetRoadStopDir(station)));
 }
 
 bool AIRoad::BuildRoad(TileIndex start, TileIndex end)
--- a/src/ai/api/ai_road.hpp	Mon Mar 26 23:42:00 2007 +0000
+++ b/src/ai/api/ai_road.hpp	Mon Mar 26 23:51:18 2007 +0000
@@ -36,6 +36,70 @@
 	bool IsRoadTile(TileIndex tile);
 
 	/**
+	 * Checks whether the given tile is actually a tile with a road depot.
+	 * @param tile the tile to check.
+	 * @pre tile is always positive and smaller than AIMap::GetMapSize().
+	 * @return true if and only if the tile has a road depot.
+	 */
+	bool IsRoadDepotTile(TileIndex tile);
+
+	/**
+	 * Checks whether the given tile is actually a tile with a road station.
+	 * @param tile the tile to check.
+	 * @pre tile is always positive and smaller than AIMap::GetMapSize().
+	 * @return true if and only if the tile has a road station.
+	 */
+	bool IsRoadStationTile(TileIndex tile);
+
+	/**
+	 * Checks whether the given tile is actually a tile with a drive through
+	 * road station.
+	 * @param tile the tile to check.
+	 * @pre tile is always positive and smaller than AIMap::GetMapSize().
+	 * @return true if and only if the tile has a drive through road station.
+	 */
+	bool IsDriveThroughRoadStationTile(TileIndex tile);
+
+	/**
+	 * Checks whether the given tiles are directly connected, i.e. whether
+	 * a road vehicle can travel from the center of the first tile to the
+	 * center of the second tile.
+	 * @param t1 the source tile.
+	 * @param t2 the destination tile.
+	 * @pre t1 is always positive and smaller than AIMap::GetMapSize().
+	 * @pre t2 is always positive and smaller than AIMap::GetMapSize().
+	 * @pre t1 and t2 are directly neighbouring tiles.
+	 * @return true if and only if a road vehicle can go from t1 to t2.
+	 */
+	bool AreRoadTilesConnected(TileIndex t1, TileIndex t2);
+
+	/**
+	 * Gets the tile in front of a road depot.
+	 * @param depot the road depot tile.
+	 * @pre IsRoadDepotTile(depot).
+	 * @return the tile in front of the depot.
+	 */
+	TileIndex GetRoadDepotFrontTile(TileIndex depot);
+
+	/**
+	 * Gets the tile in front of a road station.
+	 * @param station the road station tile.
+	 * @pre IsRoadStationTile(station).
+	 * @return the tile in front of the road station.
+	 */
+	TileIndex GetRoadStationFrontTile(TileIndex station);
+
+	/**
+	 * Gets the tile at the back of a drive through road station.
+	 * So, one side of the drive through station is retrieved with
+	 * GetTileInFrontOfStation, the other with this function.
+	 * @param station the road station tile.
+	 * @pre IsDriveThroughRoadStationTile(station).
+	 * @return the tile at the back of the drive through road station.
+	 */
+	TileIndex GetDriveThroughBackTile(TileIndex station);
+
+	/**
 	 * Builds a road from the center of tile start to the
 	 * center of tile end.
 	 * @param start the start tile of the road.
@@ -121,13 +185,20 @@
 
 	SQAIRoad.DefSQStaticMethod(engine, &AIRoad::GetClassName, "GetClassName", 1, "x");
 
-	SQAIRoad.DefSQMethod(engine, &AIRoad::IsRoadTile,        "IsRoadTile",        2, "xi");
-	SQAIRoad.DefSQMethod(engine, &AIRoad::BuildRoad,         "BuildRoad",         3, "xii");
-	SQAIRoad.DefSQMethod(engine, &AIRoad::BuildRoadDepot,    "BuildRoadDepot",    3, "xii");
-	SQAIRoad.DefSQMethod(engine, &AIRoad::BuildRoadStation,  "BuildRoadStation",  5, "xiibb");
-	SQAIRoad.DefSQMethod(engine, &AIRoad::RemoveRoad,        "RemoveRoad",        3, "xii");
-	SQAIRoad.DefSQMethod(engine, &AIRoad::RemoveRoadDepot,   "RemoveRoadDepot",   2, "xi");
-	SQAIRoad.DefSQMethod(engine, &AIRoad::RemoveRoadStation, "RemoveRoadStation", 2, "xi");
+	SQAIRoad.DefSQMethod(engine, &AIRoad::IsRoadTile,                    "IsRoadTile",                    2, "xi");
+	SQAIRoad.DefSQMethod(engine, &AIRoad::IsRoadDepotTile,               "IsRoadDepotTile",               2, "xi");
+	SQAIRoad.DefSQMethod(engine, &AIRoad::IsRoadStationTile,             "IsRoadStationTile",             2, "xi");
+	SQAIRoad.DefSQMethod(engine, &AIRoad::IsDriveThroughRoadStationTile, "IsDriveThroughRoadStationTile", 2, "xi");
+	SQAIRoad.DefSQMethod(engine, &AIRoad::AreRoadTilesConnected,         "AreRoadTilesConnected",         3, "xii");
+	SQAIRoad.DefSQMethod(engine, &AIRoad::GetRoadDepotFrontTile,         "GetRoadDepotFrontTile",         2, "xi");
+	SQAIRoad.DefSQMethod(engine, &AIRoad::GetRoadStationFrontTile,       "GetRoadStationFrontTile",       2, "xi");
+	SQAIRoad.DefSQMethod(engine, &AIRoad::GetDriveThroughBackTile,       "GetDriveThroughBackTile",       2, "xi");
+	SQAIRoad.DefSQMethod(engine, &AIRoad::BuildRoad,                     "BuildRoad",                     3, "xii");
+	SQAIRoad.DefSQMethod(engine, &AIRoad::BuildRoadDepot,                "BuildRoadDepot",                3, "xii");
+	SQAIRoad.DefSQMethod(engine, &AIRoad::BuildRoadStation,              "BuildRoadStation",              5, "xiibb");
+	SQAIRoad.DefSQMethod(engine, &AIRoad::RemoveRoad,                    "RemoveRoad",                    3, "xii");
+	SQAIRoad.DefSQMethod(engine, &AIRoad::RemoveRoadDepot,               "RemoveRoadDepot",               2, "xi");
+	SQAIRoad.DefSQMethod(engine, &AIRoad::RemoveRoadStation,             "RemoveRoadStation",             2, "xi");
 
 	SQAIRoad.PostRegister(engine);
 }