2009-01-12 17:11:45 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/** @file script_object.cpp Implementation of ScriptObject. */
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2009-06-01 22:00:47 +00:00
|
|
|
#include "../../stdafx.h"
|
|
|
|
#include "../../script/squirrel.hpp"
|
2011-01-22 14:52:20 +00:00
|
|
|
#include "../../command_func.h"
|
|
|
|
#include "../../network/network.h"
|
|
|
|
#include "../../tunnelbridge.h"
|
2009-06-01 22:00:47 +00:00
|
|
|
|
2011-11-29 23:21:04 +00:00
|
|
|
#include "../script_storage.hpp"
|
2011-11-29 23:07:38 +00:00
|
|
|
#include "../../ai/ai_instance.hpp"
|
2011-11-29 23:21:13 +00:00
|
|
|
#include "../script_fatalerror.hpp"
|
2011-11-29 23:21:24 +00:00
|
|
|
#include "../script_suspend.hpp"
|
2011-11-29 23:07:38 +00:00
|
|
|
#include "script_error.hpp"
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-05-01 09:24:19 +00:00
|
|
|
/**
|
2011-11-29 23:21:33 +00:00
|
|
|
* Get the storage associated with the current ScriptInstance.
|
2011-05-01 09:24:19 +00:00
|
|
|
* @return The storage.
|
|
|
|
*/
|
2011-11-29 23:21:04 +00:00
|
|
|
static ScriptStorage *GetStorage()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:15:35 +00:00
|
|
|
return ScriptObject::GetActiveInstance()->GetStorage();
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:43:48 +00:00
|
|
|
|
2011-11-29 23:21:33 +00:00
|
|
|
/* static */ ScriptInstance *ScriptObject::ActiveInstance::active = NULL;
|
2011-11-13 20:43:48 +00:00
|
|
|
|
2011-11-29 23:21:33 +00:00
|
|
|
ScriptObject::ActiveInstance::ActiveInstance(ScriptInstance *instance)
|
2011-11-13 20:43:48 +00:00
|
|
|
{
|
2011-11-29 23:15:35 +00:00
|
|
|
this->last_active = ScriptObject::ActiveInstance::active;
|
|
|
|
ScriptObject::ActiveInstance::active = instance;
|
2011-11-13 20:43:48 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
ScriptObject::ActiveInstance::~ActiveInstance()
|
2011-11-13 20:43:48 +00:00
|
|
|
{
|
2011-11-29 23:15:35 +00:00
|
|
|
ScriptObject::ActiveInstance::active = this->last_active;
|
2011-11-13 20:43:48 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:21:33 +00:00
|
|
|
/* static */ ScriptInstance *ScriptObject::GetActiveInstance()
|
2011-11-13 20:43:48 +00:00
|
|
|
{
|
2011-11-29 23:15:35 +00:00
|
|
|
assert(ScriptObject::ActiveInstance::active != NULL);
|
|
|
|
return ScriptObject::ActiveInstance::active;
|
2011-11-13 20:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ void ScriptObject::SetDoCommandDelay(uint ticks)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
assert(ticks > 0);
|
|
|
|
GetStorage()->delay = ticks;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ uint ScriptObject::GetDoCommandDelay()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return GetStorage()->delay;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:21:04 +00:00
|
|
|
/* static */ void ScriptObject::SetDoCommandMode(ScriptModeProc *proc, ScriptObject *instance)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
GetStorage()->mode = proc;
|
|
|
|
GetStorage()->mode_instance = instance;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:21:04 +00:00
|
|
|
/* static */ ScriptModeProc *ScriptObject::GetDoCommandMode()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return GetStorage()->mode;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ ScriptObject *ScriptObject::GetDoCommandModeInstance()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return GetStorage()->mode_instance;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ void ScriptObject::SetDoCommandCosts(Money value)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
GetStorage()->costs = CommandCost(value);
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ void ScriptObject::IncreaseDoCommandCosts(Money value)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
GetStorage()->costs.AddCost(value);
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ Money ScriptObject::GetDoCommandCosts()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return GetStorage()->costs.GetCost();
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ void ScriptObject::SetLastError(ScriptErrorType last_error)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
GetStorage()->last_error = last_error;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ ScriptErrorType ScriptObject::GetLastError()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return GetStorage()->last_error;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ void ScriptObject::SetLastCost(Money last_cost)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
GetStorage()->last_cost = last_cost;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ Money ScriptObject::GetLastCost()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return GetStorage()->last_cost;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ void ScriptObject::SetRoadType(RoadType road_type)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
GetStorage()->road_type = road_type;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ RoadType ScriptObject::GetRoadType()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return GetStorage()->road_type;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ void ScriptObject::SetRailType(RailType rail_type)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
GetStorage()->rail_type = rail_type;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ RailType ScriptObject::GetRailType()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return GetStorage()->rail_type;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ void ScriptObject::SetLastCommandRes(bool res)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
GetStorage()->last_command_res = res;
|
|
|
|
/* Also store the results of various global variables */
|
|
|
|
SetNewVehicleID(_new_vehicle_id);
|
|
|
|
SetNewSignID(_new_sign_id);
|
|
|
|
SetNewTunnelEndtile(_build_tunnel_endtile);
|
|
|
|
SetNewGroupID(_new_group_id);
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptObject::GetLastCommandRes()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return GetStorage()->last_command_res;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ void ScriptObject::SetNewVehicleID(VehicleID vehicle_id)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
GetStorage()->new_vehicle_id = vehicle_id;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ VehicleID ScriptObject::GetNewVehicleID()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return GetStorage()->new_vehicle_id;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ void ScriptObject::SetNewSignID(SignID sign_id)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
GetStorage()->new_sign_id = sign_id;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ SignID ScriptObject::GetNewSignID()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return GetStorage()->new_sign_id;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ void ScriptObject::SetNewTunnelEndtile(TileIndex tile)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
GetStorage()->new_tunnel_endtile = tile;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ TileIndex ScriptObject::GetNewTunnelEndtile()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return GetStorage()->new_tunnel_endtile;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ void ScriptObject::SetNewGroupID(GroupID group_id)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
GetStorage()->new_group_id = group_id;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ GroupID ScriptObject::GetNewGroupID()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return GetStorage()->new_group_id;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ void ScriptObject::SetAllowDoCommand(bool allow)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
GetStorage()->allow_do_command = allow;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptObject::GetAllowDoCommand()
|
2009-09-11 17:18:06 +00:00
|
|
|
{
|
|
|
|
return GetStorage()->allow_do_command;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptObject::CanSuspend()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:15:35 +00:00
|
|
|
Squirrel *squirrel = ScriptObject::GetActiveInstance()->engine;
|
2009-06-01 22:00:47 +00:00
|
|
|
return GetStorage()->allow_do_command && squirrel->CanSuspend();
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ void *&ScriptObject::GetEventPointer()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return GetStorage()->event_data;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ void *&ScriptObject::GetLogPointer()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return GetStorage()->log_data;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ void ScriptObject::SetCallbackVariable(int index, int value)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if ((size_t)index >= GetStorage()->callback_value.size()) GetStorage()->callback_value.resize(index + 1);
|
|
|
|
GetStorage()->callback_value[index] = value;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ int ScriptObject::GetCallbackVariable(int index)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return GetStorage()->callback_value[index];
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:21:33 +00:00
|
|
|
/* static */ bool ScriptObject::DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint cmd, const char *text, Script_SuspendCallbackProc *callback)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:15:35 +00:00
|
|
|
if (!ScriptObject::CanSuspend()) {
|
2011-11-29 23:21:13 +00:00
|
|
|
throw Script_FatalError("You are not allowed to execute any DoCommand (even indirect) in your constructor, Save(), Load(), and any valuator.");
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the default callback to return a true/false result of the DoCommand */
|
2011-11-29 23:21:33 +00:00
|
|
|
if (callback == NULL) callback = &ScriptInstance::DoCommandReturn;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2010-01-11 20:42:07 +00:00
|
|
|
/* Are we only interested in the estimate costs? */
|
|
|
|
bool estimate_only = GetDoCommandMode() != NULL && !GetDoCommandMode()();
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2010-08-19 14:25:07 +00:00
|
|
|
#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
|
|
|
|
|
2010-01-11 20:42:07 +00:00
|
|
|
/* Try to perform the command. */
|
|
|
|
CommandCost res = ::DoCommandPInternal(tile, p1, p2, cmd, _networking ? CcAI : NULL, text, false, estimate_only);
|
|
|
|
|
|
|
|
/* We failed; set the error and bail out */
|
2010-01-18 22:57:21 +00:00
|
|
|
if (res.Failed()) {
|
2011-11-29 23:15:35 +00:00
|
|
|
SetLastError(ScriptError::StringToError(res.GetErrorMessage()));
|
2009-01-12 17:11:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-11 20:42:07 +00:00
|
|
|
/* No error, then clear it. */
|
2011-11-29 23:15:35 +00:00
|
|
|
SetLastError(ScriptError::ERR_NONE);
|
2010-01-11 20:42:07 +00:00
|
|
|
|
|
|
|
/* Estimates, update the cost for the estimate and be done */
|
|
|
|
if (estimate_only) {
|
2009-01-12 17:11:45 +00:00
|
|
|
IncreaseDoCommandCosts(res.GetCost());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-01-11 20:42:07 +00:00
|
|
|
/* Costs of this operation. */
|
|
|
|
SetLastCost(res.GetCost());
|
|
|
|
SetLastCommandRes(true);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2010-01-11 20:42:07 +00:00
|
|
|
if (_networking) {
|
|
|
|
/* Suspend the AI till the command is really executed. */
|
2011-11-29 23:21:24 +00:00
|
|
|
throw Script_Suspend(-(int)GetDoCommandDelay(), callback);
|
2009-01-12 17:11:45 +00:00
|
|
|
} else {
|
|
|
|
IncreaseDoCommandCosts(res.GetCost());
|
|
|
|
|
|
|
|
/* Suspend the AI player for 1+ ticks, so it simulates multiplayer. This
|
|
|
|
* 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. */
|
2011-11-29 23:21:24 +00:00
|
|
|
throw Script_Suspend(GetDoCommandDelay(), callback);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|