--[[
jacobs_ladder rev004
original code by dgm5555
from Build Wall semi-automatically mod
https://forum.minetest.net/viewtopic.php?f=9&t=11565
License: WTFPL v2
--]]

jacobs_ladder = {}

minetest.register_node("jacobs_ladder:ladder", {
	description = "Jacobs Ladder",
 tiles = {"jacobs_ladder_mat.png"},
	inventory_image = "jacobs_ladder_inv.png",
	drawtype = "nodebox",
	paramtype = "light",
	paramtype2 = "facedir",
	paramtype3 = "wallmounted",
	legacy_wallmounted = true,
	light_source = 14,
	climbable = true,
	walkable = false,
	groups = {choppy=3,dig_immediate=1, attached_node=1},
drop="",
	sounds = default.node_sound_wood_defaults(),
		node_box = {
		type = "fixed",
		fixed = {
			{-0.5, 0.1875, 0.375, 0.5, 0.3125, 0.5}, -- NodeBox1
			{-0.5, -0.3125, 0.375, 0.5, -0.1875, 0.5}, -- NodeBox2
			{-0.5, -0.5, 0.375, -0.375, 0.5, 0.5}, -- NodeBox3
			{0.375, -0.5, 0.375, 0.5, 0.5, 0.5}, -- NodeBox4
		}
	},
	selection_box = {
		type = "fixed",
		fixed = {
			{-0.5, 0.1875, 0.375, 0.5, 0.3125, 0.5}, -- NodeBox1
			{-0.5, -0.3125, 0.375, 0.5, -0.1875, 0.5}, -- NodeBox2
			{-0.5, -0.5, 0.375, -0.375, 0.5, 0.5}, -- NodeBox3
			{0.375, -0.5, 0.375, 0.5, 0.5, 0.5}, -- NodeBox4
		}
	},
})


minetest.register_on_punchnode(
   function(pos, node, puncher)
      -- This grows a block to the targetHeight
      manip = minetest.get_voxel_manip()
      if node.name == "jacobs_ladder:ladder" and -- thanks Batman :)
    puncher:get_wielded_item():get_name() == "jacobs_ladder:ladder"then
         -- This just creates a 'tower' above the node you punch
         local targetHeight = 5
         growblock(node.name, targetHeight, pos, puncher)
      end
      --[[
      if node.name == "jacobs_ladder:ladder" then
         -- This grows any blocks at the same level with air above, and will follow a line of them...
         local targetHeight =5
         checkSurround(node.name, targetHeight, pos)
      end
      --]]
   end
)


function checkSurround(nodeName, targetHeight, pos)
   for dx=-1,1 do
      for dz=-1,1 do
         local p = {x=pos.x+dx, y=pos.y, z=pos.z+dz}
         manip:read_from_map(p, p)
         --minetest.chat_send_all("growwall_1")
         if (minetest.env:get_node(p).name == nodeName) then
            --minetest.chat_send_all("growwall_2")
            local p1 = {x=pos.x+dx, y=pos.y+1, z=pos.z+dz}
            manip:read_from_map(p1, p1)
            if (minetest.env:get_node(p1).name == "air") then
               --minetest.chat_send_all("growwall_3")
               growblock(nodeName, targetHeight, p)
               checkSurround(nodeName, targetHeight, p)
            end
         end
      end
   end
end


function growblock(blockName, targetHeight, pos, puncher)
   for dy=0,targetHeight do
      local p = {x=pos.x, y=math.floor(pos.y+dy), z=pos.z}
      manip:read_from_map(p, p)

      local n = minetest.env:get_node(p).name
      if (n == "air") then
         minetest.env:set_node(p, {name = "jacobs_ladder:ladder", param2=minetest.dir_to_facedir(puncher:get_look_dir())})
      end
   end
end