changeset 12660:bb0f5dabdc1f draft

(svn r17115) -Add [NoAI]: AISubsidy::SubsidyParticipantType, AISubsidy::GetSourceType, AISubsidy::GetSourceIndex, AISubsidy::GetDestinationType, AISubsidy::GetDestinationIndex for better subsidy management -Change [NoAI]: mark AISubsidy::SourceIsTown, AISubsidy::GetSource, AISubsidy::GetDestinationType, AISubsidy::GetDestination as deprecated
author smatz <smatz@openttd.org>
date Sat, 08 Aug 2009 16:53:22 +0000
parents 8d9de94b232e
children 5f51fd0bb9a6
files bin/ai/regression/regression.nut bin/ai/regression/regression.txt src/ai/api/ai_changelog.hpp src/ai/api/ai_subsidy.cpp src/ai/api/ai_subsidy.hpp src/ai/api/ai_subsidy.hpp.sq
diffstat 6 files changed, 131 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/bin/ai/regression/regression.nut
+++ b/bin/ai/regression/regression.nut
@@ -1636,15 +1636,15 @@
 function Regression::PrintSubsidy(subsidy_id)
 {
 	print("      --Subsidy (" + subsidy_id + ") --");
-	print("        IsValidSubsidy():     " + AISubsidy.IsValidSubsidy(subsidy_id));
-	print("        IsAwarded():          " + AISubsidy.IsAwarded(subsidy_id));
-	print("        GetAwardedTo():       " + AISubsidy.GetAwardedTo(subsidy_id));
-	print("        GetExpireDate():      " + AISubsidy.GetExpireDate(subsidy_id));
-	print("        SourceIsTown():       " + AISubsidy.SourceIsTown(subsidy_id));
-	print("        GetSource():          " + AISubsidy.GetSource(subsidy_id));
-	print("        DestionationIsTown(): " + AISubsidy.DestinationIsTown(subsidy_id));
-	print("        GetDestionation():    " + AISubsidy.GetDestination(subsidy_id));
-	print("        GetCargoType():       " + AISubsidy.GetCargoType(subsidy_id));
+	print("        IsValidSubsidy():      " + AISubsidy.IsValidSubsidy(subsidy_id));
+	print("        IsAwarded():           " + AISubsidy.IsAwarded(subsidy_id));
+	print("        GetAwardedTo():        " + AISubsidy.GetAwardedTo(subsidy_id));
+	print("        GetExpireDate():       " + AISubsidy.GetExpireDate(subsidy_id));
+	print("        GetSourceType():       " + AISubsidy.GetSourceType(subsidy_id));
+	print("        GetSourceIndex():      " + AISubsidy.GetSourceIndex(subsidy_id));
+	print("        GetDestinationType():  " + AISubsidy.GetDestinationType(subsidy_id));
+	print("        GetDestinationIndex(): " + AISubsidy.GetDestinationIndex(subsidy_id));
+	print("        GetCargoType():        " + AISubsidy.GetCargoType(subsidy_id));
 }
 
 
--- a/bin/ai/regression/regression.txt
+++ b/bin/ai/regression/regression.txt
@@ -8524,15 +8524,15 @@
 
   First Subsidy Test
       --Subsidy (0) --
-        IsValidSubsidy():     true
-        IsAwarded():          false
-        GetAwardedTo():       -1
-        GetExpireDate():      714080
-        SourceIsTown():       true
-        GetSource():          15
-        DestionationIsTown(): true
-        GetDestionation():    7
-        GetCargoType():       0
+        IsValidSubsidy():      true
+        IsAwarded():           false
+        GetAwardedTo():        -1
+        GetExpireDate():       714080
+        GetSourceType():       1
+        GetSourceIndex():      15
+        GetDestinationType():  1
+        GetDestinationIndex(): 7
+        GetCargoType():        0
   GetNextEvent:          instance
     GetEventType:        6
       Unknown Event
--- a/src/ai/api/ai_changelog.hpp
+++ b/src/ai/api/ai_changelog.hpp
@@ -12,6 +12,10 @@
  * \li AISign::GetMaxSignID
  * \li AITile::GetHeight
  * \li AIBaseStation::WAYPOINT_INVALID
