Changeset - r18505:6589b19bd149
[Not reviewed]
master
0 9 1
truebrain - 13 years ago 2011-11-29 23:21:24
truebrain@openttd.org
(svn r23359) -Codechange: move AI_VMSuspend to Script_Suspend (and to its own file)
10 files changed with 85 insertions and 53 deletions:
0 comments (0 inline, 0 general)
projects/openttd_vs100.vcxproj
Show inline comments
 
@@ -794,6 +794,7 @@
 
    <ClCompile Include="..\src\script\script_scanner.cpp" />
 
    <ClInclude Include="..\src\script\script_scanner.hpp" />
 
    <ClInclude Include="..\src\script\script_storage.hpp" />
 
    <ClInclude Include="..\src\script\script_suspend.hpp" />
 
    <ClCompile Include="..\src\script\squirrel.cpp" />
 
    <ClInclude Include="..\src\script\squirrel.hpp" />
 
    <ClInclude Include="..\src\script\squirrel_class.hpp" />
projects/openttd_vs100.vcxproj.filters
Show inline comments
 
@@ -1605,6 +1605,9 @@
 
    <ClInclude Include="..\src\script\script_storage.hpp">
 
      <Filter>Script</Filter>
 
    </ClInclude>
 
    <ClInclude Include="..\src\script\script_suspend.hpp">
 
      <Filter>Script</Filter>
 
    </ClInclude>
 
    <ClCompile Include="..\src\script\squirrel.cpp">
 
      <Filter>Script</Filter>
 
    </ClCompile>
projects/openttd_vs80.vcproj
Show inline comments
 
@@ -2483,6 +2483,10 @@
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\script\script_suspend.hpp"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\script\squirrel.cpp"
 
				>
 
			</File>
projects/openttd_vs90.vcproj
Show inline comments
 
@@ -2480,6 +2480,10 @@
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\script\script_suspend.hpp"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\script\squirrel.cpp"
 
				>
 
			</File>
source.list
Show inline comments
 
@@ -559,6 +559,7 @@ script/script_info.hpp
 
script/script_scanner.cpp
 
script/script_scanner.hpp
 
script/script_storage.hpp
 
script/script_suspend.hpp
 
script/squirrel.cpp
 
script/squirrel.hpp
 
script/squirrel_class.hpp
src/ai/ai_instance.cpp
Show inline comments
 
@@ -18,8 +18,9 @@
 

	
 
#include "ai_config.hpp"
 
#include "ai_gui.hpp"
 

	
 
#include "../script/script_fatalerror.hpp"
 

	
 
#include "../script/script_suspend.hpp"
 
#include "../script/script_storage.hpp"
 
#include "ai_instance.hpp"
 

	
 
