2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/** @file ai_scanner.cpp allows scanning AI scripts */
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../debug.h"
|
|
|
|
#include "../network/network.h"
|
2022-12-15 20:43:07 +00:00
|
|
|
#include "../openttd.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "../core/random_func.hpp"
|
|
|
|
|
|
|
|
#include "../script/squirrel_class.hpp"
|
2023-01-14 12:38:05 +00:00
|
|
|
#include "../script/api/script_object.hpp"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "ai_info.hpp"
|
|
|
|
#include "ai_scanner.hpp"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2011-11-13 20:52:39 +00:00
|
|
|
|
2011-11-29 23:21:52 +00:00
|
|
|
AIScannerInfo::AIScannerInfo() :
|
2009-03-15 22:41:57 +00:00
|
|
|
ScriptScanner(),
|
2019-04-10 21:07:06 +00:00
|
|
|
info_dummy(nullptr)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:21:52 +00:00
|
|
|
}
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-12-15 19:52:44 +00:00
|
|
|
void AIScannerInfo::Initialize()
|
2011-11-29 23:21:52 +00:00
|
|
|
{
|
2011-12-15 19:52:44 +00:00
|
|
|
ScriptScanner::Initialize("AIScanner");
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2019-04-15 17:49:30 +00:00
|
|
|
ScriptAllocatorScope alloc_scope(this->engine);
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/* Create the dummy AI */
|
2020-12-06 20:11:50 +00:00
|
|
|
this->main_script = "%_dummy";
|
2011-11-29 23:26:52 +00:00
|
|
|
Script_CreateDummyInfo(this->engine->GetVM(), "AI", "ai");
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:21:52 +00:00
|
|
|
void AIScannerInfo::SetDummyAI(class AIInfo *info)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:21:52 +00:00
|
|
|
this->info_dummy = info;
|
2010-11-18 23:31:06 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:21:52 +00:00
|
|
|
AIScannerInfo::~AIScannerInfo()
|
2010-11-18 23:31:06 +00:00
|
|
|
{
|
2009-01-17 15:07:35 +00:00
|
|
|
delete this->info_dummy;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2014-04-23 21:16:58 +00:00
|
|
|
void AIScannerInfo::GetScriptName(ScriptInfo *info, char *name, const char *last)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2014-04-23 21:16:58 +00:00
|
|
|
seprintf(name, last, "%s", info->GetName());
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:21:52 +00:00
|
|
|
void AIScannerInfo::RegisterAPI(class Squirrel *engine)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:21:52 +00:00
|
|
|
AIInfo::RegisterAPI(engine);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:21:52 +00:00
|
|
|
AIInfo *AIScannerInfo::SelectRandomAI() const
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2022-12-15 20:43:07 +00:00
|
|
|
if (_game_mode == GM_MENU) {
|
|
|
|
Debug(script, 0, "The intro game should not use AI, loading 'dummy' AI.");
|
|
|
|
return this->info_dummy;
|
|
|
|
}
|
|
|
|
|
2009-04-21 19:13:32 +00:00
|
|
|
uint num_random_ais = 0;
|
2020-12-25 18:38:18 +00:00
|
|
|
for (const auto &item : info_single_list) {
|
|
|
|
AIInfo *i = static_cast<AIInfo *>(item.second);
|
2011-11-29 23:21:52 +00:00
|
|
|
if (i->UseAsRandomAI()) num_random_ais++;
|
2009-04-21 19:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (num_random_ais == 0) {
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(script, 0, "No suitable AI found, loading 'dummy' AI.");
|
2009-01-12 17:11:45 +00:00
|
|
|
return this->info_dummy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find a random AI */
|
2023-01-14 12:38:05 +00:00
|
|
|
uint pos = ScriptObject::GetRandomizer(OWNER_NONE).Next(num_random_ais);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/* Find the Nth item from the array */
|
2011-11-29 23:21:52 +00:00
|
|
|
ScriptInfoList::const_iterator it = this->info_single_list.begin();
|
2011-12-02 23:40:32 +00:00
|
|
|
|
|
|
|
#define GetAIInfo(it) static_cast<AIInfo *>((*it).second)
|
|
|
|
while (!GetAIInfo(it)->UseAsRandomAI()) it++;
|
2009-04-21 19:13:32 +00:00
|
|
|
for (; pos > 0; pos--) {
|
|
|
|
it++;
|
2011-12-02 23:40:32 +00:00
|
|
|
while (!GetAIInfo(it)->UseAsRandomAI()) it++;
|
2009-04-21 19:13:32 +00:00
|
|
|
}
|
2011-12-02 23:40:32 +00:00
|
|
|
return GetAIInfo(it);
|
|
|
|
#undef GetAIInfo
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:21:52 +00:00
|
|
|
AIInfo *AIScannerInfo::FindInfo(const char *nameParam, int versionParam, bool force_exact_match)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->info_list.size() == 0) return nullptr;
|
|
|
|
if (nameParam == nullptr) return nullptr;
|
2009-01-15 18:15:12 +00:00
|
|
|
|
|
|
|
char ai_name[1024];
|
2014-04-23 20:44:42 +00:00
|
|
|
strecpy(ai_name, nameParam, lastof(ai_name));
|
2009-01-16 14:56:13 +00:00
|
|
|
strtolower(ai_name);
|
2009-01-15 18:15:12 +00:00
|
|
|
|
|
|
|
if (versionParam == -1) {
|
|
|
|
/* We want to load the latest version of this AI; so find it */
|
2011-11-29 23:21:52 +00:00
|
|
|
if (this->info_single_list.find(ai_name) != this->info_single_list.end()) return static_cast<AIInfo *>(this->info_single_list[ai_name]);
|
2020-12-25 14:22:40 +00:00
|
|
|
return nullptr;
|
2009-01-15 18:15:12 +00:00
|
|
|
}
|
|
|
|
|
2010-01-29 00:03:31 +00:00
|
|
|
if (force_exact_match) {
|
|
|
|
/* Try to find a direct 'name.version' match */
|
|
|
|
char ai_name_tmp[1024];
|
2014-04-23 21:12:09 +00:00
|
|
|
seprintf(ai_name_tmp, lastof(ai_name_tmp), "%s.%d", ai_name, versionParam);
|
2010-01-29 00:03:31 +00:00
|
|
|
strtolower(ai_name_tmp);
|
2011-11-29 23:21:52 +00:00
|
|
|
if (this->info_list.find(ai_name_tmp) != this->info_list.end()) return static_cast<AIInfo *>(this->info_list[ai_name_tmp]);
|
2020-12-25 14:22:40 +00:00
|
|
|
return nullptr;
|
2010-01-29 00:03:31 +00:00
|
|
|
}
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2020-12-25 14:22:40 +00:00
|
|
|
AIInfo *info = nullptr;
|
|
|
|
int version = -1;
|
|
|
|
|
2009-01-15 18:15:12 +00:00
|
|
|
/* See if there is a compatible AI which goes by that name, with the highest
|
|
|
|
* version which allows loading the requested version */
|
2020-12-25 18:38:18 +00:00
|
|
|
for (const auto &item : this->info_list) {
|
|
|
|
AIInfo *i = static_cast<AIInfo *>(item.second);
|
2023-04-27 09:47:08 +00:00
|
|
|
if (StrEqualsIgnoreCase(ai_name, i->GetName()) && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
|
2020-12-25 18:38:18 +00:00
|
|
|
version = item.second->GetVersion();
|
2011-11-29 23:21:52 +00:00
|
|
|
info = i;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-15 18:15:12 +00:00
|
|
|
return info;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2009-01-17 16:53:32 +00:00
|
|
|
|
2011-12-15 19:52:44 +00:00
|
|
|
void AIScannerLibrary::Initialize()
|
|
|
|
{
|
|
|
|
ScriptScanner::Initialize("AIScanner");
|
|
|
|
}
|
|
|
|
|
2014-04-23 21:16:58 +00:00
|
|
|
void AIScannerLibrary::GetScriptName(ScriptInfo *info, char *name, const char *last)
|
2011-01-03 14:52:30 +00:00
|
|
|
{
|
2011-11-29 23:21:52 +00:00
|
|
|
AILibrary *library = static_cast<AILibrary *>(info);
|
2014-04-23 21:16:58 +00:00
|
|
|
seprintf(name, last, "%s.%s", library->GetCategory(), library->GetInstanceName());
|
2011-01-03 14:52:30 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:21:52 +00:00
|
|
|
void AIScannerLibrary::RegisterAPI(class Squirrel *engine)
|
2009-01-17 16:53:32 +00:00
|
|
|
{
|
2011-11-29 23:21:52 +00:00
|
|
|
AILibrary::RegisterAPI(engine);
|
2009-01-17 16:53:32 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:21:52 +00:00
|
|
|
AILibrary *AIScannerLibrary::FindLibrary(const char *library, int version)
|
2009-01-17 16:53:32 +00:00
|
|
|
{
|
2011-11-29 23:21:52 +00:00
|
|
|
/* Internally we store libraries as 'library.version' */
|
|
|
|
char library_name[1024];
|
2014-04-23 21:12:09 +00:00
|
|
|
seprintf(library_name, lastof(library_name), "%s.%d", library, version);
|
2011-11-29 23:21:52 +00:00
|
|
|
strtolower(library_name);
|
2009-01-17 16:53:32 +00:00
|
|
|
|
2011-11-29 23:21:52 +00:00
|
|
|
/* Check if the library + version exists */
|
2020-12-25 18:38:18 +00:00
|
|
|
ScriptInfoList::iterator it = this->info_list.find(library_name);
|
|
|
|
if (it == this->info_list.end()) return nullptr;
|
2009-01-17 16:53:32 +00:00
|
|
|
|
2020-12-25 18:38:18 +00:00
|
|
|
return static_cast<AILibrary *>((*it).second);
|
2009-01-17 16:53:32 +00:00
|
|
|
}
|