13#include "../network/network.h"
14#include "../openttd.h"
15#include "../core/random_func.hpp"
17#include "../script/squirrel_class.hpp"
18#include "../script/api/script_object.hpp"
22#include "../safeguards.h"
25AIScannerInfo::AIScannerInfo() =
default;
26AIScannerInfo::~AIScannerInfo() =
default;
28void AIScannerInfo::Initialize()
30 ScriptScanner::Initialize(
"AIScanner");
56 if (_game_mode == GM_MENU) {
57 Debug(script, 0,
"The intro game should not use AI, loading 'dummy' AI.");
62 auto random_ais =
info_single_list | std::views::filter([](
const auto &item) {
return static_cast<AIInfo *
>(item.second)->UseAsRandomAI(); });
64 uint num_random_ais = std::ranges::distance(random_ais);
65 if (num_random_ais == 0) {
66 Debug(script, 0,
"No suitable AI found, loading 'dummy' AI.");
71 uint pos = ScriptObject::GetRandomizer(
OWNER_NONE).Next(num_random_ais);
72 auto it = std::ranges::next(std::begin(random_ais), pos, std::end(random_ais));
73 assert(it != std::end(random_ais));
75 return static_cast<AIInfo *
>(it->second);
80 if (this->
info_list.empty())
return nullptr;
81 if (name.empty())
return nullptr;
90 if (force_exact_match) {
92 std::string name_with_version = fmt::format(
"{}.{}", name, version);
93 auto it = this->
info_list.find(name_with_version);
94 if (it != this->
info_list.end())
return static_cast<AIInfo *
>(it->second);
99 int highest_version = -1;
103 for (
const auto &item : this->
info_list) {
115void AIScannerLibrary::Initialize()
117 ScriptScanner::Initialize(
"AIScanner");
134 std::string library_name = fmt::format(
"{}.{}", library, version);
137 ScriptInfoList::iterator it = this->
info_list.find(library_name);
138 if (it == this->
info_list.end())
return nullptr;
140 return static_cast<AILibrary *
>((*it).second);
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.
bool CanLoadFromVersion(int version) const
Check if we can start this AI.
static void RegisterAPI(Squirrel &engine)
Register the functions of this class.
All static information from an AI library like name, version, etc.
const std::string & GetCategory() const
Get the category this library is in.
static void RegisterAPI(Squirrel &engine)
Register the functions of this class.
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 SetDummyAI(std::unique_ptr< class AIInfo > &&info)
Set the Dummy AI.
std::unique_ptr< AIInfo > info_dummy
The dummy AI.
void RegisterAPI(class Squirrel &engine) override
Register the API for this ScriptInfo.
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 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.
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.
static constexpr Owner OWNER_NONE
The tile has no ownership.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
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.