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