1
0
Fork 0

(svn r18763) -Feature [FS#3095]: rerandomise AIs on reloading (via the debug window) when they were randomly chosen

release/1.0
rubidium 2010-01-09 14:41:22 +00:00
parent 7f6016031e
commit f65f276d10
5 changed files with 36 additions and 16 deletions

View File

@ -45,8 +45,9 @@ public:
/** /**
* Start a new AI company. * Start a new AI company.
* @param company At which slot the AI company should start. * @param company At which slot the AI company should start.
* @param rerandomise_ai Whether to rerandomise the configured AI.
*/ */
static void StartNew(CompanyID company); static void StartNew(CompanyID company, bool rerandomise_ai = true);
/** /**
* Called every game-tick to let AIs do something. * Called every game-tick to let AIs do something.

View File

@ -16,12 +16,13 @@
#include "ai.hpp" #include "ai.hpp"
#include "ai_config.hpp" #include "ai_config.hpp"
void AIConfig::ChangeAI(const char *name, int version) void AIConfig::ChangeAI(const char *name, int version, bool is_random_ai)
{ {
free((void *)this->name); free((void *)this->name);
this->name = (name == NULL) ? NULL : strdup(name); this->name = (name == NULL) ? NULL : strdup(name);
this->info = (name == NULL) ? NULL : AI::FindInfo(this->name, version); this->info = (name == NULL) ? NULL : AI::FindInfo(this->name, version);
this->version = (info == NULL) ? -1 : info->GetVersion(); this->version = (info == NULL) ? -1 : info->GetVersion();
this->is_random_ai = is_random_ai;
if (this->config_list != NULL) delete this->config_list; if (this->config_list != NULL) delete this->config_list;
this->config_list = (info == NULL) ? NULL : new AIConfigItemList(); this->config_list = (info == NULL) ? NULL : new AIConfigItemList();
if (this->config_list != NULL) this->config_list->push_back(_start_date_config); if (this->config_list != NULL) this->config_list->push_back(_start_date_config);
@ -56,6 +57,7 @@ AIConfig::AIConfig(const AIConfig *config)
this->info = config->info; this->info = config->info;
this->version = config->version; this->version = config->version;
this->config_list = NULL; this->config_list = NULL;
this->is_random_ai = config->is_random_ai;
for (SettingValueList::const_iterator it = config->settings.begin(); it != config->settings.end(); it++) { for (SettingValueList::const_iterator it = config->settings.begin(); it != config->settings.end(); it++) {
this->settings[strdup((*it).first)] = (*it).second; this->settings[strdup((*it).first)] = (*it).second;
@ -167,6 +169,11 @@ bool AIConfig::HasAI() const
return this->info != NULL; return this->info != NULL;
} }
bool AIConfig::IsRandomAI() const
{
return this->is_random_ai;
}
const char *AIConfig::GetName() const const char *AIConfig::GetName() const
{ {
return this->name; return this->name;

View File

@ -25,7 +25,8 @@ public:
name(NULL), name(NULL),
version(-1), version(-1),
info(NULL), info(NULL),
config_list(NULL) config_list(NULL),
is_random_ai(false)
{} {}
AIConfig(const AIConfig *config); AIConfig(const AIConfig *config);
~AIConfig(); ~AIConfig();
@ -34,8 +35,9 @@ public:
* Set another AI to be loaded in this slot. * Set another AI to be loaded in this slot.
* @param name The name of the AI. * @param name The name of the AI.
* @param version The version of the AI to load, or -1 of latest. * @param version The version of the AI to load, or -1 of latest.
* @param is_random Is the AI chosen randomly?
*/ */
void ChangeAI(const char *name, int version = -1); void ChangeAI(const char *name, int version = -1, bool is_random = false);
/** /**
* When ever the AI Scanner is reloaded, all infos become invalid. This * When ever the AI Scanner is reloaded, all infos become invalid. This
@ -89,6 +91,11 @@ public:
*/ */
bool HasAI() const; bool HasAI() const;
/**
* Is the current AI a randomly chosen AI?
*/
bool IsRandomAI() const;
/** /**
* Get the name of the AI. * Get the name of the AI.
*/ */
@ -117,6 +124,7 @@ private:
class AIInfo *info; class AIInfo *info;
SettingValueList settings; SettingValueList settings;
AIConfigItemList *config_list; AIConfigItemList *config_list;
bool is_random_ai;
}; };
#endif /* AI_CONFIG_HPP */ #endif /* AI_CONFIG_HPP */

View File

