Use MT5 set_rotation to allow all rotations of nodes

This commit is contained in:
Pierre-Yves Rollo 2019-03-09 20:24:53 +01:00
parent b43c7046d7
commit 2e98900cc0
3 changed files with 143 additions and 41 deletions

View File

@ -22,26 +22,81 @@
-- variable as spacing between entity and node -- variable as spacing between entity and node
display_api.entity_spacing = 0.002 display_api.entity_spacing = 0.002
-- Settings
display_api.rotation_restriction =
minetest.settings:get_bool("display_rotation_restriction", true)
if display_api.rotation_restriction then
minetest.log("action", "[display_api] Legacy rotation restriction in effect")
end
-- Maximum entity position relative to the node pos -- Maximum entity position relative to the node pos
local max_entity_pos = 1.5 local max_entity_pos = 1.5
-- Miscelaneous values depending on wallmounted param2 local wallmounted_rotations, facedir_rotations
local wallmounted_values = {
[2]={dx=-1, dz=0, rx=0, rz=-1, yaw=-math.pi/2},
[3]={dx=1, dz=0, rx=0, rz=1, yaw=math.pi/2 },
[4]={dx=0, dz=-1, rx=1, rz=0, yaw=0 },
[5]={dx=0, dz=1, rx=-1, rz=0, yaw=math.pi }
}
-- Miscelaneous values depending on facedir param2 if display_api.rotation_restriction then
local facedir_values = { -- Legacy rotations (MT<5.0)
[0]={dx=0, dz=-1, rx=1, rz=0, yaw=0 }, wallmounted_rotations = {
[1]={dx=-1, dz=0, rx=0, rz=-1, yaw=-math.pi/2}, [2]={x=0, y=3, z=0}, [3]={x=0, y=1, z=0},
[2]={dx=0, dz=1, rx=-1, rz=0, yaw=math.pi }, [4]={x=0, y=0, z=0}, [5]={x=0, y=2, z=0},
[3]={dx=1, dz=0, rx=0, rz=1, yaw=math.pi/2 } }
} facedir_rotations = {
[0]={x=0, y=0, z=0}, [1]={x=0, y=3, z=0},
[2]={x=0, y=2, z=0}, [3]={x=0, y=1, z=0},
}
else
-- Full rotations (MT>=5.0)
wallmounted_rotations = {
[0]={x=1, y=0, z=0}, [1]={x=3, y=0, z=0},
[2]={x=0, y=3, z=0}, [3]={x=0, y=1, z=0},
[4]={x=0, y=0, z=0}, [5]={x=0, y=2, z=0},
}
facedir_rotations = {
[ 0]={x=0, y=0, z=0}, [ 1]={x=0, y=3, z=0},
[ 2]={x=0, y=2, z=0}, [ 3]={x=0, y=1, z=0},
[ 4]={x=3, y=0, z=0}, [ 5]={x=0, y=3, z=3},
[ 6]={x=1, y=0, z=2}, [ 7]={x=0, y=1, z=1},
[ 8]={x=1, y=0, z=0}, [ 9]={x=0, y=3, z=1},
[10]={x=3, y=0, z=2}, [11]={x=0, y=1, z=3},
[12]={x=0, y=0, z=1}, [13]={x=3, y=0, z=1},
[14]={x=2, y=0, z=1}, [15]={x=1, y=0, z=1},
[16]={x=0, y=0, z=3}, [17]={x=1, y=0, z=3},
[18]={x=2, y=0, z=3}, [19]={x=3, y=0, z=3},
[20]={x=0, y=0, z=2}, [21]={x=0, y=1, z=2},
[22]={x=0, y=2, z=2}, [23]={x=0, y=3, z=2},
}
end
-- Compute other useful values depending on wallmounted and facedir param
local wallmounted_values = {}
local facedir_values = {}
local function compute_values(r)
local function rx(v) return { x=v.x, y=v.z, z=-v.y} end
local function ry(v) return { x=-v.z, y=v.y, z=v.x} end
local function rz(v) return { x=v.y, y=-v.x, z=v.z} end
local d = { x = 0, y = 0, z = 1 }
local w = { x = 1, y = 0, z = 0 }
local h = { x = 0, y = 1, z = 0 }
-- Important to keep z rotation first (not same results)
for _ = 1, r.z do d, w, h = rz(d), rz(w), rz(h) end
for _ = 1, r.x do d, w, h = rx(d), rx(w), rx(h) end
for _ = 1, r.y do d, w, h = ry(d), ry(w), ry(h) end
return {rotation=r, depth=d, width=w, height=h}
end
for i, r in pairs(facedir_rotations) do
facedir_values[i] = compute_values(r)
end
for i, r in pairs(wallmounted_rotations) do
wallmounted_values[i] = compute_values(r)
end
-- dx/dy = depth vector, rx/ly = right vector, yaw = yaw of entity,
local function get_values(node) local function get_values(node)
local ndef = minetest.registered_nodes[node.name] local ndef = minetest.registered_nodes[node.name]
@ -88,10 +143,10 @@ end
local function place_entities(pos) local function place_entities(pos)
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
local ndef = minetest.registered_nodes[node.name] local ndef = minetest.registered_nodes[node.name]
local values = get_values(node) local v = get_values(node)
local objrefs = get_entities(pos) local objrefs = get_entities(pos)
if values and ndef and ndef.display_entities then if v and ndef and ndef.display_entities then
for entity_name, props in pairs(ndef.display_entities) do for entity_name, props in pairs(ndef.display_entities) do
local depth = clip_pos_prop(props.depth) local depth = clip_pos_prop(props.depth)
local right = clip_pos_prop(props.right) local right = clip_pos_prop(props.right)
@ -102,11 +157,20 @@ local function place_entities(pos)
end end
objrefs[entity_name]:set_pos({ objrefs[entity_name]:set_pos({
x = pos.x - values.dx * depth + values.rx * right, x = pos.x + v.depth.x*depth + v.width.x*right - v.height.x*top,
y = pos.y - top, y = pos.y + v.depth.y*depth + v.width.y*right - v.height.y*top,
z = pos.z - values.dz * depth + values.rz * right}) z = pos.z + v.depth.z*depth + v.width.z*right - v.height.z*top,
})
objrefs[entity_name]:set_yaw(values.yaw + (props.yaw or 0)) if objrefs[entity_name].set_rotation then
objrefs[entity_name]:set_rotation({
x = v.rotation.x*math.pi/2,
y = v.rotation.y*math.pi/2 + (props.yaw or 0),
z = v.rotation.z*math.pi/2,
})
else -- For minetest < 5.0 -- TODO: To be removed in the future
objrefs[entity_name]:set_yaw(values.rotation.y + (props.yaw or 0))
end
end end
end end
return objrefs return objrefs
@ -162,7 +226,12 @@ end
--- On_place callback for display_api items. --- On_place callback for display_api items.
-- Does nothing more than preventing node from being placed on ceiling or ground -- Does nothing more than preventing node from being placed on ceiling or ground
-- TODO:When MT<5 is not in use anymore, this should be deprecated
function display_api.on_place(itemstack, placer, pointed_thing, override_param2) function display_api.on_place(itemstack, placer, pointed_thing, override_param2)
if not display_api.rotation_restriction then
return minetest.item_place(itemstack, placer, pointed_thing, override_param2)
end
local ndef = itemstack:get_definition() local ndef = itemstack:get_definition()
local above = pointed_thing.above local above = pointed_thing.above
local under = pointed_thing.under local under = pointed_thing.under

