(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

This commit is contained in:
yexo 2010-08-07 20:51:53 +00:00
parent af9d8824bd
commit 178f74c31c
7 changed files with 46 additions and 12 deletions

View File

@ -310,3 +310,9 @@ function()
{ {
return !this.IsEnd(); 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;
}

View File

@ -61,3 +61,9 @@ function()
{ {
return !this.IsEnd(); 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;
}

View File

@ -27,6 +27,8 @@ public:
/** /**
* Creates a list of cargos that the given industry accepts. * 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 * @ingroup AIList
*/ */
class AICargoList_IndustryAccepting : public AIAbstractList { class AICargoList_IndustryAccepting : public AIAbstractList {

View File

@ -30,12 +30,13 @@
* \li HasNext for all lists. * \li HasNext for all lists.
* *
* Other changes: * Other changes:
* \li AIRoad::BuildRoadStation now allows overbuilding. * \li AIEngine::GetMaxTractiveEffort can be used for road vehicles.
* \li AIRoad::BuildDriveThroughRoadStation now allows overbuilding.
* \li AIEngine::GetPower 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::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 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 * \b 1.0.3
* *

View File

@ -15,6 +15,7 @@
#include "../../industry.h" #include "../../industry.h"
#include "../../strings_func.h" #include "../../strings_func.h"
#include "../../station_base.h" #include "../../station_base.h"
#include "../../newgrf_industries.h"
#include "table/strings.h" #include "table/strings.h"
/* static */ int32 AIIndustry::GetIndustryCount() /* static */ int32 AIIndustry::GetIndustryCount()
@ -45,18 +46,21 @@
return industry_name; 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 (!IsValidIndustry(industry_id)) return CAS_NOT_ACCEPTED;
if (!AICargo::IsValidCargo(cargo_id)) return false; 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++) { 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) /* static */ int32 AIIndustry::GetStockpiledCargo(IndustryID industry_id, CargoID cargo_id)

View File

@ -22,6 +22,13 @@ public:
/** Get the name of this class to identify it towards squirrel. */ /** Get the name of this class to identify it towards squirrel. */
static const char *GetClassName() { return "AIIndustry"; } 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. * Gets the number of industries.
* @return The number of industries. * @return The number of industries.
@ -56,14 +63,14 @@ public:
static char *GetName(IndustryID industry_id); 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 industry_id The index of the industry.
* @param cargo_id The index of the cargo. * @param cargo_id The index of the cargo.
* @pre IsValidIndustry(industry_id). * @pre IsValidIndustry(industry_id).
* @pre AICargo::IsValidCargo(cargo_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. * Get the amount of cargo stockpiled for processing.

View File

@ -12,6 +12,10 @@
#include "ai_industry.hpp" #include "ai_industry.hpp"
namespace SQConvert { 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 */ /* 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; }
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 @@ void SQAIIndustry_Register(Squirrel *engine)
SQAIIndustry.PreRegister(engine); SQAIIndustry.PreRegister(engine);
SQAIIndustry.AddConstructor<void (AIIndustry::*)(), 1>(engine, "x"); 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::GetIndustryCount, "GetIndustryCount", 1, ".");
SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::IsValidIndustry, "IsValidIndustry", 2, ".i"); SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::IsValidIndustry, "IsValidIndustry", 2, ".i");
SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetIndustryID, "GetIndustryID", 2, ".i"); SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetIndustryID, "GetIndustryID", 2, ".i");