Files @ r27970:529bbf8cd065
Branch filter:

Location: cpp/openttd-patchpack/source/src/ai/ai_instance.cpp - annotation

frosch
Doc: Link 3rdparty licenses from README. (#11333)
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r10696:8dfe83e30d01
r10696:8dfe83e30d01
r10696:8dfe83e30d01
r10696:8dfe83e30d01
r18617:524f2e0f54dc
r10696:8dfe83e30d01
r10696:8dfe83e30d01
r10768:5cefaf608914
r11702:a9dd3b59c546
r18514:5b2abc88a36c
r18505:6589b19bd149
r18503:2415d5323e8d
r26123:a265ab51722a
r26856:c988753485d9
r18510:9d33e9643f80
r10696:8dfe83e30d01
r10696:8dfe83e30d01
r18794:ee2a72bc7637
r18794:ee2a72bc7637
r18794:ee2a72bc7637
r24326:510409fb8c32
r24326:510409fb8c32
r10696:8dfe83e30d01
r14248:a9050881acd7
r17142:9f8921b5eb4f
r12738:a48207b124c5
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r18363:f8c9df886df8
r18506:b0c2bbd32450
r18506:b0c2bbd32450
r18363:f8c9df886df8
r18363:f8c9df886df8
r18363:f8c9df886df8
r18506:b0c2bbd32450
r10696:8dfe83e30d01
r18441:63bc631b2d9b
r10696:8dfe83e30d01
r10696:8dfe83e30d01
r18774:2977cce7e239
r10696:8dfe83e30d01
r10696:8dfe83e30d01
r10696:8dfe83e30d01
r10696:8dfe83e30d01
r18506:b0c2bbd32450
r18506:b0c2bbd32450
r24326:510409fb8c32
r24326:510409fb8c32
r10696:8dfe83e30d01
r19539:8ff9f94e70e3
r12738:a48207b124c5
r12738:a48207b124c5
r10696:8dfe83e30d01
r10696:8dfe83e30d01
r18506:b0c2bbd32450
r11353:66f3a68f1504
r26641:01889b29735a
r26641:01889b29735a
r26641:01889b29735a
r26856:c988753485d9
r11702:a9dd3b59c546
r19209:9bc5f0b2cb37
r23607:36c15679007d
r14635:a75264d517b7
r11779:84d29f0087f7
r27365:849bb90cdbff
r18501:8e3d905ea4bc
r18501:8e3d905ea4bc
r11779:84d29f0087f7
r11702:a9dd3b59c546
r10696:8dfe83e30d01
r18507:a734d5918322
r18512:1adfa053a958
r18512:1adfa053a958
r23730:3b11f535de42
r18512:1adfa053a958
r18512:1adfa053a958
r18512:1adfa053a958
r27367:d55d6f0719f9
r18513:8c2a4637defb
r18513:8c2a4637defb
r18513:8c2a4637defb
r18513:8c2a4637defb
r27354:7a607c98c943
r18514:5b2abc88a36c
r18514:5b2abc88a36c
r18514:5b2abc88a36c
r18514:5b2abc88a36c
r18507:a734d5918322
r18507:a734d5918322
r26104:2873b00a1dac
r18507:a734d5918322
r26104:2873b00a1dac
r26123:a265ab51722a
r18507:a734d5918322
r26907:b8856ea20ba4
r18507:a734d5918322
r20236:61debbf7637d
r20236:61debbf7637d
r20236:61debbf7637d
r20236:61debbf7637d
r20236:61debbf7637d
r20236:61debbf7637d
r20236:61debbf7637d
r23607:36c15679007d
r20236:61debbf7637d
r26907:b8856ea20ba4
r23818:7609f20f2104
r23818:7609f20f2104
r18507:a734d5918322
r18507:a734d5918322
r26122:02442b0744ee
r18507:a734d5918322
r18507:a734d5918322
r18507:a734d5918322
/*
 * 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/>.
 */

/** @file ai_instance.cpp Implementation of AIInstance. */

#include "../stdafx.h"
#include "../debug.h"
#include "../error.h"

#include "../script/squirrel_class.hpp"

#include "ai_config.hpp"
#include "ai.hpp"

#include "../script/script_storage.hpp"
#include "../script/script_cmd.h"
#include "../script/script_gui.h"
#include "ai_info.hpp"
#include "ai_instance.hpp"

/* Manually include the Text glue. */
#include "../script/api/template/template_text.hpp.sq"

/* Convert all AI related classes to Squirrel data. */
#include "../script/api/ai/ai_includes.hpp"

#include "../company_base.h"
#include "../company_func.h"

#include "../safeguards.h"

AIInstance::AIInstance() :
	ScriptInstance("AI")
{}

void AIInstance::Initialize(AIInfo *info)
{
	this->versionAPI = info->GetAPIVersion();

	/* Register the AIController (including the "import" command) */
	SQAIController_Register(this->engine);

	ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), _current_company);
}

void AIInstance::RegisterAPI()
{
	ScriptInstance::RegisterAPI();

	/* Register all classes */
	SQAI_RegisterAll(this->engine);

	if (!this->LoadCompatibilityScripts(this->versionAPI, AI_DIR)) this->Died();
}

void AIInstance::Died()
{
	ScriptInstance::Died();

	/* Intro is not supposed to use AI, but it may have 'dummy' AI which instant dies. */
	if (_game_mode == GM_MENU) return;

	ShowScriptDebugWindow(_current_company);

	const AIInfo *info = AIConfig::GetConfig(_current_company, AIConfig::SSS_FORCE_GAME)->GetInfo();
	if (info != nullptr) {
		ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH, INVALID_STRING_ID, WL_WARNING);

		if (!info->GetURL().empty()) {
			ScriptLog::Info("Please report the error to the following URL:");
			ScriptLog::Info(info->GetURL());
		}
	}
}

void AIInstance::LoadDummyScript()
{
	ScriptAllocatorScope alloc_scope(this->engine);
	Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
}

int AIInstance::GetSetting(const std::string &name)
{
	return AIConfig::GetConfig(_current_company)->GetSetting(name);
}

ScriptInfo *AIInstance::FindLibrary(const std::string &library, int version)
{
	return (ScriptInfo *)AI::FindLibrary(library, version);
}

/**
 * DoCommand callback function for all commands executed by AIs.
 * @param cmd cmd as given to DoCommandPInternal.
 * @param result The result of the command.
 * @param data Command data as given to Command<>::Post.
 * @param result_data Additional returned data from the command.
 */
void CcAI(Commands cmd, const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data)
{
	/*
	 * 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);
	if (c == nullptr || c->ai_instance == nullptr) return;

	if (c->ai_instance->DoCommandCallback(result, data, std::move(result_data), cmd)) {
		c->ai_instance->Continue();
	}
}

CommandCallbackData *AIInstance::GetDoCommandCallback()
{
	return &CcAI;
}