summaryrefslogtreecommitdiff
path: root/src/ai
diff options
context:
space:
mode:
authoryexo <yexo@openttd.org>2009-04-21 19:13:32 +0000
committeryexo <yexo@openttd.org>2009-04-21 19:13:32 +0000
commit3949050714463bf13ffb6da231019de6db22a8e5 (patch)
treeb6dc258527d34f2a1db659be97eb7f48e16a7da6 /src/ai
parentd4d163e127a90aecc64ddcfb37c96acbde91d658 (diff)
downloadopenttd-3949050714463bf13ffb6da231019de6db22a8e5.tar.xz
(svn r16113) -Feature [NoAI]: Add UseAsRandomAI as function in info.nut. When an AI returns false, it'll never be chosen as random AI.
Diffstat (limited to 'src/ai')
-rw-r--r--src/ai/ai_info.cpp6
-rw-r--r--src/ai/ai_info.hpp6
-rw-r--r--src/ai/ai_scanner.cpp21
3 files changed, 28 insertions, 5 deletions
diff --git a/src/ai/ai_info.cpp b/src/ai/ai_info.cpp
index aa783c385..da9522d8d 100644
--- a/src/ai/ai_info.cpp
+++ b/src/ai/ai_info.cpp
@@ -66,6 +66,12 @@ AILibrary::~AILibrary()
} else {
info->min_loadable_version = info->GetVersion();
}
+ /* When there is an UseAsRandomAI function, call it. */
+ if (info->engine->MethodExists(*info->SQ_instance, "UseAsRandomAI")) {
+ if (!info->engine->CallBoolMethod(*info->SQ_instance, "UseAsRandomAI", &info->use_as_random)) return SQ_ERROR;
+ } else {
+ info->use_as_random = true;
+ }
/* Remove the link to the real instance, else it might get deleted by RegisterAI() */
sq_setinstanceup(vm, 2, NULL);
diff --git a/src/ai/ai_info.hpp b/src/ai/ai_info.hpp
index 1d86bc9ff..81b1def3e 100644
--- a/src/ai/ai_info.hpp
+++ b/src/ai/ai_info.hpp
@@ -94,9 +94,15 @@ public:
*/
int GetSettingDefaultValue(const char *name) const;
+ /**
+ * Use this AI as a random AI.
+ */
+ bool UseAsRandomAI() const { return this->use_as_random; }
+
private:
AIConfigItemList config_list;
int min_loadable_version;
+ bool use_as_random;
};
class AILibrary : public AIFileInfo {
diff --git a/src/ai/ai_scanner.cpp b/src/ai/ai_scanner.cpp
index ab9036cf7..a588f5b9b 100644
--- a/src/ai/ai_scanner.cpp
+++ b/src/ai/ai_scanner.cpp
@@ -243,20 +243,31 @@ void AIScanner::RegisterAI(AIInfo *info)
AIInfo *AIScanner::SelectRandomAI()
{
- if (this->info_single_list.size() == 0) {
+ uint num_random_ais = 0;
+ for (AIInfoList::iterator it = this->info_single_list.begin(); it != this->info_single_list.end(); it++) {
+ if (it->second->UseAsRandomAI()) num_random_ais++;
+ }
+
+ if (num_random_ais == 0) {
DEBUG(ai, 0, "No suitable AI found, loading 'dummy' AI.");
return this->info_dummy;
}
/* Find a random AI */
uint pos;
- if (_networking) pos = InteractiveRandomRange((uint16)this->info_single_list.size());
- else pos = RandomRange((uint16)this->info_single_list.size());
+ if (_networking) {
+ pos = InteractiveRandomRange(num_random_ais);
+ } else {
+ pos = RandomRange(num_random_ais);
+ }
/* Find the Nth item from the array */
AIInfoList::iterator it = this->info_single_list.begin();
- for (; pos > 0; pos--) it++;
- AIInfoList::iterator first_it = it;
+ while (!it->second->UseAsRandomAI()) it++;
+ for (; pos > 0; pos--) {
+ it++;
+ while (!it->second->UseAsRandomAI()) it++;
+ }
return (*it).second;
}