summaryrefslogtreecommitdiff
path: root/src/ai/ai_info_dummy.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/ai/ai_info_dummy.cpp')
-rw-r--r--src/ai/ai_info_dummy.cpp54
1 files changed, 39 insertions, 15 deletions
diff --git a/src/ai/ai_info_dummy.cpp b/src/ai/ai_info_dummy.cpp
index 109e7b3a0..2a6f29732 100644
--- a/src/ai/ai_info_dummy.cpp
+++ b/src/ai/ai_info_dummy.cpp
@@ -5,6 +5,10 @@
#include <squirrel.h>
#include "../stdafx.h"
+#include "../string_func.h"
+#include "../strings_func.h"
+#include "table/strings.h"
+
/* The reason this exists in C++, is that a user can trash his ai/ dir,
* leaving no AIs available. The complexity to solve this is insane, and
* therefor the alternative is used, and make sure there is always an AI
@@ -14,7 +18,7 @@
* to select manual. It is a fail-over in case no AIs are available.
*/
-const SQChar dummy_script_info[] = _SC(" \n\
+const SQChar _dummy_script_info[] = _SC(" \n\
class DummyAI extends AIInfo { \n\
function GetAuthor() { return \"OpenTTD NoAI Developers Team\"; } \n\
function GetName() { return \"DummyAI\"; } \n\
@@ -28,22 +32,12 @@ class DummyAI extends AIInfo {
RegisterDummyAI(DummyAI()); \n\
");
-const SQChar dummy_script[] = _SC(" \n\
-class DummyAI extends AIController { \n\
- function Start() { \n\
- AILog.Error(\"No suitable AI found to load.\"); \n\
- AILog.Error(\"This AI is a dummy AI and won't do anything.\"); \n\
- AILog.Error(\"You can download several AIs via the 'Online Content' system.\"); \n\
- } \n\
-} \n\
-");
-
void AI_CreateAIInfoDummy(HSQUIRRELVM vm)
{
sq_pushroottable(vm);
/* Load and run the script */
- if (SQ_SUCCEEDED(sq_compilebuffer(vm, dummy_script_info, scstrlen(dummy_script_info), _SC("dummy"), SQTrue))) {
+ if (SQ_SUCCEEDED(sq_compilebuffer(vm, _dummy_script_info, scstrlen(_dummy_script_info), _SC("dummy"), SQTrue))) {
sq_push(vm, -2);
if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) {
sq_pop(vm, 1);
@@ -55,10 +49,40 @@ void AI_CreateAIInfoDummy(HSQUIRRELVM vm)
void AI_CreateAIDummy(HSQUIRRELVM vm)
{
- sq_pushroottable(vm);
+ /* We want to translate the error message.
+ * We do this in three steps:
+ * 1) We get the error message
+ */
+ char error_message[1024];
+ GetString(error_message, STR_AI_NO_AI_FOUND, lastof(error_message));
- /* Load and run the script */
- if (SQ_SUCCEEDED(sq_compilebuffer(vm, dummy_script, scstrlen(dummy_script), _SC("dummy"), SQTrue))) {
+ /* 2) We construct the AI's code. This is done by merging a header, body and footer */
+ char dummy_script[4096];
+ char *dp = dummy_script;
+ dp = strecpy(dp, "class DummyAI extends AIController {\n function Start() {\n", lastof(dummy_script));
+
+ /* As special trick we need to split the error message on newlines and
+ * emit each newline as a separate error printing string. */
+ char *newline;
+ char *p = error_message;
+ do {
+ newline = strchr(p, '\n');
+ if (newline != NULL) *newline = '\0';
+
+ dp += seprintf(dp, lastof(dummy_script), " AILog.Error(\"%s\");\n", p);
+ p = newline + 1;
+ } while (newline != NULL);
+
+ dp = strecpy(dp, " }\n}\n", lastof(dummy_script));
+
+ /* 3) We translate the error message in the character format that Squirrel wants.
+ * We can use the fact that the wchar string printing also uses %s to print
+ * old style char strings, which is what was generated during the script generation. */
+ const SQChar *sq_dummy_script = OTTD2FS(dummy_script);
+
+ /* And finally we load and run the script */
+ sq_pushroottable(vm);
+ if (SQ_SUCCEEDED(sq_compilebuffer(vm, sq_dummy_script, scstrlen(sq_dummy_script), _SC("dummy"), SQTrue))) {
sq_push(vm, -2);
if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) {
sq_pop(vm, 1);