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");
35 free(this->main_script);
36 this->main_script =
stredup(
"%_dummy");
43 this->info_dummy = info;
46 AIScannerInfo::~AIScannerInfo()
48 delete this->info_dummy;
63 uint num_random_ais = 0;
64 for (ScriptInfoList::const_iterator it = this->info_single_list.begin(); it != this->info_single_list.end(); it++) {
69 if (num_random_ais == 0) {
70 DEBUG(script, 0,
"No suitable AI found, loading 'dummy' AI.");
71 return this->info_dummy;
77 pos = InteractiveRandomRange(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.size() == 0)
return NULL;
98 if (nameParam == NULL)
return NULL;
107 if (versionParam == -1) {
109 if (this->info_single_list.find(ai_name) != this->info_single_list.end())
return static_cast<AIInfo *>(this->info_single_list[ai_name]);
112 char *e = strrchr(ai_name,
'.');
113 if (e == NULL)
return NULL;
116 versionParam = atoi(e);
120 if (force_exact_match) {
122 char ai_name_tmp[1024];
123 seprintf(ai_name_tmp,
lastof(ai_name_tmp),
"%s.%d", ai_name, versionParam);
125 if (this->info_list.find(ai_name_tmp) != this->info_list.end())
return static_cast<AIInfo *>(this->info_list[ai_name_tmp]);
130 ScriptInfoList::iterator it = this->info_list.begin();
131 for (; it != this->info_list.end(); it++) {
143 void AIScannerLibrary::Initialize()
145 ScriptScanner::Initialize(
"AIScanner");
162 char library_name[1024];
163 seprintf(library_name,
lastof(library_name),
"%s.%d", library, version);
167 ScriptInfoList::iterator iter = this->info_list.find(library_name);
168 if (iter == this->info_list.end())
return NULL;
170 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.
void GetScriptName(ScriptInfo *info, char *name, const char *last)
Get the script name how to store the script in memory.
All static information from an Script like name, version, etc.
const char * GetCategory() const
Get the category this library is in.
void GetScriptName(ScriptInfo *info, char *name, const char *last)
Get the script name how to store the script in memory.
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.
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.
AIInfo keeps track of all information of an AI, like Author, Description, ...
class AIInfo * SelectRandomAI() const
Select a random AI.
void RegisterAPI(class Squirrel *engine)
Register the API for this ScriptInfo.
class Squirrel * engine
A wrapper around the squirrel vm.
void RegisterAPI(class Squirrel *engine)
Register the API for this ScriptInfo.