(svn r12260) [NoAI] -Change: make SelectAI return the factory, so some GUI might read how the AI is called, and who wrote it, etc etc
--- a/src/ai/ai.cpp Mon Feb 25 16:36:24 2008 +0000
+++ b/src/ai/ai.cpp Mon Feb 25 18:18:35 2008 +0000
@@ -22,6 +22,7 @@
#include "../signal_func.h"
static AIController *_ai_player[MAX_PLAYERS];
+static AIFactoryBase *_ai_factory[MAX_PLAYERS];
static uint _ai_frame_counter;
static bool _ai_enabled;
static char *_forced_ai_name = NULL;
@@ -82,12 +83,13 @@
if (!_ai_enabled) return;
if (_networking && !_network_server) return;
+ AIFactoryBase *factory;
if (_forced_ai_name == NULL) {
- _ai_player[player] = AIFactoryBase::SelectRandomAI();
+ factory = AIFactoryBase::SelectRandomAI();
} else {
- _ai_player[player] = AIFactoryBase::SelectAI(_forced_ai_name);
+ factory = AIFactoryBase::SelectAI(_forced_ai_name);
}
- if (_ai_player[player] == NULL) {
+ if (factory == NULL) {
if (_forced_ai_name == NULL) {
DEBUG(ai, 0, "Couldn't find a suitable AI to start for company '%d'", player);
} else {
@@ -96,6 +98,9 @@
/* XXX -- We have no clean way to handle this yet! */
return;
}
+
+ _ai_player[player] = factory->CreateInstance();
+ _ai_factory[player] = factory;
_current_player = player;
AIObject::ResetInternalPlayerData();
AI_StartPlayer(player, _ai_player[player]);
@@ -113,6 +118,7 @@
/* Called if this AI died */
delete _ai_player[player];
_ai_player[player] = NULL;
+ _ai_factory[player] = NULL;
}
/**
@@ -124,6 +130,7 @@
AI_Uninitialize();
memset(&_ai_player, 0, sizeof(_ai_player));
+ memset(&_ai_factory, 0, sizeof(_ai_factory));
_ai_frame_counter = 0;
_ai_enabled = true;
@@ -141,6 +148,10 @@
_forced_ai_name = (forced_ai == NULL) ? NULL : strdup(forced_ai);
}
+AIFactoryBase *AI_GetPlayerFactory(PlayerID player)
+{
+ return _ai_factory[player];
+}
/**
* Deinitializer for AI-related stuff.
@@ -186,5 +197,6 @@
void AI_Uninitialize() {}
bool AI_AllowNewAI() { return false; }
void AI_ForceAI(const char *forced_ai) {}
+AIFactoryBase *AI_GetPlayerFactory(PlayerID player) { return NULL; }
void CcAI(bool success, TileIndex tile, uint32 p1, uint32 p2) {}
#endif /* NO_THREADS */
--- a/src/ai/ai_factory.hpp Mon Feb 25 16:36:24 2008 +0000
+++ b/src/ai/ai_factory.hpp Mon Feb 25 18:18:35 2008 +0000
@@ -65,7 +65,7 @@
/**
* Select a random AI from all known AIs.
*/
- static AIController *SelectRandomAI()
+ static AIFactoryBase *SelectRandomAI()
{
if (GetFactories().size() == 0) return NULL;
@@ -93,14 +93,14 @@
/* Unable to start any AI */
if (first_it == it) return NULL;
}
- return f->CreateInstance();
+ return f;
}
/**
* Select the AI with the given name.
* @param name the AI to select.
*/
- static AIController *SelectAI(const char *name)
+ static AIFactoryBase *SelectAI(const char *name)
{
if (GetFactories().size() == 0) return NULL;
@@ -109,7 +109,7 @@
AIFactoryBase *f = (*it).second;
if (strcasecmp(name, f->name) == 0 && f->AllowStartup()) {
- return f->CreateInstance();
+ return f;
}
}
@@ -137,6 +137,10 @@
}
+ /**
+ * Get the name of the AI internally.
+ */
+ const char *GetAIName() { return this->name; }
/**
* Get the author of the AI.