(svn r23360) -Codechange: move AIInstance to ScriptInstance, making it reusable by other script API instances

This commit is contained in:
truebrain
2011-11-29 23:21:33 +00:00
parent 75c4bd280a
commit 3da8b5097a
22 changed files with 918 additions and 811 deletions

View File

@@ -39,10 +39,10 @@
* Helper function to connect a just built bridge to nearby roads.
* @param instance The AI we have to built the road for.
*/
static void _DoCommandReturnBuildBridge2(class AIInstance *instance)
static void _DoCommandReturnBuildBridge2(class ScriptInstance *instance)
{
if (!ScriptBridge::_BuildBridgeRoad2()) {
AIInstance::DoCommandReturn(instance);
ScriptInstance::DoCommandReturn(instance);
return;
}
@@ -55,10 +55,10 @@ static void _DoCommandReturnBuildBridge2(class AIInstance *instance)
* Helper function to connect a just built bridge to nearby roads.
* @param instance The AI we have to built the road for.
*/
static void _DoCommandReturnBuildBridge1(class AIInstance *instance)
static void _DoCommandReturnBuildBridge1(class ScriptInstance *instance)
{
if (!ScriptBridge::_BuildBridgeRoad1()) {
AIInstance::DoCommandReturn(instance);
ScriptInstance::DoCommandReturn(instance);
return;
}

View File

@@ -22,7 +22,7 @@
*/
class ScriptController {
friend class AIScanner;
friend class AIInstance;
friend class ScriptInstance;
public:
/**

View File

@@ -29,7 +29,7 @@
/* static */ ScriptGroup::GroupID ScriptGroup::CreateGroup(ScriptVehicle::VehicleType vehicle_type)
{
if (!ScriptObject::DoCommand(0, (::VehicleType)vehicle_type, 0, CMD_CREATE_GROUP, NULL, &AIInstance::DoCommandReturnGroupID)) return GROUP_INVALID;
if (!ScriptObject::DoCommand(0, (::VehicleType)vehicle_type, 0, CMD_CREATE_GROUP, NULL, &ScriptInstance::DoCommandReturnGroupID)) return GROUP_INVALID;
/* In case of test-mode, we return GroupID 0 */
return (ScriptGroup::GroupID)0;

View File

@@ -22,7 +22,7 @@
#include "script_error.hpp"
/**
* Get the storage associated with the current AIInstance.
* Get the storage associated with the current ScriptInstance.
* @return The storage.
*/
static ScriptStorage *GetStorage()
@@ -31,9 +31,9 @@ static ScriptStorage *GetStorage()
}
/* static */ AIInstance *ScriptObject::ActiveInstance::active = NULL;
/* static */ ScriptInstance *ScriptObject::ActiveInstance::active = NULL;
ScriptObject::ActiveInstance::ActiveInstance(AIInstance *instance)
ScriptObject::ActiveInstance::ActiveInstance(ScriptInstance *instance)
{
this->last_active = ScriptObject::ActiveInstance::active;
ScriptObject::ActiveInstance::active = instance;
@@ -44,7 +44,7 @@ ScriptObject::ActiveInstance::~ActiveInstance()
ScriptObject::ActiveInstance::active = this->last_active;
}
/* static */ AIInstance *ScriptObject::GetActiveInstance()
/* static */ ScriptInstance *ScriptObject::GetActiveInstance()
{
assert(ScriptObject::ActiveInstance::active != NULL);
return ScriptObject::ActiveInstance::active;
@@ -225,14 +225,14 @@ ScriptObject::ActiveInstance::~ActiveInstance()
return GetStorage()->callback_value[index];
}
/* static */ bool ScriptObject::DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint cmd, const char *text, AISuspendCallbackProc *callback)
/* static */ bool ScriptObject::DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint cmd, const char *text, Script_SuspendCallbackProc *callback)
{
if (!ScriptObject::CanSuspend()) {
throw Script_FatalError("You are not allowed to execute any DoCommand (even indirect) in your constructor, Save(), Load(), and any valuator.");
}
/* Set the default callback to return a true/false result of the DoCommand */
if (callback == NULL) callback = &AIInstance::DoCommandReturn;
if (callback == NULL) callback = &ScriptInstance::DoCommandReturn;
/* Are we only interested in the estimate costs? */
bool estimate_only = GetDoCommandMode() != NULL && !GetDoCommandMode()();

View File

@@ -17,11 +17,7 @@
#include "../../rail_type.h"
#include "script_types.hpp"
/**
* The callback function when an AI suspends.
*/
typedef void (AISuspendCallbackProc)(class AIInstance *instance);
#include "../script_suspend.hpp"
/**
* The callback function for Mode-classes.
@@ -30,12 +26,12 @@ typedef bool (ScriptModeProc)();
/**
* Uper-parent object of all API classes. You should never use this class in
* your AI, as it doesn't publish any public functions. It is used
* your script, as it doesn't publish any public functions. It is used
* internally to have a common place to handle general things, like internal
* command processing, and command-validation checks.
*/
class ScriptObject : public SimpleCountedObject {
friend class AIInstance;
friend class ScriptInstance;
#ifndef DOXYGEN_AI_DOCS
protected:
/**
@@ -47,12 +43,12 @@ protected:
class ActiveInstance {
friend class ScriptObject;
public:
ActiveInstance(AIInstance *instance);
ActiveInstance(ScriptInstance *instance);
~ActiveInstance();
private:
AIInstance *last_active; ///< The active instance before we go instantiated.
ScriptInstance *last_active; ///< The active instance before we go instantiated.
static AIInstance *active; ///< The global current active instance.
static ScriptInstance *active; ///< The global current active instance.
};
public:
@@ -66,13 +62,13 @@ public:
* Get the currently active instance.
* @return The instance.
*/
static class AIInstance *GetActiveInstance();
static class ScriptInstance *GetActiveInstance();
protected:
/**
* Executes a raw DoCommand for the AI.
*/
static bool DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint cmd, const char *text = NULL, AISuspendCallbackProc *callback = NULL);
static bool DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint cmd, const char *text = NULL, Script_SuspendCallbackProc *callback = NULL);
/**
* Sets the DoCommand costs counter to a value.

View File

@@ -546,10 +546,10 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr
* between the wanted and the current order.
* @param instance The AI we are doing the callback for.
*/
static void _DoCommandReturnSetOrderFlags(class AIInstance *instance)
static void _DoCommandReturnSetOrderFlags(class ScriptInstance *instance)
{
ScriptObject::SetLastCommandRes(ScriptOrder::_SetOrderFlags());
AIInstance::DoCommandReturn(instance);
ScriptInstance::DoCommandReturn(instance);
}
/* static */ bool ScriptOrder::_SetOrderFlags()

View File

@@ -69,7 +69,7 @@
EnforcePrecondition(INVALID_SIGN, !::StrEmpty(text));
EnforcePreconditionCustomError(false, ::Utf8StringLength(text) < MAX_LENGTH_SIGN_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
if (!ScriptObject::DoCommand(location, 0, 0, CMD_PLACE_SIGN, text, &AIInstance::DoCommandReturnSignID)) return INVALID_SIGN;
if (!ScriptObject::DoCommand(location, 0, 0, CMD_PLACE_SIGN, text, &ScriptInstance::DoCommandReturnSignID)) return INVALID_SIGN;
/* In case of test-mode, we return SignID 0 */
return 0;

View File

@@ -50,10 +50,10 @@
* Helper function to connect a just built tunnel to nearby roads.
* @param instance The AI we have to built the road for.
*/
static void _DoCommandReturnBuildTunnel2(class AIInstance *instance)
static void _DoCommandReturnBuildTunnel2(class ScriptInstance *instance)
{
if (!ScriptTunnel::_BuildTunnelRoad2()) {
AIInstance::DoCommandReturn(instance);
ScriptInstance::DoCommandReturn(instance);
return;
}
@@ -66,10 +66,10 @@ static void _DoCommandReturnBuildTunnel2(class AIInstance *instance)
* Helper function to connect a just built tunnel to nearby roads.
* @param instance The AI we have to built the road for.
*/
static void _DoCommandReturnBuildTunnel1(class AIInstance *instance)
static void _DoCommandReturnBuildTunnel1(class ScriptInstance *instance)
{
if (!ScriptTunnel::_BuildTunnelRoad1()) {
AIInstance::DoCommandReturn(instance);
ScriptInstance::DoCommandReturn(instance);
return;
}

View File

@@ -60,7 +60,7 @@
EnforcePreconditionCustomError(VEHICLE_INVALID, !ScriptGameSettings::IsDisabledVehicleType((ScriptVehicle::VehicleType)type), ScriptVehicle::ERR_VEHICLE_BUILD_DISABLED);
if (!ScriptObject::DoCommand(depot, engine_id, 0, ::GetCmdBuildVeh(type), NULL, &AIInstance::DoCommandReturnVehicleID)) return VEHICLE_INVALID;
if (!ScriptObject::DoCommand(depot, engine_id, 0, ::GetCmdBuildVeh(type), NULL, &ScriptInstance::DoCommandReturnVehicleID)) return VEHICLE_INVALID;
/* In case of test-mode, we return VehicleID 0 */
return 0;
@@ -70,7 +70,7 @@
{
EnforcePrecondition(false, IsValidVehicle(vehicle_id));
if (!ScriptObject::DoCommand(depot, vehicle_id, share_orders, CMD_CLONE_VEHICLE, NULL, &AIInstance::DoCommandReturnVehicleID)) return VEHICLE_INVALID;
if (!ScriptObject::DoCommand(depot, vehicle_id, share_orders, CMD_CLONE_VEHICLE, NULL, &ScriptInstance::DoCommandReturnVehicleID)) return VEHICLE_INVALID;
/* In case of test-mode, we return VehicleID 0 */
return 0;