LUAScript.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <lua/LUAScript.h>
00022 #include <lua/LUAUtil.h>
00023 #include <lua/LUAS3DLib.h>
00024 #include <lua/LUAS3DWeaponLib.h>
00025 #include <common/Logger.h>
00026 
00027 #include "lauxlib.h"
00028 #include "lualib.h"
00029 
00030 LUAScript::LUAScript(ScorchedContext *context) :
00031         context_(context),
00032         L_(0),
00033         weapon_(0)
00034 {
00035         L_ = lua_open();
00036         
00037         // Load the available libraries
00038         luaopen_base(L_); 
00039         luaopen_table(L_); 
00040         luaopen_math(L_); 
00041         luaopen_string(L_); 
00042         luaopen_s3d(L_);
00043 
00044         // Store the context globaly
00045         lua_pushlightuserdata(L_, (void *) this);
00046         lua_setglobal(L_, "s3d_script");
00047 }
00048 
00049 LUAScript::~LUAScript()
00050 {
00051         if (L_) 
00052         {
00053                 lua_close(L_);
00054                 L_ = 0;
00055         }
00056 }
00057 
00058 void LUAScript::addWeaponFunctions()
00059 {
00060         luaopen_s3dweapon(L_);
00061 }
00062 
00063 bool LUAScript::loadFromFile(const std::string &filename, std::string &error)
00064 {
00065         // Load the script
00066         bool result = true;
00067         int temp_int = luaL_dofile(L_, filename.c_str());
00068         if (temp_int != 0)
00069         {
00070                 error = S3D::formatStringBuffer(
00071                         "ERROR: LUA error : %s", lua_tostring(L_, -1));
00072                 result = false;
00073         }
00074         
00075         return result;
00076 }
00077 
00078 bool LUAScript::setGlobal(const std::string &name, fixed value)
00079 {
00080         lua_pushnumber(L_, value.getInternal());
00081         lua_setglobal(L_, name.c_str());
00082         return true;
00083 }
00084 
00085 bool LUAScript::functionExists(const std::string &functionName)
00086 {
00087         lua_getglobal(L_, functionName.c_str());
00088         bool exists = !lua_isnil(L_, -1);
00089         lua_pop(L_, 1);
00090         return exists;
00091 }
00092 
00093 bool LUAScript::startFunction(const std::string &functionName)
00094 {
00095         lua_getglobal(L_, functionName.c_str());
00096         return !lua_isnil(L_, -1);
00097 }
00098 
00099 bool LUAScript::endFunction(int argCount)
00100 {
00101         int temp_int = lua_pcall(L_, argCount, 0, 0);
00102         if (temp_int != 0)
00103         {
00104                 Logger::log(S3D::formatStringBuffer(
00105                         "ERROR: LUA error : %s", lua_tostring(L_, -1)));
00106                 return false;
00107         }
00108         return true;
00109 }
00110 
00111 bool LUAScript::addStringParameter(const std::string &str)
00112 {
00113         lua_pushstring(L_, str.c_str());
00114         return true;
00115 }
00116 
00117 bool LUAScript::addNumberParameter(fixed number)
00118 {
00119         lua_pushnumber(L_, number.getInternal());
00120         return true;
00121 }
00122 
00123 bool LUAScript::addBoolParameter(bool boolean)
00124 {
00125         lua_pushboolean(L_, boolean);
00126         return true;
00127 }
00128 
00129 bool LUAScript::addVectorParameter(const FixedVector &v)
00130 {
00131         LUAUtil::addVectorToStack(L_, v);
00132         return true;
00133 }

Generated on Mon Feb 16 15:14:51 2009 for Scorched3D by  doxygen 1.5.3