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() :
31void AIScannerInfo::Initialize()
33 ScriptScanner::Initialize(
"AIScanner");
47AIScannerInfo::~AIScannerInfo()
64 if (_game_mode == GM_MENU) {
65 Debug(script, 0,
"The intro game should not use AI, loading 'dummy' AI.");
70 auto random_ais =
info_single_list | std::views::filter([](
const auto &item) {
return static_cast<AIInfo *
>(item.second)->UseAsRandomAI(); });
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.");
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));
83 return static_cast<AIInfo *
>(it->second);
88 if (this->
info_list.empty())
return nullptr;
89 if (name.empty())
return nullptr;
98 if (force_exact_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);
107 int highest_version = -1;
111 for (
const auto &item : this->
info_list) {
123void AIScannerLibrary::Initialize()
125 ScriptScanner::Initialize(
"AIScanner");
142 std::string library_name = fmt::format(
"{}.{}", library, version);
145 ScriptInfoList::iterator it = this->
info_list.find(library_name);
146 if (it == this->
info_list.end())
return nullptr;
148 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 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.
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.