Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
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 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 ScriptInstance *ScriptObject::GetActiveInstance()
00049 {
00050 assert(ScriptObject::ActiveInstance::active != NULL);
00051 return ScriptObject::ActiveInstance::active;
00052 }
00053
00054
00055 void ScriptObject::SetDoCommandDelay(uint ticks)
00056 {
00057 assert(ticks > 0);
00058 GetStorage()->delay = ticks;
00059 }
00060
00061 uint ScriptObject::GetDoCommandDelay()
00062 {
00063 return GetStorage()->delay;
00064 }
00065
00066 void ScriptObject::SetDoCommandMode(ScriptModeProc *proc, ScriptObject *instance)
00067 {
00068 GetStorage()->mode = proc;
00069 GetStorage()->mode_instance = instance;
00070 }
00071
00072 ScriptModeProc *ScriptObject::GetDoCommandMode()
00073 {
00074 return GetStorage()->mode;
00075 }
00076
00077 ScriptObject *ScriptObject::GetDoCommandModeInstance()
00078 {
00079 return GetStorage()->mode_instance;
00080 }
00081
00082 void ScriptObject::SetDoCommandCosts(Money value)
00083 {
00084 GetStorage()->costs = CommandCost(value);
00085 }
00086
00087 void ScriptObject::IncreaseDoCommandCosts(Money value)
00088 {
00089 GetStorage()->costs.AddCost(value);
00090 }
00091
00092 Money ScriptObject::GetDoCommandCosts()
00093 {
00094 return GetStorage()->costs.GetCost();
00095 }
00096
00097 void ScriptObject::SetLastError(ScriptErrorType last_error)
00098 {
00099 GetStorage()->last_error = last_error;
00100 }
00101
00102 ScriptErrorType ScriptObject::GetLastError()
00103 {
00104 return GetStorage()->last_error;
00105 }
00106
00107 void ScriptObject::SetLastCost(Money last_cost)
00108 {
00109 GetStorage()->last_cost = last_cost;
00110 }
00111
00112 Money ScriptObject::GetLastCost()
00113 {
00114 return GetStorage()->last_cost;
00115 }
00116
00117 void ScriptObject::SetRoadType(RoadType road_type)
00118 {
00119 GetStorage()->road_type = road_type;
00120 }
00121
00122 RoadType ScriptObject::GetRoadType()
00123 {
00124 return GetStorage()->road_type;
00125 }
00126
00127 void ScriptObject::SetRailType(RailType rail_type)
00128 {
00129 GetStorage()->rail_type = rail_type;
00130 }
00131
00132 RailType ScriptObject::GetRailType()
00133 {
00134 return GetStorage()->rail_type;
00135 }
00136
00137 void ScriptObject::SetLastCommandRes(bool res)
00138 {
00139 GetStorage()->last_command_res = res;
00140
00141 SetNewVehicleID(_new_vehicle_id);
00142 SetNewSignID(_new_sign_id);
00143 SetNewTunnelEndtile(_build_tunnel_endtile);
00144 SetNewGroupID(_new_group_id);
00145 }
00146
00147 bool ScriptObject::GetLastCommandRes()
00148 {
00149 return GetStorage()->last_command_res;
00150 }
00151
00152 void ScriptObject::SetNewVehicleID(VehicleID vehicle_id)
00153 {
00154 GetStorage()->new_vehicle_id = vehicle_id;
00155 }
00156
00157 VehicleID ScriptObject::GetNewVehicleID()
00158 {
00159 return GetStorage()->new_vehicle_id;
00160 }
00161
00162 void ScriptObject::SetNewSignID(SignID sign_id)
00163 {
00164 GetStorage()->new_sign_id = sign_id;
00165 }
00166
00167 SignID ScriptObject::GetNewSignID()
00168 {
00169 return GetStorage()->new_sign_id;
00170 }
00171
00172 void ScriptObject::SetNewTunnelEndtile(TileIndex tile)
00173 {
00174 GetStorage()->new_tunnel_endtile = tile;
00175 }
00176
00177 TileIndex ScriptObject::GetNewTunnelEndtile()
00178 {
00179 return GetStorage()->new_tunnel_endtile;
00180 }
00181
00182 void ScriptObject::SetNewGroupID(GroupID group_id)
00183 {
00184 GetStorage()->new_group_id = group_id;
00185 }
00186
00187 GroupID ScriptObject::GetNewGroupID()
00188 {
00189 return GetStorage()->new_group_id;
00190 }
00191
00192 void ScriptObject::SetAllowDoCommand(bool allow)
00193 {
00194 GetStorage()->allow_do_command = allow;
00195 }
00196
00197 bool ScriptObject::GetAllowDoCommand()
00198 {
00199 return GetStorage()->allow_do_command;
00200 }
00201
00202 bool ScriptObject::CanSuspend()
00203 {
00204 Squirrel *squirrel = ScriptObject::GetActiveInstance()->engine;
00205 return GetStorage()->allow_do_command && squirrel->CanSuspend();
00206 }
00207
00208 void *&ScriptObject::GetEventPointer()
00209 {
00210 return GetStorage()->event_data;
00211 }
00212
00213 void *&ScriptObject::GetLogPointer()
00214 {
00215 return GetStorage()->log_data;
00216 }
00217
00218 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 int ScriptObject::GetCallbackVariable(int index)
00225 {
00226 return GetStorage()->callback_value[index];
00227 }
00228
00229 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
00236 if (callback == NULL) callback = &ScriptInstance::DoCommandReturn;
00237
00238
00239 bool estimate_only = GetDoCommandMode() != NULL && !GetDoCommandMode()();
00240
00241 #ifdef ENABLE_NETWORK
00242
00243 if (GetCommandFlags(cmd) & CMD_CLIENT_ID && p2 == 0) p2 = UINT32_MAX;
00244 #endif
00245
00246
00247 CommandCost res = ::DoCommandPInternal(tile, p1, p2, cmd, _networking ? ScriptObject::GetActiveInstance()->GetDoCommandCallback() : NULL, text, false, estimate_only);
00248
00249
00250 if (res.Failed()) {
00251 SetLastError(ScriptError::StringToError(res.GetErrorMessage()));
00252 return false;
00253 }
00254
00255
00256 SetLastError(ScriptError::ERR_NONE);
00257
00258
00259 if (estimate_only) {
00260 IncreaseDoCommandCosts(res.GetCost());
00261 return true;
00262 }
00263
00264
00265 SetLastCost(res.GetCost());
00266 SetLastCommandRes(true);
00267
00268 if (_networking) {
00269
00270 throw Script_Suspend(-(int)GetDoCommandDelay(), callback);
00271 } else {
00272 IncreaseDoCommandCosts(res.GetCost());
00273
00274
00275
00276
00277
00278 throw Script_Suspend(GetDoCommandDelay(), callback);
00279 }
00280
00281 NOT_REACHED();
00282 }