Index: /code/branches/script/CMakeLists.txt
===================================================================
--- /code/branches/script/CMakeLists.txt	(revision 995)
+++ /code/branches/script/CMakeLists.txt	(revision 996)
@@ -72,4 +72,5 @@
 FIND_PACKAGE(ZLIB)
 FIND_PACKAGE(Lua)
+FIND_PACKAGE(ToLua)
 
 #Sets the search paths for the linking
Index: /code/branches/script/bin/levels/sample.oxw
===================================================================
--- /code/branches/script/bin/levels/sample.oxw	(revision 995)
+++ /code/branches/script/bin/levels/sample.oxw	(revision 996)
@@ -8,5 +8,6 @@
 			<ogg src="tha_silent_partner_-_void" />
 		</ambient>
-  </audio>
+  </audio> 
+
 -->
 
Index: /code/branches/script/bin/plugins.cfg-init
===================================================================
--- /code/branches/script/bin/plugins.cfg-init	(revision 995)
+++ /code/branches/script/bin/plugins.cfg-init	(revision 996)
@@ -2,5 +2,5 @@
 
 # Define plugin folder
-PluginFolder=/usr/lib/OGRE
+PluginFolder=/usr/local/lib/OGRE
 
 # Define plugins
Index: /code/branches/script/cmake/FindToLua.cmake
===================================================================
--- /code/branches/script/cmake/FindToLua.cmake	(revision 996)
+++ /code/branches/script/cmake/FindToLua.cmake	(revision 996)
@@ -0,0 +1,31 @@
+# Find Lua includes and library
+#
+# This module defines
+#  ToLua_INCLUDE_DIR
+#  ToLua_LIBRARIES, the libraries to link against to use Lua.
+#  ToLua_LIB_DIR, the location of the libraries
+#  ToLua_FOUND, If false, do not try to use Lua
+#
+# Copyright © 2007, Matt Williams
+#
+# Redistribution and use is allowed according to the terms of the BSD license.
+
+IF (ToLua_LIBRARIES)
+    SET(ToLua_FIND_QUIETLY TRUE) # Already in cache, be silent
+ENDIF (ToLua_LIBRARIES)
+
+FIND_LIBRARY(ToLua_LIBRARIES tolua++5.1
+	/usr/lib
+	/usr/local/lib)
+
+IF (ToLua_LIBRARIES)
+    SET(ToLua_FOUND TRUE)
+ENDIF (ToLua_LIBRARIES)
+
+IF (ToLua_FOUND)
+    MESSAGE(STATUS "Found ToLua: ${ToLua_LIBRARIES}")
+ELSE (ToLua_FOUND)
+    IF (ToLua_FIND_REQUIRED)
+        MESSAGE(FATAL_ERROR "Could not find ToLua")
+    ENDIF (ToLua_FIND_REQUIRED)
+ENDIF (ToLua_FOUND)
Index: /code/branches/script/src/orxonox/CMakeLists.txt
===================================================================
--- /code/branches/script/src/orxonox/CMakeLists.txt	(revision 995)
+++ /code/branches/script/src/orxonox/CMakeLists.txt	(revision 996)
@@ -33,4 +33,5 @@
   objects/weapon/WeaponStation.cc
   script/Script.cc
+  script/tolua_script.cc
 )
 
@@ -43,4 +44,5 @@
   ${OIS_LIBRARIES}
   ${Lua_LIBRARIES}
+  ${ToLua_LIBRARIES}
   util
   core
Index: /code/branches/script/src/orxonox/core/Loader.cc
===================================================================
--- /code/branches/script/src/orxonox/core/Loader.cc	(revision 995)
+++ /code/branches/script/src/orxonox/core/Loader.cc	(revision 996)
@@ -33,4 +33,5 @@
 #include "Debug.h"
 #include "CoreIncludes.h"
+#include "../script/Script.h"
 
 #include "util/tinyxml/ticpp.h"
@@ -107,4 +108,10 @@
         Loader::currentMask_s = level->getMask() * mask;
 
