summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2010-01-09 14:41:22 +0000
committerrubidium <rubidium@openttd.org>2010-01-09 14:41:22 +0000
commitf65f276d10e528ffe2581656cfa8066a20002995 (patch)
tree40c36e7a0eb0070dbb4cb04eda09480fee9d0bbf
parent7f6016031e8367c8ce53a4017e5dadd47dc1974e (diff)
downloadopenttd-f65f276d10e528ffe2581656cfa8066a20002995.tar.xz
(svn r18763) -Feature [FS#3095]: rerandomise AIs on reloading (via the debug window) when they were randomly chosen
-rw-r--r--src/ai/ai.hpp3
-rw-r--r--src/ai/ai_config.cpp9
-rw-r--r--src/ai/ai_config.hpp12
-rw-r--r--src/ai/ai_core.cpp9
-rw-r--r--src/saveload/ai_sl.cpp19
5 files changed, 36 insertions, 16 deletions
diff --git a/src/ai/ai.hpp b/src/ai/ai.hpp
index 7d1b0673c..a23c411af 100644
--- a/src/ai/ai.hpp
+++ b/src/ai/ai.hpp
@@ -45,8 +45,9 @@ public:
/**
* Start a new AI company.
* @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.
diff --git a/src/ai/ai_config.cpp b/src/ai/ai_config.cpp
index aec2760d5..4b725728a 100644
--- a/src/ai/ai_config.cpp
+++ b/src/ai/ai_config.cpp
@@ -16,12 +16,13 @@
#include "ai.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);
this->name = (name == NULL) ? NULL : strdup(name);
this->info = (name == NULL) ? NULL : AI::FindInfo(this->name, version);
this->version = (info == NULL) ? -1 : info->GetVersion();
+ this->is_random_ai = is_random_ai;
if (this->config_list != NULL) delete this->config_list;
this->config_list = (info == NULL) ? NULL : new AIConfigItemList();
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->version = config->version;
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++) {
this->settings[strdup((*it).first)] = (*it).second;
@@ -167,6 +169,11 @@ bool AIConfig::HasAI() const
return this->info != NULL;
}
+bool AIConfig::IsRandomAI() const
+{
+ return this->is_random_ai;
+}
+
const char *AIConfig::GetName() const
{
return this->name;
diff --git a/src/ai/ai_config.hpp b/src/ai/ai_config.hpp
index 5008b3a3e..fb53b725b 100644
--- a/src/ai/ai_config.hpp
+++ b/src/ai/ai_config.hpp
@@ -25,7 +25,8 @@ public:
name(NULL),
version(-1),
info(NULL),
- config_list(NULL)
+ config_list(NULL),
+ is_random_ai(false)
{}
AIConfig(const AIConfig *config);
~AIConfig();
@@ -34,8 +35,9 @@ public:
* Set another AI to be loaded in this slot.
* @param name The name of the AI.
* @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
@@ -90,6 +92,11 @@ public:
bool HasAI() const;
/**
+ * Is the current AI a randomly chosen AI?
+ */
+ bool IsRandomAI() const;
+
+ /**
* Get the name of the AI.
*/
const char *GetName() const;
@@ -117,6 +124,7 @@ private:
class AIInfo *info;
SettingValueList settings;
AIConfigItemList *config_list;
+ bool is_random_ai;
};
#endif /* AI_CONFIG_HPP */
diff --git a/src/ai/ai_core.cpp b/src/ai/ai_core.cpp
index a8e96085b..baf738920 100644
--- a/src/ai/ai_core.cpp
+++ b/src/ai/ai_core.cpp
@@ -32,19 +32,20 @@
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));
/* Clients shouldn't start AIs */
if (_networking && !_network_server) return;
- AIInfo *info = AIConfig::GetConfig(company)->GetInfo();
- if (info == NULL) {
+ AIConfig *config = AIConfig::GetConfig(company);
+ AIInfo *info = config->GetInfo();
+ if (info == NULL || (rerandomise_ai && config->IsRandomAI())) {
info = AI::ai_scanner->SelectRandomAI();
assert(info != NULL);
/* 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;
diff --git a/src/saveload/ai_sl.cpp b/src/saveload/ai_sl.cpp
index 95fd95575..a69f3da60 100644
--- a/src/saveload/ai_sl.cpp
+++ b/src/saveload/ai_sl.cpp
@@ -22,12 +22,14 @@
static char _ai_saveload_name[64];
static int _ai_saveload_version;
static char _ai_saveload_settings[1024];
+static bool _ai_saveload_is_random;
static const SaveLoad _ai_company[] = {
- SLEG_STR(_ai_saveload_name, SLE_STRB),
- SLEG_STR(_ai_saveload_settings, SLE_STRB),
- SLEG_CONDVAR(_ai_saveload_version, SLE_UINT32, 108, SL_MAX_VERSION),
- SLE_END()
+ SLEG_STR(_ai_saveload_name, SLE_STRB),
+ SLEG_STR(_ai_saveload_settings, SLE_STRB),
+ 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()
};
static void SaveReal_AIPL(int *index_ptr)
@@ -44,6 +46,7 @@ static void SaveReal_AIPL(int *index_ptr)
_ai_saveload_version = -1;
}
+ _ai_saveload_is_random = config->IsRandomAI();
_ai_saveload_settings[0] = '\0';
config->SettingsToString(_ai_saveload_settings, lengthof(_ai_saveload_settings));
@@ -72,13 +75,13 @@ static void Load_AIPL()
AIConfig *config = AIConfig::GetConfig(index);
if (StrEmpty(_ai_saveload_name)) {
/* A random AI. */
- config->ChangeAI(NULL);
+ config->ChangeAI(NULL, -1, true);
} else {
- config->ChangeAI(_ai_saveload_name, _ai_saveload_version);
+ config->ChangeAI(_ai_saveload_name, _ai_saveload_version, _ai_saveload_is_random);
if (!config->HasAI()) {
/* No version of the AI available that can load the data. Try to load the
* 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 (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);
@@ -101,7 +104,7 @@ static void Load_AIPL()
/* Start the AI directly if it was active in the savegame */
if (Company::IsValidAiID(index)) {
- AI::StartNew(index);
+ AI::StartNew(index, false);
AI::Load(index, _ai_saveload_version);
}
}