command.cpp

Go to the documentation of this file.
00001 /* $Id: command.cpp 26371 2014-02-23 22:03:08Z frosch $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "landscape.h"
00014 #include "error.h"
00015 #include "gui.h"
00016 #include "command_func.h"
00017 #include "network/network_type.h"
00018 #include "network/network.h"
00019 #include "genworld.h"
00020 #include "strings_func.h"
00021 #include "texteff.hpp"
00022 #include "town.h"
00023 #include "date_func.h"
00024 #include "company_func.h"
00025 #include "company_base.h"
00026 #include "signal_func.h"
00027 #include "core/backup_type.hpp"
00028 #include "object_base.h"
00029 
00030 #include "table/strings.h"
00031 
00032 CommandProc CmdBuildRailroadTrack;
00033 CommandProc CmdRemoveRailroadTrack;
00034 CommandProc CmdBuildSingleRail;
00035 CommandProc CmdRemoveSingleRail;
00036 
00037 CommandProc CmdLandscapeClear;
00038 
00039 CommandProc CmdBuildBridge;
00040 
00041 CommandProc CmdBuildRailStation;
00042 CommandProc CmdRemoveFromRailStation;
00043 CommandProc CmdConvertRail;
00044 
00045 CommandProc CmdBuildSingleSignal;
00046 CommandProc CmdRemoveSingleSignal;
00047 
00048 CommandProc CmdTerraformLand;
00049 
00050 CommandProc CmdBuildObject;
00051 CommandProc CmdSellLandArea;
00052 
00053 CommandProc CmdBuildTunnel;
00054 
00055 CommandProc CmdBuildTrainDepot;
00056 CommandProc CmdBuildRailWaypoint;
00057 CommandProc CmdRenameWaypoint;
00058 CommandProc CmdRemoveFromRailWaypoint;
00059 
00060 CommandProc CmdBuildRoadStop;
00061 CommandProc CmdRemoveRoadStop;
00062 
00063 CommandProc CmdBuildLongRoad;
00064 CommandProc CmdRemoveLongRoad;
00065 CommandProc CmdBuildRoad;
00066 
00067 CommandProc CmdBuildRoadDepot;
00068 
00069 CommandProc CmdBuildAirport;
00070 
00071 CommandProc CmdBuildDock;
00072 
00073 CommandProc CmdBuildShipDepot;
00074 
00075 CommandProc CmdBuildBuoy;
00076 
00077 CommandProc CmdPlantTree;
00078 
00079 CommandProc CmdMoveRailVehicle;
00080 
00081 CommandProc CmdBuildVehicle;
00082 CommandProc CmdSellVehicle;
00083 CommandProc CmdRefitVehicle;
00084 CommandProc CmdSendVehicleToDepot;
00085 
00086 CommandProc CmdForceTrainProceed;
00087 CommandProc CmdReverseTrainDirection;
00088 
00089 CommandProc CmdClearOrderBackup;
00090 CommandProc CmdModifyOrder;
00091 CommandProc CmdSkipToOrder;
00092 CommandProc CmdDeleteOrder;
00093 CommandProc CmdInsertOrder;
00094 CommandProc CmdChangeServiceInt;
00095 
00096 CommandProc CmdBuildIndustry;
00097 
00098 CommandProc CmdSetCompanyManagerFace;
00099 CommandProc CmdSetCompanyColour;
00100 
00101 CommandProc CmdIncreaseLoan;
00102 CommandProc CmdDecreaseLoan;
00103 
00104 CommandProc CmdWantEnginePreview;
00105 
00106 CommandProc CmdRenameVehicle;
00107 CommandProc CmdRenameEngine;
00108 
00109 CommandProc CmdRenameCompany;
00110 CommandProc CmdRenamePresident;
00111 
00112 CommandProc CmdRenameStation;
00113 CommandProc CmdRenameDepot;
00114 
00115 CommandProc CmdPlaceSign;
00116 CommandProc CmdRenameSign;
00117 
00118 CommandProc CmdTurnRoadVeh;
00119 
00120 CommandProc CmdPause;
00121 
00122 CommandProc CmdBuyShareInCompany;
00123 CommandProc CmdSellShareInCompany;
00124 CommandProc CmdBuyCompany;
00125 
00126 CommandProc CmdFoundTown;
00127 CommandProc CmdRenameTown;
00128 CommandProc CmdDoTownAction;
00129 CommandProc CmdTownGrowthRate;
00130 CommandProc CmdTownCargoGoal;
00131 CommandProc CmdTownSetText;
00132 CommandProc CmdExpandTown;
00133 CommandProc CmdDeleteTown;
00134 
00135 CommandProc CmdChangeSetting;
00136 CommandProc CmdChangeCompanySetting;
00137 
00138 CommandProc CmdOrderRefit;
00139 CommandProc CmdCloneOrder;
00140 
00141 CommandProc CmdClearArea;
00142 
00143 CommandProc CmdGiveMoney;
00144 CommandProc CmdMoneyCheat;
00145 CommandProc CmdChangeBankBalance;
00146 CommandProc CmdBuildCanal;
00147 CommandProc CmdBuildLock;
00148 
00149 CommandProc CmdCreateSubsidy;
00150 CommandProc CmdCompanyCtrl;
00151 CommandProc CmdCustomNewsItem;
00152 CommandProc CmdCreateGoal;
00153 CommandProc CmdRemoveGoal;
00154 CommandProc CmdSetGoalText;
00155 CommandProc CmdSetGoalProgress;
00156 CommandProc CmdSetGoalCompleted;
00157 CommandProc CmdGoalQuestion;
00158 CommandProc CmdGoalQuestionAnswer;
00159 CommandProc CmdCreateStoryPage;
00160 CommandProc CmdCreateStoryPageElement;
00161 CommandProc CmdUpdateStoryPageElement;
00162 CommandProc CmdSetStoryPageTitle;
00163 CommandProc CmdSetStoryPageDate;
00164 CommandProc CmdShowStoryPage;
00165 CommandProc CmdRemoveStoryPage;
00166 CommandProc CmdRemoveStoryPageElement;
00167 
00168 CommandProc CmdLevelLand;
00169 
00170 CommandProc CmdBuildSignalTrack;
00171 CommandProc CmdRemoveSignalTrack;
00172 
00173 CommandProc CmdSetAutoReplace;
00174 
00175 CommandProc CmdCloneVehicle;
00176 CommandProc CmdStartStopVehicle;
00177 CommandProc CmdMassStartStopVehicle;
00178 CommandProc CmdAutoreplaceVehicle;
00179 CommandProc CmdDepotSellAllVehicles;
00180 CommandProc CmdDepotMassAutoReplace;
00181 
00182 CommandProc CmdCreateGroup;
00183 CommandProc CmdRenameGroup;
00184 CommandProc CmdDeleteGroup;
00185 CommandProc CmdAddVehicleGroup;
00186 CommandProc CmdAddSharedVehicleGroup;
00187 CommandProc CmdRemoveAllVehiclesGroup;
00188 CommandProc CmdSetGroupReplaceProtection;
00189 
00190 CommandProc CmdMoveOrder;
00191 CommandProc CmdChangeTimetable;
00192 CommandProc CmdSetVehicleOnTime;
00193 CommandProc CmdAutofillTimetable;
00194 CommandProc CmdSetTimetableStart;
00195 
00196 CommandProc CmdOpenCloseAirport;
00197 
00198 #define DEF_CMD(proc, flags, type) {proc, #proc, (CommandFlags)flags, type}
00199 
00207 static const Command _command_proc_table[] = {
00208   DEF_CMD(CmdBuildRailroadTrack,       CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_RAILROAD_TRACK
00209   DEF_CMD(CmdRemoveRailroadTrack,                     CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_RAILROAD_TRACK
00210   DEF_CMD(CmdBuildSingleRail,          CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_SINGLE_RAIL
00211   DEF_CMD(CmdRemoveSingleRail,                        CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_SINGLE_RAIL
00212   DEF_CMD(CmdLandscapeClear,                                 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_LANDSCAPE_CLEAR
00213   DEF_CMD(CmdBuildBridge,  CMD_DEITY | CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_BRIDGE
00214   DEF_CMD(CmdBuildRailStation,         CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_RAIL_STATION
00215   DEF_CMD(CmdBuildTrainDepot,          CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_TRAIN_DEPOT
00216   DEF_CMD(CmdBuildSingleSignal,                       CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_SIGNALS
00217   DEF_CMD(CmdRemoveSingleSignal,                      CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_SIGNALS
00218   DEF_CMD(CmdTerraformLand,           CMD_ALL_TILES | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_TERRAFORM_LAND
00219   DEF_CMD(CmdBuildObject,              CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_OBJECT
00220   DEF_CMD(CmdBuildTunnel,                 CMD_DEITY | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_TUNNEL
00221   DEF_CMD(CmdRemoveFromRailStation,                          0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_FROM_RAIL_STATION
00222   DEF_CMD(CmdConvertRail,                                    0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_CONVERT_RAILD
00223   DEF_CMD(CmdBuildRailWaypoint,                              0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_RAIL_WAYPOINT
00224   DEF_CMD(CmdRenameWaypoint,                                 0, CMDT_OTHER_MANAGEMENT      ), // CMD_RENAME_WAYPOINT
00225   DEF_CMD(CmdRemoveFromRailWaypoint,                         0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_FROM_RAIL_WAYPOINT
00226 
00227   DEF_CMD(CmdBuildRoadStop,            CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_ROAD_STOP
00228   DEF_CMD(CmdRemoveRoadStop,                                 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_ROAD_STOP
00229   DEF_CMD(CmdBuildLongRoad,CMD_DEITY | CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_LONG_ROAD
00230   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.
00231   DEF_CMD(CmdBuildRoad,    CMD_DEITY | CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_ROAD
00232   DEF_CMD(CmdBuildRoadDepot,           CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_ROAD_DEPOT
00233 
00234   DEF_CMD(CmdBuildAirport,             CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_AIRPORT
00235   DEF_CMD(CmdBuildDock,                               CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_DOCK
00236   DEF_CMD(CmdBuildShipDepot,                          CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_SHIP_DEPOT
00237   DEF_CMD(CmdBuildBuoy,                               CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_BUOY
00238   DEF_CMD(CmdPlantTree,                               CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_PLANT_TREE
00239 
00240   DEF_CMD(CmdBuildVehicle,                       CMD_CLIENT_ID, CMDT_VEHICLE_CONSTRUCTION  ), // CMD_BUILD_VEHICLE
00241   DEF_CMD(CmdSellVehicle,                        CMD_CLIENT_ID, CMDT_VEHICLE_CONSTRUCTION  ), // CMD_SELL_VEHICLE
00242   DEF_CMD(CmdRefitVehicle,                                   0, CMDT_VEHICLE_CONSTRUCTION  ), // CMD_REFIT_VEHICLE
00243   DEF_CMD(CmdSendVehicleToDepot,                             0, CMDT_VEHICLE_MANAGEMENT    ), // CMD_SEND_VEHICLE_TO_DEPOT
00244 
00245   DEF_CMD(CmdMoveRailVehicle,                                0, CMDT_VEHICLE_CONSTRUCTION  ), // CMD_MOVE_RAIL_VEHICLE
00246   DEF_CMD(CmdForceTrainProceed,                              0, CMDT_VEHICLE_MANAGEMENT    ), // CMD_FORCE_TRAIN_PROCEED
00247   DEF_CMD(CmdReverseTrainDirection,                          0, CMDT_VEHICLE_MANAGEMENT    ), // CMD_REVERSE_TRAIN_DIRECTION
00248 
00249   DEF_CMD(CmdClearOrderBackup,                   CMD_CLIENT_ID, CMDT_ROUTE_MANAGEMENT      ), // CMD_CLEAR_ORDER_BACKUP
00250   DEF_CMD(CmdModifyOrder,                                    0, CMDT_ROUTE_MANAGEMENT      ), // CMD_MODIFY_ORDER
00251   DEF_CMD(CmdSkipToOrder,                                    0, CMDT_ROUTE_MANAGEMENT      ), // CMD_SKIP_TO_ORDER
00252   DEF_CMD(CmdDeleteOrder,                                    0, CMDT_ROUTE_MANAGEMENT      ), // CMD_DELETE_ORDER
00253   DEF_CMD(CmdInsertOrder,                                    0, CMDT_ROUTE_MANAGEMENT      ), // CMD_INSERT_ORDER
00254 
00255   DEF_CMD(CmdChangeServiceInt,                               0, CMDT_VEHICLE_MANAGEMENT    ), // CMD_CHANGE_SERVICE_INT
00256 
00257   DEF_CMD(CmdBuildIndustry,                          CMD_DEITY, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_INDUSTRY
00258   DEF_CMD(CmdSetCompanyManagerFace,                          0, CMDT_OTHER_MANAGEMENT      ), // CMD_SET_COMPANY_MANAGER_FACE
00259   DEF_CMD(CmdSetCompanyColour,                               0, CMDT_OTHER_MANAGEMENT      ), // CMD_SET_COMPANY_COLOUR
00260 
00261   DEF_CMD(CmdIncreaseLoan,                                   0, CMDT_MONEY_MANAGEMENT      ), // CMD_INCREASE_LOAN
00262   DEF_CMD(CmdDecreaseLoan,                                   0, CMDT_MONEY_MANAGEMENT      ), // CMD_DECREASE_LOAN
00263 
00264   DEF_CMD(CmdWantEnginePreview,                              0, CMDT_VEHICLE_MANAGEMENT    ), // CMD_WANT_ENGINE_PREVIEW
00265 
00266   DEF_CMD(CmdRenameVehicle,                                  0, CMDT_OTHER_MANAGEMENT      ), // CMD_RENAME_VEHICLE
00267   DEF_CMD(CmdRenameEngine,                          CMD_SERVER, CMDT_OTHER_MANAGEMENT      ), // CMD_RENAME_ENGINE
00268 
00269   DEF_CMD(CmdRenameCompany,                                  0, CMDT_OTHER_MANAGEMENT      ), // CMD_RENAME_COMPANY
00270   DEF_CMD(CmdRenamePresident,                                0, CMDT_OTHER_MANAGEMENT      ), // CMD_RENAME_PRESIDENT
00271 
00272   DEF_CMD(CmdRenameStation,                                  0, CMDT_OTHER_MANAGEMENT      ), // CMD_RENAME_STATION
00273   DEF_CMD(CmdRenameDepot,                                    0, CMDT_OTHER_MANAGEMENT      ), // CMD_RENAME_DEPOT
00274 
00275   DEF_CMD(CmdPlaceSign,                              CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_PLACE_SIGN
00276   DEF_CMD(CmdRenameSign,                             CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_RENAME_SIGN
00277 
00278   DEF_CMD(CmdTurnRoadVeh,                                    0, CMDT_VEHICLE_MANAGEMENT    ), // CMD_TURN_ROADVEH
00279 
00280   DEF_CMD(CmdPause,                                 CMD_SERVER, CMDT_SERVER_SETTING        ), // CMD_PAUSE
00281 
00282   DEF_CMD(CmdBuyShareInCompany,                              0, CMDT_MONEY_MANAGEMENT      ), // CMD_BUY_SHARE_IN_COMPANY
00283   DEF_CMD(CmdSellShareInCompany,                             0, CMDT_MONEY_MANAGEMENT      ), // CMD_SELL_SHARE_IN_COMPANY
00284   DEF_CMD(CmdBuyCompany,                                     0, CMDT_MONEY_MANAGEMENT      ), // CMD_BUY_COMANY
00285 
00286   DEF_CMD(CmdFoundTown,                CMD_DEITY | CMD_NO_TEST, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_FOUND_TOWN; founding random town can fail only in exec run
00287   DEF_CMD(CmdRenameTown,                CMD_DEITY | CMD_SERVER, CMDT_OTHER_MANAGEMENT      ), // CMD_RENAME_TOWN
00288   DEF_CMD(CmdDoTownAction,                                   0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_DO_TOWN_ACTION
00289   DEF_CMD(CmdTownCargoGoal,                          CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_TOWN_CARGO_GOAL
00290   DEF_CMD(CmdTownGrowthRate,                         CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_TOWN_GROWTH_RATE
00291   DEF_CMD(CmdTownSetText,             CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_TOWN_SET_TEXT
00292   DEF_CMD(CmdExpandTown,                             CMD_DEITY, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_EXPAND_TOWN
00293   DEF_CMD(CmdDeleteTown,                           CMD_OFFLINE, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_DELETE_TOWN
00294 
00295   DEF_CMD(CmdOrderRefit,                                     0, CMDT_ROUTE_MANAGEMENT      ), // CMD_ORDER_REFIT
00296   DEF_CMD(CmdCloneOrder,                                     0, CMDT_ROUTE_MANAGEMENT      ), // CMD_CLONE_ORDER
00297 
00298   DEF_CMD(CmdClearArea,                            CMD_NO_TEST, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_CLEAR_AREA; destroying multi-tile houses makes town rating differ between test and execution
00299 
00300   DEF_CMD(CmdMoneyCheat,                           CMD_OFFLINE, CMDT_CHEAT                 ), // CMD_MONEY_CHEAT
00301   DEF_CMD(CmdChangeBankBalance,                      CMD_DEITY, CMDT_MONEY_MANAGEMENT      ), // CMD_CHANGE_BANK_BALANCE
00302   DEF_CMD(CmdBuildCanal,                              CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_CANAL
00303   DEF_CMD(CmdCreateSubsidy,                          CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_CREATE_SUBSIDY
00304   DEF_CMD(CmdCompanyCtrl,        CMD_SPECTATOR | CMD_CLIENT_ID, CMDT_SERVER_SETTING        ), // CMD_COMPANY_CTRL
00305   DEF_CMD(CmdCustomNewsItem,          CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_CUSTOM_NEWS_ITEM
00306   DEF_CMD(CmdCreateGoal,              CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_CREATE_GOAL
00307   DEF_CMD(CmdRemoveGoal,                             CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_REMOVE_GOAL
00308   DEF_CMD(CmdSetGoalText,             CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_SET_GOAL_TEXT
00309   DEF_CMD(CmdSetGoalProgress,         CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_SET_GOAL_PROGRESS
00310   DEF_CMD(CmdSetGoalCompleted,        CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_SET_GOAL_COMPLETED
00311   DEF_CMD(CmdGoalQuestion,            CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_GOAL_QUESTION
00312   DEF_CMD(CmdGoalQuestionAnswer,                     CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_GOAL_QUESTION_ANSWER
00313   DEF_CMD(CmdCreateStoryPage,         CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_CREATE_STORY_PAGE
00314   DEF_CMD(CmdCreateStoryPageElement,  CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_CREATE_STORY_PAGE_ELEMENT
00315   DEF_CMD(CmdUpdateStoryPageElement,  CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_UPDATE_STORY_PAGE_ELEMENT
00316   DEF_CMD(CmdSetStoryPageTitle,       CMD_STR_CTRL | CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_SET_STORY_PAGE_TITLE
00317   DEF_CMD(CmdSetStoryPageDate,                       CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_SET_STORY_PAGE_DATE
00318   DEF_CMD(CmdShowStoryPage,                          CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_SHOW_STORY_PAGE
00319   DEF_CMD(CmdRemoveStoryPage,                        CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_REMOVE_STORY_PAGE
00320   DEF_CMD(CmdRemoveStoryPageElement,                 CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_REMOVE_STORY_ELEMENT_PAGE
00321 
00322   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
00323 
00324   DEF_CMD(CmdBuildLock,                               CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_LOCK
00325 
00326   DEF_CMD(CmdBuildSignalTrack,                        CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_SIGNAL_TRACK
00327   DEF_CMD(CmdRemoveSignalTrack,                       CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_SIGNAL_TRACK
00328 
00329   DEF_CMD(CmdGiveMoney,                                      0, CMDT_MONEY_MANAGEMENT      ), // CMD_GIVE_MONEY
00330   DEF_CMD(CmdChangeSetting,                         CMD_SERVER, CMDT_SERVER_SETTING        ), // CMD_CHANGE_SETTING
00331   DEF_CMD(CmdChangeCompanySetting,                           0, CMDT_COMPANY_SETTING       ), // CMD_CHANGE_COMPANY_SETTING
00332   DEF_CMD(CmdSetAutoReplace,                                 0, CMDT_VEHICLE_MANAGEMENT    ), // CMD_SET_AUTOREPLACE
00333   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
00334   DEF_CMD(CmdStartStopVehicle,                               0, CMDT_VEHICLE_MANAGEMENT    ), // CMD_START_STOP_VEHICLE
00335   DEF_CMD(CmdMassStartStopVehicle,                           0, CMDT_VEHICLE_MANAGEMENT    ), // CMD_MASS_START_STOP
00336   DEF_CMD(CmdAutoreplaceVehicle,                             0, CMDT_VEHICLE_MANAGEMENT    ), // CMD_AUTOREPLACE_VEHICLE
00337   DEF_CMD(CmdDepotSellAllVehicles,                           0, CMDT_VEHICLE_CONSTRUCTION  ), // CMD_DEPOT_SELL_ALL_VEHICLES
00338   DEF_CMD(CmdDepotMassAutoReplace,                           0, CMDT_VEHICLE_CONSTRUCTION  ), // CMD_DEPOT_MASS_AUTOREPLACE
00339   DEF_CMD(CmdCreateGroup,                                    0, CMDT_ROUTE_MANAGEMENT      ), // CMD_CREATE_GROUP
00340   DEF_CMD(CmdDeleteGroup,                                    0, CMDT_ROUTE_MANAGEMENT      ), // CMD_DELETE_GROUP
00341   DEF_CMD(CmdRenameGroup,                                    0, CMDT_OTHER_MANAGEMENT      ), // CMD_RENAME_GROUP
00342   DEF_CMD(CmdAddVehicleGroup,                                0, CMDT_ROUTE_MANAGEMENT      ), // CMD_ADD_VEHICLE_GROUP
00343   DEF_CMD(CmdAddSharedVehicleGroup,                          0, CMDT_ROUTE_MANAGEMENT      ), // CMD_ADD_SHARE_VEHICLE_GROUP
00344   DEF_CMD(CmdRemoveAllVehiclesGroup,                         0, CMDT_ROUTE_MANAGEMENT      ), // CMD_REMOVE_ALL_VEHICLES_GROUP
00345   DEF_CMD(CmdSetGroupReplaceProtection,                      0, CMDT_ROUTE_MANAGEMENT      ), // CMD_SET_GROUP_REPLACE_PROTECTION
00346   DEF_CMD(CmdMoveOrder,                                      0, CMDT_ROUTE_MANAGEMENT      ), // CMD_MOVE_ORDER
00347   DEF_CMD(CmdChangeTimetable,                                0, CMDT_ROUTE_MANAGEMENT      ), // CMD_CHANGE_TIMETABLE
00348   DEF_CMD(CmdSetVehicleOnTime,                               0, CMDT_ROUTE_MANAGEMENT      ), // CMD_SET_VEHICLE_ON_TIME
00349   DEF_CMD(CmdAutofillTimetable,                              0, CMDT_ROUTE_MANAGEMENT      ), // CMD_AUTOFILL_TIMETABLE
00350   DEF_CMD(CmdSetTimetableStart,                              0, CMDT_ROUTE_MANAGEMENT      ), // CMD_SET_TIMETABLE_START
00351 
00352   DEF_CMD(CmdOpenCloseAirport,                               0, CMDT_ROUTE_MANAGEMENT      ), // CMD_OPEN_CLOSE_AIRPORT
00353 };
00354 
00361 bool IsValidCommand(uint32 cmd)
00362 {
00363   cmd &= CMD_ID_MASK;
00364 
00365   return cmd < lengthof(_command_proc_table) && _command_proc_table[cmd].proc != NULL;
00366 }
00367 
00375 CommandFlags GetCommandFlags(uint32 cmd)
00376 {
00377   assert(IsValidCommand(cmd));
00378 
00379   return _command_proc_table[cmd & CMD_ID_MASK].flags;
00380 }
00381 
00389 const char *GetCommandName(uint32 cmd)
00390 {
00391   assert(IsValidCommand(cmd));
00392 
00393   return _command_proc_table[cmd & CMD_ID_MASK].name;
00394 }
00395 
00401 bool IsCommandAllowedWhilePaused(uint32 cmd)
00402 {
00403   /* Lookup table for the command types that are allowed for a given pause level setting. */
00404   static const int command_type_lookup[] = {
00405     CMDPL_ALL_ACTIONS,     
00406     CMDPL_NO_LANDSCAPING,  
00407     CMDPL_NO_LANDSCAPING,  
00408     CMDPL_NO_CONSTRUCTION, 
00409     CMDPL_NO_CONSTRUCTION, 
00410     CMDPL_NO_CONSTRUCTION, 
00411     CMDPL_NO_CONSTRUCTION, 
00412     CMDPL_NO_ACTIONS,      
00413     CMDPL_NO_ACTIONS,      
00414   };
00415   assert_compile(lengthof(command_type_lookup) == CMDT_END);
00416 
00417   assert(IsValidCommand(cmd));
00418   return _game_mode == GM_EDITOR || command_type_lookup[_command_proc_table[cmd & CMD_ID_MASK].type] <= _settings_game.construction.command_pause_level;
00419 }
00420 
00421 
00422 static int _docommand_recursive = 0;
00423 
00432 CommandCost DoCommand(const CommandContainer *container, DoCommandFlag flags)
00433 {
00434   return DoCommand(container->tile, container->p1, container->p2, flags, container->cmd & CMD_ID_MASK, container->text);
00435 }
00436 
00450 CommandCost DoCommand(TileIndex tile, uint32 p1, uint32 p2, DoCommandFlag flags, uint32 cmd, const char *text)
00451 {
00452   CommandCost res;
00453 
00454   /* Do not even think about executing out-of-bounds tile-commands */
00455   if (tile != 0 && (tile >= MapSize() || (!IsValidTile(tile) && (flags & DC_ALL_TILES) == 0))) return CMD_ERROR;
00456 
00457   /* Chop of any CMD_MSG or other flags; we don't need those here */
00458   CommandProc *proc = _command_proc_table[cmd & CMD_ID_MASK].proc;
00459 
00460   _docommand_recursive++;
00461 
00462   /* only execute the test call if it's toplevel, or we're not execing. */
00463   if (_docommand_recursive == 1 || !(flags & DC_EXEC) ) {
00464     if (_docommand_recursive == 1) _cleared_object_areas.Clear();
00465     SetTownRatingTestMode(true);
00466     res = proc(tile, flags & ~DC_EXEC, p1, p2, text);
00467     SetTownRatingTestMode(false);
00468     if (res.Failed()) {
00469       goto error;
00470     }
00471 
00472     if (_docommand_recursive == 1 &&
00473         !(flags & DC_QUERY_COST) &&
00474         !(flags & DC_BANKRUPT) &&
00475         !CheckCompanyHasMoney(res)) { // CheckCompanyHasMoney() modifies 'res' to an error if it fails.
00476       goto error;
00477     }
00478 
00479     if (!(flags & DC_EXEC)) {
00480       _docommand_recursive--;
00481       return res;
00482     }
00483   }
00484 
00485   /* Execute the command here. All cost-relevant functions set the expenses type
00486    * themselves to the cost object at some point */
00487   if (_docommand_recursive == 1) _cleared_object_areas.Clear();
00488   res = proc(tile, flags, p1, p2, text);
00489   if (res.Failed()) {
00490 error:
00491     _docommand_recursive--;
00492     return res;
00493   }
00494 
00495   /* if toplevel, subtract the money. */
00496   if (--_docommand_recursive == 0 && !(flags & DC_BANKRUPT)) {
00497     SubtractMoneyFromCompany(res);
00498   }
00499 
00500   return res;
00501 }
00502 
00510 Money GetAvailableMoneyForCommand()
00511 {
00512   CompanyID company = _current_company;
00513   if (!Company::IsValidID(company)) return INT64_MAX;
00514   return Company::Get(company)->money;
00515 }
00516 
00523 bool DoCommandP(const CommandContainer *container, bool my_cmd)
00524 {
00525   return DoCommandP(container->tile, container->p1, container->p2, container->cmd, container->callback, container->text, my_cmd);
00526 }
00527 
00543 bool DoCommandP(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, bool my_cmd)
00544 {
00545   /* Cost estimation is generally only done when the
00546    * local user presses shift while doing somthing.
00547    * However, in case of incoming network commands,
00548    * map generation or the pause button we do want
00549    * to execute. */
00550   bool estimate_only = _shift_pressed && IsLocalCompany() &&
00551       !_generating_world &&
00552       !(cmd & CMD_NETWORK_COMMAND) &&
00553       (cmd & CMD_ID_MASK) != CMD_PAUSE;
00554 
00555   /* We're only sending the command, so don't do
00556    * fancy things for 'success'. */
00557   bool only_sending = _networking && !(cmd & CMD_NETWORK_COMMAND);
00558 
00559   /* Where to show the message? */
00560   int x = TileX(tile) * TILE_SIZE;
00561   int y = TileY(tile) * TILE_SIZE;
00562 
00563   if (_pause_mode != PM_UNPAUSED && !IsCommandAllowedWhilePaused(cmd)) {
00564     ShowErrorMessage(GB(cmd, 16, 16), STR_ERROR_NOT_ALLOWED_WHILE_PAUSED, WL_INFO, x, y);
00565     return false;
00566   }
00567 
00568 #ifdef ENABLE_NETWORK
00569   /* Only set p2 when the command does not come from the network. */
00570   if (!(cmd & CMD_NETWORK_COMMAND) && GetCommandFlags(cmd) & CMD_CLIENT_ID && p2 == 0) p2 = CLIENT_ID_SERVER;
00571 #endif
00572 
00573   CommandCost res = DoCommandPInternal(tile, p1, p2, cmd, callback, text, my_cmd, estimate_only);
00574   if (res.Failed()) {
00575     /* Only show the error when it's for us. */
00576     StringID error_part1 = GB(cmd, 16, 16);
00577     if (estimate_only || (IsLocalCompany() && error_part1 != 0 && my_cmd)) {
00578       ShowErrorMessage(error_part1, res.GetErrorMessage(), WL_INFO, x, y, res.GetTextRefStackGRF(), res.GetTextRefStackSize(), res.GetTextRefStack());
00579     }
00580   } else if (estimate_only) {
00581     ShowEstimatedCostOrIncome(res.GetCost(), x, y);
00582   } else if (!only_sending && res.GetCost() != 0 && tile != 0 && IsLocalCompany() && _game_mode != GM_EDITOR) {
00583     /* Only show the cost animation when we did actually
00584      * execute the command, i.e. we're not sending it to
00585      * the server, when it has cost the local company
00586      * something. Furthermore in the editor there is no
00587      * concept of cost, so don't show it there either. */
00588     ShowCostOrIncomeAnimation(x, y, GetSlopePixelZ(x, y), res.GetCost());
00589   }
00590 
00591   if (!estimate_only && !only_sending && callback != NULL) {
00592     callback(res, tile, p1, p2);
00593   }
00594 
00595   return res.Succeeded();
00596 }
00597 
00598 
00604 #define return_dcpi(cmd) { _docommand_recursive = 0; return cmd; }
00605 
00619 CommandCost DoCommandPInternal(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, bool my_cmd, bool estimate_only)
00620 {
00621   /* Prevent recursion; it gives a mess over the network */
00622   assert(_docommand_recursive == 0);
00623   _docommand_recursive = 1;
00624 
00625   /* Reset the state. */
00626   _additional_cash_required = 0;
00627 
00628   /* Get pointer to command handler */
00629   byte cmd_id = cmd & CMD_ID_MASK;
00630   assert(cmd_id < lengthof(_command_proc_table));
00631 
00632   CommandProc *proc = _command_proc_table[cmd_id].proc;
00633   /* Shouldn't happen, but you never know when someone adds
00634    * NULLs to the _command_proc_table. */
00635   assert(proc != NULL);
00636 
00637   /* Command flags are used internally */
00638   CommandFlags cmd_flags = GetCommandFlags(cmd);
00639   /* Flags get send to the DoCommand */
00640   DoCommandFlag flags = CommandFlagsToDCFlags(cmd_flags);
00641 
00642 #ifdef ENABLE_NETWORK
00643   /* Make sure p2 is properly set to a ClientID. */
00644   assert(!(cmd_flags & CMD_CLIENT_ID) || p2 != 0);
00645 #endif
00646 
00647   /* Do not even think about executing out-of-bounds tile-commands */
00648   if (tile != 0 && (tile >= MapSize() || (!IsValidTile(tile) && (cmd_flags & CMD_ALL_TILES) == 0))) return_dcpi(CMD_ERROR);
00649 
00650   /* Always execute server and spectator commands as spectator */
00651   bool exec_as_spectator = (cmd_flags & (CMD_SPECTATOR | CMD_SERVER)) != 0;
00652 
00653   /* If the company isn't valid it may only do server command or start a new company!
00654    * The server will ditch any server commands a client sends to it, so effectively
00655    * this guards the server from executing functions for an invalid company. */
00656   if (_game_mode == GM_NORMAL && !exec_as_spectator && !Company::IsValidID(_current_company) && !(_current_company == OWNER_DEITY && (cmd_flags & CMD_DEITY) != 0)) {
00657     return_dcpi(CMD_ERROR);
00658   }
00659 
00660   Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
00661   if (exec_as_spectator) cur_company.Change(COMPANY_SPECTATOR);
00662 
00663   bool test_and_exec_can_differ = (cmd_flags & CMD_NO_TEST) != 0;
00664 
00665   /* Test the command. */
00666   _cleared_object_areas.Clear();
00667   SetTownRatingTestMode(true);
00668   BasePersistentStorageArray::SwitchMode(PSM_ENTER_TESTMODE);
00669   CommandCost res = proc(tile, flags, p1, p2, text);
00670   BasePersistentStorageArray::SwitchMode(PSM_LEAVE_TESTMODE);
00671   SetTownRatingTestMode(false);
00672 
00673   /* Make sure we're not messing things up here. */
00674   assert(exec_as_spectator ? _current_company == COMPANY_SPECTATOR : cur_company.Verify());
00675 
00676   /* If the command fails, we're doing an estimate
00677    * or the player does not have enough money
00678    * (unless it's a command where the test and
00679    * execution phase might return different costs)
00680    * we bail out here. */
00681   if (res.Failed() || estimate_only ||
00682       (!test_and_exec_can_differ && !CheckCompanyHasMoney(res))) {
00683     if (!_networking || _generating_world || (cmd & CMD_NETWORK_COMMAND) != 0) {
00684       /* Log the failed command as well. Just to be able to be find
00685        * causes of desyncs due to bad command test implementations. */
00686       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));
00687     }
00688     cur_company.Restore();
00689     return_dcpi(res);
00690   }
00691 
00692 #ifdef ENABLE_NETWORK
00693   /*
00694    * If we are in network, and the command is not from the network
00695    * send it to the command-queue and abort execution
00696    */
00697   if (_networking && !_generating_world && !(cmd & CMD_NETWORK_COMMAND)) {
00698     NetworkSendCommand(tile, p1, p2, cmd & ~CMD_FLAGS_MASK, callback, text, _current_company);
00699     cur_company.Restore();
00700 
00701     /* Don't return anything special here; no error, no costs.
00702      * This way it's not handled by DoCommand and only the
00703      * actual execution of the command causes messages. Also
00704      * reset the storages as we've not executed the command. */
00705     return_dcpi(CommandCost());
00706   }
00707 #endif /* ENABLE_NETWORK */
00708   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));
00709 
00710   /* Actually try and execute the command. If no cost-type is given
00711    * use the construction one */
00712   _cleared_object_areas.Clear();
00713   BasePersistentStorageArray::SwitchMode(PSM_ENTER_COMMAND);
00714   CommandCost res2 = proc(tile, flags | DC_EXEC, p1, p2, text);
00715   BasePersistentStorageArray::SwitchMode(PSM_LEAVE_COMMAND);
00716 
00717   if (cmd_id == CMD_COMPANY_CTRL) {
00718     cur_company.Trash();
00719     /* We are a new company                  -> Switch to new local company.
00720      * We were closed down                   -> Switch to spectator
00721      * Some other company opened/closed down -> The outside function will switch back */
00722     _current_company = _local_company;
00723   } else {
00724     /* Make sure nothing bad happened, like changing the current company. */
00725     assert(exec_as_spectator ? _current_company == COMPANY_SPECTATOR : cur_company.Verify());
00726     cur_company.Restore();
00727   }
00728 
00729   /* If the test and execution can differ we have to check the
00730    * return of the command. Otherwise we can check whether the
00731    * test and execution have yielded the same result,
00732    * i.e. cost and error state are the same. */
00733   if (!test_and_exec_can_differ) {
00734     assert(res.GetCost() == res2.GetCost() && res.Failed() == res2.Failed()); // sanity check
00735   } else if (res2.Failed()) {
00736     return_dcpi(res2);
00737   }
00738 
00739   /* If we're needing more money and we haven't done
00740    * anything yet, ask for the money! */
00741   if (_additional_cash_required != 0 && res2.GetCost() == 0) {
00742     /* It could happen we removed rail, thus gained money, and deleted something else.
00743      * So make sure the signal buffer is empty even in this case */
00744     UpdateSignalsInBuffer();
00745     SetDParam(0, _additional_cash_required);
00746     return_dcpi(CommandCost(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY));
00747   }
00748 
00749   /* update last build coordinate of company. */
00750   if (tile != 0) {
00751     Company *c = Company::GetIfValid(_current_company);
00752     if (c != NULL) c->last_build_coordinate = tile;
00753   }
00754 
00755   SubtractMoneyFromCompany(res2);
00756 
00757   /* update signals if needed */
00758   UpdateSignalsInBuffer();
00759 
00760   return_dcpi(res2);
00761 }
00762 #undef return_dcpi
00763 
00764 
00770 void CommandCost::AddCost(const CommandCost &ret)
00771 {
00772   this->AddCost(ret.cost);
00773   if (this->success && !ret.success) {
00774     this->message = ret.message;
00775     this->success = false;
00776   }
00777 }
00778 
00784 uint32 CommandCost::textref_stack[16];
00785 
00791 void CommandCost::UseTextRefStack(const GRFFile *grffile, uint num_registers)
00792 {
00793   extern TemporaryStorageArray<int32, 0x110> _temp_store;
00794 
00795   assert(num_registers < lengthof(textref_stack));
00796   this->textref_stack_grffile = grffile;
00797   this->textref_stack_size = num_registers;
00798   for (uint i = 0; i < num_registers; i++) {
00799     textref_stack[i] = _temp_store.GetValue(0x100 + i);
00800   }
00801 }