Skip to content

Fix minetest.get_node_boxes for connected node boxes #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions minetest/boxes.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- Localize globals
local assert, ipairs, math, minetest, pairs, table, type, vector
= assert, ipairs, math, minetest, pairs, table, type, vector
local assert, ipairs, math, minetest, table, type, vector
= assert, ipairs, math, minetest, table, type, vector

-- Set environment
local _ENV = ...
Expand Down Expand Up @@ -34,7 +34,7 @@ local function get_node_boxes(pos, type)
if box_type == "leveled" then
boxes = table.copy(boxes)
local level = (paramtype2 == "leveled" and node.param2 or node_def.leveled or 0) / 255 - 0.5
for _, box in pairs(boxes) do
for _, box in ipairs(boxes) do
box[5] = level
end
elseif box_type == "wallmounted" then
Expand All @@ -61,44 +61,50 @@ local function get_node_boxes(pos, type)
end
if box_type == "connected" then
boxes = table.copy(boxes)
local connect_sides = {
top = {x = 0, y = 1, z = 0},
bottom = {x = 0, y = -1, z = 0},
front = {x = 0, y = 0, z = -1},
left = {x = -1, y = 0, z = 0},
back = {x = 0, y = 0, z = 1},
right = {x = 1, y = 0, z = 0}
local connect_sides = { -- don't use a dictionary, they must remain in this order
{"top", {x = 0, y = 1, z = 0}},
{"bottom", {x = 0, y = -1, z = 0}},
{"front", {x = 0, y = 0, z = -1}},
{"left", {x = -1, y = 0, z = 0}},
{"back", {x = 0, y = 0, z = 1}},
{"right", {x = 1, y = 0, z = 0}},
}
if node_def.connect_sides then
for side in pairs(connect_sides) do
if not node_def.connect_sides[side] then
connect_sides[side] = nil
local include = {}
for _, side in ipairs(node_def.connect_sides) do
include[side] = true
end
local new_connect_sides = {}
for _, pair in ipairs(connect_sides) do
if include[pair[1]] then
table.insert(new_connect_sides, pair)
end
end
connect_sides = new_connect_sides
end
local function add_collisionbox(key)
for _, box in ipairs(get_boxes(def_box[key] or {})) do
table.insert(boxes, box)
end
end
local matchers = {}
for _, nodename_or_group in pairs(node_def.connects_to or {}) do
for _, nodename_or_group in ipairs(node_def.connects_to or {}) do
table.insert(matchers, nodename_matcher(nodename_or_group))
end
local function connects_to(nodename)
for _, matcher in pairs(matchers) do
for _, matcher in ipairs(matchers) do
if matcher(nodename) then
return true
end
end
end
local connected, connected_sides
for side, direction in pairs(connect_sides) do
local neighbor = minetest.get_node(vector.add(pos, direction))
for _, pair in ipairs(connect_sides) do
local neighbor = minetest.get_node(vector.add(pos, pair[2]))
local connects = connects_to(neighbor.name)
connected = connected or connects
connected_sides = connected_sides or (side ~= "top" and side ~= "bottom")
add_collisionbox((connects and "connect_" or "disconnected_") .. side)
connected_sides = connected_sides or (pair[1] ~= "top" and pair[1] ~= "bottom")
add_collisionbox((connects and "connect_" or "disconnected_") .. pair[1])
end
if not connected then
add_collisionbox("disconnected")
Expand All @@ -121,7 +127,7 @@ local function get_node_boxes(pos, type)
if axis == 2 then
sin = -sin
end
for _, box in pairs(boxes) do
for _, box in ipairs(boxes) do
for off = 0, 3, 3 do
local axis_1, axis_2 = other_axis_1 + off, other_axis_2 + off
local value_1, value_2 = box[axis_1], box[axis_2]
Expand Down