00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00038 luaopen_base(L_);
00039 luaopen_table(L_);
00040 luaopen_math(L_);
00041 luaopen_string(L_);
00042 luaopen_s3d(L_);
00043
00044
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
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 }