1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-22 17:18:39 +00:00

Add Lua API function to resolve node/collision/selection boxes (#13964)

This commit is contained in:
grorp 2024-02-06 20:45:16 +01:00 committed by GitHub
parent 4859cf44ce
commit f2b99332d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 159 additions and 28 deletions

View file

@ -1110,7 +1110,7 @@ void push_nodebox(lua_State *L, const NodeBox &box)
case NODEBOX_FIXED:
lua_pushstring(L, "fixed");
lua_setfield(L, -2, "type");
push_box(L, box.fixed);
push_aabb3f_vector(L, box.fixed);
lua_setfield(L, -2, "fixed");
break;
case NODEBOX_WALLMOUNTED:
@ -1127,17 +1127,17 @@ void push_nodebox(lua_State *L, const NodeBox &box)
lua_pushstring(L, "connected");
lua_setfield(L, -2, "type");
const auto &c = box.getConnected();
push_box(L, c.connect_top);
push_aabb3f_vector(L, c.connect_top);
lua_setfield(L, -2, "connect_top");
push_box(L, c.connect_bottom);
push_aabb3f_vector(L, c.connect_bottom);
lua_setfield(L, -2, "connect_bottom");
push_box(L, c.connect_front);
push_aabb3f_vector(L, c.connect_front);
lua_setfield(L, -2, "connect_front");
push_box(L, c.connect_back);
push_aabb3f_vector(L, c.connect_back);
lua_setfield(L, -2, "connect_back");
push_box(L, c.connect_left);
push_aabb3f_vector(L, c.connect_left);
lua_setfield(L, -2, "connect_left");
push_box(L, c.connect_right);
push_aabb3f_vector(L, c.connect_right);
lua_setfield(L, -2, "connect_right");
// half the boxes are missing here?
break;
@ -1148,16 +1148,6 @@ void push_nodebox(lua_State *L, const NodeBox &box)
}
}
void push_box(lua_State *L, const std::vector<aabb3f> &box)
{
lua_createtable(L, box.size(), 0);
u8 i = 1;
for (const aabb3f &it : box) {
push_aabb3f(L, it);
lua_rawseti(L, -2, i++);
}
}
/******************************************************************************/
void push_palette(lua_State *L, const std::vector<video::SColor> *palette)
{

View file

@ -83,9 +83,6 @@ void push_content_features (lua_State *L,
void push_nodebox (lua_State *L,
const NodeBox &box);
void push_box (lua_State *L,
const std::vector<aabb3f> &box);
void push_palette (lua_State *L,
const std::vector<video::SColor> *palette);

View file

@ -364,20 +364,20 @@ aabb3f read_aabb3f(lua_State *L, int index, f32 scale)
return box;
}
void push_aabb3f(lua_State *L, aabb3f box)
void push_aabb3f(lua_State *L, aabb3f box, f32 divisor)
{
lua_createtable(L, 6, 0);
lua_pushnumber(L, box.MinEdge.X);
lua_pushnumber(L, box.MinEdge.X / divisor);
lua_rawseti(L, -2, 1);
lua_pushnumber(L, box.MinEdge.Y);
lua_pushnumber(L, box.MinEdge.Y / divisor);
lua_rawseti(L, -2, 2);
lua_pushnumber(L, box.MinEdge.Z);
lua_pushnumber(L, box.MinEdge.Z / divisor);
lua_rawseti(L, -2, 3);
lua_pushnumber(L, box.MaxEdge.X);
lua_pushnumber(L, box.MaxEdge.X / divisor);
lua_rawseti(L, -2, 4);
lua_pushnumber(L, box.MaxEdge.Y);
lua_pushnumber(L, box.MaxEdge.Y / divisor);
lua_rawseti(L, -2, 5);
lua_pushnumber(L, box.MaxEdge.Z);
lua_pushnumber(L, box.MaxEdge.Z / divisor);
lua_rawseti(L, -2, 6);
}
@ -409,6 +409,16 @@ std::vector<aabb3f> read_aabb3f_vector(lua_State *L, int index, f32 scale)
return boxes;
}
void push_aabb3f_vector(lua_State *L, const std::vector<aabb3f> &boxes, f32 divisor)
{
lua_createtable(L, boxes.size(), 0);
int i = 1;
for (const aabb3f &box : boxes) {
push_aabb3f(L, box, divisor);
lua_rawseti(L, -2, i++);
}
}
size_t read_stringlist(lua_State *L, int index, std::vector<std::string> *result)
{
if (index < 0)

View file

@ -110,11 +110,13 @@ void push_v2s16 (lua_State *L, v2s16 p);
void push_v2s32 (lua_State *L, v2s32 p);
void push_v2u32 (lua_State *L, v2u32 p);
void push_v3s16 (lua_State *L, v3s16 p);
void push_aabb3f (lua_State *L, aabb3f box);
void push_aabb3f (lua_State *L, aabb3f box, f32 divisor = 1.0f);
void push_ARGB8 (lua_State *L, video::SColor color);
void pushFloatPos (lua_State *L, v3f p);
void push_v3f (lua_State *L, v3f p);
void push_v2f (lua_State *L, v2f p);
void push_aabb3f_vector (lua_State *L, const std::vector<aabb3f> &boxes,
f32 divisor = 1.0f);
void warn_if_field_exists(lua_State *L, int table,
const char *fieldname,

View file

@ -562,6 +562,40 @@ int ModApiEnv::l_add_node_level(lua_State *L)
return 1;
}
// get_node_boxes(box_type, pos, [node]) -> table
// box_type = string
// pos = {x=num, y=num, z=num}
// node = {name=string, param1=num, param2=num} or nil
int ModApiEnv::l_get_node_boxes(lua_State *L)
{
GET_ENV_PTR;
std::string box_type = luaL_checkstring(L, 1);
v3s16 pos = read_v3s16(L, 2);
MapNode n;
if (lua_istable(L, 3))
n = readnode(L, 3);
else
n = env->getMap().getNode(pos);
u8 neighbors = n.getNeighbors(pos, &env->getMap());
const NodeDefManager *ndef = env->getGameDef()->ndef();
std::vector<aabb3f> boxes;
if (box_type == "node_box")
n.getNodeBoxes(ndef, &boxes, neighbors);
else if (box_type == "collision_box")
n.getCollisionBoxes(ndef, &boxes, neighbors);
else if (box_type == "selection_box")
n.getSelectionBoxes(ndef, &boxes, neighbors);
else
luaL_error(L, "get_node_boxes: box_type is invalid. Allowed values: \"node_box\", \"collision_box\", \"selection_box\"");
push_aabb3f_vector(L, boxes, BS);
return 1;
}
// find_nodes_with_meta(pos1, pos2)
int ModApiEnv::l_find_nodes_with_meta(lua_State *L)
{
@ -1456,6 +1490,7 @@ void ModApiEnv::Initialize(lua_State *L, int top)
API_FCT(get_node_level);
API_FCT(set_node_level);
API_FCT(add_node_level);
API_FCT(get_node_boxes);
API_FCT(add_entity);
API_FCT(find_nodes_with_meta);
API_FCT(get_meta);

View file

@ -120,6 +120,12 @@ private:
// pos = {x=num, y=num, z=num}
static int l_add_node_level(lua_State *L);
// get_node_boxes(box_type, pos, [node]) -> table
// box_type = string
// pos = {x=num, y=num, z=num}
// node = {name=string, param1=num, param2=num} or nil
static int l_get_node_boxes(lua_State *L);
// find_nodes_with_meta(pos1, pos2)
static int l_find_nodes_with_meta(lua_State *L);