From fe67e85700988303c6e0148fa6212bf3904c8fca Mon Sep 17 00:00:00 2001 From: yexo Date: Sat, 2 Jan 2010 15:52:29 +0000 Subject: (svn r18687) -Add [NoAI]: AIEngine::IsBuildable to check if you can build a certain engine. -Change: AIEngine::IsValidEngine will now also return true when you have at least one vehicle of that type even if you can't build it anymore. --- src/ai/api/ai_changelog.hpp | 5 +++++ src/ai/api/ai_engine.cpp | 8 ++++++++ src/ai/api/ai_engine.hpp | 10 +++++++++- src/ai/api/ai_engine.hpp.sq | 1 + src/ai/api/ai_group.cpp | 2 +- src/ai/api/ai_group.hpp | 2 +- src/ai/api/ai_vehicle.cpp | 2 +- src/ai/api/ai_vehicle.hpp | 2 +- 8 files changed, 27 insertions(+), 5 deletions(-) (limited to 'src/ai/api') diff --git a/src/ai/api/ai_changelog.hpp b/src/ai/api/ai_changelog.hpp index 0ff70f9b1..af82a18e5 100644 --- a/src/ai/api/ai_changelog.hpp +++ b/src/ai/api/ai_changelog.hpp @@ -21,6 +21,7 @@ * API additions: * \li AIBaseStation * \li AIBuoyList + * \li AIEngine::IsBuildable * \li AIEventCompanyAskMerger * \li AIIndustry::GetLastMonthTransportedPercentage * \li AIOrder::AIOF_GOTO_NEAREST_DEPOT @@ -81,6 +82,10 @@ * For GetCargoType the first most used cargo type is returned. * \li AIIndustryType::GetConstructionCost() now returns -1 if the industry is * neither buildable nor prospectable. + * \li AIEngine::IsValidEngine will now return true if you have at least one + * vehicle of that type in your company, regardless if it's still buildable + * or not. AIEngine::IsBuildable returns only true when you can actually + * build an engine. * * \b 0.7.5 * diff --git a/src/ai/api/ai_engine.cpp b/src/ai/api/ai_engine.cpp index 6416c95ff..157ee825d 100644 --- a/src/ai/api/ai_engine.cpp +++ b/src/ai/api/ai_engine.cpp @@ -12,6 +12,7 @@ #include "ai_engine.hpp" #include "ai_cargo.hpp" #include "../../company_func.h" +#include "../../company_base.h" #include "../../strings_func.h" #include "../../settings_type.h" #include "../../rail.h" @@ -20,6 +21,13 @@ #include "table/strings.h" /* static */ bool AIEngine::IsValidEngine(EngineID engine_id) +{ + const Engine *e = ::Engine::GetIfValid(engine_id); + return e != NULL && (HasBit(e->company_avail, _current_company) || ::Company::Get(_current_company)->num_engines[engine_id] > 0); + +} + +/* static */ bool AIEngine::IsBuildable(EngineID engine_id) { const Engine *e = ::Engine::GetIfValid(engine_id); return e != NULL && HasBit(e->company_avail, _current_company); diff --git a/src/ai/api/ai_engine.hpp b/src/ai/api/ai_engine.hpp index 3d213fd3e..431f622c8 100644 --- a/src/ai/api/ai_engine.hpp +++ b/src/ai/api/ai_engine.hpp @@ -26,12 +26,20 @@ public: static const char *GetClassName() { return "AIEngine"; } /** - * Checks whether the given engine type is valid and buildable by you. + * Checks whether the given engine type is valid. An engine is valid if you + * have at least one vehicle of this engine or it's currently buildable. * @param engine_id The engine to check. * @return True if and only if the engine type is valid. */ static bool IsValidEngine(EngineID engine_id); + /** + * Checks whether the given engine type is buildable by you. + * @param engine_id The engine to check. + * @return True if and only if the engine type is buildable. + */ + static bool IsBuildable(EngineID engine_id); + /** * Get the name of an engine. * @param engine_id The engine to get the name of. diff --git a/src/ai/api/ai_engine.hpp.sq b/src/ai/api/ai_engine.hpp.sq index 78f9a1c06..594f7bd5e 100644 --- a/src/ai/api/ai_engine.hpp.sq +++ b/src/ai/api/ai_engine.hpp.sq @@ -27,6 +27,7 @@ void SQAIEngine_Register(Squirrel *engine) SQAIEngine.AddConstructor(engine, "x"); SQAIEngine.DefSQStaticMethod(engine, &AIEngine::IsValidEngine, "IsValidEngine", 2, ".i"); + SQAIEngine.DefSQStaticMethod(engine, &AIEngine::IsBuildable, "IsBuildable", 2, ".i"); SQAIEngine.DefSQStaticMethod(engine, &AIEngine::GetName, "GetName", 2, ".i"); SQAIEngine.DefSQStaticMethod(engine, &AIEngine::GetCargoType, "GetCargoType", 2, ".i"); SQAIEngine.DefSQStaticMethod(engine, &AIEngine::CanRefitCargo, "CanRefitCargo", 3, ".ii"); diff --git a/src/ai/api/ai_group.cpp b/src/ai/api/ai_group.cpp index c9d3795cb..0aad40cf5 100644 --- a/src/ai/api/ai_group.cpp +++ b/src/ai/api/ai_group.cpp @@ -114,7 +114,7 @@ /* static */ bool AIGroup::SetAutoReplace(GroupID group_id, EngineID engine_id_old, EngineID engine_id_new) { EnforcePrecondition(false, IsValidGroup(group_id) || group_id == GROUP_ALL); - EnforcePrecondition(false, AIEngine::IsValidEngine(engine_id_new)); + EnforcePrecondition(false, AIEngine::IsBuildable(engine_id_new)); return AIObject::DoCommand(0, group_id << 16, (engine_id_new << 16) | engine_id_old, CMD_SET_AUTOREPLACE); } diff --git a/src/ai/api/ai_group.hpp b/src/ai/api/ai_group.hpp index a26c5fb0d..422f2962a 100644 --- a/src/ai/api/ai_group.hpp +++ b/src/ai/api/ai_group.hpp @@ -149,7 +149,7 @@ public: * @param engine_id_old The engine id to start replacing. * @param engine_id_new The engine id to replace with. * @pre IsValidGroup(group_id) || group_id == GROUP_ALL. - * @pre AIEngine.IsValidEngine(engine_id_new). + * @pre AIEngine.IsBuildable(engine_id_new). * @note To stop autoreplacing engine_id_old, call StopAutoReplace(group_id, engine_id_old). */ static bool SetAutoReplace(GroupID group_id, EngineID engine_id_old, EngineID engine_id_new); diff --git a/src/ai/api/ai_vehicle.cpp b/src/ai/api/ai_vehicle.cpp index c05f67909..03f39bae6 100644 --- a/src/ai/api/ai_vehicle.cpp +++ b/src/ai/api/ai_vehicle.cpp @@ -64,7 +64,7 @@ /* static */ VehicleID AIVehicle::BuildVehicle(TileIndex depot, EngineID engine_id) { - EnforcePrecondition(INVALID_VEHICLE, AIEngine::IsValidEngine(engine_id)); + EnforcePrecondition(INVALID_VEHICLE, AIEngine::IsBuildable(engine_id)); ::VehicleType type = ::Engine::Get(engine_id)->type; diff --git a/src/ai/api/ai_vehicle.hpp b/src/ai/api/ai_vehicle.hpp index 4041287a7..049ee8ade 100644 --- a/src/ai/api/ai_vehicle.hpp +++ b/src/ai/api/ai_vehicle.hpp @@ -300,7 +300,7 @@ public: * @param engine_id The engine to use for this vehicle. * @pre The tile at depot has a depot that can build the engine and * is owned by you. - * @pre IsValidEngine(engine_id). + * @pre AIEngine::IsBuildable(engine_id). * @exception AIVehicle::ERR_VEHICLE_TOO_MANY * @exception AIVehicle::ERR_VEHICLE_BUILD_DISABLED * @exception AIVehicle::ERR_VEHICLE_WRONG_DEPOT -- cgit v1.2.3-54-g00ecf