--Pipe Bomb
--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')
local playerWhoThrewObject = minetest.get_player_by_name("singleplayer")

minetest.register_craftitem('more_fire:pipebomb', {
   description = 'Pipe Bomb',
   inventory_image = 'more_fire_pipebomb.png',
   on_place = function(itemstack, user, pointed_thing)
      itemstack:take_item()
      minetest.sound_play('more_fire_shatter', {gain = 1.0})
      
      --smoke particles
      minetest.add_particlespawner({
         amount = 400,
         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},
         minexptime = 5,
         maxexptime = 2,
         minsize = 5,
         maxsize = 20,
         collisiondetection = true,
         texture = 'more_fire_smoke.png'})
     --more smoke particles
      minetest.add_particlespawner({
         amount = 600,
         time = 1,
         minpos = pointed_thing.above,
         maxpos = pointed_thing.above,
         minvel = {x=10, y= 3, z=10},
         maxvel = {x=-10, y= 3, z=-10},
         minacc = {x=2, y=2, z=2},
         maxacc = {x=-2, y=1, z=-2},
         minexptime = 2,
         maxexptime = 3,
         minsize = 2,
         maxsize = 20,
         collisiondetection = true,
         texture = 'more_fire_smoke.png'})
      --even more smoke particles
      minetest.add_particlespawner({
         amount = 400,
         time = 1,
         minpos = pointed_thing.above,
         maxpos = pointed_thing.above,
         minvel = {x=0.2, y=0.2, z=0.2},
         maxvel = {x=-0.2, y=0.5, z=-0.2},
         minacc = {x=10, y= 2, z=10},
         maxacc = {x=-10, y= 1, z=-10},
         minexptime = 2,
         maxexptime = 3,
         minsize = 20,
         maxsize = 2,
         collisiondetection = true,
         texture = 'more_fire_smoke.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_smokebomb.png')
         return itemstack
      end,
   })

local function throw_pipebomb(item, player)
   local playerpos = player:getpos()
   local obj = minetest.add_entity({x = playerpos.x, y = playerpos.y + 1.625, z = playerpos.z}, 'more_fire:pipebomb_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})
   item:take_item()
   playerWhoThrewObject = player
   return item
end

local function add_effects(pos)
   minetest.add_particlespawner({
      amount = 200,
      time = 0.1,
      minpos = vector.subtract(pos, 5 / 3),
      maxpos = vector.add(pos, 5 / 3),
      minvel = {x=2, y=0.2, z=2},
      maxvel = {x=-2, y=-0.5, z=-2},
      minacc = {x=1, y=-6, z=1},
      maxacc = {x=1, y=-10, z=1},
      minexptime = 1,
      maxexptime = 5,
      minsize = 10,
      maxsize = 20,
      texture = 'more_fire_smoke.png',})
   minetest.add_particlespawner({
      amount = 100,
      time = 2,
      minpos = vector.subtract(pos, 5 / 2),
      maxpos = vector.add(pos, 5 / 2),
      minvel = {x=0.2, y=0.2, z=0.2},
      maxvel = {x=-0.2, y=0.5, z=-0.2},
      minacc = {x=10, y= 2, z=10},
      maxacc = {x=-10, y= 1, z=-10},
      minexptime =1,
      maxexptime = 3,
      minsize = 5,
      maxsize = 15,
      texture = 'more_fire_smoke.png',})
end

local function plume(pos)
   minetest.set_node(pos, {name='more_fire:plume'})
   minetest.get_node_timer(pos):start(3.0)
   add_effects(pos)
end

local MORE_FIRE_PIPEBOMB_ENTITY = {
   timer=0,
   collisionbox = {0,0,0,0,0,0},
   physical = false,
   textures = {'more_fire_pipebomb.png'},
   lastpos={},
}


MORE_FIRE_PIPEBOMB_ENTITY.on_step = function(self, dtime)
   self.timer = self.timer + dtime
   local pos = self.object:getpos()
   local node = minetest.get_node(pos)
   if self.timer > 0.01 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:pipebomb_entity' and obj:get_luaentity().name ~= '__builtin:item' then
               if self.node ~= '' then
				explosives.boom(pos, {})
               local damage = 1
               obj:punch(self.object, 1.0, {
                  full_punch_interval=1.0,
                  damage_groups={fleshy=damage},
               }, nil)
               self.object:remove()
            end
         end
      end
   end
   if self.lastpos.x ~= nil then
      if node.name ~= 'air' then
         self.object:remove()
		 
         explosives.boom(pos, {
			owner = playerWhoThrewObject
		 })
		 
		minetest.add_particlespawner({
			amount = 64,
			time = 0.5,
			minpos = {x = pos.x - 2, y = pos.y - 2, z = pos.z - 2},
			maxpos = {x = pos.x + 2, y = pos.y + 2, z = pos.z + 2},
			minvel = {x = -10, y = -10, z = -10},
			maxvel = {x = 10, y = 10, z = 10},
			minacc = vector.new(),
			maxacc = vector.new(),
			minexptime = 1,
			maxexptime = 2.5,
			minsize = 2,
			maxsize = 5,
			texture = "tnt_smoke.png",
		})
		 
      end
   end
   self.lastpos={x=pos.x, y=pos.y, z=pos.z}
   end
end

minetest.register_entity('more_fire:pipebomb_entity', MORE_FIRE_PIPEBOMB_ENTITY)

minetest.override_item('more_fire:pipebomb', {on_use = throw_pipebomb})




minetest.register_craft({
   output = 'more_fire:pipe_bomb',
   recipe = {
      {'more_fire:flintstone'},
      {'more_fire:charcoal'},
      {'vessels:glass_bottle'},
   }
})
