Improve on_rotate functions (#21)

Fixed bugs and improve on_rotate and support color- paramtype2s
This commit is contained in:
12Me21 2018-09-26 13:41:30 -04:00 committed by Pierre-Yves Rollo
parent d22362a4ac
commit 44b911be9e
2 changed files with 41 additions and 67 deletions

View File

@ -26,55 +26,30 @@ display_api.entity_spacing = 0.002
-- Miscelaneous values depending on wallmounted param2 -- Miscelaneous values depending on wallmounted param2
local wallmounted_values = { local wallmounted_values = {
[0]={dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0}, -- Should never be used [2]={dx=-1, dz=0, rx=0, rz=-1, yaw=-math.pi/2},
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=1}, -- Should never be used [3]={dx=1, dz=0, rx=0, rz=1, yaw=math.pi/2 },
{dx=-1, dz=0, rx=0, rz=-1, yaw=-math.pi/2, rotate=5}, [4]={dx=0, dz=-1, rx=1, rz=0, yaw=0 },
{dx=1, dz=0, rx=0, rz=1, yaw=math.pi/2, rotate=4}, [5]={dx=0, dz=1, rx=-1, rz=0, yaw=math.pi }
{dx=0, dz=-1, rx=1, rz=0, yaw=0, rotate=2},
{dx=0, dz=1, rx=-1, rz=0, yaw=math.pi, rotate=3}
} }
-- Miscelaneous values depending on facedir param2 -- Miscelaneous values depending on facedir param2
local facedir_values = { local facedir_values = {
[0]={dx=0, dz=-1, rx=1, rz=0, yaw=0, rotate=1}, [0]={dx=0, dz=-1, rx=1, rz=0, yaw=0 },
{dx=-1, dz=0, rx=0, rz=-1, yaw=-math.pi/2, rotate=2}, [1]={dx=-1, dz=0, rx=0, rz=-1, yaw=-math.pi/2},
{dx=0, dz=1, rx=-1, rz=0, yaw=math.pi, rotate=3}, [2]={dx=0, dz=1, rx=-1, rz=0, yaw=math.pi },
{dx=1, dz=0, rx=0, rz=1, yaw=math.pi/2, rotate=0}, [3]={dx=1, dz=0, rx=0, rz=1, yaw=math.pi/2 }
-- Forbiden values : }
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
{dx=0, dz=0, rx=0, rz=0, yaw=0, rotate=0},
}
-- dx/dy = depth vector, rx/ly = right vector, yaw = yaw of entity, -- dx/dy = depth vector, rx/ly = right vector, yaw = yaw of entity,
-- rotate = next facedir/wallmount on rotate
local function get_values(node) local function get_values(node)
local ndef = minetest.registered_nodes[node.name] local ndef = minetest.registered_nodes[node.name]
if ndef then if ndef then
if ndef.paramtype2 == "wallmounted" then local paramtype2 = ndef.paramtype2
return wallmounted_values[node.param2] if paramtype2 == "wallmounted" or paramtype2 == "colorwallmounted" then
end return wallmounted_values[node.param2 % 8]
if ndef.paramtype2 == "facedir" then elseif paramtype2 == "facedir" or paramtype2 == "colorfacedir" then
return facedir_values[node.param2] return facedir_values[node.param2 % 32]
end end
end end
end end
@ -186,7 +161,7 @@ end
--- On_place callback for display_api items. Does nothing more than preventing item --- On_place callback for display_api items. Does nothing more than preventing item
--- from being placed on ceiling or ground --- from being placed on ceiling or ground
function display_api.on_place(itemstack, placer, pointed_thing) function display_api.on_place(itemstack, placer, pointed_thing, override_param2)
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
@ -200,15 +175,16 @@ function display_api.on_place(itemstack, placer, pointed_thing)
dir.y = 0 dir.y = 0
end end
local param2 local param2 = 0
if ndef then if ndef then
if ndef.paramtype2 == "wallmounted" then local paramtype2 = ndef.paramtype2
if paramtype2 == "wallmounted" or paramtype2 == "colorwallmounted" then
param2 = minetest.dir_to_wallmounted(dir) param2 = minetest.dir_to_wallmounted(dir)
elseif ndef.paramtype2 == "facedir" then elseif paramtype2 == "facedir" or paramtype2 == "colorfacedir" then
param2 = minetest.dir_to_facedir(dir) param2 = minetest.dir_to_facedir(dir)
end end
end end
return minetest.item_place(itemstack, placer, pointed_thing, param2) return minetest.item_place(itemstack, placer, pointed_thing, param2 + (override_param2 or 0))
end end
--- On_construct callback for display_api items. Creates entities and update them. --- On_construct callback for display_api items. Creates entities and update them.
@ -225,14 +201,11 @@ function display_api.on_destruct(pos)
end end
end end
-- On_rotate (screwdriver) callback for display_api items. Prevents axis rotation and reorients entities. -- On_rotate (screwdriver) callback for display_api items. Prevents invalid rotations and reorients entities.
function display_api.on_rotate(pos, node, user, mode, new_param2) function display_api.on_rotate(pos, node, user, _, new_param2)
if mode ~= 1 then return false end node.param2 = new_param2
if get_values(node) then
local values = get_values(node) minetest.swap_node(pos, node)
if values then
minetest.swap_node(pos, {name = node.name, param1 = node.param1, param2 = values.rotate})
place_entities(pos) place_entities(pos)
return true return true
else else

View File

@ -132,6 +132,8 @@ function signs_api.on_place_direction(itemstack, placer, pointed_thing)
end end
-- Handles screwdriver rotation. Direction is affected for direction signs -- Handles screwdriver rotation. Direction is affected for direction signs
-- If rotation mode is 2 and sign is directional, swap direction.
-- Otherwise use display_api's on_rotate function.
function signs_api.on_rotate(pos, node, player, mode, new_param2) function signs_api.on_rotate(pos, node, player, mode, new_param2)
if mode == 2 then if mode == 2 then
local ndef = minetest.registered_nodes[node.name] local ndef = minetest.registered_nodes[node.name]
@ -139,11 +141,10 @@ function signs_api.on_rotate(pos, node, player, mode, new_param2)
minetest.swap_node(pos, {name = ndef.signs_other_dir, minetest.swap_node(pos, {name = ndef.signs_other_dir,
param1 = node.param1, param2 = node.param2}) param1 = node.param1, param2 = node.param2})
display_api.update_entities(pos) display_api.update_entities(pos)
return true
end end
else
display_api.on_rotate(pos, node, user, mode, new_param2)
end end
return false; return display_api.on_rotate(pos, node, user, mode, new_param2)
end end
function signs_api.register_sign(mod, name, model) function signs_api.register_sign(mod, name, model)