1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Implement vector and node conversion in Lua (#12609)

Co-authored-by: sfan5 <sfan5@live.de>
This commit is contained in:
Jude Melton-Houghton 2022-10-18 18:01:44 -04:00 committed by GitHub
parent 23e9f5db43
commit b38ffdec27
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 191 additions and 167 deletions

View file

@ -48,17 +48,19 @@ extern "C" {
} \
} while (0)
#define CHECK_POS_COORD(name) CHECK_TYPE(-1, "vector coordinate " name, LUA_TNUMBER)
#define CHECK_POS_COORD(index, name) CHECK_TYPE(index, "vector coordinate " name, LUA_TNUMBER)
#define CHECK_POS_TAB(index) CHECK_TYPE(index, "vector", LUA_TTABLE)
/**
* A helper which sets the vector metatable for the table on top of the stack
* A helper which calls CUSTOM_RIDX_READ_VECTOR with the argument at the given index
*/
static void set_vector_metatable(lua_State *L)
static void read_v3_aux(lua_State *L, int index)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_VECTOR_METATABLE);
lua_setmetatable(L, -2);
lua_pushvalue(L, index);
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_READ_VECTOR);
lua_insert(L, -2);
lua_call(L, 1, 3);
}
// Retrieve an integer vector where all components are optional
@ -79,14 +81,11 @@ static bool getv3intfield(lua_State *L, int index,
void push_v3f(lua_State *L, v3f p)
{
lua_createtable(L, 0, 3);
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_PUSH_VECTOR);
lua_pushnumber(L, p.X);
lua_setfield(L, -2, "x");
lua_pushnumber(L, p.Y);
lua_setfield(L, -2, "y");
lua_pushnumber(L, p.Z);
lua_setfield(L, -2, "z");
set_vector_metatable(L);
lua_call(L, 3, 1);
}
void push_v2f(lua_State *L, v2f p)
@ -160,12 +159,12 @@ v2f check_v2f(lua_State *L, int index)
v2f p;
CHECK_POS_TAB(index);
lua_getfield(L, index, "x");
CHECK_POS_COORD("x");
CHECK_POS_COORD(-1, "x");
p.X = lua_tonumber(L, -1);
CHECK_FLOAT(p.X, "x");
lua_pop(L, 1);
lua_getfield(L, index, "y");
CHECK_POS_COORD("y");
CHECK_POS_COORD(-1, "y");
p.Y = lua_tonumber(L, -1);
CHECK_FLOAT(p.Y, "y");
lua_pop(L, 1);
@ -174,78 +173,48 @@ v2f check_v2f(lua_State *L, int index)
v3f read_v3f(lua_State *L, int index)
{
v3f pos;
CHECK_POS_TAB(index);
lua_getfield(L, index, "x");
pos.X = lua_tonumber(L, -1);
lua_pop(L, 1);
lua_getfield(L, index, "y");
pos.Y = lua_tonumber(L, -1);
lua_pop(L, 1);
lua_getfield(L, index, "z");
pos.Z = lua_tonumber(L, -1);
lua_pop(L, 1);
return pos;
read_v3_aux(L, index);
float x = lua_tonumber(L, -3);
float y = lua_tonumber(L, -2);
float z = lua_tonumber(L, -1);
lua_pop(L, 3);
return v3f(x, y, z);
}
v3f check_v3f(lua_State *L, int index)
{
v3f pos;
CHECK_POS_TAB(index);
lua_getfield(L, index, "x");
CHECK_POS_COORD("x");
pos.X = lua_tonumber(L, -1);
CHECK_FLOAT(pos.X, "x");
lua_pop(L, 1);
lua_getfield(L, index, "y");
CHECK_POS_COORD("y");
pos.Y = lua_tonumber(L, -1);
CHECK_FLOAT(pos.Y, "y");
lua_pop(L, 1);
lua_getfield(L, index, "z");
CHECK_POS_COORD("z");
pos.Z = lua_tonumber(L, -1);
CHECK_FLOAT(pos.Z, "z");
lua_pop(L, 1);
return pos;
read_v3_aux(L, index);
CHECK_POS_COORD(-3, "x");
CHECK_POS_COORD(-2, "y");
CHECK_POS_COORD(-1, "z");
float x = lua_tonumber(L, -3);
float y = lua_tonumber(L, -2);
float z = lua_tonumber(L, -1);
lua_pop(L, 3);
return v3f(x, y, z);
}
v3d read_v3d(lua_State *L, int index)
{
v3d pos;
CHECK_POS_TAB(index);
lua_getfield(L, index, "x");
pos.X = lua_tonumber(L, -1);
lua_pop(L, 1);
lua_getfield(L, index, "y");
pos.Y = lua_tonumber(L, -1);
lua_pop(L, 1);
lua_getfield(L, index, "z");
pos.Z = lua_tonumber(L, -1);
lua_pop(L, 1);
return pos;
read_v3_aux(L, index);
double x = lua_tonumber(L, -3);
double y = lua_tonumber(L, -2);
double z = lua_tonumber(L, -1);
lua_pop(L, 3);
return v3d(x, y, z);
}
v3d check_v3d(lua_State *L, int index)
{
v3d pos;
CHECK_POS_TAB(index);
lua_getfield(L, index, "x");
CHECK_POS_COORD("x");
pos.X = lua_tonumber(L, -1);
CHECK_FLOAT(pos.X, "x");
lua_pop(L, 1);
lua_getfield(L, index, "y");
CHECK_POS_COORD("y");
pos.Y = lua_tonumber(L, -1);
CHECK_FLOAT(pos.Y, "y");
lua_pop(L, 1);
lua_getfield(L, index, "z");
CHECK_POS_COORD("z");
pos.Z = lua_tonumber(L, -1);
CHECK_FLOAT(pos.Z, "z");
lua_pop(L, 1);
return pos;
read_v3_aux(L, index);
CHECK_POS_COORD(-3, "x");
CHECK_POS_COORD(-2, "y");
CHECK_POS_COORD(-1, "z");
double x = lua_tonumber(L, -3);
double y = lua_tonumber(L, -2);
double z = lua_tonumber(L, -1);
lua_pop(L, 3);
return v3d(x, y, z);
}
void push_ARGB8(lua_State *L, video::SColor color)
@ -274,14 +243,11 @@ v3f checkFloatPos(lua_State *L, int index)
void push_v3s16(lua_State *L, v3s16 p)
{
lua_createtable(L, 0, 3);
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_PUSH_VECTOR);
lua_pushinteger(L, p.X);
lua_setfield(L, -2, "x");
lua_pushinteger(L, p.Y);
lua_setfield(L, -2, "y");
lua_pushinteger(L, p.Z);
lua_setfield(L, -2, "z");
set_vector_metatable(L);
lua_call(L, 3, 1);
}
v3s16 read_v3s16(lua_State *L, int index)