4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file ai_instance.cpp Implementation of AIInstance. */
12 #include "../stdafx.h"
16 #include "../script/convert.hpp"
18 #include "ai_config.hpp"
22 #include "ai_info.hpp"
23 #include "ai_instance.hpp"
25 /* Manually include the Text glue. */
26 #include "../script/api/script_text.hpp"
27 #include "../script/api/script_controller.hpp"
29 #include "../script/api/ai/ai.hpp.sq"
31 #include "../company_base.h"
32 #include "../company_func.h"
34 AIInstance::AIInstance() :
38 static void LoadDummyScript (HSQUIRRELVM vm
);
40 void AIInstance::Initialize (const AIInfo
*info
)
42 this->versionAPI
= info
->GetAPIVersion();
44 /* Register the AIController (including the "import" command) */
45 SQAIController_Register (this);
47 ScriptInstance::Initialize (info
, _current_company
,
48 info
->use
== AIInfo::USE_DUMMY
? &LoadDummyScript
: NULL
);
51 void AIInstance::RegisterAPI()
53 ScriptInstance::RegisterAPI();
55 /* Register all classes */
58 if (!this->LoadCompatibilityScripts(this->versionAPI
, AI_DIR
)) this->Died();
61 void AIInstance::Died()
63 ScriptInstance::Died();
65 ShowAIDebugWindow(_current_company
);
67 const AIInfo
*info
= AIConfig::GetConfig(_current_company
, AIConfig::SSS_FORCE_GAME
)->GetInfo();
69 ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH
, INVALID_STRING_ID
, WL_WARNING
);
71 if (info
->GetURL() != NULL
) {
72 ScriptLog::Info("Please report the error to the following URL:");
73 ScriptLog::Info(info
->GetURL());
78 static const char dummy_script_head
[] =
79 "class DummyAI extends AIController { function Start() { AILog.Error (\"";
80 static const char dummy_script_newline
[] = "\"); AILog.Error (\"";
81 static const char dummy_script_tail
[] = "\"); } }";
83 struct DummyScriptHelper
{
88 static WChar
dummy_script_reader (SQUserPointer userdata
)
90 DummyScriptHelper
*data
= (DummyScriptHelper
*) userdata
;
92 if (data
->literal
!= NULL
) {
93 unsigned char c
= *data
->literal
;
99 if (data
->message
== NULL
) return 0;
100 data
->literal
= NULL
;
103 assert (data
->message
!= NULL
);
105 WChar c
= Utf8Consume (&data
->message
);
108 case '\0': /* switch to tail literal string */
109 data
->message
= NULL
;
110 data
->literal
= dummy_script_tail
;
113 case '\n': /* switch to newline literal string */
114 data
->literal
= dummy_script_newline
;
117 case '"': /* escape special chars */
118 data
->literal
= "\\\"";
121 case '\\': /* escape special chars */
122 data
->literal
= "\\\\";
128 return *data
->literal
++;
131 static void LoadDummyScript (HSQUIRRELVM vm
)
133 /* Get the (translated) error message. */
134 char error_message
[1024];
135 GetString (error_message
, STR_ERROR_AI_NO_AI_FOUND
);
137 /* Load and run a dummy script. */
138 DummyScriptHelper data
= { dummy_script_head
, error_message
};
142 sq_pushroottable (vm
);
143 res
= sq_compile (vm
, dummy_script_reader
, &data
, "dummy", SQTrue
);
144 assert (SQ_SUCCEEDED(res
));
147 res
= sq_call (vm
, 1, SQFalse
, SQTrue
);
148 assert (SQ_SUCCEEDED(res
));
153 int AIInstance::GetSetting(const char *name
)
155 return AIConfig::GetConfig(_current_company
)->GetSetting(name
);
158 ScriptInfo
*AIInstance::FindLibrary(const char *library
, int version
)
160 return (ScriptInfo
*)AI::FindLibrary(library
, version
);
164 * DoCommand callback function for all commands executed by AIs.
165 * @param result The result of the command.
167 void CcAI (const CommandCost
&result
)
170 * The company might not exist anymore. Check for this.
171 * The command checks are not useful since this callback
172 * is also called when the command fails, which is does
173 * when the company does not exist anymore.
175 const Company
*c
= Company::GetIfValid(_current_company
);
176 if (c
== NULL
|| c
->ai_instance
== NULL
) return;
178 c
->ai_instance
->DoCommandCallback (result
);
179 c
->ai_instance
->Continue();
182 CommandSource
AIInstance::GetCommandSource()