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/>.
8 /** @file ai_instance.cpp Implementation of AIInstance. */
10 #include "../stdafx.h"
14 #include "../script/squirrel_class.hpp"
16 #include "ai_config.hpp"
20 #include "../script/script_storage.hpp"
21 #include "ai_info.hpp"
22 #include "ai_instance.hpp"
24 /* Manually include the Text glue. */
25 #include "../script/api/template/template_text.hpp.sq"
27 /* Convert all AI related classes to Squirrel data. */
28 #include "../script/api/ai/ai_includes.hpp"
30 #include "../company_base.h"
31 #include "../company_func.h"
33 #include "../safeguards.h"
35 AIInstance::AIInstance() :
39 void AIInstance::Initialize(AIInfo
*info
)
41 this->versionAPI
= info
->GetAPIVersion();
43 /* Register the AIController (including the "import" command) */
44 SQAIController_Register(this->engine
);
46 ScriptInstance::Initialize(info
->GetMainScript(), info
->GetInstanceName(), _current_company
);
49 void AIInstance::RegisterAPI()
51 ScriptInstance::RegisterAPI();
53 /* Register all classes */
54 SQAI_RegisterAll(this->engine
);
56 if (!this->LoadCompatibilityScripts(this->versionAPI
, AI_DIR
)) this->Died();
59 void AIInstance::Died()
61 ScriptInstance::Died();
63 ShowAIDebugWindow(_current_company
);
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
);
69 if (info
->GetURL() != nullptr) {
70 ScriptLog::Info("Please report the error to the following URL:");
71 ScriptLog::Info(info
->GetURL());
76 void AIInstance::LoadDummyScript()
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");
83 int AIInstance::GetSetting(const char *name
)
85 return AIConfig::GetConfig(_current_company
)->GetSetting(name
);
88 ScriptInfo
*AIInstance::FindLibrary(const char *library
, int version
)
90 return (ScriptInfo
*)AI::FindLibrary(library
, version
);
94 * DoCommand callback function for all commands executed by AIs.
95 * @param result The result of the command.
96 * @param tile The tile on which the command was executed.
97 * @param p1 p1 as given to DoCommandPInternal.
98 * @param p2 p2 as given to DoCommandPInternal.
99 * @param cmd cmd as given to DoCommandPInternal.
101 void CcAI(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
, uint32 cmd
)
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.
109 const Company
*c
= Company::GetIfValid(_current_company
);
110 if (c
== nullptr || c
->ai_instance
== nullptr) return;
112 if (c
->ai_instance
->DoCommandCallback(result
, tile
, p1
, p2
, cmd
)) {
113 c
->ai_instance
->Continue();
117 CommandCallback
*AIInstance::GetDoCommandCallback()