-screwdriver.rotate.wallmounted = function(node, mode)
- return wallmounted_tbl[mode][node.param2]
+
+screwdriver.rotate.wallmounted = function(pos, node, mode)
+ local rotation = node.param2 % 8 -- get first 3 bits
+ local other = node.param2 - rotation
+ rotation = wallmounted_tbl[mode][rotation] or 0
+ if minetest.get_item_group(node.name, "attached_node") ~= 0 then
+ -- find an acceptable orientation
+ for i = 1, 5 do
+ if not check_attached_node(pos, rotation) then
+ rotation = wallmounted_tbl[mode][rotation] or 0
+ else
+ break
+ end
+ end
+ end
+ return rotation + other