minetest.register_node('tasks:campfire_0', {
   description = 'Campfire Smoldering',
   drawtype = 'mesh',
   mesh = 'tasks_campfire.obj',
   tiles = {{name='tasks_campfire_material.png'}, {name='tasks_campfire_base.png'}, {name='blank.png'}},
   use_texture_alpha = 'clip',
   paramtype2 = 'facedir',
   light_source = 3,
   groups = {breakable=1, tasks=1, not_in_creative_inventory=1},
   drop = 'tasks:campfire_1',
   selection_box  = {
      type = 'fixed',
      fixed = {{-.4, -.5, -.4, .4, 0, .4}}
   },
   collision_box  = {
      type = 'fixed',
      fixed = {{-.4, -.5, -.4, .4, 0, .4}}
   },
   on_rightclick = function(pos, node, clicker, itemstack)
      local name = clicker:get_player_name()
      local item = itemstack:get_name()
      if item == 'tasks:log' then
         minetest.swap_node(pos, {name = 'tasks:campfire_1', param2 = node.param2})
         local timer = minetest.get_node_timer(pos)
         local meta = minetest.get_meta(pos)
         local xp = meta:get_int('xp') or 1
         local min = meta:get_int('time_min') or 240
         local max = meta:get_int('time_max') or 600
         local random_number = math.random(min,max)
         timer:start(random_number)
         tasks.only_add_xp(xp, name)
         return ''
      else
         minetest.chat_send_player(name, 'Try with another item.')
      end
   end,
   on_punch = function(pos, node, puncher)
      local name = puncher:get_player_name()
      local wield = puncher:get_wielded_item()
      local wield_name = wield:get_name()
      if wield_name == 'tasks:log' then
         minetest.swap_node(pos, {name = 'tasks:campfire_1', param2 = node.param2})
         local timer = minetest.get_node_timer(pos)
         local meta = minetest.get_meta(pos)
         local xp = meta:get_int('xp') or 1
         local min = meta:get_int('time_min') or 240
         local max = meta:get_int('time_max') or 600
         local random_number = math.random(min,max)
         timer:start(random_number)
         tasks.only_add_xp(xp, name)
         puncher:set_wielded_item('')
      else
         minetest.chat_send_player(name, 'Try with another item.')
      end
   end,
})

minetest.register_node('tasks:campfire_1', {
   description = 'Campfire',
   drawtype = 'mesh',
   mesh = 'tasks_campfire.obj',
   tiles = {{name='tasks_campfire_material.png'}, {name='tasks_campfire_base.png'}, {name='tasks_campfire_flames.png', animation = {type = 'vertical_frames', aspect_w = 32, aspect_h = 32, length = 1.5}}},
   use_texture_alpha = 'clip',
   paramtype2 = 'facedir',
   light_source = 14,
   groups = {breakable=1, tasks=1},
   selection_box  = {
      type = 'fixed',
      fixed = {{-.4, -.5, -.4, .4, .2, .4}}
   },
   collision_box  = {
      type = 'fixed',
      fixed = {{-.4, -.5, -.4, .4, .2, .4}}
   },
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      meta:set_int('time_min', 240)
      meta:set_int('time_max', 600)
      meta:set_int('xp', 1)
   end,
   on_rightclick = tasks.right_click,
   on_timer = function(pos)
      local node = minetest.get_node(pos)
      minetest.swap_node(pos, {name = 'tasks:campfire_0', param2 = node.param2})
   end,
})

-- Return true if `index` is the first free slot in the player's main inventory
local function is_first_free_slot(player, index)
   local player_inv = player:get_inventory()
   local first_free = index+1
   for i = 1,index,1 do
      local curstk = player_inv:get_stack('main', i)
      --minetest.debug('main(' .. tostring(i) .. ') = ' .. curstk:get_name())
      if curstk:is_empty() and i < first_free then
         first_free = i
         break
      end
   end
   return (first_free == index)
end

minetest.register_node('tasks:wood_pile', {
   description = 'Wood Pile',
   drawtype = 'mesh',
   mesh = 'tasks_wood_pile.obj',
   tiles = {'tasks_wood_pile.png'},
   paramtype2 = 'facedir',
   groups = {breakable=1},
   selection_box  = {
      type = 'fixed',
      fixed = {{-.5, -.5, -.5, 1.5, .5, .5}}
   },
   collision_box  = {
      type = 'fixed',
      fixed = {{-.5, -.5, -.5, 1.5, .5, .5}}
   },
   on_punch = function(pose, node, puncher)
      local player_inv = puncher:get_inventory()
      if not player_inv:contains_item('main', {name='tasks:log', count = 4}) then
         player_inv:add_item('main', 'tasks:log')
      else
         minetest.chat_send_player(puncher:get_player_name(), 'You have too many of those already.')
      end
   end,
   on_rightclick = function(pose, node, clicker, wielded)
      local player_inv = clicker:get_inventory()
      if not player_inv:contains_item('main', {name='tasks:log', count = 4}) then
         local wield_log = is_first_free_slot(clicker, clicker:get_wield_index())
         player_inv:add_item('main', 'tasks:log')
         if wielded:is_empty() and wield_log then
            -- In addition to adding the log to the inventory, we have to give
            -- the player a log in their hand, if the selected hotbar item was
            -- the first empty slot.  Otherwise, the bare hand seemingly overrides
            -- the log we just added to the hotbar.
            return ItemStack('tasks:log')
         end
      else
         minetest.chat_send_player(clicker:get_player_name(), 'You have too many of those already.')
      end
   end,
})
