OpenTTD Source 20250523-master-g321f7e8683
ai_scanner.cpp
Go to the documentation of this file.
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 */
7
10#include "../stdafx.h"
11#include <ranges>
12#include "../debug.h"
13#include "../network/network.h"
14#include "../openttd.h"
15#include "../core/random_func.hpp"
16
17#include "../script/squirrel_class.hpp"
18#include "../script/api/script_object.hpp"
19#include "ai_info.hpp"
20#include "ai_scanner.hpp"
21
22#include "../safeguards.h"
23
24
25AIScannerInfo::AIScannerInfo() :
27 info_dummy(nullptr)
28{
29}
30
31void AIScannerInfo::Initialize()
32{
33 ScriptScanner::Initialize("AIScanner");
34
35 ScriptAllocatorScope alloc_scope(this->engine);
36
37 /* Create the dummy AI */
38 this->main_script = "%_dummy";
39 Script_CreateDummyInfo(this->engine->GetVM(), "AI", "ai");
40}
41
43{
44 this->info_dummy = info;
45}
46
47AIScannerInfo::~AIScannerInfo()
48{
49 delete this->info_dummy;
50}
51
53{
54 return info.GetName();
55}
56
61
63{
64 if (_game_mode == GM_MENU) {
65 Debug(script, 0, "The intro game should not use AI, loading 'dummy' AI.");
66 return this->info_dummy;
67 }
68
69 /* Filter for AIs suitable as Random AI. */
70 auto random_ais = info_single_list | std::views::filter([](const auto &item) { return static_cast<AIInfo *>(item.second)->UseAsRandomAI(); });
71
72 uint num_random_ais = std::ranges::distance(random_ais);
73 if (num_random_ais == 0) {
74 Debug(script, 0, "No suitable AI found, loading 'dummy' AI.");
75 return this->info_dummy;
76 }
77
78 /* Pick a random AI */
79 uint pos = ScriptObject::GetRandomizer(OWNER_NONE).Next(num_random_ais);
80 auto it = std::ranges::next(std::begin(random_ais), pos, std::end(random_ais));
81 assert(it != std::end(random_ais));
82
83 return static_cast<AIInfo *>(it->second);
84}
85
86AIInfo *AIScannerInfo::FindInfo(const std::string &name, int version, bool force_exact_match)
87{
88 if (this->info_list.empty()) return nullptr;
89 if (name.empty()) return nullptr;
90
91 if (version == -1) {
92 /* We want to load the latest version of this AI; so find it */
93 auto it = this->info_single_list.find(name);
94 if (it != this->info_single_list.end()) return static_cast<AIInfo *>(it->second);
95 return nullptr;
96 }
97
98 if (force_exact_match) {
99 /* Try to find a direct 'name.version' match */
100 std::string name_with_version = fmt::format("{}.{}", name, version);
101 auto it = this->info_list.find(name_with_version);
102 if (it != this->info_list.end()) return static_cast<AIInfo *>(it->second);
103 return nullptr;
104 }
105
106 AIInfo *info = nullptr;
107 int highest_version = -1;
108
109 /* See if there is a compatible AI which goes by that name, with the highest
110 * version which allows loading the requested version */
111 for (const auto &item : this->info_list) {
112 AIInfo *i = static_cast<AIInfo *>(item.second);
113 if (StrEqualsIgnoreCase(name, i->GetName()) && i->CanLoadFromVersion(version) && (highest_version == -1 || i->GetVersion() > highest_version)) {
114 highest_version = item.second->GetVersion();
115 info = i;
116 }
117 }
118
119 return info;
120}
121
122
123void AIScannerLibrary::Initialize()
124{
125 ScriptScanner::Initialize("AIScanner");
126}
127
129{
130 AILibrary &library = static_cast<AILibrary &>(info);
131 return fmt::format("{}.{}", library.GetCategory(), library.GetInstanceName());
132}
133
138
139AILibrary *AIScannerLibrary::FindLibrary(const std::string &library, int version)
140{
141 /* Internally we store libraries as 'library.version' */
142 std::string library_name = fmt::format("{}.{}", library, version);
143
144 /* Check if the library + version exists */
145 ScriptInfoList::iterator it = this->info_list.find(library_name);
146 if (it == this->info_list.end()) return nullptr;
147
148 return static_cast<AILibrary *>((*it).second);
149}
AIInfo keeps track of all information of an AI, like Author, Description, ...
declarations of the class for AI scanner
All static information from an AI like name, version, etc.
Definition ai_info.hpp:16
bool CanLoadFromVersion(int version) const
Check if we can start this AI.
Definition ai_info.cpp:121
static void RegisterAPI(Squirrel &engine)
Register the functions of this class.
Definition ai_info.cpp:32
All static information from an AI library like name, version, etc.
Definition ai_info.hpp:60
const std::string & GetCategory() const
Get the category this library is in.
Definition ai_info.hpp:77
static void RegisterAPI(Squirrel &engine)
Register the functions of this class.
Definition ai_info.cpp:128
class AIInfo * FindInfo(const std::string &name, int version, bool force_exact_match)
Check if we have an AI by name and version available in our list.
void RegisterAPI(class Squirrel &engine) override
Register the API for this ScriptInfo.
AIInfo * info_dummy
The dummy AI.
class AIInfo * SelectRandomAI() const
Select a random AI.
std::string GetScriptName(ScriptInfo &info) override
Get the script name how to store the script in memory.
void SetDummyAI(class AIInfo *info)
Set the Dummy AI.
void RegisterAPI(class Squirrel &engine) override
Register the API for this ScriptInfo.
std::string GetScriptName(ScriptInfo &info) override
Get the script name how to store the script in memory.
class AILibrary * FindLibrary(const std::string &library, int version)
Find a library in the pool.
All static information from an Script like name, version, etc.
const std::string & GetInstanceName() const
Get the name of the instance of the script to create.
const std::string & GetName() const
Get the Name of the script.
int GetVersion() const
Get the version of the script.
Scanner to help finding scripts.
class Squirrel * engine
The engine we're scanning with.
std::string main_script
The full path of the script.
ScriptInfoList info_list
The list of all script.
ScriptInfoList info_single_list
The list of all unique script. The best script (highest version) is shown.
HSQUIRRELVM GetVM()
Get the squirrel VM.
Definition squirrel.hpp:82
static constexpr Owner OWNER_NONE
The tile has no ownership.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
void Script_CreateDummyInfo(HSQUIRRELVM vm, std::string_view type, std::string_view dir)
Run the dummy info.nut.
bool StrEqualsIgnoreCase(std::string_view str1, std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
Definition string.cpp:321