Changeset - r18507:a734d5918322
[Not reviewed]
master
0 6 0
truebrain - 13 years ago 2011-11-29 23:21:42
truebrain@openttd.org
(svn r23361) -Codechange: move multiplayer DoCommand callback code so other script users can call their own
6 files changed with 28 insertions and 15 deletions:
0 comments (0 inline, 0 general)
src/ai/ai_core.cpp
Show inline comments
 
@@ -235,37 +235,24 @@
 
		event->Release();
 
		return;
 
	}
 

	
 
	/* Try to send the event to all AIs */
 
	for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
 
		if (c != skip_company) AI::NewEvent(c, event);
 
	}
 

	
 
	event->Release();
 
}
 

	
 
/**
 
 * DoCommand callback function for all commands executed by AIs.
 
 * @param result The result of the command.
 
 * @param tile The tile on which the command was executed.
 
 * @param p1 p1 as given to DoCommandPInternal.
 
 * @param p2 p2 as given to DoCommandPInternal.
 
 */
 
void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	Company::Get(_current_company)->ai_instance->DoCommandCallback(result, tile, p1, p2);
 
	Company::Get(_current_company)->ai_instance->Continue();
 
}
 

	
 
/* static */ void AI::Save(CompanyID company)
 
{
 
	if (!_networking || _network_server) {
 
		Company *c = Company::GetIfValid(company);
 
		assert(c != NULL && c->ai_instance != NULL);
 

	
 
		Backup<CompanyByte> cur_company(_current_company, company, FILE_LINE);
 
		c->ai_instance->Save();
 
		cur_company.Restore();
 
	} else {
 
		AIInstance::SaveEmpty();
 
	}
src/ai/ai_instance.cpp
Show inline comments
 
@@ -218,12 +218,30 @@ void AIInstance::Died()
 
	ShowAIDebugWindow(_current_company);
 

	
 
	const AIInfo *info = AIConfig::GetConfig(_current_company)->GetInfo();
 
	if (info != NULL) {
 
		ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH, INVALID_STRING_ID, WL_WARNING);
 

	
 
		if (info->GetURL() != NULL) {
 
			ScriptLog::Info("Please report the error to the following URL:");
 
			ScriptLog::Info(info->GetURL());
 
		}
 
	}
 
}
 

	
 
/**
 
 * DoCommand callback function for all commands executed by AIs.
 
 * @param result The result of the command.
 
 * @param tile The tile on which the command was executed.
 
 * @param p1 p1 as given to DoCommandPInternal.
 
 * @param p2 p2 as given to DoCommandPInternal.
 
 */
 
void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	Company::Get(_current_company)->ai_instance->DoCommandCallback(result, tile, p1, p2);
 
	Company::Get(_current_company)->ai_instance->Continue();
 
}
 

	
 
CommandCallback *AIInstance::GetDoCommandCallback()
 
{
 
	return &CcAI;
 
}
src/ai/ai_instance.hpp
Show inline comments
 
@@ -22,20 +22,21 @@ public:
 

	
 
	/**
 
	 * Initialize the AI and prepare it for its first run.
 
	 * @param info The AI to create the instance of.
 
	 */
 
	void Initialize(class AIInfo *info);
 

	
 
private:
 
	const char *versionAPI; ///< Current API used by this script.
 

	
 
	/* virtual */ void RegisterAPI();
 
	/* virtual */ void Died();
 
	/* virtual */ CommandCallback *GetDoCommandCallback();
 

	
 
	/**
 
	 * Load squirrel scripts to emulate an older API.
 
	 */
 
	bool LoadCompatibilityScripts(const char *api_version);
 
};
 

	
 
#endif /* AI_INSTANCE_HPP */
src/command_func.h
Show inline comments
 
