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.hpp Main object, on which all objects depend. */
9 
10 #ifndef SCRIPT_OBJECT_HPP
11 #define SCRIPT_OBJECT_HPP
12 
13 #include "../../misc/countedptr.hpp"
14 #include "../../road_type.h"
15 #include "../../rail_type.h"
16 
17 #include "script_types.hpp"
18 #include "../script_suspend.hpp"
19 #include "../squirrel.hpp"
20 
21 /**
22  * The callback function for Mode-classes.
23  */
24 typedef bool (ScriptModeProc)();
25 
26 /**
27  * Uper-parent object of all API classes. You should never use this class in
28  *   your script, as it doesn't publish any public functions. It is used
29  *   internally to have a common place to handle general things, like internal
30  *   command processing, and command-validation checks.
31  * @api none
32  */
33 class ScriptObject : public SimpleCountedObject {
34 friend class ScriptInstance;
35 friend class ScriptController;
36 protected:
37 	/**
38 	 * A class that handles the current active instance. By instantiating it at
39 	 *  the beginning of a function with the current active instance, it remains
40 	 *  active till the scope of the variable closes. It then automatically
41 	 *  reverts to the active instance it was before instantiating.
42 	 */
43 	class ActiveInstance {
44 	friend class ScriptObject;
45 	public:
46 		ActiveInstance(ScriptInstance *instance);
47 		~ActiveInstance();
48 	private:
49 		ScriptInstance *last_active;    ///< The active instance before we go instantiated.
50 		ScriptAllocatorScope alc_scope; ///< Keep the correct allocator for the script instance activated
51 
52 		static ScriptInstance *active;  ///< The global current active instance.
53 	};
54 
55 public:
56 	/**
57 	 * Store the latest result of a DoCommand per company.
58 	 * @param res The result of the last command.
59 	 */
60 	static void SetLastCommandRes(bool res);
61 
62 	/**
63 	 * Get the currently active instance.
64 	 * @return The instance.
65 	 */
66 	static class ScriptInstance *GetActiveInstance();
67 
68 protected:
69 	/**
70 	 * Executes a raw DoCommand for the script.
71 	 */
72 	static bool DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint cmd, const char *text = nullptr, Script_SuspendCallbackProc *callback = nullptr);
73 
74 	/**
75 	 * Store the latest command executed by the script.
76 	 */
77 	static void SetLastCommand(TileIndex tile, uint32 p1, uint32 p2, uint cmd);
78 
79 	/**
80 	 * Check if it's the latest command executed by the script.
81 	 */
82 	static bool CheckLastCommand(TileIndex tile, uint32 p1, uint32 p2, uint cmd);
83 
84 	/**
85 	 * Sets the DoCommand costs counter to a value.
86 	 */
87 	static void SetDoCommandCosts(Money value);
88 
89 	/**
90 	 * Increase the current value of the DoCommand costs counter.
91 	 */
92 	static void IncreaseDoCommandCosts(Money value);
93 
94 	/**
95 	 * Get the current DoCommand costs counter.
96 	 */
97 	static Money GetDoCommandCosts();
98 
99 	/**
100 	 * Set the DoCommand last error.
101 	 */
102 	static void SetLastError(ScriptErrorType last_error);
103 
104 	/**
105 	 * Get the DoCommand last error.
106 	 */
107 	static ScriptErrorType GetLastError();
108 
109 	/**
110 	 * Set the road type.
111 	 */
112 	static void SetRoadType(RoadType road_type);
113 
114 	/**
115 	 * Get the road type.
116 	 */
117 	static RoadType GetRoadType();
118 
119 	/**
120 	 * Set the rail type.
121 	 */
122 	static void SetRailType(RailType rail_type);
123 
124 	/**
125 	 * Get the rail type.
126 	 */
127 	static RailType GetRailType();
128 
129 	/**
130 	 * Set the current mode of your script to this proc.
131 	 */
132 	static void SetDoCommandMode(ScriptModeProc *proc, ScriptObject *instance);
133 
134 	/**
135 	 * Get the current mode your script is currently under.
136 	 */
137 	static ScriptModeProc *GetDoCommandMode();
138 
139 	/**
140 	 * Get the instance of the current mode your script is currently under.
141 	 */
142 	static ScriptObject *GetDoCommandModeInstance();
143 
144 	/**
145 	 * Set the delay of the DoCommand.
146 	 */
147 	static void SetDoCommandDelay(uint ticks);
148 
149 	/**
150 	 * Get the delay of the DoCommand.
151 	 */
152 	static uint GetDoCommandDelay();
153 
154 	/**
155 	 * Get the latest result of a DoCommand.
156 	 */
157 	static bool GetLastCommandRes();
158 
159 	/**
160 	 * Get the latest stored new_vehicle_id.
161 	 */
162 	static VehicleID GetNewVehicleID();
163 
164 	/**
165 	 * Get the latest stored new_sign_id.
166 	 */
167 	static SignID GetNewSignID();
168 
169 	/**
170 	 * Get the latest stored new_group_id.
171 	 */
172 	static GroupID GetNewGroupID();
173 
174 	/**
175 	 * Get the latest stored new_goal_id.
176 	 */
177 	static GoalID GetNewGoalID();
178 
179 	/**
180 	 * Get the latest stored new_story_page_id.
181 	 */
182 	static StoryPageID GetNewStoryPageID();
183 
184 	/**
185 	 * Get the latest stored new_story_page_id.
186 	 */
187 	static StoryPageID GetNewStoryPageElementID();
188 
189 	/**
190 	 * Store a allow_do_command per company.
191 	 * @param allow The new allow.
192 	 */
193 	static void SetAllowDoCommand(bool allow);
194 
195 	/**
196 	 * Get the internal value of allow_do_command. This can differ
197 	 * from CanSuspend() if the reason we are not allowed
198 	 * to execute a DoCommand is in squirrel and not the API.
199 	 * In that case use this function to restore the previous value.
200 	 * @return True iff DoCommands are allowed in the current scope.
201 	 */
202 	static bool GetAllowDoCommand();
203 
204 	/**
205 	 * Set the current company to execute commands for or request
206 	 *  information about.
207 	 * @param company The new company.
208 	 */
209 	static void SetCompany(CompanyID company);
210 
211 	/**
212 	 * Get the current company we are executing commands for or
213 	 *  requesting information about.
214 	 * @return The current company.
215 	 */
216 	static CompanyID GetCompany();
217 
218 	/**
219 	 * Get the root company, the company that the script really
220 	 *  runs under / for.
221 	 * @return The root company.
222 	 */
223 	static CompanyID GetRootCompany();
224 
225 	/**
226 	 * Set the cost of the last command.
227 	 */
228 	static void SetLastCost(Money last_cost);
229 
230 	/**
231 	 * Get the cost of the last command.
232 	 */
233 	static Money GetLastCost();
234 
235 	/**
236 	 * Set a variable that can be used by callback functions to pass information.
237 	 */
238 	static void SetCallbackVariable(int index, int value);
239 
240 	/**
241 	 * Get the variable that is used by callback functions to pass information.
242 	 */
243 	static int GetCallbackVariable(int index);
244 
245 	/**
246 	 * Can we suspend the script at this moment?
247 	 */
248 	static bool CanSuspend();
249 
250 	/**
251 	 * Get the pointer to store event data in.
252 	 */
253 	static void *&GetEventPointer();
254 
255 	/**
256 	 * Get the pointer to store log message in.
257 	 */
258 	static void *&GetLogPointer();
259 
260 	/**
261 	 * Get an allocated string with all control codes stripped off.
262 	 */
263 	static char *GetString(StringID string);
264 
265 private:
266 	/**
267 	 * Store a new_vehicle_id per company.
268 	 * @param vehicle_id The new VehicleID.
269 	 */
270 	static void SetNewVehicleID(VehicleID vehicle_id);
271 
272 	/**
273 	 * Store a new_sign_id per company.
274 	 * @param sign_id The new SignID.
275 	 */
276 	static void SetNewSignID(SignID sign_id);
277 
278 	/**
279 	 * Store a new_group_id per company.
280 	 * @param group_id The new GroupID.
281 	 */
282 	static void SetNewGroupID(GroupID group_id);
283 
284 	/**
285 	 * Store a new_goal_id per company.
286 	 * @param goal_id The new GoalID.
287 	 */
288 	static void SetNewGoalID(GoalID goal_id);
289 
290 	/**
291 	 * Store a new_story_page_id per company.
292 	 * @param story_page_id The new StoryPageID.
293 	 */
294 	static void SetNewStoryPageID(StoryPageID story_page_id);
295 
296 	/**
297 	 * Store a new_story_page_id per company.
298 	 * @param story_page_id The new StoryPageID.
299 	 */
300 	static void SetNewStoryPageElementID(StoryPageElementID story_page_element_id);
301 };
302 
303 #endif /* SCRIPT_OBJECT_HPP */
304