# ai_info_dummy.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_info_dummy.cpp 18862 2010-01-18 15:41:38Z rubidium $ */
    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
    00028 const SQChar _dummy_script_info[] = _SC("                                                       \n\
    00029 class DummyAI extends AIInfo {                                                                  \n\
    00030   function GetAuthor()      { return \"OpenTTD NoAI Developers Team\"; }                        \n\
    00031   function GetName()        { return \"DummyAI\"; }                                             \n\
    00032   function GetShortName()   { return \"DUMM\"; }                                                \n\
    00033   function GetDescription() { return \"A Dummy AI that is loaded when your ai/ dir is empty\"; }\n\
    00034   function GetVersion()     { return 1; }                                                       \n\
    00035   function GetDate()        { return \"2008-07-26\"; }                                          \n\
    00036   function CreateInstance() { return \"DummyAI\"; }                                             \n\
    00037 }                                                                                               \n\
    00038                                                                                                 \n\
    00039 RegisterDummyAI(DummyAI());                                                                     \n\
    00040 ");
    00041
    00042 void AI_CreateAIInfoDummy(HSQUIRRELVM vm)
    00043 {
    00044   sq_pushroottable(vm);
    00045
    00046   /* Load and run the script */
    00047   if (SQ_SUCCEEDED(sq_compilebuffer(vm, _dummy_script_info, scstrlen(_dummy_script_info), _SC("dummy"), SQTrue))) {
    00048     sq_push(vm, -2);
    00049     if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) {
    00050       sq_pop(vm, 1);
    00051       return;
    00052     }
    00053   }
    00054   NOT_REACHED();
    00055 }
    00056
    00057 void AI_CreateAIDummy(HSQUIRRELVM vm)
    00058 {
    00059   /* We want to translate the error message.
    00060    * We do this in three steps:
    00061    * 1) We get the error message
    00062    */
    00063   char error_message[1024];
    00064   GetString(error_message, STR_ERROR_AI_NO_AI_FOUND, lastof(error_message));
    00065
    00066   /* Make escapes for all quotes and slashes. */
    00067   char safe_error_message[1024];
    00068   char *q = safe_error_message;
    00069   for (const char *p = error_message; *p != '\0' && q < lastof(safe_error_message) - 2; p++, q++) {
    00070     if (*p == '"' || *p == '\\') *q++ = '\\';
    00071     *q = *p;
    00072   }
    00073   *q = '\0';
    00074
    00075   /* 2) We construct the AI's code. This is done by merging a header, body and footer */
    00076   char dummy_script[4096];
    00077   char *dp = dummy_script;
    00078   dp = strecpy(dp, "class DummyAI extends AIController {\n  function Start()\n  {\n", lastof(dummy_script));
    00079
    00080   /* As special trick we need to split the error message on newlines and
    00081    * emit each newline as a separate error printing string. */
    00082   char *newline;
    00083   char *p = safe_error_message;
    00084   do {
    00085     newline = strchr(p, '\n');
    00086     if (newline != NULL) *newline = '\0';
    00087
    00088     dp += seprintf(dp, lastof(dummy_script), "    AILog.Error(\"%s\");\n", p);
    00089     p = newline + 1;
    00090   } while (newline != NULL);
    00091
    00092   dp = strecpy(dp, "  }\n}\n", lastof(dummy_script));
    00093
    00094   /* 3) We translate the error message in the character format that Squirrel wants.
    00095    *    We can use the fact that the wchar string printing also uses %s to print
    00096    *    old style char strings, which is what was generated during the script generation. */
    00097   const SQChar *sq_dummy_script = OTTD2SQ(dummy_script);
    00098
    00099   /* And finally we load and run the script */
    00100   sq_pushroottable(vm);
    00101   if (SQ_SUCCEEDED(sq_compilebuffer(vm, sq_dummy_script, scstrlen(sq_dummy_script), _SC("dummy"), SQTrue))) {
    00102     sq_push(vm, -2);
    00103     if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) {
    00104       sq_pop(vm, 1);
    00105       return;
    00106     }
    00107   }
    00108   NOT_REACHED();
    00109 }