OpenTTD Source: src/ai/ai_instance.cpp Source File

OpenTTD Source 15.3

ai_instance.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "../stdafx.h"
11#include "../debug.h"
12#include "../error.h"
13#include "../company_base.h"
14#include "../company_func.h"
15
16#include "../script/squirrel_class.hpp"
17
18#include "ai_config.hpp"
19#include "ai.hpp"
20
21#include "../script/script_storage.hpp"
22#include "../script/script_cmd.h"
23#include "../script/script_gui.h"
24#include "ai_info.hpp"
25#include "ai_instance.hpp"
26
27#include "table/strings.h"
28
29/* Manually include the Text glue. */
30#include "../script/api/template/template_text.sq.hpp"
31
32/* Convert all AI related classes to Squirrel data. */
33#include "../script/api/ai/ai_includes.hpp"
34
35#include "../safeguards.h"
36
37AIInstance::AIInstance() :
38 ScriptInstance("AI")
39{}
40
41 void AIInstance::Initialize(AIInfo *info)
42{
43 this->api_version = info->GetAPIVersion();
44
45 /* Register the AIController (including the "import" command) */
46 SQAIController_Register(*this->engine);
47
48 ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), _current_company);
49}
50
51 void AIInstance::RegisterAPI()
52{
53 ScriptInstance::RegisterAPI();
54
55 /* Register all classes */
56 SQAI_RegisterAll(*this->engine);
57
58 if (!this->LoadCompatibilityScripts(AI_DIR, AIInfo::ApiVersions)) this->Died();
59}
60
61 void AIInstance::Died()
62{
63 ScriptInstance::Died();
64
65 /* Intro is not supposed to use AI, but it may have 'dummy' AI which instant dies. */
66 if (_game_mode == GM_MENU) return;
67
68 /* Don't show errors while loading savegame. They will be shown at end of loading anyway. */
69 if (_switch_mode != SM_NONE) return;
70
71 ShowScriptDebugWindow(_current_company);
72
73 const AIInfo *info = AIConfig::GetConfig(_current_company)->GetInfo();
74 if (info != nullptr) {
75 ShowErrorMessage(GetEncodedString(STR_ERROR_AI_PLEASE_REPORT_CRASH), {}, WL_WARNING);
76
77 if (!info->GetURL().empty()) {
78 ScriptLog::Info("Please report the error to the following URL:");
79 ScriptLog::Info(info->GetURL());
80 }
81 }
82}
83
84 void AIInstance::LoadDummyScript()
85{
86 ScriptAllocatorScope alloc_scope(this->engine.get());
87 Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
88}
89
90 int AIInstance::GetSetting(const std::string &name)
91{
92 return AIConfig::GetConfig(_current_company)->GetSetting(name);
93}
94
95 ScriptInfo *AIInstance::FindLibrary(const std::string &library, int version)
96{
97 return (ScriptInfo *)AI::FindLibrary(library, version);
98}
99
107 void CcAI(Commands cmd, const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data)
108{
109 /*
110 * The company might not exist anymore. Check for this.
111 * The command checks are not useful since this callback
112 * is also called when the command fails, which is does
113 * when the company does not exist anymore.
114 */
115 const Company *c = Company::GetIfValid(_current_company);
116 if (c == nullptr || c->ai_instance == nullptr) return;
117
118 if (c->ai_instance->DoCommandCallback(result, data, std::move(result_data), cmd)) {
119 c->ai_instance->Continue();
120 }
121}
122
123 CommandCallbackData *AIInstance::GetDoCommandCallback()
124{
125 return &CcAI;
126}
ai.hpp
Base functions for all AIs.
ai_config.hpp
AIConfig stores the configuration settings of every AI.
ai_info.hpp
AIInfo keeps track of all information of an AI, like Author, Description, ...
ai_instance.hpp
The AIInstance tracks an AI.
AIConfig::GetConfig
static AIConfig * GetConfig(CompanyID company, ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition ai_config.cpp:20
AIInfo
All static information from an AI like name, version, etc.
Definition ai_info.hpp:16
AIInfo::GetAPIVersion
const std::string & GetAPIVersion() const
Get the API version this AI is written for.
Definition ai_info.hpp:51
AIInstance::RegisterAPI
void RegisterAPI() override
Register all API functions to the VM.
Definition ai_instance.cpp:51
AIInstance::GetSetting
int GetSetting(const std::string &name) override
Get the value of a setting of the current instance.
Definition ai_instance.cpp:90
AIInstance::GetDoCommandCallback
CommandCallbackData * GetDoCommandCallback() override
Get the callback handling DoCommands in case of networking.
Definition ai_instance.cpp:123
AIInstance::Initialize
void Initialize(class AIInfo *info)
Initialize the AI and prepare it for its first run.
Definition ai_instance.cpp:41
AIInstance::Died
void Died() override
Tell the script it died.
Definition ai_instance.cpp:61
AIInstance::LoadDummyScript
void LoadDummyScript() override
Load the dummy script.
Definition ai_instance.cpp:84
AIInstance::FindLibrary
ScriptInfo * FindLibrary(const std::string &library, int version) override
Find a library.
Definition ai_instance.cpp:95
AI::FindLibrary
static class AILibrary * FindLibrary(const std::string &library, int version)
Wrapper function for AIScanner::FindLibrary.
Definition ai_core.cpp:313
CommandCost
Common return value for all commands.
Definition command_type.h:24
ScriptAllocatorScope
Definition squirrel.hpp:299
ScriptConfig::GetSetting
int GetSetting(const std::string &name) const
Get the value of a setting for this config.
Definition script_config.cpp:83
ScriptInfo
All static information from an Script like name, version, etc.
Definition script_info.hpp:30
ScriptInfo::GetInstanceName
const std::string & GetInstanceName() const
Get the name of the instance of the script to create.
Definition script_info.hpp:65
ScriptInfo::GetURL
const std::string & GetURL() const
Get the website for this script.
Definition script_info.hpp:70
ScriptInfo::GetMainScript
const std::string & GetMainScript() const
Get the filename of the main.nut script.
Definition script_info.hpp:75
ScriptInstance
Runtime information about a script like a pointer to the squirrel vm and the current state.
Definition script_instance.hpp:25
ScriptInstance::LoadCompatibilityScripts
bool LoadCompatibilityScripts(Subdirectory dir, std::span< const std::string_view > api_versions)
Load squirrel scripts to emulate an older API.
Definition script_instance.cpp:125
ScriptInstance::RegisterAPI
virtual void RegisterAPI()
Register all API functions to the VM.
Definition script_instance.cpp:100
ScriptInstance::Died
virtual void Died()
Tell the script it died.
Definition script_instance.cpp:172
ScriptInstance::engine
std::unique_ptr< class Squirrel > engine
A wrapper around the squirrel vm.
Definition script_instance.hpp:259
ScriptInstance::api_version
std::string api_version
Current API used by this script.
Definition script_instance.hpp:260
ScriptInstance::Initialize
void Initialize(const std::string &main_script, const std::string &instance_name, CompanyID company)
Initialize the script and prepare it for its first run.
Definition script_instance.cpp:58
CommandCallbackData
void CommandCallbackData(Commands cmd, const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data)
Define a callback function for the client, after the command is finished.
Definition command_type.h:511
CommandDataBuffer
std::vector< uint8_t > CommandDataBuffer
Storage buffer for serialized command data.
Definition command_type.h:481
Commands
Commands
List of commands.
Definition command_type.h:196
_current_company
CompanyID _current_company
Company currently doing an action.
Definition company_cmd.cpp:56
WL_WARNING
@ WL_WARNING
Other information.
Definition error.h:25
ShowErrorMessage
void ShowErrorMessage(EncodedString &&summary_msg, int x, int y, CommandCost &cc)
Display an error message in a window.
Definition error_gui.cpp:296
AI_DIR
@ AI_DIR
Subdirectory for all AI files.
Definition fileio_type.h:99
_switch_mode
SwitchMode _switch_mode
The next mainloop command.
Definition gfx.cpp:49
ShowScriptDebugWindow
Window * ShowScriptDebugWindow(CompanyID show_company, bool new_window)
Open the Script debug window and select the given company.
Definition script_gui.cpp:1264
Script_CreateDummy
void Script_CreateDummy(HSQUIRRELVM vm, StringID string, std::string_view type)
Run the dummy AI and let it generate an error message.
Definition script_info_dummy.cpp:81
GetEncodedString
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Definition strings.cpp:90
Company
Definition company_base.h:127
Pool::PoolItem<&_company_pool >::GetIfValid
static Titem * GetIfValid(auto index)
Returns Titem with given index.
Definition pool_type.hpp:398