   --Molotov Cocktail_[rev002]
   --base code is from throwing enhanced and potions mods

local MOD_NAME = minetest.get_current_modname()
local MOD_PATH = minetest.get_modpath(MOD_NAME)
local Vec3 = dofile(MOD_PATH..'/lib/Vec3_1-0.lua')

minetest.register_craftitem(':more_fire:molotov_cocktail', {
   description = 'Throwable Firebomb',
   inventory_image = 'more_fire_molotov_cocktail.png',
   on_place = function(itemstack, user, pointed_thing)
      itemstack:take_item()
      minetest.sound_play('more_fire_shatter', {gain = 1.0})
      n = minetest.get_node(pointed_thing)
      if pointed_thing.type == 'node' then
         minetest.add_node(pointed_thing.above, {name='fire:basic_flame'})
         minetest.sound_play('more_fire_ignite', {pos,pos})
      end
         --Shattered glass Particles
   minetest.add_particlespawner({
      amount = 40,
      time = 0.1,
      minpos = pointed_thing.above,
      maxpos = pointed_thing.above,
      minvel = {x=2, y=0.2, z=2},
      maxvel = {x=-2, y=0.5, z=-2},
      minacc = {x=0, y=-6, z=0},
      maxacc = {x=0, y=-10, z=0},
      minexptim = 0.5,
      maxexptime = 2,
      minsize = 0.2,
      maxsize = 5,
      collisiondetection = true,
      texture = 'more_fire_shatter.png'})
      --fire ember particles
      minetest.add_particlespawner({
      amount = 100,
      time = 0.1,
      minpos = pointed_thing.above,
      maxpos = pointed_thing.above,
      minvel = {x=-2, y=0.5, z=-2},
      maxvel = {x=2, y=0.5, z=2},
      minacc = {x=0, y=-10, z=0},
      maxacc = {x=0, y=-6, z=0},
      minexptime = 2,
      maxexptime = 3,
      minsize = 0.25,
      maxsize = 0.5,
      collisiondetection = true,
      texture = 'more_fire_spark.png'})
      local dir = Vec3(user:get_look_dir()) *20
      minetest.add_particle(
            {x=user:getpos().x, y=user:getpos().y+1.5, z=user:getpos().z}, {x=dir.x, y=dir.y, z=dir.z}, {x=0, y=-10, z=0}, 0.2,
               6, false, 'more_fire_molotov_cocktail.png')
         return itemstack
   end,
})

local function throw_cocktail(item, player)
   local playerpos = player:getpos()
   local obj = minetest.add_entity({x=playerpos.x,y=playerpos.y+1.625,z=playerpos.z}, 'more_fire:molotov_entity')
   local dir = player:get_look_dir()
   obj:setvelocity({x=dir.x*30, y=dir.y*30, z=dir.z*30})
   obj:setacceleration({x=dir.x*-3, y=-dir.y^8*80-10, z=dir.z*-3})
   if not minetest.settings:get_bool('creative_mode') then
      item:take_item()
   end
   return item
end

local radius = 5.0

local function add_effects(pos, radius)
   minetest.add_particlespawner({
      amount = 10,
      time = 0.2,
      minpos = vector.subtract(pos, radius / 2),
      maxpos = vector.add(pos, radius / 2),
      minvel = {x=-2, y=-2, z=-2},
      maxvel = {x=2,  y=-4,  z=2},
      minacc = {x=0, y=-4, z=0},
      --~ maxacc = {x=-20, y=-50, z=-50},
      minexptime = 1,
      maxexptime = 1.5,
      minsize = 1,
      maxsize = 2,
      texture = 'more_fire_spark.png',
   })
      minetest.add_particlespawner({
      amount = 10,
      time = 0.2,
      minpos = vector.subtract(pos, radius / 2),
      maxpos = vector.add(pos, radius / 2),
      minvel = {x=-1.25, y=-1.25, z=-1.25},
      maxvel = {x=0.5, y=-4, z=0.5},
      minacc = {x=1.25,  y=-1.25,  z=1.25},
      --~ maxacc = {x=-20, y=-50, z=-50},
      minexptime =1,
      maxexptime = 1.5,
      minsize = 1,
      maxsize = 2,
      texture = 'more_fire_spark.png',
   })
end



local MORE_FIRE_MOLOTOV_ENTITY = {
   timer=0,
   collisionbox = {0,0,0,0,0,0},
   physical = false,
   textures = {'more_fire_molotov_cocktail.png'},
   lastpos={},
}

