OpenTTD
ai_scanner.cpp
Go to the documentation of this file.
1 /* $Id: ai_scanner.cpp 26509 2014-04-25 15:40:32Z rubidium $ */
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 #include "../debug.h"
14 #include "../network/network.h"
15 #include "../core/random_func.hpp"
16 
17 #include "../script/squirrel_class.hpp"
18 #include "ai_info.hpp"
19 #include "ai_scanner.hpp"
20 
21 #include "../safeguards.h"
22 
23 
24 AIScannerInfo::AIScannerInfo() :
25  ScriptScanner(),
26  info_dummy(NULL)
27 {
28 }
29 
30 void AIScannerInfo::Initialize()
31 {
32  ScriptScanner::Initialize("AIScanner");
33 
34  /* Create the dummy AI */
35  free(this->main_script);
36  this->main_script = stredup("%_dummy");
37  extern void Script_CreateDummyInfo(HSQUIRRELVM vm, const char *type, const char *dir);
38  Script_CreateDummyInfo(this->engine->GetVM(), "AI", "ai");
39 }
40 
42 {
43  this->info_dummy = info;
44 }
45 
46 AIScannerInfo::~AIScannerInfo()
47 {
48  delete this->info_dummy;
49 }
50 
51 void AIScannerInfo::GetScriptName(ScriptInfo *info, char *name, const char *last)
52 {
53  seprintf(name, last, "%s", info->GetName());
54 }
55 
57 {
58  AIInfo::RegisterAPI(engine);
59 }
60 
62 {
63  uint num_random_ais = 0;
64  for (ScriptInfoList::const_iterator it = this->info_single_list.begin(); it != this->info_single_list.end(); it++) {
65  AIInfo *i = static_cast<AIInfo *>((*it).second);
66  if (i->UseAsRandomAI()) num_random_ais++;
67  }
68 
69  if (num_random_ais == 0) {
70  DEBUG(script, 0, "No suitable AI found, loading 'dummy' AI.");
71  return this->info_dummy;
72  }
73 
74  /* Find a random AI */
75  uint pos;
76  if (_networking) {
77  pos = InteractiveRandomRange(num_random_ais);
78  } else {
79  pos = RandomRange(num_random_ais);
80  }
81 
82  /* Find the Nth item from the array */
83  ScriptInfoList::const_iterator it = this->info_single_list.begin();
84 
85 #define GetAIInfo(it) static_cast<AIInfo *>((*it).second)
86  while (!GetAIInfo(it)->UseAsRandomAI()) it++;
87  for (; pos > 0; pos--) {
88  it++;
89  while (!GetAIInfo(it)->UseAsRandomAI()) it++;
90  }
91  return GetAIInfo(it);
92 #undef GetAIInfo
93 }
94 
95 AIInfo *AIScannerInfo::FindInfo(const char *nameParam, int versionParam, bool force_exact_match)
96 {
97  if (this->info_list.size() == 0) return NULL;
98  if (nameParam == NULL) return NULL;
99 
100  char ai_name[1024];
101  strecpy(ai_name, nameParam, lastof(ai_name));
102  strtolower(ai_name);
103 
104  AIInfo *info = NULL;
105  int version = -1;
106 
107  if (versionParam == -1) {
108  /* We want to load the latest version of this AI; so find it */
109  if (this->info_single_list.find(ai_name) != this->info_single_list.end()) return static_cast<AIInfo *>(this->info_single_list[ai_name]);
110 
111  /* If we didn't find a match AI, maybe the user included a version */
112  char *e = strrchr(ai_name, '.');
113  if (e == NULL) return NULL;
114  *e = '\0';
115  e++;
116  versionParam = atoi(e);
117  /* FALL THROUGH, like we were calling this function with a version. */
118  }
119 
120  if (force_exact_match) {
121  /* Try to find a direct 'name.version' match */
122  char ai_name_tmp[1024];
123  seprintf(ai_name_tmp, lastof(ai_name_tmp), "%s.%d", ai_name, versionParam);
124  strtolower(ai_name_tmp);
125  if (this->info_list.find(ai_name_tmp) != this->info_list.end()) return static_cast<AIInfo *>(this->info_list[ai_name_tmp]);
126  }
127 
128  /* See if there is a compatible AI which goes by that name, with the highest
129  * version which allows loading the requested version */
130  ScriptInfoList::iterator it = this->info_list.begin();
131  for (; it != this->info_list.end(); it++) {
132  AIInfo *i = static_cast<AIInfo *>((*it).second);
133  if (strcasecmp(ai_name, i->GetName()) == 0 && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
134  version = (*it).second->GetVersion();
135  info = i;
136  }
137  }
138 
139  return info;
140 }
141 
142 
143 void AIScannerLibrary::Initialize()
144 {
145  ScriptScanner::Initialize("AIScanner");
146 }
147 
148 void AIScannerLibrary::GetScriptName(ScriptInfo *info, char *name, const char *last)
149 {
150  AILibrary *library = static_cast<AILibrary *>(info);
151  seprintf(name, last, "%s.%s", library->GetCategory(), library->GetInstanceName());
152 }
153 
155 {
156  AILibrary::RegisterAPI(engine);
157 }
158 
159 AILibrary *AIScannerLibrary::FindLibrary(const char *library, int version)
160 {
161  /* Internally we store libraries as 'library.version' */
162  char library_name[1024];
163  seprintf(library_name, lastof(library_name), "%s.%d", library, version);
164  strtolower(library_name);
165 
166  /* Check if the library + version exists */
167  ScriptInfoList::iterator iter = this->info_list.find(library_name);
168  if (iter == this->info_list.end()) return NULL;
169 
170  return static_cast<AILibrary *>((*iter).second);
171 }