changeset 15732:574ae0427343 draft

(svn r20399) -Change: [NoAI] AIIndustry::IsCargoAccepted now returns 3 possible values so AIs can detect a temporaral refusal from an industry to accept some cargo type
author yexo <yexo@openttd.org>
date Sat, 07 Aug 2010 20:51:53 +0000
parents c5f579b016bc
children fc40021c45d3
files bin/ai/compat_0.7.nut bin/ai/compat_1.0.nut src/ai/api/ai_cargolist.hpp src/ai/api/ai_changelog.hpp src/ai/api/ai_industry.cpp src/ai/api/ai_industry.hpp src/ai/api/ai_industry.hpp.sq
diffstat 7 files changed, 46 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/bin/ai/compat_0.7.nut
+++ b/bin/ai/compat_0.7.nut
@@ -310,3 +310,9 @@
 {
 	return !this.IsEnd();
 }
+
+AIIndustry._IsCargoAccepted <- AIIndustry.IsCargoAccepted;
+AIIndustry.IsCargoAccepted <- function(industry_id, cargo_id)
+{
+	return AIIndustry._IsCargoAccepted(industry_id, cargo_id) != AIIndustry.CAS_NOT_ACCEPTED;
+}
--- a/bin/ai/compat_1.0.nut
+++ b/bin/ai/compat_1.0.nut
@@ -61,3 +61,9 @@
 {
 	return !this.IsEnd();
 }
+
+AIIndustry._IsCargoAccepted <- AIIndustry.IsCargoAccepted;
+AIIndustry.IsCargoAccepted <- function(industry_id, cargo_id)
+{
+	return AIIndustry._IsCargoAccepted(industry_id, cargo_id) != AIIndustry.CAS_NOT_ACCEPTED;
+}
--- a/src/ai/api/ai_cargolist.hpp
+++ b/src/ai/api/ai_cargolist.hpp
@@ -27,6 +27,8 @@
 
 /**
  * Creates a list of cargos that the given industry accepts.
+ * @note This list also includes cargos that are temporarily not accepted
+ *   by this industry, @see AIIndustry::IsCargoAccepted.
  * @ingroup AIList
  */
 class AICargoList_IndustryAccepting : public AIAbstractList {
--- a/src/ai/api/ai_changelog.hpp
+++ b/src/ai/api/ai_changelog.hpp
@@ -30,12 +30,13 @@
  * \li HasNext for all lists.
  *
  * Other changes:
- * \li AIRoad::BuildRoadStation now allows overbuilding.
- * \li AIRoad::BuildDriveThroughRoadStation now allows overbuilding.
+ * \li AIEngine::GetMaxTractiveEffort can be used for road vehicles.
  * \li AIEngine::GetPower can be used for road vehicles.
  * \li AIEngine::GetWeight can be used for road vehicles.
- * \li AIEngine::GetMaxTractiveEffort can be used for road vehicles.
+ * \li AIIndustry::IsCargoAccepted now returns CargoAcceptState instead of a boolean.
  * \li AIOrder::GetOrderFlags returns AIOrder::AIOF_INVALID for void orders as well.
+ * \li AIRoad::BuildDriveThroughRoadStation now allows overbuilding.
+ * \li AIRoad::BuildRoadStation now allows overbuilding.
  *
  * \b 1.0.3
  *
--- a/src/ai/api/ai_industry.cpp
+++ b/src/ai/api/ai_industry.cpp
@@ -15,6 +15,7 @@
 #include "../../industry.h"
 #include "../../strings_func.h"
 #include "../../station_base.h"
+#include "../../newgrf_industries.h"
 #include "table/strings.h"
 
 /* static */ int32 AIIndustry::GetIndustryCount()
@@ -45,18 +46,21 @@
 	return industry_name;
 }
 
