(svn r9283) [NoAI] -Add: API for adding/removing road, road depots and road stations. noai
authorrubidium
Sun, 18 Mar 2007 18:06:45 +0000
branchnoai
changeset 9453 727ff178a582
parent 9452 4c5eedbc3ba9
child 9454 ee6a65b37b82
(svn r9283) [NoAI] -Add: API for adding/removing road, road depots and road stations.
projects/openttd.vcproj
projects/openttd_vs80.vcproj
source.list
src/ai/api/ai_road.cpp
src/ai/api/ai_road.hpp
--- a/projects/openttd.vcproj	Sun Mar 18 18:02:27 2007 +0000
+++ b/projects/openttd.vcproj	Sun Mar 18 18:06:45 2007 +0000
@@ -1036,6 +1036,9 @@
 				RelativePath=".\..\src\ai\api\ai_object.hpp">
 			</File>
 			<File
+				RelativePath=".\..\src\ai\api\ai_road.hpp">
+			</File>
+			<File
 				RelativePath=".\..\src\ai\api\ai_settings.hpp">
 			</File>
 			<File
@@ -1076,6 +1079,9 @@
 				RelativePath=".\..\src\ai\api\ai_object.cpp">
 			</File>
 			<File
+				RelativePath=".\..\src\ai\api\ai_road.cpp">
+			</File>
+			<File
 				RelativePath=".\..\src\ai\api\ai_settings.cpp">
 			</File>
 			<File
--- a/projects/openttd_vs80.vcproj	Sun Mar 18 18:02:27 2007 +0000
+++ b/projects/openttd_vs80.vcproj	Sun Mar 18 18:06:45 2007 +0000
@@ -1600,6 +1600,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\ai\api\ai_road.hpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\ai\api\ai_settings.hpp"
 				>
 			</File>
@@ -1652,6 +1656,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\ai\api\ai_road.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\ai\api\ai_settings.cpp"
 				>
 			</File>
--- a/source.list	Sun Mar 18 18:02:27 2007 +0000
+++ b/source.list	Sun Mar 18 18:06:45 2007 +0000
@@ -317,6 +317,7 @@
 ai/api/ai_industry.hpp
 ai/api/ai_map.hpp
 ai/api/ai_object.hpp
+ai/api/ai_road.hpp
 ai/api/ai_settings.hpp
 ai/api/ai_town.hpp
 ai/api/ai_testmode.hpp
@@ -331,6 +332,7 @@
 ai/api/ai_industry.cpp
 ai/api/ai_map.cpp
 ai/api/ai_object.cpp
+ai/api/ai_road.cpp
 ai/api/ai_settings.cpp
 ai/api/ai_town.cpp
 ai/api/ai_testmode.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_road.cpp	Sun Mar 18 18:06:45 2007 +0000
