OpenTTD
ai_scanner.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #include "../stdafx.h"
13 #include "../debug.h"
14 #include "../network/network.h"
15 #include "../core/random_func.hpp"
16 
17 #include "../script/squirrel_class.hpp"
18 #include "ai_info.hpp"
19 #include "ai_scanner.hpp"
20 
21 #include "../safeguards.h"
22 
23 
24 AIScannerInfo::AIScannerInfo() :
25  ScriptScanner(),
26  info_dummy(nullptr)
27 {
28 }
29 
30 void AIScannerInfo::Initialize()
31 {
32  ScriptScanner::Initialize("AIScanner");
33 
34  ScriptAllocatorScope alloc_scope(this->engine);
35 
36  /* Create the dummy AI */
37  free(this->main_script);
38  this->main_script = stredup("%_dummy");
39  extern void Script_CreateDummyInfo(HSQUIRRELVM vm, const char *type, const char *dir);
40  Script_CreateDummyInfo(this->engine->GetVM(), "AI", "ai");
41 }
42 
44 {
45  this->info_dummy = info;
46 }
47 
48 AIScannerInfo::~AIScannerInfo()
49 {
50  delete this->info_dummy;
51 }
52 
53 void AIScannerInfo::GetScriptName(ScriptInfo *info, char *name, const char *last)
54 {
55  seprintf(name, last, "%s", info->GetName());
56 }
57 
59 {
60  AIInfo::RegisterAPI(engine);
61 }
62 
64 {
65  uint num_random_ais = 0;
66  for (ScriptInfoList::const_iterator it = this->info_single_list.begin(); it != this->info_single_list.end(); it++) {
67  AIInfo *i = static_cast<AIInfo *>((*it).second);
68  if (i->UseAsRandomAI()) num_random_ais++;
69  }
70 
71  if (num_random_ais == 0) {
72  DEBUG(script, 0, "No suitable AI found, loading 'dummy' AI.");
73  return this->info_dummy;
74  }
75 
76  /* Find a random AI */
77  uint pos;
78  if (_networking) {
79  pos = InteractiveRandomRange(num_random_ais);
80  } else {
81  pos = RandomRange(num_random_ais);
82  }
83 
84  /* Find the Nth item from the array */
85  ScriptInfoList::const_iterator it = this->info_single_list.begin();
86 
87 #define GetAIInfo(it) static_cast<AIInfo *>((*it).second)
88  while (!GetAIInfo(it)->UseAsRandomAI()) it++;
89  for (; pos > 0; pos--) {
90  it++;
91  while (!GetAIInfo(it)->UseAsRandomAI()) it++;
92  }
93  return GetAIInfo(it);
94 #undef GetAIInfo
95 }
96 
97 AIInfo *AIScannerInfo::FindInfo(const char *nameParam, int versionParam, bool force_exact_match)
98 {
99  if (this->info_list.size() == 0) return nullptr;
100  if (nameParam == nullptr) return nullptr;
101 
102  char ai_name[1024];
103  strecpy(ai_name, nameParam, lastof(ai_name));
104  strtolower(ai_name);
105 
106  AIInfo *info = nullptr;
107  int version = -1;
108 
109  if (versionParam == -1) {
110  /* We want to load the latest version of this AI; so find it */
111  if (this->info_single_list.find(ai_name) != this->info_single_list.end()) return static_cast<AIInfo *>(this->info_single_list[ai_name]);
112 
113  /* If we didn't find a match AI, maybe the user included a version */
114  char *e = strrchr(ai_name, '.');
115  if (e == nullptr) return nullptr;
116  *e = '\0';
117  e++;
118  versionParam = atoi(e);
119  /* Continue, like we were calling this function with a version. */
120  }
121 
122  if (force_exact_match) {
123  /* Try to find a direct 'name.version' match */
124  char ai_name_tmp[1024];
125  seprintf(ai_name_tmp, lastof(ai_name_tmp), "%s.%d", ai_name, versionParam);
126  strtolower(ai_name_tmp);
127  if (this->info_list.find(ai_name_tmp) != this->info_list.end()) return static_cast<AIInfo *>(this->info_list[ai_name_tmp]);
128  }
129 
130  /* See if there is a compatible AI which goes by that name, with the highest
131  * version which allows loading the requested version */
132  ScriptInfoList::iterator it = this->info_list.begin();
133  for (; it != this->info_list.end(); it++) {
134  AIInfo *i = static_cast<AIInfo *>((*it).second);
135  if (strcasecmp(ai_name, i->GetName()) == 0 && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
136  version = (*it).second->GetVersion();
137  info = i;
138  }
139  }
140 
141  return info;
142 }
143 
144 
145 void AIScannerLibrary::Initialize()
146 {
147  ScriptScanner::Initialize("AIScanner");
148 }
149 
150 void AIScannerLibrary::GetScriptName(ScriptInfo *info, char *name, const char *last)
151 {
152  AILibrary *library = static_cast<AILibrary *>(info);
153  seprintf(name, last, "%s.%s", library->GetCategory(), library->GetInstanceName());
154 }
155 
157 {
158  AILibrary::RegisterAPI(engine);
159 }
160 
161 AILibrary *AIScannerLibrary::FindLibrary(const char *library, int version)
162 {
163  /* Internally we store libraries as 'library.version' */
164  char library_name[1024];
165  seprintf(library_name, lastof(library_name), "%s.%d", library, version);
166  strtolower(library_name);
167 
168  /* Check if the library + version exists */
169  ScriptInfoList::iterator iter = this->info_list.find(library_name);
170  if (iter == this->info_list.end()) return nullptr;
171 
172  return static_cast<AILibrary *>((*iter).second);
173 }
bool UseAsRandomAI() const
Use this AI as a random AI.
Definition: ai_info.hpp:46
int GetVersion() const
Get the version of the script.
Definition: script_info.hpp:74
bool _networking
are we in networking mode?
Definition: network.cpp:54
void SetDummyAI(class AIInfo *info)
Set the Dummy AI.
Definition: ai_scanner.cpp:43
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:409
const char * GetName() const
Get the Name of the script.
Definition: script_info.hpp:59
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
bool strtolower(char *str)
Convert a given ASCII string to lowercase.
Definition: string.cpp:332
static uint32 RandomRange(uint32 limit)
Pick a random number between 0 and limit - 1, inclusive.
Definition: random_func.hpp:83
declarations of the class for AI scanner
Scanner to help finding scripts.
All static information from an Script like name, version, etc.
Definition: script_info.hpp:32
const char * GetCategory() const
Get the category this library is in.
Definition: ai_info.hpp:78
void RegisterAPI(class Squirrel *engine) override
Register the API for this ScriptInfo.
Definition: ai_scanner.cpp:58
void RegisterAPI(class Squirrel *engine) override
Register the API for this ScriptInfo.
Definition: ai_scanner.cpp:156
All static information from an AI library like name, version, etc.
Definition: ai_info.hpp:60
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:138
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.
Definition: ai_scanner.cpp:97
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition: ai_info.cpp:40
void GetScriptName(ScriptInfo *info, char *name, const char *last) override
Get the script name how to store the script in memory.
Definition: ai_scanner.cpp:150
HSQUIRRELVM GetVM()
Get the squirrel VM.
Definition: squirrel.hpp:82
bool CanLoadFromVersion(int version) const
Check if we can start this AI.
Definition: ai_info.cpp:143
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:37
All static information from an AI like name, version, etc.
Definition: ai_info.hpp:18
class AILibrary * FindLibrary(const char *library, int version)
Find a library in the pool.
Definition: ai_scanner.cpp:161
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.
Definition: ai_info.cpp:155
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:131
const char * GetInstanceName() const
Get the name of the instance of the script to create.
Definition: script_info.hpp:84
void GetScriptName(ScriptInfo *info, char *name, const char *last) override
Get the script name how to store the script in memory.
Definition: ai_scanner.cpp:53
AIInfo keeps track of all information of an AI, like Author, Description, ...
class AIInfo * SelectRandomAI() const
Select a random AI.
Definition: ai_scanner.cpp:63
class Squirrel * engine
A wrapper around the squirrel vm.