+ * \li AISubsidy::SourceIsTown
+ * \li AISubsidy::GetSource
+ * \li AISubsidy::DestinationIsTown
+ * \li AISubsidy::GetDestination
  *
  * \b 0.8.0
  *
@@ -19,6 +23,11 @@
  * \li AIBaseStation
  * \li AIBuoyList
  * \li AIRail::RemoveRailWaypointTileRect
+ * \li AISubsidy::SubsidyParticipantType
+ * \li AISubsidy::GetSourceType
+ * \li AISubsidy::GetSourceIndex
+ * \li AISubsidy::GetDestinationType
+ * \li AISubsidy::GetDestinationIndex
  *
  * Other changes:
  * \li The GetName / SetName / GetLocation functions were moved from AIStation
@@ -30,7 +39,7 @@
  *     savegames are invalid
  * \li WAYPOINT_INVALID is now deprecated, use STATION_INVALID instead
  * \li AISubsidy::GetSource and AISubsidy::GetDestination return STATION_INVALID
- *     for awarded subsidies
+ *     for awarded subsidies. They are deprecated as well
  * \li AIs can create subclasses of API classes and use API constants as part
  *     of their own constants
  * \li AIVehicleList_Station now also works for waypoints
--- a/src/ai/api/ai_subsidy.cpp
+++ b/src/ai/api/ai_subsidy.cpp
@@ -52,6 +52,7 @@
 
 /* static */ bool AISubsidy::SourceIsTown(SubsidyID subsidy_id)
 {
+	AILog::Warning("AISubsidy::SourceIsTown is deprecated and will be removed soon, please use AISubsidy::GetSourceType instead.");
 	if (!IsValidSubsidy(subsidy_id) || IsAwarded(subsidy_id)) return false;
 
 	return ::Subsidy::Get(subsidy_id)->src_type == ST_TOWN;
@@ -59,6 +60,7 @@
 
 /* static */ int32 AISubsidy::GetSource(SubsidyID subsidy_id)
 {
+	AILog::Warning("AISubsidy::GetSource is deprecated and will be removed soon, please use AISubsidy::GetSourceIndex instead.");
 	if (!IsValidSubsidy(subsidy_id)) return INVALID_STATION;
 
 	if (IsAwarded(subsidy_id)) {
@@ -69,8 +71,23 @@
 	return ::Subsidy::Get(subsidy_id)->src;
 }
 
+/* static */ AISubsidy::SubsidyParticipantType AISubsidy::GetSourceType(SubsidyID subsidy_id)
+{
+	if (!IsValidSubsidy(subsidy_id)) return SPT_INVALID;
+
+	return (SubsidyParticipantType)(uint)::Subsidy::Get(subsidy_id)->src_type;
+}
+
+/* static */ int32 AISubsidy::GetSourceIndex(SubsidyID subsidy_id)
+{
+	if (!IsValidSubsidy(subsidy_id)) return INVALID_STATION;
+
+	return ::Subsidy::Get(subsidy_id)->src;
+}
+
 /* static */ bool AISubsidy::DestinationIsTown(SubsidyID subsidy_id)
 {
+	AILog::Warning("AISubsidy::DestinationIsTown is deprecated and will be removed soon, please use AISubsidy::GetDestinationType instead.");
 	if (!IsValidSubsidy(subsidy_id) || IsAwarded(subsidy_id)) return false;
 
 	return ::Subsidy::Get(subsidy_id)->dst_type == ST_TOWN;
@@ -78,6 +95,7 @@
 
 /* static */ int32 AISubsidy::GetDestination(SubsidyID subsidy_id)
 {
+	AILog::Warning("AISubsidy::GetDestination is deprecated and will be removed soon, please use AISubsidy::GetDestinationIndex instead.");
 	if (!IsValidSubsidy(subsidy_id)) return INVALID_STATION;
 
 	if (IsAwarded(subsidy_id)) {
@@ -87,3 +105,17 @@
 
 	return ::Subsidy::Get(subsidy_id)->dst;
 }
+
+/* static */ AISubsidy::SubsidyParticipantType AISubsidy::GetDestinationType(SubsidyID subsidy_id)
+{
+	if (!IsValidSubsidy(subsidy_id)) return SPT_INVALID;
+
+	return (SubsidyParticipantType)(uint)::Subsidy::Get(subsidy_id)->dst_type;
+}
+
+/* static */ int32 AISubsidy::GetDestinationIndex(SubsidyID subsidy_id)
+{
+	if (!IsValidSubsidy(subsidy_id)) return INVALID_STATION;
+
+	return ::Subsidy::Get(subsidy_id)->dst;
+}
--- a/src/ai/api/ai_subsidy.hpp
+++ b/src/ai/api/ai_subsidy.hpp
@@ -16,6 +16,16 @@
 	static const char *GetClassName() { return "AISubsidy"; }
 
 	/**
+	 * Enumeration for source and destination of a subsidy.
+	 * @note The list of values may grow in future.
+	 */
+	enum SubsidyParticipantType {
+		SPT_INDUSTRY =    0, //!< Subsidy participant is an industry
+		SPT_TOWN     =    1, //!< Subsidy participant is a town
+		SPT_INVALID  = 0xFF, //!< Invalid/unknown participant type
+	};
+
+	/**
 	 * Check whether this is a valid SubsidyID.
 	 * @param subsidy_id The SubsidyID to check.
 	 * @return True if and only if this subsidy is still valid.
@@ -64,6 +74,7 @@
 	 * @param subsidy_id The SubsidyID to check.
 	 * @pre IsValidSubsidy(subsidy_id) && !IsAwarded(subsidy_id).
 	 * @return True if the source is a town, false if it is an industry.
+	 * @deprecated Use GetSourceType() instead.
 	 */
 	static bool SourceIsTown(SubsidyID subsidy_id);
 
@@ -75,14 +86,34 @@
 	 * @param subsidy_id The SubsidyID to check.
 	 * @pre IsValidSubsidy(subsidy_id).
 	 * @return One of TownID/IndustryID/INVALID_STATION.
+	 * @deprecated Use GetSourceIndex() instead.
 	 */
 	static int32 GetSource(SubsidyID subsidy_id);
 
 	/**
+	 * Returns the type of source of subsidy.
+	 * @param subsidy_id The SubsidyID to check.
+	 * @pre IsValidSubsidy(subsidy_id).
+	 * @return Type of source of subsidy.
+	 */
+	static SubsidyParticipantType GetSourceType(SubsidyID subsidy_id);
+
+	/**
+	 * Return the source IndustryID/TownID the subsidy is for.
+	 * \li GetSourceType(subsidy_id) == SPT_INDUSTRY -> return the IndustryID.
+	 * \li GetSourceType(subsidy_id) == SPT_TOWN -> return the TownID.
+	 * @param subsidy_id The SubsidyID to check.
+	 * @pre IsValidSubsidy(subsidy_id).
+	 * @return One of TownID/IndustryID.
+	 */
+	static int32 GetSourceIndex(SubsidyID subsidy_id);
+
+	/**
 	 * Is the destination of the subsidy a town or an industry.
 	 * @param subsidy_id The SubsidyID to check.
 	 * @pre IsValidSubsidy(subsidy_id) && !IsAwarded(subsidy_id).
 	 * @return True if the destination is a town, false if it is an industry.
+	 * @deprecated Use GetDestinationType() instead.
 	 */
 	static bool DestinationIsTown(SubsidyID subsidy_id);
 
@@ -94,8 +125,27 @@
 	 * @param subsidy_id the SubsidyID to check.
 	 * @pre IsValidSubsidy(subsidy_id).
 	 * @return One of TownID/IndustryID/INVALID_STATION.
+	 * @deprecated Use GetDestinationIndex() instead.
 	 */
 	static int32 GetDestination(SubsidyID subsidy_id);
+
+	/**
+	 * Returns the type of destination of subsidy.
+	 * @param subsidy_id The SubsidyID to check.
+	 * @pre IsValidSubsidy(subsidy_id).
+	 * @return Type of destination of subsidy.
+	 */
+	static SubsidyParticipantType GetDestinationType(SubsidyID subsidy_id);
+
+	/**
+	 * Return the destination IndustryID/TownID the subsidy is for.
+	 * \li GetDestinationType(subsidy_id) == SPT_INDUSTRY -> return the IndustryID.
+	 * \li GetDestinationType(subsidy_id) == SPT_TOWN -> return the TownID.
+	 * @param subsidy_id the SubsidyID to check.
+	 * @pre IsValidSubsidy(subsidy_id).
+	 * @return One of TownID/IndustryID.
+	 */
+	static int32 GetDestinationIndex(SubsidyID subsidy_id);
 };
 
 #endif /* AI_SUBSIDY_HPP */
--- a/src/ai/api/ai_subsidy.hpp.sq
+++ b/src/ai/api/ai_subsidy.hpp.sq
@@ -4,6 +4,10 @@
 #include "ai_subsidy.hpp"
 
 namespace SQConvert {
+	/* Allow enums to be used as Squirrel parameters */
+	template <> AISubsidy::SubsidyParticipantType GetParam(ForceType<AISubsidy::SubsidyParticipantType>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (AISubsidy::SubsidyParticipantType)tmp; }
+	template <> int Return<AISubsidy::SubsidyParticipantType>(HSQUIRRELVM vm, AISubsidy::SubsidyParticipantType res) { sq_pushinteger(vm, (int32)res); return 1; }
+
 	/* Allow AISubsidy to be used as Squirrel parameter */
 	template <> AISubsidy *GetParam(ForceType<AISubsidy *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AISubsidy *)instance; }
 	template <> AISubsidy &GetParam(ForceType<AISubsidy &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AISubsidy *)instance; }
@@ -17,15 +21,23 @@
 	SQAISubsidy.PreRegister(engine);
 	SQAISubsidy.AddConstructor<void (AISubsidy::*)(), 1>(engine, "x");
 
-	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::IsValidSubsidy,    "IsValidSubsidy",    2, ".i");
-	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::IsAwarded,         "IsAwarded",         2, ".i");
-	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::GetAwardedTo,      "GetAwardedTo",      2, ".i");
-	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::GetExpireDate,     "GetExpireDate",     2, ".i");
-	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::GetCargoType,      "GetCargoType",      2, ".i");
-	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::SourceIsTown,      "SourceIsTown",      2, ".i");
-	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::GetSource,         "GetSource",         2, ".i");
-	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::DestinationIsTown, "DestinationIsTown", 2, ".i");
-	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::GetDestination,    "GetDestination",    2, ".i");
+	SQAISubsidy.DefSQConst(engine, AISubsidy::SPT_INDUSTRY, "SPT_INDUSTRY");
+	SQAISubsidy.DefSQConst(engine, AISubsidy::SPT_TOWN,     "SPT_TOWN");
+	SQAISubsidy.DefSQConst(engine, AISubsidy::SPT_INVALID,  "SPT_INVALID");
+
+	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::IsValidSubsidy,      "IsValidSubsidy",      2, ".i");
+	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::IsAwarded,           "IsAwarded",           2, ".i");
+	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::GetAwardedTo,        "GetAwardedTo",        2, ".i");
+	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::GetExpireDate,       "GetExpireDate",       2, ".i");
+	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::GetCargoType,        "GetCargoType",        2, ".i");
+	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::SourceIsTown,        "SourceIsTown",        2, ".i");
+	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::GetSource,           "GetSource",           2, ".i");
+	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::GetSourceType,       "GetSourceType",       2, ".i");
+	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::GetSourceIndex,      "GetSourceIndex",      2, ".i");
+	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::DestinationIsTown,   "DestinationIsTown",   2, ".i");
+	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::GetDestination,      "GetDestination",      2, ".i");
+	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::GetDestinationType,  "GetDestinationType",  2, ".i");
+	SQAISubsidy.DefSQStaticMethod(engine, &AISubsidy::GetDestinationIndex, "GetDestinationIndex", 2, ".i");
 
 	SQAISubsidy.PostRegister(engine);
 }