(svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList noai
authortruelight
Thu, 12 Jul 2007 15:51:19 +0000
branchnoai
changeset 9638 f6d169a5a45a
parent 9637 9f78a12a4f53
child 9639 ab47f718b853
(svn r10529) [NoAI] -Add: added AIStation to gather information about stations directly by Id, which is used now by AIStationList
bin/ai/regression/regression.nut
source.list
src/ai/ai_squirrel.cpp
src/ai/api/ai_station.cpp
src/ai/api/ai_station.hpp
src/ai/api/ai_station.hpp.sq
src/ai/api/ai_stationlist.cpp
src/ai/api/ai_stationlist_valuator.cpp
--- a/bin/ai/regression/regression.nut	Thu Jul 12 15:50:55 2007 +0000
+++ b/bin/ai/regression/regression.nut	Thu Jul 12 15:51:19 2007 +0000
@@ -278,27 +278,6 @@
 	print("    GetDriveThroughBackTile():     " + road.GetDriveThroughBackTile(33415));
 	print("    GetRoadStationFrontTile():     " + road.GetRoadStationFrontTile(33415));
 	print("    IsRoadTile():                  " + road.IsRoadTile(33415));
-
-	local list = AIStationList(AIStationList.STATION_BUS_STOP + AIStationList.STATION_TRUCK_STOP);
-
-	print("");
-	print("--StationList--");
-	print("  Count():             " + list.Count());
-	list.Valuate(AIStationListLocation());
-	print("  Location ListDump:");
-	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
-		print("    " + i + " => " + list.GetValue(i));
-	}
-	list.Valuate(AIStationListCargoWaiting(0));
-	print("  CargoWaiting(0) ListDump:");
-	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
-		print("    " + i + " => " + list.GetValue(i));
-	}
-	list.Valuate(AIStationListCargoWaiting(1));
-	print("  CargoWaiting(1) ListDump:");
-	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
-		print("    " + i + " => " + list.GetValue(i));
-	}
 }
 
 function Regression::Sign()
@@ -326,6 +305,42 @@
 	print("  GetSignCount():    " + sign.GetSignCount());
 }
 
