
# Conflicts: # src/animated_tile.cpp # src/cargopacket.h # src/cheat_gui.cpp # src/company_cmd.cpp # src/company_gui.cpp # src/date.cpp # src/disaster_vehicle.cpp # src/dock_gui.cpp # src/economy.cpp # src/engine.cpp # src/error_gui.cpp # src/fontcache/spritefontcache.cpp # src/game/game_gui.cpp # src/game/game_text.cpp # src/gfx.cpp # src/graph_gui.cpp # src/highscore_gui.cpp # src/industry_cmd.cpp # src/lang/dutch.txt # src/lang/english_AU.txt # src/lang/english_US.txt # src/lang/finnish.txt # src/lang/french.txt # src/lang/italian.txt # src/lang/portuguese.txt # src/lang/russian.txt # src/lang/turkish.txt # src/lang/vietnamese.txt # src/main_gui.cpp # src/misc_gui.cpp # src/network/network_gui.cpp # src/network/network_server.cpp # src/newgrf.cpp # src/newgrf.h # src/newgrf_generic.cpp # src/news_gui.cpp # src/openttd.cpp # src/os/unix/unix.cpp # src/os/windows/font_win32.cpp # src/os/windows/win32.cpp # src/rail_gui.cpp # src/road_gui.cpp # src/saveload/afterload.cpp # src/saveload/misc_sl.cpp # src/saveload/oldloader_sl.cpp # src/saveload/saveload.cpp # src/saveload/saveload.h # src/script/script_gui.cpp # src/settings_table.cpp # src/signs_gui.cpp # src/smallmap_gui.cpp # src/smallmap_gui.h # src/spritecache.cpp # src/spritecache.h # src/spriteloader/grf.cpp # src/station_cmd.cpp # src/statusbar_gui.cpp # src/stdafx.h # src/strgen/strgen_base.cpp # src/subsidy.cpp # src/table/settings/difficulty_settings.ini # src/texteff.cpp # src/timetable_cmd.cpp # src/timetable_gui.cpp # src/toolbar_gui.cpp # src/town_cmd.cpp # src/town_gui.cpp # src/townname.cpp # src/vehicle.cpp # src/waypoint_cmd.cpp # src/widgets/dropdown.cpp # src/window.cpp
123 lines
3.6 KiB
C++
123 lines
3.6 KiB
C++
/*
|
|
* 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_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", ScriptType::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() != nullptr) {
|
|
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 char *name)
|
|
{
|
|
return AIConfig::GetConfig(_current_company)->GetSetting(name);
|
|
}
|
|
|
|
ScriptInfo *AIInstance::FindLibrary(const char *library, int version)
|
|
{
|
|
return (ScriptInfo *)AI::FindLibrary(library, version);
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
* @param cmd cmd as given to DoCommandPInternal.
|
|
*/
|
|
void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint64 p3, uint32 cmd)
|
|
{
|
|
/*
|
|
* 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, tile, p1, p2, p3, cmd)) {
|
|
c->ai_instance->Continue();
|
|
}
|
|
}
|
|
|
|
CommandCallback *AIInstance::GetDoCommandCallback()
|
|
{
|
|
return &CcAI;
|
|
}
|