OpenTTD Source: src/ai/ai_scanner.cpp Source File

OpenTTD Source 12.0-beta1

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 "../core/random_func.hpp"
14
15 #include "../script/squirrel_class.hpp"
16 #include "ai_info.hpp"
17 #include "ai_scanner.hpp"
18
19 #include "../safeguards.h"
20
21
22 AIScannerInfo::AIScannerInfo() :
23 ScriptScanner(),
24 info_dummy(nullptr)
25 {
26 }
27
28 void AIScannerInfo::Initialize()
29 {
30 ScriptScanner::Initialize("AIScanner");
31
32 ScriptAllocatorScope alloc_scope(this->engine);
33
34 /* Create the dummy AI */
35 this->main_script = "%_dummy";
36 extern void Script_CreateDummyInfo(HSQUIRRELVM vm, const char *type, const char *dir);
37 Script_CreateDummyInfo(this->engine->GetVM(), "AI", "ai");
38 }
39
40 void AIScannerInfo::SetDummyAI(class AIInfo *info)
41 {
42 this->info_dummy = info;
43 }
44
45 AIScannerInfo::~AIScannerInfo()
46 {
47 delete this->info_dummy;
48 }
49
50 void AIScannerInfo::GetScriptName(ScriptInfo *info, char *name, const char *last)
51 {
52 seprintf(name, last, "%s", info->GetName());
53 }
54
55 void AIScannerInfo::RegisterAPI(class Squirrel *engine)
56 {
57 AIInfo::RegisterAPI(engine);
58 }
59
60 AIInfo *AIScannerInfo::SelectRandomAI() const
61 {
62 uint num_random_ais = 0;
63 for (const auto &item : info_single_list) {
64 AIInfo *i = static_cast<AIInfo *>(item.second);
65 if (i->UseAsRandomAI()) num_random_ais++;
66 }
67
68 if (num_random_ais == 0) {
69 Debug(script, 0, "No suitable AI found, loading 'dummy' AI.");
70 return this->info_dummy;
71 }
72
73 /* Find a random AI */
74 uint pos;
75 if (_networking) {
76 pos = InteractiveRandomRange(num_random_ais);
77 } else {
78 pos = RandomRange(num_random_ais);
79 }
80
81 /* Find the Nth item from the array */
82 ScriptInfoList::const_iterator it = this->info_single_list.begin();
83
84 #define GetAIInfo(it) static_cast<AIInfo *>((*it).second)
85 while (!GetAIInfo(it)->UseAsRandomAI()) it++;
86 for (; pos > 0; pos--) {
87 it++;
88 while (!GetAIInfo(it)->UseAsRandomAI()) it++;
89 }
90 return GetAIInfo(it);
91 #undef GetAIInfo
92 }
93
94 AIInfo *AIScannerInfo::FindInfo(const char *nameParam, int versionParam, bool force_exact_match)
95 {
96 if (this->info_list.size() == 0) return nullptr;
97 if (nameParam == nullptr) return nullptr;
98
99 char ai_name[1024];
100 strecpy(ai_name, nameParam, lastof(ai_name));
101 strtolower(ai_name);
102
103 if (versionParam == -1) {
104 /* We want to load the latest version of this AI; so find it */
105 if (this->info_single_list.find(ai_name) != this->info_single_list.end()) return static_cast<AIInfo *>(this->info_single_list[ai_name]);
106 return nullptr;
107 }
108
109 if (force_exact_match) {
110 /* Try to find a direct 'name.version' match */
111 char ai_name_tmp[1024];
112 seprintf(ai_name_tmp, lastof(ai_name_tmp), "%s.%d", ai_name, versionParam);
113 strtolower(ai_name_tmp);
114 if (this->info_list.find(ai_name_tmp) != this->info_list.end()) return static_cast<AIInfo *>(this->info_list[ai_name_tmp]);
115 return nullptr;
116 }
117
118 AIInfo *info = nullptr;
119 int version = -1;
120
121 /* See if there is a compatible AI which goes by that name, with the highest
122 * version which allows loading the requested version */
123 for (const auto &item : this->info_list) {
124 AIInfo *i = static_cast<AIInfo *>(item.second);
125 if (strcasecmp(ai_name, i->GetName()) == 0 && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
126 version = item.second->GetVersion();
127 info = i;
128 }
129 }
130
131 return info;
132 }
133
134
135 void AIScannerLibrary::Initialize()
136 {
137 ScriptScanner::Initialize("AIScanner");
138 }
139
140 void AIScannerLibrary::GetScriptName(ScriptInfo *info, char *name, const char *last)
141 {
142 AILibrary *library = static_cast<AILibrary *>(info);
143 seprintf(name, last, "%s.%s", library->GetCategory(), library->GetInstanceName());
144 }
145
146 void AIScannerLibrary::RegisterAPI(class Squirrel *engine)
147 {
148 AILibrary::RegisterAPI(engine);
149 }
150
151 AILibrary *AIScannerLibrary::FindLibrary(const char *library, int version)
152 {
153 /* Internally we store libraries as 'library.version' */
154 char library_name[1024];
155 seprintf(library_name, lastof(library_name), "%s.%d", library, version);
156 strtolower(library_name);
157
158 /* Check if the library + version exists */
159 ScriptInfoList::iterator it = this->info_list.find(library_name);
160 if (it == this->info_list.end()) return nullptr;
161
162 return static_cast<AILibrary *>((*it).second);
163 }
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:447
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:140
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:94
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:151
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:56
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:40
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:535
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:146
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:50
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:112
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:394
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:60
AIScannerInfo::RegisterAPI
void RegisterAPI(class Squirrel *engine) override
Register the API for this ScriptInfo.
Definition: ai_scanner.cpp:55