(svn r13613) [NoAI] -Add: added AIStation::GetNearestTown(), which returns the nearest town AT MOMENT OF STATION CREATION (Yexo) noai
authortruebrain
Mon, 23 Jun 2008 12:33:38 +0000
branchnoai
changeset 11056 d91b79ffff9c
parent 11053 d8d48e076a3f
child 11057 188a9ca6d8de
(svn r13613) [NoAI] -Add: added AIStation::GetNearestTown(), which returns the nearest town AT MOMENT OF STATION CREATION (Yexo)
bin/ai/regression/regression.nut
bin/ai/regression/regression.txt
src/ai/api/ai_station.cpp
src/ai/api/ai_station.hpp
src/ai/api/ai_station.hpp.sq
--- a/bin/ai/regression/regression.nut	Sun Jun 22 21:41:03 2008 +0000
+++ b/bin/ai/regression/regression.nut	Mon Jun 23 12:33:38 2008 +0000
@@ -926,6 +926,10 @@
 	print("  GetCoverageRadius(TRUCK): " + AIStation.GetCoverageRadius(AIStation.STATION_TRUCK_STOP));
 	print("  GetCoverageRadius(TRAIN): " + AIStation.GetCoverageRadius(AIStation.STATION_TRAIN));
 
+	print("  GetNearestTown():         " + AIStation.GetNearestTown(0));
+	print("  GetNearestTown():         " + AIStation.GetNearestTown(10000));
+	print("  GetNearestTown():         " + AIStation.GetNearestTown(3));
+
 	local list = AIStationList(AIStation.STATION_BUS_STOP + AIStation.STATION_TRUCK_STOP);
 
 	print("");
--- a/bin/ai/regression/regression.txt	Sun Jun 22 21:41:03 2008 +0000
+++ b/bin/ai/regression/regression.txt	Mon Jun 23 12:33:38 2008 +0000
@@ -6130,6 +6130,9 @@
   GetCoverageRadius(BUS):   3
   GetCoverageRadius(TRUCK): 3
   GetCoverageRadius(TRAIN): 4
+  GetNearestTown():         15
+  GetNearestTown():         65535
+  GetNearestTown():         24
 
 --StationList--
   Count():             2
--- a/src/ai/api/ai_station.cpp	Sun Jun 22 21:41:03 2008 +0000
+++ b/src/ai/api/ai_station.cpp	Mon Jun 23 12:33:38 2008 +0000
@@ -16,6 +16,7 @@
 #include "../../core/alloc_func.hpp"
 #include "../../player_func.h"
 #include "../../settings_type.h"
+#include "../../town.h"
 #include "table/strings.h"
 
 /* static */ bool AIStation::IsValidStation(StationID station_id)
@@ -136,3 +137,10 @@
 
 	return false;
 }
+
+/* static */ TownID AIStation::GetNearestTown(StationID station_id)
+{
+	if (!IsValidStation(station_id)) return INVALID_TOWN;
+
+	return ::GetStation(station_id)->town->index;
+}
--- a/src/ai/api/ai_station.hpp	Sun Jun 22 21:41:03 2008 +0000
+++ b/src/ai/api/ai_station.hpp	Mon Jun 23 12:33:38 2008 +0000
@@ -164,6 +164,18 @@
 	 * @return True if the station has a station part of the type RoadType.
 	 */
 	static bool HasRoadType(StationID station_id, AIRoad::RoadType road_type);
+
+	/**
+	 * Get the town that was nearest to the given station when the station was built.
+	 * @param station_id The station to look at.
+	 * @return The TownID of the town whose center tile was closest to the station
+	 *  at the time the station was built.
+	 * @note There is no guarantee that the station is even near the returned town
+	 *  nor that the returns town is closest to the station now. A station that was
+	 *  'walked' to the other end of the map will still return the same town. Also,
+	 *  towns grow, towns change. So don't depend on this value too much.
+	 */
+	static TownID GetNearestTown(StationID station_id);
 };
 
 DECLARE_ENUM_AS_BIT_SET(AIStation::StationType);
--- a/src/ai/api/ai_station.hpp.sq	Sun Jun 22 21:41:03 2008 +0000
+++ b/src/ai/api/ai_station.hpp.sq	Mon Jun 23 12:33:38 2008 +0000
@@ -63,6 +63,7 @@
 	SQAIStation.DefSQStaticMethod(engine, &AIStation::IsWithinTownInfluence,      "IsWithinTownInfluence",      3, "xii");
 	SQAIStation.DefSQStaticMethod(engine, &AIStation::HasStationType,             "HasStationType",             3, "xii");
 	SQAIStation.DefSQStaticMethod(engine, &AIStation::HasRoadType,                "HasRoadType",                3, "xii");
+	SQAIStation.DefSQStaticMethod(engine, &AIStation::GetNearestTown,             "GetNearestTown",             2, "xi");
 
 	SQAIStation.PostRegister(engine);
 }