@@ -343,7 +344,7 @@ void AIInstance::GameLoop()
 
		}
 
		try {
 
			this->callback(this);
 
		} catch (AI_VMSuspend e) {
 
		} catch (Script_Suspend e) {
 
			this->suspend  = e.GetSuspendTime();
 
			this->callback = e.GetSuspendCallback();
 

	
 
@@ -373,7 +374,7 @@ void AIInstance::GameLoop()
 
			ScriptObject::SetAllowDoCommand(true);
 
			/* Start the AI by calling Start() */
 
			if (!this->engine->CallMethod(*this->instance, "Start",  _settings_game.ai.ai_max_opcode_till_suspend) || !this->engine->IsSuspended()) this->Died();
 
		} catch (AI_VMSuspend e) {
 
		} catch (Script_Suspend e) {
 
			this->suspend  = e.GetSuspendTime();
 
			this->callback = e.GetSuspendCallback();
 
		} catch (Script_FatalError e) {
 
@@ -394,7 +395,7 @@ void AIInstance::GameLoop()
 
	/* Continue the VM */
 
	try {
 
		if (!this->engine->Resume(_settings_game.ai.ai_max_opcode_till_suspend)) this->Died();
 
	} catch (AI_VMSuspend e) {
 
	} catch (Script_Suspend e) {
 
		this->suspend  = e.GetSuspendTime();
 
		this->callback = e.GetSuspendCallback();
 
	} catch (Script_FatalError e) {
src/ai/ai_instance.hpp
Show inline comments
 
@@ -13,43 +13,7 @@
 
#define AI_INSTANCE_HPP
 

	
 
#include <squirrel.h>
 

	
 
/**
 
 * The callback function when an AI suspends.
 
 */
 
typedef void (AISuspendCallbackProc)(class AIInstance *instance);
 

	
 
/**
 
 * A throw-class that is given when the VM wants to suspend.
 
 */
 
class AI_VMSuspend {
 
public:
 
	/**
 
	 * Create the suspend exception.
 
	 * @param time The amount of ticks to suspend.
 
	 * @param callback The callback to call when the AI may resume again.
 
	 */
 
	AI_VMSuspend(int time, AISuspendCallbackProc *callback) :
 
		time(time),
 
		callback(callback)
 
	{}
 

	
 
	/**
 
	 * Get the amount of ticks the AI should be suspended.
 
	 * @return The amount of AI ticks to suspend the AI.
 
	 */
 
	int GetSuspendTime() { return time; }
 

	
 
	/**
 
	 * Get the callback to call when the AI can run again.
 
	 * @return The callback function to run.
 
	 */
 
	AISuspendCallbackProc *GetSuspendCallback() { return callback; }
 

	
 
private:
 
	int time;                        ///< Amount of ticks to suspend the AI.
 
	AISuspendCallbackProc *callback; ///< Callback function to call when the AI can run again.
 
};
 
#include "../script/script_suspend.hpp"
 

	
 
/** Runtime information about an AI like a pointer to the squirrel vm and the current state. */
 
class AIInstance {
 
@@ -178,16 +142,16 @@ public:
 
	void InsertEvent(class ScriptEvent *event);
 

	
 
private:
 
	class ScriptController *controller; ///< The AI main class.
 
	class ScriptStorage *storage;       ///< Some global information for each running AI.
 
	class Squirrel *engine;             ///< A wrapper around the squirrel vm.
 
	SQObject *instance;                 ///< Squirrel-pointer to the AI main class.
 
	class ScriptController *controller;   ///< The AI main class.
 
	class ScriptStorage *storage;         ///< Some global information for each running AI.
 
	class Squirrel *engine;               ///< A wrapper around the squirrel vm.
 
	SQObject *instance;                   ///< Squirrel-pointer to the AI main class.
 

	
 
	bool is_started;                    ///< Is the AIs constructor executed?
 
	bool is_dead;                       ///< True if the AI has been stopped.
 
	bool is_save_data_on_stack;         ///< Is the save data still on the squirrel stack?
 
	int suspend;                        ///< The amount of ticks to suspend this AI before it's allowed to continue.
 
	AISuspendCallbackProc *callback;    ///< Callback that should be called in the next tick the AI runs.
 
	bool is_started;                      ///< Is the AIs constructor executed?
 
	bool is_dead;                         ///< True if the AI has been stopped.
 
	bool is_save_data_on_stack;           ///< Is the save data still on the squirrel stack?
 
	int suspend;                          ///< The amount of ticks to suspend this AI before it's allowed to continue.
 
	Script_SuspendCallbackProc *callback; ///< Callback that should be called in the next tick the AI runs.
 

	
 
	/**
 
	 * Register all API functions to the VM.
src/script/api/script_controller.cpp
Show inline comments
 
@@ -21,6 +21,7 @@
 
#include "../../ai/ai_config.hpp"
 
#include "../../ai/ai.hpp"
 
#include "../script_fatalerror.hpp"
 
#include "../script_suspend.hpp"
 
#include "script_log.hpp"
 

	
 
/* static */ void ScriptController::SetCommandDelay(int ticks)
 
@@ -40,7 +41,7 @@
 
		ticks = 1;
 
	}
 

	
 
	throw AI_VMSuspend(ticks, NULL);
 
	throw Script_Suspend(ticks, NULL);
 
}
 

	
 
/* static */ void ScriptController::Print(bool error_msg, const char *message)
src/script/api/script_object.cpp
Show inline comments
 
@@ -18,6 +18,7 @@
 
#include "../script_storage.hpp"
 
#include "../../ai/ai_instance.hpp"
 
#include "../script_fatalerror.hpp"
 
#include "../script_suspend.hpp"
 
#include "script_error.hpp"
 

	
 
/**
 
@@ -265,7 +266,7 @@ ScriptObject::ActiveInstance::~ActiveIns
 

	
 
	if (_networking) {
 
		/* Suspend the AI till the command is really executed. */
 
		throw AI_VMSuspend(-(int)GetDoCommandDelay(), callback);
 
		throw Script_Suspend(-(int)GetDoCommandDelay(), callback);
 
	} else {
 
		IncreaseDoCommandCosts(res.GetCost());
 

	
 
@@ -273,7 +274,7 @@ ScriptObject::ActiveInstance::~ActiveIns
 
		 *  both avoids confusion when a developer launched his AI in a
 
		 *  multiplayer game, but also gives time for the GUI and human player
 
		 *  to interact with the game. */
 
		throw AI_VMSuspend(GetDoCommandDelay(), callback);
 
		throw Script_Suspend(GetDoCommandDelay(), callback);
 
	}
 

	
 
	NOT_REACHED();
src/script/script_suspend.hpp
Show inline comments
 
new file 100644
 
/* $Id$ */
 

	
 
/*
 
 * This file is part of OpenTTD.
 
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
/** @file script_suspend.hpp The Script_Suspend tracks the suspension of a script. */
 

	
 
#ifndef SCRIPT_SUSPEND_HPP
 
#define SCRIPT_SUSPEND_HPP
 

	
 
/**
 
 * The callback function when a script suspends.
 
 */
 
typedef void (Script_SuspendCallbackProc)(class AIInstance *instance);
 

	
 
/**
 
 * A throw-class that is given when the script wants to suspend.
 
 */
 
class Script_Suspend {
 
public:
 
	/**
 
	 * Create the suspend exception.
 
	 * @param time The amount of ticks to suspend.
 
	 * @param callback The callback to call when the script may resume again.
 
	 */
 
	Script_Suspend(int time, Script_SuspendCallbackProc *callback) :
 
		time(time),
 
		callback(callback)
 
	{}
 

	
 
	/**
 
	 * Get the amount of ticks the script should be suspended.
 
	 * @return The amount of ticks to suspend the script.
 
	 */
 
	int GetSuspendTime() { return time; }
 

	
 
	/**
 
	 * Get the callback to call when the script can run again.
 
	 * @return The callback function to run.
 
	 */
 
	Script_SuspendCallbackProc *GetSuspendCallback() { return callback; }
 

	
 
private:
 
	int time;                             ///< Amount of ticks to suspend the script.
 
	Script_SuspendCallbackProc *callback; ///< Callback function to call when the script can run again.
 
};
 

	
 
#endif /* SCRIPT_SUSPEND_HPP */
0 comments (0 inline, 0 general)