@ -32,19 +32,20 @@
return !_networking || (_network_server && _settings_game.ai.ai_in_multiplayer); return !_networking || (_network_server && _settings_game.ai.ai_in_multiplayer);
} }
/* static */ void AI::StartNew(CompanyID company) /* static */ void AI::StartNew(CompanyID company, bool rerandomise_ai)
{ {
assert(Company::IsValidID(company)); assert(Company::IsValidID(company));
/* Clients shouldn't start AIs */ /* Clients shouldn't start AIs */
if (_networking && !_network_server) return; if (_networking && !_network_server) return;
AIInfo *info = AIConfig::GetConfig(company)->GetInfo(); AIConfig *config = AIConfig::GetConfig(company);
if (info == NULL) { AIInfo *info = config->GetInfo();
if (info == NULL || (rerandomise_ai && config->IsRandomAI())) {
info = AI::ai_scanner->SelectRandomAI(); info = AI::ai_scanner->SelectRandomAI();
assert(info != NULL); assert(info != NULL);
/* Load default data and store the name in the settings */ /* Load default data and store the name in the settings */
AIConfig::GetConfig(company)->ChangeAI(info->GetName()); config->ChangeAI(info->GetName(), -1, true);
} }
_current_company = company; _current_company = company;

View File

@ -22,11 +22,13 @@
static char _ai_saveload_name[64]; static char _ai_saveload_name[64];
static int _ai_saveload_version; static int _ai_saveload_version;
static char _ai_saveload_settings[1024]; static char _ai_saveload_settings[1024];
static bool _ai_saveload_is_random;
static const SaveLoad _ai_company[] = { static const SaveLoad _ai_company[] = {
SLEG_STR(_ai_saveload_name, SLE_STRB), SLEG_STR(_ai_saveload_name, SLE_STRB),
SLEG_STR(_ai_saveload_settings, SLE_STRB), SLEG_STR(_ai_saveload_settings, SLE_STRB),
SLEG_CONDVAR(_ai_saveload_version, SLE_UINT32, 108, SL_MAX_VERSION), SLEG_CONDVAR(_ai_saveload_version, SLE_UINT32, 108, SL_MAX_VERSION),
SLEG_CONDVAR(_ai_saveload_is_random, SLE_BOOL, 136, SL_MAX_VERSION),
SLE_END() SLE_END()
}; };
@ -44,6 +46,7 @@ static void SaveReal_AIPL(int *index_ptr)
_ai_saveload_version = -1; _ai_saveload_version = -1;
} }
_ai_saveload_is_random = config->IsRandomAI();
_ai_saveload_settings[0] = '\0'; _ai_saveload_settings[0] = '\0';
config->SettingsToString(_ai_saveload_settings, lengthof(_ai_saveload_settings)); config->SettingsToString(_ai_saveload_settings, lengthof(_ai_saveload_settings));
@ -72,13 +75,13 @@ static void Load_AIPL()
AIConfig *config = AIConfig::GetConfig(index); AIConfig *config = AIConfig::GetConfig(index);
if (StrEmpty(_ai_saveload_name)) { if (StrEmpty(_ai_saveload_name)) {
/* A random AI. */ /* A random AI. */
config->ChangeAI(NULL); config->ChangeAI(NULL, -1, true);
} else { } else {
config->ChangeAI(_ai_saveload_name, _ai_saveload_version); config->ChangeAI(_ai_saveload_name, _ai_saveload_version, _ai_saveload_is_random);
if (!config->HasAI()) { if (!config->HasAI()) {
/* No version of the AI available that can load the data. Try to load the /* No version of the AI available that can load the data. Try to load the
* latest version of the AI instead. */ * latest version of the AI instead. */
config->ChangeAI(_ai_saveload_name, -1); config->ChangeAI(_ai_saveload_name, -1, _ai_saveload_is_random);
if (!config->HasAI()) { if (!config->HasAI()) {
if (strcmp(_ai_saveload_name, "%_dummy") != 0) { if (strcmp(_ai_saveload_name, "%_dummy") != 0) {
DEBUG(ai, 0, "The savegame has an AI by the name '%s', version %d which is no longer available.", _ai_saveload_name, _ai_saveload_version); DEBUG(ai, 0, "The savegame has an AI by the name '%s', version %d which is no longer available.", _ai_saveload_name, _ai_saveload_version);
@ -101,7 +104,7 @@ static void Load_AIPL()
/* Start the AI directly if it was active in the savegame */ /* Start the AI directly if it was active in the savegame */
if (Company::IsValidAiID(index)) { if (Company::IsValidAiID(index)) {
AI::StartNew(index); AI::StartNew(index, false);
AI::Load(index, _ai_saveload_version); AI::Load(index, _ai_saveload_version);
} }
} }