OpenTTD Source
20240919-master-gdf0233f4c2
|
Go to the documentation of this file.
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"
21 #include "../safeguards.h"
24 AIScannerInfo::AIScannerInfo() :
30 void AIScannerInfo::Initialize()
32 ScriptScanner::Initialize(
"AIScanner");
46 AIScannerInfo::~AIScannerInfo()
63 if (_game_mode == GM_MENU) {
64 Debug(script, 0,
"The intro game should not use AI, loading 'dummy' AI.");
68 uint num_random_ais = 0;
74 if (num_random_ais == 0) {
75 Debug(script, 0,
"No suitable AI found, loading 'dummy' AI.");
80 uint pos = ScriptObject::GetRandomizer(
OWNER_NONE).Next(num_random_ais);
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++;
97 if (this->
info_list.empty())
return nullptr;
98 if (name.empty())
return nullptr;
107 if (force_exact_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);
116 int highest_version = -1;
120 for (
const auto &item : this->
info_list) {
132 void AIScannerLibrary::Initialize()
134 ScriptScanner::Initialize(
"AIScanner");
151 std::string library_name = fmt::format(
"{}.{}", library, version);
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);
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
class Squirrel * engine
The engine we're scanning with.
class AILibrary * FindLibrary(const std::string &library, int version)
Find a library in the pool.
HSQUIRRELVM GetVM()
Get the squirrel VM.
bool UseAsRandomAI() const
Use this AI as a random AI.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
std::string GetScriptName(ScriptInfo *info) override
Get the script name how to store the script in memory.
@ OWNER_NONE
The tile has no ownership.
AIInfo * info_dummy
The dummy AI.
All static information from an AI library like name, version, etc.
Scanner to help finding scripts.
const std::string & GetName() const
Get the Name of the script.
void Script_CreateDummyInfo(HSQUIRRELVM vm, const char *type, const char *dir)
Run the dummy info.nut.
std::string main_script
The full path of the script.
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.
ScriptInfoList info_list
The list of all script.
int GetVersion() const
Get the version of the script.
bool StrEqualsIgnoreCase(const std::string_view str1, const std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
All static information from an AI like name, version, etc.
void RegisterAPI(class Squirrel *engine) override
Register the API for this ScriptInfo.
bool CanLoadFromVersion(int version) const
Check if we can start this AI.
const std::string & GetInstanceName() const
Get the name of the instance of the script to create.
All static information from an Script like name, version, etc.
const std::string & GetCategory() const
Get the category this library is in.
ScriptInfoList info_single_list
The list of all unique script. The best script (highest version) is shown.
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.
class AIInfo * SelectRandomAI() const
Select a random AI.
void RegisterAPI(class Squirrel *engine) override
Register the API for this ScriptInfo.