summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2009-07-23 15:39:21 +0000
committerrubidium <rubidium@openttd.org>2009-07-23 15:39:21 +0000
commit1e6b1a76135a5b7cc41ff8c1f91fca67d17b5c1f (patch)
tree23a30eeeac4d9dccb1df7508c8126818d04b7b63 /src
parent5f9ee68eac0f3f7d898ca09f0800bf63b32b1f05 (diff)
downloadopenttd-1e6b1a76135a5b7cc41ff8c1f91fca67d17b5c1f.tar.xz
(svn r16927) -Change: make the 'there is no AI' error message translatable
Diffstat (limited to 'src')
-rw-r--r--src/ai/ai_info_dummy.cpp54
-rw-r--r--src/lang/english.txt1
2 files changed, 40 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);
diff --git a/src/lang/english.txt b/src/lang/english.txt
index 7a5354ec5..0547e3439 100644
--- a/src/lang/english.txt
+++ b/src/lang/english.txt
@@ -3609,6 +3609,7 @@ STR_AI_AUTHOR :{BLACK}Author:
STR_AI_VERSION :{BLACK}Version: {NUM}
STR_AI_URL :{BLACK}URL: {RAW_STRING}
STR_AI_PLEASE_REPORT_CRASH :{WHITE}One of the running AIs crashed. Please report this to the AI author with a screenshot of the AI Debug Window.
+STR_AI_NO_AI_FOUND :No suitable AI found to load.{}This AI is a dummy AI and won't do anything.{}You can download several AIs via the 'Online Content' system.
########
############ town controlled noise level