@@ -61,25 +61,25 @@ bool IsCommandAllowedWhilePaused(uint32 
 
 */
 
static inline DoCommandFlag CommandFlagsToDCFlags(CommandFlags cmd_flags)
 
{
 
	DoCommandFlag flags = DC_NONE;
 
	if (cmd_flags & CMD_NO_WATER) flags |= DC_NO_WATER;
 
	if (cmd_flags & CMD_AUTO) flags |= DC_AUTO;
 
	if (cmd_flags & CMD_ALL_TILES) flags |= DC_ALL_TILES;
 
	return flags;
 
}
 

	
 
/*** All command callbacks that exist ***/
 

	
 
/* ai/ai_core.cpp */
 
/* ai/ai_instance.cpp */
 
CommandCallback CcAI;
 

	
 
/* airport_gui.cpp */
 
CommandCallback CcBuildAirport;
 

	
 
/* bridge_gui.cpp */
 
CommandCallback CcBuildBridge;
 

	
 
/* dock_gui.cpp */
 
CommandCallback CcBuildDocks;
 
CommandCallback CcBuildCanal;
 

	
src/script/api/script_object.cpp
Show inline comments
 
@@ -234,25 +234,25 @@ ScriptObject::ActiveInstance::~ActiveIns
 
	/* Set the default callback to return a true/false result of the DoCommand */
 
	if (callback == NULL) callback = &ScriptInstance::DoCommandReturn;
 

	
 
	/* Are we only interested in the estimate costs? */
 
	bool estimate_only = GetDoCommandMode() != NULL && !GetDoCommandMode()();
 

	
 
#ifdef ENABLE_NETWORK
 
	/* Only set p2 when the command does not come from the network. */
 
	if (GetCommandFlags(cmd) & CMD_CLIENT_ID && p2 == 0) p2 = UINT32_MAX;
 
#endif
 

	
 
	/* Try to perform the command. */
 
	CommandCost res = ::DoCommandPInternal(tile, p1, p2, cmd, _networking ? CcAI : NULL, text, false, estimate_only);
 
	CommandCost res = ::DoCommandPInternal(tile, p1, p2, cmd, _networking ? ScriptObject::GetActiveInstance()->GetDoCommandCallback() : NULL, text, false, estimate_only);
 

	
 
	/* We failed; set the error and bail out */
 
	if (res.Failed()) {
 
		SetLastError(ScriptError::StringToError(res.GetErrorMessage()));
 
		return false;
 
	}
 

	
 
	/* No error, then clear it. */
 
	SetLastError(ScriptError::ERR_NONE);
 

	
 
	/* Estimates, update the cost for the estimate and be done */
 
	if (estimate_only) {
src/script/script_instance.hpp
Show inline comments
 
@@ -6,24 +6,26 @@
 
 * 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_instance.hpp The ScriptInstance tracks a script. */
 

	
 
#ifndef SCRIPT_INSTANCE_HPP
 
#define SCRIPT_INSTANCE_HPP
 

	
 
#include <squirrel.h>
 
#include "script_suspend.hpp"
 

	
 
#include "../command_type.h"
 

	
 
/** Runtime information about a script like a pointer to the squirrel vm and the current state. */
 
class ScriptInstance {
 
public:
 
	friend class ScriptObject;
 
	friend class ScriptController;
 

	
 
	/**
 
	 * Create a new script.
 
	 */
 
	ScriptInstance(const char *APIName);
 
	virtual ~ScriptInstance();
 

	
 
@@ -146,24 +148,29 @@ protected:
 
	class Squirrel *engine;               ///< A wrapper around the squirrel vm.
 

	
 
	/**
 
	 * Register all API functions to the VM.
 
	 */
 
	virtual void RegisterAPI();
 

	
 
	/**
 
	 * Tell the script it died.
 
	 */
 
	virtual void Died();
 

	
 
	/**
 
	 * Get the callback handling DoCommands in case of networking.
 
	 */
 
	virtual CommandCallback *GetDoCommandCallback() = 0;
 

	
 
private:
 
	class ScriptController *controller;   ///< The script main class.
 
	class ScriptStorage *storage;         ///< Some global information for each running script.
 
	SQObject *instance;                   ///< Squirrel-pointer to the script main class.
 

	
 
	bool is_started;                      ///< Is the scripts constructor executed?
 
	bool is_dead;                         ///< True if the script 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 script before it's allowed to continue.
 
	Script_SuspendCallbackProc *callback; ///< Callback that should be called in the next tick the script runs.
 

	
 
	/**
0 comments (0 inline, 0 general)