summaryrefslogtreecommitdiff
path: root/src/ai
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 /src/ai
parent7f6016031e8367c8ce53a4017e5dadd47dc1974e (diff)
downloadopenttd-f65f276d10e528ffe2581656cfa8066a20002995.tar.xz
(svn r18763) -Feature [FS#3095]: rerandomise AIs on reloading (via the debug window) when they were randomly chosen
Diffstat (limited to 'src/ai')
-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
4 files changed, 25 insertions, 8 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;