-/* static */ bool AIIndustry::IsCargoAccepted(IndustryID industry_id, CargoID cargo_id)
+/* static */ AIIndustry::CargoAcceptState AIIndustry::IsCargoAccepted(IndustryID industry_id, CargoID cargo_id)
 {
-	if (!IsValidIndustry(industry_id)) return false;
-	if (!AICargo::IsValidCargo(cargo_id)) return false;
+	if (!IsValidIndustry(industry_id)) return CAS_NOT_ACCEPTED;
+	if (!AICargo::IsValidCargo(cargo_id)) return CAS_NOT_ACCEPTED;
 
-	const Industry *i = ::Industry::Get(industry_id);
+	Industry *i = ::Industry::Get(industry_id);
 
 	for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
-		if (i->accepts_cargo[j] == cargo_id) return true;
+		if (i->accepts_cargo[j] == cargo_id) {
+			if (IndustryTemporarilyRefusesCargo(i, cargo_id)) return CAS_TEMP_REFUSED;
+			return CAS_ACCEPTED;
+		}
 	}
 
-	return false;
+	return CAS_NOT_ACCEPTED;
 }
 
 /* static */ int32 AIIndustry::GetStockpiledCargo(IndustryID industry_id, CargoID cargo_id)
--- a/src/ai/api/ai_industry.hpp
+++ b/src/ai/api/ai_industry.hpp
@@ -22,6 +22,13 @@
 	/** Get the name of this class to identify it towards squirrel. */
 	static const char *GetClassName() { return "AIIndustry"; }
 
+	/** Ways for an industry to accept a cargo. */
+	enum CargoAcceptState {
+		CAS_NOT_ACCEPTED, ///< The CargoID is not accepted by this industry.
+		CAS_ACCEPTED,     ///< The industry currently accepts this CargoID.
+		CAS_TEMP_REFUSED, ///< The industry temporarily refuses to accept this CargoID but may do so again in the future.
+	};
+
 	/**
 	 * Gets the number of industries.
 	 * @return The number of industries.
@@ -56,14 +63,14 @@
 	static char *GetName(IndustryID industry_id);
 
 	/**
-	 * See if an industry accepts a certain cargo.
+	 * See whether an industry currently accepts a certain cargo.
 	 * @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 True if and only if the industry accepts the cargo.
+	 * @return Whether the industry accepts, temporarily refuses or never accepts this cargo.
 	 */
-	static bool IsCargoAccepted(IndustryID industry_id, CargoID cargo_id);
+	static CargoAcceptState IsCargoAccepted(IndustryID industry_id, CargoID cargo_id);
 
 	/**
 	 * Get the amount of cargo stockpiled for processing.
--- a/src/ai/api/ai_industry.hpp.sq
+++ b/src/ai/api/ai_industry.hpp.sq
@@ -12,6 +12,10 @@
 #include "ai_industry.hpp"
 
 namespace SQConvert {
+	/* Allow enums to be used as Squirrel parameters */
+	template <> AIIndustry::CargoAcceptState GetParam(ForceType<AIIndustry::CargoAcceptState>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (AIIndustry::CargoAcceptState)tmp; }
+	template <> int Return<AIIndustry::CargoAcceptState>(HSQUIRRELVM vm, AIIndustry::CargoAcceptState res) { sq_pushinteger(vm, (int32)res); return 1; }
+
 	/* Allow AIIndustry to be used as Squirrel parameter */
 	template <> AIIndustry *GetParam(ForceType<AIIndustry *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIIndustry *)instance; }
 	template <> AIIndustry &GetParam(ForceType<AIIndustry &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIIndustry *)instance; }
@@ -26,6 +30,10 @@
 	SQAIIndustry.PreRegister(engine);
 	SQAIIndustry.AddConstructor<void (AIIndustry::*)(), 1>(engine, "x");
 
+	SQAIIndustry.DefSQConst(engine, AIIndustry::CAS_NOT_ACCEPTED, "CAS_NOT_ACCEPTED");
+	SQAIIndustry.DefSQConst(engine, AIIndustry::CAS_ACCEPTED,     "CAS_ACCEPTED");
+	SQAIIndustry.DefSQConst(engine, AIIndustry::CAS_TEMP_REFUSED, "CAS_TEMP_REFUSED");
+
 	SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetIndustryCount,                  "GetIndustryCount",                  1, ".");
 	SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::IsValidIndustry,                   "IsValidIndustry",                   2, ".i");
 	SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetIndustryID,                     "GetIndustryID",                     2, ".i");