local formspec_good =
   'size[6,3]'..
   'textarea[1,1;5,2;;;Working Fine.]'

local formspec_bad =
   'formspec_version[3]'..
   'size[10.5,7]'..
   'textarea[.5,.5;5,1;;;Shields down!!!\nNew Quantum Coil Needed ASAP.]' ..
   'button_exit[3.5,1.5;3,1;gimme;Grab a part request form]'..
   'list[current_name;part;8,1.5;1,1;]'..
   'list[current_player;main;.5,3;8,3;]'..
   'listring[current_player;main]'..
   'listring[current_name;part]'..
   'image[8,1.5;1,1;tasks_quantum_coil_line.png]'

   local on_box = {
      type = 'fixed',
         fixed = {
            {-.5, -.5, -.5, .5, .5, .5},
            {-1.5, -.5, 1.45, 1.5, 1.5, 1.5}},}

local off_box = {
   type = 'fixed',
      fixed = {
         {-.5, -.5, -.5, .5, .5, .5},},}

minetest.register_node('tasks:forcefield_gen_on',{
   description = 'Forcefield Generator',
   drawtype = 'mesh',
   mesh = 'tasks_forcefield_gen.obj',
   tiles = {name = 'tasks_forcefield_gen_on.png'},
   paramtype = 'light',
   paramtype2 = 'facedir',
   light_source = 12,
   selection_box = on_box,
   collision_box = on_box,
   groups = {breakable = 1, tasks=1},
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      meta:set_string('formspec', formspec_good)
      meta:set_string('infotext', 'Forcefield Generator')
      meta:set_int('time_min', 300)
      meta:set_int('time_max', 600)
      meta:set_int('xp', 10)
      tasks.on_construct(pos)
   end,
   on_rightclick = tasks.right_click,
   on_timer = function(pos)
      local node = minetest.get_node(pos)
      local meta = minetest.get_meta(pos)
      local infotext = meta:get_string('infotext')
      meta:set_string('formspec', formspec_bad)
      meta:set_string('infotext', infotext..' (Needs repair!)')
      minetest.swap_node(pos, {name = 'tasks:forcefield_gen_off', param2 = node.param2})
      --minetest.sound_play('tasks_smoke_alarm_chirp', {pos = pos, gain = 2, max_hear_distance = 14})
   end,
})

minetest.register_node('tasks:forcefield_gen_off',{
   description = 'Forcefield Generator',
   drawtype = 'mesh',
   mesh = 'tasks_forcefield_gen.obj',
   tiles = {name = 'tasks_forcefield_gen_off.png'},
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = off_box,
   collision_box = off_box,
   groups = {breakable = 1, not_in_creative_inventory=1, tasks=1},
   drop = 'tasks:forcefield_gen_on',
   on_receive_fields = function(pos, formname, fields, sender)
      if fields ['gimme'] then
         local player_inv = sender:get_inventory()
         if not player_inv:contains_item('main', {name='tasks:quantum_coil_req', count = 2}) then
            player_inv:add_item('main', 'tasks:quantum_coil_req')
         else
            minetest.chat_send_player(sender:get_player_name(), 'You have too many of these already.')
         end
      end
   end,
   allow_metadata_inventory_put = function(pos, listname, index, stack, player)
      if listname == 'part' then
         if stack:get_name() == 'tasks:quantum_coil' then
            local player_attributes = player:get_meta()
            local luck = player_attributes:get_int('luck')
            if luck*2 < math.random(10) then
               minetest.chat_send_player(player:get_player_name(), 'You drop the coil, fortunately it doesn\'t break.')
               return 0
            else
               return 1
            end
         end
         return 0
      end
   end,
   on_metadata_inventory_put = function(pos, listname, index, stack, player)
      local name = player:get_player_name()
      local meta = minetest.get_meta(pos)
      local xp = meta:get_int('xp') or 10
      local inv = meta:get_inventory()
      local node = minetest.get_node(pos)
      local timer = minetest.get_node_timer(pos)
      local min = meta:get_int('time_min') or 300
      local max = meta:get_int('time_max') or 600
      local random_number = math.random(min,max)
      local infotext = string.sub(meta:get_string('infotext'), 1, -17)
      inv:set_stack('part', 1, '')
      minetest.swap_node(pos, {name = 'tasks:forcefield_gen_on', param2 = node.param2})
      meta:set_string('formspec', formspec_good)
      meta:set_string('infotext', infotext)
      tasks.only_add_xp(xp, name)
      timer:start(random_number)
   end,
   })
