OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

ai_info.cpp
Go to the documentation of this file.
1 /* $Id: ai_info.cpp 27193 2015-03-17 20:45:57Z frosch $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #include "../stdafx.h"
13
14 #include "../script/squirrel_class.hpp"
15 #include "ai_info.hpp"
16 #include "ai_scanner.hpp"
17 #include "../debug.h"
18 #include "../string_func.h"
19 #include "../rev.h"
20
21 #include "../safeguards.h"
22
27 static bool CheckAPIVersion(const char *api_version)
28 {
29 return strcmp(api_version, "0.7") == 0 || strcmp(api_version, "1.0") == 0 || strcmp(api_version, "1.1") == 0 ||
30 strcmp(api_version, "1.2") == 0 || strcmp(api_version, "1.3") == 0 || strcmp(api_version, "1.4") == 0 ||
31 strcmp(api_version, "1.5") == 0 || strcmp(api_version, "1.6") == 0;
32 }
33
34 #if defined(WIN32)
35 #undef GetClassName
36 #endif /* WIN32 */
37 template <> const char *GetClassName<AIInfo, ST_AI>() { return "AIInfo"; }
38
39 /* static */ void AIInfo::RegisterAPI(Squirrel *engine)
40 {
41 /* Create the AIInfo class, and add the RegisterAI function */
42 DefSQClass<AIInfo, ST_AI> SQAIInfo("AIInfo");
43 SQAIInfo.PreRegister(engine);
44 SQAIInfo.AddConstructor<void (AIInfo::*)(), 1>(engine, "x");
45 SQAIInfo.DefSQAdvancedMethod(engine, &AIInfo::AddSetting, "AddSetting");
46 SQAIInfo.DefSQAdvancedMethod(engine, &AIInfo::AddLabels, "AddLabels");
47 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_NONE");
48 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_RANDOM, "CONFIG_RANDOM");
49 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "CONFIG_BOOLEAN");
50 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "CONFIG_INGAME");
51 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_DEVELOPER, "CONFIG_DEVELOPER");
52
53 /* Pre 1.2 had an AI prefix */
54 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "AICONFIG_NONE");
55 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_RANDOM, "AICONFIG_RANDOM");
56 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "AICONFIG_BOOLEAN");
57 SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "AICONFIG_INGAME");
58
59 SQAIInfo.PostRegister(engine);
60 engine->AddMethod("RegisterAI", &AIInfo::Constructor, 2, "tx");
61 engine->AddMethod("RegisterDummyAI", &AIInfo::DummyConstructor, 2, "tx");
62 }
63
64 /* static */ SQInteger AIInfo::Constructor(HSQUIRRELVM vm)
65 {
66 /* Get the AIInfo */
67 SQUserPointer instance = NULL;
68 if (SQ_FAILED(sq_getinstanceup(vm, 2, &instance, 0)) || instance == NULL) return sq_throwerror(vm, "Pass an instance of a child class of AIInfo to RegisterAI");
69 AIInfo *info = (AIInfo *)instance;
70
71 SQInteger res = ScriptInfo::Constructor(vm, info);
72 if (res != 0) return res;
73
74 ScriptConfigItem config = _start_date_config;
75 config.name = stredup(config.name);
76 config.description = stredup(config.description);
77 info->config_list.push_front(config);
78
79 if (info->engine->MethodExists(*info->SQ_instance, "MinVersionToLoad")) {
80 if (!info->engine->CallIntegerMethod(*info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_OPS)) return SQ_ERROR;
81 } else {
82 info->min_loadable_version = info->GetVersion();
83 }
84 /* When there is an UseAsRandomAI function, call it. */
85 if (info->engine->MethodExists(*info->SQ_instance, "UseAsRandomAI")) {
86 if (!info->engine->CallBoolMethod(*info->SQ_instance, "UseAsRandomAI", &info->use_as_random, MAX_GET_OPS)) return SQ_ERROR;
87 } else {
88 info->use_as_random = true;
89 }
90 /* Try to get the API version the AI is written for. */
91 if (info->engine->MethodExists(*info->SQ_instance, "GetAPIVersion")) {
92 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAPIVersion", &info->api_version, MAX_GET_OPS)) return SQ_ERROR;
93 if (!CheckAPIVersion(info->api_version)) {
94 DEBUG(script, 1, "Loading info.nut from (%s.%d): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
95 return SQ_ERROR;
96 }
97 } else {
98 info->api_version = stredup("0.7");
99 }
100
101 /* Remove the link to the real instance, else it might get deleted by RegisterAI() */
102 sq_setinstanceup(vm, 2, NULL);
103 /* Register the AI to the base system */
104 info->GetScanner()->RegisterScript(info);
105 return 0;
106 }
107
108 /* static */ SQInteger AIInfo::DummyConstructor(HSQUIRRELVM vm)
109 {
110 /* Get the AIInfo */
111 SQUserPointer instance;
112 sq_getinstanceup(vm, 2, &instance, 0);
113 AIInfo *info = (AIInfo *)instance;
114 info->api_version = NULL;
115
116 SQInteger res = ScriptInfo::Constructor(vm, info);
117 if (res != 0) return res;
118
119 char buf[8];
120 seprintf(buf, lastof(buf), "%d.%d", GB(_openttd_newgrf_version, 28, 4), GB(_openttd_newgrf_version, 24, 4));
121 info->api_version = stredup(buf);
122
123 /* Remove the link to the real instance, else it might get deleted by RegisterAI() */
124 sq_setinstanceup(vm, 2, NULL);
125 /* Register the AI to the base system */
126 static_cast<AIScannerInfo *>(info->GetScanner())->SetDummyAI(info);
127 return 0;
128 }
129
130 AIInfo::AIInfo() :
131 min_loadable_version(0),
132 use_as_random(false),
133 api_version(NULL)
134 {
135 }
136
137 AIInfo::~AIInfo()
138 {
139 free(this->api_version);
140 }
141
142 bool AIInfo::CanLoadFromVersion(int version) const
143 {
144 if (version == -1) return true;
145 return version >= this->min_loadable_version && version <= this->GetVersion();
146 }
147
148
149 AILibrary::~AILibrary()
150 {
151 free(this->category);
152 }
153
154 /* static */ void AILibrary::RegisterAPI(Squirrel *engine)
155 {
156 /* Create the AILibrary class, and add the RegisterLibrary function */
157 engine->AddClassBegin("AILibrary");
158 engine->AddClassEnd();
159 engine->AddMethod("RegisterLibrary", &AILibrary::Constructor, 2, "tx");
160 }
161
162 /* static */ SQInteger AILibrary::Constructor(HSQUIRRELVM vm)
163 {
164 /* Create a new library */
165 AILibrary *library = new AILibrary();
166
167 SQInteger res = ScriptInfo::Constructor(vm, library);
168 if (res != 0) {
169 delete library;
170 return res;
171 }
172
173 /* Cache the category */
174 if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethodStrdup(*library->SQ_instance, "GetCategory", &library->category, MAX_GET_OPS)) {
175 delete library;
176 return SQ_ERROR;
177 }
178
179 /* Register the Library to the base system */
180 library->GetScanner()->RegisterScript(library);
181
182 return 0;
183 }