+        // let Lua work this out:
+        //Script* lua;
+        Script::loadFile(level->getFile(), true);
+        Script::init(Script::getLuaState());
+        Script::run();
+
         try
         {
@@ -112,6 +119,10 @@
             COUT(3) << "Mask: " << Loader::currentMask_s << std::endl;
 
-            ticpp::Document xmlfile(level->getFile());
-            xmlfile.LoadFile();
+            //ticpp::Document xmlfile(level->getFile());
+            //xmlfile.LoadFile();
+            //ticpp::Element myelement(*Script::getFileString());
+            ticpp::Document xmlfile;
+            //xmlfile.ToDocument();
+            xmlfile.Parse(*Script::getFileString(), true);
 
             for ( ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++ )
Index: /code/branches/script/src/orxonox/script/Script.cc
===================================================================
--- /code/branches/script/src/orxonox/script/Script.cc	(revision 995)
+++ /code/branches/script/src/orxonox/script/Script.cc	(revision 996)
@@ -29,4 +29,7 @@
 
 #include <fstream>
+#include <map>
+
+#include "../core/CoreIncludes.h"
 
 extern "C" {
@@ -35,12 +38,32 @@
 }
 
+#include "tolua++.h"
+#include "tolua_script.h"
 
 namespace orxonox
 {
+
+  lua_State* Script::luaState_ = lua_open();
+  //lua_State* Script::luaState_ = NULL;
+  std::string Script::fileString_ = "";
+
   Script::Script()
   {
-    fileLines_ = std::list<std::string>();
-
-    state_ = lua_open();
+  }
+
+  Script::~Script()
+  {
+
+  }
+
+  /**
+      @brief Initializes a lua state
+      @param state_ the pointer of the lua_state to initialise
+  */
+  void Script::init(lua_State *state_)
+  {
+    tolua_script_open(state_);
+
+    /*
 #if LUA_VERSION_NUM == 501
     luaL_openlibs(state_);
@@ -53,9 +76,10 @@
     luaopen_debug(state_);
 #endif
-  }
-
-  Script::~Script()
-  {
-
+    */
+  }
+
+  void Script::luaPrint(std::string str)
+  {
+    fileString_ = str;
   }
 
@@ -63,6 +87,7 @@
       @brief Loads the specified file line by line
       @param filename The filename of the file
+      @param luaTags if true, the loaded file gets stripped off luaTags
   */
-  void Script::loadFile(std::string filename)
+  void Script::loadFile(std::string filename, bool luaTags)
   {
     std::ifstream file;
@@ -79,44 +104,175 @@
     {
       file.getline(line, 1024);
-      this->fileLines_.push_back(line);
-    }
-
-    // The last line is useless
-    this->fileLines_.pop_back();
+      fileString_ += line;
+    }
 
     file.close();
-  }
-
-  /**
-      @brief Parses the level file to correct Lua code
-  */
-  void Script::xmlToLua()
-  {
-    // We will iterate through all the lines and replace things.
-    std::list<std::string>::iterator it;
-    for(it = this->fileLines_.begin(); it != this->fileLines_.end(); ++it)
-    {
-      int pos = (*it).find("<?lua");
-      while (pos < (int)(*it).length())
+    //std::string output;
+
+    if (luaTags) fileString_ = replaceLuaTags(Script::fileString_);
+  }
+
+  void Script::run()
+  {
+    int error = 0;
+    std::string init = "local scr = orxonox.Script:new()\n";
+    init += fileString_;
+    error = luaL_loadstring(luaState_, init.c_str());
+    if (error == 0)
+      error = lua_pcall(luaState_, 0, 0, 0);
+    if (error != 0) COUT(0) << "Error in Lua-script: " << lua_tostring(luaState_, -1) << std::endl;
+  }
+
+
+  unsigned int Script::getNextQuote(const std::string& text, unsigned int start)
+  {
+    unsigned int quote = start - 1;
+
+    while ((quote = text.find('\"', quote + 1)) != std::string::npos)
+    {
+      unsigned int backslash = quote;
+      unsigned int numbackslashes = 0;
+      for (; backslash > 0; backslash--, numbackslashes++)
+        if (text[backslash - 1] != '\\')
+          break;
+
+      if (numbackslashes % 2 == 0)
+        break;
+    }
+
+    return quote;
+  }
+
+  std::string Script::replaceLuaTags(const std::string& text)
+  {
+    // chreate map with all Lua tags
+    std::map<unsigned int, bool> luaTags;
+    {
+      unsigned int pos = 0;
+      while ((pos = text.find("<?lua", pos)) != std::string::npos)
+        luaTags[pos++] = true;
+    }
+    {
+      unsigned int pos = 0;
+      while ((pos = text.find("?>", pos)) != std::string::npos)
+        luaTags[pos++] = false;
+    }
+
+    // erase all tags from the map that are between two quotes
+    {
+      std::map<unsigned int, bool>::iterator it = luaTags.begin();
+      bool bBetweenQuotes = false;
+      unsigned int pos = 0;
+      while ((pos = getNextQuote(text, pos)) != std::string::npos)
       {
-        // We found a lua tag
-        std::string front = (*it).substr(0,pos);
-        std::string back = (*it).substr(pos + 5);
-        (*it) = front + "]])" + back;
-        pos = (*it).find("<?lua");
-      }
-
-      pos = (*it).find("?>");
-      while (pos < (int)(*it).length())
+        while ((it != luaTags.end()) && ((*it).first < pos))
+        {
+          if (bBetweenQuotes)
+            luaTags.erase(it++);
+          else
+            ++it;
+        }
+        bBetweenQuotes = !bBetweenQuotes;
+        pos++;
+      }
+    }
+
+    // check whether on every opening <?lua tag a closing ?> tag follows
+    {
+      bool expectedValue = true;
+      for (std::map<unsigned int, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it)
       {
-        // We found a lua tag
-        std::string front = (*it).substr(0,pos);
-        std::string back = (*it).substr(pos + 2);
-        (*it) = front + "print([[" + back;
-        pos = (*it).find("?>");
-      }
-    }
-    this->fileLines_.push_front("print([[");
-    this->fileLines_.push_back("]])");
+        if ((*it).second == expectedValue)
+          expectedValue = !expectedValue;
+        else
+        {
+          expectedValue = false;
+          break;
+        }
+      }
+      if (!expectedValue) {
+        // todo: errorhandling
+        return "";
+      }
+    }
+
+    // cut the original string into pieces and put them together with print() instead of lua tags
+    std::string output;
+    {
+      std::map<unsigned int, bool>::iterator it = luaTags.begin();
+      bool bInPrintFunction = true;
+      unsigned int start = 0;
+      unsigned int end = 0;
+
+      do
+      {
+        if (it != luaTags.end())
+          end = (*(it++)).first;
+        else
+          end = std::string::npos;
+
+        unsigned int equalSignCounter = 0;
+
+        if (bInPrintFunction)
+        {
+          // count ['='[ and ]'='] and replace tags with print([[ and ]])
+          std::string temp = text.substr(start, end - start);
+          {
+            unsigned int pos = 0;
+            while ((pos = temp.find('[', pos)) != std::string::npos)
+            {
+              unsigned int tempCounter = 1;
+              unsigned int tempPos = pos++;
+              while(temp[++tempPos] == '=') {
+                tempCounter++;
+              }
+              if(temp[tempPos] != '[') {
+                tempCounter = 0;
+              }
+              else if(tempCounter == 0) {
+                tempCounter = 1;
+              }
+              if (tempCounter > equalSignCounter)
+                equalSignCounter = tempCounter;
+            }
+          }
+          {
+            unsigned int pos = 0;
+            while ((pos = temp.find(']', pos)) != std::string::npos)
+            {
+              unsigned int tempCounter = 1;
+              unsigned int tempPos = pos++;
+              while(temp[++tempPos] == '=') {
+                tempCounter++;
+              }
+              if(temp[tempPos] != ']') {
+                tempCounter = 0;
+              }
+              else if(tempCounter == 0) {
+                tempCounter = 1;
+              }
+              if (tempCounter > equalSignCounter)
+                equalSignCounter = tempCounter;
+            }
+          }
+          std::string equalSigns = "";
+          for(unsigned int i = 0; i < equalSignCounter; i++) {
+            equalSigns += "=";
+          }
+          output += "scr:luaPrint([" + equalSigns + "[" + temp + "]" + equalSigns +"])\n";
+          start = end + 5;
+        }
+        else
+        {
+          output += text.substr(start, end - start);
+          start = end + 2;
+        }
+
+        bInPrintFunction = !bInPrintFunction;
+      }
+      while (end != std::string::npos);
+    }
+
+    return output;
   }
 
