Fix #8316: Make sort industries by production and transported with a cargo filter...
[openttd-github.git] / src / ai / ai_info.cpp
blob10d34ea58a28b4439f2717f4cb25f69a486342f9
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 */
8 /** @file ai_info.cpp Implementation of AIInfo and AILibrary */
10 #include "../stdafx.h"
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>
20 #include "../safeguards.h"
22 /**
23 * Check if the API version provided by the AI is supported.
24 * @param api_version The API version as provided by the AI.
26 static bool CheckAPIVersion(const char *api_version)
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", "1.12" };
29 return versions.find(api_version) != versions.end();
32 #if defined(_WIN32)
33 #undef GetClassName
34 #endif /* _WIN32 */
35 template <> const char *GetClassName<AIInfo, ST_AI>() { return "AIInfo"; }
37 /* static */ void AIInfo::RegisterAPI(Squirrel *engine)
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");
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");
57 SQAIInfo.PostRegister(engine);
58 engine->AddMethod("RegisterAI", &AIInfo::Constructor, 2, "tx");
59 engine->AddMethod("RegisterDummyAI", &AIInfo::DummyConstructor, 2, "tx");
62 /* static */ SQInteger AIInfo::Constructor(HSQUIRRELVM vm)
64 /* Get the AIInfo */
65 SQUserPointer instance = nullptr;
66 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");
67 AIInfo *info = (AIInfo *)instance;
69 SQInteger res = ScriptInfo::Constructor(vm, info);
70 if (res != 0) return res;
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);
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();
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;
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 ({}.{}): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
93 return SQ_ERROR;
95 } else {
96 info->api_version = stredup("0.7");
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;
106 /* static */ SQInteger AIInfo::DummyConstructor(HSQUIRRELVM vm)
108 /* Get the AIInfo */
109 SQUserPointer instance;
110 sq_getinstanceup(vm, 2, &instance, nullptr);
111 AIInfo *info = (AIInfo *)instance;
112 info->api_version = nullptr;
114 SQInteger res = ScriptInfo::Constructor(vm, info);
115 if (res != 0) return res;
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);
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;
128 AIInfo::AIInfo() :
129 min_loadable_version(0),
130 use_as_random(false),
131 api_version(nullptr)
135 AIInfo::~AIInfo()
137 free(this->api_version);
140 bool AIInfo::CanLoadFromVersion(int version) const
142 if (version == -1) return true;
143 return version >= this->min_loadable_version && version <= this->GetVersion();
147 AILibrary::~AILibrary()
149 free(this->category);
152 /* static */ void AILibrary::RegisterAPI(Squirrel *engine)
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");
160 /* static */ SQInteger AILibrary::Constructor(HSQUIRRELVM vm)
162 /* Create a new library */
163 AILibrary *library = new AILibrary();
165 SQInteger res = ScriptInfo::Constructor(vm, library);
166 if (res != 0) {
167 delete library;
168 return res;
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;
177 /* Register the Library to the base system */
178 library->GetScanner()->RegisterScript(library);
180 return 0;