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/>.
8 /** @file ai_scanner.cpp allows scanning AI scripts */
10 #include "../stdafx.h"
12 #include "../network/network.h"
13 #include "../openttd.h"
14 #include "../core/random_func.hpp"
16 #include "../script/squirrel_class.hpp"
17 #include "../script/api/script_object.hpp"
18 #include "ai_info.hpp"
19 #include "ai_scanner.hpp"
21 #include "../safeguards.h"
24 AIScannerInfo::AIScannerInfo() :
30 void AIScannerInfo::Initialize()
32 ScriptScanner::Initialize("AIScanner");
34 ScriptAllocatorScope
alloc_scope(this->engine
);
36 /* Create the dummy AI */
37 this->main_script
= "%_dummy";
38 Script_CreateDummyInfo(this->engine
->GetVM(), "AI", "ai");
41 void AIScannerInfo::SetDummyAI(class AIInfo
*info
)
43 this->info_dummy
= info
;
46 AIScannerInfo::~AIScannerInfo()
48 delete this->info_dummy
;
51 std::string
AIScannerInfo::GetScriptName(ScriptInfo
*info
)
53 return info
->GetName();
56 void AIScannerInfo::RegisterAPI(class Squirrel
*engine
)
58 AIInfo::RegisterAPI(engine
);
61 AIInfo
*AIScannerInfo::SelectRandomAI() const
63 if (_game_mode
== GM_MENU
) {
64 Debug(script
, 0, "The intro game should not use AI, loading 'dummy' AI.");
65 return this->info_dummy
;
68 uint num_random_ais
= 0;
69 for (const auto &item
: info_single_list
) {
70 AIInfo
*i
= static_cast<AIInfo
*>(item
.second
);
71 if (i
->UseAsRandomAI()) num_random_ais
++;
74 if (num_random_ais
== 0) {
75 Debug(script
, 0, "No suitable AI found, loading 'dummy' AI.");
76 return this->info_dummy
;
79 /* Find a random AI */
80 uint pos
= ScriptObject::GetRandomizer(OWNER_NONE
).Next(num_random_ais
);
82 /* Find the Nth item from the array */
83 ScriptInfoList::const_iterator it
= this->info_single_list
.begin();
85 #define GetAIInfo(it) static_cast<AIInfo *>((*it).second)
86 while (!GetAIInfo(it
)->UseAsRandomAI()) it
++;
87 for (; pos
> 0; pos
--) {
89 while (!GetAIInfo(it
)->UseAsRandomAI()) it
++;
95 AIInfo
*AIScannerInfo::FindInfo(const std::string
&name
, int version
, bool force_exact_match
)
97 if (this->info_list
.empty()) return nullptr;
98 if (name
.empty()) return nullptr;
101 /* We want to load the latest version of this AI; so find it */
102 auto it
= this->info_single_list
.find(name
);
103 if (it
!= this->info_single_list
.end()) return static_cast<AIInfo
*>(it
->second
);
107 if (force_exact_match
) {
108 /* Try to find a direct 'name.version' match */
109 std::string name_with_version
= fmt::format("{}.{}", name
, version
);
110 auto it
= this->info_list
.find(name_with_version
);
111 if (it
!= this->info_list
.end()) return static_cast<AIInfo
*>(it
->second
);
115 AIInfo
*info
= nullptr;
116 int highest_version
= -1;
118 /* See if there is a compatible AI which goes by that name, with the highest
119 * version which allows loading the requested version */
120 for (const auto &item
: this->info_list
) {
121 AIInfo
*i
= static_cast<AIInfo
*>(item
.second
);
122 if (StrEqualsIgnoreCase(name
, i
->GetName()) && i
->CanLoadFromVersion(version
) && (highest_version
== -1 || i
->GetVersion() > highest_version
)) {
123 highest_version
= item
.second
->GetVersion();
132 void AIScannerLibrary::Initialize()
134 ScriptScanner::Initialize("AIScanner");
137 std::string
AIScannerLibrary::GetScriptName(ScriptInfo
*info
)
139 AILibrary
*library
= static_cast<AILibrary
*>(info
);
140 return fmt::format("{}.{}", library
->GetCategory(), library
->GetInstanceName());
143 void AIScannerLibrary::RegisterAPI(class Squirrel
*engine
)
145 AILibrary::RegisterAPI(engine
);
148 AILibrary
*AIScannerLibrary::FindLibrary(const std::string
&library
, int version
)
150 /* Internally we store libraries as 'library.version' */
151 std::string library_name
= fmt::format("{}.{}", library
, version
);
153 /* Check if the library + version exists */
154 ScriptInfoList::iterator it
= this->info_list
.find(library_name
);
155 if (it
== this->info_list
.end()) return nullptr;
157 return static_cast<AILibrary
*>((*it
).second
);