OpenTTD Source 20260218-master-g2123fca5ea
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../stdafx.h"
11
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
26static bool CheckAPIVersion(const std::string &api_version)
27{
28 return std::ranges::find(AIInfo::ApiVersions, api_version) != std::end(AIInfo::ApiVersions);
29}
30
31template <> SQInteger PushClassName<AIInfo, ScriptType::AI>(HSQUIRRELVM vm) { sq_pushstring(vm, "AIInfo"); return 1; }
32
34{
35 /* Create the AIInfo class, and add the RegisterAI function */
36 DefSQClass<AIInfo, ScriptType::AI> SQAIInfo("AIInfo");
37 SQAIInfo.PreRegister(engine);
38 SQAIInfo.AddConstructor<void (AIInfo::*)()>(engine, "x");
39 SQAIInfo.DefSQAdvancedMethod(engine, &AIInfo::AddSetting, "AddSetting");
40 SQAIInfo.DefSQAdvancedMethod(engine, &AIInfo::AddLabels, "AddLabels");
41 SQAIInfo.DefSQConst(engine, ScriptConfigFlags{}.base(), "CONFIG_NONE");
42 SQAIInfo.DefSQConst(engine, ScriptConfigFlags{}.base(), "CONFIG_RANDOM"); // Deprecated, mapped to NONE.
43 SQAIInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::Boolean}.base(), "CONFIG_BOOLEAN");
44 SQAIInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::InGame}.base(), "CONFIG_INGAME");
45 SQAIInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::Developer}.base(), "CONFIG_DEVELOPER");
46
47 /* Pre 1.2 had an AI prefix */
48 SQAIInfo.DefSQConst(engine, ScriptConfigFlags{}.base(), "AICONFIG_NONE");
49 SQAIInfo.DefSQConst(engine, ScriptConfigFlags{}.base(), "AICONFIG_RANDOM"); // Deprecated, mapped to NONE.
50 SQAIInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::Boolean}.base(), "AICONFIG_BOOLEAN");
51 SQAIInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::InGame}.base(), "AICONFIG_INGAME");
52
53 SQAIInfo.PostRegister(engine);
54 engine.AddMethod("RegisterAI", &AIInfo::Constructor, "tx");
55 engine.AddMethod("RegisterDummyAI", &AIInfo::DummyConstructor, "tx");
56}
57
58/* static */ SQInteger AIInfo::Constructor(HSQUIRRELVM vm)
59{
60 /* Get the AIInfo */
61 SQUserPointer instance = nullptr;
62 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");
63 AIInfo *info = (AIInfo *)instance;
64
65 SQInteger res = ScriptInfo::Constructor(vm, *info);
66 if (res != 0) return res;
67
68 if (info->engine->MethodExists(info->SQ_instance, "MinVersionToLoad")) {
69 if (!info->engine->CallIntegerMethod(info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_OPS)) return SQ_ERROR;
70 if (info->min_loadable_version < 0) return SQ_ERROR;
71 } else {
72 info->min_loadable_version = info->GetVersion();
73 }
74 /* When there is an UseAsRandomAI function, call it. */
75 if (info->engine->MethodExists(info->SQ_instance, "UseAsRandomAI")) {
76 if (!info->engine->CallBoolMethod(info->SQ_instance, "UseAsRandomAI", &info->use_as_random, MAX_GET_OPS)) return SQ_ERROR;
77 } else {
78 info->use_as_random = true;
79 }
80 /* Try to get the API version the AI is written for. */
81 if (info->engine->MethodExists(info->SQ_instance, "GetAPIVersion")) {
82 if (!info->engine->CallStringMethod(info->SQ_instance, "GetAPIVersion", &info->api_version, MAX_GET_OPS)) return SQ_ERROR;
83 if (!CheckAPIVersion(info->api_version)) {
84 sq_throwerror(vm, fmt::format("Loading info.nut from ({}.{}): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion()));
85 return SQ_ERROR;
86 }
87 } else {
88 info->api_version = "0.7";
89 }
90
91 /* Remove the link to the real instance, else it might get deleted by RegisterAI() */
92 sq_setinstanceup(vm, 2, nullptr);
93 /* Register the AI to the base system */
94 info->GetScanner()->RegisterScript(std::unique_ptr<AIInfo>{info});
95 return 0;
96}
97
98/* static */ SQInteger AIInfo::DummyConstructor(HSQUIRRELVM vm)
99{
100 /* Get the AIInfo */
101 SQUserPointer instance;
102 sq_getinstanceup(vm, 2, &instance, nullptr);
103 AIInfo *info = (AIInfo *)instance;
104 info->api_version = *std::rbegin(AIInfo::ApiVersions);
105
106 SQInteger res = ScriptInfo::Constructor(vm, *info);
107 if (res != 0) return res;
108
109 /* Remove the link to the real instance, else it might get deleted by RegisterAI() */
110 sq_setinstanceup(vm, 2, nullptr);
111 /* Register the AI to the base system */
112 static_cast<AIScannerInfo *>(info->GetScanner())->SetDummyAI(std::unique_ptr<AIInfo>(info));
113 return 0;
114}
115
116AIInfo::AIInfo() :
117 min_loadable_version(0),
118 use_as_random(false)
119{
120}
121
123{
124 if (version == -1) return true;
125 return version >= this->min_loadable_version && version <= this->GetVersion();
126}
127
128
130{
131 /* Create the AILibrary class, and add the RegisterLibrary function */
132 engine.AddClassBegin("AILibrary");
133 engine.AddClassEnd();
134 engine.AddMethod("RegisterLibrary", &AILibrary::Constructor, "tx");
135}
136
137/* static */ SQInteger AILibrary::Constructor(HSQUIRRELVM vm)
138{
139 /* Create a new library */
140 auto library = std::make_unique<AILibrary>();
141
142 SQInteger res = ScriptInfo::Constructor(vm, *library);
143 if (res != 0) {
144 return res;
145 }
146
147 /* Cache the category */
148 if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethod(library->SQ_instance, "GetCategory", &library->category, MAX_GET_OPS)) {
149 return SQ_ERROR;
150 }
151
152 /* Register the Library to the base system */
153 ScriptScanner *scanner = library->GetScanner();
154 scanner->RegisterScript(std::move(library));
155
156 return 0;
157}
static bool CheckAPIVersion(const std::string &api_version)
Check if the API version provided by the AI is supported.
Definition ai_info.cpp:26
AIInfo keeps track of all information of an AI, like Author, Description, ...
Declarations of the class for AI scanner.
bool CanLoadFromVersion(int version) const
Check if we can start this AI.
Definition ai_info.cpp:122
bool use_as_random
Should this AI be used when the user wants a "random AI"?
Definition ai_info.hpp:64
int min_loadable_version
The AI can load savegame data if the version is equal or greater than this.
Definition ai_info.hpp:63
std::string api_version
API version used by this AI.
Definition ai_info.hpp:65
static void RegisterAPI(Squirrel &engine)
Register the functions of this class.
Definition ai_info.cpp:33
static SQInteger Constructor(HSQUIRRELVM vm)
Create an AI, using this AIInfo as start-template.
Definition ai_info.cpp:58
static constexpr std::string_view ApiVersions[]
All valid AI API versions, in order.
Definition ai_info.hpp:19
static SQInteger DummyConstructor(HSQUIRRELVM vm)
Create a dummy-AI.
Definition ai_info.cpp:98
static void RegisterAPI(Squirrel &engine)
Register the functions of this class.
Definition ai_info.cpp:129
static SQInteger Constructor(HSQUIRRELVM vm)
Create an AI, using this AIInfo as start-template.
Definition ai_info.cpp:137
AI instantiation of a ScriptScanner.
void SetDummyAI(std::unique_ptr< class AIInfo > &&info)
Set the Dummy AI.
The template to define classes in Squirrel.
void DefSQAdvancedMethod(Squirrel &engine, Func function_proc, std::string_view function_name, bool suspendable=false)
This defines a method inside a class for Squirrel, which has access to the 'engine' (experts only!...
int version
Version of the script.
class ScriptScanner * scanner
ScriptScanner object that was used to scan this script info.
const std::string & GetName() const
Get the Name of the script.
class Squirrel * engine
Engine used to register for Squirrel.
static SQInteger Constructor(HSQUIRRELVM vm, ScriptInfo &info)
Process the creation of a FileInfo object.
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.
Scanner to help finding scripts.
void RegisterScript(std::unique_ptr< class ScriptInfo > &&info)
Register a ScriptInfo to the scanner.
bool MethodExists(HSQOBJECT instance, std::string_view method_name)
Check if a method exists in an instance.
Definition squirrel.cpp:343
Functions related to debugging.
Declaration of OTTD revision dependent variables.
A number of safeguards to prevent using unsafe methods.
@ Boolean
This value is a boolean (either 0 (false) or 1 (true) ).
@ Developer
This setting will only be visible when the Script development tools are active.
@ InGame
This setting can be changed while the Script is running.
static const int MAX_GET_OPS
Number of operations to get the author and similar information.
Defines templates for converting C++ classes to Squirrel classes.
SQInteger PushClassName(HSQUIRRELVM vm)
Helper to push the class name of a script type onto the Squirrel stack.
Definition of base types and functions in a cross-platform compatible way.
Functions related to low-level strings.