script_object.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "../../stdafx.h"
00013 #include "../../script/squirrel.hpp"
00014 #include "../../command_func.h"
00015 #include "../../company_func.h"
00016 #include "../../network/network.h"
00017 #include "../../tunnelbridge.h"
00018 
00019 #include "../script_storage.hpp"
00020 #include "../script_instance.hpp"
00021 #include "../script_fatalerror.hpp"
00022 #include "../script_suspend.hpp"
00023 #include "script_error.hpp"
00024 
00029 static ScriptStorage *GetStorage()
00030 {
00031   return ScriptObject::GetActiveInstance()->GetStorage();
00032 }
00033 
00034 
00035 /* static */ ScriptInstance *ScriptObject::ActiveInstance::active = NULL;
00036 
00037 ScriptObject::ActiveInstance::ActiveInstance(ScriptInstance *instance)
00038 {
00039   this->last_active = ScriptObject::ActiveInstance::active;
00040   ScriptObject::ActiveInstance::active = instance;
00041 }
00042 
00043 ScriptObject::ActiveInstance::~ActiveInstance()
00044 {
00045   ScriptObject::ActiveInstance::active = this->last_active;
00046 }
00047 
00048 /* static */ ScriptInstance *ScriptObject::GetActiveInstance()
00049 {
00050   assert(ScriptObject::ActiveInstance::active != NULL);
00051   return ScriptObject::ActiveInstance::active;
00052 }
00053 
00054 
00055 /* static */ void ScriptObject::SetDoCommandDelay(uint ticks)
00056 {
00057   assert(ticks > 0);
00058   GetStorage()->delay = ticks;
00059 }
00060 
00061 /* static */ uint ScriptObject::GetDoCommandDelay()
00062 {
00063   return GetStorage()->delay;
00064 }
00065 
00066 /* static */ void ScriptObject::SetDoCommandMode(ScriptModeProc *proc, ScriptObject *instance)
00067 {
00068   GetStorage()->mode = proc;
00069   GetStorage()->mode_instance = instance;
00070 }
00071 
00072 /* static */ ScriptModeProc *ScriptObject::GetDoCommandMode()
00073 {
00074   return GetStorage()->mode;
00075 }
00076 
00077 /* static */ ScriptObject *ScriptObject::GetDoCommandModeInstance()
00078 {
00079   return GetStorage()->mode_instance;
00080 }
00081 
00082 /* static */ void ScriptObject::SetDoCommandCosts(Money value)
00083 {
00084   GetStorage()->costs = CommandCost(value);
00085 }
00086 
00087 /* static */ void ScriptObject::IncreaseDoCommandCosts(Money value)
00088 {
00089   GetStorage()->costs.AddCost(value);
00090 }
00091 
00092 /* static */ Money ScriptObject::GetDoCommandCosts()
00093 {
00094   return GetStorage()->costs.GetCost();
00095 }
00096 
00097 /* static */ void ScriptObject::SetLastError(ScriptErrorType last_error)
00098 {
00099   GetStorage()->last_error = last_error;
00100 }
00101 
00102 /* static */ ScriptErrorType ScriptObject::GetLastError()
00103 {
00104   return GetStorage()->last_error;
00105 }
00106 
00107 /* static */ void ScriptObject::SetLastCost(Money last_cost)
00108 {
00109   GetStorage()->last_cost = last_cost;
00110 }
00111 
00112 /* static */ Money ScriptObject::GetLastCost()
00113 {
00114   return GetStorage()->last_cost;
00115 }
00116 
00117 /* static */ void ScriptObject::SetRoadType(RoadType road_type)
00118 {
00119   GetStorage()->road_type = road_type;
00120 }
00121 
00122 /* static */ RoadType ScriptObject::GetRoadType()
00123 {
00124   return GetStorage()->road_type;
00125 }
00126 
00127 /* static */ void ScriptObject::SetRailType(RailType rail_type)
00128 {
00129   GetStorage()->rail_type = rail_type;
00130 }
00131 
00132 /* static */ RailType ScriptObject::GetRailType()
00133 {
00134   return GetStorage()->rail_type;
00135 }
00136 
00137 /* static */ void ScriptObject::SetLastCommandRes(bool res)
00138 {
00139   GetStorage()->last_command_res = res;
00140   /* Also store the results of various global variables */
00141   SetNewVehicleID(_new_vehicle_id);
00142   SetNewSignID(_new_sign_id);
00143   SetNewTunnelEndtile(_build_tunnel_endtile);
00144   SetNewGroupID(_new_group_id);
00145 }
00146 
00147 /* static */ bool ScriptObject::GetLastCommandRes()
00148 {
00149   return GetStorage()->last_command_res;
00150 }
00151 
00152 /* static */ void ScriptObject::SetNewVehicleID(VehicleID vehicle_id)
00153 {
00154   GetStorage()->new_vehicle_id = vehicle_id;
00155 }
00156 
00157 /* static */ VehicleID ScriptObject::GetNewVehicleID()
00158 {
00159   return GetStorage()->new_vehicle_id;
00160 }
00161 
00162 /* static */ void ScriptObject::SetNewSignID(SignID sign_id)
00163 {
00164   GetStorage()->new_sign_id = sign_id;
00165 }
00166 
00167 /* static */ SignID ScriptObject::GetNewSignID()
00168 {
00169   return GetStorage()->new_sign_id;
00170 }
00171 
00172 /* static */ void ScriptObject::SetNewTunnelEndtile(TileIndex tile)
00173 {
00174   GetStorage()->new_tunnel_endtile = tile;
00175 }
00176 
00177 /* static */ TileIndex ScriptObject::GetNewTunnelEndtile()
00178 {
00179   return GetStorage()->new_tunnel_endtile;
00180 }
00181 
00182 /* static */ void ScriptObject::SetNewGroupID(GroupID group_id)
00183 {
00184   GetStorage()->new_group_id = group_id;
00185 }
00186 
00187 /* static */ GroupID ScriptObject::GetNewGroupID()
00188 {
00189   return GetStorage()->new_group_id;
00190 }
00191 
00192 /* static */ void ScriptObject::SetAllowDoCommand(bool allow)
00193 {
00194   GetStorage()->allow_do_command = allow;
00195 }
00196 
00197 /* static */ bool ScriptObject::GetAllowDoCommand()
00198 {
00199   return GetStorage()->allow_do_command;
00200 }
00201 
00202 /* static */ bool ScriptObject::CanSuspend()
00203 {
00204   Squirrel *squirrel = ScriptObject::GetActiveInstance()->engine;
00205   return GetStorage()->allow_do_command && squirrel->CanSuspend();
00206 }
00207 
00208 /* static */ void *&ScriptObject::GetEventPointer()
00209 {
00210   return GetStorage()->event_data;
00211 }
00212 
00213 /* static */ void *&ScriptObject::GetLogPointer()
00214 {
00215   return GetStorage()->log_data;
00216 }
00217 
00218 /* static */ void ScriptObject::SetCallbackVariable(int index, int value)
00219 {
00220   if ((size_t)index >= GetStorage()->callback_value.size()) GetStorage()->callback_value.resize(index + 1);
00221   GetStorage()->callback_value[index] = value;
00222 }
00223 
00224 /* static */ int ScriptObject::GetCallbackVariable(int index)
00225 {
00226   return GetStorage()->callback_value[index];
00227 }
00228 
00229 /* static */ bool ScriptObject::DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint cmd, const char *text, Script_SuspendCallbackProc *callback)
00230 {
00231   if (!ScriptObject::CanSuspend()) {
00232     throw Script_FatalError("You are not allowed to execute any DoCommand (even indirect) in your constructor, Save(), Load(), and any valuator.");
00233   }
00234 
00235   /* Set the default callback to return a true/false result of the DoCommand */
00236   if (callback == NULL) callback = &ScriptInstance::DoCommandReturn;
00237 
00238   /* Are we only interested in the estimate costs? */
00239   bool estimate_only = GetDoCommandMode() != NULL && !GetDoCommandMode()();
00240 
00241 #ifdef ENABLE_NETWORK
00242   /* Only set p2 when the command does not come from the network. */
00243   if (GetCommandFlags(cmd) & CMD_CLIENT_ID && p2 == 0) p2 = UINT32_MAX;
00244 #endif
00245 
00246   /* Try to perform the command. */
00247   CommandCost res = ::DoCommandPInternal(tile, p1, p2, cmd, _networking ? ScriptObject::GetActiveInstance()->GetDoCommandCallback() : NULL, text, false, estimate_only);
00248 
00249   /* We failed; set the error and bail out */
00250   if (res.Failed()) {
00251     SetLastError(ScriptError::StringToError(res.GetErrorMessage()));
00252     return false;
00253   }
00254 
00255   /* No error, then clear it. */
00256   SetLastError(ScriptError::ERR_NONE);
00257 
00258   /* Estimates, update the cost for the estimate and be done */
00259   if (estimate_only) {
00260     IncreaseDoCommandCosts(res.GetCost());
00261     return true;
00262   }
00263 
00264   /* Costs of this operation. */
00265   SetLastCost(res.GetCost());
00266   SetLastCommandRes(true);
00267 
00268   if (_networking) {
00269     /* Suspend the AI till the command is really executed. */
00270     throw Script_Suspend(-(int)GetDoCommandDelay(), callback);
00271   } else {
00272     IncreaseDoCommandCosts(res.GetCost());
00273 
00274     /* Suspend the AI player for 1+ ticks, so it simulates multiplayer. This
00275      *  both avoids confusion when a developer launched his AI in a
00276      *  multiplayer game, but also gives time for the GUI and human player
00277      *  to interact with the game. */
00278     throw Script_Suspend(GetDoCommandDelay(), callback);
00279   }
00280 
00281   NOT_REACHED();
00282 }