OpenTTD Source 20250612-master-gb012d9e3dc
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 <http://www.gnu.org/licenses/>.
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
42{
43 this->api_version = info->GetAPIVersion();
44
45 /* Register the AIController (including the "import" command) */
46 SQAIController_Register(*this->engine);
47
49}
50
52{
54
55 /* Register all classes */
56 SQAI_RegisterAll(*this->engine);
57
58 if (!this->LoadCompatibilityScripts(AI_DIR, AIInfo::ApiVersions)) this->Died();
59}
60
62{
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
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
85{
86 ScriptAllocatorScope alloc_scope(this->engine);
87 Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
88}
89
90int AIInstance::GetSetting(const std::string &name)
91{
93}
94
95ScriptInfo *AIInstance::FindLibrary(const std::string &library, int version)
96{
97 return (ScriptInfo *)AI::FindLibrary(library, version);
98}
99
107void 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 */
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
Base functions for all AIs.
AIConfig stores the configuration settings of every AI.
AIInfo keeps track of all information of an AI, like Author, Description, ...
The AIInstance tracks an AI.
static AIConfig * GetConfig(CompanyID company, ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition ai_config.cpp:20
All static information from an AI like name, version, etc.
Definition ai_info.hpp:16
const std::string & GetAPIVersion() const
Get the API version this AI is written for.
Definition ai_info.hpp:51
void RegisterAPI() override
Register all API functions to the VM.
int GetSetting(const std::string &name) override
Get the value of a setting of the current instance.
CommandCallbackData * GetDoCommandCallback() override
Get the callback handling DoCommands in case of networking.
void Initialize(class AIInfo *info)
Initialize the AI and prepare it for its first run.
void Died() override
Tell the script it died.
void LoadDummyScript() override
Load the dummy script.
ScriptInfo * FindLibrary(const std::string &library, int version) override
Find a library.
static class AILibrary * FindLibrary(const std::string &library, int version)
Wrapper function for AIScanner::FindLibrary.
Definition ai_core.cpp:313
Common return value for all commands.
int GetSetting(const std::string &name) const
Get the value of a setting for this config.
All static information from an Script like name, version, etc.
const std::string & GetInstanceName() const
Get the name of the instance of the script to create.
const std::string & GetURL() const
Get the website for this script.
const std::string & GetMainScript() const
Get the filename of the main.nut script.
Runtime information about a script like a pointer to the squirrel vm and the current state.
bool LoadCompatibilityScripts(Subdirectory dir, std::span< const std::string_view > api_versions)
Load squirrel scripts to emulate an older API.
virtual void RegisterAPI()
Register all API functions to the VM.
virtual void Died()
Tell the script it died.
class Squirrel * engine
A wrapper around the squirrel vm.
std::string api_version
Current API used by this script.
void Initialize(const std::string &main_script, const std::string &instance_name, CompanyID company)
Initialize the script and prepare it for its first run.
HSQUIRRELVM GetVM()
Get the squirrel VM.
Definition squirrel.hpp:82
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.
std::vector< uint8_t > CommandDataBuffer
Storage buffer for serialized command data.
Commands
List of commands.
CompanyID _current_company
Company currently doing an action.
@ WL_WARNING
Other information.
Definition error.h:25
void ShowErrorMessage(EncodedString &&summary_msg, int x, int y, CommandCost &cc)
Display an error message in a window.
@ AI_DIR
Subdirectory for all AI files.
Definition fileio_type.h:99
SwitchMode _switch_mode
The next mainloop command.
Definition gfx.cpp:49
Window * ShowScriptDebugWindow(CompanyID show_company, bool new_window)
Open the Script debug window and select the given company.
void Script_CreateDummy(HSQUIRRELVM vm, StringID string, std::string_view type)
Run the dummy AI and let it generate an error message.
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Definition strings.cpp:91
static Titem * GetIfValid(auto index)
Returns Titem with given index.