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

OpenTTD Source 14.1

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
14 #include "../script/squirrel_class.hpp"
15
16 #include "ai_config.hpp"
17 #include "ai.hpp"
18
19 #include "../script/script_storage.hpp"
20 #include "../script/script_cmd.h"
21 #include "../script/script_gui.h"
22 #include "ai_info.hpp"
23 #include "ai_instance.hpp"
24
25 /* Manually include the Text glue. */
26 #include "../script/api/template/template_text.hpp.sq"
27
28 /* Convert all AI related classes to Squirrel data. */
29 #include "../script/api/ai/ai_includes.hpp"
30
31 #include "../company_base.h"
32 #include "../company_func.h"
33
34 #include "../safeguards.h"
35
36 AIInstance::AIInstance() :
37 ScriptInstance("AI")
38 {}
39
40 void AIInstance::Initialize(AIInfo *info)
41 {
42 this->versionAPI = info->GetAPIVersion();
43
44 /* Register the AIController (including the "import" command) */
45 SQAIController_Register(this->engine);
46
47 ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), _current_company);
48 }
49
50 void AIInstance::RegisterAPI()
51 {
52 ScriptInstance::RegisterAPI();
53
54 /* Register all classes */
55 SQAI_RegisterAll(this->engine);
56
57 if (!this->LoadCompatibilityScripts(this->versionAPI, AI_DIR)) this->Died();
58 }
59
60 void AIInstance::Died()
61 {
62 ScriptInstance::Died();
63
64 /* Intro is not supposed to use AI, but it may have 'dummy' AI which instant dies. */
65 if (_game_mode == GM_MENU) return;
66
67 /* Don't show errors while loading savegame. They will be shown at end of loading anyway. */
68 if (_switch_mode != SM_NONE) return;
69
70 ShowScriptDebugWindow(_current_company);
71
72 const AIInfo *info = AIConfig::GetConfig(_current_company)->GetInfo();
73 if (info != nullptr) {
74 ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH, INVALID_STRING_ID, WL_WARNING);
75
76 if (!info->GetURL().empty()) {
77 ScriptLog::Info("Please report the error to the following URL:");
78 ScriptLog::Info(info->GetURL());
79 }
80 }
81 }
82
83 void AIInstance::LoadDummyScript()
84 {
85 ScriptAllocatorScope alloc_scope(this->engine);
86 Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
87 }
88
89 int AIInstance::GetSetting(const std::string &name)
90 {
91 return AIConfig::GetConfig(_current_company)->GetSetting(name);
92 }
93
94 ScriptInfo *AIInstance::FindLibrary(const std::string &library, int version)
95 {
96 return (ScriptInfo *)AI::FindLibrary(library, version);
97 }
98
106 void CcAI(Commands cmd, const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data)
107 {
108 /*
109 * The company might not exist anymore. Check for this.
110 * The command checks are not useful since this callback
111 * is also called when the command fails, which is does
112 * when the company does not exist anymore.
113 */
114 const Company *c = Company::GetIfValid(_current_company);
115 if (c == nullptr || c->ai_instance == nullptr) return;
116
117 if (c->ai_instance->DoCommandCallback(result, data, std::move(result_data), cmd)) {
118 c->ai_instance->Continue();
119 }
120 }
121
122 CommandCallbackData *AIInstance::GetDoCommandCallback()
123 {
124 return &CcAI;
125 }
ShowScriptDebugWindow
Window * ShowScriptDebugWindow(CompanyID show_company, bool new_window)
Open the Script debug window and select the given company.
Definition: script_gui.cpp:1303
AIInstance::GetSetting
int GetSetting(const std::string &name) override
Get the value of a setting of the current instance.
Definition: ai_instance.cpp:89
ScriptInstance::engine
class Squirrel * engine
A wrapper around the squirrel vm.
Definition: script_instance.hpp:254
AIInstance::GetDoCommandCallback
CommandCallbackData * GetDoCommandCallback() override
Get the callback handling DoCommands in case of networking.
Definition: ai_instance.cpp:122
ShowErrorMessage
void ShowErrorMessage(StringID summary_msg, int x, int y, CommandCost cc)
Display an error message in a window.
Definition: error_gui.cpp:367
AIInfo::GetAPIVersion
const std::string & GetAPIVersion() const
Get the API version this AI is written for.
Definition: ai_info.hpp:48
Pool::PoolItem<&_company_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:350
WL_WARNING
@ WL_WARNING
Other information.
Definition: error.h:25
CommandDataBuffer
std::vector< byte > CommandDataBuffer
Storage buffer for serialized command data.
Definition: command_type.h:465
CcAI
void CcAI(Commands cmd, const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data)
DoCommand callback function for all commands executed by AIs.
Definition: ai_instance.cpp:106
ScriptInstance
Runtime information about a script like a pointer to the squirrel vm and the current state.
Definition: script_instance.hpp:25
AIInstance::Initialize
void Initialize(class AIInfo *info)
Initialize the AI and prepare it for its first run.
Definition: ai_instance.cpp:40
ScriptInfo::GetMainScript
const std::string & GetMainScript() const
Get the filename of the main.nut script.
Definition: script_info.hpp:81
Squirrel::GetVM
HSQUIRRELVM GetVM()
Get the squirrel VM.
Definition: squirrel.hpp:80
Script_CreateDummy
void Script_CreateDummy(HSQUIRRELVM vm, StringID string, const char *type)
Run the dummy AI and let it generate an error message.
Definition: script_info_dummy.cpp:80
ai.hpp
ai_info.hpp
CommandCost
Common return value for all commands.
Definition: command_type.h:23
ScriptInfo::GetURL
const std::string & GetURL() const
Get the website for this script.
Definition: script_info.hpp:76
AI_DIR
@ AI_DIR
Subdirectory for all AI files.
Definition: fileio_type.h:119
AIInstance::FindLibrary
ScriptInfo * FindLibrary(const std::string &library, int version) override
Find a library.
Definition: ai_instance.cpp:94
AIInstance::LoadDummyScript
void LoadDummyScript() override
Load the dummy script.
Definition: ai_instance.cpp:83
ScriptAllocatorScope
Definition: squirrel.hpp:288
ai_instance.hpp
AIConfig::GetConfig
static AIConfig * GetConfig(CompanyID company, ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition: ai_config.cpp:20
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:495
ScriptInstance::RegisterAPI
virtual void RegisterAPI()
Register all API functions to the VM.
Definition: script_instance.cpp:114
AI::FindLibrary
static class AILibrary * FindLibrary(const std::string &library, int version)
Wrapper function for AIScanner::FindLibrary.
Definition: ai_core.cpp:331
ScriptInstance::versionAPI
std::string versionAPI
Current API used by this script.
Definition: script_instance.hpp:255
_switch_mode
SwitchMode _switch_mode
The next mainloop command.
Definition: gfx.cpp:48
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:51
ScriptInstance::LoadCompatibilityScripts
bool LoadCompatibilityScripts(const std::string &api_version, Subdirectory dir)
Load squirrel scripts to emulate an older API.
Definition: script_instance.cpp:119
AIInstance::Died
void Died() override
Tell the script it died.
Definition: ai_instance.cpp:60
ScriptInstance::DoCommandCallback
bool DoCommandCallback(const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data, Commands cmd)
DoCommand callback function for all commands executed by scripts.
Definition: script_instance.cpp:754
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:70
ScriptConfig::GetSetting
int GetSetting(const std::string &name) const
Get the value of a setting for this config.
Definition: script_config.cpp:82
ScriptInstance::Died
virtual void Died()
Tell the script it died.
Definition: script_instance.cpp:156
ScriptInstance::Continue
void Continue()
A script in multiplayer waits for the server to handle its DoCommand.
Definition: script_instance.cpp:150
AIInfo
All static information from an AI like name, version, etc.
Definition: ai_info.hpp:16
Commands
Commands
List of commands.
Definition: command_type.h:187
ScriptInfo::GetInstanceName
const std::string & GetInstanceName() const
Get the name of the instance of the script to create.
Definition: script_info.hpp:71
AIInstance::RegisterAPI
void RegisterAPI() override
Register all API functions to the VM.
Definition: ai_instance.cpp:50
ScriptInfo
All static information from an Script like name, version, etc.
Definition: script_info.hpp:30
Company
Definition: company_base.h:129
INVALID_STRING_ID
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
ai_config.hpp