OpenTTD
console_cmds.cpp
Go to the documentation of this file.
1 /* $Id: console_cmds.cpp 27914 2017-09-10 14:03:29Z adf88 $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #include "stdafx.h"
13 #include "console_internal.h"
14 #include "debug.h"
15 #include "engine_func.h"
16 #include "landscape.h"
17 #include "saveload/saveload.h"
18 #include "network/network.h"
19 #include "network/network_func.h"
20 #include "network/network_base.h"
21 #include "network/network_admin.h"
22 #include "network/network_client.h"
23 #include "command_func.h"
24 #include "settings_func.h"
25 #include "fios.h"
26 #include "fileio_func.h"
27 #include "screenshot.h"
28 #include "genworld.h"
29 #include "strings_func.h"
30 #include "viewport_func.h"
31 #include "window_func.h"
32 #include "date_func.h"
33 #include "company_func.h"
34 #include "gamelog.h"
35 #include "ai/ai.hpp"
36 #include "ai/ai_config.hpp"
37 #include "newgrf.h"
38 #include "console_func.h"
39 #include "engine_base.h"
40 #include "game/game.hpp"
41 #include "table/strings.h"
42 
43 #include "safeguards.h"
44 
45 /* scriptfile handling */
46 static bool _script_running;
47 
49 class ConsoleFileList : public FileList {
50 public:
52  {
53  this->file_list_valid = false;
54  }
55 
58  {
59  this->Clear();
60  this->file_list_valid = false;
61  }
62 
67  void ValidateFileList(bool force_reload = false)
68  {
69  if (force_reload || !this->file_list_valid) {
71  this->file_list_valid = true;
72  }
73  }
74 
76 };
77 
79 
80 /* console command defines */
81 #define DEF_CONSOLE_CMD(function) static bool function(byte argc, char *argv[])
82 #define DEF_CONSOLE_HOOK(function) static ConsoleHookResult function(bool echo)
83 
84 
85 /****************
86  * command hooks
87  ****************/
88 
89 #ifdef ENABLE_NETWORK
90 
95 static inline bool NetworkAvailable(bool echo)
96 {
97  if (!_network_available) {
98  if (echo) IConsoleError("You cannot use this command because there is no network available.");
99  return false;
100  }
101  return true;
102 }
103 
108 DEF_CONSOLE_HOOK(ConHookServerOnly)
109 {
110  if (!NetworkAvailable(echo)) return CHR_DISALLOW;
111 
112  if (!_network_server) {
113  if (echo) IConsoleError("This command is only available to a network server.");
114  return CHR_DISALLOW;
115  }
116  return CHR_ALLOW;
117 }
118 
123 DEF_CONSOLE_HOOK(ConHookClientOnly)
124 {
125  if (!NetworkAvailable(echo)) return CHR_DISALLOW;
126 
127  if (_network_server) {
128  if (echo) IConsoleError("This command is not available to a network server.");
129  return CHR_DISALLOW;
130  }
131  return CHR_ALLOW;
132 }
133 
138 DEF_CONSOLE_HOOK(ConHookNeedNetwork)
139 {
140  if (!NetworkAvailable(echo)) return CHR_DISALLOW;
141 
143  if (echo) IConsoleError("Not connected. This command is only available in multiplayer.");
144  return CHR_DISALLOW;
145  }
146  return CHR_ALLOW;
147 }
148 
153 DEF_CONSOLE_HOOK(ConHookNoNetwork)
154 {
155  if (_networking) {
156  if (echo) IConsoleError("This command is forbidden in multiplayer.");
157  return CHR_DISALLOW;
158  }
159  return CHR_ALLOW;
160 }
161 
162 #else
163 # define ConHookNoNetwork NULL
164 #endif /* ENABLE_NETWORK */
165 
166 DEF_CONSOLE_HOOK(ConHookNewGRFDeveloperTool)
167 {
169  if (_game_mode == GM_MENU) {
170  if (echo) IConsoleError("This command is only available in game and editor.");
171  return CHR_DISALLOW;
172  }
173 #ifdef ENABLE_NETWORK
174  return ConHookNoNetwork(echo);
175 #else
176  return CHR_ALLOW;
177 #endif
178  }
179  return CHR_HIDE;
180 }
181 
186 static void IConsoleHelp(const char *str)
187 {
188  IConsolePrintF(CC_WARNING, "- %s", str);
189 }
190 
195 DEF_CONSOLE_CMD(ConResetEngines)
196 {
197  if (argc == 0) {
198  IConsoleHelp("Reset status data of all engines. This might solve some issues with 'lost' engines. Usage: 'resetengines'");
199  return true;
200  }
201 
202  StartupEngines();
203  return true;
204 }
205 
211 DEF_CONSOLE_CMD(ConResetEnginePool)
212 {
213  if (argc == 0) {
214  IConsoleHelp("Reset NewGRF allocations of engine slots. This will remove invalid engine definitions, and might make default engines available again.");
215  return true;
216  }
217 
218  if (_game_mode == GM_MENU) {
219  IConsoleError("This command is only available in game and editor.");
220  return true;
221  }
222 
224  IConsoleError("This can only be done when there are no vehicles in the game.");
225  return true;
226  }
227 
228  return true;
229 }
230 
231 #ifdef _DEBUG
232 
237 DEF_CONSOLE_CMD(ConResetTile)
238 {
239  if (argc == 0) {
240  IConsoleHelp("Reset a tile to bare land. Usage: 'resettile <tile>'");
241  IConsoleHelp("Tile can be either decimal (34161) or hexadecimal (0x4a5B)");
242  return true;
243  }
244 
245  if (argc == 2) {
246  uint32 result;
247  if (GetArgumentInteger(&result, argv[1])) {
248  DoClearSquare((TileIndex)result);
249  return true;
250  }
251  }
252 
253  return false;
254 }
255 #endif /* _DEBUG */
256 
266 DEF_CONSOLE_CMD(ConScrollToTile)
267 {
268  switch (argc) {
269  case 0:
270  IConsoleHelp("Center the screen on a given tile.");
271  IConsoleHelp("Usage: 'scrollto <tile>' or 'scrollto <x> <y>'");
272  IConsoleHelp("Numbers can be either decimal (34161) or hexadecimal (0x4a5B).");
273  return true;
274 
275  case 2: {
276  uint32 result;
277  if (GetArgumentInteger(&result, argv[1])) {
278  if (result >= MapSize()) {
279  IConsolePrint(CC_ERROR, "Tile does not exist");
280  return true;
281  }
283  return true;
284  }
285  break;
286  }
287 
288  case 3: {
289  uint32 x, y;
290  if (GetArgumentInteger(&x, argv[1]) && GetArgumentInteger(&y, argv[2])) {
291  if (x >= MapSizeX() || y >= MapSizeY()) {
292  IConsolePrint(CC_ERROR, "Tile does not exist");
293  return true;
294  }
296  return true;
297  }
298  break;
299  }
300  }
301 
302  return false;
303 }
304 
311 {
312  if (argc == 0) {
313  IConsoleHelp("Save the current game. Usage: 'save <filename>'");
314  return true;
315  }
316 
317  if (argc == 2) {
318  char *filename = str_fmt("%s.sav", argv[1]);
319  IConsolePrint(CC_DEFAULT, "Saving map...");
320 
321  if (SaveOrLoad(filename, SLO_SAVE, DFT_GAME_FILE, SAVE_DIR) != SL_OK) {
322  IConsolePrint(CC_ERROR, "Saving map failed");
323  } else {
324  IConsolePrintF(CC_DEFAULT, "Map successfully saved to %s", filename);
325  }
326  free(filename);
327  return true;
328  }
329 
330  return false;
331 }
332 
337 DEF_CONSOLE_CMD(ConSaveConfig)
338 {
339  if (argc == 0) {
340  IConsoleHelp("Saves the configuration for new games to the configuration file, typically 'openttd.cfg'.");
341  IConsoleHelp("It does not save the configuration of the current game to the configuration file.");
342  return true;
343  }
344 
345  SaveToConfig();
346  IConsolePrint(CC_DEFAULT, "Saved config.");
347  return true;
348 }
349 
350 DEF_CONSOLE_CMD(ConLoad)
351 {
352  if (argc == 0) {
353  IConsoleHelp("Load a game by name or index. Usage: 'load <file | number>'");
354  return true;
355  }
356 
357  if (argc != 2) return false;
358 
359  const char *file = argv[1];
360  _console_file_list.ValidateFileList();
361  const FiosItem *item = _console_file_list.FindItem(file);
362  if (item != NULL) {
363  if (GetAbstractFileType(item->type) == FT_SAVEGAME) {
365  _file_to_saveload.SetMode(item->type);
367  _file_to_saveload.SetTitle(item->title);
368  } else {
369  IConsolePrintF(CC_ERROR, "%s: Not a savegame.", file);
370  }
371  } else {
372  IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file);
373  }
374 
375  return true;
376 }
377 
378 
379 DEF_CONSOLE_CMD(ConRemove)
380 {
381  if (argc == 0) {
382  IConsoleHelp("Remove a savegame by name or index. Usage: 'rm <file | number>'");
383  return true;
384  }
385 
386  if (argc != 2) return false;
387 
388  const char *file = argv[1];
389  _console_file_list.ValidateFileList();
390  const FiosItem *item = _console_file_list.FindItem(file);
391  if (item != NULL) {
392  if (!FiosDelete(item->name)) {
393  IConsolePrintF(CC_ERROR, "%s: Failed to delete file", file);
394  }
395  } else {
396  IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file);
397  }
398 
399  _console_file_list.InvalidateFileList();
400  return true;
401 }
402 
403 
404 /* List all the files in the current dir via console */
405 DEF_CONSOLE_CMD(ConListFiles)
406 {
407  if (argc == 0) {
408  IConsoleHelp("List all loadable savegames and directories in the current dir via console. Usage: 'ls | dir'");
409  return true;
410  }
411 
412  _console_file_list.ValidateFileList(true);
413  for (uint i = 0; i < _console_file_list.Length(); i++) {
414  IConsolePrintF(CC_DEFAULT, "%d) %s", i, _console_file_list[i].title);
415  }
416 
417  return true;
418 }
419 
420 /* Change the dir via console */
421 DEF_CONSOLE_CMD(ConChangeDirectory)
422 {
423  if (argc == 0) {
424  IConsoleHelp("Change the dir via console. Usage: 'cd <directory | number>'");
425  return true;
426  }
427 
428  if (argc != 2) return false;
429 
430  const char *file = argv[1];
431  _console_file_list.ValidateFileList(true);
432  const FiosItem *item = _console_file_list.FindItem(file);
433  if (item != NULL) {
434  switch (item->type) {
435  case FIOS_TYPE_DIR: case FIOS_TYPE_DRIVE: case FIOS_TYPE_PARENT:
436  FiosBrowseTo(item);
437  break;
438  default: IConsolePrintF(CC_ERROR, "%s: Not a directory.", file);
439  }
440  } else {
441  IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file);
442  }
443 
444  _console_file_list.InvalidateFileList();
445  return true;
446 }
447 
448 DEF_CONSOLE_CMD(ConPrintWorkingDirectory)
449 {
450  const char *path;
451 
452  if (argc == 0) {
453  IConsoleHelp("Print out the current working directory. Usage: 'pwd'");
454  return true;
455  }
456 
457  /* XXX - Workaround for broken file handling */
458  _console_file_list.ValidateFileList(true);
459  _console_file_list.InvalidateFileList();
460 
461  FiosGetDescText(&path, NULL);
462  IConsolePrint(CC_DEFAULT, path);
463  return true;
464 }
465 
466 DEF_CONSOLE_CMD(ConClearBuffer)
467 {
468  if (argc == 0) {
469  IConsoleHelp("Clear the console buffer. Usage: 'clear'");
470  return true;
471  }
472 
473  IConsoleClearBuffer();
475  return true;
476 }
477 
478 
479 /**********************************
480  * Network Core Console Commands
481  **********************************/
482 #ifdef ENABLE_NETWORK
483 
484 static bool ConKickOrBan(const char *argv, bool ban)
485 {
486  uint n;
487 
488  if (strchr(argv, '.') == NULL && strchr(argv, ':') == NULL) { // banning with ID
489  ClientID client_id = (ClientID)atoi(argv);
490 
491  /* Don't kill the server, or the client doing the rcon. The latter can't be kicked because
492  * kicking frees closes and subsequently free the connection related instances, which we
493  * would be reading from and writing to after returning. So we would read or write data
494  * from freed memory up till the segfault triggers. */
495  if (client_id == CLIENT_ID_SERVER || client_id == _redirect_console_to_client) {
496  IConsolePrintF(CC_ERROR, "ERROR: Silly boy, you can not %s yourself!", ban ? "ban" : "kick");
497  return true;
498  }
499 
501  if (ci == NULL) {
502  IConsoleError("Invalid client");
503  return true;
504  }
505 
506  if (!ban) {
507  /* Kick only this client, not all clients with that IP */
508  NetworkServerKickClient(client_id);
509  return true;
510  }
511 
512  /* When banning, kick+ban all clients with that IP */
513  n = NetworkServerKickOrBanIP(client_id, ban);
514  } else {
515  n = NetworkServerKickOrBanIP(argv, ban);
516  }
517 
518  if (n == 0) {
519  IConsolePrint(CC_DEFAULT, ban ? "Client not online, address added to banlist" : "Client not found");
520  } else {
521  IConsolePrintF(CC_DEFAULT, "%sed %u client(s)", ban ? "Bann" : "Kick", n);
522  }
523 
524  return true;
525 }
526 
527 DEF_CONSOLE_CMD(ConKick)
528 {
529  if (argc == 0) {
530  IConsoleHelp("Kick a client from a network game. Usage: 'kick <ip | client-id>'");
531  IConsoleHelp("For client-id's, see the command 'clients'");
532  return true;
533  }
534 
535  if (argc != 2) return false;
536 
537  return ConKickOrBan(argv[1], false);
538 }
539 
540 DEF_CONSOLE_CMD(ConBan)
541 {
542  if (argc == 0) {
543  IConsoleHelp("Ban a client from a network game. Usage: 'ban <ip | client-id>'");
544  IConsoleHelp("For client-id's, see the command 'clients'");
545  IConsoleHelp("If the client is no longer online, you can still ban his/her IP");
546  return true;
547  }
548 
549  if (argc != 2) return false;
550 
551  return ConKickOrBan(argv[1], true);
552 }
553 
554 DEF_CONSOLE_CMD(ConUnBan)
555 {
556  if (argc == 0) {
557  IConsoleHelp("Unban a client from a network game. Usage: 'unban <ip | banlist-index>'");
558  IConsoleHelp("For a list of banned IP's, see the command 'banlist'");
559  return true;
560  }
561 
562  if (argc != 2) return false;
563 
564  /* Try by IP. */
565  uint index;
566  for (index = 0; index < _network_ban_list.Length(); index++) {
567  if (strcmp(_network_ban_list[index], argv[1]) == 0) break;
568  }
569 
570  /* Try by index. */
571  if (index >= _network_ban_list.Length()) {
572  index = atoi(argv[1]) - 1U; // let it wrap
573  }
574 
575  if (index < _network_ban_list.Length()) {
576  char msg[64];
577  seprintf(msg, lastof(msg), "Unbanned %s", _network_ban_list[index]);
579  free(_network_ban_list[index]);
581  } else {
582  IConsolePrint(CC_DEFAULT, "Invalid list index or IP not in ban-list.");
583  IConsolePrint(CC_DEFAULT, "For a list of banned IP's, see the command 'banlist'");
584  }
585 
586  return true;
587 }
588 
589 DEF_CONSOLE_CMD(ConBanList)
590 {
591  if (argc == 0) {
592  IConsoleHelp("List the IP's of banned clients: Usage 'banlist'");
593  return true;
594  }
595 
596  IConsolePrint(CC_DEFAULT, "Banlist: ");
597 
598  uint i = 1;
599  for (char **iter = _network_ban_list.Begin(); iter != _network_ban_list.End(); iter++, i++) {
600  IConsolePrintF(CC_DEFAULT, " %d) %s", i, *iter);
601  }
602 
603  return true;
604 }
605 
606 DEF_CONSOLE_CMD(ConPauseGame)
607 {
608  if (argc == 0) {
609  IConsoleHelp("Pause a network game. Usage: 'pause'");
610  return true;
611  }
612 
614  DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
615  if (!_networking) IConsolePrint(CC_DEFAULT, "Game paused.");
616  } else {
617  IConsolePrint(CC_DEFAULT, "Game is already paused.");
618  }
619 
620  return true;
621 }
622 
623 DEF_CONSOLE_CMD(ConUnpauseGame)
624 {
625  if (argc == 0) {
626  IConsoleHelp("Unpause a network game. Usage: 'unpause'");
627  return true;
628  }
629 
631  DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE);
632  if (!_networking) IConsolePrint(CC_DEFAULT, "Game unpaused.");
633  } else if ((_pause_mode & PM_PAUSED_ERROR) != PM_UNPAUSED) {
634  IConsolePrint(CC_DEFAULT, "Game is in error state and cannot be unpaused via console.");
635  } else if (_pause_mode != PM_UNPAUSED) {
636  IConsolePrint(CC_DEFAULT, "Game cannot be unpaused manually; disable pause_on_join/min_active_clients.");
637  } else {
638  IConsolePrint(CC_DEFAULT, "Game is already unpaused.");
639  }
640 
641  return true;
642 }
643 
644 DEF_CONSOLE_CMD(ConRcon)
645 {
646  if (argc == 0) {
647  IConsoleHelp("Remote control the server from another client. Usage: 'rcon <password> <command>'");
648  IConsoleHelp("Remember to enclose the command in quotes, otherwise only the first parameter is sent");
649  return true;
650  }
651 
652  if (argc < 3) return false;
653 
654  if (_network_server) {
655  IConsoleCmdExec(argv[2]);
656  } else {
657  NetworkClientSendRcon(argv[1], argv[2]);
658  }
659  return true;
660 }
661 
662 DEF_CONSOLE_CMD(ConStatus)
663 {
664  if (argc == 0) {
665  IConsoleHelp("List the status of all clients connected to the server. Usage 'status'");
666  return true;
667  }
668 
670  return true;
671 }
672 
673 DEF_CONSOLE_CMD(ConServerInfo)
674 {
675  if (argc == 0) {
676  IConsoleHelp("List current and maximum client/company limits. Usage 'server_info'");
677  IConsoleHelp("You can change these values by modifying settings 'network.max_clients', 'network.max_companies' and 'network.max_spectators'");
678  return true;
679  }
680 
682  IConsolePrintF(CC_DEFAULT, "Current/maximum companies: %2d/%2d", (int)Company::GetNumItems(), _settings_client.network.max_companies);
683  IConsolePrintF(CC_DEFAULT, "Current/maximum spectators: %2d/%2d", NetworkSpectatorCount(), _settings_client.network.max_spectators);
684 
685  return true;
686 }
687 
688 DEF_CONSOLE_CMD(ConClientNickChange)
689 {
690  if (argc != 3) {
691  IConsoleHelp("Change the nickname of a connected client. Usage: 'client_name <client-id> <new-name>'");
692  IConsoleHelp("For client-id's, see the command 'clients'");
693  return true;
694  }
695 
696  ClientID client_id = (ClientID)atoi(argv[1]);
697 
698  if (client_id == CLIENT_ID_SERVER) {
699  IConsoleError("Please use the command 'name' to change your own name!");
700  return true;
701  }
702 
703  if (NetworkClientInfo::GetByClientID(client_id) == NULL) {
704  IConsoleError("Invalid client");
705  return true;
706  }
707 
708  if (!NetworkServerChangeClientName(client_id, argv[2])) {
709  IConsoleError("Cannot give a client a duplicate name");
710  }
711 
712  return true;
713 }
714 
715 DEF_CONSOLE_CMD(ConJoinCompany)
716 {
717  if (argc < 2) {
718  IConsoleHelp("Request joining another company. Usage: join <company-id> [<password>]");
719  IConsoleHelp("For valid company-id see company list, use 255 for spectator");
720  return true;
721  }
722 
723  CompanyID company_id = (CompanyID)(atoi(argv[1]) <= MAX_COMPANIES ? atoi(argv[1]) - 1 : atoi(argv[1]));
724 
725  /* Check we have a valid company id! */
726  if (!Company::IsValidID(company_id) && company_id != COMPANY_SPECTATOR) {
727  IConsolePrintF(CC_ERROR, "Company does not exist. Company-id must be between 1 and %d.", MAX_COMPANIES);
728  return true;
729  }
730 
731  if (NetworkClientInfo::GetByClientID(_network_own_client_id)->client_playas == company_id) {
732  IConsoleError("You are already there!");
733  return true;
734  }
735 
736  if (company_id == COMPANY_SPECTATOR && NetworkMaxSpectatorsReached()) {
737  IConsoleError("Cannot join spectators, maximum number of spectators reached.");
738  return true;
739  }
740 
741  if (company_id != COMPANY_SPECTATOR && !Company::IsHumanID(company_id)) {
742  IConsoleError("Cannot join AI company.");
743  return true;
744  }
745 
746  /* Check if the company requires a password */
747  if (NetworkCompanyIsPassworded(company_id) && argc < 3) {
748  IConsolePrintF(CC_ERROR, "Company %d requires a password to join.", company_id + 1);
749  return true;
750  }
751 
752  /* non-dedicated server may just do the move! */
753  if (_network_server) {
755  } else {
756  NetworkClientRequestMove(company_id, NetworkCompanyIsPassworded(company_id) ? argv[2] : "");
757  }
758 
759  return true;
760 }
761 
762 DEF_CONSOLE_CMD(ConMoveClient)
763 {
764  if (argc < 3) {
765  IConsoleHelp("Move a client to another company. Usage: move <client-id> <company-id>");
766  IConsoleHelp("For valid client-id see 'clients', for valid company-id see 'companies', use 255 for moving to spectators");
767  return true;
768  }
769 
770  const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID((ClientID)atoi(argv[1]));
771  CompanyID company_id = (CompanyID)(atoi(argv[2]) <= MAX_COMPANIES ? atoi(argv[2]) - 1 : atoi(argv[2]));
772 
773  /* check the client exists */
774  if (ci == NULL) {
775  IConsoleError("Invalid client-id, check the command 'clients' for valid client-id's.");
776  return true;
777  }
778 
779  if (!Company::IsValidID(company_id) && company_id != COMPANY_SPECTATOR) {
780  IConsolePrintF(CC_ERROR, "Company does not exist. Company-id must be between 1 and %d.", MAX_COMPANIES);
781  return true;
782  }
783 
784  if (company_id != COMPANY_SPECTATOR && !Company::IsHumanID(company_id)) {
785  IConsoleError("You cannot move clients to AI companies.");
786  return true;
787  }
788 
790  IConsoleError("Silly boy, you cannot move the server!");
791  return true;
792  }
793 
794  if (ci->client_playas == company_id) {
795  IConsoleError("You cannot move someone to where he/she already is!");
796  return true;
797  }
798 
799  /* we are the server, so force the update */
800  NetworkServerDoMove(ci->client_id, company_id);
801 
802  return true;
803 }
804 
805 DEF_CONSOLE_CMD(ConResetCompany)
806 {
807  if (argc == 0) {
808  IConsoleHelp("Remove an idle company from the game. Usage: 'reset_company <company-id>'");
809  IConsoleHelp("For company-id's, see the list of companies from the dropdown menu. Company 1 is 1, etc.");
810  return true;
811  }
812 
813  if (argc != 2) return false;
814 
815  CompanyID index = (CompanyID)(atoi(argv[1]) - 1);
816 
817  /* Check valid range */
818  if (!Company::IsValidID(index)) {
819  IConsolePrintF(CC_ERROR, "Company does not exist. Company-id must be between 1 and %d.", MAX_COMPANIES);
820  return true;
821  }
822 
823  if (!Company::IsHumanID(index)) {
824  IConsoleError("Company is owned by an AI.");
825  return true;
826  }
827 
828  if (NetworkCompanyHasClients(index)) {
829  IConsoleError("Cannot remove company: a client is connected to that company.");
830  return false;
831  }
833  if (ci->client_playas == index) {
834  IConsoleError("Cannot remove company: the server is connected to that company.");
835  return true;
836  }
837 
838  /* It is safe to remove this company */
839  DoCommandP(0, 2 | index << 16, CRR_MANUAL, CMD_COMPANY_CTRL);
840  IConsolePrint(CC_DEFAULT, "Company deleted.");
841 
842  return true;
843 }
844 
845 DEF_CONSOLE_CMD(ConNetworkClients)
846 {
847  if (argc == 0) {
848  IConsoleHelp("Get a list of connected clients including their ID, name, company-id, and IP. Usage: 'clients'");
849  return true;
850  }
851 
853 
854  return true;
855 }
856 
857 DEF_CONSOLE_CMD(ConNetworkReconnect)
858 {
859  if (argc == 0) {
860  IConsoleHelp("Reconnect to server to which you were connected last time. Usage: 'reconnect [<company>]'");
861  IConsoleHelp("Company 255 is spectator (default, if not specified), 0 means creating new company.");
862  IConsoleHelp("All others are a certain company with Company 1 being #1");
863  return true;
864  }
865 
866  CompanyID playas = (argc >= 2) ? (CompanyID)atoi(argv[1]) : COMPANY_SPECTATOR;
867  switch (playas) {
868  case 0: playas = COMPANY_NEW_COMPANY; break;
869  case COMPANY_SPECTATOR: /* nothing to do */ break;
870  default:
871  /* From a user pov 0 is a new company, internally it's different and all
872  * companies are offset by one to ease up on users (eg companies 1-8 not 0-7) */
873  playas--;
874  if (playas < COMPANY_FIRST || playas >= MAX_COMPANIES) return false;
875  break;
876  }
877 
879  IConsolePrint(CC_DEFAULT, "No server for reconnecting.");
880  return true;
881  }
882 
883  /* Don't resolve the address first, just print it directly as it comes from the config file. */
885 
887  return true;
888 }
889 
890 DEF_CONSOLE_CMD(ConNetworkConnect)
891 {
892  if (argc == 0) {
893  IConsoleHelp("Connect to a remote OTTD server and join the game. Usage: 'connect <ip>'");
894  IConsoleHelp("IP can contain port and company: 'IP[:Port][#Company]', eg: 'server.ottd.org:443#2'");
895  IConsoleHelp("Company #255 is spectator all others are a certain company with Company 1 being #1");
896  return true;
897  }
898 
899  if (argc < 2) return false;
900  if (_networking) NetworkDisconnect(); // we are in network-mode, first close it!
901 
902  const char *port = NULL;
903  const char *company = NULL;
904  char *ip = argv[1];
905  /* Default settings: default port and new company */
906  uint16 rport = NETWORK_DEFAULT_PORT;
907  CompanyID join_as = COMPANY_NEW_COMPANY;
908 
909  ParseConnectionString(&company, &port, ip);
910 
911  IConsolePrintF(CC_DEFAULT, "Connecting to %s...", ip);
912  if (company != NULL) {
913  join_as = (CompanyID)atoi(company);
914  IConsolePrintF(CC_DEFAULT, " company-no: %d", join_as);
915 
916  /* From a user pov 0 is a new company, internally it's different and all
917  * companies are offset by one to ease up on users (eg companies 1-8 not 0-7) */
918  if (join_as != COMPANY_SPECTATOR) {
919  if (join_as > MAX_COMPANIES) return false;
920  join_as--;
921  }
922  }
923  if (port != NULL) {
924  rport = atoi(port);
925  IConsolePrintF(CC_DEFAULT, " port: %s", port);
926  }
927 
928  NetworkClientConnectGame(NetworkAddress(ip, rport), join_as);
929 
930  return true;
931 }
932 
933 #endif /* ENABLE_NETWORK */
934 
935 /*********************************
936  * script file console commands
937  *********************************/
938 
939 DEF_CONSOLE_CMD(ConExec)
940 {
941  if (argc == 0) {
942  IConsoleHelp("Execute a local script file. Usage: 'exec <script> <?>'");
943  return true;
944  }
945 
946  if (argc < 2) return false;
947 
948  FILE *script_file = FioFOpenFile(argv[1], "r", BASE_DIR);
949 
950  if (script_file == NULL) {
951  if (argc == 2 || atoi(argv[2]) != 0) IConsoleError("script file not found");
952  return true;
953  }
954 
955  _script_running = true;
956 
957  char cmdline[ICON_CMDLN_SIZE];
958  while (_script_running && fgets(cmdline, sizeof(cmdline), script_file) != NULL) {
959  /* Remove newline characters from the executing script */
960  for (char *cmdptr = cmdline; *cmdptr != '\0'; cmdptr++) {
961  if (*cmdptr == '\n' || *cmdptr == '\r') {
962  *cmdptr = '\0';
963  break;
964  }
965  }
966  IConsoleCmdExec(cmdline);
967  }
968 
969  if (ferror(script_file)) {
970  IConsoleError("Encountered error while trying to read from script file");
971  }
972 
973  _script_running = false;
974  FioFCloseFile(script_file);
975  return true;
976 }
977 
978 DEF_CONSOLE_CMD(ConReturn)
979 {
980  if (argc == 0) {
981  IConsoleHelp("Stop executing a running script. Usage: 'return'");
982  return true;
983  }
984 
985  _script_running = false;
986  return true;
987 }
988 
989 /*****************************
990  * default console commands
991  ******************************/
992 extern bool CloseConsoleLogIfActive();
993 
994 DEF_CONSOLE_CMD(ConScript)
995 {
996  extern FILE *_iconsole_output_file;
997 
998  if (argc == 0) {
999  IConsoleHelp("Start or stop logging console output to a file. Usage: 'script <filename>'");
1000  IConsoleHelp("If filename is omitted, a running log is stopped if it is active");
1001  return true;
1002  }
1003 
1004  if (!CloseConsoleLogIfActive()) {
1005  if (argc < 2) return false;
1006 
1007  IConsolePrintF(CC_DEFAULT, "file output started to: %s", argv[1]);
1008  _iconsole_output_file = fopen(argv[1], "ab");
1009  if (_iconsole_output_file == NULL) IConsoleError("could not open file");
1010  }
1011 
1012  return true;
1013 }
1014 
1015 
1016 DEF_CONSOLE_CMD(ConEcho)
1017 {
1018  if (argc == 0) {
1019  IConsoleHelp("Print back the first argument to the console. Usage: 'echo <arg>'");
1020  return true;
1021  }
1022 
1023  if (argc < 2) return false;
1024  IConsolePrint(CC_DEFAULT, argv[1]);
1025  return true;
1026 }
1027 
1028 DEF_CONSOLE_CMD(ConEchoC)
1029 {
1030  if (argc == 0) {
1031  IConsoleHelp("Print back the first argument to the console in a given colour. Usage: 'echoc <colour> <arg2>'");
1032  return true;
1033  }
1034 
1035  if (argc < 3) return false;
1036  IConsolePrint((TextColour)Clamp(atoi(argv[1]), TC_BEGIN, TC_END - 1), argv[2]);
1037  return true;
1038 }
1039 
1040 DEF_CONSOLE_CMD(ConNewGame)
1041 {
1042  if (argc == 0) {
1043  IConsoleHelp("Start a new game. Usage: 'newgame [seed]'");
1044  IConsoleHelp("The server can force a new game using 'newgame'; any client joined will rejoin after the server is done generating the new game.");
1045  return true;
1046  }
1047 
1048  StartNewGameWithoutGUI((argc == 2) ? strtoul(argv[1], NULL, 10) : GENERATE_NEW_SEED);
1049  return true;
1050 }
1051 
1052 DEF_CONSOLE_CMD(ConRestart)
1053 {
1054  if (argc == 0) {
1055  IConsoleHelp("Restart game. Usage: 'restart'");
1056  IConsoleHelp("Restarts a game. It tries to reproduce the exact same map as the game started with.");
1057  IConsoleHelp("However:");
1058  IConsoleHelp(" * restarting games started in another version might create another map due to difference in map generation");
1059  IConsoleHelp(" * restarting games based on scenarios, loaded games or heightmaps will start a new game based on the settings stored in the scenario/savegame");
1060  return true;
1061  }
1062 
1063  /* Don't copy the _newgame pointers to the real pointers, so call SwitchToMode directly */
1067  return true;
1068 }
1069 
1075 static void PrintLineByLine(char *buf)
1076 {
1077  char *p = buf;
1078  /* Print output line by line */
1079  for (char *p2 = buf; *p2 != '\0'; p2++) {
1080  if (*p2 == '\n') {
1081  *p2 = '\0';
1082  IConsolePrintF(CC_DEFAULT, "%s", p);
1083  p = p2 + 1;
1084  }
1085  }
1086 }
1087 
1088 DEF_CONSOLE_CMD(ConListAILibs)
1089 {
1090  char buf[4096];
1091  AI::GetConsoleLibraryList(buf, lastof(buf));
1092 
1093  PrintLineByLine(buf);
1094 
1095  return true;
1096 }
1097 
1098 DEF_CONSOLE_CMD(ConListAI)
1099 {
1100  char buf[4096];
1101  AI::GetConsoleList(buf, lastof(buf));
1102 
1103  PrintLineByLine(buf);
1104 
1105  return true;
1106 }
1107 
1108 DEF_CONSOLE_CMD(ConListGameLibs)
1109 {
1110  char buf[4096];
1112 
1113  PrintLineByLine(buf);
1114 
1115  return true;
1116 }
1117 
1118 DEF_CONSOLE_CMD(ConListGame)
1119 {
1120  char buf[4096];
1121  Game::GetConsoleList(buf, lastof(buf));
1122 
1123  PrintLineByLine(buf);
1124 
1125  return true;
1126 }
1127 
1128 DEF_CONSOLE_CMD(ConStartAI)
1129 {
1130  if (argc == 0 || argc > 3) {
1131  IConsoleHelp("Start a new AI. Usage: 'start_ai [<AI>] [<settings>]'");
1132  IConsoleHelp("Start a new AI. If <AI> is given, it starts that specific AI (if found).");
1133  IConsoleHelp("If <settings> is given, it is parsed and the AI settings are set to that.");
1134  return true;
1135  }
1136 
1137  if (_game_mode != GM_NORMAL) {
1138  IConsoleWarning("AIs can only be managed in a game.");
1139  return true;
1140  }
1141 
1143  IConsoleWarning("Can't start a new AI (no more free slots).");
1144  return true;
1145  }
1146  if (_networking && !_network_server) {
1147  IConsoleWarning("Only the server can start a new AI.");
1148  return true;
1149  }
1151  IConsoleWarning("AIs are not allowed in multiplayer by configuration.");
1152  IConsoleWarning("Switch AI -> AI in multiplayer to True.");
1153  return true;
1154  }
1155  if (!AI::CanStartNew()) {
1156  IConsoleWarning("Can't start a new AI.");
1157  return true;
1158  }
1159 
1160  int n = 0;
1161  Company *c;
1162  /* Find the next free slot */
1163  FOR_ALL_COMPANIES(c) {
1164  if (c->index != n) break;
1165  n++;
1166  }
1167 
1168  AIConfig *config = AIConfig::GetConfig((CompanyID)n);
1169  if (argc >= 2) {
1170  config->Change(argv[1], -1, true);
1171  if (!config->HasScript()) {
1172  IConsoleWarning("Failed to load the specified AI");
1173  return true;
1174  }
1175  if (argc == 3) {
1176  config->StringToSettings(argv[2]);
1177  }
1178  }
1179 
1180  /* Start a new AI company */
1181  DoCommandP(0, 1 | INVALID_COMPANY << 16, 0, CMD_COMPANY_CTRL);
1182 
1183  return true;
1184 }
1185 
1186 DEF_CONSOLE_CMD(ConReloadAI)
1187 {
1188  if (argc != 2) {
1189  IConsoleHelp("Reload an AI. Usage: 'reload_ai <company-id>'");
1190  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.");
1191  return true;
1192  }
1193 
1194  if (_game_mode != GM_NORMAL) {
1195  IConsoleWarning("AIs can only be managed in a game.");
1196  return true;
1197  }
1198 
1199  if (_networking && !_network_server) {
1200  IConsoleWarning("Only the server can reload an AI.");
1201  return true;
1202  }
1203 
1204  CompanyID company_id = (CompanyID)(atoi(argv[1]) - 1);
1205  if (!Company::IsValidID(company_id)) {
1206  IConsolePrintF(CC_DEFAULT, "Unknown company. Company range is between 1 and %d.", MAX_COMPANIES);
1207  return true;
1208  }
1209 
1210  if (Company::IsHumanID(company_id)) {
1211  IConsoleWarning("Company is not controlled by an AI.");
1212  return true;
1213  }
1214 
1215  /* First kill the company of the AI, then start a new one. This should start the current AI again */
1216  DoCommandP(0, 2 | company_id << 16, CRR_MANUAL, CMD_COMPANY_CTRL);
1217  DoCommandP(0, 1 | company_id << 16, 0, CMD_COMPANY_CTRL);
1218  IConsolePrint(CC_DEFAULT, "AI reloaded.");
1219 
1220  return true;
1221 }
1222 
1223 DEF_CONSOLE_CMD(ConStopAI)
1224 {
1225  if (argc != 2) {
1226  IConsoleHelp("Stop an AI. Usage: 'stop_ai <company-id>'");
1227  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.");
1228  return true;
1229  }
1230 
1231  if (_game_mode != GM_NORMAL) {
1232  IConsoleWarning("AIs can only be managed in a game.");
1233  return true;
1234  }
1235 
1236  if (_networking && !_network_server) {
1237  IConsoleWarning("Only the server can stop an AI.");
1238  return true;
1239  }
1240 
1241  CompanyID company_id = (CompanyID)(atoi(argv[1]) - 1);
1242  if (!Company::IsValidID(company_id)) {
1243  IConsolePrintF(CC_DEFAULT, "Unknown company. Company range is between 1 and %d.", MAX_COMPANIES);
1244  return true;
1245  }
1246 
1247  if (Company::IsHumanID(company_id) || company_id == _local_company) {
1248  IConsoleWarning("Company is not controlled by an AI.");
1249  return true;
1250  }
1251 
1252  /* Now kill the company of the AI. */
1253  DoCommandP(0, 2 | company_id << 16, CRR_MANUAL, CMD_COMPANY_CTRL);
1254  IConsolePrint(CC_DEFAULT, "AI stopped, company deleted.");
1255 
1256  return true;
1257 }
1258 
1259 DEF_CONSOLE_CMD(ConRescanAI)
1260 {
1261  if (argc == 0) {
1262  IConsoleHelp("Rescan the AI dir for scripts. Usage: 'rescan_ai'");
1263  return true;
1264  }
1265 
1266  if (_networking && !_network_server) {
1267  IConsoleWarning("Only the server can rescan the AI dir for scripts.");
1268  return true;
1269  }
1270 
1271  AI::Rescan();
1272 
1273  return true;
1274 }
1275 
1276 DEF_CONSOLE_CMD(ConRescanGame)
1277 {
1278  if (argc == 0) {
1279  IConsoleHelp("Rescan the Game Script dir for scripts. Usage: 'rescan_game'");
1280  return true;
1281  }
1282 
1283  if (_networking && !_network_server) {
1284  IConsoleWarning("Only the server can rescan the Game Script dir for scripts.");
1285  return true;
1286  }
1287 
1288  Game::Rescan();
1289 
1290  return true;
1291 }
1292 
1293 DEF_CONSOLE_CMD(ConRescanNewGRF)
1294 {
1295  if (argc == 0) {
1296  IConsoleHelp("Rescan the data dir for NewGRFs. Usage: 'rescan_newgrf'");
1297  return true;
1298  }
1299 
1300  ScanNewGRFFiles(NULL);
1301 
1302  return true;
1303 }
1304 
1305 DEF_CONSOLE_CMD(ConGetSeed)
1306 {
1307  if (argc == 0) {
1308  IConsoleHelp("Returns the seed used to create this game. Usage: 'getseed'");
1309  IConsoleHelp("The seed can be used to reproduce the exact same map as the game started with.");
1310  return true;
1311  }
1312 
1314  return true;
1315 }
1316 
1317 DEF_CONSOLE_CMD(ConGetDate)
1318 {
1319  if (argc == 0) {
1320  IConsoleHelp("Returns the current date (day-month-year) of the game. Usage: 'getdate'");
1321  return true;
1322  }
1323 
1324  YearMonthDay ymd;
1325  ConvertDateToYMD(_date, &ymd);
1326  IConsolePrintF(CC_DEFAULT, "Date: %d-%d-%d", ymd.day, ymd.month + 1, ymd.year);
1327  return true;
1328 }
1329 
1330 
1331 DEF_CONSOLE_CMD(ConAlias)
1332 {
1333  IConsoleAlias *alias;
1334 
1335  if (argc == 0) {
1336  IConsoleHelp("Add a new alias, or redefine the behaviour of an existing alias . Usage: 'alias <name> <command>'");
1337  return true;
1338  }
1339 
1340  if (argc < 3) return false;
1341 
1342  alias = IConsoleAliasGet(argv[1]);
1343  if (alias == NULL) {
1344  IConsoleAliasRegister(argv[1], argv[2]);
1345  } else {
1346  free(alias->cmdline);
1347  alias->cmdline = stredup(argv[2]);
1348  }
1349  return true;
1350 }
1351 
1352 DEF_CONSOLE_CMD(ConScreenShot)
1353 {
1354  if (argc == 0) {
1355  IConsoleHelp("Create a screenshot of the game. Usage: 'screenshot [big | giant | no_con] [file name]'");
1356  IConsoleHelp("'big' makes a zoomed-in screenshot of the visible area, 'giant' makes a screenshot of the "
1357  "whole map, 'no_con' hides the console to create the screenshot. 'big' or 'giant' "
1358  "screenshots are always drawn without console");
1359  return true;
1360  }
1361 
1362  if (argc > 3) return false;
1363 
1364  ScreenshotType type = SC_VIEWPORT;
1365  const char *name = NULL;
1366 
1367  if (argc > 1) {
1368  if (strcmp(argv[1], "big") == 0) {
1369  /* screenshot big [filename] */
1370  type = SC_ZOOMEDIN;
1371  if (argc > 2) name = argv[2];
1372  } else if (strcmp(argv[1], "giant") == 0) {
1373  /* screenshot giant [filename] */
1374  type = SC_WORLD;
1375  if (argc > 2) name = argv[2];
1376  } else if (strcmp(argv[1], "no_con") == 0) {
1377  /* screenshot no_con [filename] */
1378  IConsoleClose();
1379  if (argc > 2) name = argv[2];
1380  } else if (argc == 2) {
1381  /* screenshot filename */
1382  name = argv[1];
1383  } else {
1384  /* screenshot argv[1] argv[2] - invalid */
1385  return false;
1386  }
1387  }
1388 
1389  MakeScreenshot(type, name);
1390  return true;
1391 }
1392 
1393 DEF_CONSOLE_CMD(ConInfoCmd)
1394 {
1395  if (argc == 0) {
1396  IConsoleHelp("Print out debugging information about a command. Usage: 'info_cmd <cmd>'");
1397  return true;
1398  }
1399 
1400  if (argc < 2) return false;
1401 
1402  const IConsoleCmd *cmd = IConsoleCmdGet(argv[1]);
1403  if (cmd == NULL) {
1404  IConsoleError("the given command was not found");
1405  return true;
1406  }
1407 
1408  IConsolePrintF(CC_DEFAULT, "command name: %s", cmd->name);
1409  IConsolePrintF(CC_DEFAULT, "command proc: %p", cmd->proc);
1410 
1411  if (cmd->hook != NULL) IConsoleWarning("command is hooked");
1412 
1413  return true;
1414 }
1415 
1416 DEF_CONSOLE_CMD(ConDebugLevel)
1417 {
1418  if (argc == 0) {
1419  IConsoleHelp("Get/set the default debugging level for the game. Usage: 'debug_level [<level>]'");
1420  IConsoleHelp("Level can be any combination of names, levels. Eg 'net=5 ms=4'. Remember to enclose it in \"'s");
1421  return true;
1422  }
1423 
1424  if (argc > 2) return false;
1425 
1426  if (argc == 1) {
1427  IConsolePrintF(CC_DEFAULT, "Current debug-level: '%s'", GetDebugString());
1428  } else {
1429  SetDebugString(argv[1]);
1430  }
1431 
1432  return true;
1433 }
1434 
1435 DEF_CONSOLE_CMD(ConExit)
1436 {
1437  if (argc == 0) {
1438  IConsoleHelp("Exit the game. Usage: 'exit'");
1439  return true;
1440  }
1441 
1442  if (_game_mode == GM_NORMAL && _settings_client.gui.autosave_on_exit) DoExitSave();
1443 
1444  _exit_game = true;
1445  return true;
1446 }
1447 
1448 DEF_CONSOLE_CMD(ConPart)
1449 {
1450  if (argc == 0) {
1451  IConsoleHelp("Leave the currently joined/running game (only ingame). Usage: 'part'");
1452  return true;
1453  }
1454 
1455  if (_game_mode != GM_NORMAL) return false;
1456 
1458  return true;
1459 }
1460 
1461 DEF_CONSOLE_CMD(ConHelp)
1462 {
1463  if (argc == 2) {
1464  const IConsoleCmd *cmd;
1465  const IConsoleAlias *alias;
1466 
1467  RemoveUnderscores(argv[1]);
1468  cmd = IConsoleCmdGet(argv[1]);
1469  if (cmd != NULL) {
1470  cmd->proc(0, NULL);
1471  return true;
1472  }
1473 
1474  alias = IConsoleAliasGet(argv[1]);
1475  if (alias != NULL) {
1476  cmd = IConsoleCmdGet(alias->cmdline);
1477  if (cmd != NULL) {
1478  cmd->proc(0, NULL);
1479  return true;
1480  }
1481  IConsolePrintF(CC_ERROR, "ERROR: alias is of special type, please see its execution-line: '%s'", alias->cmdline);
1482  return true;
1483  }
1484 
1485  IConsoleError("command not found");
1486  return true;
1487  }
1488 
1489  IConsolePrint(CC_WARNING, " ---- OpenTTD Console Help ---- ");
1490  IConsolePrint(CC_DEFAULT, " - commands: [command to list all commands: list_cmds]");
1491  IConsolePrint(CC_DEFAULT, " call commands with '<command> <arg2> <arg3>...'");
1492  IConsolePrint(CC_DEFAULT, " - to assign strings, or use them as arguments, enclose it within quotes");
1493  IConsolePrint(CC_DEFAULT, " like this: '<command> \"string argument with spaces\"'");
1494  IConsolePrint(CC_DEFAULT, " - use 'help <command>' to get specific information");
1495  IConsolePrint(CC_DEFAULT, " - scroll console output with shift + (up | down | pageup | pagedown)");
1496  IConsolePrint(CC_DEFAULT, " - scroll console input history with the up or down arrows");
1498  return true;
1499 }
1500 
1501 DEF_CONSOLE_CMD(ConListCommands)
1502 {
1503  if (argc == 0) {
1504  IConsoleHelp("List all registered commands. Usage: 'list_cmds [<pre-filter>]'");
1505  return true;
1506  }
1507 
1508  for (const IConsoleCmd *cmd = _iconsole_cmds; cmd != NULL; cmd = cmd->next) {
1509  if (argv[1] == NULL || strstr(cmd->name, argv[1]) != NULL) {
1510  if (cmd->hook == NULL || cmd->hook(false) != CHR_HIDE) IConsolePrintF(CC_DEFAULT, "%s", cmd->name);
1511  }
1512  }
1513 
1514  return true;
1515 }
1516 
1517 DEF_CONSOLE_CMD(ConListAliases)
1518 {
1519  if (argc == 0) {
1520  IConsoleHelp("List all registered aliases. Usage: 'list_aliases [<pre-filter>]'");
1521  return true;
1522  }
1523 
1524  for (const IConsoleAlias *alias = _iconsole_aliases; alias != NULL; alias = alias->next) {
1525  if (argv[1] == NULL || strstr(alias->name, argv[1]) != NULL) {
1526  IConsolePrintF(CC_DEFAULT, "%s => %s", alias->name, alias->cmdline);
1527  }
1528  }
1529 
1530  return true;
1531 }
1532 
1533 DEF_CONSOLE_CMD(ConCompanies)
1534 {
1535  if (argc == 0) {
1536  IConsoleHelp("List the details of all companies in the game. Usage 'companies'");
1537  return true;
1538  }
1539 
1540  Company *c;
1541  FOR_ALL_COMPANIES(c) {
1542  /* Grab the company name */
1543  char company_name[512];
1544  SetDParam(0, c->index);
1545  GetString(company_name, STR_COMPANY_NAME, lastof(company_name));
1546 
1547  const char *password_state = "";
1548  if (c->is_ai) {
1549  password_state = "AI";
1550  }
1551 #ifdef ENABLE_NETWORK
1552  else if (_network_server) {
1553  password_state = StrEmpty(_network_company_states[c->index].password) ? "unprotected" : "protected";
1554  }
1555 #endif
1556 
1557  char colour[512];
1558  GetString(colour, STR_COLOUR_DARK_BLUE + _company_colours[c->index], lastof(colour));
1559  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) %s",
1560  c->index + 1, colour, company_name,
1561  c->inaugurated_year, (int64)c->money, (int64)c->current_loan, (int64)CalculateCompanyValue(c),
1566  password_state);
1567  }
1568 
1569  return true;
1570 }
1571 
1572 #ifdef ENABLE_NETWORK
1573 
1574 DEF_CONSOLE_CMD(ConSay)
1575 {
1576  if (argc == 0) {
1577  IConsoleHelp("Chat to your fellow players in a multiplayer game. Usage: 'say \"<msg>\"'");
1578  return true;
1579  }
1580 
1581  if (argc != 2) return false;
1582 
1583  if (!_network_server) {
1584  NetworkClientSendChat(NETWORK_ACTION_CHAT, DESTTYPE_BROADCAST, 0 /* param does not matter */, argv[1]);
1585  } else {
1586  bool from_admin = (_redirect_console_to_admin < INVALID_ADMIN_ID);
1587  NetworkServerSendChat(NETWORK_ACTION_CHAT, DESTTYPE_BROADCAST, 0, argv[1], CLIENT_ID_SERVER, from_admin);
1588  }
1589 
1590  return true;
1591 }
1592 
1593 DEF_CONSOLE_CMD(ConSayCompany)
1594 {
1595  if (argc == 0) {
1596  IConsoleHelp("Chat to a certain company in a multiplayer game. Usage: 'say_company <company-no> \"<msg>\"'");
1597  IConsoleHelp("CompanyNo is the company that plays as company <companyno>, 1 through max_companies");
1598  return true;
1599  }
1600 
1601  if (argc != 3) return false;
1602 
1603  CompanyID company_id = (CompanyID)(atoi(argv[1]) - 1);
1604  if (!Company::IsValidID(company_id)) {
1605  IConsolePrintF(CC_DEFAULT, "Unknown company. Company range is between 1 and %d.", MAX_COMPANIES);
1606  return true;
1607  }
1608 
1609  if (!_network_server) {
1610  NetworkClientSendChat(NETWORK_ACTION_CHAT_COMPANY, DESTTYPE_TEAM, company_id, argv[2]);
1611  } else {
1612  bool from_admin = (_redirect_console_to_admin < INVALID_ADMIN_ID);
1613  NetworkServerSendChat(NETWORK_ACTION_CHAT_COMPANY, DESTTYPE_TEAM, company_id, argv[2], CLIENT_ID_SERVER, from_admin);
1614  }
1615 
1616  return true;
1617 }
1618 
1619 DEF_CONSOLE_CMD(ConSayClient)
1620 {
1621  if (argc == 0) {
1622  IConsoleHelp("Chat to a certain client in a multiplayer game. Usage: 'say_client <client-no> \"<msg>\"'");
1623  IConsoleHelp("For client-id's, see the command 'clients'");
1624  return true;
1625  }
1626 
1627  if (argc != 3) return false;
1628 
1629  if (!_network_server) {
1630  NetworkClientSendChat(NETWORK_ACTION_CHAT_CLIENT, DESTTYPE_CLIENT, atoi(argv[1]), argv[2]);
1631  } else {
1632  bool from_admin = (_redirect_console_to_admin < INVALID_ADMIN_ID);
1633  NetworkServerSendChat(NETWORK_ACTION_CHAT_CLIENT, DESTTYPE_CLIENT, atoi(argv[1]), argv[2], CLIENT_ID_SERVER, from_admin);
1634  }
1635 
1636  return true;
1637 }
1638 
1639 DEF_CONSOLE_CMD(ConCompanyPassword)
1640 {
1641  if (argc == 0) {
1642  const char *helpmsg;
1643 
1644  if (_network_dedicated) {
1645  helpmsg = "Change the password of a company. Usage: 'company_pw <company-no> \"<password>\"";
1646  } else if (_network_server) {
1647  helpmsg = "Change the password of your or any other company. Usage: 'company_pw [<company-no>] \"<password>\"'";
1648  } else {
1649  helpmsg = "Change the password of your company. Usage: 'company_pw \"<password>\"'";
1650  }
1651 
1652  IConsoleHelp(helpmsg);
1653  IConsoleHelp("Use \"*\" to disable the password.");
1654  return true;
1655  }
1656 
1657  CompanyID company_id;
1658  const char *password;
1659  const char *errormsg;
1660 
1661  if (argc == 2) {
1662  company_id = _local_company;
1663  password = argv[1];
1664  errormsg = "You have to own a company to make use of this command.";
1665  } else if (argc == 3 && _network_server) {
1666  company_id = (CompanyID)(atoi(argv[1]) - 1);
1667  password = argv[2];
1668  errormsg = "You have to specify the ID of a valid human controlled company.";
1669  } else {
1670  return false;
1671  }
1672 
1673  if (!Company::IsValidHumanID(company_id)) {
1674  IConsoleError(errormsg);
1675  return false;
1676  }
1677 
1678  password = NetworkChangeCompanyPassword(company_id, password);
1679 
1680  if (StrEmpty(password)) {
1681  IConsolePrintF(CC_WARNING, "Company password cleared");
1682  } else {
1683  IConsolePrintF(CC_WARNING, "Company password changed to: %s", password);
1684  }
1685 
1686  return true;
1687 }
1688 
1689 /* Content downloading only is available with ZLIB */
1690 #if defined(WITH_ZLIB)
1691 #include "network/network_content.h"
1692 
1694 static ContentType StringToContentType(const char *str)
1695 {
1696  static const char * const inv_lookup[] = { "", "base", "newgrf", "ai", "ailib", "scenario", "heightmap" };
1697  for (uint i = 1 /* there is no type 0 */; i < lengthof(inv_lookup); i++) {
1698  if (strcasecmp(str, inv_lookup[i]) == 0) return (ContentType)i;
1699  }
1700  return CONTENT_TYPE_END;
1701 }
1702 
1705  void OnConnect(bool success)
1706  {
1707  IConsolePrintF(CC_DEFAULT, "Content server connection %s", success ? "established" : "failed");
1708  }
1709 
1711  {
1712  IConsolePrintF(CC_DEFAULT, "Content server connection closed");
1713  }
1714 
1716  {
1717  IConsolePrintF(CC_DEFAULT, "Completed download of %d", cid);
1718  }
1719 };
1720 
1725 static void OutputContentState(const ContentInfo *const ci)
1726 {
1727  static const char * const types[] = { "Base graphics", "NewGRF", "AI", "AI library", "Scenario", "Heightmap", "Base sound", "Base music", "Game script", "GS library" };
1728  assert_compile(lengthof(types) == CONTENT_TYPE_END - CONTENT_TYPE_BEGIN);
1729  static const char * const states[] = { "Not selected", "Selected", "Dep Selected", "Installed", "Unknown" };
1730  static const TextColour state_to_colour[] = { CC_COMMAND, CC_INFO, CC_INFO, CC_WHITE, CC_ERROR };
1731 
1732  char buf[sizeof(ci->md5sum) * 2 + 1];
1733  md5sumToString(buf, lastof(buf), ci->md5sum);
1734  IConsolePrintF(state_to_colour[ci->state], "%d, %s, %s, %s, %08X, %s", ci->id, types[ci->type - 1], states[ci->state], ci->name, ci->unique_id, buf);
1735 }
1736 
1737 DEF_CONSOLE_CMD(ConContent)
1738 {
1739  static ContentCallback *cb = NULL;
1740  if (cb == NULL) {
1741  cb = new ConsoleContentCallback();
1743  }
1744 
1745  if (argc <= 1) {
1746  IConsoleHelp("Query, select and download content. Usage: 'content update|upgrade|select [all|id]|unselect [all|id]|state [filter]|download'");
1747  IConsoleHelp(" update: get a new list of downloadable content; must be run first");
1748  IConsoleHelp(" upgrade: select all items that are upgrades");
1749  IConsoleHelp(" select: select a specific item given by its id or 'all' to select all. If no parameter is given, all selected content will be listed");
1750  IConsoleHelp(" unselect: unselect a specific item given by its id or 'all' to unselect all");
1751  IConsoleHelp(" state: show the download/select state of all downloadable content. Optionally give a filter string");
1752  IConsoleHelp(" download: download all content you've selected");
1753  return true;
1754  }
1755 
1756  if (strcasecmp(argv[1], "update") == 0) {
1758  return true;
1759  }
1760 
1761  if (strcasecmp(argv[1], "upgrade") == 0) {
1763  return true;
1764  }
1765 
1766  if (strcasecmp(argv[1], "select") == 0) {
1767  if (argc <= 2) {
1768  /* List selected content */
1769  IConsolePrintF(CC_WHITE, "id, type, state, name");
1771  if ((*iter)->state != ContentInfo::SELECTED && (*iter)->state != ContentInfo::AUTOSELECTED) continue;
1772  OutputContentState(*iter);
1773  }
1774  } else if (strcasecmp(argv[2], "all") == 0) {
1776  } else {
1777  _network_content_client.Select((ContentID)atoi(argv[2]));
1778  }
1779  return true;
1780  }
1781 
1782  if (strcasecmp(argv[1], "unselect") == 0) {
1783  if (argc <= 2) {
1784  IConsoleError("You must enter the id.");
1785  return false;
1786  }
1787  if (strcasecmp(argv[2], "all") == 0) {
1789  } else {
1790  _network_content_client.Unselect((ContentID)atoi(argv[2]));
1791  }
1792  return true;
1793  }
1794 
1795  if (strcasecmp(argv[1], "state") == 0) {
1796  IConsolePrintF(CC_WHITE, "id, type, state, name");
1798  if (argc > 2 && strcasestr((*iter)->name, argv[2]) == NULL) continue;
1799  OutputContentState(*iter);
1800  }
1801  return true;
1802  }
1803 
1804  if (strcasecmp(argv[1], "download") == 0) {
1805  uint files;
1806  uint bytes;
1808  IConsolePrintF(CC_DEFAULT, "Downloading %d file(s) (%d bytes)", files, bytes);
1809  return true;
1810  }
1811 
1812  return false;
1813 }
1814 #endif /* defined(WITH_ZLIB) */
1815 #endif /* ENABLE_NETWORK */
1816 
1817 DEF_CONSOLE_CMD(ConSetting)
1818 {
1819  if (argc == 0) {
1820  IConsoleHelp("Change setting for all clients. Usage: 'setting <name> [<value>]'");
1821  IConsoleHelp("Omitting <value> will print out the current value of the setting.");
1822  return true;
1823  }
1824 
1825  if (argc == 1 || argc > 3) return false;
1826 
1827  if (argc == 2) {
1828  IConsoleGetSetting(argv[1]);
1829  } else {
1830  IConsoleSetSetting(argv[1], argv[2]);
1831  }
1832 
1833  return true;
1834 }
1835 
1836 DEF_CONSOLE_CMD(ConSettingNewgame)
1837 {
1838  if (argc == 0) {
1839  IConsoleHelp("Change setting for the next game. Usage: 'setting_newgame <name> [<value>]'");
1840  IConsoleHelp("Omitting <value> will print out the current value of the setting.");
1841  return true;
1842  }
1843 
1844  if (argc == 1 || argc > 3) return false;
1845 
1846  if (argc == 2) {
1847  IConsoleGetSetting(argv[1], true);
1848  } else {
1849  IConsoleSetSetting(argv[1], argv[2], true);
1850  }
1851 
1852  return true;
1853 }
1854 
1855 DEF_CONSOLE_CMD(ConListSettings)
1856 {
1857  if (argc == 0) {
1858  IConsoleHelp("List settings. Usage: 'list_settings [<pre-filter>]'");
1859  return true;
1860  }
1861 
1862  if (argc > 2) return false;
1863 
1864  IConsoleListSettings((argc == 2) ? argv[1] : NULL);
1865  return true;
1866 }
1867 
1868 DEF_CONSOLE_CMD(ConGamelogPrint)
1869 {
1871  return true;
1872 }
1873 
1874 DEF_CONSOLE_CMD(ConNewGRFReload)
1875 {
1876  if (argc == 0) {
1877  IConsoleHelp("Reloads all active NewGRFs from disk. Equivalent to reapplying NewGRFs via the settings, but without asking for confirmation. This might crash OpenTTD!");
1878  return true;
1879  }
1880 
1881  ReloadNewGRFData();
1882  return true;
1883 }
1884 
1885 #ifdef _DEBUG
1886 /******************
1887  * debug commands
1888  ******************/
1889 
1890 static void IConsoleDebugLibRegister()
1891 {
1892  IConsoleCmdRegister("resettile", ConResetTile);
1893  IConsoleAliasRegister("dbg_echo", "echo %A; echo %B");
1894  IConsoleAliasRegister("dbg_echo2", "echo %!");
1895 }
1896 #endif
1897 
1898 /*******************************
1899  * console command registration
1900  *******************************/
1901 
1902 void IConsoleStdLibRegister()
1903 {
1904  IConsoleCmdRegister("debug_level", ConDebugLevel);
1905  IConsoleCmdRegister("echo", ConEcho);
1906  IConsoleCmdRegister("echoc", ConEchoC);
1907  IConsoleCmdRegister("exec", ConExec);
1908  IConsoleCmdRegister("exit", ConExit);
1909  IConsoleCmdRegister("part", ConPart);
1910  IConsoleCmdRegister("help", ConHelp);
1911  IConsoleCmdRegister("info_cmd", ConInfoCmd);
1912  IConsoleCmdRegister("list_cmds", ConListCommands);
1913  IConsoleCmdRegister("list_aliases", ConListAliases);
1914  IConsoleCmdRegister("newgame", ConNewGame);
1915  IConsoleCmdRegister("restart", ConRestart);
1916  IConsoleCmdRegister("getseed", ConGetSeed);
1917  IConsoleCmdRegister("getdate", ConGetDate);
1918  IConsoleCmdRegister("quit", ConExit);
1919  IConsoleCmdRegister("resetengines", ConResetEngines, ConHookNoNetwork);
1920  IConsoleCmdRegister("reset_enginepool", ConResetEnginePool, ConHookNoNetwork);
1921  IConsoleCmdRegister("return", ConReturn);
1922  IConsoleCmdRegister("screenshot", ConScreenShot);
1923  IConsoleCmdRegister("script", ConScript);
1924  IConsoleCmdRegister("scrollto", ConScrollToTile);
1925  IConsoleCmdRegister("alias", ConAlias);
1926  IConsoleCmdRegister("load", ConLoad);
1927  IConsoleCmdRegister("rm", ConRemove);
1928  IConsoleCmdRegister("save", ConSave);
1929  IConsoleCmdRegister("saveconfig", ConSaveConfig);
1930  IConsoleCmdRegister("ls", ConListFiles);
1931  IConsoleCmdRegister("cd", ConChangeDirectory);
1932  IConsoleCmdRegister("pwd", ConPrintWorkingDirectory);
1933  IConsoleCmdRegister("clear", ConClearBuffer);
1934  IConsoleCmdRegister("setting", ConSetting);
1935  IConsoleCmdRegister("setting_newgame", ConSettingNewgame);
1936  IConsoleCmdRegister("list_settings",ConListSettings);
1937  IConsoleCmdRegister("gamelog", ConGamelogPrint);
1938  IConsoleCmdRegister("rescan_newgrf", ConRescanNewGRF);
1939 
1940  IConsoleAliasRegister("dir", "ls");
1941  IConsoleAliasRegister("del", "rm %+");
1942  IConsoleAliasRegister("newmap", "newgame");
1943  IConsoleAliasRegister("patch", "setting %+");
1944  IConsoleAliasRegister("set", "setting %+");
1945  IConsoleAliasRegister("set_newgame", "setting_newgame %+");
1946  IConsoleAliasRegister("list_patches", "list_settings %+");
1947  IConsoleAliasRegister("developer", "setting developer %+");
1948 
1949  IConsoleCmdRegister("list_ai_libs", ConListAILibs);
1950  IConsoleCmdRegister("list_ai", ConListAI);
1951  IConsoleCmdRegister("reload_ai", ConReloadAI);
1952  IConsoleCmdRegister("rescan_ai", ConRescanAI);
1953  IConsoleCmdRegister("start_ai", ConStartAI);
1954  IConsoleCmdRegister("stop_ai", ConStopAI);
1955 
1956  IConsoleCmdRegister("list_game", ConListGame);
1957  IConsoleCmdRegister("list_game_libs", ConListGameLibs);
1958  IConsoleCmdRegister("rescan_game", ConRescanGame);
1959 
1960  IConsoleCmdRegister("companies", ConCompanies);
1961  IConsoleAliasRegister("players", "companies");
1962 
1963  /* networking functions */
1964 #ifdef ENABLE_NETWORK
1965 /* Content downloading is only available with ZLIB */
1966 #if defined(WITH_ZLIB)
1967  IConsoleCmdRegister("content", ConContent);
1968 #endif /* defined(WITH_ZLIB) */
1969 
1970  /*** Networking commands ***/
1971  IConsoleCmdRegister("say", ConSay, ConHookNeedNetwork);
1972  IConsoleCmdRegister("say_company", ConSayCompany, ConHookNeedNetwork);
1973  IConsoleAliasRegister("say_player", "say_company %+");
1974  IConsoleCmdRegister("say_client", ConSayClient, ConHookNeedNetwork);
1975 
1976  IConsoleCmdRegister("connect", ConNetworkConnect, ConHookClientOnly);
1977  IConsoleCmdRegister("clients", ConNetworkClients, ConHookNeedNetwork);
1978  IConsoleCmdRegister("status", ConStatus, ConHookServerOnly);
1979  IConsoleCmdRegister("server_info", ConServerInfo, ConHookServerOnly);
1980  IConsoleAliasRegister("info", "server_info");
1981  IConsoleCmdRegister("reconnect", ConNetworkReconnect, ConHookClientOnly);
1982  IConsoleCmdRegister("rcon", ConRcon, ConHookNeedNetwork);
1983 
1984  IConsoleCmdRegister("join", ConJoinCompany, ConHookNeedNetwork);
1985  IConsoleAliasRegister("spectate", "join 255");
1986  IConsoleCmdRegister("move", ConMoveClient, ConHookServerOnly);
1987  IConsoleCmdRegister("reset_company", ConResetCompany, ConHookServerOnly);
1988  IConsoleAliasRegister("clean_company", "reset_company %A");
1989  IConsoleCmdRegister("client_name", ConClientNickChange, ConHookServerOnly);
1990  IConsoleCmdRegister("kick", ConKick, ConHookServerOnly);
1991  IConsoleCmdRegister("ban", ConBan, ConHookServerOnly);
1992  IConsoleCmdRegister("unban", ConUnBan, ConHookServerOnly);
1993  IConsoleCmdRegister("banlist", ConBanList, ConHookServerOnly);
1994 
1995  IConsoleCmdRegister("pause", ConPauseGame, ConHookServerOnly);
1996  IConsoleCmdRegister("unpause", ConUnpauseGame, ConHookServerOnly);
1997 
1998  IConsoleCmdRegister("company_pw", ConCompanyPassword, ConHookNeedNetwork);
1999  IConsoleAliasRegister("company_password", "company_pw %+");
2000 
2001  IConsoleAliasRegister("net_frame_freq", "setting frame_freq %+");
2002  IConsoleAliasRegister("net_sync_freq", "setting sync_freq %+");
2003  IConsoleAliasRegister("server_pw", "setting server_password %+");
2004  IConsoleAliasRegister("server_password", "setting server_password %+");
2005  IConsoleAliasRegister("rcon_pw", "setting rcon_password %+");
2006  IConsoleAliasRegister("rcon_password", "setting rcon_password %+");
2007  IConsoleAliasRegister("name", "setting client_name %+");
2008  IConsoleAliasRegister("server_name", "setting server_name %+");
2009  IConsoleAliasRegister("server_port", "setting server_port %+");
2010  IConsoleAliasRegister("server_advertise", "setting server_advertise %+");
2011  IConsoleAliasRegister("max_clients", "setting max_clients %+");
2012  IConsoleAliasRegister("max_companies", "setting max_companies %+");
2013  IConsoleAliasRegister("max_spectators", "setting max_spectators %+");
2014  IConsoleAliasRegister("max_join_time", "setting max_join_time %+");
2015  IConsoleAliasRegister("pause_on_join", "setting pause_on_join %+");
2016  IConsoleAliasRegister("autoclean_companies", "setting autoclean_companies %+");
2017  IConsoleAliasRegister("autoclean_protected", "setting autoclean_protected %+");
2018  IConsoleAliasRegister("autoclean_unprotected", "setting autoclean_unprotected %+");
2019  IConsoleAliasRegister("restart_game_year", "setting restart_game_year %+");
2020  IConsoleAliasRegister("min_players", "setting min_active_clients %+");
2021  IConsoleAliasRegister("reload_cfg", "setting reload_cfg %+");
2022 #endif /* ENABLE_NETWORK */
2023 
2024  /* debugging stuff */
2025 #ifdef _DEBUG
2026  IConsoleDebugLibRegister();
2027 #endif
2028 
2029  /* NewGRF development stuff */
2030  IConsoleCmdRegister("reload_newgrfs", ConNewGRFReload, ConHookNewGRFDeveloperTool);
2031 }
AISettings ai
what may the AI do?
World screenshot.
Definition: screenshot.h:25
Functions related to OTTD&#39;s strings.
Helper to mark the end of the types.
Definition: tcp_content.h:37
static void OutputContentState(const ContentInfo *const ci)
Outputs content state information to console.
Send message/notice to all clients (All)
Definition: network_type.h:83
uint NetworkServerKickOrBanIP(ClientID client_id, bool ban)
Ban, or kick, everyone joined from the given client&#39;s IP.
const ContentInfo *const * ConstContentIterator
Iterator for the constant content vector.
static uint MapSizeX()
Get the size of the map along the X.
Definition: map_func.h:74
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:77
used in multiplayer to create a new companies etc.
Definition: command_type.h:279
void IConsoleGetSetting(const char *name, bool force_newgame)
Output value of a specific setting to the console.
Definition: settings.cpp:2127
IConsoleCmd * next
next command in list
void IConsoleWarning(const char *string)
It is possible to print warnings to the console.
Definition: console.cpp:165
void ParseConnectionString(const char **company, const char **port, char *connection_string)
Converts a string to ip/port/company Format: IP:port::company.
Definition: network.cpp:474
bool _networking
are we in networking mode?
Definition: network.cpp:56
void DownloadSelectedContent(uint &files, uint &bytes, bool fallback=false)
Actually begin downloading the content we selected.
char *CDECL str_fmt(const char *str,...)
Format, "printf", into a newly allocated string.
Definition: string.cpp:139
ConstContentIterator Begin() const
Get the begin of the content inf iterator.
static void PrintLineByLine(char *buf)
Print a text buffer line by line to the console.
static uint MapSizeY()
Get the size of the map along the Y.
Definition: map_func.h:84
Container for all information known about a client.
Definition: network_base.h:27
IConsoleCmd * _iconsole_cmds
list of registered commands
Definition: console.cpp:28
uint32 unique_id
Unique ID; either GRF ID or shortname.
Definition: tcp_content.h:77
A game normally paused.
Definition: openttd.h:59
A normal unpaused game.
Definition: openttd.h:58
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3120
uint16 last_port
port of the last joined server
void FioFCloseFile(FILE *f)
Close a file in a safe way.
Definition: fileio.cpp:342
void NetworkServerShowStatusToConsole()
Show the status message of all clients on the console.
void Clear()
Remove all items from the list.
Definition: fios.h:179
Switch to game intro menu.
Definition: openttd.h:32
void SetName(const char *name)
Set the name of the file.
Definition: saveload.cpp:2948
Functions related to dates.
void StartNewGameWithoutGUI(uint seed)
Start a normal game without the GUI.
Day day
Day (1..31)
Definition: date_type.h:106
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:398
Load game, Play Scenario.
Definition: openttd.h:31
static uint MapLogX()
Logarithm of the map size along the X side.
Definition: map_func.h:53
Year inaugurated_year
Year of starting the company.
Definition: company_base.h:79
Functions related to debugging.
bool NetworkCompanyHasClients(CompanyID company)
Check whether a particular company has clients.
Callbacks for notifying others about incoming data.
static char * GetConsoleList(char *p, const char *last, bool newest_only=false)
Wrapper function for AIScanner::GetAIConsoleList.
Definition: ai_core.cpp:317
Functions to be called to log possibly unsafe game events.
ClientID client_id
Client identifier (same as ClientState->client_id)
Definition: network_base.h:28
FileToSaveLoad _file_to_saveload
File to save or load in the openttd loop.
Definition: saveload.cpp:272
The company is manually removed.
Definition: company_type.h:60
Screenshot of viewport.
Definition: screenshot.h:21
char * md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
Convert the md5sum to a hexadecimal string representation.
Definition: string.cpp:416
void IConsoleListSettings(const char *prefilter)
List all settings and their value to the console.
Definition: settings.cpp:2160
static bool IsHumanID(size_t index)
Is this company a company not controlled by a NoAI program?
Definition: company_base.h:157
Client part of the network protocol.
void Change(const char *name, int version=-1, bool force_exact_match=false, bool is_random=false)
Set another Script to be loaded in this slot.
Wrapper for (un)resolved network addresses; there&#39;s no reason to transform a numeric IP to a string a...
Definition: address.h:31
const T * Begin() const
Get the pointer to the first item (const)
ClientID _redirect_console_to_client
If not invalid, redirect the console output to a client.
Definition: network.cpp:64
static const uint GENERATE_NEW_SEED
Create a new random seed.
Definition: genworld.h:25
void NetworkClientRequestMove(CompanyID company_id, const char *pass)
Notify the server of this client wanting to be moved to another company.
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
void OnDownloadComplete(ContentID cid)
We have finished downloading a file.
void NetworkServerDoMove(ClientID client_id, CompanyID company_id)
Handle the tid-bits of moving a client from one company to another.
void OnConnect(bool success)
Callback for when the connection has finished.
static const AdminIndex INVALID_ADMIN_ID
An invalid admin marker.
Definition: network_type.h:58
Functions for Standard In/Out file operations.
Send message/notice to only a certain client (Private)
Definition: network_type.h:85
The content has been selected as dependency.
Definition: tcp_content.h:63
void DoExitSave()
Do a save when exiting the game (_settings_client.gui.autosave_on_exit)
Definition: saveload.cpp:2875
IConsoleCmdProc * proc
process executed when command is typed
void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data)
Send a chat message.
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
The client is spectating.
Definition: company_type.h:37
The client wants a new company.
Definition: company_type.h:36
char * name
name of command
static ContentType StringToContentType(const char *str)
Resolve a string to a content type.
static const TextColour CC_DEFAULT
Default colour of the console.
Definition: console_type.h:25
IConsoleAlias * _iconsole_aliases
list of registered aliases
Definition: console.cpp:29
bool NetworkCompanyIsPassworded(CompanyID company_id)
Check if the company we want to join requires a password.
Definition: network.cpp:225
A game paused because a (critical) error.
Definition: openttd.h:62
Functions related to world/map generation.
Functions to make screenshots.
Base core network types and some helper functions to access them.
bool NetworkMaxSpectatorsReached()
Check if max_spectatos has been reached on the server (local check only).
AdminIndex _redirect_console_to_admin
Redirection of the (remote) console to the admin.
static bool _script_running
Script is running (used to abort execution when #ConReturn is encountered).
void SaveToConfig()
Save the values to the configuration file.
Definition: settings.cpp:1730
uint8 map_x
X size of map.
ContentID
Unique identifier for the content.
Definition: tcp_content.h:53
Fully zoomed in screenshot of the visible area.
Definition: screenshot.h:23
const T * End() const
Get the pointer behind the last valid item (const)
Asynchronous callback.
File is being saved.
Definition: fileio_type.h:52
Deals with finding savegames.
Definition: fios.h:98
Servers always have this ID.
Definition: network_type.h:45
Hide the existence of the command.
StringID FiosGetDescText(const char **path, uint64 *total_free)
Get descriptive texts.
Definition: fios.cpp:141
void OnDisconnect()
Callback for when the connection got disconnected.
static bool IsValidHumanID(size_t index)
Is this company a valid company, not controlled by a NoAI program?
Definition: company_base.h:144
bool _network_available
is network mode available?
Definition: network.cpp:58
void Unselect(ContentID cid)
Unselect a specific content id.
bool _network_dedicated
are we a dedicated server?
Definition: network.cpp:59
Console; Window numbers:
Definition: window_type.h:633
CompanyByte _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
const FiosItem * FindItem(const char *file)
Find file information of a file by its name from the file list.
Definition: fios.cpp:108
static char * GetConsoleList(char *p, const char *last, bool newest_only=false)
Wrapper function for GameScanner::GetConsoleList.
Definition: game_core.cpp:221
Functions related to (drawing on) viewports.
uint8 map_y
Y size of map.
void UnselectAll()
Unselect everything that we&#39;ve not downloaded so far.
Save game or scenario file.
Definition: fileio_type.h:33
SmallVector< FiosItem, 32 > files
The list of files.
Definition: fios.h:193
static const size_t MAX_SIZE
Make template parameter accessible from outside.
Definition: pool_type.hpp:87
old or new savegame
Definition: fileio_type.h:20
static ConsoleFileList _console_file_list
File storage cache for the console.
char password[NETWORK_PASSWORD_LENGTH]
The password for the company.
Definition: network_type.h:69
Aircraft vehicle type.
Definition: vehicle_type.h:27
static bool IsConnected()
Check whether the client is actually connected (and in the game).
char name[32]
Name of the content.
Definition: tcp_content.h:73
uint Length() const
Get the number of files in the list.
Definition: fios.h:123
NetworkSettings network
settings related to the network
void AddCallback(ContentCallback *cb)
Add a callback to this class.
Internally used functions for the console.
void SetTitle(const char *title)
Set the title of the file.
Definition: saveload.cpp:2957
Functions/types related to saving and loading games.
CompanyID client_playas
As which company is this client playing (CompanyID)
Definition: network_base.h:31
ConstContentIterator End() const
Get the end of the content inf iterator.
uint Length() const
Get the number of items in the list.
void GamelogPrintConsole()
Print the gamelog data to the console.
Definition: gamelog.cpp:322
static char * GetConsoleLibraryList(char *p, const char *last)
Wrapper function for GameScanner::GetConsoleLibraryList.
Definition: game_core.cpp:226
IConsoleAlias * next
next alias in list
void NetworkServerKickClient(ClientID client_id)
Kick a single client.
void SelectUpgrade()
Select everything that&#39;s an update for something we&#39;ve got.
Functions related to engines.
uint8 max_spectators
maximum amount of spectators
bool HasScript() const
Is this config attached to an Script? In other words, is there a Script that is assigned to this slot...
void IConsolePrint(TextColour colour_code, const char *string)
Handle the printing of text entered into the console or redirected there by any other means...
Definition: console.cpp:88
IConsoleCmd * IConsoleCmdGet(const char *name)
Find the command pointed to by its string.
Definition: console.cpp:272
File list storage for the console, for caching the last &#39;ls&#39; command.
Money current_loan
Amount of money borrowed from the bank.
Definition: company_base.h:66
void IConsoleCmdExec(const char *cmdstr)
Execute a given command passed to us.
Definition: console.cpp:409
Allow command execution.
Functions related to setting/changing the settings.
ClientID _network_own_client_id
Our client identifier.
Definition: network.cpp:63
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:76
FILE * FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:474
void CDECL IConsolePrintF(TextColour colour_code, const char *format,...)
Handle the printing of text entered into the console or redirected there by any other means...
Definition: console.cpp:132
Definition of base types and functions in a cross-platform compatible way.
Data structure to convert between Date and triplet (year, month, and day).
Definition: date_type.h:103
A number of safeguards to prevent using unsafe methods.
ContentID id
Unique (server side) ID for the content.
Definition: tcp_content.h:70
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:247
NetworkCompanyState * _network_company_states
Statistics about some companies.
Definition: network.cpp:62
Base directory for all savegames.
Definition: fileio_type.h:112
Base directory for all subdirectories.
Definition: fileio_type.h:111
void BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop)
Construct a file list with the given kind of files, for the stated purpose.
Definition: fios.cpp:76
static AIConfig * GetConfig(CompanyID company, ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition: ai_config.cpp:38
void NetworkPrintClients()
Print all the clients to the console.
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:126
Console functions used outside of the console code.
void RequestContentList(ContentType type)
Request the content list for the given type.
State state
Whether the content info is selected (for download)
Definition: tcp_content.h:83
GroupStatistics group_all[VEH_COMPANY_END]
NOSAVE: Statistics for the ALL_GROUP group.
Definition: company_base.h:122
Money CalculateCompanyValue(const Company *c, bool including_loan=true)
Calculate the value of the company.
Definition: economy.cpp:113
static void IConsoleHelp(const char *str)
Show help for the console.
byte clients_on
Current count of clients on server.
Definition: game.h:30
bool is_ai
If true, the company is (also) controlled by the computer (a NoAI program).
Definition: company_base.h:94
bool autosave_on_exit
save an autosave when you quit the game, but do not ask "Do you really want to quit?"
Year year
Year (0...)
Definition: date_type.h:104
Money money
Money owned by the company.
Definition: company_base.h:64
void InvalidateFileList()
Declare the file storage cache as being invalid, also clears all stored files.
const char * GetDebugString()
Print out the current debug-level.
Definition: debug.cpp:233
AbstractFileType GetAbstractFileType(FiosType fios_type)
Extract the abstract file type from a FiosType.
Definition: fileio_type.h:92
Basic functions/variables used all over the place.
Part of the network protocol handling content distribution.
uint16 num_vehicle
Number of vehicles.
Definition: group.h:26
SaveOrLoadResult SaveOrLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
Main Save or Load function where the high-level saveload functions are handled.
Definition: saveload.cpp:2790
bool DoCommandP(const CommandContainer *container, bool my_cmd)
Shortcut for the long DoCommandP when having a container with the data.
Definition: command.cpp:527
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
NetworkServerGameInfo _network_game_info
Information about our game.
Definition: network.cpp:61
PauseModeByte _pause_mode
The current pause mode.
Definition: gfx.cpp:48
Road vehicle type.
Definition: vehicle_type.h:25
File is being loaded.
Definition: fileio_type.h:51
Month month
Month (0..11)
Definition: date_type.h:105
byte md5sum[16]
The MD5 checksum.
Definition: tcp_content.h:78
Maximum number of companies.
Definition: company_type.h:25
static const TextColour CC_COMMAND
Colour for the console&#39;s commands.
Definition: console_type.h:30
IConsoleAlias * IConsoleAliasGet(const char *name)
Find the alias pointed to by its string.
Definition: console.cpp:310
uint8 max_companies
maximum amount of companies
char * RemoveUnderscores(char *name)
Remove underscores from a string; the string will be modified!
Definition: console.cpp:241
Ship vehicle type.
Definition: vehicle_type.h:26
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:139
bool MakeScreenshot(ScreenshotType t, const char *name)
Make an actual screenshot.
Definition: screenshot.cpp:819
uint8 FindFirstBit(uint32 x)
Search the first set bit in a 32 bit variable.
bool newgrf_developer_tools
activate NewGRF developer tools and allow modifying NewGRFs in an existing game
ContentType
The values in the enum are important; they are used as database &#39;keys&#39;.
Definition: tcp_content.h:25
bool NetworkServerChangeClientName(ClientID client_id, const char *new_name)
Change the client name of the given client.
bool FiosDelete(const char *name)
Delete a file.
Definition: fios.cpp:270
void StartupEngines()
Start/initialise all our engines.
Definition: engine.cpp:688
void Select(ContentID cid)
Select a specific content id.
Functions related to companies.
static uint MapSize()
Get the size of the map.
Definition: map_func.h:94
void NetworkServerSendChat(NetworkAction action, DestType type, int dest, const char *msg, ClientID from_id, int64 data=0, bool from_admin=false)
Send an actual chat message.
static const uint16 NETWORK_DEFAULT_PORT
The default port of the game server (TCP & UDP)
Definition: config.h:31
Base class for engines.
void SelectAll()
Select everything we can select.
uint32 generation_seed
noise seed for world generation
GUISettings gui
settings related to the GUI
–Aliases– Aliases are like shortcuts for complex functions, variable assignments, etc.
static bool ResetToCurrentNewGRFConfig()
Tries to reset the engine mapping to match the current NewGRF configuration.
Definition: engine.cpp:528
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
Declarations for savegames operations.
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
void IConsoleAliasRegister(const char *name, const char *cmd)
Register a an alias for an already existing command in the console.
Definition: console.cpp:287
static size_t GetNumItems()
Returns number of valid items in the pool.
Definition: pool_type.hpp:276
List of file information.
Definition: fios.h:106
ContentType type
Type of content.
Definition: tcp_content.h:69
const char * FiosBrowseTo(const FiosItem *item)
Browse to a new path based on the passed item, starting at #_fios_path.
Definition: fios.cpp:152
uint8 max_clients
maximum amount of clients
ClientID
&#39;Unique&#39; identifier to be given to clients
Definition: network_type.h:43
Restart –> &#39;Random game&#39; with current settings.
Definition: openttd.h:29
void NetworkClientSendRcon(const char *password, const char *command)
Send a remote console command.
char * cmdline
command(s) that is/are being aliased
static void Rescan()
Rescans all searchpaths for available AIs.
Definition: ai_core.cpp:347
IConsoleHook * hook
any special trigger action that needs executing
void IConsoleError(const char *string)
It is possible to print error information to the console.
Definition: console.cpp:175
DEF_CONSOLE_CMD(ConResetEngines)
Reset status of all engines.
Helper to mark the begin of the types.
Definition: tcp_content.h:26
void Erase(T *item)
Removes given item from this vector.
StringList _network_ban_list
The banned clients.
Definition: network.cpp:69
void ValidateFileList(bool force_reload=false)
(Re-)validate the file storage cache.
SwitchMode _switch_mode
The next mainloop command.
Definition: gfx.cpp:47
Functions related to OTTD&#39;s landscape.
bool ScrollMainWindowToTile(TileIndex tile, bool instant)
Scrolls the viewport of the main window to a given location.
Definition: viewport.cpp:2268
void ScanNewGRFFiles(NewGRFScanCallback *callback)
Scan for all NewGRFs.
Base functions for all Games.
Functions related to commands.
Network functions used by other parts of OpenTTD.
bool _network_server
network-server is active
Definition: network.cpp:57
static const uint ICON_CMDLN_SIZE
maximum length of a typed in command
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-NULL) Titem.
Definition: pool_type.hpp:235
Colours _company_colours[MAX_COMPANIES]
NOSAVE: can be determined from company structs.
Definition: company_cmd.cpp:48
static char * GetConsoleLibraryList(char *p, const char *last)
Wrapper function for AIScanner::GetAIConsoleLibraryList.
Definition: ai_core.cpp:322
static const TextColour CC_ERROR
Colour for error lines.
Definition: console_type.h:26
void NetworkDisconnect(bool blocking, bool close_admins)
We want to disconnect from the host/clients.
Definition: network.cpp:798
bool ai_in_multiplayer
so we allow AIs in multiplayer
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
The content has been manually selected.
Definition: tcp_content.h:62
char last_host[NETWORK_HOSTNAME_LENGTH]
IP address of the last joined server.
static bool CanStartNew()
Is it possible to start a new AI company?
Definition: ai_core.cpp:31
completed successfully
Definition: saveload.h:20
Base functions for all AIs.
const T * Get(uint index) const
Get the pointer to item "number" (const)
GameCreationSettings game_creation
settings used during the creation of a game (map)
void SetMode(FiosType ft)
Set the mode and file type of the file to save or load based on the type of file entry at the file sy...
Definition: saveload.cpp:2919
void SetDebugString(const char *s)
Set debugging levels by parsing the text in s.
Definition: debug.cpp:179
bool file_list_valid
If set, the file list is valid.
AIConfig stores the configuration settings of every AI.
Owner
Enum for all companies/owners.
Definition: company_type.h:20
Window functions not directly related to making/drawing windows.
void ReloadNewGRFData()
Reload all NewGRF files during a running game.
Definition: afterload.cpp:3004
static NetworkClientInfo * GetByClientID(ClientID client_id)
Return the CI given it&#39;s client-identifier.
Definition: network.cpp:126
void StringToSettings(const char *value)
Convert a string which is stored in the config file or savegames to custom settings of this Script...
const char * NetworkChangeCompanyPassword(CompanyID company_id, const char *password)
Change the company password of a given company.
Definition: network.cpp:174
Disallow command execution.
Date _date
Current date in days (day counter)
Definition: date.cpp:28
static bool NetworkAvailable(bool echo)
Check network availability and inform in console about failure of detection.
void ConvertDateToYMD(Date date, YearMonthDay *ymd)
Converts a Date to a Year, Month & Day.
Definition: date.cpp:94
An invalid company.
Definition: company_type.h:32
static const TextColour CC_WARNING
Colour for warning lines.
Definition: console_type.h:27
Send message/notice to everyone playing the same company (Team)
Definition: network_type.h:84
Container for all important information about a piece of content.
Definition: tcp_content.h:58
bool GetArgumentInteger(uint32 *value, const char *arg)
Change a string into its number representation.
Definition: console.cpp:187
static const TextColour CC_WHITE
White console lines for various things such as the welcome.
Definition: console_type.h:31
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:165
void IConsoleCmdRegister(const char *name, IConsoleCmdProc *proc, IConsoleHook *hook)
Register a new command to be used in the console.
Definition: console.cpp:256
ClientNetworkContentSocketHandler _network_content_client
The client we use to connect to the server.
static const TextColour CC_INFO
Colour for information lines.
Definition: console_type.h:28
DEF_CONSOLE_HOOK(ConHookServerOnly)
Check whether we are a server.
Server part of the admin network protocol.
void IConsoleClose()
Close the in-game console.
ScreenshotType
Type of requested screenshot.
Definition: screenshot.h:20
Train vehicle type.
Definition: vehicle_type.h:24
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:201
Base for the NewGRF implementation.
pause the game
Definition: command_type.h:255