ai_info_dummy.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include <squirrel.h>
00013 #include "../stdafx.h"
00014 
00015 #include "../string_func.h"
00016 #include "../strings_func.h"
00017 #include "table/strings.h"
00018 
00019 /* The reason this exists in C++, is that a user can trash his ai/ dir,
00020  *  leaving no AIs available. The complexity to solve this is insane, and
00021  *  therefor the alternative is used, and make sure there is always an AI
00022  *  available, no matter what the situation is. By defining it in C++, there
00023  *  is simply now way a user can delete it, and therefor safe to use. It has
00024  *  to be noted that this AI is complete invisible for the user, and impossible
00025  *  to select manual. It is a fail-over in case no AIs are available.
00026  */
00027 
00029 const SQChar _dummy_script_info[] = _SC("                                                       \n\
00030 class DummyAI extends AIInfo {                                                                  \n\
00031   function GetAuthor()      { return \"OpenTTD NoAI Developers Team\"; }                        \n\
00032   function GetName()        { return \"DummyAI\"; }                                             \n\
00033   function GetShortName()   { return \"DUMM\"; }                                                \n\
00034   function GetDescription() { return \"A Dummy AI that is loaded when your ai/ dir is empty\"; }\n\
00035   function GetVersion()     { return 1; }                                                       \n\
00036   function GetDate()        { return \"2008-07-26\"; }                                          \n\
00037   function CreateInstance() { return \"DummyAI\"; }                                             \n\
00038 }                                                                                               \n\
00039                                                                                                 \n\
00040 RegisterDummyAI(DummyAI());                                                                     \n\
00041 ");
00042 
00044 void AI_CreateAIInfoDummy(HSQUIRRELVM vm)
00045 {
00046   sq_pushroottable(vm);
00047 
00048   /* Load and run the script */
00049   if (SQ_SUCCEEDED(sq_compilebuffer(vm, _dummy_script_info, scstrlen(_dummy_script_info), _SC("dummy"), SQTrue))) {
00050     sq_push(vm, -2);
00051     if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) {
00052       sq_pop(vm, 1);
00053       return;
00054     }
00055   }
00056   NOT_REACHED();
00057 }
00058 
00060 void AI_CreateAIDummy(HSQUIRRELVM vm)
00061 {
00062   /* We want to translate the error message.
00063    * We do this in three steps:
00064    * 1) We get the error message
00065    */
00066   char error_message[1024];
00067   GetString(error_message, STR_ERROR_AI_NO_AI_FOUND, lastof(error_message));
00068 
00069   /* Make escapes for all quotes and slashes. */
00070   char safe_error_message[1024];
00071   char *q = safe_error_message;
00072   for (const char *p = error_message; *p != '\0' && q < lastof(safe_error_message) - 2; p++, q++) {
00073     if (*p == '"' || *p == '\\') *q++ = '\\';
00074     *q = *p;
00075   }
00076   *q = '\0';
00077 
00078   /* 2) We construct the AI's code. This is done by merging a header, body and footer */
00079   char dummy_script[4096];
00080   char *dp = dummy_script;
00081   dp = strecpy(dp, "class DummyAI extends AIController {\n  function Start()\n  {\n", lastof(dummy_script));
00082 
00083   /* As special trick we need to split the error message on newlines and
00084    * emit each newline as a separate error printing string. */
00085   char *newline;
00086   char *p = safe_error_message;
00087   do {
00088     newline = strchr(p, '\n');
00089     if (newline != NULL) *newline = '\0';
00090 
00091     dp += seprintf(dp, lastof(dummy_script), "    AILog.Error(\"%s\");\n", p);
00092     p = newline + 1;
00093   } while (newline != NULL);
00094 
00095   dp = strecpy(dp, "  }\n}\n", lastof(dummy_script));
00096 
00097   /* 3) We translate the error message in the character format that Squirrel wants.
00098    *    We can use the fact that the wchar string printing also uses %s to print
00099    *    old style char strings, which is what was generated during the script generation. */
00100   const SQChar *sq_dummy_script = OTTD2SQ(dummy_script);
00101 
00102   /* And finally we load and run the script */
00103   sq_pushroottable(vm);
00104   if (SQ_SUCCEEDED(sq_compilebuffer(vm, sq_dummy_script, scstrlen(sq_dummy_script), _SC("dummy"), SQTrue))) {
00105     sq_push(vm, -2);
00106     if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) {
00107       sq_pop(vm, 1);
00108       return;
00109     }
00110   }
00111   NOT_REACHED();
00112 }

Generated on Thu Apr 14 00:48:10 2011 for OpenTTD by  doxygen 1.6.1