1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
8 /** @file script_object.cpp Implementation of ScriptObject. */
9 
10 #include "../../stdafx.h"
11 #include "../../script/squirrel.hpp"
12 #include "../../command_func.h"
13 #include "../../company_func.h"
14 #include "../../company_base.h"
15 #include "../../network/network.h"
16 #include "../../genworld.h"
17 #include "../../string_func.h"
18 #include "../../strings_func.h"
19 
20 #include "../script_storage.hpp"
21 #include "../script_instance.hpp"
22 #include "../script_fatalerror.hpp"
23 #include "script_error.hpp"
24 #include "../../debug.h"
25 
26 #include "../../safeguards.h"
27 
28 /**
29  * Get the storage associated with the current ScriptInstance.
30  * @return The storage.
31  */
GetStorage()32 static ScriptStorage *GetStorage()
33 {
34 	return ScriptObject::GetActiveInstance()->GetStorage();
35 }
36 
37 
38 /* static */ ScriptInstance *ScriptObject::ActiveInstance::active = nullptr;
39 
ActiveInstance(ScriptInstance * instance)40 ScriptObject::ActiveInstance::ActiveInstance(ScriptInstance *instance) : alc_scope(instance->engine)
41 {
42 	this->last_active = ScriptObject::ActiveInstance::active;
43 	ScriptObject::ActiveInstance::active = instance;
44 }
45 
~ActiveInstance()46 ScriptObject::ActiveInstance::~ActiveInstance()
47 {
48 	ScriptObject::ActiveInstance::active = this->last_active;
49 }
50 
GetActiveInstance()51 /* static */ ScriptInstance *ScriptObject::GetActiveInstance()
52 {
53 	assert(ScriptObject::ActiveInstance::active != nullptr);
54 	return ScriptObject::ActiveInstance::active;
55 }
56 
57 
SetDoCommandDelay(uint ticks)58 /* static */ void ScriptObject::SetDoCommandDelay(uint ticks)
59 {
60 	assert(ticks > 0);
61 	GetStorage()->delay = ticks;
62 }
63 
GetDoCommandDelay()64 /* static */ uint ScriptObject::GetDoCommandDelay()
65 {
66 	return GetStorage()->delay;
67 }
68 
SetDoCommandMode(ScriptModeProc * proc,ScriptObject * instance)69 /* static */ void ScriptObject::SetDoCommandMode(ScriptModeProc *proc, ScriptObject *instance)
70 {
71 	GetStorage()->mode = proc;
72 	GetStorage()->mode_instance = instance;
73 }
74 
GetDoCommandMode()75 /* static */ ScriptModeProc *ScriptObject::GetDoCommandMode()
76 {
77 	return GetStorage()->mode;
78 }
79 
GetDoCommandModeInstance()80 /* static */ ScriptObject *ScriptObject::GetDoCommandModeInstance()
81 {
82 	return GetStorage()->mode_instance;
83 }
84 
SetLastCommand(TileIndex tile,uint32 p1,uint32 p2,uint32 cmd)85 /* static */ void ScriptObject::SetLastCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
86 {
87 	ScriptStorage *s = GetStorage();
88 	Debug(script, 6, "SetLastCommand company={:02d} tile={:06x} p1={:08x} p2={:08x} cmd={}", s->root_company, tile, p1, p2, cmd);
89 	s->last_tile = tile;
90 	s->last_p1 = p1;
91 	s->last_p2 = p2;
92 	s->last_cmd = cmd & CMD_ID_MASK;
93 }
94 
CheckLastCommand(TileIndex tile,uint32 p1,uint32 p2,uint32 cmd)95 /* static */ bool ScriptObject::CheckLastCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
96 {
97 	ScriptStorage *s = GetStorage();
98 	Debug(script, 6, "CheckLastCommand company={:02d} tile={:06x} p1={:08x} p2={:08x} cmd={}", s->root_company, tile, p1, p2, cmd);
99 	if (s->last_tile != tile) return false;
100 	if (s->last_p1 != p1) return false;
101 	if (s->last_p2 != p2) return false;
102 	if (s->last_cmd != (cmd & CMD_ID_MASK)) return false;
103 	return true;
104 }
105 
SetDoCommandCosts(Money value)106 /* static */ void ScriptObject::SetDoCommandCosts(Money value)
107 {
108 	GetStorage()->costs = CommandCost(value);
109 }
110 
IncreaseDoCommandCosts(Money value)111 /* static */ void ScriptObject::IncreaseDoCommandCosts(Money value)
112 {
113 	GetStorage()->costs.AddCost(value);
114 }
115 
GetDoCommandCosts()116 /* static */ Money ScriptObject::GetDoCommandCosts()
117 {
118 	return GetStorage()->costs.GetCost();
119 }
120 
SetLastError(ScriptErrorType last_error)121 /* static */ void ScriptObject::SetLastError(ScriptErrorType last_error)
122 {
123 	GetStorage()->last_error = last_error;
124 }
125 
GetLastError()126 /* static */ ScriptErrorType ScriptObject::GetLastError()
127 {
128 	return GetStorage()->last_error;
129 }
130 
SetLastCost(Money last_cost)131 /* static */ void ScriptObject::SetLastCost(Money last_cost)
132 {
133 	GetStorage()->last_cost = last_cost;
134 }
135 
GetLastCost()136 /* static */ Money ScriptObject::GetLastCost()
137 {
138 	return GetStorage()->last_cost;
139 }
140 
SetRoadType(RoadType road_type)141 /* static */ void ScriptObject::SetRoadType(RoadType road_type)
142 {
143 	GetStorage()->road_type = road_type;
144 }
145 
GetRoadType()146 /* static */ RoadType ScriptObject::GetRoadType()
147 {
148 	return GetStorage()->road_type;
149 }
150 
SetRailType(RailType rail_type)151 /* static */ void ScriptObject::SetRailType(RailType rail_type)
152 {
153 	GetStorage()->rail_type = rail_type;
154 }
155 
GetRailType()156 /* static */ RailType ScriptObject::GetRailType()
157 {
158 	return GetStorage()->rail_type;
159 }
160 
SetLastCommandRes(bool res)161 /* static */ void ScriptObject::SetLastCommandRes(bool res)
162 {
163 	GetStorage()->last_command_res = res;
164 	/* Also store the results of various global variables */
165 	SetNewVehicleID(_new_vehicle_id);
166 	SetNewSignID(_new_sign_id);
167 	SetNewGroupID(_new_group_id);
168 	SetNewGoalID(_new_goal_id);
169 	SetNewStoryPageID(_new_story_page_id);
170 	SetNewStoryPageElementID(_new_story_page_element_id);
171 }
172 
GetLastCommandRes()173 /* static */ bool ScriptObject::GetLastCommandRes()
174 {
175 	return GetStorage()->last_command_res;
176 }
177 
SetNewVehicleID(VehicleID vehicle_id)178 /* static */ void ScriptObject::SetNewVehicleID(VehicleID vehicle_id)
179 {
180 	GetStorage()->new_vehicle_id = vehicle_id;
181 }
182 
GetNewVehicleID()183 /* static */ VehicleID ScriptObject::GetNewVehicleID()
184 {
185 	return GetStorage()->new_vehicle_id;
186 }
187 
SetNewSignID(SignID sign_id)188 /* static */ void ScriptObject::SetNewSignID(SignID sign_id)
189 {
190 	GetStorage()->new_sign_id = sign_id;
191 }
192 
GetNewSignID()193 /* static */ SignID ScriptObject::GetNewSignID()
194 {
195 	return GetStorage()->new_sign_id;
196 }
197 
SetNewGroupID(GroupID group_id)198 /* static */ void ScriptObject::SetNewGroupID(GroupID group_id)
199 {
200 	GetStorage()->new_group_id = group_id;
201 }
202 
GetNewGroupID()203 /* static */ GroupID ScriptObject::GetNewGroupID()
204 {
205 	return GetStorage()->new_group_id;
206 }
207 
SetNewGoalID(GoalID goal_id)208 /* static */ void ScriptObject::SetNewGoalID(GoalID goal_id)
209 {
210 	GetStorage()->new_goal_id = goal_id;
211 }
212 
GetNewGoalID()213 /* static */ GroupID ScriptObject::GetNewGoalID()
214 {
215 	return GetStorage()->new_goal_id;
216 }
217 
SetNewStoryPageID(StoryPageID story_page_id)218 /* static */ void ScriptObject::SetNewStoryPageID(StoryPageID story_page_id)
219 {
220 	GetStorage()->new_story_page_id = story_page_id;
221 }
222 
GetNewStoryPageID()223 /* static */ GroupID ScriptObject::GetNewStoryPageID()
224 {
225 	return GetStorage()->new_story_page_id;
226 }
227 
SetNewStoryPageElementID(StoryPageElementID story_page_element_id)228 /* static */ void ScriptObject::SetNewStoryPageElementID(StoryPageElementID story_page_element_id)
229 {
230 	GetStorage()->new_story_page_element_id = story_page_element_id;
231 }
232 
GetNewStoryPageElementID()233 /* static */ GroupID ScriptObject::GetNewStoryPageElementID()
234 {
235 	return GetStorage()->new_story_page_element_id;
236 }
237 
SetAllowDoCommand(bool allow)238 /* static */ void ScriptObject::SetAllowDoCommand(bool allow)
239 {
240 	GetStorage()->allow_do_command = allow;
241 }
242 
GetAllowDoCommand()243 /* static */ bool ScriptObject::GetAllowDoCommand()
244 {
245 	return GetStorage()->allow_do_command;
246 }
247 
SetCompany(CompanyID company)248 /* static */ void ScriptObject::SetCompany(CompanyID company)
249 {
250 	if (GetStorage()->root_company == INVALID_OWNER) GetStorage()->root_company = company;
251 	GetStorage()->company = company;
252 
253 	_current_company = company;
254 }
255 
GetCompany()256 /* static */ CompanyID ScriptObject::GetCompany()
257 {
258 	return GetStorage()->company;
259 }
260 
GetRootCompany()261 /* static */ CompanyID ScriptObject::GetRootCompany()
262 {
263 	return GetStorage()->root_company;
264 }
265 
CanSuspend()266 /* static */ bool ScriptObject::CanSuspend()
267 {
268 	Squirrel *squirrel = ScriptObject::GetActiveInstance()->engine;
269 	return GetStorage()->allow_do_command && squirrel->CanSuspend();
270 }
271 
GetEventPointer()272 /* static */ void *&ScriptObject::GetEventPointer()
273 {
274 	return GetStorage()->event_data;
275 }
276 
GetLogPointer()277 /* static */ void *&ScriptObject::GetLogPointer()
278 {
279 	return GetStorage()->log_data;
280 }
281 
GetString(StringID string)282 /* static */ char *ScriptObject::GetString(StringID string)
283 {
284 	char buffer[64];
285 	::GetString(buffer, string, lastof(buffer));
286 	::StrMakeValidInPlace(buffer, lastof(buffer), SVS_NONE);
287 	return ::stredup(buffer);
288 }
289 
SetCallbackVariable(int index,int value)290 /* static */ void ScriptObject::SetCallbackVariable(int index, int value)
291 {
292 	if ((size_t)index >= GetStorage()->callback_value.size()) GetStorage()->callback_value.resize(index + 1);
293 	GetStorage()->callback_value[index] = value;
294 }
295 
GetCallbackVariable(int index)296 /* static */ int ScriptObject::GetCallbackVariable(int index)
297 {
298 	return GetStorage()->callback_value[index];
299 }
300 
DoCommand(TileIndex tile,uint32 p1,uint32 p2,uint cmd,const char * text,Script_SuspendCallbackProc * callback)301 /* static */ bool ScriptObject::DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint cmd, const char *text, Script_SuspendCallbackProc *callback)
302 {
303 	if (!ScriptObject::CanSuspend()) {
304 		throw Script_FatalError("You are not allowed to execute any DoCommand (even indirect) in your constructor, Save(), Load(), and any valuator.");
305 	}
306 
307 	if (ScriptObject::GetCompany() != OWNER_DEITY && !::Company::IsValidID(ScriptObject::GetCompany())) {
308 		ScriptObject::SetLastError(ScriptError::ERR_PRECONDITION_INVALID_COMPANY);
309 		return false;
310 	}
311 
312 	std::string command_text = text == nullptr ? std::string{} : text;
313 	if (!command_text.empty() && (GetCommandFlags(cmd) & CMD_STR_CTRL) == 0) {
314 		/* The string must be valid, i.e. not contain special codes. Since some
315 		 * can be made with GSText, make sure the control codes are removed. */
316 		command_text = ::StrMakeValid(command_text, SVS_NONE);
317 	}
318 
319 	/* Set the default callback to return a true/false result of the DoCommand */
320 	if (callback == nullptr) callback = &ScriptInstance::DoCommandReturn;
321 
322 	/* Are we only interested in the estimate costs? */
323 	bool estimate_only = GetDoCommandMode() != nullptr && !GetDoCommandMode()();
324 
325 	/* Only set p2 when the command does not come from the network. */
326 	if (GetCommandFlags(cmd) & CMD_CLIENT_ID && p2 == 0) p2 = UINT32_MAX;
327 
328 	/* Store the command for command callback validation. */
329 	if (!estimate_only && _networking && !_generating_world) SetLastCommand(tile, p1, p2, cmd);
330 
331 	/* Try to perform the command. */
332 	CommandCost res = ::DoCommandPInternal(tile, p1, p2, cmd, (_networking && !_generating_world) ? ScriptObject::GetActiveInstance()->GetDoCommandCallback() : nullptr, command_text, false, estimate_only);
333 
334 	/* We failed; set the error and bail out */
335 	if (res.Failed()) {
336 		SetLastError(ScriptError::StringToError(res.GetErrorMessage()));
337 		return false;
338 	}
339 
340 	/* No error, then clear it. */
341 	SetLastError(ScriptError::ERR_NONE);
342 
343 	/* Estimates, update the cost for the estimate and be done */
344 	if (estimate_only) {
345 		IncreaseDoCommandCosts(res.GetCost());
346 		return true;
347 	}
348 
349 	/* Costs of this operation. */
350 	SetLastCost(res.GetCost());
351 	SetLastCommandRes(true);
352 
353 	if (_generating_world) {
354 		IncreaseDoCommandCosts(res.GetCost());
355 		if (callback != nullptr) {
356 			/* Insert return value into to stack and throw a control code that
357 			 * the return value in the stack should be used. */
358 			callback(GetActiveInstance());
359 			throw SQInteger(1);
360 		}
361 		return true;
362 	} else if (_networking) {
363 		/* Suspend the script till the command is really executed. */
364 		throw Script_Suspend(-(int)GetDoCommandDelay(), callback);
365 	} else {
366 		IncreaseDoCommandCosts(res.GetCost());
367 
368 		/* Suspend the script player for 1+ ticks, so it simulates multiplayer. This
369 		 *  both avoids confusion when a developer launched the script in a
370 		 *  multiplayer game, but also gives time for the GUI and human player
371 		 *  to interact with the game. */
372 		throw Script_Suspend(GetDoCommandDelay(), callback);
373 	}
374 
375 	NOT_REACHED();
376 }
377