12 #include "../stdafx.h" 14 #include "../network/network.h" 15 #include "../core/random_func.hpp" 17 #include "../script/squirrel_class.hpp" 21 #include "../safeguards.h" 24 AIScannerInfo::AIScannerInfo() :
30 void AIScannerInfo::Initialize()
32 ScriptScanner::Initialize(
"AIScanner");
37 free(this->main_script);
38 this->main_script =
stredup(
"%_dummy");
45 this->info_dummy = info;
48 AIScannerInfo::~AIScannerInfo()
50 delete this->info_dummy;
65 uint num_random_ais = 0;
66 for (ScriptInfoList::const_iterator it = this->info_single_list.begin(); it != this->info_single_list.end(); it++) {
71 if (num_random_ais == 0) {
72 DEBUG(script, 0,
"No suitable AI found, loading 'dummy' AI.");
73 return this->info_dummy;
79 pos = InteractiveRandomRange(num_random_ais);
85 ScriptInfoList::const_iterator it = this->info_single_list.begin();
87 #define GetAIInfo(it) static_cast<AIInfo *>((*it).second) 88 while (!GetAIInfo(it)->UseAsRandomAI()) it++;
89 for (; pos > 0; pos--) {
91 while (!GetAIInfo(it)->UseAsRandomAI()) it++;
99 if (this->info_list.size() == 0)
return nullptr;
100 if (nameParam ==
nullptr)
return nullptr;
109 if (versionParam == -1) {
111 if (this->info_single_list.find(ai_name) != this->info_single_list.end())
return static_cast<AIInfo *>(this->info_single_list[ai_name]);
114 char *e = strrchr(ai_name,
'.');
115 if (e ==
nullptr)
return nullptr;
118 versionParam = atoi(e);
122 if (force_exact_match) {
124 char ai_name_tmp[1024];
125 seprintf(ai_name_tmp,
lastof(ai_name_tmp),
"%s.%d", ai_name, versionParam);
127 if (this->info_list.find(ai_name_tmp) != this->info_list.end())
return static_cast<AIInfo *>(this->info_list[ai_name_tmp]);
132 ScriptInfoList::iterator it = this->info_list.begin();
133 for (; it != this->info_list.end(); it++) {
145 void AIScannerLibrary::Initialize()
147 ScriptScanner::Initialize(
"AIScanner");
164 char library_name[1024];
165 seprintf(library_name,
lastof(library_name),
"%s.%d", library, version);
169 ScriptInfoList::iterator iter = this->info_list.find(library_name);
170 if (iter == this->info_list.end())
return nullptr;
172 return static_cast<AILibrary *
>((*iter).second);
bool UseAsRandomAI() const
Use this AI as a random AI.
int GetVersion() const
Get the version of the script.
bool _networking
are we in networking mode?
void SetDummyAI(class AIInfo *info)
Set the Dummy AI.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
const char * GetName() const
Get the Name of the script.
#define lastof(x)
Get the last element of an fixed size array.
bool strtolower(char *str)
Convert a given ASCII string to lowercase.
static uint32 RandomRange(uint32 limit)
Pick a random number between 0 and limit - 1, inclusive.
declarations of the class for AI scanner
Scanner to help finding scripts.
All static information from an Script like name, version, etc.
const char * GetCategory() const
Get the category this library is in.
void RegisterAPI(class Squirrel *engine) override
Register the API for this ScriptInfo.
void RegisterAPI(class Squirrel *engine) override
Register the API for this ScriptInfo.
All static information from an AI library like name, version, etc.
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
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.
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
void GetScriptName(ScriptInfo *info, char *name, const char *last) override
Get the script name how to store the script in memory.
HSQUIRRELVM GetVM()
Get the squirrel VM.
bool CanLoadFromVersion(int version) const
Check if we can start this AI.
#define DEBUG(name, level,...)
Output a line of debugging information.
All static information from an AI like name, version, etc.
class AILibrary * FindLibrary(const char *library, int version)
Find a library in the pool.
void Script_CreateDummyInfo(HSQUIRRELVM vm, const char *type, const char *dir)
Run the dummy info.nut.
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
const char * GetInstanceName() const
Get the name of the instance of the script to create.
void GetScriptName(ScriptInfo *info, char *name, const char *last) override
Get the script name how to store the script in memory.
AIInfo keeps track of all information of an AI, like Author, Description, ...
class AIInfo * SelectRandomAI() const
Select a random AI.
class Squirrel * engine
A wrapper around the squirrel vm.