Turn a conditional into an assertion in ConLoad
[openttd/fttd.git] / src / ai / ai_instance.cpp
blobe76ebfc93ef650a9060f81b4c4d12bdc9145b09a
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file ai_instance.cpp Implementation of AIInstance. */
12 #include "../stdafx.h"
13 #include "../debug.h"
14 #include "../error.h"
16 #include "../script/convert.hpp"
18 #include "ai_config.hpp"
19 #include "ai_gui.hpp"
20 #include "ai.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() :
35 ScriptInstance("AI")
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 */
56 SQAI_Register (this);
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();
68 if (info != NULL) {
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 {
84 const char *literal;
85 const char *message;
88 static WChar dummy_script_reader (SQUserPointer userdata)
90 DummyScriptHelper *data = (DummyScriptHelper*) userdata;
92 if (data->literal != NULL) {
93 unsigned char c = *data->literal;
94 if (c != 0) {
95 assert (c < '\x7f');
96 data->literal++;
97 return c;
99 if (data->message == NULL) return 0;
100 data->literal = NULL;
103 assert (data->message != NULL);
105 WChar c = Utf8Consume (&data->message);
107 switch (c) {
108 case '\0': /* switch to tail literal string */
109 data->message = NULL;
110 data->literal = dummy_script_tail;
111 break;
113 case '\n': /* switch to newline literal string */
114 data->literal = dummy_script_newline;
115 break;
117 case '"': /* escape special chars */
118 data->literal = "\\\"";
119 break;
121 case '\\': /* escape special chars */
122 data->literal = "\\\\";
123 break;
125 default: return c;
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 };
140 SQRESULT res;
142 sq_pushroottable (vm);
143 res = sq_compile (vm, dummy_script_reader, &data, "dummy", SQTrue);
144 assert (SQ_SUCCEEDED(res));
146 sq_push (vm, -2);
147 res = sq_call (vm, 1, SQFalse, SQTrue);
148 assert (SQ_SUCCEEDED(res));
150 sq_pop (vm, 1);
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()
184 return CMDSRC_AI;