+function Regression::Station()
+{
+	local station = AIStation();
+
+	print("");
+	print("--Station--");
+	print("  IsValidStation(0):        " + station.IsValidStation(0));
+	print("  IsValidStation(1000):     " + station.IsValidStation(1000));
+	print("  GetLocation(0):           " + station.GetLocation(0));
+	print("  GetLocation(1000):        " + station.GetLocation(1000));
+	print("  GetCargoWaiting(0, 0):    " + station.GetCargoWaiting(0, 0));
+	print("  GetCargoWaiting(1000, 0): " + station.GetCargoWaiting(1000, 0));
+	print("  GetCargoWaiting(0, 1000): " + station.GetCargoWaiting(0, 1000));
+
+	local list = AIStationList(AIStationList.STATION_BUS_STOP + AIStationList.STATION_TRUCK_STOP);
+
+	print("");
+	print("--StationList--");
+	print("  Count():             " + list.Count());
+	list.Valuate(AIStationListLocation());
+	print("  Location ListDump:");
+	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
+		print("    " + i + " => " + list.GetValue(i));
+	}
+	list.Valuate(AIStationListCargoWaiting(0));
+	print("  CargoWaiting(0) ListDump:");
+	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
+		print("    " + i + " => " + list.GetValue(i));
+	}
+	list.Valuate(AIStationListCargoWaiting(1));
+	print("  CargoWaiting(1) ListDump:");
+	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
+		print("    " + i + " => " + list.GetValue(i));
+	}
+}
+
 function Regression::TileList()
 {
 	local list = AITileList();
@@ -554,6 +569,7 @@
 	this.List();
 	this.Road();
 	this.Sign();
+	this.Station();
 	this.TileList();
 	this.Town();
 	this.TownList();
--- a/source.list	Thu Jul 12 15:50:55 2007 +0000
+++ b/source.list	Thu Jul 12 15:51:19 2007 +0000
@@ -346,6 +346,7 @@
 ai/api/ai_road.hpp
 ai/api/ai_settings.hpp
 ai/api/ai_sign.hpp
+ai/api/ai_station.hpp
 ai/api/ai_stationlist.hpp
 ai/api/ai_stationlist_valuator.hpp
 ai/api/ai_testmode.hpp
@@ -376,6 +377,7 @@
 ai/api/ai_road.cpp
 ai/api/ai_settings.cpp
 ai/api/ai_sign.cpp
+ai/api/ai_station.cpp
 ai/api/ai_stationlist.cpp
 ai/api/ai_stationlist_valuator.cpp
 ai/api/ai_testmode.cpp
--- a/src/ai/ai_squirrel.cpp	Thu Jul 12 15:50:55 2007 +0000
+++ b/src/ai/ai_squirrel.cpp	Thu Jul 12 15:51:19 2007 +0000
@@ -35,6 +35,7 @@
 #include "api/ai_road.hpp.sq"
 #include "api/ai_settings.hpp.sq"
 #include "api/ai_sign.hpp.sq"
+#include "api/ai_station.hpp.sq"
 #include "api/ai_stationlist.hpp.sq"
 #include "api/ai_stationlist_valuator.hpp.sq"
 #include "api/ai_testmode.hpp.sq"
@@ -217,6 +218,7 @@
 	SQAIStationListCargoWaitingRegister(this->engine);
 	SQAIStationListLocationRegister(this->engine);
 	SQAIStationListRegister(this->engine);
+	SQAIStationRegister(this->engine);
 	SQAITestModeRegister(this->engine);
 	SQAITileListBuildableRegister(this->engine);
 	SQAITileListCargoAcceptanceRegister(this->engine);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_station.cpp	Thu Jul 12 15:51:19 2007 +0000
@@ -0,0 +1,27 @@
+/* $Id$ */
+
+/** @file ai_station.cpp handles the functions of the AIStation class */
+
+#include "ai_station.hpp"
+#include "ai_cargo.hpp"
+#include "../../station.h"
+
+/* static */ bool AIStation::IsValidStation(StationID station_id)
+{
+	return ::IsValidStationID(station_id) && ::GetStation(station_id)->owner == _current_player;
+}
+
+/* static */ TileIndex AIStation::GetLocation(StationID station_id)
+{
+	if (!AIStation::IsValidStation(station_id)) return INVALID_TILE;
+
+	return ::GetStation(station_id)->xy;
+}
+
+/* static */ int32 AIStation::GetCargoWaiting(StationID station_id, CargoID cargo_id)
+{
+	if (!AIStation::IsValidStation(station_id)) return -1;
+	if (!AICargo::IsValidCargo(cargo_id)) return -1;
+
+	return ::GetStation(station_id)->goods[cargo_id].cargo.Count();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_station.hpp	Thu Jul 12 15:51:19 2007 +0000
@@ -0,0 +1,46 @@
+/* $Id$ */
+
+/** @file ai_station.hpp Everything to query and build stations */
+
+#ifndef AI_STATION_HPP
+#define AI_STATION_HPP
+
+#include "ai_object.hpp"
+
+/**
+ * Class that handles all station related functions.
+ */
+class AIStation : public AIObject {
+public:
+	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AIStation"; }
+
+	/**
+	 * Checks whether the given station is valid and owned by you.
+	 * @param station_id the station to check.
+	 * @return true if and only if the station is valid.
+	 */
+	static bool IsValidStation(StationID station_id);
+
+	/**
+	 * Get the current location of a station.
+	 * @param station_id the station to get the location of.
+	 * @pre IsValidStation(station_id).
+	 * @return the tile the station is currently on.
+	 */
+	static TileIndex GetLocation(StationID station_id);
+
+	/**
+	 * See how much cargo there is waiting on a station.
+	 * @param station_id the station to get the cargo-waiting of.
+	 * @param cargo_id the cargo to get the cargo-waiting of.
+	 * @pre IsValidStation(station_id).
+	 * @pre IsValidCargo(cargo_id).
+	 * @return the amount of units waiting at the station.
+	 */
+	static int32 GetCargoWaiting(StationID station_id, CargoID cargo_id);
+};
+
+#endif /* AI_STATION_HPP */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_station.hpp.sq	Thu Jul 12 15:51:19 2007 +0000
@@ -0,0 +1,22 @@
+#include "ai_station.hpp"
+
+namespace SQConvert {
+	/* Allow AIStation to be used as Squirrel parameter */
+	template <> AIStation *GetParam(ForceType<AIStation *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIStation *)instance; }
+	template <> AIStation &GetParam(ForceType<AIStation &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIStation *)instance; }
+	template <> const AIStation *GetParam(ForceType<const AIStation *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIStation *)instance; }
+	template <> const AIStation &GetParam(ForceType<const AIStation &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIStation *)instance; }
+}; // namespace SQConvert
+
+void SQAIStationRegister(Squirrel *engine) {
+	DefSQClass <AIStation> SQAIStation("AIStation");
+	SQAIStation.PreRegister(engine);
+	SQAIStation.AddConstructor<void (AIStation::*)(), 1>(engine, "x");
+
+	SQAIStation.DefSQStaticMethod(engine, &AIStation::GetClassName,    "GetClassName",    1, "x");
+	SQAIStation.DefSQStaticMethod(engine, &AIStation::IsValidStation,  "IsValidStation",  2, "xi");
+	SQAIStation.DefSQStaticMethod(engine, &AIStation::GetLocation,     "GetLocation",     2, "xi");
+	SQAIStation.DefSQStaticMethod(engine, &AIStation::GetCargoWaiting, "GetCargoWaiting", 3, "xii");
+
+	SQAIStation.PostRegister(engine);
+}
--- a/src/ai/api/ai_stationlist.cpp	Thu Jul 12 15:50:55 2007 +0000
+++ b/src/ai/api/ai_stationlist.cpp	Thu Jul 12 15:51:19 2007 +0000
@@ -1,3 +1,5 @@
+/* $Id$ */
+
 #include "ai_stationlist.hpp"
 #include "../../player.h"
 #include "../../station.h"
--- a/src/ai/api/ai_stationlist_valuator.cpp	Thu Jul 12 15:50:55 2007 +0000
+++ b/src/ai/api/ai_stationlist_valuator.cpp	Thu Jul 12 15:51:19 2007 +0000
@@ -1,15 +1,14 @@
+/* $Id$ */
+
 #include "ai_stationlist_valuator.hpp"
-#include "../../station.h"
+#include "ai_station.hpp"
 
 int32 AIStationListLocation::Valuate(int32 station) const
 {
-	if (!IsValidStationID(station)) return INVALID_TILE;
-	return GetStation(station)->xy;
+	return AIStation::GetLocation(station);
 }
 
 int32 AIStationListCargoWaiting::Valuate(int32 station) const
 {
-	if (!IsValidStationID(station)) return -1;
-	return GetStation(station)->goods[this->cargo_type].cargo.Count();
+	return AIStation::GetCargoWaiting(station, this->cargo_type);
 }
-