--Helper function to see if there is open space to place a double wide node.
--All wide nodes should extend to the right.
--Returns true if item should be placeable.

--[[
### Use this code on nodes that need to check space to side.
Replace placed_node with the node that should be placed. Leave blank to use the default invisible item.

after_place_node = function(pos, placer, itemstack)
   if not epic.space_to_side(pos, placed_node) then
      minetest.remove_node(pos)
      return itemstack
   end
end,

### Use this code on nodes that need to remove the empty node.

after_dig_node = function(pos, oldnode)
   epic.remove_side_node(pos, oldnode)
end,

]]
fdir_table = {
   {  1,  0 },
   {  0, -1 },
   { -1,  0 },
   {  0,  1 },
   {  1,  0 },
   {  0, -1 },
   { -1,  0 },
   {  0,  1 },
}

function epic.space_to_side(pos, placed_node)
   local node = minetest.get_node(pos)
   local fdir = node.param2 % 32
   local pos2 = {x = pos.x + fdir_table[fdir+1][1], y=pos.y, z = pos.z + fdir_table[fdir+1][2]}
   local node2 = minetest.get_node(pos2)
   local node2def = minetest.registered_nodes[node2.name] or nil
   if not node2def.buildable_to then
      return false
   else
      local placed_node = placed_node or 'epic:empty'
      minetest.set_node(pos2,{name = placed_node, param2=fdir})
      return true
   end
end

function epic.remove_side_node(pos, oldnode)
   local fdir = oldnode.param2 % 32
   local pos2 = {x = pos.x + fdir_table[fdir+1][1], y=pos.y, z = pos.z + fdir_table[fdir+1][2]}
   local node2 = minetest.get_node(pos2).name
   if minetest.get_item_group(node2, 'empty_node') > 0 then
      minetest.remove_node(pos2)
   end
end

function epic.space_to_front(pos)
   local node = minetest.get_node(pos)
   local fdir = node.param2 % 32
   local pos2 = {x = pos.x + fdir_table[fdir+4][1], y=pos.y, z = pos.z + fdir_table[fdir+4][2]}
   local node2 = minetest.get_node(pos2)
   local node2def = minetest.registered_nodes[node2.name] or nil
   if not node2def.buildable_to then
      return false
   else
      minetest.set_node(pos2,{name = 'epic:empty'})
      return true
   end
end

function epic.remove_front_node(pos, oldnode)
   local fdir = oldnode.param2 % 32
   local pos2 = {x = pos.x + fdir_table[fdir+4][1], y=pos.y, z = pos.z + fdir_table[fdir+4][2]}
   local node2 = minetest.get_node(pos2).name
   if minetest.get_item_group(node2, 'empty_node') > 0 then
      minetest.remove_node(pos2)
   end
end

function epic.space_on_top(pos)
   local above = {x = pos.x, y = pos.y + 1, z = pos.z}
   local top_node = minetest.get_node(above)
   local top_def = minetest.registered_nodes[top_node.name] or nil
   if not top_def.buildable_to then
      return false
   else
      minetest.set_node(above,{name='epic:empty'})
      return true
   end
end

function epic.remove_top_node(pos)
   local above = {x = pos.x, y = pos.y + 1, z = pos.z}
   local top_node = minetest.get_node(above).name
   if minetest.get_item_group(top_node, 'empty_node') > 0 then
      minetest.remove_node(above)
   end
end

function epic.space_to_top_and_side(pos)
   local node = minetest.get_node(pos)
   local fdir = node.param2 % 32
   local side = {x = pos.x + fdir_table[fdir+1][1], y=pos.y, z = pos.z + fdir_table[fdir+1][2]}
   local top = {x = pos.x, y = pos.y + 1, z = pos.z}
   local side_node = minetest.get_node(side)
   local top_node = minetest.get_node(top)
   local side_def = minetest.registered_nodes[side_node.name] or nil
   local top_def = minetest.registered_nodes[top_node.name] or nil
   if not top_def.buildable_to or not side_def.buildable_to then
      return false
   else
      minetest.set_node(side,{name='epic:empty'})
      minetest.set_node(top,{name='epic:empty'})
      return true
   end
end

function epic.space_for_picnic(pos, placed_node)
   local node = minetest.get_node(pos)
   local fdir = node.param2 % 32
   local pos2 = {x = pos.x + fdir_table[fdir+1][1], y=pos.y, z = pos.z + fdir_table[fdir+1][2]} --Side node
   local pos3 = {x = pos.x + fdir_table[fdir+4][1], y=pos.y, z = pos.z + fdir_table[fdir+4][2]} --Across node
   local node2 = minetest.get_node(pos2)
   local node3 = minetest.get_node(pos3)
   local node2def = minetest.registered_nodes[node2.name] or nil
   local node3def = minetest.registered_nodes[node3.name] or nil
   if not node2def.buildable_to or not node3def.buildable_to then
      return false
   else
      local placed_node = placed_node or 'epic:empty'
      minetest.set_node(pos2,{name = placed_node, param2=fdir})
      minetest.set_node(pos3,{name = placed_node, param2=fdir})
      return true
   end
end

function epic.remove_picnic_nodes(pos, oldnode)
   local fdir = oldnode.param2 % 32
   local pos2 = {x = pos.x + fdir_table[fdir+1][1], y=pos.y, z = pos.z + fdir_table[fdir+1][2]} --Side node
   local pos3 = {x = pos.x + fdir_table[fdir+4][1], y=pos.y, z = pos.z + fdir_table[fdir+4][2]} --Across node
   local node2 = minetest.get_node(pos2).name
   local node3 = minetest.get_node(pos3).name
   if minetest.get_item_group(node2, 'empty_node') > 0 and minetest.get_item_group(node3, 'empty_node') >0 then
      minetest.remove_node(pos2)
      minetest.remove_node(pos3)
   end
end
