Fix cc39fa9: New orders are non-stop by default (#8689)
[openttd-github.git] / src / ai / ai_scanner.cpp
blob9b3613712df8e98d64abee40de06323c024b6404
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file ai_scanner.cpp allows scanning AI scripts */
10 #include "../stdafx.h"
11 #include "../debug.h"
12 #include "../network/network.h"
13 #include "../core/random_func.hpp"
15 #include "../script/squirrel_class.hpp"
16 #include "ai_info.hpp"
17 #include "ai_scanner.hpp"
19 #include "../safeguards.h"
22 AIScannerInfo::AIScannerInfo() :
23 ScriptScanner(),
24 info_dummy(nullptr)
28 void AIScannerInfo::Initialize()
30 ScriptScanner::Initialize("AIScanner");
32 ScriptAllocatorScope alloc_scope(this->engine);
34 /* Create the dummy AI */
35 this->main_script = "%_dummy";
36 extern void Script_CreateDummyInfo(HSQUIRRELVM vm, const char *type, const char *dir);
37 Script_CreateDummyInfo(this->engine->GetVM(), "AI", "ai");
40 void AIScannerInfo::SetDummyAI(class AIInfo *info)
42 this->info_dummy = info;
45 AIScannerInfo::~AIScannerInfo()
47 delete this->info_dummy;
50 void AIScannerInfo::GetScriptName(ScriptInfo *info, char *name, const char *last)
52 seprintf(name, last, "%s", info->GetName());
55 void AIScannerInfo::RegisterAPI(class Squirrel *engine)
57 AIInfo::RegisterAPI(engine);
60 AIInfo *AIScannerInfo::SelectRandomAI() const
62 uint num_random_ais = 0;
63 for (const auto &item : info_single_list) {
64 AIInfo *i = static_cast<AIInfo *>(item.second);
65 if (i->UseAsRandomAI()) num_random_ais++;
68 if (num_random_ais == 0) {
69 DEBUG(script, 0, "No suitable AI found, loading 'dummy' AI.");
70 return this->info_dummy;
73 /* Find a random AI */
74 uint pos;
75 if (_networking) {
76 pos = InteractiveRandomRange(num_random_ais);
77 } else {
78 pos = RandomRange(num_random_ais);
81 /* Find the Nth item from the array */
82 ScriptInfoList::const_iterator it = this->info_single_list.begin();
84 #define GetAIInfo(it) static_cast<AIInfo *>((*it).second)
85 while (!GetAIInfo(it)->UseAsRandomAI()) it++;
86 for (; pos > 0; pos--) {
87 it++;
88 while (!GetAIInfo(it)->UseAsRandomAI()) it++;
90 return GetAIInfo(it);
91 #undef GetAIInfo
94 AIInfo *AIScannerInfo::FindInfo(const char *nameParam, int versionParam, bool force_exact_match)
96 if (this->info_list.size() == 0) return nullptr;
97 if (nameParam == nullptr) return nullptr;
99 char ai_name[1024];
100 strecpy(ai_name, nameParam, lastof(ai_name));
101 strtolower(ai_name);
103 if (versionParam == -1) {
104 /* We want to load the latest version of this AI; so find it */
105 if (this->info_single_list.find(ai_name) != this->info_single_list.end()) return static_cast<AIInfo *>(this->info_single_list[ai_name]);
106 return nullptr;
109 if (force_exact_match) {
110 /* Try to find a direct 'name.version' match */
111 char ai_name_tmp[1024];
112 seprintf(ai_name_tmp, lastof(ai_name_tmp), "%s.%d", ai_name, versionParam);
113 strtolower(ai_name_tmp);
114 if (this->info_list.find(ai_name_tmp) != this->info_list.end()) return static_cast<AIInfo *>(this->info_list[ai_name_tmp]);
115 return nullptr;
118 AIInfo *info = nullptr;
119 int version = -1;
121 /* See if there is a compatible AI which goes by that name, with the highest
122 * version which allows loading the requested version */
123 for (const auto &item : this->info_list) {
124 AIInfo *i = static_cast<AIInfo *>(item.second);
125 if (strcasecmp(ai_name, i->GetName()) == 0 && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
126 version = item.second->GetVersion();
127 info = i;
131 return info;
135 void AIScannerLibrary::Initialize()
137 ScriptScanner::Initialize("AIScanner");
140 void AIScannerLibrary::GetScriptName(ScriptInfo *info, char *name, const char *last)
142 AILibrary *library = static_cast<AILibrary *>(info);
143 seprintf(name, last, "%s.%s", library->GetCategory(), library->GetInstanceName());
146 void AIScannerLibrary::RegisterAPI(class Squirrel *engine)
148 AILibrary::RegisterAPI(engine);
151 AILibrary *AIScannerLibrary::FindLibrary(const char *library, int version)
153 /* Internally we store libraries as 'library.version' */
154 char library_name[1024];
155 seprintf(library_name, lastof(library_name), "%s.%d", library, version);
156 strtolower(library_name);
158 /* Check if the library + version exists */
159 ScriptInfoList::iterator it = this->info_list.find(library_name);
160 if (it == this->info_list.end()) return nullptr;
162 return static_cast<AILibrary *>((*it).second);