MORE_FIRE_MOLOTOV_ENTITY.on_step = function(self, dtime)
   self.timer = self.timer + dtime
   local pos = self.object:getpos()
   local node = minetest.get_node(pos)
   minetest.add_particlespawner({
      amount = 10,
      time = 0.5,
      minpos = pos,
      maxpos = pos,
      minvel = {x=-0, y=0, z=-0.5},
      maxvel = {x=0,  y=0,  z=-0.75},
      minacc = vector.new(),
      maxacc = vector.new(),
      minexptime = 0.5,
      maxexptime = 1,
      minsize = 0.25,
      maxsize = 0.5,
      texture = 'more_fire_smoke.png',
   })
   minetest.add_particlespawner({
      amount = 100,
      time = 0.25,
      minpos = pos,
      maxpos = pos,
      minvel = {x=-0, y=0, z=-0.5},
      maxvel = {x=0,  y=0,  z=-0.75},
      minacc = {x=0,  y=0,  z=-0.75},
      maxacc = {x=-0, y=0, z=-0.5},
      minexptime = 0.25,
      maxexptime = 0.5,
      minsize = 0.5,
      maxsize = 0.75,
      texture = 'more_fire_spark.png',
   })
   if self.timer>0.2 then
      local objs = minetest.get_objects_inside_radius({x=pos.x,y=pos.y,z=pos.z}, 1)
      for k, obj in pairs(objs) do
         if obj:get_luaentity() ~= nil then
            if obj:get_luaentity().name ~= 'more_fire:molotov_entity' and obj:get_luaentity().name ~= '__builtin:item' then
               if self.node ~= '' then
                minetest.sound_play('more_fire_shatter', {gain = 1.0})
                  for dx=-3,3 do
                     for dy=-3,3 do
                        for dz=-3,3 do
                           local p = {x=pos.x+dx, y=pos.y+dy, z=pos.z+dz}
                           local n = minetest.get_node(pos).name
						   if n == "default:grass" then
                           --if minetest.registered_nodes[n].groups.flammable or math.random(1, 100) <= 20 then
                           minetest.sound_play('more_fire_ignite', {pos = self.lastpos})
                              minetest.set_node(p, {name='fire:basic_flame'})
                           
                           end
                        end
                     end
                  end
               end
               self.object:remove()
            end
         else
            if self.node ~= '' then
                  minetest.sound_play('more_fire_shatter', {gain = 1.0})
               for dx=-2,2 do
                  for dy=-2,2 do
                     for dz=-2,2 do
                        local p = {x=pos.x+dx, y=pos.y+dy, z=pos.z+dz}
                        local n = minetest.get_node(pos).name
                        if n == "default:grass" then
                           --if minetest.registered_nodes[n].groups.flammable or math.random(1, 100) <= 20 then
                           minetest.sound_play('more_fire_ignite', {pos = self.lastpos})
                              minetest.set_node(p, {name='fire:basic_flame'})
                           
                           end
                     end
                  end
               end
            end
            self.object:remove()
         end
      end
   end

   if self.lastpos.x~=nil then
      if node.name ~= 'air' then
         if self.node ~= '' then
         minetest.sound_play('more_fire_shatter', {gain = 1.0})
            for dx=-1,1 do
               for dy=-1,1 do
                  for dz=-1,1 do
                     local p = {x = pos.x + dx, y = pos.y + dy, z = pos.z + dz}
					 local p1 = {x = pos.x + dx, y=pos.y + dy - 1, z = pos.z + dz}
                     local n = minetest.get_node(p).name
					 local n1 = minetest.get_node(p1).name
                     if n == "air" and n1 ~= "fire:basic_flame" or math.random(1, 100) <= 5 then
                        minetest.sound_play('more_fire_ignite', {pos = self.lastpos})
                        minetest.set_node(p, {name='fire:basic_flame'})
                     end
                  end
               end
            end
         end
         self.object:remove()
      end
   end
   self.lastpos={x=pos.x, y=pos.y, z=pos.z}
end

minetest.register_entity(':more_fire:molotov_entity', MORE_FIRE_MOLOTOV_ENTITY)

minetest.override_item('more_fire:molotov_cocktail', {on_use = throw_cocktail})



minetest.register_abm({
   nodenames={'fire:basic_flame'},
   neighbors={'air'},
   interval = 1,
   chance = 2,
      action = function(pos, node)
      if minetest.get_node({x=pos.x, y=pos.y+1.0, z=pos.z}).name == 'air' and
         minetest.get_node({x=pos.x, y=pos.y+2.0, z=pos.z}).name == 'air' then
            minetest.add_particlespawner({
               amount = 1,
               time = 2,
               minpos = pos,
               maxpos = pos,
               minvel = {x=-2, y=2, z=-2},
               maxvel = {x=1, y=3, z=1},
               minacc = {x=0, y=6, z=0},
               maxacc = {x=0, y=2, z=0},
               minexptime = 1,
               maxexptime = 3,
               minsize = 10,
               maxsize = 20,
               collisiondetection = false,
               texture = 'more_fire_smoke.png'})
            minetest.add_particlespawner({
               amount = 15,
               time = 4,
               minpos = pos,
               maxpos = pos,
               minvel = {x=0, y= 3, z=0},
               maxvel = {x=0, y=5, z=0},
               minacc = {x=0.1, y=0.5, z=-0.1},
               maxacc = {x=-0.2, y=2, z=0.2},
               minexptime = 1,
               maxexptime = 3,
               minsize = 5,
               maxsize = 10,
               collisiondetection = false,
               texture ='more_fire_smoke.png'})
            end
         end
})

   --crafting recipes
minetest.register_craft( {
   output = 'more_fire:molotov_cocktail',
   recipe = {
      {'farming:cotton'},
      {'more_fire:oil'},
      {'vessels:glass_bottle'},
   }
})
