diff src/openttd.cpp @ 10696:7edccbb67398 draft

(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see. NoAI is an API (a framework) to build your own AIs in. See: http://wiki.openttd.org/wiki/index.php/AI:Main_Page With many thanks to: - glx and Rubidium for their syncing, feedback and hard work - Yexo for his feedback, patches, and AIs which tested the system very deep - Morloth for his feedback and patches - TJIP for hosting a challenge which kept NoAI on track - All AI authors for testing our AI API, and all other people who helped in one way or another -Remove: all old AIs and their cheats/hacks
author truebrain <truebrain@openttd.org>
date Mon, 12 Jan 2009 17:11:45 +0000
parents ee6aceffc966
children 0b7a4eee5b46
line wrap: on
line diff
--- a/src/openttd.cpp
+++ b/src/openttd.cpp
@@ -44,7 +44,8 @@
 #include "signs_base.h"
 #include "signs_func.h"
 #include "waypoint.h"
-#include "ai/ai.h"
+#include "ai/ai.hpp"
+#include "ai/ai_config.hpp"
 #include "train.h"
 #include "yapf/yapf.h"
 #include "settings_func.h"
@@ -164,7 +165,7 @@
  */
 static void ShowHelp()
 {
-	char buf[4096];
+	char buf[8192];
 	char *p = buf;
 
 	p += seprintf(p, lastof(buf), "OpenTTD %s\n", _openttd_revision);
@@ -176,6 +177,7 @@
 		"  -s drv              = Set sound driver (see below) (param bufsize,hz)\n"
 		"  -m drv              = Set music driver (see below)\n"
 		"  -b drv              = Set the blitter to use (see below)\n"
+		"  -a ai               = Force use of specific AI (see below)\n"
 		"  -r res              = Set resolution (for instance 800x600)\n"
 		"  -h                  = Display this help text\n"
 		"  -t year             = Set starting year\n"
@@ -211,6 +213,11 @@
 	/* List the blitters */
 	p = BlitterFactoryBase::GetBlittersInfo(p, lastof(buf));
 
+	/* We need to initialize the AI, so it finds the AIs */
+	AI::Initialize();
+	p = AI::GetConsoleList(p, lastof(buf));
+	AI::Uninitialize(true);
+
 	/* ShowInfo put output to stderr, but version information should go
 	 * to stdout; this is the only exception */
 #if !defined(WIN32) && !defined(WIN64)
@@ -321,7 +328,7 @@
 static void ShutdownGame()
 {
 	/* stop the AI */
-	AI_Uninitialize();
+	AI::Uninitialize(false);
 
 	IConsoleFree();
 
@@ -382,6 +389,24 @@
 	if (_music_driver->IsSongPlaying()) ResetMusic();
 }
 
+void MakeNewgameSettingsLive()
+{
+	for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
+		if (_settings_game.ai_config[c] != NULL) {
+			delete _settings_game.ai_config[c];
+		}
+	}
+
+	_settings_game = _settings_newgame;
+
+	for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
+		_settings_game.ai_config[c] = NULL;
+		if (_settings_newgame.ai_config[c] != NULL) {
+			_settings_game.ai_config[c] = new AIConfig(_settings_newgame.ai_config[c]);
+		}
+	}
+}
+
 byte _savegame_sort_order;
 #if defined(UNIX) && !defined(__MORPHOS__)
 extern void DedicatedFork();
@@ -531,7 +556,9 @@
 		DedicatedFork();
 #endif
 
+	AI::Initialize();
 	LoadFromConfig();
+	AI::Uninitialize(true);
 	CheckConfig();
 	LoadFromHighScore();
 
@@ -666,7 +693,7 @@
 	if (_settings_newgame.difficulty.diff_level == 9) SetDifficultyLevel(0, &_settings_newgame.difficulty);
 
 	/* Make sure _settings is filled with _settings_newgame if we switch to a game directly */
-	if (_switch_mode != SM_NONE) _settings_game = _settings_newgame;
+	if (_switch_mode != SM_NONE) MakeNewgameSettingsLive();
 
 	/* initialize the ingame console */
 	IConsoleInit();
@@ -750,14 +777,17 @@
 	SettingsDisableElrail(_settings_game.vehicle.disable_elrails);
 
 	/* In a dedicated server, the server does not play */
-	if (_network_dedicated) {
+	if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 0) {
 		SetLocalCompany(COMPANY_SPECTATOR);
+		IConsoleCmdExec("exec scripts/game_start.scr 0");
 		return;
 	}
 
 	/* Create a single company */
 	DoStartupNewCompany(false);
 
+	IConsoleCmdExec("exec scripts/game_start.scr 0");
+
 	SetLocalCompany(COMPANY_FIRST);
 	_current_company = _local_company;
 	DoCommandP(0, (_settings_client.gui.autorenew << 15 ) | (_settings_client.gui.autorenew_months << 16) | 4, _settings_client.gui.autorenew_money, CMD_SET_AUTOREPLACE);
@@ -909,7 +939,7 @@
 				/* check if we should reload the config */
 				if (_settings_client.network.reload_cfg) {
 					LoadFromConfig();
-					_settings_game = _settings_newgame;
+					MakeNewgameSettingsLive();
 					ResetGRFConfig(false);
 				}
 				NetworkServerStart();
@@ -920,6 +950,8 @@
 		}
 	}
 #endif /* ENABLE_NETWORK */
+	/* Make sure all AI controllers are gone at quiting game */
+	if (new_mode != SM_SAVE) AI::KillAll();
 
 	switch (new_mode) {
 		case SM_EDITOR: /* Switch to scenario editor */
@@ -959,6 +991,8 @@
 				/* Update the local company for a loaded game. It is either always
 				* company #1 (eg 0) or in the case of a dedicated server a spectator */
 				SetLocalCompany(_network_dedicated ? COMPANY_SPECTATOR : COMPANY_FIRST);
+				/* Execute the game-start script */
+				IConsoleCmdExec("exec scripts/game_start.scr 0");
 				/* Decrease pause counter (was increased from opening load dialog) */
 				DoCommandP(0, 0, 0, CMD_PAUSE);
 #ifdef ENABLE_NETWORK
@@ -1112,7 +1146,7 @@
 		CallLandscapeTick();
 		ClearStorageChanges(true);
 
-		AI_RunGameLoop();
+		AI::GameLoop();
 
 		CallWindowTickEvent();
 		NewsLoop();