(svn r13557) [NoAI] -Add: function to determine whether two adjacent tiles can be travelled by a ship. noai
authorrubidium
Tue, 17 Jun 2008 22:37:09 +0000
branchnoai
changeset 11001 716595242a19
parent 11000 51305152af09
child 11002 3a11ba88eb7c
(svn r13557) [NoAI] -Add: function to determine whether two adjacent tiles can be travelled by a ship.
src/ai/api/ai_marine.cpp
src/ai/api/ai_marine.hpp
src/ai/api/ai_marine.hpp.sq
--- a/src/ai/api/ai_marine.cpp	Tue Jun 17 21:44:41 2008 +0000
+++ b/src/ai/api/ai_marine.cpp	Tue Jun 17 22:37:09 2008 +0000
@@ -8,6 +8,7 @@
 #include "../../variables.h"
 #include "../../station_map.h"
 #include "../../water_map.h"
+#include "../../tile_cmd.h"
 
 
 /* static */ bool AIMarine::IsWaterDepotTile(TileIndex tile)
@@ -45,6 +46,27 @@
 	return ::IsTileType(tile, MP_WATER) && ::IsCanal(tile);
 }
 
+/* static */ bool AIMarine::AreWaterTilesConnected(TileIndex t1, TileIndex t2)
+{
+	if (!::IsValidTile(t1)) return false;
+	if (!::IsValidTile(t2)) return false;
+
+	/* Tiles not neighbouring */
+	if (::DistanceManhattan(t1, t2) != 1) return false;
+	if (t1 > t2) Swap(t1, t2);
+
+	uint32 gtts1 = ::GetTileTrackStatus(t1, TRANSPORT_WATER, 0);
+	uint32 gtts2 = ::GetTileTrackStatus(t2, TRANSPORT_WATER, 0);
+
+	/* Ship can't travel on one of the tiles. */
+	if (gtts1 == 0 || gtts2 == 0) return false;
+
+	DiagDirection to_other_tile = (TileX(t1) == TileX(t2)) ? DIAGDIR_SE : DIAGDIR_SW;
+
+	/* Check whether we can 'leave' the tile at the border and 'enter' the other tile at the border */
+	return (gtts1 & DiagdirReachesTrackdirs(ReverseDiagDir(to_other_tile))) != 0 && (gtts2 & DiagdirReachesTrackdirs(to_other_tile)) != 0;
+}
+
 /* static */ bool AIMarine::BuildWaterDepot(TileIndex tile, bool vertical)
 {
 	EnforcePrecondition(false, ::IsValidTile(tile));
--- a/src/ai/api/ai_marine.hpp	Tue Jun 17 21:44:41 2008 +0000
+++ b/src/ai/api/ai_marine.hpp	Tue Jun 17 22:37:09 2008 +0000
@@ -67,6 +67,19 @@
 	static bool IsCanalTile(TileIndex tile);
 
 	/**
+	 * Checks whether the given tiles are directly connected, i.e. whether
+	 *  a ship vehicle can travel from the center of the first tile to the
+	 *  center of the second tile.
+	 * @param tile_from The source tile.
+	 * @param tile_to The destination tile.
+	 * @pre AIMap::IsValidTile(tile_from).
+	 * @pre AIMap::IsValidTile(tile_to).
+	 * @pre 'tile_from' and 'tile_to' are directly neighbouring tiles.
+	 * @return True if and only if a ship can go from tile_from to tile_to.
+	 */
+	static bool AreWaterTilesConnected(TileIndex tile_from, TileIndex tile_to);
+
+	/**
 	 * Builds a water depot on tile.
 	 * @param tile The tile where the water depot will be build.
 	 * @param vertical If true, depot will be vertical, else horizontal.
--- a/src/ai/api/ai_marine.hpp.sq	Tue Jun 17 21:44:41 2008 +0000
+++ b/src/ai/api/ai_marine.hpp.sq	Tue Jun 17 22:37:09 2008 +0000
@@ -28,22 +28,23 @@
 
 	AIError::RegisterErrorMapString(AIMarine::ERR_MARINE_MUST_BE_BUILT_ON_WATER, "ERR_MARINE_MUST_BE_BUILT_ON_WATER");
 
-	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::GetClassName,     "GetClassName",     1, "x");
-	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::IsWaterDepotTile, "IsWaterDepotTile", 2, "xi");
-	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::IsDockTile,       "IsDockTile",       2, "xi");
-	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::IsBuoyTile,       "IsBuoyTile",       2, "xi");
-	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::IsLockTile,       "IsLockTile",       2, "xi");
-	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::IsCanalTile,      "IsCanalTile",      2, "xi");
-	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::BuildWaterDepot,  "BuildWaterDepot",  3, "xib");
-	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::BuildDock,        "BuildDock",        2, "xi");
-	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::BuildBuoy,        "BuildBuoy",        2, "xi");
-	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::BuildLock,        "BuildLock",        2, "xi");
-	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::BuildCanal,       "BuildCanal",       2, "xi");
-	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::RemoveWaterDepot, "RemoveWaterDepot", 2, "xi");
-	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::RemoveDock,       "RemoveDock",       2, "xi");
-	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::RemoveBuoy,       "RemoveBuoy",       2, "xi");
-	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::RemoveLock,       "RemoveLock",       2, "xi");
-	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::RemoveCanal,      "RemoveCanal",      2, "xi");
+	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::GetClassName,           "GetClassName",           1, "x");
+	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::IsWaterDepotTile,       "IsWaterDepotTile",       2, "xi");
+	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::IsDockTile,             "IsDockTile",             2, "xi");
+	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::IsBuoyTile,             "IsBuoyTile",             2, "xi");
+	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::IsLockTile,             "IsLockTile",             2, "xi");
+	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::IsCanalTile,            "IsCanalTile",            2, "xi");
+	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::AreWaterTilesConnected, "AreWaterTilesConnected", 3, "xii");
+	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::BuildWaterDepot,        "BuildWaterDepot",        3, "xib");
+	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::BuildDock,              "BuildDock",              2, "xi");
+	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::BuildBuoy,              "BuildBuoy",              2, "xi");
+	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::BuildLock,              "BuildLock",              2, "xi");
+	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::BuildCanal,             "BuildCanal",             2, "xi");
+	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::RemoveWaterDepot,       "RemoveWaterDepot",       2, "xi");
+	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::RemoveDock,             "RemoveDock",             2, "xi");
+	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::RemoveBuoy,             "RemoveBuoy",             2, "xi");
+	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::RemoveLock,             "RemoveLock",             2, "xi");
+	SQAIMarine.DefSQStaticMethod(engine, &AIMarine::RemoveCanal,            "RemoveCanal",            2, "xi");
 
 	SQAIMarine.PostRegister(engine);
 }