Index: /code/branches/script/src/orxonox/script/Script.h
===================================================================
--- /code/branches/script/src/orxonox/script/Script.h	(revision 995)
+++ /code/branches/script/src/orxonox/script/Script.h	(revision 996)
@@ -1,5 +1,5 @@
 /**
  @file  script.h
- @brief Representation of a lua script
+ @brief Representation of an interface to lua
  @author Benjamin Knecht <beni_at_orxonox.net>
  */
@@ -15,16 +15,24 @@
 #include <string>
 
-namespace orxonox
-{
+namespace orxonox // tolua_export
+{ // tolua_export
 
-  class Script
-  {
-    Script();
+  class Script // tolua_export
+  { // tolua_export
+    public:
+    Script(); // tolua_export
     ~Script();
 
-    void loadFile(std::string filename);
-    void xmlToLua();
+    static void loadFile(std::string filename, bool luaTags);
+    static void init(lua_State *state_);
+    //void xmlToLua();
+    static void run();
+    void luaPrint(std::string str); // tolua_export
 
-    inline lua_State* getLuaState() { return state_; };
+    inline static lua_State* getLuaState() { return luaState_; };
+    inline static std::string* getFileString() { return &fileString_; };
+
+    static unsigned int getNextQuote(const std::string& text, unsigned int start);
+    static std::string replaceLuaTags(const std::string& text);
 
     private:
@@ -32,8 +40,8 @@
       //std::list<std::string>& getLevelFileLines();
 
-      std::list<std::string> fileLines_;
-      lua_State* state_;
+      static std::string fileString_;
+      static lua_State* luaState_;
 
-  };
-}
+  }; // tolua_export
+} // tolua_export
 #endif /* _Script_H__ */
