summaryrefslogtreecommitdiff
path: root/src/openttd.cpp
diff options
context:
space:
mode:
authortruebrain <truebrain@openttd.org>2009-01-12 17:11:45 +0000
committertruebrain <truebrain@openttd.org>2009-01-12 17:11:45 +0000
commita3dd7506d377b1434f913bd65c019eed52b64b6e (patch)
treeced1a262eb143ad6e64ec02f4a4c89835c0c32fd /src/openttd.cpp
parent9294f9616866b9778c22076c19b5a32b4f85f788 (diff)
downloadopenttd-a3dd7506d377b1434f913bd65c019eed52b64b6e.tar.xz
(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
Diffstat (limited to 'src/openttd.cpp')
-rw-r--r--src/openttd.cpp48
1 files changed, 41 insertions, 7 deletions
diff --git a/src/openttd.cpp b/src/openttd.cpp
index 66425cec1..8fd819aef 100644
--- 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 @@ void CDECL ShowInfoF(const char *str, ...)
*/
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 @@ static void ShowHelp()
" -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 @@ static void ShowHelp()
/* 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 InitializeDynamicVariables()
static void ShutdownGame()
{
/* stop the AI */
- AI_Uninitialize();
+ AI::Uninitialize(false);
IConsoleFree();
@@ -382,6 +389,24 @@ static void LoadIntroGame()
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 @@ int ttd_main(int argc, char *argv[])
DedicatedFork();
#endif
+ AI::Initialize();
LoadFromConfig();
+ AI::Uninitialize(true);
CheckConfig();
LoadFromHighScore();
@@ -666,7 +693,7 @@ int ttd_main(int argc, char *argv[])
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 @@ static void MakeNewGameDone()
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 @@ void SwitchMode(int new_mode)
/* 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 @@ void SwitchMode(int new_mode)
}
}
#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 @@ void SwitchMode(int new_mode)
/* 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 @@ void StateGameLoop()
CallLandscapeTick();
ClearStorageChanges(true);
- AI_RunGameLoop();
+ AI::GameLoop();
CallWindowTickEvent();
NewsLoop();