Fix #10490: Allow ships to exit depots if another is not moving at the exit point...
[openttd-github.git] / src / ai / ai_instance.cpp
blobdb4db4b81950bd6c9529e15f8e2431b0a1a86027
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 */
8 /** @file ai_instance.cpp Implementation of AIInstance. */
10 #include "../stdafx.h"
11 #include "../debug.h"
12 #include "../error.h"
14 #include "../script/squirrel_class.hpp"
16 #include "ai_config.hpp"
17 #include "ai.hpp"
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"
25 /* Manually include the Text glue. */
26 #include "../script/api/template/template_text.hpp.sq"
28 /* Convert all AI related classes to Squirrel data. */
29 #include "../script/api/ai/ai_includes.hpp"
31 #include "../company_base.h"
32 #include "../company_func.h"
34 #include "../safeguards.h"
36 AIInstance::AIInstance() :
37 ScriptInstance("AI")
40 void AIInstance::Initialize(AIInfo *info)
42 this->versionAPI = info->GetAPIVersion();
44 /* Register the AIController (including the "import" command) */
45 SQAIController_Register(this->engine);
47 ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), _current_company);
50 void AIInstance::RegisterAPI()
52 ScriptInstance::RegisterAPI();
54 /* Register all classes */
55 SQAI_RegisterAll(this->engine);
57 if (!this->LoadCompatibilityScripts(this->versionAPI, AI_DIR)) this->Died();
60 void AIInstance::Died()
62 ScriptInstance::Died();
64 /* Intro is not supposed to use AI, but it may have 'dummy' AI which instant dies. */
65 if (_game_mode == GM_MENU) return;
67 /* Don't show errors while loading savegame. They will be shown at end of loading anyway. */
68 if (_switch_mode != SM_NONE) return;
70 ShowScriptDebugWindow(_current_company);
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);
76 if (!info->GetURL().empty()) {
77 ScriptLog::Info("Please report the error to the following URL:");
78 ScriptLog::Info(info->GetURL());
83 void AIInstance::LoadDummyScript()
85 ScriptAllocatorScope alloc_scope(this->engine);
86 Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
89 int AIInstance::GetSetting(const std::string &name)
91 return AIConfig::GetConfig(_current_company)->GetSetting(name);
94 ScriptInfo *AIInstance::FindLibrary(const std::string &library, int version)
96 return (ScriptInfo *)AI::FindLibrary(library, version);
99 /**
100 * DoCommand callback function for all commands executed by AIs.
101 * @param cmd cmd as given to DoCommandPInternal.
102 * @param result The result of the command.
103 * @param data Command data as given to Command<>::Post.
104 * @param result_data Additional returned data from the command.
106 void CcAI(Commands cmd, const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data)
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.
114 const Company *c = Company::GetIfValid(_current_company);
115 if (c == nullptr || c->ai_instance == nullptr) return;
117 if (c->ai_instance->DoCommandCallback(result, data, std::move(result_data), cmd)) {
118 c->ai_instance->Continue();
122 CommandCallbackData *AIInstance::GetDoCommandCallback()
124 return &CcAI;