Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / ai / ai_scanner.cpp
blob5f16de3f91772323bdcc6f720a6d8505025e6430
1 /* $Id$ */
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 */
10 /** @file ai_scanner.cpp allows scanning AI scripts */
12 #include "../stdafx.h"
13 #include "../debug.h"
14 #include "../network/network.h"
15 #include "../core/random_func.hpp"
17 #include "../script/squirrel_class.hpp"
18 #include "ai_info.hpp"
19 #include "ai_scanner.hpp"
21 #include "../safeguards.h"
24 AIScannerInfo::AIScannerInfo() :
25 ScriptScanner(),
26 info_dummy(NULL)
30 void AIScannerInfo::Initialize()
32 ScriptScanner::Initialize("AIScanner");
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");
41 void AIScannerInfo::SetDummyAI(class AIInfo *info)
43 this->info_dummy = info;
46 AIScannerInfo::~AIScannerInfo()
48 delete this->info_dummy;
51 void AIScannerInfo::GetScriptName(ScriptInfo *info, char *name, const char *last)
53 seprintf(name, last, "%s", info->GetName());
56 void AIScannerInfo::RegisterAPI(class Squirrel *engine)
58 AIInfo::RegisterAPI(engine);
61 AIInfo *AIScannerInfo::SelectRandomAI() const
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++;
69 if (num_random_ais == 0) {
70 DEBUG(script, 0, "No suitable AI found, loading 'dummy' AI.");
71 return this->info_dummy;
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);
82 /* Find the Nth item from the array */
83 ScriptInfoList::const_iterator it = this->info_single_list.begin();
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++;
91 return GetAIInfo(it);
92 #undef GetAIInfo
95 AIInfo *AIScannerInfo::FindInfo(const char *nameParam, int versionParam, bool force_exact_match)
97 if (this->info_list.size() == 0) return NULL;
98 if (nameParam == NULL) return NULL;
100 char ai_name[1024];
101 strecpy(ai_name, nameParam, lastof(ai_name));
102 strtolower(ai_name);
104 AIInfo *info = NULL;
105 int version = -1;
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]);
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 /* Continue, like we were calling this function with a version. */
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]);
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;
139 return info;
143 void AIScannerLibrary::Initialize()
145 ScriptScanner::Initialize("AIScanner");
148 void AIScannerLibrary::GetScriptName(ScriptInfo *info, char *name, const char *last)
150 AILibrary *library = static_cast<AILibrary *>(info);
151 seprintf(name, last, "%s.%s", library->GetCategory(), library->GetInstanceName());
154 void AIScannerLibrary::RegisterAPI(class Squirrel *engine)
156 AILibrary::RegisterAPI(engine);
159 AILibrary *AIScannerLibrary::FindLibrary(const char *library, int version)
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);
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;
170 return static_cast<AILibrary *>((*iter).second);