00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "console_internal.h"
00014 #include "debug.h"
00015 #include "engine_func.h"
00016 #include "landscape.h"
00017 #include "saveload/saveload.h"
00018 #include "network/network.h"
00019 #include "network/network_func.h"
00020 #include "network/network_base.h"
00021 #include "network/network_admin.h"
00022 #include "command_func.h"
00023 #include "settings_func.h"
00024 #include "fios.h"
00025 #include "fileio_func.h"
00026 #include "screenshot.h"
00027 #include "genworld.h"
00028 #include "strings_func.h"
00029 #include "viewport_func.h"
00030 #include "window_func.h"
00031 #include "date_func.h"
00032 #include "vehicle_func.h"
00033 #include "company_func.h"
00034 #include "gamelog.h"
00035 #include "ai/ai.hpp"
00036 #include "ai/ai_config.hpp"
00037 #include "newgrf.h"
00038 #include "console_func.h"
00039
00040 #ifdef ENABLE_NETWORK
00041 #include "table/strings.h"
00042 #endif
00043
00044
00045 static bool _script_running;
00046
00047
00048 #define DEF_CONSOLE_CMD(function) static bool function(byte argc, char *argv[])
00049 #define DEF_CONSOLE_HOOK(function) static ConsoleHookResult function(bool echo)
00050
00051
00052
00053
00054
00055
00056 #ifdef ENABLE_NETWORK
00057
00058 static inline bool NetworkAvailable(bool echo)
00059 {
00060 if (!_network_available) {
00061 if (echo) IConsoleError("You cannot use this command because there is no network available.");
00062 return false;
00063 }
00064 return true;
00065 }
00066
00067 DEF_CONSOLE_HOOK(ConHookServerOnly)
00068 {
00069 if (!NetworkAvailable(echo)) return CHR_DISALLOW;
00070
00071 if (!_network_server) {
00072 if (echo) IConsoleError("This command is only available to a network server.");
00073 return CHR_DISALLOW;
00074 }
00075 return CHR_ALLOW;
00076 }
00077
00078 DEF_CONSOLE_HOOK(ConHookClientOnly)
00079 {
00080 if (!NetworkAvailable(echo)) return CHR_DISALLOW;
00081
00082 if (_network_server) {
00083 if (echo) IConsoleError("This command is not available to a network server.");
00084 return CHR_DISALLOW;
00085 }
00086 return CHR_ALLOW;
00087 }
00088
00089 DEF_CONSOLE_HOOK(ConHookNeedNetwork)
00090 {
00091 if (!NetworkAvailable(echo)) return CHR_DISALLOW;
00092
00093 if (!_networking) {
00094 if (echo) IConsoleError("Not connected. This command is only available in multiplayer.");
00095 return CHR_DISALLOW;
00096 }
00097 return CHR_ALLOW;
00098 }
00099
00100 DEF_CONSOLE_HOOK(ConHookNoNetwork)
00101 {
00102 if (_networking) {
00103 if (echo) IConsoleError("This command is forbidden in multiplayer.");
00104 return CHR_DISALLOW;
00105 }
00106 return CHR_ALLOW;
00107 }
00108
00109 #else
00110 # define ConHookNoNetwork NULL
00111 #endif
00112
00113 DEF_CONSOLE_HOOK(ConHookNewGRFDeveloperTool)
00114 {
00115 if (_settings_client.gui.newgrf_developer_tools) {
00116 if (_game_mode == GM_MENU) {
00117 if (echo) IConsoleError("This command is only available in game and editor.");
00118 return CHR_DISALLOW;
00119 }
00120 #ifdef ENABLE_NETWORK
00121 return ConHookNoNetwork(echo);
00122 #else
00123 return CHR_ALLOW;
00124 #endif
00125 }
00126 return CHR_HIDE;
00127 }
00128
00129 static void IConsoleHelp(const char *str)
00130 {
00131 IConsolePrintF(CC_WARNING, "- %s", str);
00132 }
00133
00134 DEF_CONSOLE_CMD(ConResetEngines)
00135 {
00136 if (argc == 0) {
00137 IConsoleHelp("Reset status data of all engines. This might solve some issues with 'lost' engines. Usage: 'resetengines'");
00138 return true;
00139 }
00140
00141 StartupEngines();
00142 return true;
00143 }
00144
00145 #ifdef _DEBUG
00146 DEF_CONSOLE_CMD(ConResetTile)
00147 {
00148 if (argc == 0) {
00149 IConsoleHelp("Reset a tile to bare land. Usage: 'resettile <tile>'");
00150 IConsoleHelp("Tile can be either decimal (34161) or hexadecimal (0x4a5B)");
00151 return true;
00152 }
00153
00154 if (argc == 2) {
00155 uint32 result;
00156 if (GetArgumentInteger(&result, argv[1])) {
00157 DoClearSquare((TileIndex)result);
00158 return true;
00159 }
00160 }
00161
00162 return false;
00163 }
00164 #endif
00165
00166 DEF_CONSOLE_CMD(ConScrollToTile)
00167 {
00168 switch (argc) {
00169 case 0:
00170 IConsoleHelp("Center the screen on a given tile.");
00171 IConsoleHelp("Usage: 'scrollto <tile>' or 'scrollto <x> <y>'");
00172 IConsoleHelp("Numbers can be either decimal (34161) or hexadecimal (0x4a5B).");
00173 return true;
00174
00175 case 2: {
00176 uint32 result;
00177 if (GetArgumentInteger(&result, argv[1])) {
00178 if (result >= MapSize()) {
00179 IConsolePrint(CC_ERROR, "Tile does not exist");
00180 return true;
00181 }
00182 ScrollMainWindowToTile((TileIndex)result);
00183 return true;
00184 }
00185 break;
00186 }
00187
00188 case 3: {
00189 uint32 x, y;
00190 if (GetArgumentInteger(&x, argv[1]) && GetArgumentInteger(&y, argv[2])) {
00191 if (x >= MapSizeX() || y >= MapSizeY()) {
00192 IConsolePrint(CC_ERROR, "Tile does not exist");
00193 return true;
00194 }
00195 ScrollMainWindowToTile(TileXY(x, y));
00196 return true;
00197 }
00198 break;
00199 }
00200 }
00201
00202 return false;
00203 }
00204
00205
00206 DEF_CONSOLE_CMD(ConSave)
00207 {
00208 if (argc == 0) {
00209 IConsoleHelp("Save the current game. Usage: 'save <filename>'");
00210 return true;
00211 }
00212
00213 if (argc == 2) {
00214 char *filename = str_fmt("%s.sav", argv[1]);
00215 IConsolePrint(CC_DEFAULT, "Saving map...");
00216
00217 if (SaveOrLoad(filename, SL_SAVE, SAVE_DIR) != SL_OK) {
00218 IConsolePrint(CC_ERROR, "Saving map failed");
00219 } else {
00220 IConsolePrintF(CC_DEFAULT, "Map successfully saved to %s", filename);
00221 }
00222 free(filename);
00223 return true;
00224 }
00225
00226 return false;
00227 }
00228
00229
00230 DEF_CONSOLE_CMD(ConSaveConfig)
00231 {
00232 if (argc == 0) {
00233 IConsoleHelp("Saves the configuration for new games to the configuration file, typically 'openttd.cfg'.");
00234 IConsoleHelp("It does not save the configuration of the current game to the configuration file.");
00235 return true;
00236 }
00237
00238 SaveToConfig();
00239 IConsolePrint(CC_DEFAULT, "Saved config.");
00240 return true;
00241 }
00242
00243 static const FiosItem *GetFiosItem(const char *file)
00244 {
00245 _saveload_mode = SLD_LOAD_GAME;
00246 BuildFileList();
00247
00248 for (const FiosItem *item = _fios_items.Begin(); item != _fios_items.End(); item++) {
00249 if (strcmp(file, item->name) == 0) return item;
00250 if (strcmp(file, item->title) == 0) return item;
00251 }
00252
00253
00254 char *endptr;
00255 int i = strtol(file, &endptr, 10);
00256 if (file == endptr || *endptr != '\0') i = -1;
00257
00258 if (IsInsideMM(i, 0, _fios_items.Length())) return _fios_items.Get(i);
00259
00260
00261
00262 char long_file[MAX_PATH];
00263 seprintf(long_file, lastof(long_file), "%s.sav", file);
00264 for (const FiosItem *item = _fios_items.Begin(); item != _fios_items.End(); item++) {
00265 if (strcmp(long_file, item->name) == 0) return item;
00266 if (strcmp(long_file, item->title) == 0) return item;
00267 }
00268
00269 return NULL;
00270 }
00271
00272
00273 DEF_CONSOLE_CMD(ConLoad)
00274 {
00275 if (argc == 0) {
00276 IConsoleHelp("Load a game by name or index. Usage: 'load <file | number>'");
00277 return true;
00278 }
00279
00280 if (argc != 2) return false;
00281
00282 const char *file = argv[1];
00283 const FiosItem *item = GetFiosItem(file);
00284 if (item != NULL) {
00285 switch (item->type) {
00286 case FIOS_TYPE_FILE: case FIOS_TYPE_OLDFILE: {
00287 _switch_mode = SM_LOAD;
00288 SetFiosType(item->type);
00289
00290 strecpy(_file_to_saveload.name, FiosBrowseTo(item), lastof(_file_to_saveload.name));
00291 strecpy(_file_to_saveload.title, item->title, lastof(_file_to_saveload.title));
00292 break;
00293 }
00294 default: IConsolePrintF(CC_ERROR, "%s: Not a savegame.", file);
00295 }
00296 } else {
00297 IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file);
00298 }
00299
00300 FiosFreeSavegameList();
00301 return true;
00302 }
00303
00304
00305 DEF_CONSOLE_CMD(ConRemove)
00306 {
00307 if (argc == 0) {
00308 IConsoleHelp("Remove a savegame by name or index. Usage: 'rm <file | number>'");
00309 return true;
00310 }
00311
00312 if (argc != 2) return false;
00313
00314 const char *file = argv[1];
00315 const FiosItem *item = GetFiosItem(file);
00316 if (item != NULL) {
00317 if (!FiosDelete(item->name)) {
00318 IConsolePrintF(CC_ERROR, "%s: Failed to delete file", file);
00319 }
00320 } else {
00321 IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file);
00322 }
00323
00324 FiosFreeSavegameList();
00325 return true;
00326 }
00327
00328
00329
00330 DEF_CONSOLE_CMD(ConListFiles)
00331 {
00332 if (argc == 0) {
00333 IConsoleHelp("List all loadable savegames and directories in the current dir via console. Usage: 'ls | dir'");
00334 return true;
00335 }
00336
00337 BuildFileList();
00338
00339 for (uint i = 0; i < _fios_items.Length(); i++) {
00340 IConsolePrintF(CC_DEFAULT, "%d) %s", i, _fios_items[i].title);
00341 }
00342
00343 FiosFreeSavegameList();
00344 return true;
00345 }
00346
00347
00348 DEF_CONSOLE_CMD(ConChangeDirectory)
00349 {
00350 if (argc == 0) {
00351 IConsoleHelp("Change the dir via console. Usage: 'cd <directory | number>'");
00352 return true;
00353 }
00354
00355 if (argc != 2) return false;
00356
00357 const char *file = argv[1];
00358 const FiosItem *item = GetFiosItem(file);
00359 if (item != NULL) {
00360 switch (item->type) {
00361 case FIOS_TYPE_DIR: case FIOS_TYPE_DRIVE: case FIOS_TYPE_PARENT:
00362 FiosBrowseTo(item);
00363 break;
00364 default: IConsolePrintF(CC_ERROR, "%s: Not a directory.", file);
00365 }
00366 } else {
00367 IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file);
00368 }
00369
00370 FiosFreeSavegameList();
00371 return true;
00372 }
00373
00374 DEF_CONSOLE_CMD(ConPrintWorkingDirectory)
00375 {
00376 const char *path;
00377
00378 if (argc == 0) {
00379 IConsoleHelp("Print out the current working directory. Usage: 'pwd'");
00380 return true;
00381 }
00382
00383
00384 FiosGetSavegameList(SLD_LOAD_GAME);
00385 FiosFreeSavegameList();
00386
00387 FiosGetDescText(&path, NULL);
00388 IConsolePrint(CC_DEFAULT, path);
00389 return true;
00390 }
00391
00392 DEF_CONSOLE_CMD(ConClearBuffer)
00393 {
00394 if (argc == 0) {
00395 IConsoleHelp("Clear the console buffer. Usage: 'clear'");
00396 return true;
00397 }
00398
00399 IConsoleClearBuffer();
00400 SetWindowDirty(WC_CONSOLE, 0);
00401 return true;
00402 }
00403
00404
00405
00406
00407
00408 #ifdef ENABLE_NETWORK
00409
00410 static bool ConKickOrBan(const char *argv, bool ban)
00411 {
00412 const char *ip = argv;
00413
00414 if (strchr(argv, '.') == NULL && strchr(argv, ':') == NULL) {
00415 ClientID client_id = (ClientID)atoi(argv);
00416
00417 if (client_id == CLIENT_ID_SERVER) {
00418 IConsolePrintF(CC_ERROR, "ERROR: Silly boy, you can not %s yourself!", ban ? "ban" : "kick");
00419 return true;
00420 }
00421
00422 NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id);
00423 if (ci == NULL) {
00424 IConsoleError("Invalid client");
00425 return true;
00426 }
00427
00428 if (!ban) {
00429
00430 NetworkServerKickClient(client_id);
00431 return true;
00432 }
00433
00434
00435 ip = GetClientIP(ci);
00436 }
00437
00438 uint n = NetworkServerKickOrBanIP(ip, ban);
00439 if (n == 0) {
00440 IConsolePrint(CC_DEFAULT, ban ? "Client not online, address added to banlist" : "Client not found");
00441 } else {
00442 IConsolePrintF(CC_DEFAULT, "%sed %u client(s)", ban ? "Bann" : "Kick", n);
00443 }
00444
00445 return true;
00446 }
00447
00448 DEF_CONSOLE_CMD(ConKick)
00449 {
00450 if (argc == 0) {
00451 IConsoleHelp("Kick a client from a network game. Usage: 'kick <ip | client-id>'");
00452 IConsoleHelp("For client-id's, see the command 'clients'");
00453 return true;
00454 }
00455
00456 if (argc != 2) return false;
00457
00458 return ConKickOrBan(argv[1], false);
00459 }
00460
00461 DEF_CONSOLE_CMD(ConBan)
00462 {
00463 if (argc == 0) {
00464 IConsoleHelp("Ban a client from a network game. Usage: 'ban <ip | client-id>'");
00465 IConsoleHelp("For client-id's, see the command 'clients'");
00466 IConsoleHelp("If the client is no longer online, you can still ban his/her IP");
00467 return true;
00468 }
00469
00470 if (argc != 2) return false;
00471
00472 return ConKickOrBan(argv[1], true);
00473 }
00474
00475 DEF_CONSOLE_CMD(ConUnBan)
00476 {
00477
00478 if (argc == 0) {
00479 IConsoleHelp("Unban a client from a network game. Usage: 'unban <ip | client-id>'");
00480 IConsoleHelp("For a list of banned IP's, see the command 'banlist'");
00481 return true;
00482 }
00483
00484 if (argc != 2) return false;
00485
00486 uint index = (strchr(argv[1], '.') == NULL) ? atoi(argv[1]) : 0;
00487 index--;
00488 uint i = 0;
00489
00490 for (char **iter = _network_ban_list.Begin(); iter != _network_ban_list.End(); iter++, i++) {
00491 if (strcmp(_network_ban_list[i], argv[1]) == 0 || index == i) {
00492 free(_network_ban_list[i]);
00493 _network_ban_list.Erase(iter);
00494 IConsolePrint(CC_DEFAULT, "IP unbanned.");
00495 return true;
00496 }
00497 }
00498
00499 IConsolePrint(CC_DEFAULT, "IP not in ban-list.");
00500 return true;
00501 }
00502
00503 DEF_CONSOLE_CMD(ConBanList)
00504 {
00505 if (argc == 0) {
00506 IConsoleHelp("List the IP's of banned clients: Usage 'banlist'");
00507 return true;
00508 }
00509
00510 IConsolePrint(CC_DEFAULT, "Banlist: ");
00511
00512 uint i = 1;
00513 for (char **iter = _network_ban_list.Begin(); iter != _network_ban_list.End(); iter++, i++) {
00514 IConsolePrintF(CC_DEFAULT, " %d) %s", i, *iter);
00515 }
00516
00517 return true;
00518 }
00519
00520 DEF_CONSOLE_CMD(ConPauseGame)
00521 {
00522 if (argc == 0) {
00523 IConsoleHelp("Pause a network game. Usage: 'pause'");
00524 return true;
00525 }
00526
00527 if ((_pause_mode & PM_PAUSED_NORMAL) == PM_UNPAUSED) {
00528 DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
00529 if (!_networking) IConsolePrint(CC_DEFAULT, "Game paused.");
00530 } else {
00531 IConsolePrint(CC_DEFAULT, "Game is already paused.");
00532 }
00533
00534 return true;
00535 }
00536
00537 DEF_CONSOLE_CMD(ConUnPauseGame)
00538 {
00539 if (argc == 0) {
00540 IConsoleHelp("Unpause a network game. Usage: 'unpause'");
00541 return true;
00542 }
00543
00544 if ((_pause_mode & PM_PAUSED_NORMAL) != PM_UNPAUSED) {
00545 DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE);
00546 if (!_networking) IConsolePrint(CC_DEFAULT, "Game unpaused.");
00547 } else if ((_pause_mode & PM_PAUSED_ERROR) != PM_UNPAUSED) {
00548 IConsolePrint(CC_DEFAULT, "Game is in error state and cannot be unpaused via console.");
00549 } else if (_pause_mode != PM_UNPAUSED) {
00550 IConsolePrint(CC_DEFAULT, "Game cannot be unpaused manually; disable pause_on_join/min_active_clients.");
00551 } else {
00552 IConsolePrint(CC_DEFAULT, "Game is already unpaused.");
00553 }
00554
00555 return true;
00556 }
00557
00558 DEF_CONSOLE_CMD(ConRcon)
00559 {
00560 if (argc == 0) {
00561 IConsoleHelp("Remote control the server from another client. Usage: 'rcon <password> <command>'");
00562 IConsoleHelp("Remember to enclose the command in quotes, otherwise only the first parameter is sent");
00563 return true;
00564 }
00565
00566 if (argc < 3) return false;
00567
00568 if (_network_server) {
00569 IConsoleCmdExec(argv[2]);
00570 } else {
00571 NetworkClientSendRcon(argv[1], argv[2]);
00572 }
00573 return true;
00574 }
00575
00576 DEF_CONSOLE_CMD(ConStatus)
00577 {
00578 if (argc == 0) {
00579 IConsoleHelp("List the status of all clients connected to the server. Usage 'status'");
00580 return true;
00581 }
00582
00583 NetworkServerShowStatusToConsole();
00584 return true;
00585 }
00586
00587 DEF_CONSOLE_CMD(ConServerInfo)
00588 {
00589 if (argc == 0) {
00590 IConsoleHelp("List current and maximum client/company limits. Usage 'server_info'");
00591 IConsoleHelp("You can change these values by modifying settings 'network.max_clients', 'network.max_companies' and 'network.max_spectators'");
00592 return true;
00593 }
00594
00595 IConsolePrintF(CC_DEFAULT, "Current/maximum clients: %2d/%2d", _network_game_info.clients_on, _settings_client.network.max_clients);
00596 IConsolePrintF(CC_DEFAULT, "Current/maximum companies: %2d/%2d", (int)Company::GetNumItems(), _settings_client.network.max_companies);
00597 IConsolePrintF(CC_DEFAULT, "Current/maximum spectators: %2d/%2d", NetworkSpectatorCount(), _settings_client.network.max_spectators);
00598
00599 return true;
00600 }
00601
00602 DEF_CONSOLE_CMD(ConClientNickChange)
00603 {
00604 if (argc != 3) {
00605 IConsoleHelp("Change the nickname of a connected client. Usage: 'client_name <client-id> <new-name>'");
00606 IConsoleHelp("For client-id's, see the command 'clients'");
00607 return true;
00608 }
00609
00610 ClientID client_id = (ClientID)atoi(argv[1]);
00611
00612 if (client_id == CLIENT_ID_SERVER) {
00613 IConsoleError("Please use the command 'name' to change your own name!");
00614 return true;
00615 }
00616
00617 if (NetworkFindClientInfoFromClientID(client_id) == NULL) {
00618 IConsoleError("Invalid client");
00619 return true;
00620 }
00621
00622 if (!NetworkServerChangeClientName(client_id, argv[2])) {
00623 IConsoleError("Cannot give a client a duplicate name");
00624 }
00625
00626 return true;
00627 }
00628
00629 DEF_CONSOLE_CMD(ConJoinCompany)
00630 {
00631 if (argc < 2) {
00632 IConsoleHelp("Request joining another company. Usage: join <company-id> [<password>]");
00633 IConsoleHelp("For valid company-id see company list, use 255 for spectator");
00634 return true;
00635 }
00636
00637 CompanyID company_id = (CompanyID)(atoi(argv[1]) <= MAX_COMPANIES ? atoi(argv[1]) - 1 : atoi(argv[1]));
00638
00639
00640 if (!Company::IsValidID(company_id) && company_id != COMPANY_SPECTATOR) {
00641 IConsolePrintF(CC_ERROR, "Company does not exist. Company-id must be between 1 and %d.", MAX_COMPANIES);
00642 return true;
00643 }
00644
00645 if (NetworkFindClientInfoFromClientID(_network_own_client_id)->client_playas == company_id) {
00646 IConsoleError("You are already there!");
00647 return true;
00648 }
00649
00650 if (company_id == COMPANY_SPECTATOR && NetworkMaxSpectatorsReached()) {
00651 IConsoleError("Cannot join spectators, maximum number of spectators reached.");
00652 return true;
00653 }
00654
00655 if (company_id != COMPANY_SPECTATOR && !Company::IsHumanID(company_id)) {
00656 IConsoleError("Cannot join AI company.");
00657 return true;
00658 }
00659
00660
00661 if (NetworkCompanyIsPassworded(company_id) && argc < 3) {
00662 IConsolePrintF(CC_ERROR, "Company %d requires a password to join.", company_id + 1);
00663 return true;
00664 }
00665
00666
00667 if (_network_server) {
00668 NetworkServerDoMove(CLIENT_ID_SERVER, company_id);
00669 } else {
00670 NetworkClientRequestMove(company_id, NetworkCompanyIsPassworded(company_id) ? argv[2] : "");
00671 }
00672
00673 return true;
00674 }
00675
00676 DEF_CONSOLE_CMD(ConMoveClient)
00677 {
00678 if (argc < 3) {
00679 IConsoleHelp("Move a client to another company. Usage: move <client-id> <company-id>");
00680 IConsoleHelp("For valid client-id see 'clients', for valid company-id see 'companies', use 255 for moving to spectators");
00681 return true;
00682 }
00683
00684 const NetworkClientInfo *ci = NetworkFindClientInfoFromClientID((ClientID)atoi(argv[1]));
00685 CompanyID company_id = (CompanyID)(atoi(argv[2]) <= MAX_COMPANIES ? atoi(argv[2]) - 1 : atoi(argv[2]));
00686
00687
00688 if (ci == NULL) {
00689 IConsoleError("Invalid client-id, check the command 'clients' for valid client-id's.");
00690 return true;
00691 }
00692
00693 if (!Company::IsValidID(company_id) && company_id != COMPANY_SPECTATOR) {
00694 IConsolePrintF(CC_ERROR, "Company does not exist. Company-id must be between 1 and %d.", MAX_COMPANIES);
00695 return true;
00696 }
00697
00698 if (company_id != COMPANY_SPECTATOR && !Company::IsHumanID(company_id)) {
00699 IConsoleError("You cannot move clients to AI companies.");
00700 return true;
00701 }
00702
00703 if (ci->client_id == CLIENT_ID_SERVER && _network_dedicated) {
00704 IConsoleError("Silly boy, you cannot move the server!");
00705 return true;
00706 }
00707
00708 if (ci->client_playas == company_id) {
00709 IConsoleError("You cannot move someone to where he/she already is!");
00710 return true;
00711 }
00712
00713
00714 NetworkServerDoMove(ci->client_id, company_id);
00715
00716 return true;
00717 }
00718
00719 DEF_CONSOLE_CMD(ConResetCompany)
00720 {
00721 if (argc == 0) {
00722 IConsoleHelp("Remove an idle company from the game. Usage: 'reset_company <company-id>'");
00723 IConsoleHelp("For company-id's, see the list of companies from the dropdown menu. Company 1 is 1, etc.");
00724 return true;
00725 }
00726
00727 if (argc != 2) return false;
00728
00729 CompanyID index = (CompanyID)(atoi(argv[1]) - 1);
00730
00731
00732 if (!Company::IsValidID(index)) {
00733 IConsolePrintF(CC_ERROR, "Company does not exist. Company-id must be between 1 and %d.", MAX_COMPANIES);
00734 return true;
00735 }
00736
00737 if (!Company::IsHumanID(index)) {
00738 IConsoleError("Company is owned by an AI.");
00739 return true;
00740 }
00741
00742 if (NetworkCompanyHasClients(index)) {
00743 IConsoleError("Cannot remove company: a client is connected to that company.");
00744 return false;
00745 }
00746 const NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(CLIENT_ID_SERVER);
00747 if (ci->client_playas == index) {
00748 IConsoleError("Cannot remove company: the server is connected to that company.");
00749 return true;
00750 }
00751
00752
00753 DoCommandP(0, 2 | index << 16, 0, CMD_COMPANY_CTRL);
00754 IConsolePrint(CC_DEFAULT, "Company deleted.");
00755
00756 return true;
00757 }
00758
00759 DEF_CONSOLE_CMD(ConNetworkClients)
00760 {
00761 if (argc == 0) {
00762 IConsoleHelp("Get a list of connected clients including their ID, name, company-id, and IP. Usage: 'clients'");
00763 return true;
00764 }
00765
00766 NetworkPrintClients();
00767
00768 return true;
00769 }
00770
00771 DEF_CONSOLE_CMD(ConNetworkReconnect)
00772 {
00773 if (argc == 0) {
00774 IConsoleHelp("Reconnect to server to which you were connected last time. Usage: 'reconnect [<company>]'");
00775 IConsoleHelp("Company 255 is spectator (default, if not specified), 0 means creating new company.");
00776 IConsoleHelp("All others are a certain company with Company 1 being #1");
00777 return true;
00778 }
00779
00780 CompanyID playas = (argc >= 2) ? (CompanyID)atoi(argv[1]) : COMPANY_SPECTATOR;
00781 switch (playas) {
00782 case 0: playas = COMPANY_NEW_COMPANY; break;
00783 case COMPANY_SPECTATOR: break;
00784 default:
00785
00786
00787 playas--;
00788 if (playas < COMPANY_FIRST || playas >= MAX_COMPANIES) return false;
00789 break;
00790 }
00791
00792 if (StrEmpty(_settings_client.network.last_host)) {
00793 IConsolePrint(CC_DEFAULT, "No server for reconnecting.");
00794 return true;
00795 }
00796
00797
00798 IConsolePrintF(CC_DEFAULT, "Reconnecting to %s:%d...", _settings_client.network.last_host, _settings_client.network.last_port);
00799
00800 NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), playas);
00801 return true;
00802 }
00803
00804 DEF_CONSOLE_CMD(ConNetworkConnect)
00805 {
00806 if (argc == 0) {
00807 IConsoleHelp("Connect to a remote OTTD server and join the game. Usage: 'connect <ip>'");
00808 IConsoleHelp("IP can contain port and company: 'IP[:Port][#Company]', eg: 'server.ottd.org:443#2'");
00809 IConsoleHelp("Company #255 is spectator all others are a certain company with Company 1 being #1");
00810 return true;
00811 }
00812
00813 if (argc < 2) return false;
00814 if (_networking) NetworkDisconnect();
00815
00816 const char *port = NULL;
00817 const char *company = NULL;
00818 char *ip = argv[1];
00819
00820 uint16 rport = NETWORK_DEFAULT_PORT;
00821 CompanyID join_as = COMPANY_NEW_COMPANY;
00822
00823 ParseConnectionString(&company, &port, ip);
00824
00825 IConsolePrintF(CC_DEFAULT, "Connecting to %s...", ip);
00826 if (company != NULL) {
00827 join_as = (CompanyID)atoi(company);
00828 IConsolePrintF(CC_DEFAULT, " company-no: %d", join_as);
00829
00830
00831
00832 if (join_as != COMPANY_SPECTATOR) {
00833 if (join_as > MAX_COMPANIES) return false;
00834 join_as--;
00835 }
00836 }
00837 if (port != NULL) {
00838 rport = atoi(port);
00839 IConsolePrintF(CC_DEFAULT, " port: %s", port);
00840 }
00841
00842 NetworkClientConnectGame(NetworkAddress(ip, rport), join_as);
00843
00844 return true;
00845 }
00846
00847 #endif
00848
00849
00850
00851
00852
00853 DEF_CONSOLE_CMD(ConExec)
00854 {
00855 if (argc == 0) {
00856 IConsoleHelp("Execute a local script file. Usage: 'exec <script> <?>'");
00857 return true;
00858 }
00859
00860 if (argc < 2) return false;
00861
00862 FILE *script_file = FioFOpenFile(argv[1], "r", BASE_DIR);
00863
00864 if (script_file == NULL) {
00865 if (argc == 2 || atoi(argv[2]) != 0) IConsoleError("script file not found");
00866 return true;
00867 }
00868
00869 _script_running = true;
00870
00871 char cmdline[ICON_CMDLN_SIZE];
00872 while (_script_running && fgets(cmdline, sizeof(cmdline), script_file) != NULL) {
00873
00874 for (char *cmdptr = cmdline; *cmdptr != '\0'; cmdptr++) {
00875 if (*cmdptr == '\n' || *cmdptr == '\r') {
00876 *cmdptr = '\0';
00877 break;
00878 }
00879 }
00880 IConsoleCmdExec(cmdline);
00881 }
00882
00883 if (ferror(script_file)) {
00884 IConsoleError("Encountered errror while trying to read from script file");
00885 }
00886
00887 _script_running = false;
00888 FioFCloseFile(script_file);
00889 return true;
00890 }
00891
00892 DEF_CONSOLE_CMD(ConReturn)
00893 {
00894 if (argc == 0) {
00895 IConsoleHelp("Stop executing a running script. Usage: 'return'");
00896 return true;
00897 }
00898
00899 _script_running = false;
00900 return true;
00901 }
00902
00903
00904
00905
00906 extern bool CloseConsoleLogIfActive();
00907
00908 DEF_CONSOLE_CMD(ConScript)
00909 {
00910 extern FILE *_iconsole_output_file;
00911
00912 if (argc == 0) {
00913 IConsoleHelp("Start or stop logging console output to a file. Usage: 'script <filename>'");
00914 IConsoleHelp("If filename is omitted, a running log is stopped if it is active");
00915 return true;
00916 }
00917
00918 if (!CloseConsoleLogIfActive()) {
00919 if (argc < 2) return false;
00920
00921 IConsolePrintF(CC_DEFAULT, "file output started to: %s", argv[1]);
00922 _iconsole_output_file = fopen(argv[1], "ab");
00923 if (_iconsole_output_file == NULL) IConsoleError("could not open file");
00924 }
00925
00926 return true;
00927 }
00928
00929
00930 DEF_CONSOLE_CMD(ConEcho)
00931 {
00932 if (argc == 0) {
00933 IConsoleHelp("Print back the first argument to the console. Usage: 'echo <arg>'");
00934 return true;
00935 }
00936
00937 if (argc < 2) return false;
00938 IConsolePrint(CC_DEFAULT, argv[1]);
00939 return true;
00940 }
00941
00942 DEF_CONSOLE_CMD(ConEchoC)
00943 {
00944 if (argc == 0) {
00945 IConsoleHelp("Print back the first argument to the console in a given colour. Usage: 'echoc <colour> <arg2>'");
00946 return true;
00947 }
00948
00949 if (argc < 3) return false;
00950 IConsolePrint((TextColour)Clamp(atoi(argv[1]), TC_BEGIN, TC_END - 1), argv[2]);
00951 return true;
00952 }
00953
00954 DEF_CONSOLE_CMD(ConNewGame)
00955 {
00956 if (argc == 0) {
00957 IConsoleHelp("Start a new game. Usage: 'newgame [seed]'");
00958 IConsoleHelp("The server can force a new game using 'newgame'; any client joined will rejoin after the server is done generating the new game.");
00959 return true;
00960 }
00961
00962 StartNewGameWithoutGUI((argc == 2) ? strtoul(argv[1], NULL, 10) : GENERATE_NEW_SEED);
00963 return true;
00964 }
00965
00966 extern void SwitchToMode(SwitchMode new_mode);
00967
00968 DEF_CONSOLE_CMD(ConRestart)
00969 {
00970 if (argc == 0) {
00971 IConsoleHelp("Restart game. Usage: 'restart'");
00972 IConsoleHelp("Restarts a game. It tries to reproduce the exact same map as the game started with.");
00973 IConsoleHelp("However:");
00974 IConsoleHelp(" * restarting games started in another version might create another map due to difference in map generation");
00975 IConsoleHelp(" * restarting games based on scenarios, loaded games or heightmaps will start a new game based on the settings stored in the scenario/savegame");
00976 return true;
00977 }
00978
00979
00980 _settings_game.game_creation.map_x = MapLogX();
00981 _settings_game.game_creation.map_y = FindFirstBit(MapSizeY());
00982 _switch_mode = SM_RESTARTGAME;
00983 return true;
00984 }
00985
00986 #ifdef ENABLE_AI
00987
00992 static void PrintLineByLine(char *buf)
00993 {
00994 char *p = buf;
00995
00996 for (char *p2 = buf; *p2 != '\0'; p2++) {
00997 if (*p2 == '\n') {
00998 *p2 = '\0';
00999 IConsolePrintF(CC_DEFAULT, "%s", p);
01000 p = p2 + 1;
01001 }
01002 }
01003 }
01004
01005 DEF_CONSOLE_CMD(ConListAILibs)
01006 {
01007 char buf[4096];
01008 AI::GetConsoleLibraryList(buf, lastof(buf));
01009
01010 PrintLineByLine(buf);
01011
01012 return true;
01013 }
01014
01015 DEF_CONSOLE_CMD(ConListAI)
01016 {
01017 char buf[4096];
01018 AI::GetConsoleList(buf, lastof(buf));
01019
01020 PrintLineByLine(buf);
01021
01022 return true;
01023 }
01024
01025 DEF_CONSOLE_CMD(ConStartAI)
01026 {
01027 if (argc == 0 || argc > 3) {
01028 IConsoleHelp("Start a new AI. Usage: 'start_ai [<AI>] [<settings>]'");
01029 IConsoleHelp("Start a new AI. If <AI> is given, it starts that specific AI (if found).");
01030 IConsoleHelp("If <settings> is given, it is parsed and the AI settings are set to that.");
01031 return true;
01032 }
01033
01034 if (_game_mode != GM_NORMAL) {
01035 IConsoleWarning("AIs can only be managed in a game.");
01036 return true;
01037 }
01038
01039 if (Company::GetNumItems() == CompanyPool::MAX_SIZE) {
01040 IConsoleWarning("Can't start a new AI (no more free slots).");
01041 return true;
01042 }
01043 if (_networking && !_network_server) {
01044 IConsoleWarning("Only the server can start a new AI.");
01045 return true;
01046 }
01047 if (_networking && !_settings_game.ai.ai_in_multiplayer) {
01048 IConsoleWarning("AIs are not allowed in multiplayer by configuration.");
01049 IConsoleWarning("Switch AI -> AI in multiplayer to True.");
01050 return true;
01051 }
01052 if (!AI::CanStartNew()) {
01053 IConsoleWarning("Can't start a new AI.");
01054 return true;
01055 }
01056
01057 int n = 0;
01058 Company *c;
01059
01060 FOR_ALL_COMPANIES(c) {
01061 if (c->index != n) break;
01062 n++;
01063 }
01064
01065 AIConfig *config = AIConfig::GetConfig((CompanyID)n);
01066 if (argc >= 2) {
01067 config->ChangeAI(argv[1], -1, true);
01068 if (!config->HasAI()) {
01069 IConsoleWarning("Failed to load the specified AI");
01070 return true;
01071 }
01072 if (argc == 3) {
01073 config->StringToSettings(argv[2]);
01074 }
01075 }
01076
01077
01078 DoCommandP(0, 1 | INVALID_COMPANY << 16, 0, CMD_COMPANY_CTRL);
01079
01080 return true;
01081 }
01082
01083 DEF_CONSOLE_CMD(ConReloadAI)
01084 {
01085 if (argc != 2) {
01086 IConsoleHelp("Reload an AI. Usage: 'reload_ai <company-id>'");
01087 IConsoleHelp("Reload the AI with the given company id. For company-id's, see the list of companies from the dropdown menu. Company 1 is 1, etc.");
01088 return true;
01089 }
01090
01091 if (_game_mode != GM_NORMAL) {
01092 IConsoleWarning("AIs can only be managed in a game.");
01093 return true;
01094 }
01095
01096 if (_networking && !_network_server) {
01097 IConsoleWarning("Only the server can reload an AI.");
01098 return true;
01099 }
01100
01101 CompanyID company_id = (CompanyID)(atoi(argv[1]) - 1);
01102 if (!Company::IsValidID(company_id)) {
01103 IConsolePrintF(CC_DEFAULT, "Unknown company. Company range is between 1 and %d.", MAX_COMPANIES);
01104 return true;
01105 }
01106
01107 if (Company::IsHumanID(company_id)) {
01108 IConsoleWarning("Company is not controlled by an AI.");
01109 return true;
01110 }
01111
01112
01113 DoCommandP(0, 2 | company_id << 16, 0, CMD_COMPANY_CTRL);
01114 DoCommandP(0, 1 | company_id << 16, 0, CMD_COMPANY_CTRL);
01115 IConsolePrint(CC_DEFAULT, "AI reloaded.");
01116
01117 return true;
01118 }
01119
01120 DEF_CONSOLE_CMD(ConStopAI)
01121 {
01122 if (argc != 2) {
01123 IConsoleHelp("Stop an AI. Usage: 'stop_ai <company-id>'");
01124 IConsoleHelp("Stop the AI with the given company id. For company-id's, see the list of companies from the dropdown menu. Company 1 is 1, etc.");
01125 return true;
01126 }
01127
01128 if (_game_mode != GM_NORMAL) {
01129 IConsoleWarning("AIs can only be managed in a game.");
01130 return true;
01131 }
01132
01133 if (_networking && !_network_server) {
01134 IConsoleWarning("Only the server can stop an AI.");
01135 return true;
01136 }
01137
01138 CompanyID company_id = (CompanyID)(atoi(argv[1]) - 1);
01139 if (!Company::IsValidID(company_id)) {
01140 IConsolePrintF(CC_DEFAULT, "Unknown company. Company range is between 1 and %d.", MAX_COMPANIES);
01141 return true;
01142 }
01143
01144 if (Company::IsHumanID(company_id)) {
01145 IConsoleWarning("Company is not controlled by an AI.");
01146 return true;
01147 }
01148
01149
01150 DoCommandP(0, 2 | company_id << 16, 0, CMD_COMPANY_CTRL);
01151 IConsolePrint(CC_DEFAULT, "AI stopped, company deleted.");
01152
01153 return true;
01154 }
01155
01156 DEF_CONSOLE_CMD(ConRescanAI)
01157 {
01158 if (argc == 0) {
01159 IConsoleHelp("Rescan the AI dir for scripts. Usage: 'rescan_ai'");
01160 return true;
01161 }
01162
01163 if (_networking && !_network_server) {
01164 IConsoleWarning("Only the server can rescan the AI dir for scripts.");
01165 return true;
01166 }
01167
01168 TarScanner::DoScan();
01169 AI::Rescan();
01170 InvalidateWindowData(WC_AI_LIST, 0, 1);
01171 SetWindowDirty(WC_AI_SETTINGS, 0);
01172
01173 return true;
01174 }
01175 #endif
01176
01177 DEF_CONSOLE_CMD(ConRescanNewGRF)
01178 {
01179 if (argc == 0) {
01180 IConsoleHelp("Rescan the data dir for NewGRFs. Usage: 'rescan_newgrf'");
01181 return true;
01182 }
01183
01184 TarScanner::DoScan();
01185 ScanNewGRFFiles();
01186 InvalidateWindowData(WC_GAME_OPTIONS, 0, 1);
01187
01188 return true;
01189 }
01190
01191 DEF_CONSOLE_CMD(ConGetSeed)
01192 {
01193 if (argc == 0) {
01194 IConsoleHelp("Returns the seed used to create this game. Usage: 'getseed'");
01195 IConsoleHelp("The seed can be used to reproduce the exact same map as the game started with.");
01196 return true;
01197 }
01198
01199 IConsolePrintF(CC_DEFAULT, "Generation Seed: %u", _settings_game.game_creation.generation_seed);
01200 return true;
01201 }
01202
01203 DEF_CONSOLE_CMD(ConGetDate)
01204 {
01205 if (argc == 0) {
01206 IConsoleHelp("Returns the current date (day-month-year) of the game. Usage: 'getdate'");
01207 return true;
01208 }
01209
01210 YearMonthDay ymd;
01211 ConvertDateToYMD(_date, &ymd);
01212 IConsolePrintF(CC_DEFAULT, "Date: %d-%d-%d", ymd.day, ymd.month + 1, ymd.year);
01213 return true;
01214 }
01215
01216
01217 DEF_CONSOLE_CMD(ConAlias)
01218 {
01219 IConsoleAlias *alias;
01220
01221 if (argc == 0) {
01222 IConsoleHelp("Add a new alias, or redefine the behaviour of an existing alias . Usage: 'alias <name> <command>'");
01223 return true;
01224 }
01225
01226 if (argc < 3) return false;
01227
01228 alias = IConsoleAliasGet(argv[1]);
01229 if (alias == NULL) {
01230 IConsoleAliasRegister(argv[1], argv[2]);
01231 } else {
01232 free(alias->cmdline);
01233 alias->cmdline = strdup(argv[2]);
01234 }
01235 return true;
01236 }
01237
01238 DEF_CONSOLE_CMD(ConScreenShot)
01239 {
01240 if (argc == 0) {
01241 IConsoleHelp("Create a screenshot of the game. Usage: 'screenshot [big | giant | no_con] [file name]'");
01242 IConsoleHelp("'big' makes a zoomed-in screenshot of the visible area, 'giant' makes a screenshot of the "
01243 "whole map, 'no_con' hides the console to create the screenshot. 'big' or 'giant' "
01244 "screenshots are always drawn without console");
01245 return true;
01246 }
01247
01248 if (argc > 3) return false;
01249
01250 ScreenshotType type = SC_VIEWPORT;
01251 const char *name = NULL;
01252
01253 if (argc > 1) {
01254 if (strcmp(argv[1], "big") == 0) {
01255
01256 type = SC_ZOOMEDIN;
01257 if (argc > 2) name = argv[2];
01258 } else if (strcmp(argv[1], "giant") == 0) {
01259
01260 type = SC_WORLD;
01261 if (argc > 2) name = argv[2];
01262 } else if (strcmp(argv[1], "no_con") == 0) {
01263
01264 IConsoleClose();
01265 if (argc > 2) name = argv[2];
01266 } else if (argc == 2) {
01267
01268 name = argv[1];
01269 } else {
01270
01271 return false;
01272 }
01273 }
01274
01275 MakeScreenshot(type, name);
01276 return true;
01277 }
01278
01279 DEF_CONSOLE_CMD(ConInfoCmd)
01280 {
01281 if (argc == 0) {
01282 IConsoleHelp("Print out debugging information about a command. Usage: 'info_cmd <cmd>'");
01283 return true;
01284 }
01285
01286 if (argc < 2) return false;
01287
01288 const IConsoleCmd *cmd = IConsoleCmdGet(argv[1]);
01289 if (cmd == NULL) {
01290 IConsoleError("the given command was not found");
01291 return true;
01292 }
01293
01294 IConsolePrintF(CC_DEFAULT, "command name: %s", cmd->name);
01295 IConsolePrintF(CC_DEFAULT, "command proc: %p", cmd->proc);
01296
01297 if (cmd->hook != NULL) IConsoleWarning("command is hooked");
01298
01299 return true;
01300 }
01301
01302 DEF_CONSOLE_CMD(ConDebugLevel)
01303 {
01304 if (argc == 0) {
01305 IConsoleHelp("Get/set the default debugging level for the game. Usage: 'debug_level [<level>]'");
01306 IConsoleHelp("Level can be any combination of names, levels. Eg 'net=5 ms=4'. Remember to enclose it in \"'s");
01307 return true;
01308 }
01309
01310 if (argc > 2) return false;
01311
01312 if (argc == 1) {
01313 IConsolePrintF(CC_DEFAULT, "Current debug-level: '%s'", GetDebugString());
01314 } else {
01315 SetDebugString(argv[1]);
01316 }
01317
01318 return true;
01319 }
01320
01321 DEF_CONSOLE_CMD(ConExit)
01322 {
01323 if (argc == 0) {
01324 IConsoleHelp("Exit the game. Usage: 'exit'");
01325 return true;
01326 }
01327
01328 if (_game_mode == GM_NORMAL && _settings_client.gui.autosave_on_exit) DoExitSave();
01329
01330 _exit_game = true;
01331 return true;
01332 }
01333
01334 DEF_CONSOLE_CMD(ConPart)
01335 {
01336 if (argc == 0) {
01337 IConsoleHelp("Leave the currently joined/running game (only ingame). Usage: 'part'");
01338 return true;
01339 }
01340
01341 if (_game_mode != GM_NORMAL) return false;
01342
01343 _switch_mode = SM_MENU;
01344 return true;
01345 }
01346
01347 DEF_CONSOLE_CMD(ConHelp)
01348 {
01349 if (argc == 2) {
01350 const IConsoleCmd *cmd;
01351 const IConsoleAlias *alias;
01352
01353 RemoveUnderscores(argv[1]);
01354 cmd = IConsoleCmdGet(argv[1]);
01355 if (cmd != NULL) {
01356 cmd->proc(0, NULL);
01357 return true;
01358 }
01359
01360 alias = IConsoleAliasGet(argv[1]);
01361 if (alias != NULL) {
01362 cmd = IConsoleCmdGet(alias->cmdline);
01363 if (cmd != NULL) {
01364 cmd->proc(0, NULL);
01365 return true;
01366 }
01367 IConsolePrintF(CC_ERROR, "ERROR: alias is of special type, please see its execution-line: '%s'", alias->cmdline);
01368 return true;
01369 }
01370
01371 IConsoleError("command not found");
01372 return true;
01373 }
01374
01375 IConsolePrint(CC_WARNING, " ---- OpenTTD Console Help ---- ");
01376 IConsolePrint(CC_DEFAULT, " - commands: [command to list all commands: list_cmds]");
01377 IConsolePrint(CC_DEFAULT, " call commands with '<command> <arg2> <arg3>...'");
01378 IConsolePrint(CC_DEFAULT, " - to assign strings, or use them as arguments, enclose it within quotes");
01379 IConsolePrint(CC_DEFAULT, " like this: '<command> \"string argument with spaces\"'");
01380 IConsolePrint(CC_DEFAULT, " - use 'help <command>' to get specific information");
01381 IConsolePrint(CC_DEFAULT, " - scroll console output with shift + (up | down) | (pageup | pagedown))");
01382 IConsolePrint(CC_DEFAULT, " - scroll console input history with the up | down arrows");
01383 IConsolePrint(CC_DEFAULT, "");
01384 return true;
01385 }
01386
01387 DEF_CONSOLE_CMD(ConListCommands)
01388 {
01389 if (argc == 0) {
01390 IConsoleHelp("List all registered commands. Usage: 'list_cmds [<pre-filter>]'");
01391 return true;
01392 }
01393
01394 for (const IConsoleCmd *cmd = _iconsole_cmds; cmd != NULL; cmd = cmd->next) {
01395 if (argv[1] == NULL || strstr(cmd->name, argv[1]) != NULL) {
01396 if (cmd->hook == NULL || cmd->hook(false) != CHR_HIDE) IConsolePrintF(CC_DEFAULT, "%s", cmd->name);
01397 }
01398 }
01399
01400 return true;
01401 }
01402
01403 DEF_CONSOLE_CMD(ConListAliases)
01404 {
01405 if (argc == 0) {
01406 IConsoleHelp("List all registered aliases. Usage: 'list_aliases [<pre-filter>]'");
01407 return true;
01408 }
01409
01410 for (const IConsoleAlias *alias = _iconsole_aliases; alias != NULL; alias = alias->next) {
01411 if (argv[1] == NULL || strstr(alias->name, argv[1]) != NULL) {
01412 IConsolePrintF(CC_DEFAULT, "%s => %s", alias->name, alias->cmdline);
01413 }
01414 }
01415
01416 return true;
01417 }
01418
01419 #ifdef ENABLE_NETWORK
01420
01421 DEF_CONSOLE_CMD(ConSay)
01422 {
01423 if (argc == 0) {
01424 IConsoleHelp("Chat to your fellow players in a multiplayer game. Usage: 'say \"<msg>\"'");
01425 return true;
01426 }
01427
01428 if (argc != 2) return false;
01429
01430 if (!_network_server) {
01431 NetworkClientSendChat(NETWORK_ACTION_CHAT, DESTTYPE_BROADCAST, 0 , argv[1]);
01432 } else {
01433 bool from_admin = (_redirect_console_to_admin < INVALID_ADMIN_ID);
01434 NetworkServerSendChat(NETWORK_ACTION_CHAT, DESTTYPE_BROADCAST, 0, argv[1], CLIENT_ID_SERVER, from_admin);
01435 }
01436
01437 return true;
01438 }
01439
01440 DEF_CONSOLE_CMD(ConCompanies)
01441 {
01442 if (argc == 0) {
01443 IConsoleHelp("List the in-game details of all clients connected to the server. Usage 'companies'");
01444 return true;
01445 }
01446 NetworkCompanyStats company_stats[MAX_COMPANIES];
01447 NetworkPopulateCompanyStats(company_stats);
01448
01449 Company *c;
01450 FOR_ALL_COMPANIES(c) {
01451
01452 char company_name[NETWORK_COMPANY_NAME_LENGTH];
01453 SetDParam(0, c->index);
01454 GetString(company_name, STR_COMPANY_NAME, lastof(company_name));
01455
01456 char buffer[512];
01457 const NetworkCompanyStats *stats = &company_stats[c->index];
01458
01459 GetString(buffer, STR_COLOUR_DARK_BLUE + _company_colours[c->index], lastof(buffer));
01460 IConsolePrintF(CC_INFO, "#:%d(%s) Company Name: '%s' Year Founded: %d Money: " OTTD_PRINTF64 " Loan: " OTTD_PRINTF64 " Value: " OTTD_PRINTF64 " (T:%d, R:%d, P:%d, S:%d) %sprotected",
01461 c->index + 1, buffer, company_name, c->inaugurated_year, (int64)c->money, (int64)c->current_loan, (int64)CalculateCompanyValue(c),
01462 stats->num_vehicle[0],
01463 stats->num_vehicle[1] + stats->num_vehicle[2],
01464 stats->num_vehicle[3],
01465 stats->num_vehicle[4],
01466 StrEmpty(_network_company_states[c->index].password) ? "un" : "");
01467 }
01468
01469 return true;
01470 }
01471
01472 DEF_CONSOLE_CMD(ConSayCompany)
01473 {
01474 if (argc == 0) {
01475 IConsoleHelp("Chat to a certain company in a multiplayer game. Usage: 'say_company <company-no> \"<msg>\"'");
01476 IConsoleHelp("CompanyNo is the company that plays as company <companyno>, 1 through max_companies");
01477 return true;
01478 }
01479
01480 if (argc != 3) return false;
01481
01482 CompanyID company_id = (CompanyID)(atoi(argv[1]) - 1);
01483 if (!Company::IsValidID(company_id)) {
01484 IConsolePrintF(CC_DEFAULT, "Unknown company. Company range is between 1 and %d.", MAX_COMPANIES);
01485 return true;
01486 }
01487
01488 if (!_network_server) {
01489 NetworkClientSendChat(NETWORK_ACTION_CHAT_COMPANY, DESTTYPE_TEAM, company_id, argv[2]);
01490 } else {
01491 bool from_admin = (_redirect_console_to_admin < INVALID_ADMIN_ID);
01492 NetworkServerSendChat(NETWORK_ACTION_CHAT_COMPANY, DESTTYPE_TEAM, company_id, argv[2], CLIENT_ID_SERVER, from_admin);
01493 }
01494
01495 return true;
01496 }
01497
01498 DEF_CONSOLE_CMD(ConSayClient)
01499 {
01500 if (argc == 0) {
01501 IConsoleHelp("Chat to a certain client in a multiplayer game. Usage: 'say_client <client-no> \"<msg>\"'");
01502 IConsoleHelp("For client-id's, see the command 'clients'");
01503 return true;
01504 }
01505
01506 if (argc != 3) return false;
01507
01508 if (!_network_server) {
01509 NetworkClientSendChat(NETWORK_ACTION_CHAT_CLIENT, DESTTYPE_CLIENT, atoi(argv[1]), argv[2]);
01510 } else {
01511 bool from_admin = (_redirect_console_to_admin < INVALID_ADMIN_ID);
01512 NetworkServerSendChat(NETWORK_ACTION_CHAT_CLIENT, DESTTYPE_CLIENT, atoi(argv[1]), argv[2], CLIENT_ID_SERVER, from_admin);
01513 }
01514
01515 return true;
01516 }
01517
01518 DEF_CONSOLE_CMD(ConCompanyPassword)
01519 {
01520 if (argc == 0) {
01521 const char *helpmsg;
01522
01523 if (_network_dedicated) {
01524 helpmsg = "Change the password of a company. Usage: 'company_pw <company-no> \"<password>\"";
01525 } else if (_network_server) {
01526 helpmsg = "Change the password of your or any other company. Usage: 'company_pw [<company-no>] \"<password>\"'";
01527 } else {
01528 helpmsg = "Change the password of your company. Usage: 'company_pw \"<password>\"'";
01529 }
01530
01531 IConsoleHelp(helpmsg);
01532 IConsoleHelp("Use \"*\" to disable the password.");
01533 return true;
01534 }
01535
01536 CompanyID company_id;
01537 const char *password;
01538 const char *errormsg;
01539
01540 if (argc == 2) {
01541 company_id = _local_company;
01542 password = argv[1];
01543 errormsg = "You have to own a company to make use of this command.";
01544 } else if (argc == 3 && _network_server) {
01545 company_id = (CompanyID)(atoi(argv[1]) - 1);
01546 password = argv[2];
01547 errormsg = "You have to specify the ID of a valid human controlled company.";
01548 } else {
01549 return false;
01550 }
01551
01552 if (!Company::IsValidHumanID(company_id)) {
01553 IConsoleError(errormsg);
01554 return false;
01555 }
01556
01557 password = NetworkChangeCompanyPassword(company_id, password, false);
01558
01559 if (StrEmpty(password)) {
01560 IConsolePrintF(CC_WARNING, "Company password cleared");
01561 } else {
01562 IConsolePrintF(CC_WARNING, "Company password changed to: %s", password);
01563 }
01564
01565 return true;
01566 }
01567
01568
01569 #if defined(WITH_ZLIB)
01570 #include "network/network_content.h"
01571
01573 static ContentType StringToContentType(const char *str)
01574 {
01575 static const char * const inv_lookup[] = { "", "base", "newgrf", "ai", "ailib", "scenario", "heightmap" };
01576 for (uint i = 1 ; i < lengthof(inv_lookup); i++) {
01577 if (strcasecmp(str, inv_lookup[i]) == 0) return (ContentType)i;
01578 }
01579 return CONTENT_TYPE_END;
01580 }
01581
01583 struct ConsoleContentCallback : public ContentCallback {
01584 void OnConnect(bool success)
01585 {
01586 IConsolePrintF(CC_DEFAULT, "Content server connection %s", success ? "established" : "failed");
01587 }
01588
01589 void OnDisconnect()
01590 {
01591 IConsolePrintF(CC_DEFAULT, "Content server connection closed");
01592 }
01593
01594 void OnDownloadComplete(ContentID cid)
01595 {
01596 IConsolePrintF(CC_DEFAULT, "Completed download of %d", cid);
01597 }
01598 };
01599
01600 DEF_CONSOLE_CMD(ConContent)
01601 {
01602 static ContentCallback *cb = NULL;
01603 if (cb == NULL) {
01604 cb = new ConsoleContentCallback();
01605 _network_content_client.AddCallback(cb);
01606 }
01607
01608 if (argc <= 1) {
01609 IConsoleHelp("Query, select and download content. Usage: 'content update|upgrade|select [all|id]|unselect [all|id]|state|download'");
01610 IConsoleHelp(" update: get a new list of downloadable content; must be run first");
01611 IConsoleHelp(" upgrade: select all items that are upgrades");
01612 IConsoleHelp(" select: select a specific item given by its id or 'all' to select all");
01613 IConsoleHelp(" unselect: unselect a specific item given by its id or 'all' to unselect all");
01614 IConsoleHelp(" state: show the download/select state of all downloadable content");
01615 IConsoleHelp(" download: download all content you've selected");
01616 return true;
01617 }
01618
01619 if (strcasecmp(argv[1], "update") == 0) {
01620 _network_content_client.RequestContentList((argc > 2) ? StringToContentType(argv[2]) : CONTENT_TYPE_END);
01621 return true;
01622 }
01623
01624 if (strcasecmp(argv[1], "upgrade") == 0) {
01625 _network_content_client.SelectUpgrade();
01626 return true;
01627 }
01628
01629 if (strcasecmp(argv[1], "select") == 0) {
01630 if (argc <= 2) {
01631 IConsoleError("You must enter the id.");
01632 return false;
01633 }
01634 if (strcasecmp(argv[2], "all") == 0) {
01635 _network_content_client.SelectAll();
01636 } else {
01637 _network_content_client.Select((ContentID)atoi(argv[2]));
01638 }
01639 return true;
01640 }
01641
01642 if (strcasecmp(argv[1], "unselect") == 0) {
01643 if (argc <= 2) {
01644 IConsoleError("You must enter the id.");
01645 return false;
01646 }
01647 if (strcasecmp(argv[2], "all") == 0) {
01648 _network_content_client.UnselectAll();
01649 } else {
01650 _network_content_client.Unselect((ContentID)atoi(argv[2]));
01651 }
01652 return true;
01653 }
01654
01655 if (strcasecmp(argv[1], "state") == 0) {
01656 IConsolePrintF(CC_WHITE, "id, type, state, name");
01657 for (ConstContentIterator iter = _network_content_client.Begin(); iter != _network_content_client.End(); iter++) {
01658 static const char * const types[] = { "Base graphics", "NewGRF", "AI", "AI library", "Scenario", "Heightmap", "Base sound", "Base music" };
01659 assert_compile(lengthof(types) == CONTENT_TYPE_END - CONTENT_TYPE_BEGIN);
01660 static const char * const states[] = { "Not selected", "Selected", "Dep Selected", "Installed", "Unknown" };
01661 static const TextColour state_to_colour[] = { CC_COMMAND, CC_INFO, CC_INFO, CC_WHITE, CC_ERROR };
01662
01663 const ContentInfo *ci = *iter;
01664 IConsolePrintF(state_to_colour[ci->state], "%d, %s, %s, %s", ci->id, types[ci->type - 1], states[ci->state], ci->name);
01665 }
01666 return true;
01667 }
01668
01669 if (strcasecmp(argv[1], "download") == 0) {
01670 uint files;
01671 uint bytes;
01672 _network_content_client.DownloadSelectedContent(files, bytes);
01673 IConsolePrintF(CC_DEFAULT, "Downloading %d file(s) (%d bytes)", files, bytes);
01674 return true;
01675 }
01676
01677 return false;
01678 }
01679 #endif
01680 #endif
01681
01682 DEF_CONSOLE_CMD(ConSetting)
01683 {
01684 if (argc == 0) {
01685 IConsoleHelp("Change setting for all clients. Usage: 'setting <name> [<value>]'");
01686 IConsoleHelp("Omitting <value> will print out the current value of the setting.");
01687 return true;
01688 }
01689
01690 if (argc == 1 || argc > 3) return false;
01691
01692 if (argc == 2) {
01693 IConsoleGetSetting(argv[1]);
01694 } else {
01695 IConsoleSetSetting(argv[1], argv[2]);
01696 }
01697
01698 return true;
01699 }
01700
01701 DEF_CONSOLE_CMD(ConSettingNewgame)
01702 {
01703 if (argc == 0) {
01704 IConsoleHelp("Change setting for the next game. Usage: 'setting_newgame <name> [<value>]'");
01705 IConsoleHelp("Omitting <value> will print out the current value of the setting.");
01706 return true;
01707 }
01708
01709 if (argc == 1 || argc > 3) return false;
01710
01711 if (argc == 2) {
01712 IConsoleGetSetting(argv[1], true);
01713 } else {
01714 IConsoleSetSetting(argv[1], argv[2], true);
01715 }
01716
01717 return true;
01718 }
01719
01720 DEF_CONSOLE_CMD(ConListSettings)
01721 {
01722 if (argc == 0) {
01723 IConsoleHelp("List settings. Usage: 'list_settings [<pre-filter>]'");
01724 return true;
01725 }
01726
01727 if (argc > 2) return false;
01728
01729 IConsoleListSettings((argc == 2) ? argv[1] : NULL);
01730 return true;
01731 }
01732
01733 DEF_CONSOLE_CMD(ConGamelogPrint)
01734 {
01735 GamelogPrintConsole();
01736 return true;
01737 }
01738
01739 DEF_CONSOLE_CMD(ConNewGRFReload)
01740 {
01741 if (argc == 0) {
01742 IConsoleHelp("Reloads all active NewGRFs from disk. Equivalent to reapplying NewGRFs via the settings, but without asking for confirmation. This might crash OpenTTD!");
01743 return true;
01744 }
01745
01746 ReloadNewGRFData();
01747 return true;
01748 }
01749
01750 #ifdef _DEBUG
01751
01752
01753
01754
01755 static void IConsoleDebugLibRegister()
01756 {
01757 IConsoleCmdRegister("resettile", ConResetTile);
01758 IConsoleAliasRegister("dbg_echo", "echo %A; echo %B");
01759 IConsoleAliasRegister("dbg_echo2", "echo %!");
01760 }
01761 #endif
01762
01763
01764
01765
01766
01767 void IConsoleStdLibRegister()
01768 {
01769 IConsoleCmdRegister("debug_level", ConDebugLevel);
01770 IConsoleCmdRegister("echo", ConEcho);
01771 IConsoleCmdRegister("echoc", ConEchoC);
01772 IConsoleCmdRegister("exec", ConExec);
01773 IConsoleCmdRegister("exit", ConExit);
01774 IConsoleCmdRegister("part", ConPart);
01775 IConsoleCmdRegister("help", ConHelp);
01776 IConsoleCmdRegister("info_cmd", ConInfoCmd);
01777 IConsoleCmdRegister("list_cmds", ConListCommands);
01778 IConsoleCmdRegister("list_aliases", ConListAliases);
01779 IConsoleCmdRegister("newgame", ConNewGame);
01780 IConsoleCmdRegister("restart", ConRestart);
01781 IConsoleCmdRegister("getseed", ConGetSeed);
01782 IConsoleCmdRegister("getdate", ConGetDate);
01783 IConsoleCmdRegister("quit", ConExit);
01784 IConsoleCmdRegister("resetengines", ConResetEngines, ConHookNoNetwork);
01785 IConsoleCmdRegister("return", ConReturn);
01786 IConsoleCmdRegister("screenshot", ConScreenShot);
01787 IConsoleCmdRegister("script", ConScript);
01788 IConsoleCmdRegister("scrollto", ConScrollToTile);
01789 IConsoleCmdRegister("alias", ConAlias);
01790 IConsoleCmdRegister("load", ConLoad);
01791 IConsoleCmdRegister("rm", ConRemove);
01792 IConsoleCmdRegister("save", ConSave);
01793 IConsoleCmdRegister("saveconfig", ConSaveConfig);
01794 IConsoleCmdRegister("ls", ConListFiles);
01795 IConsoleCmdRegister("cd", ConChangeDirectory);
01796 IConsoleCmdRegister("pwd", ConPrintWorkingDirectory);
01797 IConsoleCmdRegister("clear", ConClearBuffer);
01798 IConsoleCmdRegister("setting", ConSetting);
01799 IConsoleCmdRegister("setting_newgame", ConSettingNewgame);
01800 IConsoleCmdRegister("list_settings",ConListSettings);
01801 IConsoleCmdRegister("gamelog", ConGamelogPrint);
01802 IConsoleCmdRegister("rescan_newgrf", ConRescanNewGRF);
01803
01804 IConsoleAliasRegister("dir", "ls");
01805 IConsoleAliasRegister("del", "rm %+");
01806 IConsoleAliasRegister("newmap", "newgame");
01807 IConsoleAliasRegister("patch", "setting %+");
01808 IConsoleAliasRegister("set", "setting %+");
01809 IConsoleAliasRegister("set_newgame", "setting_newgame %+");
01810 IConsoleAliasRegister("list_patches", "list_settings %+");
01811 IConsoleAliasRegister("developer", "setting developer %+");
01812
01813 #ifdef ENABLE_AI
01814 IConsoleCmdRegister("list_ai_libs", ConListAILibs);
01815 IConsoleCmdRegister("list_ai", ConListAI);
01816 IConsoleCmdRegister("reload_ai", ConReloadAI);
01817 IConsoleCmdRegister("rescan_ai", ConRescanAI);
01818 IConsoleCmdRegister("start_ai", ConStartAI);
01819 IConsoleCmdRegister("stop_ai", ConStopAI);
01820 #endif
01821
01822
01823 #ifdef ENABLE_NETWORK
01824
01825 #if defined(WITH_ZLIB)
01826 IConsoleCmdRegister("content", ConContent);
01827 #endif
01828
01829
01830 IConsoleCmdRegister("say", ConSay, ConHookNeedNetwork);
01831 IConsoleCmdRegister("companies", ConCompanies, ConHookServerOnly);
01832 IConsoleAliasRegister("players", "companies");
01833 IConsoleCmdRegister("say_company", ConSayCompany, ConHookNeedNetwork);
01834 IConsoleAliasRegister("say_player", "say_company %+");
01835 IConsoleCmdRegister("say_client", ConSayClient, ConHookNeedNetwork);
01836
01837 IConsoleCmdRegister("connect", ConNetworkConnect, ConHookClientOnly);
01838 IConsoleCmdRegister("clients", ConNetworkClients, ConHookNeedNetwork);
01839 IConsoleCmdRegister("status", ConStatus, ConHookServerOnly);
01840 IConsoleCmdRegister("server_info", ConServerInfo, ConHookServerOnly);
01841 IConsoleAliasRegister("info", "server_info");
01842 IConsoleCmdRegister("reconnect", ConNetworkReconnect, ConHookClientOnly);
01843 IConsoleCmdRegister("rcon", ConRcon, ConHookNeedNetwork);
01844
01845 IConsoleCmdRegister("join", ConJoinCompany, ConHookNeedNetwork);
01846 IConsoleAliasRegister("spectate", "join 255");
01847 IConsoleCmdRegister("move", ConMoveClient, ConHookServerOnly);
01848 IConsoleCmdRegister("reset_company", ConResetCompany, ConHookServerOnly);
01849 IConsoleAliasRegister("clean_company", "reset_company %A");
01850 IConsoleCmdRegister("client_name", ConClientNickChange, ConHookServerOnly);
01851 IConsoleCmdRegister("kick", ConKick, ConHookServerOnly);
01852 IConsoleCmdRegister("ban", ConBan, ConHookServerOnly);
01853 IConsoleCmdRegister("unban", ConUnBan, ConHookServerOnly);
01854 IConsoleCmdRegister("banlist", ConBanList, ConHookServerOnly);
01855
01856 IConsoleCmdRegister("pause", ConPauseGame, ConHookServerOnly);
01857 IConsoleCmdRegister("unpause", ConUnPauseGame, ConHookServerOnly);
01858
01859 IConsoleCmdRegister("company_pw", ConCompanyPassword, ConHookNeedNetwork);
01860 IConsoleAliasRegister("company_password", "company_pw %+");
01861
01862 IConsoleAliasRegister("net_frame_freq", "setting frame_freq %+");
01863 IConsoleAliasRegister("net_sync_freq", "setting sync_freq %+");
01864 IConsoleAliasRegister("server_pw", "setting server_password %+");
01865 IConsoleAliasRegister("server_password", "setting server_password %+");
01866 IConsoleAliasRegister("rcon_pw", "setting rcon_password %+");
01867 IConsoleAliasRegister("rcon_password", "setting rcon_password %+");
01868 IConsoleAliasRegister("name", "setting client_name %+");
01869 IConsoleAliasRegister("server_name", "setting server_name %+");
01870 IConsoleAliasRegister("server_port", "setting server_port %+");
01871 IConsoleAliasRegister("server_advertise", "setting server_advertise %+");
01872 IConsoleAliasRegister("max_clients", "setting max_clients %+");
01873 IConsoleAliasRegister("max_companies", "setting max_companies %+");
01874 IConsoleAliasRegister("max_spectators", "setting max_spectators %+");
01875 IConsoleAliasRegister("max_join_time", "setting max_join_time %+");
01876 IConsoleAliasRegister("pause_on_join", "setting pause_on_join %+");
01877 IConsoleAliasRegister("autoclean_companies", "setting autoclean_companies %+");
01878 IConsoleAliasRegister("autoclean_protected", "setting autoclean_protected %+");
01879 IConsoleAliasRegister("autoclean_unprotected", "setting autoclean_unprotected %+");
01880 IConsoleAliasRegister("restart_game_year", "setting restart_game_year %+");
01881 IConsoleAliasRegister("min_players", "setting min_active_clients %+");
01882 IConsoleAliasRegister("reload_cfg", "setting reload_cfg %+");
01883 #endif
01884
01885
01886 #ifdef _DEBUG
01887 IConsoleDebugLibRegister();
01888 #endif
01889
01890
01891 IConsoleCmdRegister("reload_newgrfs", ConNewGRFReload, ConHookNewGRFDeveloperTool);
01892 }