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

OpenTTD Source 12.2

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 "ai_info.hpp"
22 #include "ai_instance.hpp"
23
24 /* Manually include the Text glue. */
25 #include "../script/api/template/template_text.hpp.sq"
26
27 /* Convert all AI related classes to Squirrel data. */
28 #include "../script/api/ai/ai_includes.hpp"
29
30 #include "../company_base.h"
31 #include "../company_func.h"
32
33 #include "../safeguards.h"
34
35 AIInstance::AIInstance() :
36 ScriptInstance("AI")
37 {}
38
39 void AIInstance::Initialize(AIInfo *info)
40 {
41 this->versionAPI = info->GetAPIVersion();
42
43 /* Register the AIController (including the "import" command) */
44 SQAIController_Register(this->engine);
45
46 ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), _current_company);
47 }
48
49 void AIInstance::RegisterAPI()
50 {
51 ScriptInstance::RegisterAPI();
52
53 /* Register all classes */
54 SQAI_RegisterAll(this->engine);
55
56 if (!this->LoadCompatibilityScripts(this->versionAPI, AI_DIR)) this->Died();
57 }
58
59 void AIInstance::Died()
60 {
61 ScriptInstance::Died();
62
63 ShowAIDebugWindow(_current_company);
64
65 const AIInfo *info = AIConfig::GetConfig(_current_company, AIConfig::SSS_FORCE_GAME)->GetInfo();
66 if (info != nullptr) {
67 ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH, INVALID_STRING_ID, WL_WARNING);
68
69 if (info->GetURL() != nullptr) {
70 ScriptLog::Info("Please report the error to the following URL:");
71 ScriptLog::Info(info->GetURL());
72 }
73 }
74 }
75
76 void AIInstance::LoadDummyScript()
77 {
78 ScriptAllocatorScope alloc_scope(this->engine);
79 extern void Script_CreateDummy(HSQUIRRELVM vm, StringID string, const char *type);
80 Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
81 }
82
83 int AIInstance::GetSetting(const char *name)
84 {
85 return AIConfig::GetConfig(_current_company)->GetSetting(name);
86 }
87
88 ScriptInfo *AIInstance::FindLibrary(const char *library, int version)
89 {
90 return (ScriptInfo *)AI::FindLibrary(library, version);
91 }
92
101 void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
102 {
103 /*
104 * The company might not exist anymore. Check for this.
105 * The command checks are not useful since this callback
106 * is also called when the command fails, which is does
107 * when the company does not exist anymore.
108 */
109 const Company *c = Company::GetIfValid(_current_company);
110 if (c == nullptr || c->ai_instance == nullptr) return;
111
112 if (c->ai_instance->DoCommandCallback(result, tile, p1, p2, cmd)) {
113 c->ai_instance->Continue();
114 }
115 }
116
117 CommandCallback *AIInstance::GetDoCommandCallback()
118 {
119 return &CcAI;
120 }
AI::FindLibrary
static class AILibrary * FindLibrary(const char *library, int version)
Wrapper function for AIScanner::FindLibrary.
Definition: ai_core.cpp:343
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
ScriptInstance::engine
class Squirrel * engine
A wrapper around the squirrel vm.
Definition: script_instance.hpp:215
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
ScriptInstance::DoCommandCallback
bool DoCommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
DoCommand callback function for all commands executed by scripts.
Definition: script_instance.cpp:690
WL_WARNING
@ WL_WARNING
Other information.
Definition: error.h:23
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:23
AIInstance::Initialize
void Initialize(class AIInfo *info)
Initialize the AI and prepare it for its first run.
Definition: ai_instance.cpp:39
ai_gui.hpp
CommandCallback
void CommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Define a callback function for the client, after the command is finished.
Definition: command_type.h:474
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:383
ai.hpp
ai_info.hpp
CommandCost
Common return value for all commands.
Definition: command_type.h:23
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:76
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
ScriptInstance::RegisterAPI
virtual void RegisterAPI()
Register all API functions to the VM.
Definition: script_instance.cpp:109
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:69
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:59
AIInstance::FindLibrary
ScriptInfo * FindLibrary(const char *library, int version) override
Find a library.
Definition: ai_instance.cpp:88
ShowAIDebugWindow
Window * ShowAIDebugWindow(CompanyID show_company)
Open the AI debug window and select the given company.
Definition: ai_gui.cpp:1537
ScriptInstance::Died
virtual void Died()
Tell the script it died.
Definition: script_instance.cpp:153
ScriptInstance::Continue
void Continue()
A script in multiplayer waits for the server to handle its DoCommand.
Definition: script_instance.cpp:147
AIInfo
All static information from an AI like name, version, etc.
Definition: ai_info.hpp:16
AIInstance::GetDoCommandCallback
CommandCallback * GetDoCommandCallback() override
Get the callback handling DoCommands in case of networking.
Definition: ai_instance.cpp:117
ScriptInstance::LoadCompatibilityScripts
bool LoadCompatibilityScripts(const char *api_version, Subdirectory dir)
Load squirrel scripts to emulate an older API.
Definition: script_instance.cpp:115
CcAI
void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
DoCommand callback function for all commands executed by AIs.
Definition: ai_instance.cpp:101
AIInstance::RegisterAPI
void RegisterAPI() override
Register all API functions to the VM.
Definition: ai_instance.cpp:49
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:216
Company
Definition: company_base.h:115
ScriptConfig::SSS_FORCE_GAME
@ SSS_FORCE_GAME
Get the Script config from the current game.
Definition: script_config.hpp:105
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:83
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