local pp_col_box_1 = {
   type = 'fixed',
   fixed = {{-.2, -.5, -.2, .2, .0, .2}}
}

local pp_col_box_2 = {
   type = 'fixed',
   fixed = {{-.6, -.5, -.2, .2, .2, .2}}
}

local pp_col_box_3 = {
   type = 'fixed',
   fixed = {{-.6, -.5, -.2, .6, .2, .2}}
}

local pp_col_box_4 = {
   type = 'fixed',
   fixed = {{-.6, -.5, -.2, .6, .45, .2}}
}

local pp_col_box_5 = {
   type = 'fixed',
   fixed = {{-.7, -.5, -.2, .7, .7, .2}}
}

local pp_col_box_6 = {
   type = 'fixed',
   fixed = {{-.7, -.5, -.2, .7, .7, .2}}
}

local pp_col_box_7 = {
   type = 'fixed',
   fixed = {{-.6, -.5, -.2, .6, .9, .2}}
}

local prickly_pear_table = { --number, after_dig, col_box
   {1, 'air', pp_col_box_1},
   {2, 'desert_life:prickly_pear_1', pp_col_box_2},
   {3, 'desert_life:prickly_pear_2', pp_col_box_3},
   {4, 'desert_life:prickly_pear_3', pp_col_box_4},
   {5, 'desert_life:prickly_pear_4', pp_col_box_5},
   {6, 'desert_life:prickly_pear_5', pp_col_box_6},
   {7, 'desert_life:prickly_pear_6', pp_col_box_7},
}

for i in ipairs (prickly_pear_table) do
   local num = prickly_pear_table[i][1]
   local AD = prickly_pear_table[i][2]
   local col = prickly_pear_table[i][3]

   minetest.register_node('desert_life:prickly_pear_'..num, {
      description = 'Prickly Pear',
      drawtype = 'mesh',
      mesh = 'dl_pp_'..num..'.obj',
      tiles = {'dl_prickly_pear.png'},
      use_texture_alpha = 'opaque',
      groups = {not_in_creative_inventory=1, dl_pp=1, flora=1},
      paramtype = 'light',
      paramtype2 = 'facedir',
      selection_box = col,
      collision_box = col,
      on_punch = function(pos, node, player, pointed_thing)
         minetest.set_node(pos, {name = AD, param2 = node.param2})
         player:get_inventory():add_item('main', 'desert_life:prickly_pear') --If inventory is full it should be dropped.
--        local stack = ItemStack('desert_life:prickly_pear')
--        local leftover = minetest.add_item('main', stack)
--        if leftover:get_count() > 0 then
--           minetest.add_item (pos, 'desert_life:prickly_pear '..leftover:get_count())
--        end
         local damage_chance = math.random(1,15)
         if damage_chance == 1 then
            local hp = player:get_hp()
            player:set_hp(hp - 1)
         end
      end
      })
end


minetest.register_node('desert_life:prickly_pear', {
   description = 'Prickly Pear Pad',
   drawtype = 'mesh',
   mesh = 'dl_pp_1.obj',
   tiles = {'dl_prickly_pear.png'},
   use_texture_alpha = 'opaque',
   groups = {dig_immediate = 3, flora=1},
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = {
      type = 'fixed',
      fixed = {{-.2, -.5, -.2, .2, .0, .2}}
   },
   collision_box = {
      type = 'fixed',
      fixed = {{-.2, -.5, -.2, .2, .0, .2}} -- left bottom front right top back
   },
   drop = 'desert_life:prickly_pear',
   after_place_node = function(pos, placer, itemstack)
      local under = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z})
      local node = minetest.get_node(pos)
      if under.name == 'default:sand' or under.name == 'default:desert_sand' then
         minetest.set_node(pos, {name = 'desert_life:prickly_pear_1', param2 = node.param2})
      end
   end,
})

minetest.register_decoration({
      deco_type = "simple",
      place_on = {"default:desert_sand"},
      sidelen = 16,
      noise_params = {
         offset = .005,
         scale = 0.002,
         spread = {x = 150, y = 150, z = 150},
         seed = 150,
         octaves = 3,
         persist = 0.6
      },
      y_min = -10,
      y_max = 60,
      decoration = "desert_life:prickly_pear_1",
      param2 = 0,
      param2_max = 3,
      biomes = {'desert'},
})

minetest.register_abm{
   nodenames = {"group:dl_pp"},
   label = 'Prickly pear growth/spread',
   interval = 40,
   chance = 30,
   action = function(pos)
      local node = minetest.get_node(pos)
      if node.name == 'desert_life:prickly_pear_1' then
         minetest.set_node(pos, {name = "desert_life:prickly_pear_2", param2 = node.param2})
      elseif node.name == 'desert_life:prickly_pear_2' then
         minetest.set_node(pos, {name = "desert_life:prickly_pear_3", param2 = node.param2})
      elseif node.name == 'desert_life:prickly_pear_3' then
         minetest.set_node(pos, {name = "desert_life:prickly_pear_4", param2 = node.param2})
      elseif node.name == 'desert_life:prickly_pear_4' then
         minetest.set_node(pos, {name = "desert_life:prickly_pear_5", param2 = node.param2})
      elseif node.name == 'desert_life:prickly_pear_5' then
         minetest.set_node(pos, {name = "desert_life:prickly_pear_6", param2 = node.param2})
      elseif node.name == 'desert_life:prickly_pear_6' then
         minetest.set_node(pos, {name = "desert_life:prickly_pear_7", param2 = node.param2})
      elseif node.name == 'desert_life:prickly_pear_7' then
         desert_life.spread('desert_life:prickly_pear_1', pos, 1, 'default:desert_sand', 'air', 22)
      end
   end,
}
