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
 
@@ -241,25 +241,12 @@
 
		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);
 

	
src/ai/ai_instance.cpp
Show inline comments
 
@@ -224,6 +224,24 @@ void AIInstance::Died()
 
		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
 
@@ -28,12 +28,13 @@ public:
 

	
 
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);
 
};
src/command_func.h
Show inline comments
 
@@ -67,13 +67,13 @@ static inline DoCommandFlag CommandFlags
 
	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 */
src/script/api/script_object.cpp
Show inline comments
 
@@ -240,13 +240,13 @@ ScriptObject::ActiveInstance::~ActiveIns
 
#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;
 
	}
src/script/script_instance.hpp
Show inline comments
 
@@ -12,12 +12,14 @@
 
#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;
 

	
 
@@ -152,12 +154,17 @@ protected:
 

	
 
	/**
 
	 * 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?
0 comments (0 inline, 0 general)