@@ -0,0 +1,85 @@
+/* $Id$ */
+
+/** @file ai_town.cpp handles the functions of the AIRoad class */
+
+#include "ai_road.hpp"
+#include "../../command.h"
+#include "../../road_map.h"
+#include "../../station_map.h"
+
+bool AIRoad::IsRoadTile(TileIndex tile)
+{
+	/* Outside of the map */
+	if (tile >= _map_size) return false;
+
+	return IsTileType(tile, MP_STREET) && GetRoadTileType(tile) != ROAD_TILE_DEPOT;
+}
+
+bool AIRoad::BuildRoad(TileIndex start, TileIndex end)
+{
+	/* Outside of the map */
+	if (start >= _map_size || end >= _map_size || start == end) return false;
+	/* Not on one line */
+	if (TileX(start) != TileX(end) &&
+			TileY(start) != TileY(end)) return false;
+
+	return this->DoCommand(end, start, (TileX(start) != TileX(end) ? 4 : 0) | 3, DC_EXEC, CMD_BUILD_LONG_ROAD);
+}
+
+bool AIRoad::BuildRoadDepot(TileIndex tile, TileIndex front)
+{
+	/* Outside of the map */
+	if (tile >= _map_size || tile == front) return false;
+
+	uint entrance_dir = (TileX(tile) > TileX(front)) << 1 || (TileY(tile) > TileY(front));
+
+	return this->DoCommand(tile, entrance_dir, 0, DC_EXEC, CMD_BUILD_ROAD_DEPOT);
+}
+
+bool AIRoad::BuildRoadStation(TileIndex tile, TileIndex front, bool bus, bool drive_through)
+{
+	/* Outside of the map */
+	if (tile >= _map_size || tile == front) return false;
+
+	uint entrance_dir;
+	if (drive_through) {
+		entrance_dir = TileX(tile) != TileX(front);
+	} else {
+		entrance_dir = (TileX(tile) > TileX(front)) << 1 || (TileY(tile) > TileY(front));
+	}
+
+	return this->DoCommand(tile, entrance_dir, drive_through << 1 | bus, DC_EXEC, CMD_BUILD_ROAD_STOP);
+}
+
+bool AIRoad::RemoveRoad(TileIndex start, TileIndex end)
+{
+	/* Outside of the map */
+	if (start >= _map_size || end >= _map_size) return false;
+	/* Not on one line */
+	if (TileX(start) != TileX(end) &&
+			TileY(start) != TileY(end)) return false;
+
+	return this->DoCommand(end, start, (TileX(start) != TileX(end) ? 4 : 0) | 3, DC_EXEC, CMD_REMOVE_LONG_ROAD);
+}
+
+bool AIRoad::RemoveRoadDepot(TileIndex tile)
+{
+	/* Outside of the map */
+	if (tile >= _map_size) return false;
+
+	/* Not a road depot tile */
+	if (!IsTileType(tile, MP_STREET) || GetRoadTileType(tile) != ROAD_TILE_DEPOT) return false;
+
+	return this->DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
+}
+
+bool AIRoad::RemoveRoadStation(TileIndex tile)
+{
+	/* Outside of the map */
+	if (tile >= _map_size) return false;
+
+	/* Not a road station tile */
+	if (!IsTileType(tile, MP_STATION) || !IsRoadStop(tile)) return false;
+
+	return this->DoCommand(tile, 0, GetRoadStopType(tile), DC_EXEC, CMD_REMOVE_ROAD_STOP);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_road.hpp	Sun Mar 18 18:06:45 2007 +0000
@@ -0,0 +1,121 @@
+/* $Id$ */
+
+/** @file ai_map.hpp Everything to query and build roads */
+
+#ifndef AI_ROAD_HPP
+#define AI_ROAD_HPP
+
+#include "ai_object.hpp"
+
+/**
+ * Class that handles all road related functions.
+ *
+ * Roads are always build from tile center to tile center.
+ * Furthermore stations and depots need to be build on tiles
+ * that already have road on them. For depots and non-drive
+ * through stations that means that only one roadbit may be
+ * built at the location. The advantage of this method is
+ * that the AI does not need to think about pointing the
+ * exit of the depots and roadstations towards the correct
+ * direction. For drive-through roadstops this works about
+ * the same, though they have to be built on straight roads.
+ */
+class AIRoad : public AIObject {
+public:
+	/**
+	 * Checks whether the given tile is actually a tile with road.
+	 * @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 road.
+	 */
+	bool IsRoadTile(TileIndex tile);
+
+	/**
+	 * Builds a road from the center of tile start to the
+	 * center of tile end.
+	 * @param start the start tile of the road.
+	 * @param end   the end tile of the road.
+	 * @pre start is not equal to end
+	 * @pre start is always positive and smaller than AIMap::GetMapSize().
+	 * @pre end is always positive and smaller than AIMap::GetMapSize().
+	 * @pre start and end are in a straight line, i.e.
+	 *  AIMap::GetTileX(start) == AIMap::GetTileX(end) or
+	 *  AIMap::GetTileY(start) == AIMap::GetTileY(end).
+	 * @return whether the road has been/can be build or not.
+	 */
+	bool BuildRoad(TileIndex start, TileIndex end);
+
+	/**
+	 * Builds a road depot.
+	 * @param tile  place to build the depot.
+	 * @param front the tile exactly in front of the depot
+	 * @pre tile is always positive and smaller than AIMap::GetMapSize().
+	 * @pre front is always positive and smaller than AIMap::GetMapSize().
+	 * @pre tile is not equal to front
+	 * @return whether the road depot has been/can be build or not.
+	 */
+	bool BuildRoadDepot(TileIndex tile, TileIndex front);
+
+	/**
+	 * Builds a road bus or truck station.
+	 * @param tile  place to build the depot.
+	 * @param front the tile exactly in front of the station.
+	 *   For drive-through stations either entrance side can be used.
+	 * @param bus   whether to build a bus or truck station.
+	 * @param drive_through whether to make the station drive through or not.
+	 * @pre tile is always positive and smaller than AIMap::GetMapSize().
+	 * @pre front is always positive and smaller than AIMap::GetMapSize().
+	 * @pre tile is not equal to front
+	 * @return whether the station has been/can be build or not.
+	 */
+	bool BuildRoadStation(TileIndex tile, TileIndex front, bool bus, bool drive_through);
+
+	/**
+	 * Removes a road from the center of tile start to the
+	 * center of tile end.
+	 * @param start the start tile of the road.
+	 * @param end   the end tile of the road.
+	 * @pre start is always positive and smaller than AIMap::GetMapSize().
+	 * @pre end is always positive and smaller than AIMap::GetMapSize().
+	 * @pre start and end are in a straight line, i.e.
+	 *  AIMap::GetTileX(start) == AIMap::GetTileX(end) or
+	 *  AIMap::GetTileY(start) == AIMap::GetTileY(end).
+	 * @return whether the road has been/can be removed or not.
+	 */
+	bool RemoveRoad(TileIndex start, TileIndex end);
+
+	/**
+	 * Removes a road depot.
+	 * @param tile place to remove the depot from.
+	 * @pre tile is always positive and smaller than AIMap::GetMapSize().
+	 * @pre tile is a road depot.
+	 * @return whether the road depot has been/can be removed or not.
+	 */
+	bool RemoveRoadDepot(TileIndex tile);
+
+	/**
+	 * Removes a road bus or truck station.
+	 * @param tile place to remove the station from.
+	 * @pre tile is always positive and smaller than AIMap::GetMapSize().
+	 * @pre tile is a road station.
+	 * @return whether the station has been/can be removed or not.
+	 */
+	bool RemoveRoadStation(TileIndex tile);
+};
+
+#ifdef DEFINE_SQUIRREL_CLASS
+void SQAIRoadRegister(Squirrel *engine) {
+	DefSQClass <AIRoad> SQAIRoad("AIRoad");
+	SQAIRoad.PreRegister(engine);
+	SQAIRoad.AddConstructor(engine);
+	SQAIRoad.DefSQFunction(engine, &AIRoad::BuildRoad,         "BuildRoad");
+	SQAIRoad.DefSQFunction(engine, &AIRoad::BuildRoadDepot,    "BuildRoadDepot");
+	SQAIRoad.DefSQFunction(engine, &AIRoad::BuildRoadStation,  "BuildRoadStation");
+	SQAIRoad.DefSQFunction(engine, &AIRoad::RemoveRoad,        "RemoveRoad");
+	SQAIRoad.DefSQFunction(engine, &AIRoad::RemoveRoadDepot,   "RemoveRoadDepot");
+	SQAIRoad.DefSQFunction(engine, &AIRoad::RemoveRoadStation, "RemoveRoadStation");
+	SQAIRoad.PostRegister(engine);
+}
+#endif /* SQUIRREL_CLASS */
+
+#endif /* AI_ROAD_HPP */