# command.cpp

Go to the documentation of this file.

    00001 /* $Id: command.cpp 15434 2009-02-09 21:20:05Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "openttd.h"
    00007 #include "landscape.h"
    00008 #include "tile_map.h"
    00009 #include "gui.h"
    00010 #include "command_func.h"
    00011 #include "network/network.h"
    00012 #include "genworld.h"
    00013 #include "newgrf_storage.h"
    00014 #include "strings_func.h"
    00015 #include "gfx_func.h"
    00016 #include "functions.h"
    00017 #include "town.h"
    00018 #include "date_func.h"
    00019 #include "debug.h"
    00020 #include "company_func.h"
    00021 #include "company_base.h"
    00022 #include "signal_func.h"
    00023
    00024 #include "table/strings.h"
    00025
    00026 StringID _error_message;
    00027
    00037 #define DEF_COMMAND(yyyy) CommandCost yyyy(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00038
    00039 DEF_COMMAND(CmdBuildRailroadTrack);
    00040 DEF_COMMAND(CmdRemoveRailroadTrack);
    00041 DEF_COMMAND(CmdBuildSingleRail);
    00042 DEF_COMMAND(CmdRemoveSingleRail);
    00043
    00044 DEF_COMMAND(CmdLandscapeClear);
    00045
    00046 DEF_COMMAND(CmdBuildBridge);
    00047
    00048 DEF_COMMAND(CmdBuildRailroadStation);
    00049 DEF_COMMAND(CmdRemoveFromRailroadStation);
    00050 DEF_COMMAND(CmdConvertRail);
    00051
    00052 DEF_COMMAND(CmdBuildSingleSignal);
    00053 DEF_COMMAND(CmdRemoveSingleSignal);
    00054
    00055 DEF_COMMAND(CmdTerraformLand);
    00056
    00057 DEF_COMMAND(CmdPurchaseLandArea);
    00058 DEF_COMMAND(CmdSellLandArea);
    00059
    00060 DEF_COMMAND(CmdBuildTunnel);
    00061
    00062 DEF_COMMAND(CmdBuildTrainDepot);
    00063 DEF_COMMAND(CmdBuildTrainWaypoint);
    00064 DEF_COMMAND(CmdRenameWaypoint);
    00065 DEF_COMMAND(CmdRemoveTrainWaypoint);
    00066
    00067 DEF_COMMAND(CmdBuildRoadStop);
    00068 DEF_COMMAND(CmdRemoveRoadStop);
    00069
    00070 DEF_COMMAND(CmdBuildLongRoad);
    00071 DEF_COMMAND(CmdRemoveLongRoad);
    00072 DEF_COMMAND(CmdBuildRoad);
    00073 DEF_COMMAND(CmdRemoveRoad);
    00074
    00075 DEF_COMMAND(CmdBuildRoadDepot);
    00076
    00077 DEF_COMMAND(CmdBuildAirport);
    00078
    00079 DEF_COMMAND(CmdBuildDock);
    00080
    00081 DEF_COMMAND(CmdBuildShipDepot);
    00082
    00083 DEF_COMMAND(CmdBuildBuoy);
    00084
    00085 DEF_COMMAND(CmdPlantTree);
    00086
    00087 DEF_COMMAND(CmdBuildRailVehicle);
    00088 DEF_COMMAND(CmdMoveRailVehicle);
    00089
    00090 DEF_COMMAND(CmdSellRailWagon);
    00091
    00092 DEF_COMMAND(CmdSendTrainToDepot);
    00093 DEF_COMMAND(CmdForceTrainProceed);
    00094 DEF_COMMAND(CmdReverseTrainDirection);
    00095
    00096 DEF_COMMAND(CmdModifyOrder);
    00097 DEF_COMMAND(CmdSkipToOrder);
    00098 DEF_COMMAND(CmdDeleteOrder);
    00099 DEF_COMMAND(CmdInsertOrder);
    00100 DEF_COMMAND(CmdChangeServiceInt);
    00101 DEF_COMMAND(CmdRestoreOrderIndex);
    00102
    00103 DEF_COMMAND(CmdBuildIndustry);
    00104
    00105 DEF_COMMAND(CmdBuildCompanyHQ);
    00106 DEF_COMMAND(CmdSetCompanyManagerFace);
    00107 DEF_COMMAND(CmdSetCompanyColour);
    00108
    00109 DEF_COMMAND(CmdIncreaseLoan);
    00110 DEF_COMMAND(CmdDecreaseLoan);
    00111
    00112 DEF_COMMAND(CmdWantEnginePreview);
    00113
    00114 DEF_COMMAND(CmdRenameVehicle);
    00115 DEF_COMMAND(CmdRenameEngine);
    00116
    00117 DEF_COMMAND(CmdRenameCompany);
    00118 DEF_COMMAND(CmdRenamePresident);
    00119
    00120 DEF_COMMAND(CmdRenameStation);
    00121
    00122 DEF_COMMAND(CmdSellAircraft);
    00123 DEF_COMMAND(CmdBuildAircraft);
    00124 DEF_COMMAND(CmdSendAircraftToHangar);
    00125 DEF_COMMAND(CmdRefitAircraft);
    00126
    00127 DEF_COMMAND(CmdPlaceSign);
    00128 DEF_COMMAND(CmdRenameSign);
    00129
    00130 DEF_COMMAND(CmdBuildRoadVeh);
    00131 DEF_COMMAND(CmdSellRoadVeh);
    00132 DEF_COMMAND(CmdSendRoadVehToDepot);
    00133 DEF_COMMAND(CmdTurnRoadVeh);
    00134 DEF_COMMAND(CmdRefitRoadVeh);
    00135
    00136 DEF_COMMAND(CmdPause);
    00137
    00138 DEF_COMMAND(CmdBuyShareInCompany);
    00139 DEF_COMMAND(CmdSellShareInCompany);
    00140 DEF_COMMAND(CmdBuyCompany);
    00141
    00142 DEF_COMMAND(CmdBuildTown);
    00143
    00144 DEF_COMMAND(CmdRenameTown);
    00145 DEF_COMMAND(CmdDoTownAction);
    00146
    00147 DEF_COMMAND(CmdChangeSetting);
    00148
    00149 DEF_COMMAND(CmdSellShip);
    00150 DEF_COMMAND(CmdBuildShip);
    00151 DEF_COMMAND(CmdSendShipToDepot);
    00152 DEF_COMMAND(CmdRefitShip);
    00153
    00154 DEF_COMMAND(CmdOrderRefit);
    00155 DEF_COMMAND(CmdCloneOrder);
    00156
    00157 DEF_COMMAND(CmdClearArea);
    00158
    00159 DEF_COMMAND(CmdGiveMoney);
    00160 DEF_COMMAND(CmdMoneyCheat);
    00161 DEF_COMMAND(CmdBuildCanal);
    00162 DEF_COMMAND(CmdBuildLock);
    00163
    00164 DEF_COMMAND(CmdCompanyCtrl);
    00165
    00166 DEF_COMMAND(CmdLevelLand);
    00167
    00168 DEF_COMMAND(CmdRefitRailVehicle);
    00169
    00170 DEF_COMMAND(CmdBuildSignalTrack);
    00171 DEF_COMMAND(CmdRemoveSignalTrack);
    00172
    00173 DEF_COMMAND(CmdSetAutoReplace);
    00174
    00175 DEF_COMMAND(CmdCloneVehicle);
    00176 DEF_COMMAND(CmdStartStopVehicle);
    00177 DEF_COMMAND(CmdMassStartStopVehicle);
    00178 DEF_COMMAND(CmdAutoreplaceVehicle);
    00179 DEF_COMMAND(CmdDepotSellAllVehicles);
    00180 DEF_COMMAND(CmdDepotMassAutoReplace);
    00181
    00182 DEF_COMMAND(CmdCreateGroup);
    00183 DEF_COMMAND(CmdRenameGroup);
    00184 DEF_COMMAND(CmdDeleteGroup);
    00185 DEF_COMMAND(CmdAddVehicleGroup);
    00186 DEF_COMMAND(CmdAddSharedVehicleGroup);
    00187 DEF_COMMAND(CmdRemoveAllVehiclesGroup);
    00188 DEF_COMMAND(CmdSetGroupReplaceProtection);
    00189
    00190 DEF_COMMAND(CmdMoveOrder);
    00191 DEF_COMMAND(CmdChangeTimetable);
    00192 DEF_COMMAND(CmdSetVehicleOnTime);
    00193 DEF_COMMAND(CmdAutofillTimetable);
    00194 #undef DEF_COMMAND
    00195
    00203 static const Command _command_proc_table[] = {
    00204   {CmdBuildRailroadTrack,   CMD_NO_WATER | CMD_AUTO}, /* CMD_BUILD_RAILROAD_TRACK */
    00205   {CmdRemoveRailroadTrack,                 CMD_AUTO}, /* CMD_REMOVE_RAILROAD_TRACK */
    00206   {CmdBuildSingleRail,      CMD_NO_WATER | CMD_AUTO}, /* CMD_BUILD_SINGLE_RAIL */
    00207   {CmdRemoveSingleRail,                    CMD_AUTO}, /* CMD_REMOVE_SINGLE_RAIL */
    00208   {CmdLandscapeClear,                             0}, /* CMD_LANDSCAPE_CLEAR */
    00209   {CmdBuildBridge,                         CMD_AUTO}, /* CMD_BUILD_BRIDGE */
    00210   {CmdBuildRailroadStation, CMD_NO_WATER | CMD_AUTO}, /* CMD_BUILD_RAILROAD_STATION */
    00211   {CmdBuildTrainDepot,      CMD_NO_WATER | CMD_AUTO}, /* CMD_BUILD_TRAIN_DEPOT */
    00212   {CmdBuildSingleSignal,                   CMD_AUTO}, /* CMD_BUILD_SIGNALS */
    00213   {CmdRemoveSingleSignal,                  CMD_AUTO}, /* CMD_REMOVE_SIGNALS */
    00214   {CmdTerraformLand,       CMD_ALL_TILES | CMD_AUTO}, /* CMD_TERRAFORM_LAND */
    00215   {CmdPurchaseLandArea,     CMD_NO_WATER | CMD_AUTO}, /* CMD_PURCHASE_LAND_AREA */
    00216   {CmdSellLandArea,                               0}, /* CMD_SELL_LAND_AREA */
    00217   {CmdBuildTunnel,                         CMD_AUTO}, /* CMD_BUILD_TUNNEL */
    00218   {CmdRemoveFromRailroadStation,                  0}, /* CMD_REMOVE_FROM_RAILROAD_STATION */
    00219   {CmdConvertRail,                                0}, /* CMD_CONVERT_RAILD */
    00220   {CmdBuildTrainWaypoint,                         0}, /* CMD_BUILD_TRAIN_WAYPOINT */
    00221   {CmdRenameWaypoint,                             0}, /* CMD_RENAME_WAYPOINT */
    00222   {CmdRemoveTrainWaypoint,                        0}, /* CMD_REMOVE_TRAIN_WAYPOINT */
    00223
    00224   {CmdBuildRoadStop,        CMD_NO_WATER | CMD_AUTO}, /* CMD_BUILD_ROAD_STOP */
    00225   {CmdRemoveRoadStop,                             0}, /* CMD_REMOVE_ROAD_STOP */
    00226   {CmdBuildLongRoad,        CMD_NO_WATER | CMD_AUTO}, /* CMD_BUILD_LONG_ROAD */
    00227   {CmdRemoveLongRoad,        CMD_NO_TEST | CMD_AUTO}, /* 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. */
    00228   {CmdBuildRoad,                                  0}, /* CMD_BUILD_ROAD */
    00229   {CmdRemoveRoad,                                 0}, /* CMD_REMOVE_ROAD */
    00230   {CmdBuildRoadDepot,       CMD_NO_WATER | CMD_AUTO}, /* CMD_BUILD_ROAD_DEPOT */
    00231
    00232   {CmdBuildAirport,         CMD_NO_WATER | CMD_AUTO}, /* CMD_BUILD_AIRPORT */
    00233   {CmdBuildDock,                           CMD_AUTO}, /* CMD_BUILD_DOCK */
    00234   {CmdBuildShipDepot,                      CMD_AUTO}, /* CMD_BUILD_SHIP_DEPOT */
    00235   {CmdBuildBuoy,                           CMD_AUTO}, /* CMD_BUILD_BUOY */
    00236   {CmdPlantTree,                           CMD_AUTO}, /* CMD_PLANT_TREE */
    00237   {CmdBuildRailVehicle,                           0}, /* CMD_BUILD_RAIL_VEHICLE */
    00238   {CmdMoveRailVehicle,                            0}, /* CMD_MOVE_RAIL_VEHICLE */
    00239
    00240   {CmdSellRailWagon,                              0}, /* CMD_SELL_RAIL_WAGON */
    00241   {CmdSendTrainToDepot,                           0}, /* CMD_SEND_TRAIN_TO_DEPOT */
    00242   {CmdForceTrainProceed,                          0}, /* CMD_FORCE_TRAIN_PROCEED */
    00243   {CmdReverseTrainDirection,                      0}, /* CMD_REVERSE_TRAIN_DIRECTION */
    00244
    00245   {CmdModifyOrder,                                0}, /* CMD_MODIFY_ORDER */
    00246   {CmdSkipToOrder,                                0}, /* CMD_SKIP_TO_ORDER */
    00247   {CmdDeleteOrder,                                0}, /* CMD_DELETE_ORDER */
    00248   {CmdInsertOrder,                                0}, /* CMD_INSERT_ORDER */
    00249
    00250   {CmdChangeServiceInt,                           0}, /* CMD_CHANGE_SERVICE_INT */
    00251
    00252   {CmdBuildIndustry,                              0}, /* CMD_BUILD_INDUSTRY */
    00253   {CmdBuildCompanyHQ,       CMD_NO_WATER | CMD_AUTO}, /* CMD_BUILD_COMPANY_HQ */
    00254   {CmdSetCompanyManagerFace,                      0}, /* CMD_SET_COMPANY_MANAGER_FACE */
    00255   {CmdSetCompanyColour,                           0}, /* CMD_SET_COMPANY_COLOUR */
    00256
    00257   {CmdIncreaseLoan,                               0}, /* CMD_INCREASE_LOAN */
    00258   {CmdDecreaseLoan,                               0}, /* CMD_DECREASE_LOAN */
    00259
    00260   {CmdWantEnginePreview,                          0}, /* CMD_WANT_ENGINE_PREVIEW */
    00261
    00262   {CmdRenameVehicle,                              0}, /* CMD_RENAME_VEHICLE */
    00263   {CmdRenameEngine,                               0}, /* CMD_RENAME_ENGINE */
    00264
    00265   {CmdRenameCompany,                              0}, /* CMD_RENAME_COMPANY */
    00266   {CmdRenamePresident,                            0}, /* CMD_RENAME_PRESIDENT */
    00267
    00268   {CmdRenameStation,                              0}, /* CMD_RENAME_STATION */
    00269
    00270   {CmdSellAircraft,                               0}, /* CMD_SELL_AIRCRAFT */
    00271
    00272   {CmdBuildAircraft,                              0}, /* CMD_BUILD_AIRCRAFT */
    00273   {CmdSendAircraftToHangar,                       0}, /* CMD_SEND_AIRCRAFT_TO_HANGAR */
    00274   {CmdRefitAircraft,                              0}, /* CMD_REFIT_AIRCRAFT */
    00275
    00276   {CmdPlaceSign,                                  0}, /* CMD_PLACE_SIGN */
    00277   {CmdRenameSign,                                 0}, /* CMD_RENAME_SIGN */
    00278
    00279   {CmdBuildRoadVeh,                               0}, /* CMD_BUILD_ROAD_VEH */
    00280   {CmdSellRoadVeh,                                0}, /* CMD_SELL_ROAD_VEH */
    00281   {CmdSendRoadVehToDepot,                         0}, /* CMD_SEND_ROADVEH_TO_DEPOT */
    00282   {CmdTurnRoadVeh,                                0}, /* CMD_TURN_ROADVEH */
    00283   {CmdRefitRoadVeh,                               0}, /* CMD_REFIT_ROAD_VEH */
    00284
    00285   {CmdPause,                             CMD_SERVER}, /* CMD_PAUSE */
    00286
    00287   {CmdBuyShareInCompany,                          0}, /* CMD_BUY_SHARE_IN_COMPANY */
    00288   {CmdSellShareInCompany,                         0}, /* CMD_SELL_SHARE_IN_COMPANY */
    00289   {CmdBuyCompany,                                 0}, /* CMD_BUY_COMANY */
    00290
    00291   {CmdBuildTown,                        CMD_OFFLINE}, /* CMD_BUILD_TOWN */
    00292   {CmdRenameTown,                        CMD_SERVER}, /* CMD_RENAME_TOWN */
    00293   {CmdDoTownAction,                               0}, /* CMD_DO_TOWN_ACTION */
    00294
    00295   {CmdSellShip,                                   0}, /* CMD_SELL_SHIP */
    00296   {CmdBuildShip,                                  0}, /* CMD_BUILD_SHIP */
    00297   {CmdSendShipToDepot,                            0}, /* CMD_SEND_SHIP_TO_DEPOT */
    00298   {CmdRefitShip,                                  0}, /* CMD_REFIT_SHIP */
    00299
    00300   {CmdOrderRefit,                                 0}, /* CMD_ORDER_REFIT */
    00301   {CmdCloneOrder,                                 0}, /* CMD_CLONE_ORDER */
    00302
    00303   {CmdClearArea,                        CMD_NO_TEST}, /* CMD_CLEAR_AREA; destroying multi-tile houses makes town rating differ between test and execution */
    00304
    00305   {CmdMoneyCheat,                       CMD_OFFLINE}, /* CMD_MONEY_CHEAT */
    00306   {CmdBuildCanal,                          CMD_AUTO}, /* CMD_BUILD_CANAL */
    00307   {CmdCompanyCtrl,                    CMD_SPECTATOR}, /* CMD_COMPANY_CTRL */
    00308
    00309   {CmdLevelLand, CMD_ALL_TILES | CMD_NO_TEST | CMD_AUTO}, /* CMD_LEVEL_LAND; test run might clear tiles multiple times, in execution that only happens once */
    00310
    00311   {CmdRefitRailVehicle,                           0}, /* CMD_REFIT_RAIL_VEHICLE */
    00312   {CmdRestoreOrderIndex,                          0}, /* CMD_RESTORE_ORDER_INDEX */
    00313   {CmdBuildLock,                           CMD_AUTO}, /* CMD_BUILD_LOCK */
    00314
    00315   {CmdBuildSignalTrack,                    CMD_AUTO}, /* CMD_BUILD_SIGNAL_TRACK */
    00316   {CmdRemoveSignalTrack,                   CMD_AUTO}, /* CMD_REMOVE_SIGNAL_TRACK */
    00317
    00318   {CmdGiveMoney,                                  0}, /* CMD_GIVE_MONEY */
    00319   {CmdChangeSetting,                     CMD_SERVER}, /* CMD_CHANGE_SETTING */
    00320   {CmdSetAutoReplace,                             0}, /* CMD_SET_AUTOREPLACE */
    00321   {CmdCloneVehicle,                     CMD_NO_TEST}, /* CMD_CLONE_VEHICLE; NewGRF callbacks influence building and refitting making it impossible to correctly estimate the cost */
    00322   {CmdStartStopVehicle,                           0}, /* CMD_START_STOP_VEHICLE */
    00323   {CmdMassStartStopVehicle,                       0}, /* CMD_MASS_START_STOP */
    00324   {CmdAutoreplaceVehicle,                         0}, /* CMD_AUTOREPLACE_VEHICLE */
    00325   {CmdDepotSellAllVehicles,                       0}, /* CMD_DEPOT_SELL_ALL_VEHICLES */
    00326   {CmdDepotMassAutoReplace,                       0}, /* CMD_DEPOT_MASS_AUTOREPLACE */
    00327   {CmdCreateGroup,                                0}, /* CMD_CREATE_GROUP */
    00328   {CmdDeleteGroup,                                0}, /* CMD_DELETE_GROUP */
    00329   {CmdRenameGroup,                                0}, /* CMD_RENAME_GROUP */
    00330   {CmdAddVehicleGroup,                            0}, /* CMD_ADD_VEHICLE_GROUP */
    00331   {CmdAddSharedVehicleGroup,                      0}, /* CMD_ADD_SHARE_VEHICLE_GROUP */
    00332   {CmdRemoveAllVehiclesGroup,                     0}, /* CMD_REMOVE_ALL_VEHICLES_GROUP */
    00333   {CmdSetGroupReplaceProtection,                  0}, /* CMD_SET_GROUP_REPLACE_PROTECTION */
    00334   {CmdMoveOrder,                                  0}, /* CMD_MOVE_ORDER */
    00335   {CmdChangeTimetable,                            0}, /* CMD_CHANGE_TIMETABLE */
    00336   {CmdSetVehicleOnTime,                           0}, /* CMD_SET_VEHICLE_ON_TIME */
    00337   {CmdAutofillTimetable,                          0}, /* CMD_AUTOFILL_TIMETABLE */
    00338 };
    00339
    00346 bool IsValidCommand(uint32 cmd)
    00347 {
    00348   cmd &= CMD_ID_MASK;
    00349
    00350   return
    00351     cmd < lengthof(_command_proc_table) &&
    00352     _command_proc_table[cmd].proc != NULL;
    00353 }
    00354
    00362 byte GetCommandFlags(uint32 cmd)
    00363 {
    00364   assert(IsValidCommand(cmd));
    00365
    00366   return _command_proc_table[cmd & CMD_ID_MASK].flags;
    00367 }
    00368
    00369 static int _docommand_recursive = 0;
    00370
    00379 CommandCost DoCommand(const CommandContainer *container, DoCommandFlag flags)
    00380 {
    00381   return DoCommand(container->tile, container->p1, container->p2, flags, container->cmd & CMD_ID_MASK, container->text);
    00382 }
    00383
    00396 CommandCost DoCommand(TileIndex tile, uint32 p1, uint32 p2, DoCommandFlag flags, uint32 cmd, const char *text)
    00397 {
    00398   CommandCost res;
    00399
    00400   /* Do not even think about executing out-of-bounds tile-commands */
    00401   if (tile != 0 && (tile >= MapSize() || (!IsValidTile(tile) && (flags & DC_ALL_TILES) == 0))) return CMD_ERROR;
    00402
    00403   CommandProc *proc = _command_proc_table[cmd].proc;
    00404
    00405   if (_docommand_recursive == 0) _error_message = INVALID_STRING_ID;
    00406
    00407   _docommand_recursive++;
    00408
    00409   /* only execute the test call if it's toplevel, or we're not execing. */
    00410   if (_docommand_recursive == 1 || !(flags & DC_EXEC) ) {
    00411     SetTownRatingTestMode(true);
    00412     res = proc(tile, flags & ~DC_EXEC, p1, p2, text);
    00413     SetTownRatingTestMode(false);
    00414     if (CmdFailed(res)) {
    00415       res.SetGlobalErrorMessage();
    00416       goto error;
    00417     }
    00418
    00419     if (_docommand_recursive == 1 &&
    00420         !(flags & DC_QUERY_COST) &&
    00421         !(flags & DC_BANKRUPT) &&
    00422         res.GetCost() != 0 &&
    00423         !CheckCompanyHasMoney(res)) {
    00424       goto error;
    00425     }
    00426
    00427     if (!(flags & DC_EXEC)) {
    00428       _docommand_recursive--;
    00429       return res;
    00430     }
    00431   }
    00432
    00433   /* Execute the command here. All cost-relevant functions set the expenses type
    00434    * themselves to the cost object at some point */
    00435   res = proc(tile, flags, p1, p2, text);
    00436   if (CmdFailed(res)) {
    00437     res.SetGlobalErrorMessage();
    00438 error:
    00439     _docommand_recursive--;
    00440     return CMD_ERROR;
    00441   }
    00442
    00443   /* if toplevel, subtract the money. */
    00444   if (--_docommand_recursive == 0 && !(flags & DC_BANKRUPT)) {
    00445     SubtractMoneyFromCompany(res);
    00446   }
    00447
    00448   return res;
    00449 }
    00450
    00458 Money GetAvailableMoneyForCommand()
    00459 {
    00460   CompanyID company = _current_company;
    00461   if (!IsValidCompanyID(company)) return INT64_MAX;
    00462   return GetCompany(company)->money;
    00463 }
    00464
    00471 bool DoCommandP(const CommandContainer *container, bool my_cmd)
    00472 {
    00473   return DoCommandP(container->tile, container->p1, container->p2, container->cmd, container->callback, container->text, my_cmd);
    00474 }
    00475
    00491 bool DoCommandP(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, bool my_cmd)
    00492 {
    00493   assert(_docommand_recursive == 0);
    00494
    00495   CommandCost res, res2;
    00496
    00497   int x = TileX(tile) * TILE_SIZE;
    00498   int y = TileY(tile) * TILE_SIZE;
    00499
    00500   _error_message = INVALID_STRING_ID;
    00501   StringID error_part1 = GB(cmd, 16, 16);
    00502   _additional_cash_required = 0;
    00503
    00504   CompanyID old_company = _current_company;
    00505
    00508   if (_current_company == COMPANY_SPECTATOR && !_network_server) {
    00509     if (my_cmd) ShowErrorMessage(_error_message, error_part1, x, y);
    00510     return false;
    00511   }
    00512
    00513   /* get pointer to command handler */
    00514   byte cmd_id = cmd & CMD_ID_MASK;
    00515   assert(cmd_id < lengthof(_command_proc_table));
    00516
    00517   CommandProc *proc = _command_proc_table[cmd_id].proc;
    00518   if (proc == NULL) return false;
    00519
    00520   /* Command flags are used internally */
    00521   uint cmd_flags = GetCommandFlags(cmd);
    00522   /* Flags get send to the DoCommand */
    00523   DoCommandFlag flags = CommandFlagsToDCFlags(cmd_flags);
    00524
    00525   /* Do not even think about executing out-of-bounds tile-commands */
    00526   if (tile != 0 && (tile >= MapSize() || (!IsValidTile(tile) && (cmd_flags & CMD_ALL_TILES) == 0))) return false;
    00527
    00528   /* If the server is a spectator, it may only do server commands! */
    00529   if (_current_company == COMPANY_SPECTATOR && (cmd_flags & (CMD_SPECTATOR | CMD_SERVER)) == 0) return false;
    00530
    00531   bool notest = (cmd_flags & CMD_NO_TEST) != 0;
    00532
    00533   _docommand_recursive = 1;
    00534
    00535   /* cost estimation only? */
    00536   if (!IsGeneratingWorld() &&
    00537       _shift_pressed &&
    00538       IsLocalCompany() &&
    00539       !(cmd & CMD_NETWORK_COMMAND) &&
    00540       cmd_id != CMD_PAUSE) {
    00541     /* estimate the cost. */
    00542     SetTownRatingTestMode(true);
    00543     res = proc(tile, flags, p1, p2, text);
    00544     SetTownRatingTestMode(false);
    00545     if (CmdFailed(res)) {
    00546       res.SetGlobalErrorMessage();
    00547       ShowErrorMessage(_error_message, error_part1, x, y);
    00548     } else {
    00549       ShowEstimatedCostOrIncome(res.GetCost(), x, y);
    00550     }
    00551
    00552     _docommand_recursive = 0;
    00553     ClearStorageChanges(false);
    00554     return false;
    00555   }
    00556
    00557
    00558   if (!((cmd & CMD_NO_TEST_IF_IN_NETWORK) && _networking)) {
    00559     /* first test if the command can be executed. */
    00560     SetTownRatingTestMode(true);
    00561     res = proc(tile, flags, p1, p2, text);
    00562     SetTownRatingTestMode(false);
    00563     assert(cmd_id == CMD_COMPANY_CTRL || old_company == _current_company);
    00564     if (CmdFailed(res)) {
    00565       res.SetGlobalErrorMessage();
    00566       goto show_error;
    00567     }
    00568     /* no money? Only check if notest is off */
    00569     if (!notest && res.GetCost() != 0 && !CheckCompanyHasMoney(res)) goto show_error;
    00570   }
    00571
    00572 #ifdef ENABLE_NETWORK
    00573
    00580   if (_networking && !(cmd & CMD_NETWORK_COMMAND)) {
    00581     CompanyID bck = _local_company;
    00582     if (_network_dedicated || (_network_server && bck == COMPANY_SPECTATOR)) _local_company = COMPANY_FIRST;
    00583     NetworkSend_Command(tile, p1, p2, cmd & ~CMD_FLAGS_MASK, callback, text);
    00584     if (_network_dedicated || (_network_server && bck == COMPANY_SPECTATOR)) _local_company = bck;
    00585     _docommand_recursive = 0;
    00586     ClearStorageChanges(false);
    00587     return true;
    00588   }
    00589 #endif /* ENABLE_NETWORK */
    00590   DEBUG(desync, 1, "cmd: %08x; %08x; %1x; %06x; %08x; %08x; %04x; %s\n", _date, _date_fract, (int)_current_company, tile, p1, p2, cmd & ~CMD_NETWORK_COMMAND, text);
    00591
    00592   /* update last build coordinate of company. */
    00593   if (tile != 0 && IsValidCompanyID(_current_company)) {
    00594     GetCompany(_current_company)->last_build_coordinate = tile;
    00595   }
    00596
    00597   /* Actually try and execute the command. If no cost-type is given
    00598    * use the construction one */
    00599   res2 = proc(tile, flags | DC_EXEC, p1, p2, text);
    00600
    00601   assert(cmd_id == CMD_COMPANY_CTRL || old_company == _current_company);
    00602
    00603   /* If notest is on, it means the result of the test can be different than
    00604    *  the real command.. so ignore the test */
    00605   if (!notest && !((cmd & CMD_NO_TEST_IF_IN_NETWORK) && _networking)) {
    00606     assert(res.GetCost() == res2.GetCost() && CmdFailed(res) == CmdFailed(res2)); // sanity check
    00607   } else {
    00608     if (CmdFailed(res2)) {
    00609       res.SetGlobalErrorMessage();
    00610       goto show_error;
    00611     }
    00612   }
    00613
    00614   SubtractMoneyFromCompany(res2);
    00615
    00616   /* update signals if needed */
    00617   UpdateSignalsInBuffer();
    00618
    00619   if (IsLocalCompany() && _game_mode != GM_EDITOR) {
    00620     if (res2.GetCost() != 0 && tile != 0) ShowCostOrIncomeAnimation(x, y, GetSlopeZ(x, y), res2.GetCost());
    00621     if (_additional_cash_required != 0) {
    00622       SetDParam(0, _additional_cash_required);
    00623       if (my_cmd) ShowErrorMessage(STR_0003_NOT_ENOUGH_CASH_REQUIRES, error_part1, x, y);
    00624       if (res2.GetCost() == 0) goto callb_err;
    00625     }
    00626   }
    00627
    00628   _docommand_recursive = 0;
    00629
    00630   if (callback) callback(true, tile, p1, p2);
    00631   ClearStorageChanges(true);
    00632   return true;
    00633
    00634 show_error:
    00635   /* show error message if the command fails? */
    00636   if (IsLocalCompany() && error_part1 != 0 && my_cmd) {
    00637     ShowErrorMessage(_error_message, error_part1, x, y);
    00638   }
    00639
    00640 callb_err:
    00641   _docommand_recursive = 0;
    00642
    00643   if (callback) callback(false, tile, p1, p2);
    00644   ClearStorageChanges(false);
    00645   return false;
    00646 }
    00647
    00648
    00649 CommandCost CommandCost::AddCost(CommandCost ret)
    00650 {
    00651   this->AddCost(ret.cost);
    00652   if (this->success && !ret.success) {
    00653     this->message = ret.message;
    00654     this->success = false;
    00655   }
    00656   return *this;
    00657 }