summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkrzys-h <krzys_h@interia.pl>2013-05-19 16:56:08 +0200
committerkrzys-h <krzys_h@interia.pl>2013-05-19 16:56:08 +0200
commit6b25608e698cc117ffeeaa6454bd6fc1a72fdc50 (patch)
tree37c34ebfbf70b3e49f8de985a7f77ad4c93a5203
parentb9d0ee034e1e0b78cbca137cc2f39930fb7ad127 (diff)
downloadcolobot-6b25608e698cc117ffeeaa6454bd6fc1a72fdc50.tar.gz
colobot-6b25608e698cc117ffeeaa6454bd6fc1a72fdc50.tar.bz2
colobot-6b25608e698cc117ffeeaa6454bd6fc1a72fdc50.zip
Added progfunc( funcname );
For simple creating of programs executing public functions :) Example: public void object::SecondBot() { message("It works!"); } extern void object::FirstBot() { object item = radar(BotFactory); item.factory(WheeledGrabber, progfunc("SecondBot")); }
-rw-r--r--src/script/cbottoken.cpp3
-rw-r--r--src/script/script.cpp36
-rw-r--r--src/script/script.h2
3 files changed, 41 insertions, 0 deletions
diff --git a/src/script/cbottoken.cpp b/src/script/cbottoken.cpp
index bf29e1d..1747e5c 100644
--- a/src/script/cbottoken.cpp
+++ b/src/script/cbottoken.cpp
@@ -253,6 +253,7 @@ std::string GetHelpFilename(const char *token)
if ( strcmp(token, "getresearchenable" ) == 0 ) return std::string("help/") + CApplication::GetInstancePointer()->GetLanguageChar() + std::string("/cbot/getresen.txt");
if ( strcmp(token, "getresearchdone" ) == 0 ) return std::string("help/") + CApplication::GetInstancePointer()->GetLanguageChar() + std::string("/cbot/getresdo.txt");
if ( strcmp(token, "retobject" ) == 0 ) return std::string("help/") + CApplication::GetInstancePointer()->GetLanguageChar() + std::string("/cbot/retobj.txt");
+ if ( strcmp(token, "progfunc" ) == 0 ) return std::string("help/") + CApplication::GetInstancePointer()->GetLanguageChar() + std::string("/cbot/factory.txt");
if ( strcmp(token, "busy" ) == 0 ) return std::string("help/") + CApplication::GetInstancePointer()->GetLanguageChar() + std::string("/cbot/busy.txt");
if ( strcmp(token, "factory" ) == 0 ) return std::string("help/") + CApplication::GetInstancePointer()->GetLanguageChar() + std::string("/cbot/factory.txt");
if ( strcmp(token, "destroy" ) == 0 ) return std::string("help/") + CApplication::GetInstancePointer()->GetLanguageChar() + std::string("/cbot/destroy.txt");
@@ -378,6 +379,7 @@ bool IsFunction(const char *token)
if ( strcmp(token, "getresearchdone" ) == 0 ) return true;
if ( strcmp(token, "retobjectbyid") == 0 ) return true;
if ( strcmp(token, "retobject" ) == 0 ) return true;
+ if ( strcmp(token, "progfunc" ) == 0 ) return true;
if ( strcmp(token, "busy" ) == 0 ) return true;
if ( strcmp(token, "factory" ) == 0 ) return true;
if ( strcmp(token, "destroy" ) == 0 ) return true;
@@ -469,6 +471,7 @@ const char* GetHelpText(const char *token)
if ( strcmp(token, "getresearchdone" ) == 0 ) return "getresearchdone ( );";
if ( strcmp(token, "retobject" ) == 0 ) return "retobject ( rank );";
if ( strcmp(token, "retobjectbyid") == 0 ) return "retobjectbyid ( rank );";
+ if ( strcmp(token, "progfunc" ) == 0 ) return "progfunc ( funcname );";
if ( strcmp(token, "busy" ) == 0 ) return "object.busy ( );";
if ( strcmp(token, "factory" ) == 0 ) return "object.factory ( cat, program );";
if ( strcmp(token, "destroy" ) == 0 ) return "object.destroy ( );";
diff --git a/src/script/script.cpp b/src/script/script.cpp
index 1f86e71..dc10425 100644
--- a/src/script/script.cpp
+++ b/src/script/script.cpp
@@ -155,6 +155,18 @@ CBotTypResult CScript::cString(CBotVar* &var, void* user)
return CBotTypResult(CBotTypFloat);
}
+// Compiling a procedure with a single string, returning string.
+
+CBotTypResult CScript::cStringString(CBotVar* &var, void* user)
+{
+ if ( var == 0 ) return CBotTypResult(CBotErrLowParam);
+ if ( var->GetType() != CBotTypString &&
+ var->GetType() > CBotTypDouble ) return CBotTypResult(CBotErrBadNum);
+ var = var->GetNext();
+ if ( var != 0 ) return CBotTypResult(CBotErrOverParam);
+ return CBotTypResult(CBotTypString);
+}
+
// Seeking value in an array of integers.
@@ -499,6 +511,29 @@ bool CScript::rGetObject(CBotVar* var, CBotVar* result, int& exception, void* us
return true;
}
+
+// Instruction "progfunc(funcname)".
+
+bool CScript::rProgFunc(CBotVar* var, CBotVar* result, int& exception, void* user)
+{
+ CBotString cbs;
+ const char* funcname;
+ std::string program;
+
+ cbs = var->GetValString();
+ funcname = cbs;
+
+ //TODO: Translation :)
+ program = "extern void object::Auto()\n{\n\t\n\t//Automatically generated by progfunc(\"";
+ program += funcname;
+ program += "\");\n\t";
+ program += funcname;
+ program += "();\n\t\n}\n";
+
+ result->SetValString(program.c_str());
+
+ return true;
+}
// Compilation of instruction "object.busy()"
CBotTypResult CScript::cBusy(CBotVar* thisclass, CBotVar* &var)
{
@@ -3402,6 +3437,7 @@ void CScript::InitFonctions()
CBotProgram::AddFunction("setresearchenable", rSetResearchEnable, CScript::cOneFloat);
CBotProgram::AddFunction("setresearchdone", rSetResearchDone, CScript::cOneFloat);
+ CBotProgram::AddFunction("progfunc", rProgFunc, CScript::cStringString);
CBotProgram::AddFunction("retobject", rGetObject, CScript::cGetObject);
CBotProgram::AddFunction("retobjectbyid", rGetObjectById, CScript::cGetObject);
CBotProgram::AddFunction("delete", rDelete, CScript::cDelete);
diff --git a/src/script/script.h b/src/script/script.h
index e3d3b7a..1c02daf 100644
--- a/src/script/script.h
+++ b/src/script/script.h
@@ -98,6 +98,7 @@ private:
static CBotTypResult cOneFloat(CBotVar* &var, void* user);
static CBotTypResult cTwoFloat(CBotVar* &var, void* user);
static CBotTypResult cString(CBotVar* &var, void* user);
+ static CBotTypResult cStringString(CBotVar* &var, void* user);
static CBotTypResult cEndMission(CBotVar* &var, void* user);
static CBotTypResult cPlayMusic(CBotVar* &var, void* user);
static CBotTypResult cGetObject(CBotVar* &var, void* user);
@@ -146,6 +147,7 @@ private:
static bool rSetResearchDone(CBotVar* var, CBotVar* result, int& exception, void* user);
static bool rGetObjectById(CBotVar* var, CBotVar* result, int& exception, void* user);
static bool rGetObject(CBotVar* var, CBotVar* result, int& exception, void* user);
+ static bool rProgFunc(CBotVar* var, CBotVar* result, int& exception, void* user);
static bool rDelete(CBotVar* var, CBotVar* result, int& exception, void* user);
static bool rSearch(CBotVar* var, CBotVar* result, int& exception, void* user);
static bool rRadar(CBotVar* var, CBotVar* result, int& exception, void* user);