00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <lua/LUAUtil.h>
00022 #include <common/Logger.h>
00023
00024 void LUAUtil::addVectorToStack(lua_State *L, const FixedVector &vector)
00025 {
00026 FixedVector &vec = (FixedVector &) vector;
00027
00028 lua_newtable(L);
00029 lua_pushstring(L, "x");
00030 lua_pushnumber(L, vec[0].getInternal());
00031 lua_settable(L, -3);
00032
00033 lua_pushstring(L, "y");
00034 lua_pushnumber(L, vec[1].getInternal());
00035 lua_settable(L, -3);
00036
00037 lua_pushstring(L, "z");
00038 lua_pushnumber(L, vec[2].getInternal());
00039 lua_settable(L, -3);
00040 }
00041
00042 FixedVector LUAUtil::getVectorFromStack(lua_State *L, int position)
00043 {
00044 FixedVector result;
00045
00046 luaL_checktype(L, position, LUA_TTABLE);
00047
00048 lua_pushstring(L, "x");
00049 lua_gettable(L, position);
00050 result[0] = fixed(true, luaL_checknumber(L, -1));
00051 lua_pop(L, 1);
00052
00053 lua_pushstring(L, "y");
00054 lua_gettable(L, position);
00055 result[1] = fixed(true, luaL_checknumber(L, -1));
00056 lua_pop(L, 1);
00057
00058 lua_pushstring(L, "z");
00059 lua_gettable(L, position);
00060 result[2] = fixed(true, luaL_checknumber(L, -1));
00061 lua_pop(L, 1);
00062
00063 return result;
00064 }
00065
00066 fixed LUAUtil::getNumberFromTable(lua_State *L, int tablePosition, const char *name, fixed defaultResult)
00067 {
00068 lua_pushstring(L, name);
00069 lua_gettable(L, tablePosition);
00070
00071 fixed result = defaultResult;
00072 if (!lua_isnil(L, -1))
00073 {
00074 result = fixed(true, luaL_checknumber(L, -1));
00075 }
00076 lua_pop(L, 1);
00077
00078 return result;
00079 }
00080
00081 int LUAUtil::getIntFromTable(lua_State *L, int tablePosition, const char *name, int defaultResult)
00082 {
00083 lua_pushstring(L, name);
00084 lua_gettable(L, tablePosition);
00085
00086 int result = defaultResult;
00087 if (!lua_isnil(L, -1))
00088 {
00089 fixed fresult = fixed(true, luaL_checknumber(L, -1));
00090 result = fresult.asInt();
00091 }
00092 lua_pop(L, 1);
00093
00094 return result;
00095 }
00096
00097 bool LUAUtil::getBoolFromTable(lua_State *L, int tablePosition,
00098 const char *name, bool defaultResult)
00099 {
00100 lua_pushstring(L, name);
00101 lua_gettable(L, tablePosition);
00102
00103 bool result = defaultResult;
00104 if (!lua_isnil(L, -1) &&
00105 lua_isboolean(L, -1))
00106 {
00107 result = (lua_toboolean(L, -1) != 0);
00108 }
00109 lua_pop(L, 1);
00110
00111 return result;
00112 }
00113
00114 std::string LUAUtil::getStringFromTable(lua_State *L, int tablePosition,
00115 const char *name, std::string &defaultResult)
00116 {
00117 lua_pushstring(L, name);
00118 lua_gettable(L, tablePosition);
00119
00120 std::string result = defaultResult;
00121 if (!lua_isnil(L, -1) &&
00122 lua_isstring(L, -1))
00123 {
00124 result = lua_tostring(L, -1);
00125 }
00126 lua_pop(L, 1);
00127
00128 return result;
00129 }