Change: Only resort town directory window on population change if necessary
[openttd-github.git] / src / command_type.h
blobd03639367b504495f8a97d65691e54ada950dfa6
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 */
8 /** @file command_type.h Types related to commands. */
10 #ifndef COMMAND_TYPE_H
11 #define COMMAND_TYPE_H
13 #include "economy_type.h"
14 #include "strings_type.h"
15 #include "tile_type.h"
17 struct GRFFile;
19 /**
20 * Common return value for all commands. Wraps the cost and
21 * a possible error message/state together.
23 class CommandCost {
24 ExpensesType expense_type; ///< the type of expence as shown on the finances view
25 Money cost; ///< The cost of this action
26 StringID message; ///< Warning message for when success is unset
27 bool success; ///< Whether the comment went fine up to this moment
28 const GRFFile *textref_stack_grffile; ///< NewGRF providing the #TextRefStack content.
29 uint textref_stack_size; ///< Number of uint32 values to put on the #TextRefStack for the error message.
31 static uint32 textref_stack[16];
33 public:
34 /**
35 * Creates a command cost return with no cost and no error
37 CommandCost() : expense_type(INVALID_EXPENSES), cost(0), message(INVALID_STRING_ID), success(true), textref_stack_grffile(nullptr), textref_stack_size(0) {}
39 /**
40 * Creates a command return value the is failed with the given message
42 explicit CommandCost(StringID msg) : expense_type(INVALID_EXPENSES), cost(0), message(msg), success(false), textref_stack_grffile(nullptr), textref_stack_size(0) {}
44 /**
45 * Creates a command cost with given expense type and start cost of 0
46 * @param ex_t the expense type
48 explicit CommandCost(ExpensesType ex_t) : expense_type(ex_t), cost(0), message(INVALID_STRING_ID), success(true), textref_stack_grffile(nullptr), textref_stack_size(0) {}
50 /**
51 * Creates a command return value with the given start cost and expense type
52 * @param ex_t the expense type
53 * @param cst the initial cost of this command
55 CommandCost(ExpensesType ex_t, const Money &cst) : expense_type(ex_t), cost(cst), message(INVALID_STRING_ID), success(true), textref_stack_grffile(nullptr), textref_stack_size(0) {}
58 /**
59 * Adds the given cost to the cost of the command.
60 * @param cost the cost to add
62 inline void AddCost(const Money &cost)
64 this->cost += cost;
67 void AddCost(const CommandCost &cmd_cost);
69 /**
70 * Multiplies the cost of the command by the given factor.
71 * @param factor factor to multiply the costs with
73 inline void MultiplyCost(int factor)
75 this->cost *= factor;
78 /**
79 * The costs as made up to this moment
80 * @return the costs
82 inline Money GetCost() const
84 return this->cost;
87 /**
88 * The expense type of the cost
89 * @return the expense type
91 inline ExpensesType GetExpensesType() const
93 return this->expense_type;
96 /**
97 * Makes this #CommandCost behave like an error command.
98 * @param message The error message.
100 void MakeError(StringID message)
102 assert(message != INVALID_STRING_ID);
103 this->success = false;
104 this->message = message;
107 void UseTextRefStack(const GRFFile *grffile, uint num_registers);
110 * Returns the NewGRF providing the #TextRefStack of the error message.
111 * @return the NewGRF.
113 const GRFFile *GetTextRefStackGRF() const
115 return this->textref_stack_grffile;
119 * Returns the number of uint32 values for the #TextRefStack of the error message.
120 * @return number of uint32 values.
122 uint GetTextRefStackSize() const
124 return this->textref_stack_size;
128 * Returns a pointer to the values for the #TextRefStack of the error message.
129 * @return uint32 values for the #TextRefStack
131 const uint32 *GetTextRefStack() const
133 return textref_stack;
137 * Returns the error message of a command
138 * @return the error message, if succeeded #INVALID_STRING_ID
140 StringID GetErrorMessage() const
142 if (this->success) return INVALID_STRING_ID;
143 return this->message;
147 * Did this command succeed?
148 * @return true if and only if it succeeded
150 inline bool Succeeded() const
152 return this->success;
156 * Did this command fail?
157 * @return true if and only if it failed
159 inline bool Failed() const
161 return !this->success;
166 * List of commands.
168 * This enum defines all possible commands which can be executed to the game
169 * engine. Observing the game like the query-tool or checking the profit of a
170 * vehicle don't result in a command which should be executed in the engine
171 * nor send to the server in a network game.
173 * @see _command_proc_table
175 enum Commands {
176 CMD_BUILD_RAILROAD_TRACK, ///< build a rail track
177 CMD_REMOVE_RAILROAD_TRACK, ///< remove a rail track
178 CMD_BUILD_SINGLE_RAIL, ///< build a single rail track
179 CMD_REMOVE_SINGLE_RAIL, ///< remove a single rail track
180 CMD_LANDSCAPE_CLEAR, ///< demolish a tile
181 CMD_BUILD_BRIDGE, ///< build a bridge
182 CMD_BUILD_RAIL_STATION, ///< build a rail station
183 CMD_BUILD_TRAIN_DEPOT, ///< build a train depot
184 CMD_BUILD_SIGNALS, ///< build a signal
185 CMD_REMOVE_SIGNALS, ///< remove a signal
186 CMD_TERRAFORM_LAND, ///< terraform a tile
187 CMD_BUILD_OBJECT, ///< build an object
188 CMD_BUILD_TUNNEL, ///< build a tunnel
190 CMD_REMOVE_FROM_RAIL_STATION, ///< remove a (rectangle of) tiles from a rail station
191 CMD_CONVERT_RAIL, ///< convert a rail type
193 CMD_BUILD_RAIL_WAYPOINT, ///< build a waypoint
194 CMD_RENAME_WAYPOINT, ///< rename a waypoint
195 CMD_REMOVE_FROM_RAIL_WAYPOINT, ///< remove a (rectangle of) tiles from a rail waypoint
197 CMD_BUILD_ROAD_STOP, ///< build a road stop
198 CMD_REMOVE_ROAD_STOP, ///< remove a road stop
199 CMD_BUILD_LONG_ROAD, ///< build a complete road (not a "half" one)
200 CMD_REMOVE_LONG_ROAD, ///< remove a complete road (not a "half" one)
201 CMD_BUILD_ROAD, ///< build a "half" road
202 CMD_BUILD_ROAD_DEPOT, ///< build a road depot
203 CMD_CONVERT_ROAD, ///< convert a road type
205 CMD_BUILD_AIRPORT, ///< build an airport
207 CMD_BUILD_DOCK, ///< build a dock
209 CMD_BUILD_SHIP_DEPOT, ///< build a ship depot
210 CMD_BUILD_BUOY, ///< build a buoy
212 CMD_PLANT_TREE, ///< plant a tree
214 CMD_BUILD_VEHICLE, ///< build a vehicle
215 CMD_SELL_VEHICLE, ///< sell a vehicle
216 CMD_REFIT_VEHICLE, ///< refit the cargo space of a vehicle
217 CMD_SEND_VEHICLE_TO_DEPOT, ///< send a vehicle to a depot
218 CMD_SET_VEHICLE_VISIBILITY, ///< hide or unhide a vehicle in the build vehicle and autoreplace GUIs
220 CMD_MOVE_RAIL_VEHICLE, ///< move a rail vehicle (in the depot)
221 CMD_FORCE_TRAIN_PROCEED, ///< proceed a train to pass a red signal
222 CMD_REVERSE_TRAIN_DIRECTION, ///< turn a train around
224 CMD_CLEAR_ORDER_BACKUP, ///< clear the order backup of a given user/tile
225 CMD_MODIFY_ORDER, ///< modify an order (like set full-load)
226 CMD_SKIP_TO_ORDER, ///< skip an order to the next of specific one
227 CMD_DELETE_ORDER, ///< delete an order
228 CMD_INSERT_ORDER, ///< insert a new order
230 CMD_CHANGE_SERVICE_INT, ///< change the server interval of a vehicle
232 CMD_BUILD_INDUSTRY, ///< build a new industry
234 CMD_SET_COMPANY_MANAGER_FACE, ///< set the manager's face of the company
235 CMD_SET_COMPANY_COLOUR, ///< set the colour of the company
237 CMD_INCREASE_LOAN, ///< increase the loan from the bank
238 CMD_DECREASE_LOAN, ///< decrease the loan from the bank
240 CMD_WANT_ENGINE_PREVIEW, ///< confirm the preview of an engine
242 CMD_RENAME_VEHICLE, ///< rename a whole vehicle
243 CMD_RENAME_ENGINE, ///< rename a engine (in the engine list)
244 CMD_RENAME_COMPANY, ///< change the company name
245 CMD_RENAME_PRESIDENT, ///< change the president name
246 CMD_RENAME_STATION, ///< rename a station
247 CMD_RENAME_DEPOT, ///< rename a depot
249 CMD_PLACE_SIGN, ///< place a sign
250 CMD_RENAME_SIGN, ///< rename a sign
252 CMD_TURN_ROADVEH, ///< turn a road vehicle around
254 CMD_PAUSE, ///< pause the game
256 CMD_BUY_SHARE_IN_COMPANY, ///< buy a share from a company
257 CMD_SELL_SHARE_IN_COMPANY, ///< sell a share from a company
258 CMD_BUY_COMPANY, ///< buy a company which is bankrupt
260 CMD_FOUND_TOWN, ///< found a town
261 CMD_RENAME_TOWN, ///< rename a town
262 CMD_DO_TOWN_ACTION, ///< do a action from the town detail window (like advertises or bribe)
263 CMD_TOWN_CARGO_GOAL, ///< set the goal of a cargo for a town
264 CMD_TOWN_GROWTH_RATE, ///< set the town growth rate
265 CMD_TOWN_RATING, ///< set rating of a company in a town
266 CMD_TOWN_SET_TEXT, ///< set the custom text of a town
267 CMD_EXPAND_TOWN, ///< expand a town
268 CMD_DELETE_TOWN, ///< delete a town
270 CMD_ORDER_REFIT, ///< change the refit information of an order (for "goto depot" )
271 CMD_CLONE_ORDER, ///< clone (and share) an order
272 CMD_CLEAR_AREA, ///< clear an area
274 CMD_MONEY_CHEAT, ///< do the money cheat
275 CMD_CHANGE_BANK_BALANCE, ///< change bank balance to charge costs or give money from a GS
276 CMD_BUILD_CANAL, ///< build a canal
278 CMD_CREATE_SUBSIDY, ///< create a new subsidy
279 CMD_COMPANY_CTRL, ///< used in multiplayer to create a new companies etc.
280 CMD_CUSTOM_NEWS_ITEM, ///< create a custom news message
281 CMD_CREATE_GOAL, ///< create a new goal
282 CMD_REMOVE_GOAL, ///< remove a goal
283 CMD_SET_GOAL_TEXT, ///< update goal text of a goal
284 CMD_SET_GOAL_PROGRESS, ///< update goal progress text of a goal
285 CMD_SET_GOAL_COMPLETED, ///< update goal completed status of a goal
286 CMD_GOAL_QUESTION, ///< ask a goal related question
287 CMD_GOAL_QUESTION_ANSWER, ///< answer(s) to CMD_GOAL_QUESTION
288 CMD_CREATE_STORY_PAGE, ///< create a new story page
289 CMD_CREATE_STORY_PAGE_ELEMENT, ///< create a new story page element
290 CMD_UPDATE_STORY_PAGE_ELEMENT, ///< update a story page element
291 CMD_SET_STORY_PAGE_TITLE, ///< update title of a story page
292 CMD_SET_STORY_PAGE_DATE, ///< update date of a story page
293 CMD_SHOW_STORY_PAGE, ///< show a story page
294 CMD_REMOVE_STORY_PAGE, ///< remove a story page
295 CMD_REMOVE_STORY_PAGE_ELEMENT, ///< remove a story page element
296 CMD_SCROLL_VIEWPORT, ///< scroll main viewport of players
298 CMD_LEVEL_LAND, ///< level land
300 CMD_BUILD_LOCK, ///< build a lock
302 CMD_BUILD_SIGNAL_TRACK, ///< add signals along a track (by dragging)
303 CMD_REMOVE_SIGNAL_TRACK, ///< remove signals along a track (by dragging)
305 CMD_GIVE_MONEY, ///< give money to another company
306 CMD_CHANGE_SETTING, ///< change a setting
307 CMD_CHANGE_COMPANY_SETTING, ///< change a company setting
309 CMD_SET_AUTOREPLACE, ///< set an autoreplace entry
311 CMD_CLONE_VEHICLE, ///< clone a vehicle
312 CMD_START_STOP_VEHICLE, ///< start or stop a vehicle
313 CMD_MASS_START_STOP, ///< start/stop all vehicles (in a depot)
314 CMD_AUTOREPLACE_VEHICLE, ///< replace/renew a vehicle while it is in a depot
315 CMD_DEPOT_SELL_ALL_VEHICLES, ///< sell all vehicles which are in a given depot
316 CMD_DEPOT_MASS_AUTOREPLACE, ///< force the autoreplace to take action in a given depot
318 CMD_CREATE_GROUP, ///< create a new group
319 CMD_DELETE_GROUP, ///< delete a group
320 CMD_ALTER_GROUP, ///< alter a group
321 CMD_ADD_VEHICLE_GROUP, ///< add a vehicle to a group
322 CMD_ADD_SHARED_VEHICLE_GROUP, ///< add all other shared vehicles to a group which are missing
323 CMD_REMOVE_ALL_VEHICLES_GROUP, ///< remove all vehicles from a group
324 CMD_SET_GROUP_REPLACE_PROTECTION, ///< set the autoreplace-protection for a group
325 CMD_SET_GROUP_LIVERY, ///< set the livery for a group
327 CMD_MOVE_ORDER, ///< move an order
328 CMD_CHANGE_TIMETABLE, ///< change the timetable for a vehicle
329 CMD_SET_VEHICLE_ON_TIME, ///< set the vehicle on time feature (timetable)
330 CMD_AUTOFILL_TIMETABLE, ///< autofill the timetable
331 CMD_SET_TIMETABLE_START, ///< set the date that a timetable should start
333 CMD_OPEN_CLOSE_AIRPORT, ///< open/close an airport to incoming aircraft
335 CMD_END, ///< Must ALWAYS be on the end of this list!! (period)
339 * List of flags for a command.
341 * This enums defines some flags which can be used for the commands.
343 enum DoCommandFlag {
344 DC_NONE = 0x000, ///< no flag is set
345 DC_EXEC = 0x001, ///< execute the given command
346 DC_AUTO = 0x002, ///< don't allow building on structures
347 DC_QUERY_COST = 0x004, ///< query cost only, don't build.
348 DC_NO_WATER = 0x008, ///< don't allow building on water
349 DC_NO_RAIL_OVERLAP = 0x010, ///< don't allow overlap of rails (used in buildrail)
350 DC_NO_TEST_TOWN_RATING = 0x020, ///< town rating does not disallow you from building
351 DC_BANKRUPT = 0x040, ///< company bankrupts, skip money check, skip vehicle on tile check in some cases
352 DC_AUTOREPLACE = 0x080, ///< autoreplace/autorenew is in progress, this shall disable vehicle limits when building, and ignore certain restrictions when undoing things (like vehicle attach callback)
353 DC_NO_CARGO_CAP_CHECK = 0x100, ///< when autoreplace/autorenew is in progress, this shall prevent truncating the amount of cargo in the vehicle to prevent testing the command to remove cargo
354 DC_ALL_TILES = 0x200, ///< allow this command also on MP_VOID tiles
355 DC_NO_MODIFY_TOWN_RATING = 0x400, ///< do not change town rating
356 DC_FORCE_CLEAR_TILE = 0x800, ///< do not only remove the object on the tile, but also clear any water left on it
358 DECLARE_ENUM_AS_BIT_SET(DoCommandFlag)
361 * Used to combine a StringID with the command.
363 * This macro can be used to add a StringID (the error message to show) on a command-id
364 * (CMD_xxx). Use the binary or-operator "|" to combine the command with the result from
365 * this macro.
367 * @param x The StringID to combine with a command-id
369 #define CMD_MSG(x) ((x) << 16)
372 * Defines some flags.
374 * This enumeration defines some flags which are binary-or'ed on a command.
376 enum FlaggedCommands {
377 CMD_NETWORK_COMMAND = 0x0100, ///< execute the command without sending it on the network
378 CMD_FLAGS_MASK = 0xFF00, ///< mask for all command flags
379 CMD_ID_MASK = 0x00FF, ///< mask for the command ID
383 * Command flags for the command table _command_proc_table.
385 * This enumeration defines flags for the _command_proc_table.
387 enum CommandFlags {
388 CMD_SERVER = 0x001, ///< the command can only be initiated by the server
389 CMD_SPECTATOR = 0x002, ///< the command may be initiated by a spectator
390 CMD_OFFLINE = 0x004, ///< the command cannot be executed in a multiplayer game; single-player only
391 CMD_AUTO = 0x008, ///< set the DC_AUTO flag on this command
392 CMD_ALL_TILES = 0x010, ///< allow this command also on MP_VOID tiles
393 CMD_NO_TEST = 0x020, ///< the command's output may differ between test and execute due to town rating changes etc.
394 CMD_NO_WATER = 0x040, ///< set the DC_NO_WATER flag on this command
395 CMD_CLIENT_ID = 0x080, ///< set p2 with the ClientID of the sending client.
396 CMD_DEITY = 0x100, ///< the command may be executed by COMPANY_DEITY
397 CMD_STR_CTRL = 0x200, ///< the command's string may contain control strings
398 CMD_NO_EST = 0x400, ///< the command is never estimated.
400 DECLARE_ENUM_AS_BIT_SET(CommandFlags)
402 /** Types of commands we have. */
403 enum CommandType {
404 CMDT_LANDSCAPE_CONSTRUCTION, ///< Construction and destruction of objects on the map.
405 CMDT_VEHICLE_CONSTRUCTION, ///< Construction, modification (incl. refit) and destruction of vehicles.
406 CMDT_MONEY_MANAGEMENT, ///< Management of money, i.e. loans and shares.
407 CMDT_VEHICLE_MANAGEMENT, ///< Stopping, starting, sending to depot, turning around, replace orders etc.
408 CMDT_ROUTE_MANAGEMENT, ///< Modifications to route management (orders, groups, etc).
409 CMDT_OTHER_MANAGEMENT, ///< Renaming stuff, changing company colours, placing signs, etc.
410 CMDT_COMPANY_SETTING, ///< Changing settings related to a company.
411 CMDT_SERVER_SETTING, ///< Pausing/removing companies/server settings.
412 CMDT_CHEAT, ///< A cheat of some sorts.
414 CMDT_END, ///< Magic end marker.
417 /** Different command pause levels. */
418 enum CommandPauseLevel {
419 CMDPL_NO_ACTIONS, ///< No user actions may be executed.
420 CMDPL_NO_CONSTRUCTION, ///< No construction actions may be executed.
421 CMDPL_NO_LANDSCAPING, ///< No landscaping actions may be executed.
422 CMDPL_ALL_ACTIONS, ///< All actions may be executed.
426 * Defines the callback type for all command handler functions.
428 * This type defines the function header for all functions which handles a CMD_* command.
429 * A command handler use the parameters to act according to the meaning of the command.
430 * The tile parameter defines the tile to perform an action on.
431 * The flag parameter is filled with flags from the DC_* enumeration. The parameters
432 * p1 and p2 are filled with parameters for the command like "which road type", "which
433 * order" or "direction". Each function should mentioned in there doxygen comments
434 * the usage of these parameters.
436 * @param tile The tile to apply a command on
437 * @param flags Flags for the command, from the DC_* enumeration
438 * @param p1 Additional data for the command
439 * @param p2 Additional data for the command
440 * @param text Additional text
441 * @return The CommandCost of the command, which can be succeeded or failed.
443 typedef CommandCost CommandProc(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text);
446 * Define a command with the flags which belongs to it.
448 * This struct connect a command handler function with the flags created with
449 * the #CMD_AUTO, #CMD_OFFLINE and #CMD_SERVER values.
451 struct Command {
452 CommandProc *proc; ///< The procedure to actually executing
453 const char *name; ///< A human readable name for the procedure
454 CommandFlags flags; ///< The (command) flags to that apply to this command
455 CommandType type; ///< The type of command.
459 * Define a callback function for the client, after the command is finished.
461 * Functions of this type are called after the command is finished. The parameters
462 * are from the #CommandProc callback type. The boolean parameter indicates if the
463 * command succeeded or failed.
465 * @param result The result of the executed command
466 * @param tile The tile of the command action
467 * @param p1 Additional data of the command
468 * @param p1 Additional data of the command
469 * @see CommandProc
471 typedef void CommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd);
474 * Structure for buffering the build command when selecting a station to join.
476 struct CommandContainer {
477 TileIndex tile; ///< tile command being executed on.
478 uint32 p1; ///< parameter p1.
479 uint32 p2; ///< parameter p2.
480 uint32 cmd; ///< command being executed.
481 CommandCallback *callback; ///< any callback function executed upon successful completion of the command.
482 char text[32 * MAX_CHAR_LENGTH]; ///< possible text sent for name changes etc, in bytes including '\0'.
485 #endif /* COMMAND_TYPE_H */