OpenTTD Source
1.11.0-beta2
|
Go to the documentation of this file.
10 #include "../stdafx.h"
12 #include "../network/network.h"
13 #include "../core/random_func.hpp"
15 #include "../script/squirrel_class.hpp"
19 #include "../safeguards.h"
22 AIScannerInfo::AIScannerInfo() :
28 void AIScannerInfo::Initialize()
30 ScriptScanner::Initialize(
"AIScanner");
45 AIScannerInfo::~AIScannerInfo()
62 uint num_random_ais = 0;
68 if (num_random_ais == 0) {
69 DEBUG(script, 0,
"No suitable AI found, loading 'dummy' AI.");
76 pos = InteractiveRandomRange(num_random_ais);
82 ScriptInfoList::const_iterator it = this->info_single_list.begin();
84 #define GetAIInfo(it) static_cast<AIInfo *>((*it).second)
85 while (!GetAIInfo(it)->UseAsRandomAI()) it++;
86 for (; pos > 0; pos--) {
88 while (!GetAIInfo(it)->UseAsRandomAI()) it++;
96 if (this->
info_list.size() == 0)
return nullptr;
97 if (nameParam ==
nullptr)
return nullptr;
103 if (versionParam == -1) {
109 if (force_exact_match) {
111 char ai_name_tmp[1024];
112 seprintf(ai_name_tmp,
lastof(ai_name_tmp),
"%s.%d", ai_name, versionParam);
114 if (this->
info_list.find(ai_name_tmp) != this->info_list.end())
return static_cast<AIInfo *
>(this->
info_list[ai_name_tmp]);
123 for (
const auto &item : this->
info_list) {
135 void AIScannerLibrary::Initialize()
137 ScriptScanner::Initialize(
"AIScanner");
154 char library_name[1024];
155 seprintf(library_name,
lastof(library_name),
"%s.%d", library, version);
159 ScriptInfoList::iterator it = this->
info_list.find(library_name);
160 if (it == this->
info_list.end())
return nullptr;
162 return static_cast<AILibrary *
>((*it).second);
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
bool strtolower(char *str)
Convert a given ASCII string to lowercase.
class Squirrel * engine
The engine we're scanning with.
void GetScriptName(ScriptInfo *info, char *name, const char *last) override
Get the script name how to store the script in memory.
class AIInfo * FindInfo(const char *nameParam, int versionParam, bool force_exact_match)
Check if we have an AI by name and version available in our list.
HSQUIRRELVM GetVM()
Get the squirrel VM.
bool UseAsRandomAI() const
Use this AI as a random AI.
static uint32 RandomRange(uint32 limit)
Pick a random number between 0 and limit - 1, inclusive.
class AILibrary * FindLibrary(const char *library, int version)
Find a library in the pool.
#define DEBUG(name, level,...)
Output a line of debugging information.
AIInfo * info_dummy
The dummy AI.
All static information from an AI library like name, version, etc.
Scanner to help finding scripts.
bool _networking
are we in networking mode?
std::string main_script
The full path of the script.
void SetDummyAI(class AIInfo *info)
Set the Dummy AI.
const char * GetInstanceName() const
Get the name of the instance of the script to create.
ScriptInfoList info_list
The list of all script.
int GetVersion() const
Get the version of the script.
void Script_CreateDummyInfo(HSQUIRRELVM vm, const char *type, const char *dir)
Run the dummy info.nut.
const char * GetCategory() const
Get the category this library is in.
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
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.
void GetScriptName(ScriptInfo *info, char *name, const char *last) override
Get the script name how to store the script in memory.
const char * GetName() const
Get the Name of the script.
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
All static information from an Script like name, version, etc.
#define lastof(x)
Get the last element of an fixed size array.
ScriptInfoList info_single_list
The list of all unique script. The best script (highest version) is shown.
class AIInfo * SelectRandomAI() const
Select a random AI.
void RegisterAPI(class Squirrel *engine) override
Register the API for this ScriptInfo.