OpenTTD Source  13.2.1
ai_scanner.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "../stdafx.h"
11 #include "../debug.h"
12 #include "../network/network.h"
13 #include "../openttd.h"
14 #include "../core/random_func.hpp"
15 
16 #include "../script/squirrel_class.hpp"
17 #include "ai_info.hpp"
18 #include "ai_scanner.hpp"
19 
20 #include "../safeguards.h"
21 
22 
23 AIScannerInfo::AIScannerInfo() :
24  ScriptScanner(),
25  info_dummy(nullptr)
26 {
27 }
28 
29 void AIScannerInfo::Initialize()
30 {
31  ScriptScanner::Initialize("AIScanner");
32 
33  ScriptAllocatorScope alloc_scope(this->engine);
34 
35  /* Create the dummy AI */
36  this->main_script = "%_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 {
59 }
60 
62 {
63  if (_game_mode == GM_MENU) {
64  Debug(script, 0, "The intro game should not use AI, loading 'dummy' AI.");
65  return this->info_dummy;
66  }
67 
68  uint num_random_ais = 0;
69  for (const auto &item : info_single_list) {
70  AIInfo *i = static_cast<AIInfo *>(item.second);
71  if (i->UseAsRandomAI()) num_random_ais++;
72  }
73 
74  if (num_random_ais == 0) {
75  Debug(script, 0, "No suitable AI found, loading 'dummy' AI.");
76  return this->info_dummy;
77  }
78 
79  /* Find a random AI */
80  uint pos;
81  if (_networking) {
82  pos = InteractiveRandomRange(num_random_ais);
83  } else {
84  pos = RandomRange(num_random_ais);
85  }
86 
87  /* Find the Nth item from the array */
88  ScriptInfoList::const_iterator it = this->info_single_list.begin();
89 
90 #define GetAIInfo(it) static_cast<AIInfo *>((*it).second)
91  while (!GetAIInfo(it)->UseAsRandomAI()) it++;
92  for (; pos > 0; pos--) {
93  it++;
94  while (!GetAIInfo(it)->UseAsRandomAI()) it++;
95  }
96  return GetAIInfo(it);
97 #undef GetAIInfo
98 }
99 
100 AIInfo *AIScannerInfo::FindInfo(const char *nameParam, int versionParam, bool force_exact_match)
101 {
102  if (this->info_list.size() == 0) return nullptr;
103  if (nameParam == nullptr) return nullptr;
104 
105  char ai_name[1024];
106  strecpy(ai_name, nameParam, lastof(ai_name));
107  strtolower(ai_name);
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  return nullptr;
113  }
114 
115  if (force_exact_match) {
116  /* Try to find a direct 'name.version' match */
117  char ai_name_tmp[1024];
118  seprintf(ai_name_tmp, lastof(ai_name_tmp), "%s.%d", ai_name, versionParam);
119  strtolower(ai_name_tmp);
120  if (this->info_list.find(ai_name_tmp) != this->info_list.end()) return static_cast<AIInfo *>(this->info_list[ai_name_tmp]);
121  return nullptr;
122  }
123 
124  AIInfo *info = nullptr;
125  int version = -1;
126 
127  /* See if there is a compatible AI which goes by that name, with the highest
128  * version which allows loading the requested version */
129  for (const auto &item : this->info_list) {
130  AIInfo *i = static_cast<AIInfo *>(item.second);
131  if (strcasecmp(ai_name, i->GetName()) == 0 && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
132  version = item.second->GetVersion();
133  info = i;
134  }
135  }
136 
137  return info;
138 }
139 
140 
141 void AIScannerLibrary::Initialize()
142 {
143  ScriptScanner::Initialize("AIScanner");
144 }
145 
146 void AIScannerLibrary::GetScriptName(ScriptInfo *info, char *name, const char *last)
147 {
148  AILibrary *library = static_cast<AILibrary *>(info);
149  seprintf(name, last, "%s.%s", library->GetCategory(), library->GetInstanceName());
150 }
151 
153 {
155 }
156 
157 AILibrary *AIScannerLibrary::FindLibrary(const char *library, int version)
158 {
159  /* Internally we store libraries as 'library.version' */
160  char library_name[1024];
161  seprintf(library_name, lastof(library_name), "%s.%d", library, version);
162  strtolower(library_name);
163 
164  /* Check if the library + version exists */
165  ScriptInfoList::iterator it = this->info_list.find(library_name);
166  if (it == this->info_list.end()) return nullptr;
167 
168  return static_cast<AILibrary *>((*it).second);
169 }
AILibrary::RegisterAPI
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition: ai_info.cpp:152
strtolower
bool strtolower(char *str)
Convert a given ASCII string to lowercase.
Definition: string.cpp:465
ScriptScanner::engine
class Squirrel * engine
The engine we're scanning with.
Definition: script_scanner.hpp:86
AIScannerLibrary::GetScriptName
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:146
AIScannerInfo::FindInfo
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:100
Squirrel::GetVM
HSQUIRRELVM GetVM()
Get the squirrel VM.
Definition: squirrel.hpp:80
AIInfo::UseAsRandomAI
bool UseAsRandomAI() const
Use this AI as a random AI.
Definition: ai_info.hpp:44
RandomRange
static uint32 RandomRange(uint32 limit)
Pick a random number between 0 and limit - 1, inclusive.
Definition: random_func.hpp:81
ai_info.hpp
Squirrel
Definition: squirrel.hpp:23
AIScannerLibrary::FindLibrary
class AILibrary * FindLibrary(const char *library, int version)
Find a library in the pool.
Definition: ai_scanner.cpp:157
ScriptAllocatorScope
Definition: squirrel.hpp:288
AIScannerInfo::info_dummy
AIInfo * info_dummy
The dummy AI.
Definition: ai_scanner.hpp:50
AILibrary
All static information from an AI library like name, version, etc.
Definition: ai_info.hpp:58
ScriptScanner
Scanner to help finding scripts.
Definition: script_scanner.hpp:20
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:58
ScriptScanner::main_script
std::string main_script
The full path of the script.
Definition: script_scanner.hpp:87
AIScannerInfo::SetDummyAI
void SetDummyAI(class AIInfo *info)
Set the Dummy AI.
Definition: ai_scanner.cpp:41
ScriptInfo::GetInstanceName
const char * GetInstanceName() const
Get the name of the instance of the script to create.
Definition: script_info.hpp:80
ScriptScanner::info_list
ScriptInfoList info_list
The list of all script.
Definition: script_scanner.hpp:90
ScriptInfo::GetVersion
int GetVersion() const
Get the version of the script.
Definition: script_info.hpp:70
Script_CreateDummyInfo
void Script_CreateDummyInfo(HSQUIRRELVM vm, const char *type, const char *dir)
Run the dummy info.nut.
Definition: script_info_dummy.cpp:28
AILibrary::GetCategory
const char * GetCategory() const
Get the category this library is in.
Definition: ai_info.hpp:76
AIInfo::RegisterAPI
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition: ai_info.cpp:37
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:554
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
ai_scanner.hpp
AIInfo
All static information from an AI like name, version, etc.
Definition: ai_info.hpp:16
AIScannerLibrary::RegisterAPI
void RegisterAPI(class Squirrel *engine) override
Register the API for this ScriptInfo.
Definition: ai_scanner.cpp:152
AIInfo::CanLoadFromVersion
bool CanLoadFromVersion(int version) const
Check if we can start this AI.
Definition: ai_info.cpp:140
AIScannerInfo::GetScriptName
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:51
ScriptInfo::GetName
const char * GetName() const
Get the Name of the script.
Definition: script_info.hpp:55
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:113
ScriptInfo
All static information from an Script like name, version, etc.
Definition: script_info.hpp:30
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:402
ScriptScanner::info_single_list
ScriptInfoList info_single_list
The list of all unique script. The best script (highest version) is shown.
Definition: script_scanner.hpp:91
AIScannerInfo::SelectRandomAI
class AIInfo * SelectRandomAI() const
Select a random AI.
Definition: ai_scanner.cpp:61
AIScannerInfo::RegisterAPI
void RegisterAPI(class Squirrel *engine) override
Register the API for this ScriptInfo.
Definition: ai_scanner.cpp:56