OpenTTD Source 15.0-beta1
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
19#include "../safeguards.h"
20
25static bool CheckAPIVersion(const std::string &api_version)
26{
27 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", "12", "13", "14", "15" };
28 return versions.find(api_version) != versions.end();
29}
30
31#if defined(_WIN32)
32#undef GetClassName
33#endif /* _WIN32 */
34template <> const char *GetClassName<AIInfo, ScriptType::AI>() { return "AIInfo"; }
35
36/* static */ void AIInfo::RegisterAPI(Squirrel *engine)
37{
38 /* Create the AIInfo class, and add the RegisterAI function */
39 DefSQClass<AIInfo, ScriptType::AI> SQAIInfo("AIInfo");
40 SQAIInfo.PreRegister(engine);
41 SQAIInfo.AddConstructor<void (AIInfo::*)(), 1>(engine, "x");
42 SQAIInfo.DefSQAdvancedMethod(engine, &AIInfo::AddSetting, "AddSetting");
43 SQAIInfo.DefSQAdvancedMethod(engine, &AIInfo::AddLabels, "AddLabels");
44 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_NONE");
45 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_RANDOM"); // Deprecated, mapped to NONE.
46 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "CONFIG_BOOLEAN");
47 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "CONFIG_INGAME");
48 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_DEVELOPER, "CONFIG_DEVELOPER");
49
50 /* Pre 1.2 had an AI prefix */
51 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "AICONFIG_NONE");
52 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "AICONFIG_RANDOM"); // Deprecated, mapped to NONE.
53 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "AICONFIG_BOOLEAN");
54 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "AICONFIG_INGAME");
55
56 SQAIInfo.PostRegister(engine);
57 engine->AddMethod("RegisterAI", &AIInfo::Constructor, 2, "tx");
58 engine->AddMethod("RegisterDummyAI", &AIInfo::DummyConstructor, 2, "tx");
59}
60
61/* static */ SQInteger AIInfo::Constructor(HSQUIRRELVM vm)
62{
63 /* Get the AIInfo */
64 SQUserPointer instance = nullptr;
65 if (SQ_FAILED(sq_getinstanceup(vm, 2, &instance, nullptr)) || instance == nullptr) return sq_throwerror(vm, "Pass an instance of a child class of AIInfo to RegisterAI");
66 AIInfo *info = (AIInfo *)instance;
67
68 SQInteger res = ScriptInfo::Constructor(vm, info);
69 if (res != 0) return res;
70
71 if (info->engine->MethodExists(info->SQ_instance, "MinVersionToLoad")) {
72 if (!info->engine->CallIntegerMethod(info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_OPS)) return SQ_ERROR;
73 } else {
74 info->min_loadable_version = info->GetVersion();
75 }
76 /* When there is an UseAsRandomAI function, call it. */
77 if (info->engine->MethodExists(info->SQ_instance, "UseAsRandomAI")) {
78 if (!info->engine->CallBoolMethod(info->SQ_instance, "UseAsRandomAI", &info->use_as_random, MAX_GET_OPS)) return SQ_ERROR;
79 } else {
80 info->use_as_random = true;
81 }
82 /* Try to get the API version the AI is written for. */
83 if (info->engine->MethodExists(info->SQ_instance, "GetAPIVersion")) {
84 if (!info->engine->CallStringMethod(info->SQ_instance, "GetAPIVersion", &info->api_version, MAX_GET_OPS)) return SQ_ERROR;
85 if (!CheckAPIVersion(info->api_version)) {
86 Debug(script, 1, "Loading info.nut from ({}.{}): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
87 return SQ_ERROR;
88 }
89 } else {
90 info->api_version = "0.7";
91 }
92
93 /* Remove the link to the real instance, else it might get deleted by RegisterAI() */
94 sq_setinstanceup(vm, 2, nullptr);
95 /* Register the AI to the base system */
96 info->GetScanner()->RegisterScript(info);
97 return 0;
98}
99
100/* static */ SQInteger AIInfo::DummyConstructor(HSQUIRRELVM vm)
101{
102 /* Get the AIInfo */
103 SQUserPointer instance;
104 sq_getinstanceup(vm, 2, &instance, nullptr);
105 AIInfo *info = (AIInfo *)instance;
106 info->api_version = fmt::format("{}.{}", GB(_openttd_newgrf_version, 28, 4), GB(_openttd_newgrf_version, 24, 4));
107
108 SQInteger res = ScriptInfo::Constructor(vm, info);
109 if (res != 0) return res;
110
111 /* Remove the link to the real instance, else it might get deleted by RegisterAI() */
112 sq_setinstanceup(vm, 2, nullptr);
113 /* Register the AI to the base system */
114 static_cast<AIScannerInfo *>(info->GetScanner())->SetDummyAI(info);
115 return 0;
116}
117
118AIInfo::AIInfo() :
119 min_loadable_version(0),
120 use_as_random(false)
121{
122}
123
124bool AIInfo::CanLoadFromVersion(int version) const
125{
126 if (version == -1) return true;
127 return version >= this->min_loadable_version && version <= this->GetVersion();
128}
129
130
131/* static */ void AILibrary::RegisterAPI(Squirrel *engine)
132{
133 /* Create the AILibrary class, and add the RegisterLibrary function */
134 engine->AddClassBegin("AILibrary");
136 engine->AddMethod("RegisterLibrary", &AILibrary::Constructor, 2, "tx");
137}
138
139/* static */ SQInteger AILibrary::Constructor(HSQUIRRELVM vm)
140{
141 /* Create a new library */
142 AILibrary *library = new AILibrary();
143
144 SQInteger res = ScriptInfo::Constructor(vm, library);
145 if (res != 0) {
146 delete library;
147 return res;
148 }
149
150 /* Cache the category */
151 if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethod(library->SQ_instance, "GetCategory", &library->category, MAX_GET_OPS)) {
152 delete library;
153 return SQ_ERROR;
154 }
155
156 /* Register the Library to the base system */
157 library->GetScanner()->RegisterScript(library);
158
159 return 0;
160}
static bool CheckAPIVersion(const std::string &api_version)
Check if the API version provided by the AI is supported.
Definition ai_info.cpp:25
AIInfo keeps track of all information of an AI, like Author, Description, ...
declarations of the class for AI scanner
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
All static information from an AI like name, version, etc.
Definition ai_info.hpp:16
bool CanLoadFromVersion(int version) const
Check if we can start this AI.
Definition ai_info.cpp:124
bool use_as_random
Should this AI be used when the user wants a "random AI"?
Definition ai_info.hpp:52
int min_loadable_version
The AI can load savegame data if the version is equal or greater than this.
Definition ai_info.hpp:51
std::string api_version
API version used by this AI.
Definition ai_info.hpp:53
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition ai_info.cpp:36
static SQInteger Constructor(HSQUIRRELVM vm)
Create an AI, using this AIInfo as start-template.
Definition ai_info.cpp:61
static SQInteger DummyConstructor(HSQUIRRELVM vm)
Create a dummy-AI.
Definition ai_info.cpp:100
All static information from an AI library like name, version, etc.
Definition ai_info.hpp:57
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition ai_info.cpp:131
std::string category
The category this library is in.
Definition ai_info.hpp:77
static SQInteger Constructor(HSQUIRRELVM vm)
Create an AI, using this AIInfo as start-template.
Definition ai_info.cpp:139
The template to define classes in Squirrel.
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!...
int version
Version of the script.
static SQInteger Constructor(HSQUIRRELVM vm, ScriptInfo *info)
Process the creation of a FileInfo object.
const std::string & GetName() const
Get the Name of the script.
bool CheckMethod(const char *name) const
Check if a given method exists.
class Squirrel * engine
Engine used to register for Squirrel.
SQInteger AddSetting(HSQUIRRELVM vm)
Set a setting.
SQInteger AddLabels(HSQUIRRELVM vm)
Add labels for a setting.
int GetVersion() const
Get the version of the script.
HSQOBJECT SQ_instance
The Squirrel instance created for this info.
virtual class ScriptScanner * GetScanner()
Get the scanner which has found this ScriptInfo.
void RegisterScript(class ScriptInfo *info)
Register a ScriptInfo to the scanner.
void AddClassEnd()
Finishes adding a class to the global scope.
Definition squirrel.cpp:337
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:278
void AddClassBegin(const char *class_name)
Adds a class to the global scope.
Definition squirrel.cpp:313
bool MethodExists(HSQOBJECT instance, const char *method_name)
Check if a method exists in an instance.
Definition squirrel.cpp:345
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition debug.h:37
@ SCRIPTCONFIG_INGAME
This setting can be changed while the Script is running.
@ SCRIPTCONFIG_BOOLEAN
This value is a boolean (either 0 (false) or 1 (true) ).
@ SCRIPTCONFIG_NONE
No flags set.
@ SCRIPTCONFIG_DEVELOPER
This setting will only be visible when the Script development tools are active.
static const int MAX_GET_OPS
Number of operations to get the author and similar information.