From 36497b584e64a6230519f0bda91ed18441cdddc3 Mon Sep 17 00:00:00 2001 From: smatz Date: Sat, 29 Aug 2009 11:18:03 +0000 Subject: [PATCH] (svn r17305) [0.7] -Backport from trunk: -Fix: [NoAI] AITown::GetLastMonthTransported didn't work as documented at all, make it return what AITown::GetLastMonthProduction did -Change: [NoAI] AITown::GetLastMonthProduction now returns the same value as AITown::GetMaxProduction -Add: [NoAI] AITown::GetLastMonthTransportedPercentage and AIIndustry::GetLastMonthTransportedPercentage --- src/ai/api/ai_industry.cpp | 14 ++++++++++++ src/ai/api/ai_industry.hpp | 10 ++++++++ src/ai/api/ai_industry.hpp.sq | 35 ++++++++++++++-------------- src/ai/api/ai_town.cpp | 19 ++++++++++------ src/ai/api/ai_town.hpp | 12 ++++++++++ src/ai/api/ai_town.hpp.sq | 43 ++++++++++++++++++----------------- 6 files changed, 88 insertions(+), 45 deletions(-) diff --git a/src/ai/api/ai_industry.cpp b/src/ai/api/ai_industry.cpp index 7c07535021..f0f769ce08 100644 --- a/src/ai/api/ai_industry.cpp +++ b/src/ai/api/ai_industry.cpp @@ -91,6 +91,20 @@ return -1; } +/* static */ int32 AIIndustry::GetLastMonthTransportedPercentage(IndustryID industry_id, CargoID cargo_id) +{ + if (!IsValidIndustry(industry_id)) return -1; + if (!AICargo::IsValidCargo(cargo_id)) return -1; + + const Industry *i = ::GetIndustry(industry_id); + + for (byte j = 0; j < lengthof(i->produced_cargo); j++) { + if (i->produced_cargo[j] == cargo_id) return i->last_month_pct_transported[j] * 100 >> 8; + } + + return -1; +} + /* static */ TileIndex AIIndustry::GetLocation(IndustryID industry_id) { if (!IsValidIndustry(industry_id)) return INVALID_TILE; diff --git a/src/ai/api/ai_industry.hpp b/src/ai/api/ai_industry.hpp index da27ec9207..6c4901c6f2 100644 --- a/src/ai/api/ai_industry.hpp +++ b/src/ai/api/ai_industry.hpp @@ -77,6 +77,16 @@ public: */ static int32 GetLastMonthTransported(IndustryID industry_id, CargoID cargo_id); + /** + * Get the percentage of cargo transported from an industry last month. + * @param industry_id The index of the industry. + * @param cargo_id The index of the cargo. + * @pre IsValidIndustry(industry_id). + * @pre AICargo::IsValidCargo(cargo_id). + * @return The percentage of given cargo transported from this industry last month. + */ + static int32 GetLastMonthTransportedPercentage(IndustryID industry_id, CargoID cargo_id); + /** * Gets the location of the industry. * @param industry_id The index of the industry. diff --git a/src/ai/api/ai_industry.hpp.sq b/src/ai/api/ai_industry.hpp.sq index 2e93f177e5..0e856ed91d 100644 --- a/src/ai/api/ai_industry.hpp.sq +++ b/src/ai/api/ai_industry.hpp.sq @@ -17,23 +17,24 @@ void SQAIIndustry_Register(Squirrel *engine) { SQAIIndustry.PreRegister(engine); SQAIIndustry.AddConstructor(engine, "x"); - SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetIndustryCount, "GetIndustryCount", 1, "."); - SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::IsValidIndustry, "IsValidIndustry", 2, ".i"); - SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetName, "GetName", 2, ".i"); - SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::IsCargoAccepted, "IsCargoAccepted", 3, ".ii"); - SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetStockpiledCargo, "GetStockpiledCargo", 3, ".ii"); - SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetLastMonthProduction, "GetLastMonthProduction", 3, ".ii"); - SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetLastMonthTransported, "GetLastMonthTransported", 3, ".ii"); - SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetLocation, "GetLocation", 2, ".i"); - SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetAmountOfStationsAround, "GetAmountOfStationsAround", 2, ".i"); - SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetDistanceManhattanToTile, "GetDistanceManhattanToTile", 3, ".ii"); - SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetDistanceSquareToTile, "GetDistanceSquareToTile", 3, ".ii"); - SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::IsBuiltOnWater, "IsBuiltOnWater", 2, ".i"); - SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::HasHeliport, "HasHeliport", 2, ".i"); - SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetHeliportLocation, "GetHeliportLocation", 2, ".i"); - SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::HasDock, "HasDock", 2, ".i"); - SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetDockLocation, "GetDockLocation", 2, ".i"); - SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetIndustryType, "GetIndustryType", 2, ".i"); + SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetIndustryCount, "GetIndustryCount", 1, "."); + SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::IsValidIndustry, "IsValidIndustry", 2, ".i"); + SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetName, "GetName", 2, ".i"); + SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::IsCargoAccepted, "IsCargoAccepted", 3, ".ii"); + SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetStockpiledCargo, "GetStockpiledCargo", 3, ".ii"); + SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetLastMonthProduction, "GetLastMonthProduction", 3, ".ii"); + SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetLastMonthTransported, "GetLastMonthTransported", 3, ".ii"); + SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetLastMonthTransportedPercentage, "GetLastMonthTransportedPercentage", 3, ".ii"); + SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetLocation, "GetLocation", 2, ".i"); + SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetAmountOfStationsAround, "GetAmountOfStationsAround", 2, ".i"); + SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetDistanceManhattanToTile, "GetDistanceManhattanToTile", 3, ".ii"); + SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetDistanceSquareToTile, "GetDistanceSquareToTile", 3, ".ii"); + SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::IsBuiltOnWater, "IsBuiltOnWater", 2, ".i"); + SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::HasHeliport, "HasHeliport", 2, ".i"); + SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetHeliportLocation, "GetHeliportLocation", 2, ".i"); + SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::HasDock, "HasDock", 2, ".i"); + SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetDockLocation, "GetDockLocation", 2, ".i"); + SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetIndustryType, "GetIndustryType", 2, ".i"); SQAIIndustry.PostRegister(engine); } diff --git a/src/ai/api/ai_town.cpp b/src/ai/api/ai_town.cpp index f7584c9791..5f899859f8 100644 --- a/src/ai/api/ai_town.cpp +++ b/src/ai/api/ai_town.cpp @@ -65,8 +65,8 @@ const Town *t = ::GetTown(town_id); switch(AICargo::GetTownEffect(cargo_id)) { - case AICargo::TE_PASSENGERS: return t->act_pass; - case AICargo::TE_MAIL: return t->act_mail; + case AICargo::TE_PASSENGERS: return t->max_pass; + case AICargo::TE_MAIL: return t->max_mail; default: return -1; } } @@ -79,22 +79,27 @@ const Town *t = ::GetTown(town_id); switch(AICargo::GetTownEffect(cargo_id)) { - case AICargo::TE_PASSENGERS: return t->pct_pass_transported; - case AICargo::TE_MAIL: return t->pct_mail_transported; + case AICargo::TE_PASSENGERS: return t->act_pass; + case AICargo::TE_MAIL: return t->act_mail; default: return -1; } } /* static */ int32 AITown::GetMaxProduction(TownID town_id, CargoID cargo_id) +{ + return AITown::GetLastMonthProduction(town_id, cargo_id); +} + +/* static */ int32 AITown::GetLastMonthTransportedPercentage(TownID town_id, CargoID cargo_id) { if (!IsValidTown(town_id)) return -1; if (!AICargo::IsValidCargo(cargo_id)) return -1; const Town *t = ::GetTown(town_id); - switch(AICargo::GetTownEffect(cargo_id)) { - case AICargo::TE_PASSENGERS: return t->max_pass; - case AICargo::TE_MAIL: return t->max_mail; + switch (AICargo::GetTownEffect(cargo_id)) { + case AICargo::TE_PASSENGERS: return t->pct_pass_transported * 100 >> 8; + case AICargo::TE_MAIL: return t->pct_mail_transported * 100 >> 8; default: return -1; } } diff --git a/src/ai/api/ai_town.hpp b/src/ai/api/ai_town.hpp index c6476145bc..4194ce47b3 100644 --- a/src/ai/api/ai_town.hpp +++ b/src/ai/api/ai_town.hpp @@ -180,6 +180,18 @@ public: */ static int32 GetMaxProduction(TownID town_id, CargoID cargo_id); + /** + * Get the percentage of transported production of the given cargo at a town. + * @param town_id The index of the town. + * @param cargo_id The index of the cargo. + * @pre IsValidTown(town_id). + * @pre AICargo::IsValidCargo(cargo_id). + * @pre AICargo::GetTownEffect(cargo_id) == TE_PASSENGERS || AICargo::GetTownEffect(cargo_id) == TE_MAIL. + * @return The percentage of given cargo transported from this town last month. + * @post Return value is always non-negative. + */ + static int32 GetLastMonthTransportedPercentage(TownID town_id, CargoID cargo_id); + /** * Get the manhattan distance from the tile to the AITown::GetLocation() * of the town. diff --git a/src/ai/api/ai_town.hpp.sq b/src/ai/api/ai_town.hpp.sq index a1babb000d..5db065b5b4 100644 --- a/src/ai/api/ai_town.hpp.sq +++ b/src/ai/api/ai_town.hpp.sq @@ -49,27 +49,28 @@ void SQAITown_Register(Squirrel *engine) { SQAITown.DefSQConst(engine, AITown::ROAD_LAYOUT_3x3, "ROAD_LAYOUT_3x3"); SQAITown.DefSQConst(engine, AITown::ROAD_LAYOUT_INVALID, "ROAD_LAYOUT_INVALID"); - SQAITown.DefSQStaticMethod(engine, &AITown::GetTownCount, "GetTownCount", 1, "."); - SQAITown.DefSQStaticMethod(engine, &AITown::IsValidTown, "IsValidTown", 2, ".i"); - SQAITown.DefSQStaticMethod(engine, &AITown::GetName, "GetName", 2, ".i"); - SQAITown.DefSQStaticMethod(engine, &AITown::GetPopulation, "GetPopulation", 2, ".i"); - SQAITown.DefSQStaticMethod(engine, &AITown::GetHouseCount, "GetHouseCount", 2, ".i"); - SQAITown.DefSQStaticMethod(engine, &AITown::GetLocation, "GetLocation", 2, ".i"); - SQAITown.DefSQStaticMethod(engine, &AITown::GetLastMonthProduction, "GetLastMonthProduction", 3, ".ii"); - SQAITown.DefSQStaticMethod(engine, &AITown::GetLastMonthTransported, "GetLastMonthTransported", 3, ".ii"); - SQAITown.DefSQStaticMethod(engine, &AITown::GetMaxProduction, "GetMaxProduction", 3, ".ii"); - SQAITown.DefSQStaticMethod(engine, &AITown::GetDistanceManhattanToTile, "GetDistanceManhattanToTile", 3, ".ii"); - SQAITown.DefSQStaticMethod(engine, &AITown::GetDistanceSquareToTile, "GetDistanceSquareToTile", 3, ".ii"); - SQAITown.DefSQStaticMethod(engine, &AITown::IsWithinTownInfluence, "IsWithinTownInfluence", 3, ".ii"); - SQAITown.DefSQStaticMethod(engine, &AITown::HasStatue, "HasStatue", 2, ".i"); - SQAITown.DefSQStaticMethod(engine, &AITown::GetRoadReworkDuration, "GetRoadReworkDuration", 2, ".i"); - SQAITown.DefSQStaticMethod(engine, &AITown::GetExclusiveRightsCompany, "GetExclusiveRightsCompany", 2, ".i"); - SQAITown.DefSQStaticMethod(engine, &AITown::GetExclusiveRightsDuration, "GetExclusiveRightsDuration", 2, ".i"); - SQAITown.DefSQStaticMethod(engine, &AITown::IsActionAvailable, "IsActionAvailable", 3, ".ii"); - SQAITown.DefSQStaticMethod(engine, &AITown::PerformTownAction, "PerformTownAction", 3, ".ii"); - SQAITown.DefSQStaticMethod(engine, &AITown::GetRating, "GetRating", 3, ".ii"); - SQAITown.DefSQStaticMethod(engine, &AITown::GetAllowedNoise, "GetAllowedNoise", 2, ".i"); - SQAITown.DefSQStaticMethod(engine, &AITown::GetRoadLayout, "GetRoadLayout", 2, ".i"); + SQAITown.DefSQStaticMethod(engine, &AITown::GetTownCount, "GetTownCount", 1, "."); + SQAITown.DefSQStaticMethod(engine, &AITown::IsValidTown, "IsValidTown", 2, ".i"); + SQAITown.DefSQStaticMethod(engine, &AITown::GetName, "GetName", 2, ".i"); + SQAITown.DefSQStaticMethod(engine, &AITown::GetPopulation, "GetPopulation", 2, ".i"); + SQAITown.DefSQStaticMethod(engine, &AITown::GetHouseCount, "GetHouseCount", 2, ".i"); + SQAITown.DefSQStaticMethod(engine, &AITown::GetLocation, "GetLocation", 2, ".i"); + SQAITown.DefSQStaticMethod(engine, &AITown::GetLastMonthProduction, "GetLastMonthProduction", 3, ".ii"); + SQAITown.DefSQStaticMethod(engine, &AITown::GetLastMonthTransported, "GetLastMonthTransported", 3, ".ii"); + SQAITown.DefSQStaticMethod(engine, &AITown::GetMaxProduction, "GetMaxProduction", 3, ".ii"); + SQAITown.DefSQStaticMethod(engine, &AITown::GetLastMonthTransportedPercentage, "GetLastMonthTransportedPercentage", 3, ".ii"); + SQAITown.DefSQStaticMethod(engine, &AITown::GetDistanceManhattanToTile, "GetDistanceManhattanToTile", 3, ".ii"); + SQAITown.DefSQStaticMethod(engine, &AITown::GetDistanceSquareToTile, "GetDistanceSquareToTile", 3, ".ii"); + SQAITown.DefSQStaticMethod(engine, &AITown::IsWithinTownInfluence, "IsWithinTownInfluence", 3, ".ii"); + SQAITown.DefSQStaticMethod(engine, &AITown::HasStatue, "HasStatue", 2, ".i"); + SQAITown.DefSQStaticMethod(engine, &AITown::GetRoadReworkDuration, "GetRoadReworkDuration", 2, ".i"); + SQAITown.DefSQStaticMethod(engine, &AITown::GetExclusiveRightsCompany, "GetExclusiveRightsCompany", 2, ".i"); + SQAITown.DefSQStaticMethod(engine, &AITown::GetExclusiveRightsDuration, "GetExclusiveRightsDuration", 2, ".i"); + SQAITown.DefSQStaticMethod(engine, &AITown::IsActionAvailable, "IsActionAvailable", 3, ".ii"); + SQAITown.DefSQStaticMethod(engine, &AITown::PerformTownAction, "PerformTownAction", 3, ".ii"); + SQAITown.DefSQStaticMethod(engine, &AITown::GetRating, "GetRating", 3, ".ii"); + SQAITown.DefSQStaticMethod(engine, &AITown::GetAllowedNoise, "GetAllowedNoise", 2, ".i"); + SQAITown.DefSQStaticMethod(engine, &AITown::GetRoadLayout, "GetRoadLayout", 2, ".i"); SQAITown.PostRegister(engine); }