Index: src/console_cmds.cpp =================================================================== --- src/console_cmds.cpp (revision 19627) +++ src/console_cmds.cpp (working copy) @@ -1088,11 +1132,11 @@ return true; } -DEF_CONSOLE_CMD(ConReloadAI) +DEF_CONSOLE_CMD(ConRestartAI) { if (argc != 2) { - IConsoleHelp("Reload an AI. Usage: 'reload_ai '"); - 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."); + IConsoleHelp("Restart an AI. Usage: 'restart_ai '"); + IConsoleHelp("Delete and start again 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."); return true; } @@ -1102,7 +1146,7 @@ } if (_networking && !_network_server) { - IConsoleWarning("Only the server can reload an AI."); + IConsoleWarning("Only the server can restart an AI."); return true; } @@ -1120,11 +1164,69 @@ /* First kill the company of the AI, then start a new one. This should start the current AI again */ DoCommandP(0, 2, company_id, CMD_COMPANY_CTRL); DoCommandP(0, 1, company_id, CMD_COMPANY_CTRL); + IConsolePrint(CC_DEFAULT, "AI restarted."); + + return true; +} + +DEF_CONSOLE_CMD(ConReloadAI) +{ + if (argc <2 || argc > 4) { + IConsoleHelp("Reload a new AI. Usage: 'reload_ai [] []'"); + IConsoleHelp("Reload an AI code without removing the company. If is given, that specific AI (if found) is started instead of the old one."); + IConsoleHelp("If is given, it is parsed and the AI settings are set to that."); + return true; + } + + if (_game_mode != GM_NORMAL) { + IConsoleWarning("AIs can only be managed in a game."); + return true; + } + if (_networking && !_network_server) { + IConsoleWarning("Only the server can start a new AI."); + return true; + } + + CompanyID company_id = (CompanyID)(atoi(argv[1]) - 1); + if (!Company::IsValidID(company_id)) { + IConsolePrintF(CC_DEFAULT, "Unknown company. Company range is between 1 and %d.", MAX_COMPANIES); + return true; + } + + if (Company::IsHumanID(company_id)) { + IConsoleWarning("Company is not controlled by an AI."); + return true; + } + + /* Load different AI if specified by parameters */ + AIConfig *config = AIConfig::GetConfig(company_id); + if (argc >= 3) { + /** Store name of current AI - in case new AI is not available, return back to current one */ + char* oldname = strdup(config->GetName()); + config->ChangeAI(argv[2]); + if (!config->HasAI()) { + config->ChangeAI(oldname); + free(oldname); + IConsoleWarning("Failed to load the specified AI"); + return true; + } + free(oldname); + if (argc == 4) { + config->StringToSettings(argv[3]); + } + } + + /* Stop current AI */ + AI::Stop(company_id); + + /* Start a new AI */ + AI::StartNew(company_id); IConsolePrint(CC_DEFAULT, "AI reloaded."); return true; } + DEF_CONSOLE_CMD(ConStopAI) { if (argc != 2) { @@ -1782,6 +1885,7 @@ #ifdef ENABLE_AI IConsoleCmdRegister("list_ai", ConListAI); IConsoleCmdRegister("reload_ai", ConReloadAI); + IConsoleCmdRegister("restart_ai", ConRestartAI); IConsoleCmdRegister("rescan_ai", ConRescanAI); IConsoleCmdRegister("start_ai", ConStartAI); IConsoleCmdRegister("stop_ai", ConStopAI);