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

@ -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
@ -97,9 +72,9 @@ local function get_entities(pos)
if ndef and ndef.display_entities then if ndef and ndef.display_entities then
for _, objref in ipairs(minetest.get_objects_inside_radius(pos, 1.5)) do for _, objref in ipairs(minetest.get_objects_inside_radius(pos, 1.5)) do
local entity = objref:get_luaentity() local entity = objref:get_luaentity()
if entity and ndef.display_entities[entity.name] and check_entity_pos(pos, objref) then if entity and ndef.display_entities[entity.name] and check_entity_pos(pos, objref) then
if objrefs[entity.name] then if objrefs[entity.name] then
objref:remove() objref:remove()
else else
objrefs[entity.name] = objref objrefs[entity.name] = objref
end end
@ -123,9 +98,9 @@ local function place_entities(pos)
local ndef = minetest.registered_nodes[node.name] local ndef = minetest.registered_nodes[node.name]
local values = get_values(node) local values = get_values(node)
local objrefs = get_entities(pos) local objrefs = get_entities(pos)
if values and ndef and ndef.display_entities then if values 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)
@ -133,12 +108,12 @@ local function place_entities(pos)
if not objrefs[entity_name] then if not objrefs[entity_name] then
objrefs[entity_name] = minetest.add_entity(pos, entity_name) objrefs[entity_name] = minetest.add_entity(pos, entity_name)
end end
objrefs[entity_name]:setpos({ objrefs[entity_name]:setpos({
x = pos.x - values.dx * depth + values.rx * right, x = pos.x - values.dx * depth + values.rx * right,
y = pos.y - top, y = pos.y - top,
z = pos.z - values.dz * depth + values.rz * right}) z = pos.z - values.dz * depth + values.rz * right})
objrefs[entity_name]:setyaw(values.yaw) objrefs[entity_name]:setyaw(values.yaw)
end end
end end
@ -151,7 +126,7 @@ local function call_node_on_display_update(pos, objref)
local entity = objref:get_luaentity() local entity = objref:get_luaentity()
if ndef and ndef.display_entities and entity and ndef.display_entities[entity.name] then if ndef and ndef.display_entities and entity and ndef.display_entities[entity.name] then
ndef.display_entities[entity.name].on_display_update(pos, objref) ndef.display_entities[entity.name].on_display_update(pos, objref)
end end
end end
--- Force entity update --- Force entity update
@ -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

@ -132,18 +132,19 @@ 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]
if ndef.signs_other_dir then if ndef.signs_other_dir then
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)
end return true
else end
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)