Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / script / api / script_object.hpp
blobd96060925d14c924582e9ea38b283923739d925b
1 /* $Id: script_object.hpp 25342 2013-06-09 12:19:09Z zuu $ */
3 /*
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/>.
8 */
10 /** @file script_object.hpp Main object, on which all objects depend. */
12 #ifndef SCRIPT_OBJECT_HPP
13 #define SCRIPT_OBJECT_HPP
15 #include "../../misc/countedptr.hpp"
16 #include "../../road_type.h"
17 #include "../../rail_type.h"
19 #include "script_types.hpp"
20 #include "../script_suspend.hpp"
22 /**
23 * The callback function for Mode-classes.
25 typedef bool (ScriptModeProc)();
27 /**
28 * Uper-parent object of all API classes. You should never use this class in
29 * your script, as it doesn't publish any public functions. It is used
30 * internally to have a common place to handle general things, like internal
31 * command processing, and command-validation checks.
32 * @api none
34 class ScriptObject : public SimpleCountedObject {
35 friend class ScriptInstance;
36 friend class ScriptController;
37 protected:
38 /**
39 * A class that handles the current active instance. By instantiating it at
40 * the beginning of a function with the current active instance, it remains
41 * active till the scope of the variable closes. It then automatically
42 * reverts to the active instance it was before instantiating.
44 class ActiveInstance {
45 friend class ScriptObject;
46 public:
47 ActiveInstance(ScriptInstance *instance);
48 ~ActiveInstance();
49 private:
50 ScriptInstance *last_active; ///< The active instance before we go instantiated.
52 static ScriptInstance *active; ///< The global current active instance.
55 public:
56 /**
57 * Store the latest result of a DoCommand per company.
58 * @param res The result of the last command.
60 static void SetLastCommandRes(bool res);
62 /**
63 * Get the currently active instance.
64 * @return The instance.
66 static class ScriptInstance *GetActiveInstance();
68 protected:
69 /**
70 * Executes a raw DoCommand for the script.
72 static bool DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint cmd, const char *text = NULL, Script_SuspendCallbackProc *callback = NULL);
74 /**
75 * Sets the DoCommand costs counter to a value.
77 static void SetDoCommandCosts(Money value);
79 /**
80 * Increase the current value of the DoCommand costs counter.
82 static void IncreaseDoCommandCosts(Money value);
84 /**
85 * Get the current DoCommand costs counter.
87 static Money GetDoCommandCosts();
89 /**
90 * Set the DoCommand last error.
92 static void SetLastError(ScriptErrorType last_error);
94 /**
95 * Get the DoCommand last error.
97 static ScriptErrorType GetLastError();
99 /**
100 * Set the road type.
102 static void SetRoadType(RoadType road_type);
105 * Get the road type.
107 static RoadType GetRoadType();
110 * Set the rail type.
112 static void SetRailType(RailType rail_type);
115 * Get the rail type.
117 static RailType GetRailType();
120 * Set the current mode of your script to this proc.
122 static void SetDoCommandMode(ScriptModeProc *proc, ScriptObject *instance);
125 * Get the current mode your script is currently under.
127 static ScriptModeProc *GetDoCommandMode();
130 * Get the instance of the current mode your script is currently under.
132 static ScriptObject *GetDoCommandModeInstance();
135 * Set the delay of the DoCommand.
137 static void SetDoCommandDelay(uint ticks);
140 * Get the delay of the DoCommand.
142 static uint GetDoCommandDelay();
145 * Get the latest result of a DoCommand.
147 static bool GetLastCommandRes();
150 * Get the latest stored new_vehicle_id.
152 static VehicleID GetNewVehicleID();
155 * Get the latest stored new_sign_id.
157 static SignID GetNewSignID();
160 * Get the latest stored new_group_id.
162 static GroupID GetNewGroupID();
165 * Get the latest stored new_goal_id.
167 static GoalID GetNewGoalID();
170 * Get the latest stored new_story_page_id.
172 static StoryPageID GetNewStoryPageID();
175 * Get the latest stored new_story_page_id.
177 static StoryPageID GetNewStoryPageElementID();
180 * Store a allow_do_command per company.
181 * @param allow The new allow.
183 static void SetAllowDoCommand(bool allow);
186 * Get the internal value of allow_do_command. This can differ
187 * from CanSuspend() if the reason we are not allowed
188 * to execute a DoCommand is in squirrel and not the API.
189 * In that case use this function to restore the previous value.
190 * @return True iff DoCommands are allowed in the current scope.
192 static bool GetAllowDoCommand();
195 * Set the current company to execute commands for or request
196 * information about.
197 * @param company The new company.
199 static void SetCompany(CompanyID company);
202 * Get the current company we are executing commands for or
203 * requesting information about.
204 * @return The current company.
206 static CompanyID GetCompany();
209 * Get the root company, the company that the script really
210 * runs under / for.
211 * @return The root company.
213 static CompanyID GetRootCompany();
216 * Set the cost of the last command.
218 static void SetLastCost(Money last_cost);
221 * Get the cost of the last command.
223 static Money GetLastCost();
226 * Set a variable that can be used by callback functions to pass information.
228 static void SetCallbackVariable(int index, int value);
231 * Get the variable that is used by callback functions to pass information.
233 static int GetCallbackVariable(int index);
236 * Can we suspend the script at this moment?
238 static bool CanSuspend();
241 * Get the pointer to store event data in.
243 static void *&GetEventPointer();
246 * Get the pointer to store log message in.
248 static void *&GetLogPointer();
251 * Get an allocated string with all control codes stripped off.
253 static char *GetString(StringID string);
255 private:
257 * Store a new_vehicle_id per company.
258 * @param vehicle_id The new VehicleID.
260 static void SetNewVehicleID(VehicleID vehicle_id);
263 * Store a new_sign_id per company.
264 * @param sign_id The new SignID.
266 static void SetNewSignID(SignID sign_id);
269 * Store a new_group_id per company.
270 * @param group_id The new GroupID.
272 static void SetNewGroupID(GroupID group_id);
275 * Store a new_goal_id per company.
276 * @param goal_id The new GoalID.
278 static void SetNewGoalID(GoalID goal_id);
281 * Store a new_story_page_id per company.
282 * @param story_page_id The new StoryPageID.
284 static void SetNewStoryPageID(StoryPageID story_page_id);
287 * Store a new_story_page_id per company.
288 * @param story_page_id The new StoryPageID.
290 static void SetNewStoryPageElementID(StoryPageElementID story_page_element_id);
293 #endif /* SCRIPT_OBJECT_HPP */