OpenTTD
ai_scanner.cpp
Go to the documentation of this file.
1 /* $Id: ai_scanner.cpp 27893 2017-08-13 18:38:42Z frosch $ */
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(NULL)
27 {
28 }
29 
30 void AIScannerInfo::Initialize()
31 {
32  ScriptScanner::Initialize("AIScanner");
33 
34  /* Create the dummy AI */
35  free(this->main_script);
36  this->main_script = stredup("%_dummy");
37  extern void Script_CreateDummyInfo(HSQUIRRELVM vm, const char *type, const char *dir);
38  Script_CreateDummyInfo(this->engine->GetVM(), "AI", "ai");
39 }
40 
42 {
43  this->info_dummy = info;
44 }
45 
46 AIScannerInfo::~AIScannerInfo()
47 {
48  delete this->info_dummy;
49 }
50 
51 void AIScannerInfo::GetScriptName(ScriptInfo *info, char *name, const char *last)
52 {
53  seprintf(name, last, "%s", info->GetName());
54 }
55 
57 {
58  AIInfo::RegisterAPI(engine);
59 }
60 
62 {
63  uint num_random_ais = 0;
64  for (ScriptInfoList::const_iterator it = this->info_single_list.begin(); it != this->info_single_list.end(); it++) {
65  AIInfo *i = static_cast<AIInfo *>((*it).second);
66  if (i->UseAsRandomAI()) num_random_ais++;
67  }
68 
69  if (num_random_ais == 0) {
70  DEBUG(script, 0, "No suitable AI found, loading 'dummy' AI.");
71  return this->info_dummy;
72  }
73 
74  /* Find a random AI */
75  uint pos;
76  if (_networking) {
77  pos = InteractiveRandomRange(num_random_ais);
78  } else {
79  pos = RandomRange(num_random_ais);
80  }
81 
82  /* Find the Nth item from the array */
83  ScriptInfoList::const_iterator it = this->info_single_list.begin();
84 
85 #define GetAIInfo(it) static_cast<AIInfo *>((*it).second)
86  while (!GetAIInfo(it)->UseAsRandomAI()) it++;
87  for (; pos > 0; pos--) {
88  it++;
89  while (!GetAIInfo(it)->UseAsRandomAI()) it++;
90  }
91  return GetAIInfo(it);
92 #undef GetAIInfo
93 }
94 
95 AIInfo *AIScannerInfo::FindInfo(const char *nameParam, int versionParam, bool force_exact_match)
96 {
97  if (this->info_list.size() == 0) return NULL;
98  if (nameParam == NULL) return NULL;
99 
100  char ai_name[1024];
101  strecpy(ai_name, nameParam, lastof(ai_name));
102  strtolower(ai_name);
103 
104  AIInfo *info = NULL;
105  int version = -1;
106 
107  if (versionParam == -1) {
108  /* We want to load the latest version of this AI; so find it */
109  if (this->info_single_list.find(ai_name) != this->info_single_list.end()) return static_cast<AIInfo *>(this->info_single_list[ai_name]);
110 
111  /* If we didn't find a match AI, maybe the user included a version */
112  char *e = strrchr(ai_name, '.');
113  if (e == NULL) return NULL;
114  *e = '\0';
115  e++;
116  versionParam = atoi(e);
117  /* Continue, like we were calling this function with a version. */
118  }
119 
120  if (force_exact_match) {
121  /* Try to find a direct 'name.version' match */
122  char ai_name_tmp[1024];
123  seprintf(ai_name_tmp, lastof(ai_name_tmp), "%s.%d", ai_name, versionParam);
124  strtolower(ai_name_tmp);
125  if (this->info_list.find(ai_name_tmp) != this->info_list.end()) return static_cast<AIInfo *>(this->info_list[ai_name_tmp]);
126  }
127 
128  /* See if there is a compatible AI which goes by that name, with the highest
129  * version which allows loading the requested version */
130  ScriptInfoList::iterator it = this->info_list.begin();
131  for (; it != this->info_list.end(); it++) {
132  AIInfo *i = static_cast<AIInfo *>((*it).second);
133  if (strcasecmp(ai_name, i->GetName()) == 0 && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
134  version = (*it).second->GetVersion();
135  info = i;
136  }
137  }
138 
139  return info;
140 }
141 
142 
143 void AIScannerLibrary::Initialize()
144 {
145  ScriptScanner::Initialize("AIScanner");
146 }
147 
148 void AIScannerLibrary::GetScriptName(ScriptInfo *info, char *name, const char *last)
149 {
150  AILibrary *library = static_cast<AILibrary *>(info);
151  seprintf(name, last, "%s.%s", library->GetCategory(), library->GetInstanceName());
152 }
153 
155 {
156  AILibrary::RegisterAPI(engine);
157 }
158 
159 AILibrary *AIScannerLibrary::FindLibrary(const char *library, int version)
160 {
161  /* Internally we store libraries as 'library.version' */
162  char library_name[1024];
163  seprintf(library_name, lastof(library_name), "%s.%d", library, version);
164  strtolower(library_name);
165 
166  /* Check if the library + version exists */
167  ScriptInfoList::iterator iter = this->info_list.find(library_name);
168  if (iter == this->info_list.end()) return NULL;
169 
170  return static_cast<AILibrary *>((*iter).second);
171 }
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:56
void SetDummyAI(class AIInfo *info)
Set the Dummy AI.
Definition: ai_scanner.cpp:41
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:398
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:320
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.
void GetScriptName(ScriptInfo *info, char *name, const char *last)
Get the script name how to store the script in memory.
Definition: ai_scanner.cpp:148
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 GetScriptName(ScriptInfo *info, char *name, const char *last)
Get the script name how to store the script in memory.
Definition: ai_scanner.cpp:51
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:126
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:95
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition: ai_info.cpp:40
HSQUIRRELVM GetVM()
Get the squirrel VM.
Definition: squirrel.hpp:77
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:39
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:159
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:114
const char * GetInstanceName() const
Get the name of the instance of the script to create.
Definition: script_info.hpp:84
AIInfo keeps track of all information of an AI, like Author, Description, ...
class AIInfo * SelectRandomAI() const
Select a random AI.
Definition: ai_scanner.cpp:61
void RegisterAPI(class Squirrel *engine)
Register the API for this ScriptInfo.
Definition: ai_scanner.cpp:154
class Squirrel * engine
A wrapper around the squirrel vm.
void RegisterAPI(class Squirrel *engine)
Register the API for this ScriptInfo.
Definition: ai_scanner.cpp:56