4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file script_object.cpp Implementation of ScriptObject. */
12 #include "../../stdafx.h"
13 #include "../../script/squirrel.hpp"
14 #include "../../command_func.h"
15 #include "../../company_func.h"
16 #include "../../company_base.h"
17 #include "../../network/network.h"
18 #include "../../genworld.h"
19 #include "../../string_func.h"
20 #include "../../strings_func.h"
22 #include "../script_storage.hpp"
23 #include "../script_instance.hpp"
24 #include "../script_fatalerror.hpp"
25 #include "script_error.hpp"
27 #include "../../safeguards.h"
30 * Get the storage associated with the current ScriptInstance.
31 * @return The storage.
33 static ScriptStorage
*GetStorage()
35 return ScriptObject::GetActiveInstance()->GetStorage();
39 /* static */ ScriptInstance
*ScriptObject::ActiveInstance::active
= NULL
;
41 ScriptObject::ActiveInstance::ActiveInstance(ScriptInstance
*instance
)
43 this->last_active
= ScriptObject::ActiveInstance::active
;
44 ScriptObject::ActiveInstance::active
= instance
;
47 ScriptObject::ActiveInstance::~ActiveInstance()
49 ScriptObject::ActiveInstance::active
= this->last_active
;
52 /* static */ ScriptInstance
*ScriptObject::GetActiveInstance()
54 assert(ScriptObject::ActiveInstance::active
!= NULL
);
55 return ScriptObject::ActiveInstance::active
;
59 /* static */ void ScriptObject::SetDoCommandDelay(uint ticks
)
62 GetStorage()->delay
= ticks
;
65 /* static */ uint
ScriptObject::GetDoCommandDelay()
67 return GetStorage()->delay
;
70 /* static */ void ScriptObject::SetDoCommandMode(ScriptModeProc
*proc
, ScriptObject
*instance
)
72 GetStorage()->mode
= proc
;
73 GetStorage()->mode_instance
= instance
;
76 /* static */ ScriptModeProc
*ScriptObject::GetDoCommandMode()
78 return GetStorage()->mode
;
81 /* static */ ScriptObject
*ScriptObject::GetDoCommandModeInstance()
83 return GetStorage()->mode_instance
;
86 /* static */ void ScriptObject::SetDoCommandCosts(Money value
)
88 GetStorage()->costs
= CommandCost(value
);
91 /* static */ void ScriptObject::IncreaseDoCommandCosts(Money value
)
93 GetStorage()->costs
.AddCost(value
);
96 /* static */ Money
ScriptObject::GetDoCommandCosts()
98 return GetStorage()->costs
.GetCost();
101 /* static */ void ScriptObject::SetLastError(ScriptErrorType last_error
)
103 GetStorage()->last_error
= last_error
;
106 /* static */ ScriptErrorType
ScriptObject::GetLastError()
108 return GetStorage()->last_error
;
111 /* static */ void ScriptObject::SetLastCost(Money last_cost
)
113 GetStorage()->last_cost
= last_cost
;
116 /* static */ Money
ScriptObject::GetLastCost()
118 return GetStorage()->last_cost
;
121 /* static */ void ScriptObject::SetRoadType(RoadType road_type
)
123 GetStorage()->road_type
= road_type
;
126 /* static */ RoadType
ScriptObject::GetRoadType()
128 return GetStorage()->road_type
;
131 /* static */ void ScriptObject::SetRailType(RailType rail_type
)
133 GetStorage()->rail_type
= rail_type
;
136 /* static */ RailType
ScriptObject::GetRailType()
138 return GetStorage()->rail_type
;
141 /* static */ void ScriptObject::SetLastCommandRes(bool res
)
143 GetStorage()->last_command_res
= res
;
144 /* Also store the results of various global variables */
145 SetNewVehicleID(_new_vehicle_id
);
146 SetNewSignID(_new_sign_id
);
147 SetNewGroupID(_new_group_id
);
148 SetNewGoalID(_new_goal_id
);
149 SetNewStoryPageID(_new_story_page_id
);
150 SetNewStoryPageElementID(_new_story_page_element_id
);
153 /* static */ bool ScriptObject::GetLastCommandRes()
155 return GetStorage()->last_command_res
;
158 /* static */ void ScriptObject::SetNewVehicleID(VehicleID vehicle_id
)
160 GetStorage()->new_vehicle_id
= vehicle_id
;
163 /* static */ VehicleID
ScriptObject::GetNewVehicleID()
165 return GetStorage()->new_vehicle_id
;
168 /* static */ void ScriptObject::SetNewSignID(SignID sign_id
)
170 GetStorage()->new_sign_id
= sign_id
;
173 /* static */ SignID
ScriptObject::GetNewSignID()
175 return GetStorage()->new_sign_id
;
178 /* static */ void ScriptObject::SetNewGroupID(GroupID group_id
)
180 GetStorage()->new_group_id
= group_id
;
183 /* static */ GroupID
ScriptObject::GetNewGroupID()
185 return GetStorage()->new_group_id
;
188 /* static */ void ScriptObject::SetNewGoalID(GoalID goal_id
)
190 GetStorage()->new_goal_id
= goal_id
;
193 /* static */ GroupID
ScriptObject::GetNewGoalID()
195 return GetStorage()->new_goal_id
;
198 /* static */ void ScriptObject::SetNewStoryPageID(StoryPageID story_page_id
)
200 GetStorage()->new_story_page_id
= story_page_id
;
203 /* static */ GroupID
ScriptObject::GetNewStoryPageID()
205 return GetStorage()->new_story_page_id
;
208 /* static */ void ScriptObject::SetNewStoryPageElementID(StoryPageElementID story_page_element_id
)
210 GetStorage()->new_story_page_element_id
= story_page_element_id
;
213 /* static */ GroupID
ScriptObject::GetNewStoryPageElementID()
215 return GetStorage()->new_story_page_element_id
;
218 /* static */ void ScriptObject::SetAllowDoCommand(bool allow
)
220 GetStorage()->allow_do_command
= allow
;
223 /* static */ bool ScriptObject::GetAllowDoCommand()
225 return GetStorage()->allow_do_command
;
228 /* static */ void ScriptObject::SetCompany(CompanyID company
)
230 if (GetStorage()->root_company
== INVALID_OWNER
) GetStorage()->root_company
= company
;
231 GetStorage()->company
= company
;
233 _current_company
= company
;
236 /* static */ CompanyID
ScriptObject::GetCompany()
238 return GetStorage()->company
;
241 /* static */ CompanyID
ScriptObject::GetRootCompany()
243 return GetStorage()->root_company
;
246 /* static */ bool ScriptObject::CanSuspend()
248 Squirrel
*squirrel
= ScriptObject::GetActiveInstance()->engine
;
249 return GetStorage()->allow_do_command
&& squirrel
->CanSuspend();
252 /* static */ void *&ScriptObject::GetEventPointer()
254 return GetStorage()->event_data
;
257 /* static */ void *&ScriptObject::GetLogPointer()
259 return GetStorage()->log_data
;
262 /* static */ char *ScriptObject::GetString(StringID string
)
265 ::GetString(buffer
, string
, lastof(buffer
));
266 ::str_validate(buffer
, lastof(buffer
), SVS_NONE
);
267 return ::stredup(buffer
);
270 /* static */ void ScriptObject::SetCallbackVariable(int index
, int value
)
272 if ((size_t)index
>= GetStorage()->callback_value
.size()) GetStorage()->callback_value
.resize(index
+ 1);
273 GetStorage()->callback_value
[index
] = value
;
276 /* static */ int ScriptObject::GetCallbackVariable(int index
)
278 return GetStorage()->callback_value
[index
];
281 /* static */ bool ScriptObject::DoCommand(TileIndex tile
, uint32 p1
, uint32 p2
, uint cmd
, const char *text
, Script_SuspendCallbackProc
*callback
)
283 if (!ScriptObject::CanSuspend()) {
284 throw Script_FatalError("You are not allowed to execute any DoCommand (even indirect) in your constructor, Save(), Load(), and any valuator.");
287 if (ScriptObject::GetCompany() != OWNER_DEITY
&& !::Company::IsValidID(ScriptObject::GetCompany())) {
288 ScriptObject::SetLastError(ScriptError::ERR_PRECONDITION_INVALID_COMPANY
);
292 if (!StrEmpty(text
) && (GetCommandFlags(cmd
) & CMD_STR_CTRL
) == 0) {
293 /* The string must be valid, i.e. not contain special codes. Since some
294 * can be made with GSText, make sure the control codes are removed. */
295 ::str_validate(const_cast<char *>(text
), text
+ strlen(text
), SVS_NONE
);
298 /* Set the default callback to return a true/false result of the DoCommand */
299 if (callback
== NULL
) callback
= &ScriptInstance::DoCommandReturn
;
301 /* Are we only interested in the estimate costs? */
302 bool estimate_only
= GetDoCommandMode() != NULL
&& !GetDoCommandMode()();
304 #ifdef ENABLE_NETWORK
305 /* Only set p2 when the command does not come from the network. */
306 if (GetCommandFlags(cmd
) & CMD_CLIENT_ID
&& p2
== 0) p2
= UINT32_MAX
;
309 /* Try to perform the command. */
310 CommandCost res
= ::DoCommandPInternal(tile
, p1
, p2
, cmd
, (_networking
&& !_generating_world
) ? ScriptObject::GetActiveInstance()->GetDoCommandCallback() : NULL
, text
, false, estimate_only
, 0);
312 /* We failed; set the error and bail out */
314 SetLastError(ScriptError::StringToError(res
.GetErrorMessage()));
318 /* No error, then clear it. */
319 SetLastError(ScriptError::ERR_NONE
);
321 /* Estimates, update the cost for the estimate and be done */
323 IncreaseDoCommandCosts(res
.GetCost());
327 /* Costs of this operation. */
328 SetLastCost(res
.GetCost());
329 SetLastCommandRes(true);
331 if (_generating_world
) {
332 IncreaseDoCommandCosts(res
.GetCost());
333 if (callback
!= NULL
) {
334 /* Insert return value into to stack and throw a control code that
335 * the return value in the stack should be used. */
336 callback(GetActiveInstance());
340 } else if (_networking
) {
341 /* Suspend the script till the command is really executed. */
342 throw Script_Suspend(-(int)GetDoCommandDelay(), callback
);
344 IncreaseDoCommandCosts(res
.GetCost());
346 /* Suspend the script player for 1+ ticks, so it simulates multiplayer. This
347 * both avoids confusion when a developer launched his script in a
348 * multiplayer game, but also gives time for the GUI and human player
349 * to interact with the game. */
350 throw Script_Suspend(GetDoCommandDelay(), callback
);