OpenTTD Source  13.2.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_gui.hpp"
18 #include "ai.hpp"
19 
20 #include "../script/script_storage.hpp"
21 #include "../script/script_cmd.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 
41 {
42  this->versionAPI = info->GetAPIVersion();
43 
44  /* Register the AIController (including the "import" command) */
45  SQAIController_Register(this->engine);
46 
48 }
49 
51 {
53 
54  /* Register all classes */
55  SQAI_RegisterAll(this->engine);
56 
57  if (!this->LoadCompatibilityScripts(this->versionAPI, AI_DIR)) this->Died();
58 }
59 
61 {
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 
68 
70  if (info != nullptr) {
71  ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH, INVALID_STRING_ID, WL_WARNING);
72 
73  if (info->GetURL() != nullptr) {
74  ScriptLog::Info("Please report the error to the following URL:");
75  ScriptLog::Info(info->GetURL());
76  }
77  }
78 }
79 
81 {
82  ScriptAllocatorScope alloc_scope(this->engine);
83  extern void Script_CreateDummy(HSQUIRRELVM vm, StringID string, const char *type);
84  Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
85 }
86 
87 int AIInstance::GetSetting(const char *name)
88 {
90 }
91 
92 ScriptInfo *AIInstance::FindLibrary(const char *library, int version)
93 {
94  return (ScriptInfo *)AI::FindLibrary(library, version);
95 }
96 
104 void CcAI(Commands cmd, const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data)
105 {
106  /*
107  * The company might not exist anymore. Check for this.
108  * The command checks are not useful since this callback
109  * is also called when the command fails, which is does
110  * when the company does not exist anymore.
111  */
113  if (c == nullptr || c->ai_instance == nullptr) return;
114 
115  if (c->ai_instance->DoCommandCallback(result, data, std::move(result_data), cmd)) {
116  c->ai_instance->Continue();
117  }
118 }
119 
121 {
122  return &CcAI;
123 }
AI::FindLibrary
static class AILibrary * FindLibrary(const char *library, int version)
Wrapper function for AIScanner::FindLibrary.
Definition: ai_core.cpp:330
ScriptInstance::engine
class Squirrel * engine
A wrapper around the squirrel vm.
Definition: script_instance.hpp:249
AIInstance::GetDoCommandCallback
CommandCallbackData * GetDoCommandCallback() override
Get the callback handling DoCommands in case of networking.
Definition: ai_instance.cpp:120
ScriptInfo::GetMainScript
const char * GetMainScript() const
Get the filename of the main.nut script.
Definition: script_info.hpp:90
Pool::PoolItem<&_company_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:348
AIInfo::GetAPIVersion
const char * GetAPIVersion() const
Get the API version this AI is written for.
Definition: ai_info.hpp:49
Commands
Commands
List of commands.
Definition: command_type.h:176
WL_WARNING
@ WL_WARNING
Other information.
Definition: error.h:23
CommandDataBuffer
std::vector< byte > CommandDataBuffer
Storage buffer for serialized command data.
Definition: command_type.h:451
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:104
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:58
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
ai_gui.hpp
ScriptInfo::GetURL
const char * GetURL() const
Get the website for this script.
Definition: script_info.hpp:85
Squirrel::GetVM
HSQUIRRELVM GetVM()
Get the squirrel VM.
Definition: squirrel.hpp:80
ShowErrorMessage
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=nullptr, uint textref_stack_size=0, const uint32 *textref_stack=nullptr)
Display an error message in a window.
Definition: error_gui.cpp:377
ai.hpp
ai_info.hpp
CommandCost
Common return value for all commands.
Definition: command_type.h:24
AI_DIR
@ AI_DIR
Subdirectory for all AI files.
Definition: fileio_type.h:119
AIInstance::LoadDummyScript
void LoadDummyScript() override
Load the dummy script.
Definition: ai_instance.cpp:80
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:45
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:481
ScriptInstance::RegisterAPI
virtual void RegisterAPI()
Register all API functions to the VM.
Definition: script_instance.cpp:111
ScriptInstance::Initialize
void Initialize(const char *main_script, const char *instance_name, CompanyID company)
Initialize the script and prepare it for its first run.
Definition: script_instance.cpp:71
ScriptInfo::GetInstanceName
const char * GetInstanceName() const
Get the name of the instance of the script to create.
Definition: script_info.hpp:80
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
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:747
AIInstance::FindLibrary
ScriptInfo * FindLibrary(const char *library, int version) override
Find a library.
Definition: ai_instance.cpp:92
ShowAIDebugWindow
Window * ShowAIDebugWindow(CompanyID show_company)
Open the AI debug window and select the given company.
Definition: ai_gui.cpp:1469
ScriptInstance::Died
virtual void Died()
Tell the script it died.
Definition: script_instance.cpp:155
ScriptInstance::Continue
void Continue()
A script in multiplayer waits for the server to handle its DoCommand.
Definition: script_instance.cpp:149
AIInfo
All static information from an AI like name, version, etc.
Definition: ai_info.hpp:16
ScriptInstance::LoadCompatibilityScripts
bool LoadCompatibilityScripts(const char *api_version, Subdirectory dir)
Load squirrel scripts to emulate an older API.
Definition: script_instance.cpp:117
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
ScriptInstance::versionAPI
const char * versionAPI
Current API used by this script.
Definition: script_instance.hpp:250
Company
Definition: company_base.h:117
ScriptConfig::SSS_FORCE_GAME
@ SSS_FORCE_GAME
Get the Script config from the current game.
Definition: script_config.hpp:110
AIConfig::GetSetting
int GetSetting(const char *name) const override
Get the value of a setting for this config.
Definition: ai_config.cpp:89
AIInstance::GetSetting
int GetSetting(const char *name) override
Get the value of a setting of the current instance.
Definition: ai_instance.cpp:87
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