View File

@ -0,0 +1 @@
display_rotation_restriction(Legacy nodes rotation restriction MT<5.0) bool true

View File

@ -92,36 +92,40 @@ function signs_api.on_place_direction(itemstack, placer, pointed_thing)
local name = itemstack:get_name() local name = itemstack:get_name()
local ndef = minetest.registered_nodes[name] local ndef = minetest.registered_nodes[name]
local bdir = {x = pointed_thing.under.x - pointed_thing.above.x, local bdir = {
y = pointed_thing.under.y - pointed_thing.above.y, x = pointed_thing.under.x - pointed_thing.above.x,
z = pointed_thing.under.z - pointed_thing.above.z} y = pointed_thing.under.y - pointed_thing.above.y,
z = pointed_thing.under.z - pointed_thing.above.z}
local pdir = placer:get_look_dir() local pdir = placer:get_look_dir()
local ndir, test local ndir, test
if ndef.paramtype2 == "facedir" then if ndef.paramtype2 == "facedir" then
if bdir.x == 0 and bdir.z == 0 then -- If legacy mode, only accept upright nodes
if display_api.rotation_restriction and bdir.x == 0 and bdir.z == 0 then
-- Ceiling or floor pointed (facedir chosen from player dir) -- Ceiling or floor pointed (facedir chosen from player dir)
ndir = minetest.dir_to_facedir({x=pdir.x, y=0, z=pdir.z}) ndir = minetest.dir_to_facedir({x=pdir.x, y=0, z=pdir.z})
else else
-- Wall pointed -- Wall pointed or no rotation restriction
ndir = minetest.dir_to_facedir(bdir) ndir = minetest.dir_to_facedir(bdir, not display_api.rotation_restriction)
end end
test = {[0]=-pdir.x, pdir.z, pdir.x, -pdir.z} test = { [0]=-pdir.x, pdir.z, pdir.x, -pdir.z, -pdir.x, [8]=pdir.x }
end end
if ndef.paramtype2 == "wallmounted" then if ndef.paramtype2 == "wallmounted" then
ndir = minetest.dir_to_wallmounted(bdir) ndir = minetest.dir_to_wallmounted(bdir)
if ndir == 0 or ndir == 1 then -- If legacy mode, only accept upright nodes
-- Ceiling or floor if display_api.rotation_restriction and (ndir == 0 or ndir == 1) then
ndir = minetest.dir_to_wallmounted({x=pdir.x, y=0, z=pdir.z}) ndir = minetest.dir_to_wallmounted({x=pdir.x, y=0, z=pdir.z})
end end
test = {0, pdir.z, -pdir.z, -pdir.x, pdir.x} test = { [0]=-pdir.x, -pdir.x, pdir.z, -pdir.z, -pdir.x, pdir.x}
end end
-- Only for direction signs -- Only for direction signs
-- TODO:Maybe improve ground and ceiling placement in every directions
if ndef.signs_other_dir then if ndef.signs_other_dir then
if test[ndir] > 0 then if test[ndir] > 0 then
itemstack:set_name(ndef.signs_other_dir) itemstack:set_name(ndef.signs_other_dir)
@ -135,20 +139,48 @@ function signs_api.on_place_direction(itemstack, placer, pointed_thing)
end end
end end
-- Handles screwdriver rotation. Direction is affected for direction signs -- Handles screwdriver rotation
-- If rotation mode is 2 and sign is directional, swap direction. -- (see "if" block below for rotation restriction mode).
-- Otherwise use display_api's on_rotate function. signs_api.on_rotate = function(pos, node, player, mode, new_param2)
function signs_api.on_rotate(pos, node, player, mode, new_param2) -- If rotation mode is 1 and sign is directional, swap direction between
if mode == 2 then -- each rotation.
if mode == 1 then
local ndef = minetest.registered_nodes[node.name] local ndef = minetest.registered_nodes[node.name]
if ndef.signs_other_dir then if ndef.signs_other_dir then
minetest.swap_node(pos, {name = ndef.signs_other_dir, -- Switch direction
param1 = node.param1, param2 = node.param2}) node = {name = ndef.signs_other_dir,
param1 = node.param1, param2 = node.param2}
minetest.swap_node(pos, node)
display_api.update_entities(pos) display_api.update_entities(pos)
return true -- Rotate only if not "main" sign
-- TODO:Improve detection of "main" direction sign
if ndef.groups and ndef.groups.not_in_creative_inventory then
return display_api.on_rotate(pos, node, player, mode, new_param2)
else
return true
end
end end
end end
return display_api.on_rotate(pos, node, user, mode, new_param2) return display_api.on_rotate(pos, node, player, mode, new_param2)
end
-- Legacy mode with rotation restriction
-- TODO:When MT < 5.0 no more in use, to be removed
if display_api.rotation_restriction then
signs_api.on_rotate = function(pos, node, player, mode, new_param2)
-- If rotation mode is 2 and sign is directional, swap direction.
-- Otherwise use display_api's on_rotate function.
if mode == 2 then
local ndef = minetest.registered_nodes[node.name]
if ndef.signs_other_dir then
minetest.swap_node(pos, {name = ndef.signs_other_dir,
param1 = node.param1, param2 = node.param2})
display_api.update_entities(pos)
return true
end
end
return display_api.on_rotate(pos, node, player, mode, new_param2)
end
end end
function signs_api.register_sign(mod, name, model) function signs_api.register_sign(mod, name, model)