diff options
Diffstat (limited to 'src/shared/lua_api/common.cpp')
| -rw-r--r-- | src/shared/lua_api/common.cpp | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/src/shared/lua_api/common.cpp b/src/shared/lua_api/common.cpp new file mode 100644 index 0000000..68bf203 --- /dev/null +++ b/src/shared/lua_api/common.cpp @@ -0,0 +1,205 @@ + +extern "C" { + #include <lua.h> + #include <lauxlib.h> + #include <lualib.h> +} + +#include "common.hpp" + +//Expose things to the lua state +void loadLLibs(lua_State* L){ + + lua_pushcfunction(L,luaopen_base); + lua_pushliteral(L,""); + lua_call(L,1,0); + + lua_pushcfunction(L,luaopen_table); + lua_pushliteral(L,LUA_TABLIBNAME); + lua_call(L,1,0); + + lua_pushcfunction(L,luaopen_string); + lua_pushliteral(L,LUA_STRLIBNAME); + lua_call(L,1,0); + + lua_pushcfunction(L,luaopen_math); + lua_pushliteral(L,LUA_MATHLIBNAME); + lua_call(L,1,0); + + lua_pushcfunction(L,luaopen_string); + lua_pushliteral(L,LUA_STRLIBNAME); + lua_call(L,1,0); + + /* + lua_pushcfunction(L,luaopen_string); + lua_pushliteral(L,LUA_STRLIBNAME); + lua_call(L,1,0); + */ +} + + +int pushvector3i(lua_State* L,long a,long b,long c){ + lua_newtable(L);//{} + + lua_pushinteger(L,1);//{},1 + lua_pushinteger(L,a);//{},1,a + lua_settable(L,-3);//{} + + lua_pushinteger(L,2);//{},2 + lua_pushinteger(L,b);//{},2,b + lua_settable(L,-3);//{} + + lua_pushinteger(L,3);//{},3 + lua_pushinteger(L,c);//{},3,c + lua_settable(L,-3);//{} + + return 1; +} +int pushvector3d(lua_State* L,double a,double b,double c){ + lua_newtable(L);//{} + + lua_pushinteger(L,1);//{},1 + lua_pushnumber(L,a);//{},1,a + lua_settable(L,-3);//{} + + lua_pushinteger(L,2);//{},2 + lua_pushnumber(L,b);//{},2,b + lua_settable(L,-3);//{} + + lua_pushinteger(L,3);//{},3 + lua_pushnumber(L,c);//{},3,c + lua_settable(L,-3);//{} + + return 1; +} +int pushvector2i(lua_State* L, long a, long b){ + lua_newtable(L); + + lua_pushinteger(L,1); + lua_pushinteger(L,a); + lua_settable(L,-3); + + lua_pushinteger(L,2); + lua_pushinteger(L,b); + lua_settable(L,-3); + + return 1; +} + +int popvector4i(lua_State* L,long* a,long* b,long* c, long* d){ + lua_pushinteger(L,1);//{v4},1 + lua_gettable(L,-2);//{v4},v4[1] + *a = lua_tointeger(L,-1);//{v4},v4[1] + lua_pop(L,1);//{v4} + + lua_pushinteger(L,2);//{v4},2 + lua_gettable(L,-2);//{v4},v4[2] + *b = lua_tointeger(L,-1);//{v4},v4[2] + lua_pop(L,1);//{v4} + + lua_pushinteger(L,3);//{v4},3 + lua_gettable(L,-2);//{v4},v4[3] + *c = lua_tointeger(L,-1);//{v4},v4[3] + lua_pop(L,1);//{v4} + + lua_pushinteger(L,4);//{v4},3 + lua_gettable(L,-2);//{v4},v4[3] + *d = lua_tointeger(L,-1);//{v4},v4[3] + lua_pop(L,1);//{v4} + + lua_pop(L,1);// + return 0; +} + +int popvector3i(lua_State* L,long* a,long* b,long* c){//{v3} + lua_pushinteger(L,1);//{v3},1 + lua_gettable(L,-2);//{v3},v3[1] + *a = lua_tointeger(L,-1);//{v3},v3[1] + lua_pop(L,1);//{v3} + + lua_pushinteger(L,2);//{v3},2 + lua_gettable(L,-2);//{v3},v3[2] + *b = lua_tointeger(L,-1);//{v3},v3[2] + lua_pop(L,1);//{v3} + + lua_pushinteger(L,3);//{v3},3 + lua_gettable(L,-2);//{v3},v3[3] + *c = lua_tointeger(L,-1);//{v3},v3[3] + lua_pop(L,1);//{v3} + + lua_pop(L,1);// + return 0; +} + + + +int popvector3d(lua_State* L,double* a,double* b,double* c){ + lua_pushinteger(L,1); + lua_gettable(L,-2); + *a = lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushinteger(L,2); + lua_gettable(L,-2); + *b = lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushinteger(L,3); + lua_gettable(L,-2); + *c = lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pop(L,1); + return 0; +} + +int popvector4d(lua_State* L, double *a, double *b, double *c, double *d){ + lua_pushinteger(L,1);//{a,b,c,d},1 + lua_gettable(L,-2);//{a,b,c,d},a + *a = lua_tonumber(L,-1);//{a,b,c,d},a + lua_pop(L,1);//{a,b,c,d} + + lua_pushinteger(L,2); + lua_gettable(L,-2); + *b = lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushinteger(L,3); + lua_gettable(L,-2); + *c = lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushinteger(L,4); + lua_gettable(L,-2); + *d = lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pop(L,1); + return 0; +} + +//{{sx,sy},{ex,ey}} +int poprecti(lua_State* L, long *sx, long *sy, long *ex, long *ey){ + lua_pushnumber(L,1); + lua_gettable(L,-2); + popvector2i(L,sx,sy); + lua_pushnumber(L,2); + lua_gettable(L,-2); + popvector2i(L,ex,ey); + lua_pop(L,1); + return 0; +} + +int popvector2i(lua_State* L, long* a, long* b){ + lua_pushinteger(L,1); + lua_gettable(L,-2); + *a = lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushinteger(L,2); + lua_gettable(L,-2); + *b = lua_tonumber(L,-1); + lua_pop(L,2); + + return 0; +} |
