comparison src/ai/ai_info.cpp @ 18518:928151e04253 draft

(svn r23362) -Codechange: refactor AIScanner, splitting it in AIScannerInfo and AIScannerLibrary
author truebrain <truebrain@openttd.org>
date Tue, 29 Nov 2011 23:21:52 +0000
parents ae9def3e6284
children bb2c3b72ca78
comparison
equal deleted inserted replaced
18517:3dc74f255217 18518:928151e04253
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. 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. 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/>. 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 */ 8 */
9 9
10 /** @file ai_info.cpp Implementation of AIFileInfo */ 10 /** @file ai_info.cpp Implementation of AIInfo and AILibrary */
11 11
12 #include "../stdafx.h" 12 #include "../stdafx.h"
13 13
14 #include "../script/squirrel_helper.hpp" 14 #include "../script/squirrel_helper.hpp"
15 #include "../script/squirrel_class.hpp"
15 #include "ai_info.hpp" 16 #include "ai_info.hpp"
16 #include "ai_scanner.hpp" 17 #include "ai_scanner.hpp"
17 #include "../settings_type.h" 18 #include "../settings_type.h"
18 #include "../debug.h" 19 #include "../debug.h"
19 #include "../rev.h" 20 #include "../rev.h"
21 #include "ai.hpp"
20 22
21 /** Maximum number of operations allowed for getting a particular setting. */ 23 /** Maximum number of operations allowed for getting a particular setting. */
22 static const int MAX_GET_SETTING_OPS = 100000; 24 static const int MAX_GET_SETTING_OPS = 100000;
23 25
24 /** Configuration for AI start date, every AI has this setting. */ 26 /** Configuration for AI start date, every AI has this setting. */
35 30, 37 30,
36 AICONFIG_NONE, 38 AICONFIG_NONE,
37 NULL 39 NULL
38 }; 40 };
39 41
40 AILibrary::~AILibrary()
41 {
42 free(this->category);
43 }
44
45 /* static */ SQInteger AIFileInfo::Constructor(HSQUIRRELVM vm, AIFileInfo *info)
46 {
47 SQInteger res = ScriptFileInfo::Constructor(vm, info);
48 if (res != 0) return res;
49 info->base = ((AIScanner *)Squirrel::GetGlobalPointer(vm));
50
51 return 0;
52 }
53
54 /** 42 /**
55 * Check if the API version provided by the AI is supported. 43 * Check if the API version provided by the AI is supported.
56 * @param api_version The API version as provided by the AI. 44 * @param api_version The API version as provided by the AI.
57 */ 45 */
58 static bool CheckAPIVersion(const char *api_version) 46 static bool CheckAPIVersion(const char *api_version)
59 { 47 {
60 return strcmp(api_version, "0.7") == 0 || strcmp(api_version, "1.0") == 0 || strcmp(api_version, "1.1") == 0 || strcmp(api_version, "1.2") == 0; 48 return strcmp(api_version, "0.7") == 0 || strcmp(api_version, "1.0") == 0 || strcmp(api_version, "1.1") == 0 || strcmp(api_version, "1.2") == 0;
61 } 49 }
62 50
51 #if defined(WIN32)
52 #undef GetClassName
53 #endif /* WIN32 */
54 template <> const char *GetClassName<AIInfo, ST_AI>() { return "AIInfo"; }
55
56 /* static */ void AIInfo::RegisterAPI(Squirrel *engine)
57 {
58 /* Create the AIInfo class, and add the RegisterAI function */
59 DefSQClass<AIInfo, ST_AI> SQAIInfo("AIInfo");
60 SQAIInfo.PreRegister(engine);
61 SQAIInfo.AddConstructor<void (AIInfo::*)(), 1>(engine, "x");
62 SQAIInfo.DefSQAdvancedMethod(engine, &AIInfo::AddSetting, "AddSetting");
63 SQAIInfo.DefSQAdvancedMethod(engine, &AIInfo::AddLabels, "AddLabels");
64 SQAIInfo.DefSQConst(engine, AICONFIG_NONE, "AICONFIG_NONE");
65 SQAIInfo.DefSQConst(engine, AICONFIG_RANDOM, "AICONFIG_RANDOM");
66 SQAIInfo.DefSQConst(engine, AICONFIG_BOOLEAN, "AICONFIG_BOOLEAN");
67 SQAIInfo.DefSQConst(engine, AICONFIG_INGAME, "AICONFIG_INGAME");
68 SQAIInfo.DefSQConst(engine, AICONFIG_AI_DEVELOPER, "AICONFIG_AI_DEVELOPER");
69 SQAIInfo.PostRegister(engine);
70 engine->AddMethod("RegisterAI", &AIInfo::Constructor, 2, "tx");
71 engine->AddMethod("RegisterDummyAI", &AIInfo::DummyConstructor, 2, "tx");
72 }
73
63 /* static */ SQInteger AIInfo::Constructor(HSQUIRRELVM vm) 74 /* static */ SQInteger AIInfo::Constructor(HSQUIRRELVM vm)
64 { 75 {
65 /* Get the AIInfo */ 76 /* Get the AIInfo */
66 SQUserPointer instance = NULL; 77 SQUserPointer instance = NULL;
67 if (SQ_FAILED(sq_getinstanceup(vm, 2, &instance, 0)) || instance == NULL) return sq_throwerror(vm, _SC("Pass an instance of a child class of AIInfo to RegisterAI")); 78 if (SQ_FAILED(sq_getinstanceup(vm, 2, &instance, 0)) || instance == NULL) return sq_throwerror(vm, _SC("Pass an instance of a child class of AIInfo to RegisterAI"));
68 AIInfo *info = (AIInfo *)instance; 79 AIInfo *info = (AIInfo *)instance;
69 80
70 SQInteger res = AIFileInfo::Constructor(vm, info); 81 SQInteger res = ScriptInfo::Constructor(vm, info);
71 if (res != 0) return res; 82 if (res != 0) return res;
72 83
73 AIConfigItem config = _start_date_config; 84 AIConfigItem config = _start_date_config;
74 config.name = strdup(config.name); 85 config.name = strdup(config.name);
75 config.description = strdup(config.description); 86 config.description = strdup(config.description);
102 } 113 }
103 114
104 /* Remove the link to the real instance, else it might get deleted by RegisterAI() */ 115 /* Remove the link to the real instance, else it might get deleted by RegisterAI() */
105 sq_setinstanceup(vm, 2, NULL); 116 sq_setinstanceup(vm, 2, NULL);
106 /* Register the AI to the base system */ 117 /* Register the AI to the base system */
107 info->base->RegisterAI(info); 118 info->GetScanner()->RegisterScript(info);
108 return 0; 119 return 0;
109 } 120 }
110 121
111 /* static */ SQInteger AIInfo::DummyConstructor(HSQUIRRELVM vm) 122 /* static */ SQInteger AIInfo::DummyConstructor(HSQUIRRELVM vm)
112 { 123 {
114 SQUserPointer instance; 125 SQUserPointer instance;
115 sq_getinstanceup(vm, 2, &instance, 0); 126 sq_getinstanceup(vm, 2, &instance, 0);
116 AIInfo *info = (AIInfo *)instance; 127 AIInfo *info = (AIInfo *)instance;
117 info->api_version = NULL; 128 info->api_version = NULL;
118 129
119 SQInteger res = AIFileInfo::Constructor(vm, info); 130 SQInteger res = ScriptInfo::Constructor(vm, info);
120 if (res != 0) return res; 131 if (res != 0) return res;
121 132
122 char buf[8]; 133 char buf[8];
123 seprintf(buf, lastof(buf), "%d.%d", GB(_openttd_newgrf_version, 28, 4), GB(_openttd_newgrf_version, 24, 4)); 134 seprintf(buf, lastof(buf), "%d.%d", GB(_openttd_newgrf_version, 28, 4), GB(_openttd_newgrf_version, 24, 4));
124 info->api_version = strdup(buf); 135 info->api_version = strdup(buf);
125 136
126 /* Remove the link to the real instance, else it might get deleted by RegisterAI() */ 137 /* Remove the link to the real instance, else it might get deleted by RegisterAI() */
127 sq_setinstanceup(vm, 2, NULL); 138 sq_setinstanceup(vm, 2, NULL);
128 /* Register the AI to the base system */ 139 /* Register the AI to the base system */
129 info->base->SetDummyAI(info); 140 static_cast<AIScannerInfo *>(info->GetScanner())->SetDummyAI(info);
130 return 0; 141 return 0;
131 } 142 }
132 143
133 bool AIInfo::GetSettings() 144 bool AIInfo::GetSettings()
134 { 145 {
348 359
349 /* There is no such setting */ 360 /* There is no such setting */
350 return -1; 361 return -1;
351 } 362 }
352 363
364
365 AILibrary::~AILibrary()
366 {
367 free(this->category);
368 }
369
370 /* static */ void AILibrary::RegisterAPI(Squirrel *engine)
371 {
372 /* Create the AILibrary class, and add the RegisterLibrary function */
373 engine->AddClassBegin("AILibrary");
374 engine->AddClassEnd();
375 engine->AddMethod("RegisterLibrary", &AILibrary::Constructor, 2, "tx");
376 }
377
353 /* static */ SQInteger AILibrary::Constructor(HSQUIRRELVM vm) 378 /* static */ SQInteger AILibrary::Constructor(HSQUIRRELVM vm)
354 { 379 {
355 /* Create a new AIFileInfo */ 380 /* Create a new library */
356 AILibrary *library = new AILibrary(); 381 AILibrary *library = new AILibrary();
357 382
358 SQInteger res = AIFileInfo::Constructor(vm, library); 383 SQInteger res = ScriptInfo::Constructor(vm, library);
359 if (res != 0) { 384 if (res != 0) {
360 delete library; 385 delete library;
361 return res; 386 return res;
362 } 387 }
363 388
366 delete library; 391 delete library;
367 return SQ_ERROR; 392 return SQ_ERROR;
368 } 393 }
369 394
370 /* Register the Library to the base system */ 395 /* Register the Library to the base system */
371 library->base->RegisterLibrary(library); 396 library->GetScanner()->RegisterScript(library);
372 397
373 return 0; 398 return 0;
374 } 399 }