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

OpenTTD Source 1.11.0-RC1

ai_info.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
12 #include "../script/squirrel_class.hpp"
13 #include "ai_info.hpp"
14 #include "ai_scanner.hpp"
15 #include "../debug.h"
16 #include "../string_func.h"
17 #include "../rev.h"
18 #include <set>
19
20 #include "../safeguards.h"
21
26 static bool CheckAPIVersion(const char *api_version)
27 {
28 static const std::set<std::string> versions = { "0.7", "1.0", "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.8", "1.9", "1.10", "1.11" };
29 return versions.find(api_version) != versions.end();
30 }
31
32 #if defined(_WIN32)
33 #undef GetClassName
34 #endif /* _WIN32 */
35 template <> const char *GetClassName<AIInfo, ST_AI>() { return "AIInfo"; }
36
37 /* static */ void AIInfo::RegisterAPI(Squirrel *engine)
38 {
39 /* Create the AIInfo class, and add the RegisterAI function */
40 DefSQClass<AIInfo, ST_AI> SQAIInfo("AIInfo");
41 SQAIInfo.PreRegister(engine);
42 SQAIInfo.AddConstructor<void (AIInfo::*)(), 1>(engine, "x");
43 SQAIInfo.DefSQAdvancedMethod(engine, &AIInfo::AddSetting, "AddSetting");
44 SQAIInfo.DefSQAdvancedMethod(engine, &AIInfo::AddLabels, "AddLabels");
45 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_NONE");
46 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_RANDOM, "CONFIG_RANDOM");
47 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "CONFIG_BOOLEAN");
48 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "CONFIG_INGAME");
49 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_DEVELOPER, "CONFIG_DEVELOPER");
50
51 /* Pre 1.2 had an AI prefix */
52 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "AICONFIG_NONE");
53 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_RANDOM, "AICONFIG_RANDOM");
54 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "AICONFIG_BOOLEAN");
55 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "AICONFIG_INGAME");
56
57 SQAIInfo.PostRegister(engine);
58 engine->AddMethod("RegisterAI", &AIInfo::Constructor, 2, "tx");
59 engine->AddMethod("RegisterDummyAI", &AIInfo::DummyConstructor, 2, "tx");
60 }
61
62 /* static */ SQInteger AIInfo::Constructor(HSQUIRRELVM vm)
63 {
64 /* Get the AIInfo */
65 SQUserPointer instance = nullptr;
66 if (SQ_FAILED(sq_getinstanceup(vm, 2, &instance, 0)) || instance == nullptr) return sq_throwerror(vm, "Pass an instance of a child class of AIInfo to RegisterAI");
67 AIInfo *info = (AIInfo *)instance;
68
69 SQInteger res = ScriptInfo::Constructor(vm, info);
70 if (res != 0) return res;
71
72 ScriptConfigItem config = _start_date_config;
73 config.name = stredup(config.name);
74 config.description = stredup(config.description);
75 info->config_list.push_front(config);
76
77 if (info->engine->MethodExists(*info->SQ_instance, "MinVersionToLoad")) {
78 if (!info->engine->CallIntegerMethod(*info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_OPS)) return SQ_ERROR;
79 } else {
80 info->min_loadable_version = info->GetVersion();
81 }
82 /* When there is an UseAsRandomAI function, call it. */
83 if (info->engine->MethodExists(*info->SQ_instance, "UseAsRandomAI")) {
84 if (!info->engine->CallBoolMethod(*info->SQ_instance, "UseAsRandomAI", &info->use_as_random, MAX_GET_OPS)) return SQ_ERROR;
85 } else {
86 info->use_as_random = true;
87 }
88 /* Try to get the API version the AI is written for. */
89 if (info->engine->MethodExists(*info->SQ_instance, "GetAPIVersion")) {
90 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAPIVersion", &info->api_version, MAX_GET_OPS)) return SQ_ERROR;
91 if (!CheckAPIVersion(info->api_version)) {
92 DEBUG(script, 1, "Loading info.nut from (%s.%d): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
93 return SQ_ERROR;
94 }
95 } else {
96 info->api_version = stredup("0.7");
97 }
98
99 /* Remove the link to the real instance, else it might get deleted by RegisterAI() */
100 sq_setinstanceup(vm, 2, nullptr);
101 /* Register the AI to the base system */
102 info->GetScanner()->RegisterScript(info);
103 return 0;
104 }
105
106 /* static */ SQInteger AIInfo::DummyConstructor(HSQUIRRELVM vm)
107 {
108 /* Get the AIInfo */
109 SQUserPointer instance;
110 sq_getinstanceup(vm, 2, &instance, 0);
111 AIInfo *info = (AIInfo *)instance;
112 info->api_version = nullptr;
113
114 SQInteger res = ScriptInfo::Constructor(vm, info);
115 if (res != 0) return res;
116
117 char buf[8];
118 seprintf(buf, lastof(buf), "%d.%d", GB(_openttd_newgrf_version, 28, 4), GB(_openttd_newgrf_version, 24, 4));
119 info->api_version = stredup(buf);
120
121 /* Remove the link to the real instance, else it might get deleted by RegisterAI() */
122 sq_setinstanceup(vm, 2, nullptr);
123 /* Register the AI to the base system */
124 static_cast<AIScannerInfo *>(info->GetScanner())->SetDummyAI(info);
125 return 0;
126 }
127
128 AIInfo::AIInfo() :
129 min_loadable_version(0),
130 use_as_random(false),
131 api_version(nullptr)
132 {
133 }
134
135 AIInfo::~AIInfo()
136 {
137 free(this->api_version);
138 }
139
140 bool AIInfo::CanLoadFromVersion(int version) const
141 {
142 if (version == -1) return true;
143 return version >= this->min_loadable_version && version <= this->GetVersion();
144 }
145
146
147 AILibrary::~AILibrary()
148 {
149 free(this->category);
150 }
151
152 /* static */ void AILibrary::RegisterAPI(Squirrel *engine)
153 {
154 /* Create the AILibrary class, and add the RegisterLibrary function */
155 engine->AddClassBegin("AILibrary");
156 engine->AddClassEnd();
157 engine->AddMethod("RegisterLibrary", &AILibrary::Constructor, 2, "tx");
158 }
159
160 /* static */ SQInteger AILibrary::Constructor(HSQUIRRELVM vm)
161 {
162 /* Create a new library */
163 AILibrary *library = new AILibrary();
164
165 SQInteger res = ScriptInfo::Constructor(vm, library);
166 if (res != 0) {
167 delete library;
168 return res;
169 }
170
171 /* Cache the category */
172 if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethodStrdup(*library->SQ_instance, "GetCategory", &library->category, MAX_GET_OPS)) {
173 delete library;
174 return SQ_ERROR;
175 }
176
177 /* Register the Library to the base system */
178 library->GetScanner()->RegisterScript(library);
179
180 return 0;
181 }
AIInfo::use_as_random
bool use_as_random
Should this AI be used when the user wants a "random AI"?
Definition: ai_info.hpp:53
ScriptInfo::AddSetting
SQInteger AddSetting(HSQUIRRELVM vm)
Set a setting.
Definition: script_info.cpp:112
AILibrary::RegisterAPI
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition: ai_info.cpp:152
_start_date_config
ScriptConfigItem _start_date_config
Configuration for AI start date, every AI has this setting.
Definition: ai_config.cpp:20
ScriptInfo::engine
class Squirrel * engine
Engine used to register for Squirrel.
Definition: script_info.hpp:148
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
ScriptInfo::Constructor
static SQInteger Constructor(HSQUIRRELVM vm, ScriptInfo *info)
Process the creation of a FileInfo object.
Definition: script_info.cpp:56
AIInfo::api_version
const char * api_version
API version used by this AI.
Definition: ai_info.hpp:54
Squirrel::AddClassBegin
void AddClassBegin(const char *class_name)
Adds a class to the global scope.
Definition: squirrel.cpp:260
SCRIPTCONFIG_BOOLEAN
@ SCRIPTCONFIG_BOOLEAN
This value is a boolean (either 0 (false) or 1 (true) ).
Definition: script_config.hpp:24
ScriptInfo::version
int version
Version of the script.
Definition: script_info.hpp:161
ai_info.hpp
AIInfo::min_loadable_version
int min_loadable_version
The AI can load savegame data if the version is equal or greater than this.
Definition: ai_info.hpp:52
Squirrel
Definition: squirrel.hpp:23
Squirrel::AddClassEnd
void AddClassEnd()
Finishes adding a class to the global scope.
Definition: squirrel.cpp:284
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
MAX_GET_OPS
static const int MAX_GET_OPS
Number of operations to get the author and similar information.
Definition: script_info.hpp:25
DefSQClass::DefSQAdvancedMethod
void DefSQAdvancedMethod(Squirrel *engine, Func function_proc, const char *function_name)
This defines a method inside a class for Squirrel, which has access to the 'engine' (experts only!...
Definition: squirrel_class.hpp:43
AILibrary
All static information from an AI library like name, version, etc.
Definition: ai_info.hpp:58
SCRIPTCONFIG_NONE
@ SCRIPTCONFIG_NONE
No flags set.
Definition: script_config.hpp:22
ScriptInfo::CheckMethod
bool CheckMethod(const char *name) const
Check if a given method exists.
Definition: script_info.cpp:45
SCRIPTCONFIG_INGAME
@ SCRIPTCONFIG_INGAME
This setting can be changed while the Script is running.
Definition: script_config.hpp:25
SCRIPTCONFIG_DEVELOPER
@ SCRIPTCONFIG_DEVELOPER
This setting will only be visible when the Script development tools are active.
Definition: script_config.hpp:26
AILibrary::Constructor
static SQInteger Constructor(HSQUIRRELVM vm)
Create an AI, using this AIInfo as start-template.
Definition: ai_info.cpp:160
AIInfo::Constructor
static SQInteger Constructor(HSQUIRRELVM vm)
Create an AI, using this AIInfo as start-template.
Definition: ai_info.cpp:62
AIScannerInfo
Definition: ai_scanner.hpp:15
ScriptInfo::config_list
ScriptConfigItemList config_list
List of settings from this Script.
Definition: script_info.hpp:150
AIInfo::DummyConstructor
static SQInteger DummyConstructor(HSQUIRRELVM vm)
Create a dummy-AI.
Definition: ai_info.cpp:106
ScriptScanner::RegisterScript
void RegisterScript(class ScriptInfo *info)
Register a ScriptInfo to the scanner.
Definition: script_scanner.cpp:98
ScriptInfo::GetVersion
int GetVersion() const
Get the version of the script.
Definition: script_info.hpp:70
Squirrel::MethodExists
bool MethodExists(HSQOBJECT instance, const char *method_name)
Check if a method exists in an instance.
Definition: squirrel.cpp:292
ScriptInfo::AddLabels
SQInteger AddLabels(HSQUIRRELVM vm)
Add labels for a setting.
Definition: script_info.cpp:225
ScriptConfigItem::description
const char * description
The description of the configuration setting.
Definition: script_config.hpp:34
Squirrel::AddMethod
void AddMethod(const char *method_name, SQFUNCTION proc, uint nparam=0, const char *params=nullptr, void *userdata=nullptr, int size=0)
Adds a function to the stack.
Definition: squirrel.cpp:225
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:442
stredup
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:137
SCRIPTCONFIG_RANDOM
@ SCRIPTCONFIG_RANDOM
When randomizing the Script, pick any value between min_value and max_value when on custom difficulty...
Definition: script_config.hpp:23
ai_scanner.hpp
AIInfo
All static information from an AI like name, version, etc.
Definition: ai_info.hpp:16
ScriptInfo::SQ_instance
HSQOBJECT * SQ_instance
The Squirrel instance created for this info.
Definition: script_info.hpp:149
DefSQClass
The template to define classes in Squirrel.
Definition: squirrel_class.hpp:20
AIInfo::CanLoadFromVersion
bool CanLoadFromVersion(int version) const
Check if we can start this AI.
Definition: ai_info.cpp:140
ScriptInfo::GetName
const char * GetName() const
Get the Name of the script.
Definition: script_info.hpp:55
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:454
ScriptConfigItem
Info about a single Script setting.
Definition: script_config.hpp:32
ScriptConfigItem::name
const char * name
The name of the configuration setting.
Definition: script_config.hpp:33
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383
ScriptInfo::GetScanner
virtual class ScriptScanner * GetScanner()
Get the scanner which has found this ScriptInfo.
Definition: script_info.hpp:110
AILibrary::category
const char * category
The category this library is in.
Definition: ai_info.hpp:79
CheckAPIVersion
static bool CheckAPIVersion(const char *api_version)
Check if the API version provided by the AI is supported.
Definition: ai_info.cpp:26