Rearrange storage of reserved tracks for railway tiles
[openttd/fttd.git] / src / command.cpp
blobfe44dabde84e86b966b28ce18cc56ba1c40a80fa
1 /* $Id$ */
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 command.cpp Handling of commands. */
12 #include "stdafx.h"
13 #include "landscape.h"
14 #include "error.h"
15 #include "gui.h"
16 #include "command_func.h"
17 #include "network/network_type.h"
18 #include "network/network.h"
19 #include "genworld.h"
20 #include "strings_func.h"
21 #include "texteff.hpp"
22 #include "town.h"
23 #include "date_func.h"
24 #include "company_func.h"
25 #include "company_base.h"
26 #include "signal_func.h"
27 #include "core/backup_type.hpp"
28 #include "object_base.h"
30 #include "table/strings.h"
32 CommandProc CmdBuildRailroadTrack;
33 CommandProc CmdRemoveRailroadTrack;
34 CommandProc CmdBuildSingleRail;
35 CommandProc CmdRemoveSingleRail;
37 CommandProc CmdLandscapeClear;
39 CommandProc CmdBuildBridge;
41 CommandProc CmdBuildRailStation;
42 CommandProc CmdRemoveFromRailStation;
43 CommandProc CmdConvertRail;
45 CommandProc CmdBuildSingleSignal;
46 CommandProc CmdRemoveSingleSignal;
48 CommandProc CmdTerraformLand;
50 CommandProc CmdBuildObject;
51 CommandProc CmdSellLandArea;
53 CommandProc CmdBuildTunnel;
55 CommandProc CmdBuildTrainDepot;
56 CommandProc CmdBuildRailWaypoint;
57 CommandProc CmdRenameWaypoint;
58 CommandProc CmdRemoveFromRailWaypoint;
60 CommandProc CmdBuildRoadStop;
61 CommandProc CmdRemoveRoadStop;
63 CommandProc CmdBuildLongRoad;
64 CommandProc CmdRemoveLongRoad;
65 CommandProc CmdBuildRoad;
67 CommandProc CmdBuildRoadDepot;
69 CommandProc CmdBuildAirport;
71 CommandProc CmdBuildDock;
73 CommandProc CmdBuildShipDepot;
75 CommandProc CmdBuildBuoy;
77 CommandProc CmdPlantTree;
79 CommandProc CmdMoveRailVehicle;
81 CommandProc CmdBuildVehicle;
82 CommandProc CmdSellVehicle;
83 CommandProc CmdRefitVehicle;
84 CommandProc CmdSendVehicleToDepot;
86 CommandProc CmdForceTrainProceed;
87 CommandProc CmdReverseTrainDirection;
89 CommandProc CmdClearOrderBackup;
90 CommandProc CmdModifyOrder;
91 CommandProc CmdSkipToOrder;
92 CommandProc CmdDeleteOrder;
93 CommandProc CmdInsertOrder;
94 CommandProc CmdChangeServiceInt;
96 CommandProc CmdBuildIndustry;
98 CommandProc CmdSetCompanyManagerFace;
99 CommandProc CmdSetCompanyColour;
101 CommandProc CmdIncreaseLoan;
102 CommandProc CmdDecreaseLoan;
104 CommandProc CmdWantEnginePreview;
106 CommandProc CmdRenameVehicle;
107 CommandProc CmdRenameEngine;
109 CommandProc CmdRenameCompany;
110 CommandProc CmdRenamePresident;
112 CommandProc CmdRenameStation;
113 CommandProc CmdRenameDepot;
115 CommandProc CmdPlaceSign;
116 CommandProc CmdRenameSign;
118 CommandProc CmdTurnRoadVeh;
120 CommandProc CmdPause;
122 CommandProc CmdBuyShareInCompany;
123 CommandProc CmdSellShareInCompany;
124 CommandProc CmdBuyCompany;
126 CommandProc CmdFoundTown;
127 CommandProc CmdRenameTown;
128 CommandProc CmdDoTownAction;
129 CommandProc CmdTownGrowthRate;
130 CommandProc CmdTownCargoGoal;
131 CommandProc CmdTownSetText;
132 CommandProc CmdExpandTown;
133 CommandProc CmdDeleteTown;
135 CommandProc CmdChangeSetting;
136 CommandProc CmdChangeCompanySetting;
138 CommandProc CmdOrderRefit;
139 CommandProc CmdCloneOrder;
141 CommandProc CmdClearArea;
143 CommandProc CmdGiveMoney;
144 CommandProc CmdMoneyCheat;
145 CommandProc CmdChangeBankBalance;
146 CommandProc CmdBuildCanal;
147 CommandProc CmdBuildLock;
149 CommandProc CmdCreateSubsidy;
150 CommandProc CmdCompanyCtrl;
151 CommandProc CmdCustomNewsItem;
152 CommandProc CmdCreateGoal;
153 CommandProc CmdRemoveGoal;
154 CommandProc CmdSetGoalText;
155 CommandProc CmdSetGoalProgress;
156 CommandProc CmdSetGoalCompleted;
157 CommandProc CmdGoalQuestion;
158 CommandProc CmdGoalQuestionAnswer;
159 CommandProc CmdCreateStoryPage;
160 CommandProc CmdCreateStoryPageElement;
161 CommandProc CmdUpdateStoryPageElement;
162 CommandProc CmdSetStoryPageTitle;
163 CommandProc CmdShowStoryPage;
164 CommandProc CmdRemoveStoryPage;
166 CommandProc CmdLevelLand;
168 CommandProc CmdBuildSignalTrack;
169 CommandProc CmdRemoveSignalTrack;
171 CommandProc CmdSetAutoReplace;
173 CommandProc CmdCloneVehicle;
174 CommandProc CmdStartStopVehicle;
175 CommandProc CmdMassStartStopVehicle;
176 CommandProc CmdAutoreplaceVehicle;
177 CommandProc CmdDepotSellAllVehicles;
178 CommandProc CmdDepotMassAutoReplace;
180 CommandProc CmdCreateGroup;
181 CommandProc CmdRenameGroup;
182 CommandProc CmdDeleteGroup;
183 CommandProc CmdAddVehicleGroup;
184 CommandProc CmdAddSharedVehicleGroup;
185 CommandProc CmdRemoveAllVehiclesGroup;
186 CommandProc CmdSetGroupReplaceProtection;
188 CommandProc CmdMoveOrder;
189 CommandProc CmdChangeTimetable;
190 CommandProc CmdSetVehicleOnTime;
191 CommandProc CmdAutofillTimetable;
192 CommandProc CmdSetTimetableStart;
194 CommandProc CmdOpenCloseAirport;
196 #define DEF_CMD(proc, flags, type) {proc, #proc, (CommandFlags)flags, type}
199 * The master command table
201 * This table contains all possible CommandProc functions with
202 * the flags which belongs to it. The indices are the same
203 * as the value from the CMD_* enums.
205 static const Command _command_proc_table[] = {
206 DEF_CMD(CmdBuildRailroadTrack, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_RAILROAD_TRACK
207 DEF_CMD(CmdRemoveRailroadTrack, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_RAILROAD_TRACK
208 DEF_CMD(CmdBuildSingleRail, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_SINGLE_RAIL
209 DEF_CMD(CmdRemoveSingleRail, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_SINGLE_RAIL
210 DEF_CMD(CmdLandscapeClear, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_LANDSCAPE_CLEAR
211 DEF_CMD(CmdBuildBridge, CMD_DEITY | CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_BRIDGE
212 DEF_CMD(CmdBuildRailStation, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_RAIL_STATION
213 DEF_CMD(CmdBuildTrainDepot, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_TRAIN_DEPOT
214 DEF_CMD(CmdBuildSingleSignal, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_SIGNALS
215 DEF_CMD(CmdRemoveSingleSignal, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_SIGNALS
216 DEF_CMD(CmdTerraformLand, CMD_ALL_TILES | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_TERRAFORM_LAND
217 DEF_CMD(CmdBuildObject, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_OBJECT
218 DEF_CMD(CmdBuildTunnel, CMD_DEITY | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_TUNNEL
219 DEF_CMD(CmdRemoveFromRailStation, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_FROM_RAIL_STATION
220 DEF_CMD(CmdConvertRail, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_CONVERT_RAILD
221 DEF_CMD(CmdBuildRailWaypoint, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_RAIL_WAYPOINT
222 DEF_CMD(CmdRenameWaypoint, 0, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_WAYPOINT
223 DEF_CMD(CmdRemoveFromRailWaypoint, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_FROM_RAIL_WAYPOINT
225 DEF_CMD(CmdBuildRoadStop, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_ROAD_STOP
226 DEF_CMD(CmdRemoveRoadStop, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_ROAD_STOP
227 DEF_CMD(CmdBuildLongRoad,CMD_DEITY | CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_LONG_ROAD
228 DEF_CMD(CmdRemoveLongRoad, CMD_NO_TEST | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_LONG_ROAD; towns may disallow removing road bits (as they are connected) in test, but in exec they're removed and thus removing is allowed.
229 DEF_CMD(CmdBuildRoad, CMD_DEITY | CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_ROAD
230 DEF_CMD(CmdBuildRoadDepot, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_ROAD_DEPOT
232 DEF_CMD(CmdBuildAirport, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_AIRPORT
233 DEF_CMD(CmdBuildDock, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_DOCK
234 DEF_CMD(CmdBuildShipDepot, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_SHIP_DEPOT
235 DEF_CMD(CmdBuildBuoy, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_BUOY
236 DEF_CMD(CmdPlantTree, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_PLANT_TREE
238 DEF_CMD(CmdBuildVehicle, CMD_CLIENT_ID, CMDT_VEHICLE_CONSTRUCTION ), // CMD_BUILD_VEHICLE
239 DEF_CMD(CmdSellVehicle, CMD_CLIENT_ID, CMDT_VEHICLE_CONSTRUCTION ), // CMD_SELL_VEHICLE
240 DEF_CMD(CmdRefitVehicle, 0, CMDT_VEHICLE_CONSTRUCTION ), // CMD_REFIT_VEHICLE
241 DEF_CMD(CmdSendVehicleToDepot, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_SEND_VEHICLE_TO_DEPOT
243 DEF_CMD(CmdMoveRailVehicle, 0, CMDT_VEHICLE_CONSTRUCTION ), // CMD_MOVE_RAIL_VEHICLE
244 DEF_CMD(CmdForceTrainProceed, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_FORCE_TRAIN_PROCEED
245 DEF_CMD(CmdReverseTrainDirection, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_REVERSE_TRAIN_DIRECTION
247 DEF_CMD(CmdClearOrderBackup, CMD_CLIENT_ID, CMDT_ROUTE_MANAGEMENT ), // CMD_CLEAR_ORDER_BACKUP
248 DEF_CMD(CmdModifyOrder, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_MODIFY_ORDER
249 DEF_CMD(CmdSkipToOrder, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_SKIP_TO_ORDER
250 DEF_CMD(CmdDeleteOrder, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_DELETE_ORDER
251 DEF_CMD(CmdInsertOrder, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_INSERT_ORDER
253 DEF_CMD(CmdChangeServiceInt, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_CHANGE_SERVICE_INT
255 DEF_CMD(CmdBuildIndustry, CMD_DEITY, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_INDUSTRY
256 DEF_CMD(CmdSetCompanyManagerFace, 0, CMDT_OTHER_MANAGEMENT ), // CMD_SET_COMPANY_MANAGER_FACE
257 DEF_CMD(CmdSetCompanyColour, 0, CMDT_OTHER_MANAGEMENT ), // CMD_SET_COMPANY_COLOUR
259 DEF_CMD(CmdIncreaseLoan, 0, CMDT_MONEY_MANAGEMENT ), // CMD_INCREASE_LOAN
260 DEF_CMD(CmdDecreaseLoan, 0, CMDT_MONEY_MANAGEMENT ), // CMD_DECREASE_LOAN
262 DEF_CMD(CmdWantEnginePreview, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_WANT_ENGINE_PREVIEW
264 DEF_CMD(CmdRenameVehicle, 0, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_VEHICLE
265 DEF_CMD(CmdRenameEngine, CMD_SERVER, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_ENGINE
267 DEF_CMD(CmdRenameCompany, 0, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_COMPANY
268 DEF_CMD(CmdRenamePresident, 0, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_PRESIDENT
270 DEF_CMD(CmdRenameStation, 0, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_STATION
271 DEF_CMD(CmdRenameDepot, 0, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_DEPOT
273 DEF_CMD(CmdPlaceSign, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_PLACE_SIGN
274 DEF_CMD(CmdRenameSign, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_SIGN
276 DEF_CMD(CmdTurnRoadVeh, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_TURN_ROADVEH
278 DEF_CMD(CmdPause, CMD_SERVER, CMDT_SERVER_SETTING ), // CMD_PAUSE
280 DEF_CMD(CmdBuyShareInCompany, 0, CMDT_MONEY_MANAGEMENT ), // CMD_BUY_SHARE_IN_COMPANY
281 DEF_CMD(CmdSellShareInCompany, 0, CMDT_MONEY_MANAGEMENT ), // CMD_SELL_SHARE_IN_COMPANY
282 DEF_CMD(CmdBuyCompany, 0, CMDT_MONEY_MANAGEMENT ), // CMD_BUY_COMANY
284 DEF_CMD(CmdFoundTown, CMD_DEITY | CMD_NO_TEST, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_FOUND_TOWN; founding random town can fail only in exec run
285 DEF_CMD(CmdRenameTown, CMD_DEITY | CMD_SERVER, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_TOWN
286 DEF_CMD(CmdDoTownAction, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_DO_TOWN_ACTION
287 DEF_CMD(CmdTownCargoGoal, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_TOWN_CARGO_GOAL
288 DEF_CMD(CmdTownGrowthRate, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_TOWN_GROWTH_RATE
289 DEF_CMD(CmdTownSetText, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_TOWN_SET_TEXT
290 DEF_CMD(CmdExpandTown, CMD_DEITY, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_EXPAND_TOWN
291 DEF_CMD(CmdDeleteTown, CMD_OFFLINE, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_DELETE_TOWN
293 DEF_CMD(CmdOrderRefit, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_ORDER_REFIT
294 DEF_CMD(CmdCloneOrder, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_CLONE_ORDER
296 DEF_CMD(CmdClearArea, CMD_NO_TEST, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_CLEAR_AREA; destroying multi-tile houses makes town rating differ between test and execution
298 DEF_CMD(CmdMoneyCheat, CMD_OFFLINE, CMDT_CHEAT ), // CMD_MONEY_CHEAT
299 DEF_CMD(CmdChangeBankBalance, CMD_DEITY, CMDT_MONEY_MANAGEMENT ), // CMD_CHANGE_BANK_BALANCE
300 DEF_CMD(CmdBuildCanal, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_CANAL
301 DEF_CMD(CmdCreateSubsidy, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CREATE_SUBSIDY
302 DEF_CMD(CmdCompanyCtrl, CMD_SPECTATOR | CMD_CLIENT_ID, CMDT_SERVER_SETTING ), // CMD_COMPANY_CTRL
303 DEF_CMD(CmdCustomNewsItem, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CUSTOM_NEWS_ITEM
304 DEF_CMD(CmdCreateGoal, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CREATE_GOAL
305 DEF_CMD(CmdRemoveGoal, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_REMOVE_GOAL
306 DEF_CMD(CmdSetGoalText, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_SET_GOAL_TEXT
307 DEF_CMD(CmdSetGoalProgress, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_SET_GOAL_PROGRESS
308 DEF_CMD(CmdSetGoalCompleted, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_SET_GOAL_COMPLETED
309 DEF_CMD(CmdGoalQuestion, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_GOAL_QUESTION
310 DEF_CMD(CmdGoalQuestionAnswer, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_GOAL_QUESTION_ANSWER
311 DEF_CMD(CmdCreateStoryPage, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CREATE_STORY_PAGE
312 DEF_CMD(CmdCreateStoryPageElement, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CREATE_STORY_PAGE_ELEMENT
313 DEF_CMD(CmdUpdateStoryPageElement, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_UPDATE_STORY_PAGE_ELEMENT
314 DEF_CMD(CmdSetStoryPageTitle, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_SET_STORY_PAGE_TITLE
315 DEF_CMD(CmdShowStoryPage, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_SHOW_STORY_PAGE
316 DEF_CMD(CmdRemoveStoryPage, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_REMOVE_STORY_PAGE
318 DEF_CMD(CmdLevelLand, CMD_ALL_TILES | CMD_NO_TEST | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_LEVEL_LAND; test run might clear tiles multiple times, in execution that only happens once
320 DEF_CMD(CmdBuildLock, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_LOCK
322 DEF_CMD(CmdBuildSignalTrack, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_SIGNAL_TRACK
323 DEF_CMD(CmdRemoveSignalTrack, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_SIGNAL_TRACK
325 DEF_CMD(CmdGiveMoney, 0, CMDT_MONEY_MANAGEMENT ), // CMD_GIVE_MONEY
326 DEF_CMD(CmdChangeSetting, CMD_SERVER, CMDT_SERVER_SETTING ), // CMD_CHANGE_SETTING
327 DEF_CMD(CmdChangeCompanySetting, 0, CMDT_COMPANY_SETTING ), // CMD_CHANGE_COMPANY_SETTING
328 DEF_CMD(CmdSetAutoReplace, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_SET_AUTOREPLACE
329 DEF_CMD(CmdCloneVehicle, CMD_NO_TEST, CMDT_VEHICLE_CONSTRUCTION ), // CMD_CLONE_VEHICLE; NewGRF callbacks influence building and refitting making it impossible to correctly estimate the cost
330 DEF_CMD(CmdStartStopVehicle, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_START_STOP_VEHICLE
331 DEF_CMD(CmdMassStartStopVehicle, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_MASS_START_STOP
332 DEF_CMD(CmdAutoreplaceVehicle, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_AUTOREPLACE_VEHICLE
333 DEF_CMD(CmdDepotSellAllVehicles, 0, CMDT_VEHICLE_CONSTRUCTION ), // CMD_DEPOT_SELL_ALL_VEHICLES
334 DEF_CMD(CmdDepotMassAutoReplace, 0, CMDT_VEHICLE_CONSTRUCTION ), // CMD_DEPOT_MASS_AUTOREPLACE
335 DEF_CMD(CmdCreateGroup, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_CREATE_GROUP
336 DEF_CMD(CmdDeleteGroup, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_DELETE_GROUP
337 DEF_CMD(CmdRenameGroup, 0, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_GROUP
338 DEF_CMD(CmdAddVehicleGroup, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_ADD_VEHICLE_GROUP
339 DEF_CMD(CmdAddSharedVehicleGroup, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_ADD_SHARE_VEHICLE_GROUP
340 DEF_CMD(CmdRemoveAllVehiclesGroup, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_REMOVE_ALL_VEHICLES_GROUP
341 DEF_CMD(CmdSetGroupReplaceProtection, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_SET_GROUP_REPLACE_PROTECTION
342 DEF_CMD(CmdMoveOrder, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_MOVE_ORDER
343 DEF_CMD(CmdChangeTimetable, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_CHANGE_TIMETABLE
344 DEF_CMD(CmdSetVehicleOnTime, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_SET_VEHICLE_ON_TIME
345 DEF_CMD(CmdAutofillTimetable, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_AUTOFILL_TIMETABLE
346 DEF_CMD(CmdSetTimetableStart, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_SET_TIMETABLE_START
348 DEF_CMD(CmdOpenCloseAirport, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_OPEN_CLOSE_AIRPORT
352 * This function range-checks a cmd, and checks if the cmd is not NULL
354 * @param cmd The integer value of a command
355 * @return true if the command is valid (and got a CommandProc function)
357 bool IsValidCommand(uint32 cmd)
359 cmd &= CMD_ID_MASK;
361 return cmd < lengthof(_command_proc_table) && _command_proc_table[cmd].proc != NULL;
365 * This function mask the parameter with CMD_ID_MASK and returns
366 * the flags which belongs to the given command.
368 * @param cmd The integer value of the command
369 * @return The flags for this command
371 CommandFlags GetCommandFlags(uint32 cmd)
373 assert(IsValidCommand(cmd));
375 return _command_proc_table[cmd & CMD_ID_MASK].flags;
379 * This function mask the parameter with CMD_ID_MASK and returns
380 * the name which belongs to the given command.
382 * @param cmd The integer value of the command
383 * @return The name for this command
385 const char *GetCommandName(uint32 cmd)
387 assert(IsValidCommand(cmd));
389 return _command_proc_table[cmd & CMD_ID_MASK].name;
393 * Returns whether the command is allowed while the game is paused.
394 * @param cmd The command to check.
395 * @return True if the command is allowed while paused, false otherwise.
397 bool IsCommandAllowedWhilePaused(uint32 cmd)
399 /* Lookup table for the command types that are allowed for a given pause level setting. */
400 static const int command_type_lookup[] = {
401 CMDPL_ALL_ACTIONS, ///< CMDT_LANDSCAPE_CONSTRUCTION
402 CMDPL_NO_LANDSCAPING, ///< CMDT_VEHICLE_CONSTRUCTION
403 CMDPL_NO_LANDSCAPING, ///< CMDT_MONEY_MANAGEMENT
404 CMDPL_NO_CONSTRUCTION, ///< CMDT_VEHICLE_MANAGEMENT
405 CMDPL_NO_CONSTRUCTION, ///< CMDT_ROUTE_MANAGEMENT
406 CMDPL_NO_CONSTRUCTION, ///< CMDT_OTHER_MANAGEMENT
407 CMDPL_NO_CONSTRUCTION, ///< CMDT_COMPANY_SETTING
408 CMDPL_NO_ACTIONS, ///< CMDT_SERVER_SETTING
409 CMDPL_NO_ACTIONS, ///< CMDT_CHEAT
411 assert_compile(lengthof(command_type_lookup) == CMDT_END);
413 assert(IsValidCommand(cmd));
414 return _game_mode == GM_EDITOR || command_type_lookup[_command_proc_table[cmd & CMD_ID_MASK].type] <= _settings_game.construction.command_pause_level;
418 static int _docommand_recursive = 0;
421 * Shorthand for calling the long DoCommand with a container.
423 * @param container Container with (almost) all information
424 * @param flags Flags for the command and how to execute the command
425 * @see CommandProc
426 * @return the cost
428 CommandCost DoCommand(const CommandContainer *container, DoCommandFlag flags)
430 return DoCommand(container->tile, container->p1, container->p2, flags, container->cmd & CMD_ID_MASK, container->text);
434 * This function executes a given command with the parameters from the #CommandProc parameter list.
435 * Depending on the flags parameter it execute or test a command.
437 * @param tile The tile to apply the command on (for the #CommandProc)
438 * @param p1 Additional data for the command (for the #CommandProc)
439 * @param p2 Additional data for the command (for the #CommandProc)
440 * @param flags Flags for the command and how to execute the command
441 * @param cmd The command-id to execute (a value of the CMD_* enums)
442 * @param text The text to pass
443 * @see CommandProc
444 * @return the cost
446 CommandCost DoCommand(TileIndex tile, uint32 p1, uint32 p2, DoCommandFlag flags, uint32 cmd, const char *text)
448 CommandCost res;
450 /* Do not even think about executing out-of-bounds tile-commands */
451 if (tile != 0 && (tile >= MapSize() || (!IsValidTile(tile) && (flags & DC_ALL_TILES) == 0))) return CMD_ERROR;
453 /* Chop of any CMD_MSG or other flags; we don't need those here */
454 CommandProc *proc = _command_proc_table[cmd & CMD_ID_MASK].proc;
456 _docommand_recursive++;
458 /* only execute the test call if it's toplevel, or we're not execing. */
459 if (_docommand_recursive == 1 || !(flags & DC_EXEC) ) {
460 if (_docommand_recursive == 1) _cleared_object_areas.Clear();
461 SetTownRatingTestMode(true);
462 res = proc(tile, flags & ~DC_EXEC, p1, p2, text);
463 SetTownRatingTestMode(false);
464 if (res.Failed()) {
465 goto error;
468 if (_docommand_recursive == 1 &&
469 !(flags & DC_QUERY_COST) &&
470 !(flags & DC_BANKRUPT) &&
471 !CheckCompanyHasMoney(res)) { // CheckCompanyHasMoney() modifies 'res' to an error if it fails.
472 goto error;
475 if (!(flags & DC_EXEC)) {
476 _docommand_recursive--;
477 return res;
481 /* Execute the command here. All cost-relevant functions set the expenses type
482 * themselves to the cost object at some point */
483 if (_docommand_recursive == 1) _cleared_object_areas.Clear();
484 res = proc(tile, flags, p1, p2, text);
485 if (res.Failed()) {
486 error:
487 _docommand_recursive--;
488 return res;
491 /* if toplevel, subtract the money. */
492 if (--_docommand_recursive == 0 && !(flags & DC_BANKRUPT)) {
493 SubtractMoneyFromCompany(res);
496 return res;
500 * This functions returns the money which can be used to execute a command.
501 * This is either the money of the current company or INT64_MAX if there
502 * is no such a company "at the moment" like the server itself.
504 * @return The available money of a company or INT64_MAX
506 Money GetAvailableMoneyForCommand()
508 CompanyID company = _current_company;
509 if (!Company::IsValidID(company)) return INT64_MAX;
510 return Company::Get(company)->money;
514 * Shortcut for the long DoCommandP when having a container with the data.
515 * @param container the container with information.
516 * @param my_cmd indicator if the command is from a company or server (to display error messages for a user)
517 * @return true if the command succeeded, else false
519 bool DoCommandP(const CommandContainer *container, bool my_cmd)
521 return DoCommandP(container->tile, container->p1, container->p2, container->cmd, container->callback, container->text, my_cmd);
525 * Toplevel network safe docommand function for the current company. Must not be called recursively.
526 * The callback is called when the command succeeded or failed. The parameters
527 * \a tile, \a p1, and \a p2 are from the #CommandProc function. The parameter \a cmd is the command to execute.
528 * The parameter \a my_cmd is used to indicate if the command is from a company or the server.
530 * @param tile The tile to perform a command on (see #CommandProc)
531 * @param p1 Additional data for the command (see #CommandProc)
532 * @param p2 Additional data for the command (see #CommandProc)
533 * @param cmd The command to execute (a CMD_* value)
534 * @param callback A callback function to call after the command is finished
535 * @param text The text to pass
536 * @param my_cmd indicator if the command is from a company or server (to display error messages for a user)
537 * @return \c true if the command succeeded, else \c false.
539 bool DoCommandP(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, bool my_cmd)
541 /* Cost estimation is generally only done when the
542 * local user presses shift while doing somthing.
543 * However, in case of incoming network commands,
544 * map generation or the pause button we do want
545 * to execute. */
546 bool estimate_only = _shift_pressed && IsLocalCompany() &&
547 !_generating_world &&
548 !(cmd & CMD_NETWORK_COMMAND) &&
549 (cmd & CMD_ID_MASK) != CMD_PAUSE;
551 /* We're only sending the command, so don't do
552 * fancy things for 'success'. */
553 bool only_sending = _networking && !(cmd & CMD_NETWORK_COMMAND);
555 /* Where to show the message? */
556 int x = TileX(tile) * TILE_SIZE;
557 int y = TileY(tile) * TILE_SIZE;
559 if (_pause_mode != PM_UNPAUSED && !IsCommandAllowedWhilePaused(cmd)) {
560 ShowErrorMessage(GB(cmd, 16, 16), STR_ERROR_NOT_ALLOWED_WHILE_PAUSED, WL_INFO, x, y);
561 return false;
564 #ifdef ENABLE_NETWORK
565 /* Only set p2 when the command does not come from the network. */
566 if (!(cmd & CMD_NETWORK_COMMAND) && GetCommandFlags(cmd) & CMD_CLIENT_ID && p2 == 0) p2 = CLIENT_ID_SERVER;
567 #endif
569 CommandCost res = DoCommandPInternal(tile, p1, p2, cmd, callback, text, my_cmd, estimate_only);
570 if (res.Failed()) {
571 /* Only show the error when it's for us. */
572 StringID error_part1 = GB(cmd, 16, 16);
573 if (estimate_only || (IsLocalCompany() && error_part1 != 0 && my_cmd)) {
574 ShowErrorMessage(error_part1, res.GetErrorMessage(), WL_INFO, x, y, res.GetTextRefStackSize(), res.GetTextRefStack());
576 } else if (estimate_only) {
577 ShowEstimatedCostOrIncome(res.GetCost(), x, y);
578 } else if (!only_sending && res.GetCost() != 0 && tile != 0 && IsLocalCompany() && _game_mode != GM_EDITOR) {
579 /* Only show the cost animation when we did actually
580 * execute the command, i.e. we're not sending it to
581 * the server, when it has cost the local company
582 * something. Furthermore in the editor there is no
583 * concept of cost, so don't show it there either. */
584 ShowCostOrIncomeAnimation(x, y, GetSlopePixelZ(x, y), res.GetCost());
587 if (!estimate_only && !only_sending && callback != NULL) {
588 callback(res, tile, p1, p2);
591 return res.Succeeded();
596 * Helper to deduplicate the code for returning.
597 * @param cmd the command cost to return.
598 * @param clear whether to keep the storage changes or not.
600 #define return_dcpi(cmd, clear) { _docommand_recursive = 0; ClearPersistentStorageChanges(clear); return cmd; }
603 * Helper function for the toplevel network safe docommand function for the current company.
605 * @param tile The tile to perform a command on (see #CommandProc)
606 * @param p1 Additional data for the command (see #CommandProc)
607 * @param p2 Additional data for the command (see #CommandProc)
608 * @param cmd The command to execute (a CMD_* value)
609 * @param callback A callback function to call after the command is finished
610 * @param text The text to pass
611 * @param my_cmd indicator if the command is from a company or server (to display error messages for a user)
612 * @param estimate_only whether to give only the estimate or also execute the command
613 * @return the command cost of this function.
615 CommandCost DoCommandPInternal(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, bool my_cmd, bool estimate_only)
617 /* Prevent recursion; it gives a mess over the network */
618 assert(_docommand_recursive == 0);
619 _docommand_recursive = 1;
621 /* Reset the state. */
622 _additional_cash_required = 0;
624 /* Get pointer to command handler */
625 byte cmd_id = cmd & CMD_ID_MASK;
626 assert(cmd_id < lengthof(_command_proc_table));
628 CommandProc *proc = _command_proc_table[cmd_id].proc;
629 /* Shouldn't happen, but you never know when someone adds
630 * NULLs to the _command_proc_table. */
631 assert(proc != NULL);
633 /* Command flags are used internally */
634 CommandFlags cmd_flags = GetCommandFlags(cmd);
635 /* Flags get send to the DoCommand */
636 DoCommandFlag flags = CommandFlagsToDCFlags(cmd_flags);
638 #ifdef ENABLE_NETWORK
639 /* Make sure p2 is properly set to a ClientID. */
640 assert(!(cmd_flags & CMD_CLIENT_ID) || p2 != 0);
641 #endif
643 /* Do not even think about executing out-of-bounds tile-commands */
644 if (tile != 0 && (tile >= MapSize() || (!IsValidTile(tile) && (cmd_flags & CMD_ALL_TILES) == 0))) return_dcpi(CMD_ERROR, false);
646 /* Always execute server and spectator commands as spectator */
647 bool exec_as_spectator = (cmd_flags & (CMD_SPECTATOR | CMD_SERVER)) != 0;
649 /* If the company isn't valid it may only do server command or start a new company!
650 * The server will ditch any server commands a client sends to it, so effectively
651 * this guards the server from executing functions for an invalid company. */
652 if (_game_mode == GM_NORMAL && !exec_as_spectator && !Company::IsValidID(_current_company) && !(_current_company == OWNER_DEITY && (cmd_flags & CMD_DEITY) != 0)) {
653 return_dcpi(CMD_ERROR, false);
656 Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
657 if (exec_as_spectator) cur_company.Change(COMPANY_SPECTATOR);
659 bool test_and_exec_can_differ = (cmd_flags & CMD_NO_TEST) != 0;
661 /* Test the command. */
662 _cleared_object_areas.Clear();
663 SetTownRatingTestMode(true);
664 ClearPersistentStorageChanges(false);
665 CommandCost res = proc(tile, flags, p1, p2, text);
666 SetTownRatingTestMode(false);
668 /* Make sure we're not messing things up here. */
669 assert(exec_as_spectator ? _current_company == COMPANY_SPECTATOR : cur_company.Verify());
671 /* If the command fails, we're doing an estimate
672 * or the player does not have enough money
673 * (unless it's a command where the test and
674 * execution phase might return different costs)
675 * we bail out here. */
676 if (res.Failed() || estimate_only ||
677 (!test_and_exec_can_differ && !CheckCompanyHasMoney(res))) {
678 if (!_networking || _generating_world || (cmd & CMD_NETWORK_COMMAND) != 0) {
679 /* Log the failed command as well. Just to be able to be find
680 * causes of desyncs due to bad command test implementations. */
681 DEBUG(desync, 1, "cmdf: %08x; %02x; %02x; %06x; %08x; %08x; %08x; \"%s\" (%s)", _date, _date_fract, (int)_current_company, tile, p1, p2, cmd & ~CMD_NETWORK_COMMAND, text, GetCommandName(cmd));
683 cur_company.Restore();
684 return_dcpi(res, false);
687 #ifdef ENABLE_NETWORK
689 * If we are in network, and the command is not from the network
690 * send it to the command-queue and abort execution
692 if (_networking && !_generating_world && !(cmd & CMD_NETWORK_COMMAND)) {
693 NetworkSendCommand(tile, p1, p2, cmd & ~CMD_FLAGS_MASK, callback, text, _current_company);
694 cur_company.Restore();
696 /* Don't return anything special here; no error, no costs.
697 * This way it's not handled by DoCommand and only the
698 * actual execution of the command causes messages. Also
699 * reset the storages as we've not executed the command. */
700 return_dcpi(CommandCost(), false);
702 #endif /* ENABLE_NETWORK */
703 DEBUG(desync, 1, "cmd: %08x; %02x; %02x; %06x; %08x; %08x; %08x; \"%s\" (%s)", _date, _date_fract, (int)_current_company, tile, p1, p2, cmd & ~CMD_NETWORK_COMMAND, text, GetCommandName(cmd));
705 /* Actually try and execute the command. If no cost-type is given
706 * use the construction one */
707 _cleared_object_areas.Clear();
708 ClearPersistentStorageChanges(false);
709 CommandCost res2 = proc(tile, flags | DC_EXEC, p1, p2, text);
711 if (cmd_id == CMD_COMPANY_CTRL) {
712 cur_company.Trash();
713 /* We are a new company -> Switch to new local company.
714 * We were closed down -> Switch to spectator
715 * Some other company opened/closed down -> The outside function will switch back */
716 _current_company = _local_company;
717 } else {
718 /* Make sure nothing bad happened, like changing the current company. */
719 assert(exec_as_spectator ? _current_company == COMPANY_SPECTATOR : cur_company.Verify());
720 cur_company.Restore();
723 /* If the test and execution can differ we have to check the
724 * return of the command. Otherwise we can check whether the
725 * test and execution have yielded the same result,
726 * i.e. cost and error state are the same. */
727 if (!test_and_exec_can_differ) {
728 assert(res.GetCost() == res2.GetCost() && res.Failed() == res2.Failed()); // sanity check
729 } else if (res2.Failed()) {
730 return_dcpi(res2, false);
733 /* If we're needing more money and we haven't done
734 * anything yet, ask for the money! */
735 if (_additional_cash_required != 0 && res2.GetCost() == 0) {
736 /* It could happen we removed rail, thus gained money, and deleted something else.
737 * So make sure the signal buffer is empty even in this case */
738 UpdateSignalsInBuffer();
739 SetDParam(0, _additional_cash_required);
740 return_dcpi(CommandCost(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY), false);
743 /* update last build coordinate of company. */
744 if (tile != 0) {
745 Company *c = Company::GetIfValid(_current_company);
746 if (c != NULL) c->last_build_coordinate = tile;
749 SubtractMoneyFromCompany(res2);
751 /* update signals if needed */
752 UpdateSignalsInBuffer();
754 return_dcpi(res2, true);
756 #undef return_dcpi
760 * Adds the cost of the given command return value to this cost.
761 * Also takes a possible error message when it is set.
762 * @param ret The command to add the cost of.
764 void CommandCost::AddCost(const CommandCost &ret)
766 this->AddCost(ret.cost);
767 if (this->success && !ret.success) {
768 this->message = ret.message;
769 this->success = false;
774 * Values to put on the #TextRefStack for the error message.
775 * There is only one static instance of the array, just like there is only one
776 * instance of normal DParams.
778 uint32 CommandCost::textref_stack[16];
781 * Activate usage of the NewGRF #TextRefStack for the error message.
782 * @param number of entries to copy from the temporary NewGRF registers
784 void CommandCost::UseTextRefStack(uint num_registers)
786 extern TemporaryStorageArray<int32, 0x110> _temp_store;
788 assert(num_registers < lengthof(textref_stack));
789 this->textref_stack_size = num_registers;
790 for (uint i = 0; i < num_registers; i++) {
791 textref_stack[i] = _temp_store.GetValue(0x100 + i);