OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

command.cpp
Go to the documentation of this file.
1 /* $Id: command.cpp 26802 2014-09-07 16:12:58Z alberth $ */
2
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 */
9
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"
29
30 #include "table/strings.h"
31
32 #include "safeguards.h"
33
34 CommandProc CmdBuildRailroadTrack;
35 CommandProc CmdRemoveRailroadTrack;
36 CommandProc CmdBuildSingleRail;
37 CommandProc CmdRemoveSingleRail;
38
39 CommandProc CmdLandscapeClear;
40
41 CommandProc CmdBuildBridge;
42
43 CommandProc CmdBuildRailStation;
44 CommandProc CmdRemoveFromRailStation;
45 CommandProc CmdConvertRail;
46
47 CommandProc CmdBuildSingleSignal;
48 CommandProc CmdRemoveSingleSignal;
49
50 CommandProc CmdTerraformLand;
51
52 CommandProc CmdBuildObject;
53 CommandProc CmdSellLandArea;
54
55 CommandProc CmdBuildTunnel;
56
57 CommandProc CmdBuildTrainDepot;
58 CommandProc CmdBuildRailWaypoint;
59 CommandProc CmdRenameWaypoint;
60 CommandProc CmdRemoveFromRailWaypoint;
61
62 CommandProc CmdBuildRoadStop;
63 CommandProc CmdRemoveRoadStop;
64
65 CommandProc CmdBuildLongRoad;
66 CommandProc CmdRemoveLongRoad;
67 CommandProc CmdBuildRoad;
68
69 CommandProc CmdBuildRoadDepot;
70
71 CommandProc CmdBuildAirport;
72
73 CommandProc CmdBuildDock;
74
75 CommandProc CmdBuildShipDepot;
76
77 CommandProc CmdBuildBuoy;
78
79 CommandProc CmdPlantTree;
80
81 CommandProc CmdMoveRailVehicle;
82
83 CommandProc CmdBuildVehicle;
84 CommandProc CmdSellVehicle;
85 CommandProc CmdRefitVehicle;
86 CommandProc CmdSendVehicleToDepot;
87 CommandProc CmdSetVehicleVisibility;
88
89 CommandProc CmdForceTrainProceed;
90 CommandProc CmdReverseTrainDirection;
91
92 CommandProc CmdClearOrderBackup;
93 CommandProc CmdModifyOrder;
94 CommandProc CmdSkipToOrder;
95 CommandProc CmdDeleteOrder;
96 CommandProc CmdInsertOrder;
97 CommandProc CmdChangeServiceInt;
98
99 CommandProc CmdBuildIndustry;
100
101 CommandProc CmdSetCompanyManagerFace;
102 CommandProc CmdSetCompanyColour;
103
104 CommandProc CmdIncreaseLoan;
105 CommandProc CmdDecreaseLoan;
106
107 CommandProc CmdWantEnginePreview;
108
109 CommandProc CmdRenameVehicle;
110 CommandProc CmdRenameEngine;
111
112 CommandProc CmdRenameCompany;
113 CommandProc CmdRenamePresident;
114
115 CommandProc CmdRenameStation;
116 CommandProc CmdRenameDepot;
117
118 CommandProc CmdPlaceSign;
119 CommandProc CmdRenameSign;
120
121 CommandProc CmdTurnRoadVeh;
122
123 CommandProc CmdPause;
124
125 CommandProc CmdBuyShareInCompany;
126 CommandProc CmdSellShareInCompany;
127 CommandProc CmdBuyCompany;
128
129 CommandProc CmdFoundTown;
130 CommandProc CmdRenameTown;
131 CommandProc CmdDoTownAction;
132 CommandProc CmdTownGrowthRate;
133 CommandProc CmdTownCargoGoal;
134 CommandProc CmdTownSetText;
135 CommandProc CmdExpandTown;
136 CommandProc CmdDeleteTown;
137
138 CommandProc CmdChangeSetting;
139 CommandProc CmdChangeCompanySetting;
140
141 CommandProc CmdOrderRefit;
142 CommandProc CmdCloneOrder;
143
144 CommandProc CmdClearArea;
145
146 CommandProc CmdGiveMoney;
147 CommandProc CmdMoneyCheat;
148 CommandProc CmdChangeBankBalance;
149 CommandProc CmdBuildCanal;
150 CommandProc CmdBuildLock;
151
152 CommandProc CmdCreateSubsidy;
153 CommandProc CmdCompanyCtrl;
154 CommandProc CmdCustomNewsItem;
155 CommandProc CmdCreateGoal;
156 CommandProc CmdRemoveGoal;
157 CommandProc CmdSetGoalText;
158 CommandProc CmdSetGoalProgress;
159 CommandProc CmdSetGoalCompleted;
160 CommandProc CmdGoalQuestion;
161 CommandProc CmdGoalQuestionAnswer;
162 CommandProc CmdCreateStoryPage;
163 CommandProc CmdCreateStoryPageElement;
164 CommandProc CmdUpdateStoryPageElement;
165 CommandProc CmdSetStoryPageTitle;
166 CommandProc CmdSetStoryPageDate;
167 CommandProc CmdShowStoryPage;
168 CommandProc CmdRemoveStoryPage;
169 CommandProc CmdRemoveStoryPageElement;
170
171 CommandProc CmdLevelLand;
172
173 CommandProc CmdBuildSignalTrack;
174 CommandProc CmdRemoveSignalTrack;
175
176 CommandProc CmdSetAutoReplace;
177
178 CommandProc CmdCloneVehicle;
179 CommandProc CmdStartStopVehicle;
180 CommandProc CmdMassStartStopVehicle;
181 CommandProc CmdAutoreplaceVehicle;
182 CommandProc CmdDepotSellAllVehicles;
183 CommandProc CmdDepotMassAutoReplace;
184
185 CommandProc CmdCreateGroup;
186 CommandProc CmdAlterGroup;
187 CommandProc CmdDeleteGroup;
188 CommandProc CmdAddVehicleGroup;
189 CommandProc CmdAddSharedVehicleGroup;
190 CommandProc CmdRemoveAllVehiclesGroup;
191 CommandProc CmdSetGroupReplaceProtection;
192
193 CommandProc CmdMoveOrder;
194 CommandProc CmdChangeTimetable;
195 CommandProc CmdSetVehicleOnTime;
196 CommandProc CmdAutofillTimetable;
197 CommandProc CmdSetTimetableStart;
198
199 CommandProc CmdOpenCloseAirport;
200
201 #define DEF_CMD(proc, flags, type) {proc, #proc, (CommandFlags)flags, type}
202
210 static const Command _command_proc_table[] = {
211 DEF_CMD(CmdBuildRailroadTrack, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_RAILROAD_TRACK
212 DEF_CMD(CmdRemoveRailroadTrack, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_RAILROAD_TRACK
213 DEF_CMD(CmdBuildSingleRail, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_SINGLE_RAIL
214 DEF_CMD(CmdRemoveSingleRail, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_SINGLE_RAIL
215 DEF_CMD(CmdLandscapeClear, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_LANDSCAPE_CLEAR
216 DEF_CMD(CmdBuildBridge, CMD_DEITY | CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_BRIDGE
217 DEF_CMD(CmdBuildRailStation, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_RAIL_STATION
218 DEF_CMD(CmdBuildTrainDepot, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_TRAIN_DEPOT
219 DEF_CMD(CmdBuildSingleSignal, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_SIGNALS
220 DEF_CMD(CmdRemoveSingleSignal, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_SIGNALS
221 DEF_CMD(CmdTerraformLand, CMD_ALL_TILES | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_TERRAFORM_LAND
222 DEF_CMD(CmdBuildObject, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_OBJECT
223 DEF_CMD(CmdBuildTunnel, CMD_DEITY | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_TUNNEL
224 DEF_CMD(CmdRemoveFromRailStation, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_FROM_RAIL_STATION
225 DEF_CMD(CmdConvertRail, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_CONVERT_RAILD
226 DEF_CMD(CmdBuildRailWaypoint, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_RAIL_WAYPOINT
227 DEF_CMD(CmdRenameWaypoint, 0, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_WAYPOINT
228 DEF_CMD(CmdRemoveFromRailWaypoint, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_FROM_RAIL_WAYPOINT
229
230 DEF_CMD(CmdBuildRoadStop, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_ROAD_STOP
231 DEF_CMD(CmdRemoveRoadStop, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_ROAD_STOP
232 DEF_CMD(CmdBuildLongRoad,CMD_DEITY | CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_LONG_ROAD
233 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.
234 DEF_CMD(CmdBuildRoad, CMD_DEITY | CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_ROAD
235 DEF_CMD(CmdBuildRoadDepot, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_ROAD_DEPOT
236
237 DEF_CMD(CmdBuildAirport, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_AIRPORT
238 DEF_CMD(CmdBuildDock, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_DOCK
239 DEF_CMD(CmdBuildShipDepot, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_SHIP_DEPOT
240 DEF_CMD(CmdBuildBuoy, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_BUOY
241 DEF_CMD(CmdPlantTree, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_PLANT_TREE
242
243 DEF_CMD(CmdBuildVehicle, CMD_CLIENT_ID, CMDT_VEHICLE_CONSTRUCTION ), // CMD_BUILD_VEHICLE
244 DEF_CMD(CmdSellVehicle, CMD_CLIENT_ID, CMDT_VEHICLE_CONSTRUCTION ), // CMD_SELL_VEHICLE
245 DEF_CMD(CmdRefitVehicle, 0, CMDT_VEHICLE_CONSTRUCTION ), // CMD_REFIT_VEHICLE
246 DEF_CMD(CmdSendVehicleToDepot, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_SEND_VEHICLE_TO_DEPOT
247 DEF_CMD(CmdSetVehicleVisibility, 0, CMDT_COMPANY_SETTING ), // CMD_SET_VEHICLE_VISIBILITY
248
249 DEF_CMD(CmdMoveRailVehicle, 0, CMDT_VEHICLE_CONSTRUCTION ), // CMD_MOVE_RAIL_VEHICLE
250 DEF_CMD(CmdForceTrainProceed, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_FORCE_TRAIN_PROCEED
251 DEF_CMD(CmdReverseTrainDirection, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_REVERSE_TRAIN_DIRECTION
252
253 DEF_CMD(CmdClearOrderBackup, CMD_CLIENT_ID, CMDT_SERVER_SETTING ), // CMD_CLEAR_ORDER_BACKUP
254 DEF_CMD(CmdModifyOrder, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_MODIFY_ORDER
255 DEF_CMD(CmdSkipToOrder, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_SKIP_TO_ORDER
256 DEF_CMD(CmdDeleteOrder, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_DELETE_ORDER
257 DEF_CMD(CmdInsertOrder, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_INSERT_ORDER
258
259 DEF_CMD(CmdChangeServiceInt, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_CHANGE_SERVICE_INT
260
261 DEF_CMD(CmdBuildIndustry, CMD_DEITY, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_INDUSTRY
262 DEF_CMD(CmdSetCompanyManagerFace, 0, CMDT_OTHER_MANAGEMENT ), // CMD_SET_COMPANY_MANAGER_FACE
263 DEF_CMD(CmdSetCompanyColour, 0, CMDT_OTHER_MANAGEMENT ), // CMD_SET_COMPANY_COLOUR
264
265 DEF_CMD(CmdIncreaseLoan, 0, CMDT_MONEY_MANAGEMENT ), // CMD_INCREASE_LOAN
266 DEF_CMD(CmdDecreaseLoan, 0, CMDT_MONEY_MANAGEMENT ), // CMD_DECREASE_LOAN
267
268 DEF_CMD(CmdWantEnginePreview, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_WANT_ENGINE_PREVIEW
269
270 DEF_CMD(CmdRenameVehicle, 0, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_VEHICLE
271 DEF_CMD(CmdRenameEngine, CMD_SERVER, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_ENGINE
272
273 DEF_CMD(CmdRenameCompany, 0, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_COMPANY
274 DEF_CMD(CmdRenamePresident, 0, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_PRESIDENT
275
276 DEF_CMD(CmdRenameStation, 0, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_STATION
277 DEF_CMD(CmdRenameDepot, 0, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_DEPOT
278
279 DEF_CMD(CmdPlaceSign, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_PLACE_SIGN
280 DEF_CMD(CmdRenameSign, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_SIGN
281
282 DEF_CMD(CmdTurnRoadVeh, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_TURN_ROADVEH
283
284 DEF_CMD(CmdPause, CMD_SERVER, CMDT_SERVER_SETTING ), // CMD_PAUSE
285
286 DEF_CMD(CmdBuyShareInCompany, 0, CMDT_MONEY_MANAGEMENT ), // CMD_BUY_SHARE_IN_COMPANY
287 DEF_CMD(CmdSellShareInCompany, 0, CMDT_MONEY_MANAGEMENT ), // CMD_SELL_SHARE_IN_COMPANY
288 DEF_CMD(CmdBuyCompany, 0, CMDT_MONEY_MANAGEMENT ), // CMD_BUY_COMANY
289
290 DEF_CMD(CmdFoundTown, CMD_DEITY | CMD_NO_TEST, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_FOUND_TOWN; founding random town can fail only in exec run
291 DEF_CMD(CmdRenameTown, CMD_DEITY | CMD_SERVER, CMDT_OTHER_MANAGEMENT ), // CMD_RENAME_TOWN
292 DEF_CMD(CmdDoTownAction, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_DO_TOWN_ACTION
293 DEF_CMD(CmdTownCargoGoal, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_TOWN_CARGO_GOAL
294 DEF_CMD(CmdTownGrowthRate, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_TOWN_GROWTH_RATE
295 DEF_CMD(CmdTownSetText, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_TOWN_SET_TEXT
296 DEF_CMD(CmdExpandTown, CMD_DEITY, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_EXPAND_TOWN
297 DEF_CMD(CmdDeleteTown, CMD_OFFLINE, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_DELETE_TOWN
298
299 DEF_CMD(CmdOrderRefit, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_ORDER_REFIT
300 DEF_CMD(CmdCloneOrder, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_CLONE_ORDER
301
302 DEF_CMD(CmdClearArea, CMD_NO_TEST, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_CLEAR_AREA; destroying multi-tile houses makes town rating differ between test and execution
303
304 DEF_CMD(CmdMoneyCheat, CMD_OFFLINE, CMDT_CHEAT ), // CMD_MONEY_CHEAT
305 DEF_CMD(CmdChangeBankBalance, CMD_DEITY, CMDT_MONEY_MANAGEMENT ), // CMD_CHANGE_BANK_BALANCE
306 DEF_CMD(CmdBuildCanal, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_CANAL
307 DEF_CMD(CmdCreateSubsidy, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CREATE_SUBSIDY
308 DEF_CMD(CmdCompanyCtrl, CMD_SPECTATOR | CMD_CLIENT_ID, CMDT_SERVER_SETTING ), // CMD_COMPANY_CTRL
309 DEF_CMD(CmdCustomNewsItem, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CUSTOM_NEWS_ITEM
310 DEF_CMD(CmdCreateGoal, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CREATE_GOAL
311 DEF_CMD(CmdRemoveGoal, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_REMOVE_GOAL
312 DEF_CMD(CmdSetGoalText, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_SET_GOAL_TEXT
313 DEF_CMD(CmdSetGoalProgress, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_SET_GOAL_PROGRESS
314 DEF_CMD(CmdSetGoalCompleted, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_SET_GOAL_COMPLETED
315 DEF_CMD(CmdGoalQuestion, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_GOAL_QUESTION
316 DEF_CMD(CmdGoalQuestionAnswer, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_GOAL_QUESTION_ANSWER
317 DEF_CMD(CmdCreateStoryPage, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CREATE_STORY_PAGE
318 DEF_CMD(CmdCreateStoryPageElement, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_CREATE_STORY_PAGE_ELEMENT
319 DEF_CMD(CmdUpdateStoryPageElement, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_UPDATE_STORY_PAGE_ELEMENT
320 DEF_CMD(CmdSetStoryPageTitle, CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_SET_STORY_PAGE_TITLE
321 DEF_CMD(CmdSetStoryPageDate, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_SET_STORY_PAGE_DATE
322 DEF_CMD(CmdShowStoryPage, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_SHOW_STORY_PAGE
323 DEF_CMD(CmdRemoveStoryPage, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_REMOVE_STORY_PAGE
324 DEF_CMD(CmdRemoveStoryPageElement, CMD_DEITY, CMDT_OTHER_MANAGEMENT ), // CMD_REMOVE_STORY_ELEMENT_PAGE
325
326 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
327
328 DEF_CMD(CmdBuildLock, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_LOCK
329
330 DEF_CMD(CmdBuildSignalTrack, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_SIGNAL_TRACK
331 DEF_CMD(CmdRemoveSignalTrack, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_SIGNAL_TRACK
332
333 DEF_CMD(CmdGiveMoney, 0, CMDT_MONEY_MANAGEMENT ), // CMD_GIVE_MONEY
334 DEF_CMD(CmdChangeSetting, CMD_SERVER, CMDT_SERVER_SETTING ), // CMD_CHANGE_SETTING
335 DEF_CMD(CmdChangeCompanySetting, 0, CMDT_COMPANY_SETTING ), // CMD_CHANGE_COMPANY_SETTING
336 DEF_CMD(CmdSetAutoReplace, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_SET_AUTOREPLACE
337 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
338 DEF_CMD(CmdStartStopVehicle, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_START_STOP_VEHICLE
339 DEF_CMD(CmdMassStartStopVehicle, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_MASS_START_STOP
340 DEF_CMD(CmdAutoreplaceVehicle, 0, CMDT_VEHICLE_MANAGEMENT ), // CMD_AUTOREPLACE_VEHICLE
341 DEF_CMD(CmdDepotSellAllVehicles, 0, CMDT_VEHICLE_CONSTRUCTION ), // CMD_DEPOT_SELL_ALL_VEHICLES
342 DEF_CMD(CmdDepotMassAutoReplace, 0, CMDT_VEHICLE_CONSTRUCTION ), // CMD_DEPOT_MASS_AUTOREPLACE
343 DEF_CMD(CmdCreateGroup, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_CREATE_GROUP
344 DEF_CMD(CmdDeleteGroup, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_DELETE_GROUP
345 DEF_CMD(CmdAlterGroup, 0, CMDT_OTHER_MANAGEMENT ), // CMD_ALTER_GROUP
346 DEF_CMD(CmdAddVehicleGroup, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_ADD_VEHICLE_GROUP
347 DEF_CMD(CmdAddSharedVehicleGroup, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_ADD_SHARE_VEHICLE_GROUP
348 DEF_CMD(CmdRemoveAllVehiclesGroup, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_REMOVE_ALL_VEHICLES_GROUP
349 DEF_CMD(CmdSetGroupReplaceProtection, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_SET_GROUP_REPLACE_PROTECTION
350 DEF_CMD(CmdMoveOrder, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_MOVE_ORDER
351 DEF_CMD(CmdChangeTimetable, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_CHANGE_TIMETABLE
352 DEF_CMD(CmdSetVehicleOnTime, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_SET_VEHICLE_ON_TIME
353 DEF_CMD(CmdAutofillTimetable, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_AUTOFILL_TIMETABLE
354 DEF_CMD(CmdSetTimetableStart, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_SET_TIMETABLE_START
355
356 DEF_CMD(CmdOpenCloseAirport, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_OPEN_CLOSE_AIRPORT
357 };
358
365 bool IsValidCommand(uint32 cmd)
366 {
367 cmd &= CMD_ID_MASK;
368
369 return cmd < lengthof(_command_proc_table) && _command_proc_table[cmd].proc != NULL;
370 }
371
379 CommandFlags GetCommandFlags(uint32 cmd)
380 {
381 assert(IsValidCommand(cmd));
382
383 return _command_proc_table[cmd & CMD_ID_MASK].flags;
384 }
385
393 const char *GetCommandName(uint32 cmd)
394 {
395 assert(IsValidCommand(cmd));
396
397 return _command_proc_table[cmd & CMD_ID_MASK].name;
398 }
399
405 bool IsCommandAllowedWhilePaused(uint32 cmd)
406 {
407 /* Lookup table for the command types that are allowed for a given pause level setting. */
408 static const int command_type_lookup[] = {
409 CMDPL_ALL_ACTIONS,
410 CMDPL_NO_LANDSCAPING,
411 CMDPL_NO_LANDSCAPING,
412 CMDPL_NO_CONSTRUCTION,
413 CMDPL_NO_CONSTRUCTION,
414 CMDPL_NO_CONSTRUCTION,
415 CMDPL_NO_CONSTRUCTION,
416 CMDPL_NO_ACTIONS,
417 CMDPL_NO_ACTIONS,
418 };
419 assert_compile(lengthof(command_type_lookup) == CMDT_END);
420
421 assert(IsValidCommand(cmd));
422 return _game_mode == GM_EDITOR || command_type_lookup[_command_proc_table[cmd & CMD_ID_MASK].type] <= _settings_game.construction.command_pause_level;
423 }
424
425
426 static int _docommand_recursive = 0;
427
436 CommandCost DoCommand(const CommandContainer *container, DoCommandFlag flags)
437 {
438 return DoCommand(container->tile, container->p1, container->p2, flags, container->cmd & CMD_ID_MASK, container->text);
439 }
440
454 CommandCost DoCommand(TileIndex tile, uint32 p1, uint32 p2, DoCommandFlag flags, uint32 cmd, const char *text)
455 {
456 CommandCost res;
457
458 /* Do not even think about executing out-of-bounds tile-commands */
459 if (tile != 0 && (tile >= MapSize() || (!IsValidTile(tile) && (flags & DC_ALL_TILES) == 0))) return CMD_ERROR;
460
461 /* Chop of any CMD_MSG or other flags; we don't need those here */
462 CommandProc *proc = _command_proc_table[cmd & CMD_ID_MASK].proc;
463
464 _docommand_recursive++;
465
466 /* only execute the test call if it's toplevel, or we're not execing. */
467 if (_docommand_recursive == 1 || !(flags & DC_EXEC) ) {
468 if (_docommand_recursive == 1) _cleared_object_areas.Clear();
469 SetTownRatingTestMode(true);
470 res = proc(tile, flags & ~DC_EXEC, p1, p2, text);
471 SetTownRatingTestMode(false);
472 if (res.Failed()) {
473 goto error;
474 }
475
476 if (_docommand_recursive == 1 &&
477 !(flags & DC_QUERY_COST) &&
478 !(flags & DC_BANKRUPT) &&
479 !CheckCompanyHasMoney(res)) { // CheckCompanyHasMoney() modifies 'res' to an error if it fails.
480 goto error;
481 }
482
483 if (!(flags & DC_EXEC)) {
484 _docommand_recursive--;
485 return res;
486 }
487 }
488
489 /* Execute the command here. All cost-relevant functions set the expenses type
490 * themselves to the cost object at some point */
491 if (_docommand_recursive == 1) _cleared_object_areas.Clear();
492 res = proc(tile, flags, p1, p2, text);
493 if (res.Failed()) {
494 error:
495 _docommand_recursive--;
496 return res;
497 }
498
499 /* if toplevel, subtract the money. */
500 if (--_docommand_recursive == 0 && !(flags & DC_BANKRUPT)) {
501 SubtractMoneyFromCompany(res);
502 }
503
504 return res;
505 }
506
514 Money GetAvailableMoneyForCommand()
515 {
516 CompanyID company = _current_company;
517 if (!Company::IsValidID(company)) return INT64_MAX;
518 return Company::Get(company)->money;
519 }
520
527 bool DoCommandP(const CommandContainer *container, bool my_cmd)
528 {
529 return DoCommandP(container->tile, container->p1, container->p2, container->cmd, container->callback, container->text, my_cmd);
530 }
531
547 bool DoCommandP(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, bool my_cmd)
548 {
549 /* Cost estimation is generally only done when the
550 * local user presses shift while doing somthing.
551 * However, in case of incoming network commands,
552 * map generation or the pause button we do want
553 * to execute. */
554 bool estimate_only = _shift_pressed && IsLocalCompany() &&
555 !_generating_world &&
556 !(cmd & CMD_NETWORK_COMMAND) &&
557 (cmd & CMD_ID_MASK) != CMD_PAUSE;
558
559 /* We're only sending the command, so don't do
560 * fancy things for 'success'. */
561 bool only_sending = _networking && !(cmd & CMD_NETWORK_COMMAND);
562
563 /* Where to show the message? */
564 int x = TileX(tile) * TILE_SIZE;
565 int y = TileY(tile) * TILE_SIZE;
566
567 if (_pause_mode != PM_UNPAUSED && !IsCommandAllowedWhilePaused(cmd)) {
568 ShowErrorMessage(GB(cmd, 16, 16), STR_ERROR_NOT_ALLOWED_WHILE_PAUSED, WL_INFO, x, y);
569 return false;
570 }
571
572 #ifdef ENABLE_NETWORK
573 /* Only set p2 when the command does not come from the network. */
574 if (!(cmd & CMD_NETWORK_COMMAND) && GetCommandFlags(cmd) & CMD_CLIENT_ID && p2 == 0) p2 = CLIENT_ID_SERVER;
575 #endif
576
577 CommandCost res = DoCommandPInternal(tile, p1, p2, cmd, callback, text, my_cmd, estimate_only);
578 if (res.Failed()) {
579 /* Only show the error when it's for us. */
580 StringID error_part1 = GB(cmd, 16, 16);
581 if (estimate_only || (IsLocalCompany() && error_part1 != 0 && my_cmd)) {
582 ShowErrorMessage(error_part1, res.GetErrorMessage(), WL_INFO, x, y, res.GetTextRefStackGRF(), res.GetTextRefStackSize(), res.GetTextRefStack());
583 }
584 } else if (estimate_only) {
585 ShowEstimatedCostOrIncome(res.GetCost(), x, y);
586 } else if (!only_sending && res.GetCost() != 0 && tile != 0 && IsLocalCompany() && _game_mode != GM_EDITOR) {
587 /* Only show the cost animation when we did actually
588 * execute the command, i.e. we're not sending it to
589 * the server, when it has cost the local company
590 * something. Furthermore in the editor there is no
591 * concept of cost, so don't show it there either. */
592 ShowCostOrIncomeAnimation(x, y, GetSlopePixelZ(x, y), res.GetCost());
593 }
594
595 if (!estimate_only && !only_sending && callback != NULL) {
596 callback(res, tile, p1, p2);
597 }
598
599 return res.Succeeded();
600 }
601
602
608 #define return_dcpi(cmd) { _docommand_recursive = 0; return cmd; }
609
623 CommandCost DoCommandPInternal(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, bool my_cmd, bool estimate_only)
624 {
625 /* Prevent recursion; it gives a mess over the network */
626 assert(_docommand_recursive == 0);
627 _docommand_recursive = 1;
628
629 /* Reset the state. */
630 _additional_cash_required = 0;
631
632 /* Get pointer to command handler */
633 byte cmd_id = cmd & CMD_ID_MASK;
634 assert(cmd_id < lengthof(_command_proc_table));
635
636 CommandProc *proc = _command_proc_table[cmd_id].proc;
637 /* Shouldn't happen, but you never know when someone adds
638 * NULLs to the _command_proc_table. */
639 assert(proc != NULL);
640
641 /* Command flags are used internally */
642 CommandFlags cmd_flags = GetCommandFlags(cmd);
643 /* Flags get send to the DoCommand */
644 DoCommandFlag flags = CommandFlagsToDCFlags(cmd_flags);
645
646 #ifdef ENABLE_NETWORK
647 /* Make sure p2 is properly set to a ClientID. */
648 assert(!(cmd_flags & CMD_CLIENT_ID) || p2 != 0);
649 #endif
650
651 /* Do not even think about executing out-of-bounds tile-commands */
652 if (tile != 0 && (tile >= MapSize() || (!IsValidTile(tile) && (cmd_flags & CMD_ALL_TILES) == 0))) return_dcpi(CMD_ERROR);
653
654 /* Always execute server and spectator commands as spectator */
655 bool exec_as_spectator = (cmd_flags & (CMD_SPECTATOR | CMD_SERVER)) != 0;
656
657 /* If the company isn't valid it may only do server command or start a new company!
658 * The server will ditch any server commands a client sends to it, so effectively
659 * this guards the server from executing functions for an invalid company. */
660 if (_game_mode == GM_NORMAL && !exec_as_spectator && !Company::IsValidID(_current_company) && !(_current_company == OWNER_DEITY && (cmd_flags & CMD_DEITY) != 0)) {
661 return_dcpi(CMD_ERROR);
662 }
663
664 Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
665 if (exec_as_spectator) cur_company.Change(COMPANY_SPECTATOR);
666
667 bool test_and_exec_can_differ = (cmd_flags & CMD_NO_TEST) != 0;
668
669 /* Test the command. */
670 _cleared_object_areas.Clear();
671 SetTownRatingTestMode(true);
672 BasePersistentStorageArray::SwitchMode(PSM_ENTER_TESTMODE);
673 CommandCost res = proc(tile, flags, p1, p2, text);
674 BasePersistentStorageArray::SwitchMode(PSM_LEAVE_TESTMODE);
675 SetTownRatingTestMode(false);
676
677 /* Make sure we're not messing things up here. */
678 assert(exec_as_spectator ? _current_company == COMPANY_SPECTATOR : cur_company.Verify());
679
680 /* If the command fails, we're doing an estimate
681 * or the player does not have enough money
682 * (unless it's a command where the test and
683 * execution phase might return different costs)
684 * we bail out here. */
685 if (res.Failed() || estimate_only ||
686 (!test_and_exec_can_differ && !CheckCompanyHasMoney(res))) {
687 if (!_networking || _generating_world || (cmd & CMD_NETWORK_COMMAND) != 0) {
688 /* Log the failed command as well. Just to be able to be find
689 * causes of desyncs due to bad command test implementations. */
690 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));
691 }
692 cur_company.Restore();
693 return_dcpi(res);
694 }
695
696 #ifdef ENABLE_NETWORK
697 /*
698 * If we are in network, and the command is not from the network
699 * send it to the command-queue and abort execution
700 */
701 if (_networking && !_generating_world && !(cmd & CMD_NETWORK_COMMAND)) {
702 NetworkSendCommand(tile, p1, p2, cmd & ~CMD_FLAGS_MASK, callback, text, _current_company);
703 cur_company.Restore();
704
705 /* Don't return anything special here; no error, no costs.
706 * This way it's not handled by DoCommand and only the
707 * actual execution of the command causes messages. Also
708 * reset the storages as we've not executed the command. */
709 return_dcpi(CommandCost());
710 }
711 #endif /* ENABLE_NETWORK */
712 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));
713
714 /* Actually try and execute the command. If no cost-type is given
715 * use the construction one */
716 _cleared_object_areas.Clear();
717 BasePersistentStorageArray::SwitchMode(PSM_ENTER_COMMAND);
718 CommandCost res2 = proc(tile, flags | DC_EXEC, p1, p2, text);
719 BasePersistentStorageArray::SwitchMode(PSM_LEAVE_COMMAND);
720
721 if (cmd_id == CMD_COMPANY_CTRL) {
722 cur_company.Trash();
723 /* We are a new company -> Switch to new local company.
724 * We were closed down -> Switch to spectator
725 * Some other company opened/closed down -> The outside function will switch back */
726 _current_company = _local_company;
727 } else {
728 /* Make sure nothing bad happened, like changing the current company. */
729 assert(exec_as_spectator ? _current_company == COMPANY_SPECTATOR : cur_company.Verify());
730 cur_company.Restore();
731 }
732
733 /* If the test and execution can differ we have to check the
734 * return of the command. Otherwise we can check whether the
735 * test and execution have yielded the same result,
736 * i.e. cost and error state are the same. */
737 if (!test_and_exec_can_differ) {
738 assert(res.GetCost() == res2.GetCost() && res.Failed() == res2.Failed()); // sanity check
739 } else if (res2.Failed()) {
740 return_dcpi(res2);
741 }
742
743 /* If we're needing more money and we haven't done
744 * anything yet, ask for the money! */
745 if (_additional_cash_required != 0 && res2.GetCost() == 0) {
746 /* It could happen we removed rail, thus gained money, and deleted something else.
747 * So make sure the signal buffer is empty even in this case */
748 UpdateSignalsInBuffer();
749 SetDParam(0, _additional_cash_required);
750 return_dcpi(CommandCost(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY));
751 }
752
753 /* update last build coordinate of company. */
754 if (tile != 0) {
755 Company *c = Company::GetIfValid(_current_company);
756 if (c != NULL) c->last_build_coordinate = tile;
757 }
758
759 SubtractMoneyFromCompany(res2);
760
761 /* update signals if needed */
762 UpdateSignalsInBuffer();
763
764 return_dcpi(res2);
765 }
766 #undef return_dcpi
767
768
774 void CommandCost::AddCost(const CommandCost &ret)
775 {
776 this->AddCost(ret.cost);
777 if (this->success && !ret.success) {
778 this->message = ret.message;
779 this->success = false;
780 }
781 }
782
788 uint32 CommandCost::textref_stack[16];
789
795 void CommandCost::UseTextRefStack(const GRFFile *grffile, uint num_registers)
796 {
797 extern TemporaryStorageArray<int32, 0x110> _temp_store;
798
799 assert(num_registers < lengthof(textref_stack));
800 this->textref_stack_grffile = grffile;
801 this->textref_stack_size = num_registers;
802 for (uint i = 0; i < num_registers; i++) {
803 textref_stack[i] = _temp_store.GetValue(0x100 + i);
804 }
805 }