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_instance.cpp Implementation of AIInstance. */
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../debug.h"
|
2011-12-10 13:54:10 +00:00
|
|
|
#include "../error.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
#include "../script/squirrel_class.hpp"
|
2009-01-16 00:05:26 +00:00
|
|
|
|
2009-04-19 15:14:23 +00:00
|
|
|
#include "ai_config.hpp"
|
2009-03-14 01:32:04 +00:00
|
|
|
#include "ai_gui.hpp"
|
2011-11-29 23:27:08 +00:00
|
|
|
#include "ai.hpp"
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-11-29 23:21:04 +00:00
|
|
|
#include "../script/script_storage.hpp"
|
2011-11-29 23:26:35 +00:00
|
|
|
#include "ai_info.hpp"
|
2011-11-29 23:21:04 +00:00
|
|
|
#include "ai_instance.hpp"
|
|
|
|
|
2011-12-21 15:06:00 +00:00
|
|
|
/* Manually include the Text glue. */
|
|
|
|
#include "../script/api/template/template_text.hpp.sq"
|
|
|
|
|
2019-03-12 14:37:57 +00:00
|
|
|
/* Convert all AI related classes to Squirrel data. */
|
|
|
|
#include "../script/api/ai/ai_includes.hpp"
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "../company_base.h"
|
2011-01-22 14:52:20 +00:00
|
|
|
#include "../company_func.h"
|
2009-08-18 18:51:42 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2011-11-13 20:43:48 +00:00
|
|
|
AIInstance::AIInstance() :
|
2011-11-29 23:21:33 +00:00
|
|
|
ScriptInstance("AI")
|
|
|
|
{}
|
2011-11-13 20:43:48 +00:00
|
|
|
|
|
|
|
void AIInstance::Initialize(AIInfo *info)
|
|
|
|
{
|
2011-11-29 23:21:33 +00:00
|
|
|
this->versionAPI = info->GetAPIVersion();
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-11-23 13:39:36 +00:00
|
|
|
/* Register the AIController (including the "import" command) */
|
2009-01-12 17:11:45 +00:00
|
|
|
SQAIController_Register(this->engine);
|
|
|
|
|
2011-12-19 21:05:25 +00:00
|
|
|
ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), _current_company);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AIInstance::RegisterAPI()
|
|
|
|
{
|
2011-11-29 23:21:33 +00:00
|
|
|
ScriptInstance::RegisterAPI();
|
|
|
|
|
2019-03-12 14:37:57 +00:00
|
|
|
/* Register all classes */
|
|
|
|
SQAI_RegisterAll(this->engine);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2012-08-13 19:22:26 +00:00
|
|
|
if (!this->LoadCompatibilityScripts(this->versionAPI, AI_DIR)) this->Died();
|
2009-08-18 18:51:42 +00:00
|
|
|
}
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
void AIInstance::Died()
|
|
|
|
{
|
2011-11-29 23:21:33 +00:00
|
|
|
ScriptInstance::Died();
|
2009-03-14 01:32:04 +00:00
|
|
|
|
|
|
|
ShowAIDebugWindow(_current_company);
|
2009-04-19 15:14:23 +00:00
|
|
|
|
2012-04-09 12:35:01 +00:00
|
|
|
const AIInfo *info = AIConfig::GetConfig(_current_company, AIConfig::SSS_FORCE_GAME)->GetInfo();
|
2019-04-10 21:07:06 +00:00
|
|
|
if (info != nullptr) {
|
2010-02-24 14:46:15 +00:00
|
|
|
ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH, INVALID_STRING_ID, WL_WARNING);
|
2009-04-26 19:30:31 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (info->GetURL() != nullptr) {
|
2011-11-29 23:15:35 +00:00
|
|
|
ScriptLog::Info("Please report the error to the following URL:");
|
|
|
|
ScriptLog::Info(info->GetURL());
|
2009-04-26 19:30:31 +00:00
|
|
|
}
|
2009-04-19 15:14:23 +00:00
|
|
|
}
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
2011-11-29 23:21:42 +00:00
|
|
|
|
2011-11-29 23:26:52 +00:00
|
|
|
void AIInstance::LoadDummyScript()
|
|
|
|
{
|
2019-04-15 17:49:30 +00:00
|
|
|
ScriptAllocatorScope alloc_scope(this->engine);
|
2011-11-29 23:26:52 +00:00
|
|
|
extern void Script_CreateDummy(HSQUIRRELVM vm, StringID string, const char *type);
|
|
|
|
Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:27:01 +00:00
|
|
|
int AIInstance::GetSetting(const char *name)
|
|
|
|
{
|
|
|
|
return AIConfig::GetConfig(_current_company)->GetSetting(name);
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:27:08 +00:00
|
|
|
ScriptInfo *AIInstance::FindLibrary(const char *library, int version)
|
|
|
|
{
|
|
|
|
return (ScriptInfo *)AI::FindLibrary(library, version);
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:21:42 +00:00
|
|
|
/**
|
|
|
|
* DoCommand callback function for all commands executed by AIs.
|
|
|
|
* @param result The result of the command.
|
|
|
|
* @param tile The tile on which the command was executed.
|
|
|
|
* @param p1 p1 as given to DoCommandPInternal.
|
|
|
|
* @param p2 p2 as given to DoCommandPInternal.
|
2019-09-07 16:37:01 +00:00
|
|
|
* @param cmd cmd as given to DoCommandPInternal.
|
2011-11-29 23:21:42 +00:00
|
|
|
*/
|
2019-09-07 16:37:01 +00:00
|
|
|
void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
|
2011-11-29 23:21:42 +00:00
|
|
|
{
|
2013-05-12 13:12:55 +00: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 21:07:06 +00:00
|
|
|
if (c == nullptr || c->ai_instance == nullptr) return;
|
2013-05-12 13:12:55 +00:00
|
|
|
|
2019-09-07 16:37:01 +00:00
|
|
|
if (c->ai_instance->DoCommandCallback(result, tile, p1, p2, cmd)) {
|
|
|
|
c->ai_instance->Continue();
|
|
|
|
}
|
2011-11-29 23:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CommandCallback *AIInstance::GetDoCommandCallback()
|
|
|
|
{
|
|
|
|
return &CcAI;
|
|
|
|
}
|