2009-08-21 22:21:05 +02: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 18:11:45 +01:00
|
|
|
/** @file ai_instance.cpp Implementation of AIInstance. */
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../debug.h"
|
2011-12-10 14:54:10 +01:00
|
|
|
#include "../error.h"
|
2009-01-12 18:11:45 +01:00
|
|
|
|
|
|
|
#include "../script/squirrel_class.hpp"
|
2009-01-16 01:05:26 +01:00
|
|
|
|
2009-04-19 17:14:23 +02:00
|
|
|
#include "ai_config.hpp"
|
2011-11-30 00:27:08 +01:00
|
|
|
#include "ai.hpp"
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2011-11-30 00:21:04 +01:00
|
|
|
#include "../script/script_storage.hpp"
|
2021-11-30 00:52:23 +01:00
|
|
|
#include "../script/script_cmd.h"
|
2023-02-04 04:17:55 +01:00
|
|
|
#include "../script/script_gui.h"
|
2011-11-30 00:26:35 +01:00
|
|
|
#include "ai_info.hpp"
|
2011-11-30 00:21:04 +01:00
|
|
|
#include "ai_instance.hpp"
|
|
|
|
|
2011-12-21 16:06:00 +01:00
|
|
|
/* Manually include the Text glue. */
|
|
|
|
#include "../script/api/template/template_text.hpp.sq"
|
|
|
|
|
2019-03-12 15:37:57 +01:00
|
|
|
/* Convert all AI related classes to Squirrel data. */
|
|
|
|
#include "../script/api/ai/ai_includes.hpp"
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2010-01-15 17:41:15 +01:00
|
|
|
#include "../company_base.h"
|
2011-01-22 15:52:20 +01:00
|
|
|
#include "../company_func.h"
|
2009-08-18 20:51:42 +02:00
|
|
|
|
2014-04-23 22:13:33 +02:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2011-11-13 21:43:48 +01:00
|
|
|
AIInstance::AIInstance() :
|
2011-11-30 00:21:33 +01:00
|
|
|
ScriptInstance("AI")
|
|
|
|
{}
|
2011-11-13 21:43:48 +01:00
|
|
|
|
|
|
|
void AIInstance::Initialize(AIInfo *info)
|
|
|
|
{
|
2011-11-30 00:21:33 +01:00
|
|
|
this->versionAPI = info->GetAPIVersion();
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2011-11-23 14:39:36 +01:00
|
|
|
/* Register the AIController (including the "import" command) */
|
2009-01-12 18:11:45 +01:00
|
|
|
SQAIController_Register(this->engine);
|
|
|
|
|
2011-12-19 22:05:25 +01:00
|
|
|
ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), _current_company);
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AIInstance::RegisterAPI()
|
|
|
|
{
|
2011-11-30 00:21:33 +01:00
|
|
|
ScriptInstance::RegisterAPI();
|
|
|
|
|
2019-03-12 15:37:57 +01:00
|
|
|
/* Register all classes */
|
|
|
|
SQAI_RegisterAll(this->engine);
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2012-08-13 21:22:26 +02:00
|
|
|
if (!this->LoadCompatibilityScripts(this->versionAPI, AI_DIR)) this->Died();
|
2009-08-18 20:51:42 +02:00
|
|
|
}
|
|
|
|
|
2009-01-12 18:11:45 +01:00
|
|
|
void AIInstance::Died()
|
|
|
|
{
|
2011-11-30 00:21:33 +01:00
|
|
|
ScriptInstance::Died();
|
2009-03-14 02:32:04 +01:00
|
|
|
|
2022-12-15 21:43:07 +01:00
|
|
|
/* Intro is not supposed to use AI, but it may have 'dummy' AI which instant dies. */
|
|
|
|
if (_game_mode == GM_MENU) return;
|
|
|
|
|
2023-02-04 04:17:55 +01:00
|
|
|
ShowScriptDebugWindow(_current_company);
|
2009-04-19 17:14:23 +02:00
|
|
|
|
2012-04-09 14:35:01 +02:00
|
|
|
const AIInfo *info = AIConfig::GetConfig(_current_company, AIConfig::SSS_FORCE_GAME)->GetInfo();
|
2019-04-10 23:07:06 +02:00
|
|
|
if (info != nullptr) {
|
2010-02-24 15:46:15 +01:00
|
|
|
ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH, INVALID_STRING_ID, WL_WARNING);
|
2009-04-26 21:30:31 +02:00
|
|
|
|
2019-04-10 23:07:06 +02:00
|
|
|
if (info->GetURL() != nullptr) {
|
2011-11-30 00:15:35 +01:00
|
|
|
ScriptLog::Info("Please report the error to the following URL:");
|
|
|
|
ScriptLog::Info(info->GetURL());
|
2009-04-26 21:30:31 +02:00
|
|
|
}
|
2009-04-19 17:14:23 +02:00
|
|
|
}
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
2011-11-30 00:21:42 +01:00
|
|
|
|
2011-11-30 00:26:52 +01:00
|
|
|
void AIInstance::LoadDummyScript()
|
|
|
|
{
|
2019-04-15 19:49:30 +02:00
|
|
|
ScriptAllocatorScope alloc_scope(this->engine);
|
2011-11-30 00:26:52 +01:00
|
|
|
Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
|
|
|
|
}
|
|
|
|
|
2011-11-30 00:27:01 +01:00
|
|
|
int AIInstance::GetSetting(const char *name)
|
|
|
|
{
|
|
|
|
return AIConfig::GetConfig(_current_company)->GetSetting(name);
|
|
|
|
}
|
|
|
|
|
2011-11-30 00:27:08 +01:00
|
|
|
ScriptInfo *AIInstance::FindLibrary(const char *library, int version)
|
|
|
|
{
|
|
|
|
return (ScriptInfo *)AI::FindLibrary(library, version);
|
|
|
|
}
|
|
|
|
|
2011-11-30 00:21:42 +01:00
|
|
|
/**
|
|
|
|
* DoCommand callback function for all commands executed by AIs.
|
2021-10-10 17:20:27 +02:00
|
|
|
* @param cmd cmd as given to DoCommandPInternal.
|
2021-10-31 22:07:22 +01:00
|
|
|
* @param result The result of the command.
|
|
|
|
* @param data Command data as given to Command<>::Post.
|
2021-11-30 00:52:23 +01:00
|
|
|
* @param result_data Additional returned data from the command.
|
2011-11-30 00:21:42 +01:00
|
|
|
*/
|
2023-02-24 22:50:11 +01:00
|
|
|
void CcAI(Commands cmd, const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data)
|
2011-11-30 00:21:42 +01:00
|
|
|
{
|
2013-05-12 15:12:55 +02:00
|
|
|
/*
|
|
|
|
* The company might not exist anymore. Check for this.
|
|
|
|
* The command checks are not useful since this callback
|
|
|
|
* is also called when the command fails, which is does
|
|
|
|
* when the company does not exist anymore.
|
|
|
|
*/
|
|
|
|
const Company *c = Company::GetIfValid(_current_company);
|
2019-04-10 23:07:06 +02:00
|
|
|
if (c == nullptr || c->ai_instance == nullptr) return;
|
2013-05-12 15:12:55 +02:00
|
|
|
|
2023-02-24 22:50:11 +01:00
|
|
|
if (c->ai_instance->DoCommandCallback(result, data, std::move(result_data), cmd)) {
|
2019-09-07 18:37:01 +02:00
|
|
|
c->ai_instance->Continue();
|
|
|
|
}
|
2011-11-30 00:21:42 +01:00
|
|
|
}
|
|
|
|
|
2021-11-28 22:43:38 +01:00
|
|
|
CommandCallbackData *AIInstance::GetDoCommandCallback()
|
2011-11-30 00:21:42 +01:00
|
|
|
{
|
|
|
|
return &CcAI;
|
|
|
|
}
|