Index: /code/branches/script/src/orxonox/script/tolua_script.cc
===================================================================
--- /code/branches/script/src/orxonox/script/tolua_script.cc	(revision 996)
+++ /code/branches/script/src/orxonox/script/tolua_script.cc	(revision 996)
@@ -0,0 +1,157 @@
+/*
+** Lua binding: script
+** Generated automatically by tolua++-1.0.92 on Sat Apr  5 20:29:18 2008.
+*/
+
+#ifndef __cplusplus
+#include "stdlib.h"
+#endif
+#include "string.h"
+
+#include "tolua++.h"
+
+/* Exported function */
+TOLUA_API int  tolua_script_open (lua_State* tolua_S);
+
+#include "Script.h"
+
+/* function to release collected object via destructor */
+#ifdef __cplusplus
+
+static int tolua_collect_orxonox__Script (lua_State* tolua_S)
+{
+ orxonox::Script* self = (orxonox::Script*) tolua_tousertype(tolua_S,1,0);
+	Mtolua_delete(self);
+	return 0;
+}
+#endif
+
+
+/* function to register type */
+static void tolua_reg_types (lua_State* tolua_S)
+{
+ tolua_usertype(tolua_S,"orxonox::Script");
+}
+
+/* method: new of class  orxonox::Script */
+#ifndef TOLUA_DISABLE_tolua_script_orxonox_Script_new00
+static int tolua_script_orxonox_Script_new00(lua_State* tolua_S)
+{
+#ifndef TOLUA_RELEASE
+ tolua_Error tolua_err;
+ if (
+     !tolua_isusertable(tolua_S,1,"orxonox::Script",0,&tolua_err) ||
+     !tolua_isnoobj(tolua_S,2,&tolua_err)
+ )
+  goto tolua_lerror;
+ else
+#endif
+ {
+  {
+   orxonox::Script* tolua_ret = (orxonox::Script*)  Mtolua_new((orxonox::Script)());
+    tolua_pushusertype(tolua_S,(void*)tolua_ret,"orxonox::Script");
+  }
+ }
+ return 1;
+#ifndef TOLUA_RELEASE
+ tolua_lerror:
+ tolua_error(tolua_S,"#ferror in function 'new'.",&tolua_err);
+ return 0;
+#endif
+}
+#endif //#ifndef TOLUA_DISABLE
+
+/* method: new_local of class  orxonox::Script */
+#ifndef TOLUA_DISABLE_tolua_script_orxonox_Script_new00_local
+static int tolua_script_orxonox_Script_new00_local(lua_State* tolua_S)
+{
+#ifndef TOLUA_RELEASE
+ tolua_Error tolua_err;
+ if (
+     !tolua_isusertable(tolua_S,1,"orxonox::Script",0,&tolua_err) ||
+     !tolua_isnoobj(tolua_S,2,&tolua_err)
+ )
+  goto tolua_lerror;
+ else
+#endif
+ {
+  {
+   orxonox::Script* tolua_ret = (orxonox::Script*)  Mtolua_new((orxonox::Script)());
+    tolua_pushusertype(tolua_S,(void*)tolua_ret,"orxonox::Script");
+    tolua_register_gc(tolua_S,lua_gettop(tolua_S));
+  }
+ }
+ return 1;
+#ifndef TOLUA_RELEASE
+ tolua_lerror:
+ tolua_error(tolua_S,"#ferror in function 'new'.",&tolua_err);
+ return 0;
+#endif
+}
+#endif //#ifndef TOLUA_DISABLE
+
+/* method: luaPrint of class  orxonox::Script */
+#ifndef TOLUA_DISABLE_tolua_script_orxonox_Script_luaPrint00
+static int tolua_script_orxonox_Script_luaPrint00(lua_State* tolua_S)
+{
+#ifndef TOLUA_RELEASE
+ tolua_Error tolua_err;
+ if (
+     !tolua_isusertype(tolua_S,1,"orxonox::Script",0,&tolua_err) ||
+     !tolua_isstring(tolua_S,2,0,&tolua_err) ||
+     !tolua_isnoobj(tolua_S,3,&tolua_err)
+ )
+  goto tolua_lerror;
+ else
+#endif
+ {
+  orxonox::Script* self = (orxonox::Script*)  tolua_tousertype(tolua_S,1,0);
+  char* str = ((char*)  tolua_tostring(tolua_S,2,0));
+#ifndef TOLUA_RELEASE
+  if (!self) tolua_error(tolua_S,"invalid 'self' in function 'luaPrint'",NULL);
+#endif
+  {
+   self->luaPrint(str);
+  }
+ }
+ return 0;
+#ifndef TOLUA_RELEASE
+ tolua_lerror:
+ tolua_error(tolua_S,"#ferror in function 'luaPrint'.",&tolua_err);
+ return 0;
+#endif
+}
+#endif //#ifndef TOLUA_DISABLE
+
+/* Open function */
+TOLUA_API int tolua_script_open (lua_State* tolua_S)
+{
+ tolua_open(tolua_S);
+ tolua_reg_types(tolua_S);
+ tolua_module(tolua_S,NULL,0);
+ tolua_beginmodule(tolua_S,NULL);
+  tolua_module(tolua_S,"orxonox",0);
+  tolua_beginmodule(tolua_S,"orxonox");
+   #ifdef __cplusplus
+   tolua_cclass(tolua_S,"Script","orxonox::Script","",tolua_collect_orxonox__Script);
+   #else
+   tolua_cclass(tolua_S,"Script","orxonox::Script","",NULL);
+   #endif
+   tolua_beginmodule(tolua_S,"Script");
+    tolua_function(tolua_S,"new",tolua_script_orxonox_Script_new00);
+    tolua_function(tolua_S,"new_local",tolua_script_orxonox_Script_new00_local);
+    tolua_function(tolua_S,".call",tolua_script_orxonox_Script_new00_local);
+    tolua_function(tolua_S,"luaPrint",tolua_script_orxonox_Script_luaPrint00);
+   tolua_endmodule(tolua_S);
+  tolua_endmodule(tolua_S);
+ tolua_endmodule(tolua_S);
+ return 1;
+}
+
+
+#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501
+ TOLUA_API int luaopen_script (lua_State* tolua_S) {
+ return tolua_script_open(tolua_S);
+};
+#endif
+
Index: /code/branches/script/src/orxonox/script/tolua_script.h
===================================================================
--- /code/branches/script/src/orxonox/script/tolua_script.h	(revision 996)
+++ /code/branches/script/src/orxonox/script/tolua_script.h	(revision 996)
@@ -0,0 +1,8 @@
+/*
+** Lua binding: script
+** Generated automatically by tolua++-1.0.92 on Sat Apr  5 20:29:18 2008.
+*/
+
+/* Exported function */
+TOLUA_API int  tolua_script_open (lua_State* tolua_S);
+
Index: /code/branches/script/src/orxonox/script/tolua_script.pkg
===================================================================
--- /code/branches/script/src/orxonox/script/tolua_script.pkg	(revision 996)
+++ /code/branches/script/src/orxonox/script/tolua_script.pkg	(revision 996)
@@ -0,0 +1,1 @@
+$cfile "Script.h"
