(svn r12251) [NoAI] -Add: 2 console commands: 'list_ai' and 'start_ai', listing or starting an AI noai
authortruebrain
Mon, 25 Feb 2008 14:59:56 +0000
branchnoai
changeset 9763 d6098707195b
parent 9762 ad9c304c6c77
child 9764 e499a1afc774
(svn r12251) [NoAI] -Add: 2 console commands: 'list_ai' and 'start_ai', listing or starting an AI
src/console_cmds.cpp
--- a/src/console_cmds.cpp	Mon Feb 25 14:56:45 2008 +0000
+++ b/src/console_cmds.cpp	Mon Feb 25 14:59:56 2008 +0000
@@ -33,6 +33,8 @@
 #include "player_func.h"
 #include "player_base.h"
 #include "settings_type.h"
+#include "ai/ai.h"
+#include "ai/ai_factory.hpp"
 
 #ifdef ENABLE_NETWORK
 	#include "table/strings.h"
@@ -929,6 +931,55 @@
 	return true;
 }
 
+DEF_CONSOLE_CMD(ConListAI)
+{
+	char buf[1024];
+	char *p = &buf[0];
+	p = AIFactoryBase::GetAIConsoleList(p, lastof(buf));
+
+	p = &buf[0];
+	/* Print output line by line */
+	for (char *p2 = &buf[0]; *p2 != '\0'; p2++) {
+		if (*p2 == '\n') {
+			*p2 = '\0';
+			IConsolePrintF(_icolour_def, "%s", p);
+			p = p2 + 1;
+		}
+	}
+
+	return true;
+}
+
+DEF_CONSOLE_CMD(ConStartAI)
+{
+	if (argc == 0) {
+		IConsoleHelp("Start a new AI. Usage: 'startai [<AI>]'");
+		IConsoleHelp("Start a new AI. If <AI> is given, it starts that specific AI (if found).");
+		return true;
+	}
+
+	uint n = 0;
+	Player *p;
+	FOR_ALL_PLAYERS(p) {
+		if (p->is_active) n++;
+	}
+	if (n == MAX_PLAYERS) {
+		IConsoleWarning("Can't start a new AI (no more free slots).");
+		return true;
+	}
+	if (!AI_AllowNewAI()) {
+		IConsoleWarning("Can't start a new AI (you are not the server?).");
+		return true;
+	}
+
+	AI_ForceAI(argc == 1 ? NULL : argv[1]);
+
+	/* Start a new AI player */
+	DoCommandP(0, 1, 0, NULL, CMD_PLAYER_CTRL);
+
+	return true;
+}
+
 DEF_CONSOLE_CMD(ConGetSeed)
 {
 	if (argc == 0) {
@@ -1511,6 +1562,7 @@
 	IConsoleCmdRegister("help",         ConHelp);
 	IConsoleCmdRegister("info_cmd",     ConInfoCmd);
 	IConsoleCmdRegister("info_var",     ConInfoVar);
+	IConsoleCmdRegister("list_ai",      ConListAI);
 	IConsoleCmdRegister("list_cmds",    ConListCommands);
 	IConsoleCmdRegister("list_vars",    ConListVariables);
 	IConsoleCmdRegister("list_aliases", ConListAliases);
@@ -1529,6 +1581,7 @@
 	IConsoleCmdRegister("rm",           ConRemove);
 	IConsoleCmdRegister("save",         ConSave);
 	IConsoleCmdRegister("saveconfig",   ConSaveConfig);
+	IConsoleCmdRegister("start_ai",     ConStartAI);
 	IConsoleCmdRegister("ls",           ConListFiles);
 	IConsoleCmdRegister("cd",           ConChangeDirectory);
 	IConsoleCmdRegister("pwd",          ConPrintWorkingDirectory);