Fix: Violation of strict weak ordering in engine name sorter
[openttd-github.git] / src / command.cpp
blob453f305d38480219b2d19d1f456ae8594bf35c2c
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.cpp Handling of commands. */
10 #include "stdafx.h"
11 #include "landscape.h"
12 #include "error.h"
13 #include "gui.h"
14 #include "command_func.h"
15 #include "network/network_type.h"
16 #include "network/network.h"
17 #include "genworld.h"
18 #include "strings_func.h"
19 #include "texteff.hpp"
20 #include "town.h"
21 #include "date_func.h"
22 #include "company_func.h"
23 #include "company_base.h"
24 #include "signal_func.h"
25 #include "core/backup_type.hpp"
26 #include "object_base.h"
28 #include "table/strings.h"
30 #include "safeguards.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 CmdConvertRoad;
71 CommandProc CmdBuildAirport;
73 CommandProc CmdBuildDock;
75 CommandProc CmdBuildShipDepot;
77 CommandProc CmdBuildBuoy;
79 CommandProc CmdPlantTree;
81 CommandProc CmdMoveRailVehicle;
83 CommandProc CmdBuildVehicle;
84 CommandProc CmdSellVehicle;
85 CommandProc CmdRefitVehicle;
86 CommandProc CmdSendVehicleToDepot;
87 CommandProc CmdSetVehicleVisibility;
89 CommandProc CmdForceTrainProceed;
90 CommandProc CmdReverseTrainDirection;
92 CommandProc CmdClearOrderBackup;
93 CommandProc CmdModifyOrder;
94 CommandProc CmdSkipToOrder;
95 CommandProc CmdDeleteOrder;
96 CommandProc CmdInsertOrder;
97 CommandProc CmdChangeServiceInt;
99 CommandProc CmdBuildIndustry;
101 CommandProc CmdSetCompanyManagerFace;
102 CommandProc CmdSetCompanyColour;
104 CommandProc CmdIncreaseLoan;
105 CommandProc CmdDecreaseLoan;
107 CommandProc CmdWantEnginePreview;
108 CommandProc CmdEngineCtrl;
110 CommandProc CmdRenameVehicle;
111 CommandProc CmdRenameEngine;
113 CommandProc CmdRenameCompany;
114 CommandProc CmdRenamePresident;
116 CommandProc CmdRenameStation;
117 CommandProc CmdRenameDepot;
119 CommandProc CmdPlaceSign;
120 CommandProc CmdRenameSign;
122 CommandProc CmdTurnRoadVeh;
124 CommandProc CmdPause;
126 CommandProc CmdBuyShareInCompany;
127 CommandProc CmdSellShareInCompany;
128 CommandProc CmdBuyCompany;
130 CommandProc CmdFoundTown;
131 CommandProc CmdRenameTown;
132 CommandProc CmdDoTownAction;
133 CommandProc CmdTownGrowthRate;
134 CommandProc CmdTownRating;
135 CommandProc CmdTownCargoGoal;
136 CommandProc CmdTownSetText;
137 CommandProc CmdExpandTown;
138 CommandProc CmdDeleteTown;
140 CommandProc CmdChangeSetting;
141 CommandProc CmdChangeCompanySetting;
143 CommandProc CmdOrderRefit;
144 CommandProc CmdCloneOrder;
146 CommandProc CmdClearArea;
148 CommandProc CmdGiveMoney;
149 CommandProc CmdMoneyCheat;
150 CommandProc CmdChangeBankBalance;
151 CommandProc CmdBuildCanal;
152 CommandProc CmdBuildLock;
154 CommandProc CmdCreateSubsidy;
155 CommandProc CmdCompanyCtrl;
156 CommandProc CmdCustomNewsItem;
157 CommandProc CmdCreateGoal;
158 CommandProc CmdRemoveGoal;
159 CommandProc CmdSetGoalText;
160 CommandProc CmdSetGoalProgress;
161 CommandProc CmdSetGoalCompleted;
162 CommandProc CmdGoalQuestion;
163 CommandProc CmdGoalQuestionAnswer;
164 CommandProc CmdCreateStoryPage;
165 CommandProc CmdCreateStoryPageElement;
166 CommandProc CmdUpdateStoryPageElement;
167 CommandProc CmdSetStoryPageTitle;
168 CommandProc CmdSetStoryPageDate;
169 CommandProc CmdShowStoryPage;
170 CommandProc CmdRemoveStoryPage;
171 CommandProc CmdRemoveStoryPageElement;
172 CommandProc CmdScrollViewport;
173 CommandProc CmdStoryPageButton;
175 CommandProc CmdLevelLand;
177 CommandProc CmdBuildSignalTrack;
178 CommandProc CmdRemoveSignalTrack;
180 CommandProc CmdSetAutoReplace;
182 CommandProc CmdCloneVehicle;
183 CommandProc CmdStartStopVehicle;
184 CommandProc CmdMassStartStopVehicle;
185 CommandProc CmdAutoreplaceVehicle;
186 CommandProc CmdDepotSellAllVehicles;
187 CommandProc CmdDepotMassAutoReplace;
189 CommandProc CmdCreateGroup;
190 CommandProc CmdAlterGroup;
191 CommandProc CmdDeleteGroup;
192 CommandProc CmdAddVehicleGroup;
193 CommandProc CmdAddSharedVehicleGroup;
194 CommandProc CmdRemoveAllVehiclesGroup;
195 CommandProc CmdSetGroupReplaceProtection;
196 CommandProc CmdSetGroupLivery;
198 CommandProc CmdMoveOrder;
199 CommandProc CmdChangeTimetable;
200 CommandProc CmdSetVehicleOnTime;
201 CommandProc CmdAutofillTimetable;
202 CommandProc CmdSetTimetableStart;
204 CommandProc CmdOpenCloseAirport;
206 #define DEF_CMD(proc, flags, type) {proc, #proc, (CommandFlags)flags, type}
209 * The master command table
211 * This table contains all possible CommandProc functions with
212 * the flags which belongs to it. The indices are the same
213 * as the value from the CMD_* enums.
215 static const Command _command_proc_table[] = {
216 DEF_CMD(CmdBuildRailroadTrack, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_RAILROAD_TRACK
217 DEF_CMD(CmdRemoveRailroadTrack, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_RAILROAD_TRACK
218 DEF_CMD(CmdBuildSingleRail, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_SINGLE_RAIL
219 DEF_CMD(CmdRemoveSingleRail, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_SINGLE_RAIL
220 DEF_CMD(CmdLandscapeClear, CMD_DEITY, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_LANDSCAPE_CLEAR
221 DEF_CMD(CmdBuildBridge, CMD_DEITY | CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_BRIDGE
222 DEF_CMD(CmdBuildRailStation, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_RAIL_STATION
223 DEF_CMD(CmdBuildTrainDepot, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_TRAIN_DEPOT
224 DEF_CMD(CmdBuildSingleSignal, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_SIGNALS
225 DEF_CMD(CmdRemoveSingleSignal, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_SIGNALS
226 DEF_CMD(CmdTerraformLand, CMD_ALL_TILES | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_TERRAFORM_LAND
227 DEF_CMD(CmdBuildObject, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_OBJECT
228 DEF_CMD(CmdBuildTunnel, CMD_DEITY | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_TUNNEL
229 DEF_CMD(CmdRemoveFromRailStation, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_FROM_RAIL_STATION
230 DEF_CMD(CmdConvertRail, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_CONVERT_RAILD
231 DEF_CMD(CmdBuildRailWaypoint, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_RAIL_WAYPOINT
232 DEF_CMD(CmdRenameWaypoint, 0, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_WAYPOINT
233 DEF_CMD(CmdRemoveFromRailWaypoint, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_FROM_RAIL_WAYPOINT
235 DEF_CMD(CmdBuildRoadStop, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_ROAD_STOP
236 DEF_CMD(CmdRemoveRoadStop, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_ROAD_STOP
237 DEF_CMD(CmdBuildLongRoad,CMD_DEITY | CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_LONG_ROAD
238 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.
239 DEF_CMD(CmdBuildRoad, CMD_DEITY | CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_ROAD
240 DEF_CMD(CmdBuildRoadDepot, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_ROAD_DEPOT
241 DEF_CMD(CmdConvertRoad, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_CONVERT_ROAD
243 DEF_CMD(CmdBuildAirport, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_AIRPORT
244 DEF_CMD(CmdBuildDock, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_DOCK
245 DEF_CMD(CmdBuildShipDepot, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_SHIP_DEPOT
246 DEF_CMD(CmdBuildBuoy, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_BUOY
247 DEF_CMD(CmdPlantTree, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_PLANT_TREE
249 DEF_CMD(CmdBuildVehicle, CMD_CLIENT_ID, CMDT_VEHICLE_CONSTRUCTION ), // CMD_BUILD_VEHICLE
250 DEF_CMD(CmdSellVehicle, CMD_CLIENT_ID, CMDT_VEHICLE_CONSTRUCTION ), // CMD_SELL_VEHICLE
251 DEF_CMD(CmdRefitVehicle, 0, CMDT_VEHICLE_CONSTRUCTION ), // CMD_REFIT_VEHICLE
252 DEF_CMD(CmdSendVehicleToDepot, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_SEND_VEHICLE_TO_DEPOT
253 DEF_CMD(CmdSetVehicleVisibility, 0, CMDT_COMPANY_SETTING ), // CMD_SET_VEHICLE_VISIBILITY
255 DEF_CMD(CmdMoveRailVehicle, 0, CMDT_VEHICLE_CONSTRUCTION ), // CMD_MOVE_RAIL_VEHICLE
256 DEF_CMD(CmdForceTrainProceed, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_FORCE_TRAIN_PROCEED
257 DEF_CMD(CmdReverseTrainDirection, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_REVERSE_TRAIN_DIRECTION
259 DEF_CMD(CmdClearOrderBackup, CMD_CLIENT_ID, CMDT_SERVER_SETTING ), // CMD_CLEAR_ORDER_BACKUP
260 DEF_CMD(CmdModifyOrder, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_MODIFY_ORDER
261 DEF_CMD(CmdSkipToOrder, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_SKIP_TO_ORDER
262 DEF_CMD(CmdDeleteOrder, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_DELETE_ORDER
263 DEF_CMD(CmdInsertOrder, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_INSERT_ORDER
265 DEF_CMD(CmdChangeServiceInt, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_CHANGE_SERVICE_INT
267 DEF_CMD(CmdBuildIndustry, CMD_DEITY, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_INDUSTRY
268 DEF_CMD(CmdSetCompanyManagerFace, 0, CMDT_OTHER_MANAGEMENT ), // CMD_SET_COMPANY_MANAGER_FACE
269 DEF_CMD(CmdSetCompanyColour, 0, CMDT_OTHER_MANAGEMENT ), // CMD_SET_COMPANY_COLOUR
271 DEF_CMD(CmdIncreaseLoan, 0, CMDT_MONEY_MANAGEMENT ), // CMD_INCREASE_LOAN
272 DEF_CMD(CmdDecreaseLoan, 0, CMDT_MONEY_MANAGEMENT ), // CMD_DECREASE_LOAN
274 DEF_CMD(CmdWantEnginePreview, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_WANT_ENGINE_PREVIEW
275 DEF_CMD(CmdEngineCtrl, CMD_DEITY, CMDT_VEHICLE_MANAGEMENT ), // CMD_ENGINE_CTRL
277 DEF_CMD(CmdRenameVehicle, 0, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_VEHICLE
278 DEF_CMD(CmdRenameEngine, CMD_SERVER, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_ENGINE
280 DEF_CMD(CmdRenameCompany, 0, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_COMPANY
281 DEF_CMD(CmdRenamePresident, 0, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_PRESIDENT
283 DEF_CMD(CmdRenameStation, 0, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_STATION
284 DEF_CMD(CmdRenameDepot, 0, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_DEPOT
286 DEF_CMD(CmdPlaceSign, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_PLACE_SIGN
287 DEF_CMD(CmdRenameSign, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_SIGN
289 DEF_CMD(CmdTurnRoadVeh, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_TURN_ROADVEH
291 DEF_CMD(CmdPause, CMD_SERVER | CMD_NO_EST, CMDT_SERVER_SETTING ), // CMD_PAUSE
293 DEF_CMD(CmdBuyShareInCompany, 0, CMDT_MONEY_MANAGEMENT ), // CMD_BUY_SHARE_IN_COMPANY
294 DEF_CMD(CmdSellShareInCompany, 0, CMDT_MONEY_MANAGEMENT ), // CMD_SELL_SHARE_IN_COMPANY
295 DEF_CMD(CmdBuyCompany, 0, CMDT_MONEY_MANAGEMENT ), // CMD_BUY_COMANY
297 DEF_CMD(CmdFoundTown, CMD_DEITY | CMD_NO_TEST, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_FOUND_TOWN; founding random town can fail only in exec run
298 DEF_CMD(CmdRenameTown, CMD_DEITY | CMD_SERVER, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_TOWN
299 DEF_CMD(CmdDoTownAction, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_DO_TOWN_ACTION
300 DEF_CMD(CmdTownCargoGoal, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_TOWN_CARGO_GOAL
301 DEF_CMD(CmdTownGrowthRate, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_TOWN_GROWTH_RATE
302 DEF_CMD(CmdTownRating, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_TOWN_RATING
303 DEF_CMD(CmdTownSetText, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_TOWN_SET_TEXT
304 DEF_CMD(CmdExpandTown, CMD_DEITY, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_EXPAND_TOWN
305 DEF_CMD(CmdDeleteTown, CMD_OFFLINE, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_DELETE_TOWN
307 DEF_CMD(CmdOrderRefit, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_ORDER_REFIT
308 DEF_CMD(CmdCloneOrder, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_CLONE_ORDER
310 DEF_CMD(CmdClearArea, CMD_NO_TEST, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_CLEAR_AREA; destroying multi-tile houses makes town rating differ between test and execution
312 DEF_CMD(CmdMoneyCheat, CMD_OFFLINE, CMDT_CHEAT ), // CMD_MONEY_CHEAT
313 DEF_CMD(CmdChangeBankBalance, CMD_DEITY, CMDT_MONEY_MANAGEMENT ), // CMD_CHANGE_BANK_BALANCE
314 DEF_CMD(CmdBuildCanal, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_CANAL
315 DEF_CMD(CmdCreateSubsidy, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CREATE_SUBSIDY
316 DEF_CMD(CmdCompanyCtrl, CMD_SPECTATOR | CMD_CLIENT_ID | CMD_NO_EST, CMDT_SERVER_SETTING ), // CMD_COMPANY_CTRL
317 DEF_CMD(CmdCustomNewsItem, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CUSTOM_NEWS_ITEM
318 DEF_CMD(CmdCreateGoal, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CREATE_GOAL
319 DEF_CMD(CmdRemoveGoal, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_REMOVE_GOAL
320 DEF_CMD(CmdSetGoalText, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_SET_GOAL_TEXT
321 DEF_CMD(CmdSetGoalProgress, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_SET_GOAL_PROGRESS
322 DEF_CMD(CmdSetGoalCompleted, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_SET_GOAL_COMPLETED
323 DEF_CMD(CmdGoalQuestion, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_GOAL_QUESTION
324 DEF_CMD(CmdGoalQuestionAnswer, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_GOAL_QUESTION_ANSWER
325 DEF_CMD(CmdCreateStoryPage, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CREATE_STORY_PAGE
326 DEF_CMD(CmdCreateStoryPageElement, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CREATE_STORY_PAGE_ELEMENT
327 DEF_CMD(CmdUpdateStoryPageElement, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_UPDATE_STORY_PAGE_ELEMENT
328 DEF_CMD(CmdSetStoryPageTitle, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_SET_STORY_PAGE_TITLE
329 DEF_CMD(CmdSetStoryPageDate, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_SET_STORY_PAGE_DATE
330 DEF_CMD(CmdShowStoryPage, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_SHOW_STORY_PAGE
331 DEF_CMD(CmdRemoveStoryPage, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_REMOVE_STORY_PAGE
332 DEF_CMD(CmdRemoveStoryPageElement, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_REMOVE_STORY_ELEMENT_PAGE
333 DEF_CMD(CmdScrollViewport, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_SCROLL_VIEWPORT
334 DEF_CMD(CmdStoryPageButton, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_STORY_PAGE_BUTTON
336 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
338 DEF_CMD(CmdBuildLock, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_LOCK
340 DEF_CMD(CmdBuildSignalTrack, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_SIGNAL_TRACK
341 DEF_CMD(CmdRemoveSignalTrack, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_SIGNAL_TRACK
343 DEF_CMD(CmdGiveMoney, 0, CMDT_MONEY_MANAGEMENT ), // CMD_GIVE_MONEY
344 DEF_CMD(CmdChangeSetting, CMD_SERVER, CMDT_SERVER_SETTING ), // CMD_CHANGE_SETTING
345 DEF_CMD(CmdChangeCompanySetting, 0, CMDT_COMPANY_SETTING ), // CMD_CHANGE_COMPANY_SETTING
346 DEF_CMD(CmdSetAutoReplace, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_SET_AUTOREPLACE
347 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
348 DEF_CMD(CmdStartStopVehicle, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_START_STOP_VEHICLE
349 DEF_CMD(CmdMassStartStopVehicle, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_MASS_START_STOP
350 DEF_CMD(CmdAutoreplaceVehicle, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_AUTOREPLACE_VEHICLE
351 DEF_CMD(CmdDepotSellAllVehicles, 0, CMDT_VEHICLE_CONSTRUCTION ), // CMD_DEPOT_SELL_ALL_VEHICLES
352 DEF_CMD(CmdDepotMassAutoReplace, 0, CMDT_VEHICLE_CONSTRUCTION ), // CMD_DEPOT_MASS_AUTOREPLACE
353 DEF_CMD(CmdCreateGroup, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_CREATE_GROUP
354 DEF_CMD(CmdDeleteGroup, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_DELETE_GROUP
355 DEF_CMD(CmdAlterGroup, 0, CMDT_OTHER_MANAGEMENT ), // CMD_ALTER_GROUP
356 DEF_CMD(CmdAddVehicleGroup, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_ADD_VEHICLE_GROUP
357 DEF_CMD(CmdAddSharedVehicleGroup, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_ADD_SHARE_VEHICLE_GROUP
358 DEF_CMD(CmdRemoveAllVehiclesGroup, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_REMOVE_ALL_VEHICLES_GROUP
359 DEF_CMD(CmdSetGroupReplaceProtection, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_SET_GROUP_REPLACE_PROTECTION
360 DEF_CMD(CmdSetGroupLivery, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_SET_GROUP_LIVERY
361 DEF_CMD(CmdMoveOrder, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_MOVE_ORDER
362 DEF_CMD(CmdChangeTimetable, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_CHANGE_TIMETABLE
363 DEF_CMD(CmdSetVehicleOnTime, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_SET_VEHICLE_ON_TIME
364 DEF_CMD(CmdAutofillTimetable, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_AUTOFILL_TIMETABLE
365 DEF_CMD(CmdSetTimetableStart, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_SET_TIMETABLE_START
367 DEF_CMD(CmdOpenCloseAirport, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_OPEN_CLOSE_AIRPORT
371 * This function range-checks a cmd, and checks if the cmd is not nullptr
373 * @param cmd The integer value of a command
374 * @return true if the command is valid (and got a CommandProc function)
376 bool IsValidCommand(uint32 cmd)
378 cmd &= CMD_ID_MASK;
380 return cmd < lengthof(_command_proc_table) && _command_proc_table[cmd].proc != nullptr;
384 * This function mask the parameter with CMD_ID_MASK and returns
385 * the flags which belongs to the given command.
387 * @param cmd The integer value of the command
388 * @return The flags for this command
390 CommandFlags GetCommandFlags(uint32 cmd)
392 assert(IsValidCommand(cmd));
394 return _command_proc_table[cmd & CMD_ID_MASK].flags;
398 * This function mask the parameter with CMD_ID_MASK and returns
399 * the name which belongs to the given command.
401 * @param cmd The integer value of the command
402 * @return The name for this command
404 const char *GetCommandName(uint32 cmd)
406 assert(IsValidCommand(cmd));
408 return _command_proc_table[cmd & CMD_ID_MASK].name;
412 * Returns whether the command is allowed while the game is paused.
413 * @param cmd The command to check.
414 * @return True if the command is allowed while paused, false otherwise.
416 bool IsCommandAllowedWhilePaused(uint32 cmd)
418 /* Lookup table for the command types that are allowed for a given pause level setting. */
419 static const int command_type_lookup[] = {
420 CMDPL_ALL_ACTIONS, ///< CMDT_LANDSCAPE_CONSTRUCTION
421 CMDPL_NO_LANDSCAPING, ///< CMDT_VEHICLE_CONSTRUCTION
422 CMDPL_NO_LANDSCAPING, ///< CMDT_MONEY_MANAGEMENT
423 CMDPL_NO_CONSTRUCTION, ///< CMDT_VEHICLE_MANAGEMENT
424 CMDPL_NO_CONSTRUCTION, ///< CMDT_ROUTE_MANAGEMENT
425 CMDPL_NO_CONSTRUCTION, ///< CMDT_OTHER_MANAGEMENT
426 CMDPL_NO_CONSTRUCTION, ///< CMDT_COMPANY_SETTING
427 CMDPL_NO_ACTIONS, ///< CMDT_SERVER_SETTING
428 CMDPL_NO_ACTIONS, ///< CMDT_CHEAT
430 assert_compile(lengthof(command_type_lookup) == CMDT_END);
432 assert(IsValidCommand(cmd));
433 return _game_mode == GM_EDITOR || command_type_lookup[_command_proc_table[cmd & CMD_ID_MASK].type] <= _settings_game.construction.command_pause_level;
437 static int _docommand_recursive = 0;
440 * Shorthand for calling the long DoCommand with a container.
442 * @param container Container with (almost) all information
443 * @param flags Flags for the command and how to execute the command
444 * @see CommandProc
445 * @return the cost
447 CommandCost DoCommand(const CommandContainer *container, DoCommandFlag flags)
449 return DoCommand(container->tile, container->p1, container->p2, flags, container->cmd & CMD_ID_MASK, container->text);
453 * This function executes a given command with the parameters from the #CommandProc parameter list.
454 * Depending on the flags parameter it execute or test a command.
456 * @param tile The tile to apply the command on (for the #CommandProc)
457 * @param p1 Additional data for the command (for the #CommandProc)
458 * @param p2 Additional data for the command (for the #CommandProc)
459 * @param flags Flags for the command and how to execute the command
460 * @param cmd The command-id to execute (a value of the CMD_* enums)
461 * @param text The text to pass
462 * @see CommandProc
463 * @return the cost
465 CommandCost DoCommand(TileIndex tile, uint32 p1, uint32 p2, DoCommandFlag flags, uint32 cmd, const char *text)
467 CommandCost res;
469 /* Do not even think about executing out-of-bounds tile-commands */
470 if (tile != 0 && (tile >= MapSize() || (!IsValidTile(tile) && (flags & DC_ALL_TILES) == 0))) return CMD_ERROR;
472 /* Chop of any CMD_MSG or other flags; we don't need those here */
473 CommandProc *proc = _command_proc_table[cmd & CMD_ID_MASK].proc;
475 _docommand_recursive++;
477 /* only execute the test call if it's toplevel, or we're not execing. */
478 if (_docommand_recursive == 1 || !(flags & DC_EXEC) ) {
479 if (_docommand_recursive == 1) _cleared_object_areas.clear();
480 SetTownRatingTestMode(true);
481 res = proc(tile, flags & ~DC_EXEC, p1, p2, text);
482 SetTownRatingTestMode(false);
483 if (res.Failed()) {
484 goto error;
487 if (_docommand_recursive == 1 &&
488 !(flags & DC_QUERY_COST) &&
489 !(flags & DC_BANKRUPT) &&
490 !CheckCompanyHasMoney(res)) { // CheckCompanyHasMoney() modifies 'res' to an error if it fails.
491 goto error;
494 if (!(flags & DC_EXEC)) {
495 _docommand_recursive--;
496 return res;
500 /* Execute the command here. All cost-relevant functions set the expenses type
501 * themselves to the cost object at some point */
502 if (_docommand_recursive == 1) _cleared_object_areas.clear();
503 res = proc(tile, flags, p1, p2, text);
504 if (res.Failed()) {
505 error:
506 _docommand_recursive--;
507 return res;
510 /* if toplevel, subtract the money. */
511 if (--_docommand_recursive == 0 && !(flags & DC_BANKRUPT)) {
512 SubtractMoneyFromCompany(res);
515 return res;
519 * This functions returns the money which can be used to execute a command.
520 * This is either the money of the current company or INT64_MAX if there
521 * is no such a company "at the moment" like the server itself.
523 * @return The available money of a company or INT64_MAX
525 Money GetAvailableMoneyForCommand()
527 CompanyID company = _current_company;
528 if (!Company::IsValidID(company)) return INT64_MAX;
529 return Company::Get(company)->money;
533 * Shortcut for the long DoCommandP when having a container with the data.
534 * @param container the container with information.
535 * @param my_cmd indicator if the command is from a company or server (to display error messages for a user)
536 * @return true if the command succeeded, else false
538 bool DoCommandP(const CommandContainer *container, bool my_cmd)
540 return DoCommandP(container->tile, container->p1, container->p2, container->cmd, container->callback, container->text, my_cmd);
544 * Toplevel network safe docommand function for the current company. Must not be called recursively.
545 * The callback is called when the command succeeded or failed. The parameters
546 * \a tile, \a p1, and \a p2 are from the #CommandProc function. The parameter \a cmd is the command to execute.
547 * The parameter \a my_cmd is used to indicate if the command is from a company or the server.
549 * @param tile The tile to perform a command on (see #CommandProc)
550 * @param p1 Additional data for the command (see #CommandProc)
551 * @param p2 Additional data for the command (see #CommandProc)
552 * @param cmd The command to execute (a CMD_* value)
553 * @param callback A callback function to call after the command is finished
554 * @param text The text to pass
555 * @param my_cmd indicator if the command is from a company or server (to display error messages for a user)
556 * @return \c true if the command succeeded, else \c false.
558 bool DoCommandP(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, bool my_cmd)
560 /* Cost estimation is generally only done when the
561 * local user presses shift while doing something.
562 * However, in case of incoming network commands,
563 * map generation or the pause button we do want
564 * to execute. */
565 bool estimate_only = _shift_pressed && IsLocalCompany() &&
566 !_generating_world &&
567 !(cmd & CMD_NETWORK_COMMAND) &&
568 !(GetCommandFlags(cmd) & CMD_NO_EST);
570 /* We're only sending the command, so don't do
571 * fancy things for 'success'. */
572 bool only_sending = _networking && !(cmd & CMD_NETWORK_COMMAND);
574 /* Where to show the message? */
575 int x = TileX(tile) * TILE_SIZE;
576 int y = TileY(tile) * TILE_SIZE;
578 if (_pause_mode != PM_UNPAUSED && !IsCommandAllowedWhilePaused(cmd) && !estimate_only) {
579 ShowErrorMessage(GB(cmd, 16, 16), STR_ERROR_NOT_ALLOWED_WHILE_PAUSED, WL_INFO, x, y);
580 return false;
583 /* Only set p2 when the command does not come from the network. */
584 if (!(cmd & CMD_NETWORK_COMMAND) && GetCommandFlags(cmd) & CMD_CLIENT_ID && p2 == 0) p2 = CLIENT_ID_SERVER;
586 CommandCost res = DoCommandPInternal(tile, p1, p2, cmd, callback, text, my_cmd, estimate_only);
587 if (res.Failed()) {
588 /* Only show the error when it's for us. */
589 StringID error_part1 = GB(cmd, 16, 16);
590 if (estimate_only || (IsLocalCompany() && error_part1 != 0 && my_cmd)) {
591 ShowErrorMessage(error_part1, res.GetErrorMessage(), WL_INFO, x, y, res.GetTextRefStackGRF(), res.GetTextRefStackSize(), res.GetTextRefStack());
593 } else if (estimate_only) {
594 ShowEstimatedCostOrIncome(res.GetCost(), x, y);
595 } else if (!only_sending && res.GetCost() != 0 && tile != 0 && IsLocalCompany() && _game_mode != GM_EDITOR) {
596 /* Only show the cost animation when we did actually
597 * execute the command, i.e. we're not sending it to
598 * the server, when it has cost the local company
599 * something. Furthermore in the editor there is no
600 * concept of cost, so don't show it there either. */
601 ShowCostOrIncomeAnimation(x, y, GetSlopePixelZ(x, y), res.GetCost());
604 if (!estimate_only && !only_sending && callback != nullptr) {
605 callback(res, tile, p1, p2, cmd);
608 return res.Succeeded();
613 * Helper to deduplicate the code for returning.
614 * @param cmd the command cost to return.
616 #define return_dcpi(cmd) { _docommand_recursive = 0; return cmd; }
619 * Helper function for the toplevel network safe docommand function for the current company.
621 * @param tile The tile to perform a command on (see #CommandProc)
622 * @param p1 Additional data for the command (see #CommandProc)
623 * @param p2 Additional data for the command (see #CommandProc)
624 * @param cmd The command to execute (a CMD_* value)
625 * @param callback A callback function to call after the command is finished
626 * @param text The text to pass
627 * @param my_cmd indicator if the command is from a company or server (to display error messages for a user)
628 * @param estimate_only whether to give only the estimate or also execute the command
629 * @return the command cost of this function.
631 CommandCost DoCommandPInternal(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, bool my_cmd, bool estimate_only)
633 /* Prevent recursion; it gives a mess over the network */
634 assert(_docommand_recursive == 0);
635 _docommand_recursive = 1;
637 /* Reset the state. */
638 _additional_cash_required = 0;
640 /* Get pointer to command handler */
641 byte cmd_id = cmd & CMD_ID_MASK;
642 assert(cmd_id < lengthof(_command_proc_table));
644 CommandProc *proc = _command_proc_table[cmd_id].proc;
645 /* Shouldn't happen, but you never know when someone adds
646 * NULLs to the _command_proc_table. */
647 assert(proc != nullptr);
649 /* Command flags are used internally */
650 CommandFlags cmd_flags = GetCommandFlags(cmd);
651 /* Flags get send to the DoCommand */
652 DoCommandFlag flags = CommandFlagsToDCFlags(cmd_flags);
654 /* Make sure p2 is properly set to a ClientID. */
655 assert(!(cmd_flags & CMD_CLIENT_ID) || p2 != 0);
657 /* Do not even think about executing out-of-bounds tile-commands */
658 if (tile != 0 && (tile >= MapSize() || (!IsValidTile(tile) && (cmd_flags & CMD_ALL_TILES) == 0))) return_dcpi(CMD_ERROR);
660 /* Always execute server and spectator commands as spectator */
661 bool exec_as_spectator = (cmd_flags & (CMD_SPECTATOR | CMD_SERVER)) != 0;
663 /* If the company isn't valid it may only do server command or start a new company!
664 * The server will ditch any server commands a client sends to it, so effectively
665 * this guards the server from executing functions for an invalid company. */
666 if (_game_mode == GM_NORMAL && !exec_as_spectator && !Company::IsValidID(_current_company) && !(_current_company == OWNER_DEITY && (cmd_flags & CMD_DEITY) != 0)) {
667 return_dcpi(CMD_ERROR);
670 Backup<CompanyID> cur_company(_current_company, FILE_LINE);
671 if (exec_as_spectator) cur_company.Change(COMPANY_SPECTATOR);
673 bool test_and_exec_can_differ = (cmd_flags & CMD_NO_TEST) != 0;
675 /* Test the command. */
676 _cleared_object_areas.clear();
677 SetTownRatingTestMode(true);
678 BasePersistentStorageArray::SwitchMode(PSM_ENTER_TESTMODE);
679 CommandCost res = proc(tile, flags, p1, p2, text);
680 BasePersistentStorageArray::SwitchMode(PSM_LEAVE_TESTMODE);
681 SetTownRatingTestMode(false);
683 /* Make sure we're not messing things up here. */
684 assert(exec_as_spectator ? _current_company == COMPANY_SPECTATOR : cur_company.Verify());
686 /* If the command fails, we're doing an estimate
687 * or the player does not have enough money
688 * (unless it's a command where the test and
689 * execution phase might return different costs)
690 * we bail out here. */
691 if (res.Failed() || estimate_only ||
692 (!test_and_exec_can_differ && !CheckCompanyHasMoney(res))) {
693 if (!_networking || _generating_world || (cmd & CMD_NETWORK_COMMAND) != 0) {
694 /* Log the failed command as well. Just to be able to be find
695 * causes of desyncs due to bad command test implementations. */
696 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));
698 cur_company.Restore();
699 return_dcpi(res);
703 * If we are in network, and the command is not from the network
704 * send it to the command-queue and abort execution
706 if (_networking && !_generating_world && !(cmd & CMD_NETWORK_COMMAND)) {
707 NetworkSendCommand(tile, p1, p2, cmd & ~CMD_FLAGS_MASK, callback, text, _current_company);
708 cur_company.Restore();
710 /* Don't return anything special here; no error, no costs.
711 * This way it's not handled by DoCommand and only the
712 * actual execution of the command causes messages. Also
713 * reset the storages as we've not executed the command. */
714 return_dcpi(CommandCost());
716 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));
718 /* Actually try and execute the command. If no cost-type is given
719 * use the construction one */
720 _cleared_object_areas.clear();
721 BasePersistentStorageArray::SwitchMode(PSM_ENTER_COMMAND);
722 CommandCost res2 = proc(tile, flags | DC_EXEC, p1, p2, text);
723 BasePersistentStorageArray::SwitchMode(PSM_LEAVE_COMMAND);
725 if (cmd_id == CMD_COMPANY_CTRL) {
726 cur_company.Trash();
727 /* We are a new company -> Switch to new local company.
728 * We were closed down -> Switch to spectator
729 * Some other company opened/closed down -> The outside function will switch back */
730 _current_company = _local_company;
731 } else {
732 /* Make sure nothing bad happened, like changing the current company. */
733 assert(exec_as_spectator ? _current_company == COMPANY_SPECTATOR : cur_company.Verify());
734 cur_company.Restore();
737 /* If the test and execution can differ we have to check the
738 * return of the command. Otherwise we can check whether the
739 * test and execution have yielded the same result,
740 * i.e. cost and error state are the same. */
741 if (!test_and_exec_can_differ) {
742 assert(res.GetCost() == res2.GetCost() && res.Failed() == res2.Failed()); // sanity check
743 } else if (res2.Failed()) {
744 return_dcpi(res2);
747 /* If we're needing more money and we haven't done
748 * anything yet, ask for the money! */
749 if (_additional_cash_required != 0 && res2.GetCost() == 0) {
750 /* It could happen we removed rail, thus gained money, and deleted something else.
751 * So make sure the signal buffer is empty even in this case */
752 UpdateSignalsInBuffer();
753 SetDParam(0, _additional_cash_required);
754 return_dcpi(CommandCost(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY));
757 /* update last build coordinate of company. */
758 if (tile != 0) {
759 Company *c = Company::GetIfValid(_current_company);
760 if (c != nullptr) c->last_build_coordinate = tile;
763 SubtractMoneyFromCompany(res2);
765 /* update signals if needed */
766 UpdateSignalsInBuffer();
768 return_dcpi(res2);
770 #undef return_dcpi
774 * Adds the cost of the given command return value to this cost.
775 * Also takes a possible error message when it is set.
776 * @param ret The command to add the cost of.
778 void CommandCost::AddCost(const CommandCost &ret)
780 this->AddCost(ret.cost);
781 if (this->success && !ret.success) {
782 this->message = ret.message;
783 this->success = false;
788 * Values to put on the #TextRefStack for the error message.
789 * There is only one static instance of the array, just like there is only one
790 * instance of normal DParams.
792 uint32 CommandCost::textref_stack[16];
795 * Activate usage of the NewGRF #TextRefStack for the error message.
796 * @param grffile NewGRF that provides the #TextRefStack
797 * @param num_registers number of entries to copy from the temporary NewGRF registers
799 void CommandCost::UseTextRefStack(const GRFFile *grffile, uint num_registers)
801 extern TemporaryStorageArray<int32, 0x110> _temp_store;
803 assert(num_registers < lengthof(textref_stack));
804 this->textref_stack_grffile = grffile;
805 this->textref_stack_size = num_registers;
806 for (uint i = 0; i < num_registers; i++) {
807 textref_stack[i] = _temp_store.GetValue(0x100 + i);