summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryexo <yexo@openttd.org>2010-01-02 15:52:29 +0000
committeryexo <yexo@openttd.org>2010-01-02 15:52:29 +0000
commitfe67e85700988303c6e0148fa6212bf3904c8fca (patch)
tree84c8f9073611d231b11fa755b46c051ccc587ade
parentea75e7da60300bd0b1913a1a19ea554da21a592c (diff)
downloadopenttd-fe67e85700988303c6e0148fa6212bf3904c8fca.tar.xz
(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.
-rw-r--r--bin/ai/compat_0.7.nut159
-rw-r--r--src/ai/api/ai_changelog.hpp5
-rw-r--r--src/ai/api/ai_engine.cpp8
-rw-r--r--src/ai/api/ai_engine.hpp10
-rw-r--r--src/ai/api/ai_engine.hpp.sq1
-rw-r--r--src/ai/api/ai_group.cpp2
-rw-r--r--src/ai/api/ai_group.hpp2
-rw-r--r--src/ai/api/ai_vehicle.cpp2
-rw-r--r--src/ai/api/ai_vehicle.hpp2
9 files changed, 186 insertions, 5 deletions
diff --git a/bin/ai/compat_0.7.nut b/bin/ai/compat_0.7.nut
index 0cd8fe8d2..a90c79314 100644
--- a/bin/ai/compat_0.7.nut
+++ b/bin/ai/compat_0.7.nut
@@ -91,3 +91,162 @@ AIVehicle.SkipToVehicleOrder <- function(vehicle_id, order_position)
{
return AIOrder.SkipToOrder(vehicle_id, order_position);
}
+
+AIEngine.IsValidEngine <- function(engine_id)
+{
+ return AIEngine.IsBuildable(engine_id);
+}
+
+AIEngine._GetName <- AIEngine.GetName;
+AIEngine.GetName <- function(engine_id)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return null;
+ return AIEngine._GetName(engine_id);
+}
+
+AIEngine._GetCargoType <- AIEngine.GetCargoType;
+AIEngine.GetCargoType <- function(engine_id)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return 255;
+ return AIEngine._GetCargoType(engine_id);
+}
+
+AIEngine._CanRefitCargo <- AIEngine.CanRefitCargo;
+AIEngine.CanRefitCargo <- function(engine_id, cargo_id)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return false;
+ return AIEngine._CanRefitCargo(engine_id, cargo_id);
+}
+
+AIEngine._CanPullCargo <- AIEngine.CanPullCargo;
+AIEngine.CanPullCargo <- function(engine_id, cargo_id)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return false;
+ return AIEngine._CanPullCargo(engine_id, cargo_id);
+}
+
+AIEngine._GetCapacity <- AIEngine.GetCapacity;
+AIEngine.GetCapacity <- function(engine_id)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return -1;
+ return AIEngine._GetCapacity(engine_id);
+}
+
+AIEngine._GetReliability <- AIEngine.GetReliability;
+AIEngine.GetReliability <- function(engine_id)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return -1;
+ return AIEngine._GetReliability(engine_id);
+}
+
+AIEngine._GetMaxSpeed <- AIEngine.GetMaxSpeed;
+AIEngine.GetMaxSpeed <- function(engine_id)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return -1;
+ return AIEngine._GetMaxSpeed(engine_id);
+}
+
+AIEngine._GetPrice <- AIEngine.GetPrice;
+AIEngine.GetPrice <- function(engine_id)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return -1;
+ return AIEngine._GetPrice(engine_id);
+}
+
+AIEngine._GetMaxAge <- AIEngine.GetMaxAge;
+AIEngine.GetMaxAge <- function(engine_id)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return -1;
+ return AIEngine._GetMaxAge(engine_id);
+}
+
+AIEngine._GetRunningCost <- AIEngine.GetRunningCost;
+AIEngine.GetRunningCost <- function(engine_id)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return -1;
+ return AIEngine._GetRunningCost(engine_id);
+}
+
+AIEngine._GetPower <- AIEngine.GetPower;
+AIEngine.GetPower <- function(engine_id)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return -1;
+ return AIEngine._GetPower(engine_id);
+}
+
+AIEngine._GetWeight <- AIEngine.GetWeight;
+AIEngine.GetWeight <- function(engine_id)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return -1;
+ return AIEngine._GetWeight(engine_id);
+}
+
+AIEngine._GetMaxTractiveEffort <- AIEngine.GetMaxTractiveEffort;
+AIEngine.GetMaxTractiveEffort <- function(engine_id)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return -1;
+ return AIEngine._GetMaxTractiveEffort(engine_id);
+}
+
+AIEngine._GetDesignDate <- AIEngine.GetDesignDate;
+AIEngine.GetDesignDate <- function(engine_id)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return -1;
+ return AIEngine._GetDesignDate(engine_id);
+}
+
+AIEngine._GetVehicleType <- AIEngine.GetVehicleType;
+AIEngine.GetVehicleType <- function(engine_id)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return AIVehicle.VT_INVALID;
+ return AIEngine._GetVehicleType(engine_id);
+}
+
+AIEngine._IsWagon <- AIEngine.IsWagon;
+AIEngine.IsWagon <- function(engine_id)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return false;
+ return AIEngine._IsWagon(engine_id);
+}
+
+AIEngine._CanRunOnRail <- AIEngine.CanRunOnRail;
+AIEngine.CanRunOnRail <- function(engine_id, track_rail_type)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return false;
+ return AIEngine._CanRunOnRail(engine_id, track_rail_type);
+}
+
+AIEngine._HasPowerOnRail <- AIEngine.HasPowerOnRail;
+AIEngine.HasPowerOnRail <- function(engine_id, track_rail_type)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return false;
+ return AIEngine._HasPowerOnRail(engine_id, track_rail_type);
+}
+
+AIEngine._GetRoadType <- AIEngine.GetRoadType;
+AIEngine.GetRoadType <- function(engine_id)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return AIRoad.ROADTYPE_INVALID;
+ return AIEngine._GetRoadType(engine_id);
+}
+
+AIEngine._GetRailType <- AIEngine.GetRailType;
+AIEngine.GetRailType <- function(engine_id)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return AIRail.RAILTYPE_INVALID;
+ return AIEngine._GetRailType(engine_id);
+}
+
+AIEngine._IsArticulated <- AIEngine.IsArticulated;
+AIEngine.IsArticulated <- function(engine_id)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return false;
+ return AIEngine._IsArticulated(engine_id);
+}
+
+AIEngine._GetPlaneType <- AIEngine.GetPlaneType;
+AIEngine.GetPlaneType <- function(engine_id)
+{
+ if (!AIEngine.IsBuildable(engine_id)) return -1;
+ return AIEngine._GetPlaneType(engine_id);
+}
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"
@@ -22,6 +23,13 @@
/* 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,13 +26,21 @@ 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.
* @pre IsValidEngine(engine_id).
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<void (AIEngine::*)(), 1>(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