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