From b43fd7266e2cc66028a7d15a1cee75bf89f6988e Mon Sep 17 00:00:00 2001 From: smatz Date: Sat, 8 Aug 2009 16:53:22 +0000 Subject: (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 --- bin/ai/regression/regression.nut | 18 +++++++-------- bin/ai/regression/regression.txt | 18 +++++++-------- src/ai/api/ai_changelog.hpp | 11 ++++++++- src/ai/api/ai_subsidy.cpp | 32 +++++++++++++++++++++++++ src/ai/api/ai_subsidy.hpp | 50 ++++++++++++++++++++++++++++++++++++++++ src/ai/api/ai_subsidy.hpp.sq | 30 ++++++++++++++++-------- 6 files changed, 131 insertions(+), 28 deletions(-) diff --git a/bin/ai/regression/regression.nut b/bin/ai/regression/regression.nut index 8d0abe3b3..5e1ab100a 100644 --- a/bin/ai/regression/regression.nut +++ b/bin/ai/regression/regression.nut @@ -1636,15 +1636,15 @@ function Regression::Vehicle() 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)); } diff --git a/bin/ai/regression/regression.txt b/bin/ai/regression/regression.txt index 1ac7b1242..31bc3a417 100644 --- 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 diff --git a/src/ai/api/ai_changelog.hpp b/src/ai/api/ai_changelog.hpp index a8e511030..42a67ad17 100644 --- 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 diff --git a/src/ai/api/ai_subsidy.cpp b/src/ai/api/ai_subsidy.cpp index 39358e963..df6889ac7 100644 --- 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; +} diff --git a/src/ai/api/ai_subsidy.hpp b/src/ai/api/ai_subsidy.hpp index 96ad92c31..b4d190bbf 100644 --- a/src/ai/api/ai_subsidy.hpp +++ b/src/ai/api/ai_subsidy.hpp @@ -15,6 +15,16 @@ class AISubsidy : public AIObject { public: 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. @@ -64,6 +74,7 @@ public: * @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 @@ public: * @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 @@ public: * @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 */ diff --git a/src/ai/api/ai_subsidy.hpp.sq b/src/ai/api/ai_subsidy.hpp.sq index a87f9101b..8221e2338 100644 --- 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, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (AISubsidy::SubsidyParticipantType)tmp; } + template <> int Return(HSQUIRRELVM vm, AISubsidy::SubsidyParticipantType res) { sq_pushinteger(vm, (int32)res); return 1; } + /* Allow AISubsidy to be used as Squirrel parameter */ template <> AISubsidy *GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AISubsidy *)instance; } template <> AISubsidy &GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AISubsidy *)instance; } @@ -17,15 +21,23 @@ void SQAISubsidy_Register(Squirrel *engine) { SQAISubsidy.PreRegister(engine); SQAISubsidy.AddConstructor(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); } -- cgit v1.2.3-70-g09d2