--------------------[ GRENADE FUNCTIONS ]---------------------------------------------

function CreateGrenade()
	local Grenade = Instance.new("Model")
	
	local Center = Instance.new("Part")
	Center.BrickColor = S.GrenadeColor
	Center.Name = "Center"
	Center.CanCollide = false
	Center.Elasticity = 0
	Center.FormFactor = Enum.FormFactor.Custom
	Center.Size = S.GrenadeSize
	Center.BottomSurface = Enum.SurfaceType.Smooth
	Center.TopSurface = Enum.SurfaceType.Smooth
	Center.Parent = Grenade
	
	local Mesh1 = Instance.new("SpecialMesh")
	Mesh1.MeshType = Enum.MeshType.Sphere
	Mesh1.Parent = Center
	
	return Grenade
end

function CreateKnife()
	local Knife = Instance.new("Part")
	Knife.BrickColor = S.GrenadeColor
	Knife.Name = "Knife"
	Knife.CanCollide = false
	Knife.FormFactor = Enum.FormFactor.Custom
	Knife.Size = VEC3(1, 1, 3)
	
	local Mesh = Instance.new("SpecialMesh")
	Mesh.MeshId = "http://www.roblox.com/asset/?id=12221720"
	Mesh.MeshType = Enum.MeshType.FileMesh
	Mesh.Scale = VEC3(0.5, 0.5, 0.5)
	Mesh.Parent = Knife
	
	return Knife
end

function CreateTarget()
	local Target = Instance.new("Model")
	
	local Center = Instance.new("Part")
	Center.BrickColor = BrickColor.new("Bright red")
	Center.Material = Enum.Material.SmoothPlastic
	Center.Transparency = 0.3
	Center.Name = "Center"
	Center.Anchored = true
	Center.CanCollide = false
	Center.FormFactor = Enum.FormFactor.Custom
	Center.Size = VEC3(4, 0.2, 4)
	Center.Parent = Target
	local CylinderMesh = Instance.new("CylinderMesh")
	CylinderMesh.Parent = Center
	
	local Line = Instance.new("Part")
	Line.BrickColor = BrickColor.new("Bright red")
	Line.Transparency = 0.3
	Line.Name = "Line"
	Line.CFrame = Center.CFrame * CFrame.new(0, 5.1, 0)
	Line.Anchored = true
	Line.CanCollide = false
	Line.FormFactor = Enum.FormFactor.Custom
	Line.Size = VEC3(0.4, 10, 0.4)
	Line.BottomSurface = Enum.SurfaceType.Smooth
	Line.TopSurface = Enum.SurfaceType.Smooth
	Line.Parent = Target
	
	return Target
end

function DetonateExplosive(Grenade)
	CreateShockwave(Grenade.Position, S.GrenadeBlastRadius)
	local GrenadePos = Grenade.Position
	local E = Instance.new("Explosion")
	E.BlastPressure = S.GrenadeBlastPressure
	E.BlastRadius = S.GrenadeBlastRadius
	E.DestroyJointRadiusPercent = (S.GrenadeRangeBasedDamage and 0 or 1)
	E.ExplosionType = S.GrenadeExplosionType
	E.Position = GrenadePos
	E.Hit:connect(function(HObj, HDist)
		if HObj.Name == "Torso" and (not HObj:IsDescendantOf(Character)) then
			if S.GrenadeRangeBasedDamage then
				local ClosestPart = nil
				local ClosestDist = math.huge
				
				for _, P in pairs(HObj.Parent:GetChildren()) do
					if P:IsA("BasePart") then
						local Dist = (GrenadePos - P.Position).magnitude
						if Dist < ClosestDist then
							ClosestPart = P
							ClosestDist = Dist
						end
					end
				end
				
				local Dir = (ClosestPart.Position - GrenadePos).unit
				local H, P = AdvRayCast(GrenadePos, Dir, 999)
				local RayHitHuman = H:IsDescendantOf(HObj.Parent)
				if (S.GrenadeRayCastExplosions and RayHitHuman) or (not S.GrenadeRayCastExplosions) then
					local HitHumanoid = FindFirstClass(HObj.Parent, "Humanoid")
					if HitHumanoid and HitHumanoid.Health > 0 and IsEnemy(HitHumanoid) then
						local DistFactor = ClosestDist / S.GrenadeBlastRadius
						local DistInvert = math.max(1 - DistFactor,0)
						local NewDamage = DistInvert * S.LethalGrenadeDamage
						
						local CreatorTag = Instance.new("ObjectValue")
						CreatorTag.Value = Player
						CreatorTag.Name = "creator"
						CreatorTag.Parent = HitHumanoid
						HitHumanoid:TakeDamage(NewDamage)
						MarkHit()
					end
				end
			else
				local HitHumanoid = FindFirstClass(HObj.Parent, "Humanoid")
				if HitHumanoid and HitHumanoid.Health > 0 and IsEnemy(HitHumanoid) then
					local CreatorTag = Instance.new("ObjectValue")
					CreatorTag.Value = Player
					CreatorTag.Name = "creator"
					CreatorTag.Parent = HitHumanoid
					MarkHit()
				end
			end
		end
	end)
	E.Parent = game.Workspace
	
	wait()
	
	Grenade.Parent:Destroy()
end

function DetonateSmoke(Grenade)
	CreateShockwave(Grenade.Position, S.GrenadeEffectRadius)
	local GrenadePos = Grenade.Position
	
	spawn(function()
		for i = 1, math.floor(S.GrenadeEffectRadius / 5) + RAND(5, 10) do
			local Size = RAND(S.GrenadeEffectRadius * 0.6, S.GrenadeEffectRadius * 0.8)
			local Dist = RAND(0, S.GrenadeEffectRadius - Size)
			local XRot, YRot = RAD(RAND(0, 180, 10)), RAD(RAND(0, 360, 10))
			local RotLV = (CFANG(0, YRot, 0) * CFANG(XRot, 0, 0)).lookVector
			local Pos = GrenadePos + (RotLV * VEC3(Dist, Dist / 2, Dist))
			
			local Smoke = Instance.new("Part")
			Smoke.Transparency = 1
			Smoke.Name = "Smoke"
			Smoke.Anchored = true
			Smoke.CanCollide = false
			Smoke.FormFactor = Enum.FormFactor.Symmetric
			Smoke.Size = VEC3(1, 1, 1)
			Smoke.TopSurface = Enum.SurfaceType.Smooth
			Smoke.BottomSurface = Enum.SurfaceType.Smooth
			
			local Mesh = Instance.new("SpecialMesh")
			Mesh.MeshType = Enum.MeshType.Sphere
			Mesh.Scale = VEC3(Size, Size, Size)
			Mesh.Parent = Smoke
			
			Smoke.Parent = Gun_Ignore
			Smoke.CFrame = CF(Pos)
			
			spawn(function()
				local Trans = RAND(0.3, 0.5, 0.01)
				for X = 0, 90, 2 do
					Smoke.CFrame = CF(GrenadePos:lerp(Pos, Sine(X)))
					Smoke.Transparency = NumLerp(1, Trans, Sine(X))
					RS:wait()
				end
				
				wait(S.GrenadeEffectTime)
				
				for X = 0, 90, 0.5 do
					Smoke.CFrame = CF(Pos:lerp(Pos + VEC3(0, 20, 0), 1 - COS(RAD(X))))
					Smoke.Transparency = NumLerp(Trans, 1, Sine(X))
					RS:wait()
				end
				
				Smoke:Destroy()
			end)
			
			if i % 3 == 0 then
				RS:wait()
			end
		end
	end)
	
	wait()
	
	Grenade.Parent:Destroy()
end

function ThrowGrenade(Type)
	local Grenade0 = nil
	if S.TrajectoryAssist then
		spawn(function()
			local X = 0
			local Vel = (Type == 1 and S.LethalGrenadeThrowVelocity or S.TacticalGrenadeThrowVelocity)
			
			local GetX = function(Ang, T)
				local Vx = Vel * math.cos(Ang)
				return Vx * T
			end
			
			local GetY = function(Ang, T)
				local V0y = Vel * math.sin(Ang)
				local Vy = V0y + (-196.2 * T)
				return (Vy * T) - (-98.1 * T * T)
			end
			
			local Target = CreateTarget()
			Target.Parent = game.Workspace
			Target.PrimaryPart = Target:WaitForChild("Center")
			
			while (Keys[S.LethalGrenadeKey] or Keys[S.TacticalGrenadeKey]) and Selected do
				X = X + math.rad(10)
				for _,v in pairs(Target:GetChildren()) do
					v.Transparency = 0.2 + ((math.sin(X) + 1) / 5)
				end
				
				local Lines = {}
				local LastX, LastY = nil, nil
				for T = 0, 10, 0.1 do
					local XPos = GetX(math.rad(7) - HeadRot, T)
					local YPos = GetY(math.rad(7) - HeadRot, T)
					if LastX and LastY then
						local LookV3 = HRP.CFrame.lookVector
						local LastPos = (Head.CFrame * CF(1.5, 0, 0)).p + (LookV3 * LastX) + VEC3(0, LastY, 0)
						local NewPos = (Head.CFrame * CF(1.5, 0, 0)).p + (LookV3 * XPos) + VEC3(0, YPos, 0)
						local LineCF = CF(LastPos, NewPos)
						local Dist = (LastPos - NewPos).magnitude
						local NewDist = Dist
						
						local H, P = AdvRayCast(LastPos, (NewPos - LastPos), 1, {Camera, unpack(Ignore)})
						if H then
							NewDist = (P - LastPos).magnitude
							
							local SurfaceCF = GetHitSurfaceCFrame(P, H)
							local SurfaceDir = CF(H.CFrame.p, SurfaceCF.p)
							local SurfaceDist = SurfaceDir.lookVector * (H.CFrame.p - SurfaceCF.p).magnitude / 2
							local SurfaceOffset = P - SurfaceCF.p + SurfaceDist
							local SurfaceCFrame = SurfaceDir + SurfaceDist + SurfaceOffset
							
							Target:SetPrimaryPartCFrame(SurfaceCFrame * CFANG(RAD(-90), 0, 0))
							
							Target.Parent = Camera
						else
							Target.Parent = nil
						end
						
						local Line = Instance.new("Part")
						Line.BrickColor = BrickColor.Red()
						Line.Material = Enum.Material.SmoothPlastic
						Line.Transparency = 0.2 + ((math.sin(X) + 1) / 5)
						Line.Anchored = true
						Line.CanCollide = false
						Line.FormFactor = Enum.FormFactor.Custom
						Line.Size = Vector3.new(0.4, 0.4, NewDist)
						Line.TopSurface = Enum.SurfaceType.Smooth
						Line.BottomSurface = Enum.SurfaceType.Smooth
						Line.CFrame = LineCF + (LineCF.lookVector * NewDist / 2)
						Line.Parent = Camera
						
						table.insert(Lines, Line)
						
						LastX,LastY = XPos,YPos
						
						if H then break end
					else
						LastX,LastY = XPos,YPos
					end
				end
				
				wait()
				
				for _,Line in pairs(Lines) do
					Line:Destroy()
				end
			end
			
			Target:Destroy()
		end)
	end
	
	local AnimTable = {
		function()
			TweenJoint(LWeld, CF(-1.5, 0, 0), CF(0, 0.6, 0), Linear, 0.2)
			TweenJoint(RWeld, CF(1.5, 0, 0) * CFANG(0, 0, RAD(-10)), CF(0, 0.6, 0), Linear, 0.2)
			TweenJoint(Grip, Grip.C0, CFANG(0, RAD(10), 0), Linear, 0.2)
			wait(0.3)
		end;
	
		function()
			Grip.Part0 = Torso
			Grip.C1 = CF(-1, 0.5, -0.5)
			
			if S.LethalGrenadeType == 3 and Type == 1 then
				Grenade0 = CreateKnife()
				Grenade0.Parent = Gun_Ignore
				
				local Weld = Instance.new("Weld")
				Weld.Part0 = FakeRArm
				Weld.Part1 = Grenade0
				Weld.C0 = Grip.C0
				Weld.C1 = CF(0, 0, -0.5) * CFANG(RAD(90), RAD(90), 0)
				Weld.Parent = Grenade0
				
				TweenJoint(LWeld2, CF(), CF(), Sine, 0.3)
				TweenJoint(RWeld2, CF(), CF(), Sine, 0.3)
				TweenJoint(LWeld, ArmC0[1], CF(0, 0.5, 0.1) * CFANG(RAD(90), 0, 0), Sine, 0.3)
				TweenJoint(RWeld, ArmC0[2], CF(0, 0.4, 0.1) * CFANG(RAD(-80), 0, 0), Sine, 0.3)
				wait(0.3)
			else
				Grenade0 = CreateGrenade()
				Grenade0.Parent = Gun_Ignore
				
				local Weld = Instance.new("Weld")
				Weld.Part0 = FakeRArm
				Weld.Part1 = Grenade0:WaitForChild("Center")
				Weld.C0 = Grip.C0
				Weld.Parent = Grenade0:WaitForChild("Center")
				
				TweenJoint(LWeld2, CF(), CFANG(0, RAD(80), 0), Linear, 0.25)
				TweenJoint(RWeld2, CF(), CFANG(0, RAD(-80), 0), Linear, 0.25)
				TweenJoint(LWeld, ArmC0[1], CF(-0.2, 0.8, 0.1) * CFANG(RAD(10), 0, RAD(-30)), Linear, 0.25)
				TweenJoint(RWeld, ArmC0[2], CF(0.2, 0.8, 0.1) * CFANG(RAD(10), 0, RAD(30)), Linear, 0.25)
				wait(0.3)
			end
		end;
	
		function()
			repeat wait() until (not Keys[S.LethalGrenadeKey]) and (not Keys[S.TacticalGrenadeKey]) or (not Selected)
		end;
	
		function()
			if S.LethalGrenadeType ~= 3 or Type == 2 then
				TweenJoint(LWeld2, CF(), CFANG(0, RAD(45), 0), Sine, 0.2)
				TweenJoint(RWeld2, CF(), CFANG(0, RAD(-45), 0), Sine, 0.2)
				TweenJoint(LWeld, ArmC0[1], CF(0, 0.8, 0.1), Sine, 0.2)
				TweenJoint(RWeld, ArmC0[2], CF(0, 0.8, 0.1), Sine, 0.2)
				wait(0.2)
			end
		end;
	
		function()
			if S.LethalGrenadeType ~= 3 or Type == 2 then
				TweenJoint(LWeld2, CF(), CF(), Sine, 0.3)
				TweenJoint(RWeld2, CF(), CF(), Sine, 0.3)
				TweenJoint(LWeld, ArmC0[1], CF(0, 0.5, 0.1) * CFANG(RAD(90), 0, 0), Sine, 0.3)
				TweenJoint(RWeld, ArmC0[2], CF(0, 0.4, 0.1) * CFANG(RAD(-80), 0, 0), Sine, 0.3)
				wait(0.3)
			end
		end;
	
		function()
			TweenJoint(RWeld, ArmC0[2], CF(0, 0.8, 0.1) * CFANG(RAD(-10), 0, 0), Sine, 0.1)
			wait(0.07)
		end;
	
		function()
			local Main = nil
			Grenade0:Destroy()
			
			if S.LethalGrenadeType == 3 and Type == 1 then
				local Grenade1 = CreateKnife()
				Main = Grenade1
				
				Grenade1.Parent = Gun_Ignore
				
				Main.CFrame = FakeRArm.CFrame * Grip.C0 * CF(0, 0.5, 0) * CFANG(RAD(-90), 0, RAD(90))
				Main.Velocity = Main.Velocity + ((Head.CFrame * CFANG(RAD(7), 0, 0)).lookVector * S.LethalGrenadeThrowVelocity)
				Main.RotVelocity = (Main.CFrame * CFANG(RAD(90), 0, 0)).lookVector * 20
			else
				local Grenade1 = CreateGrenade()
				Main = Grenade1:WaitForChild("Center")
				
				local Sound = Instance.new("Sound")
				Sound.SoundId = (Type == 1 and S.ExplosionSound or "rbxassetid://156283116")
				Sound.Volume = 1
				Sound.PlayOnRemove = true
				Sound.Parent = Main
				
				Grenade1.Parent = Gun_Ignore
				
				Main.CanCollide = true
				Main.CFrame = FakeRArm.CFrame * Grip.C0
				if Type == 1 then
					Main.Velocity = Main.Velocity + ((Head.CFrame * CFANG(RAD(7), 0, 0)).lookVector * S.LethalGrenadeThrowVelocity)
				elseif Type == 2 then
					Main.Velocity = Main.Velocity + ((Head.CFrame * CFANG(RAD(7), 0, 0)).lookVector * S.TacticalGrenadeThrowVelocity)
				end
			end
			
			spawn(function()
				if Type == 1 then
					if S.LethalGrenadeType == 1 then
						if S.TimerStartOnHit then
							local Detonated = false
							Main.Touched:connect(function(Obj)
								if IsIgnored(Obj) or Detonated then return end
								Main.Velocity = Main.Velocity / 4
								Detonated = true
								
								wait(S.DetonationTime)
								
								DetonateExplosive(Main)
							end)
						else
							spawn(function()
								local Touched = false
								Main.Touched:connect(function(Obj)
									if IsIgnored(Obj) or Touched then return end
									Touched = true
									Main.Velocity = Main.Velocity / 4
								end)
							end)
							wait(S.DetonationTime)
							DetonateExplosive(Main)
						end
					elseif S.LethalGrenadeType == 2 then
						local Detonated = false
						local GrenadeCF = nil
						Main.Touched:connect(function(Obj)
							if IsIgnored(Obj) or Detonated then return end
							GrenadeCF = Main.CFrame
							
							local W = Instance.new("Weld")
							W.Name = "Semtex"
							W.Part0 = Main
							W.Part1 = Obj
							W.C0 = GrenadeCF:toObjectSpace(Obj.CFrame)
							W.Parent = Main
							
							Main.ChildRemoved:connect(function(C)
								if C.Name == "Semtex" then
									local W = Instance.new("Weld")
									W.Name = "Semtex"
									W.Part0 = Main
									W.Part1 = Obj
									W.C0 = GrenadeCF:toObjectSpace(Obj.CFrame)
									W.Parent = Main
								end
							end)
							
							if S.TimerStartOnHit then
								Detonated = true
								wait(S.DetonationTime)
								DetonateExplosive(Main)
							end
						end)
						
						if (not S.TimerStartOnHit) then
							wait(S.DetonationTime)
							Detonated = true
							DetonateExplosive(Main)
						end
					elseif S.LethalGrenadeType == 3 then
						local Touched = false
						Main.Touched:connect(function(Obj)
							if IsIgnored(Obj) or Touched then return end
							Touched = true
							
							local W = Instance.new("Weld")
							W.Name = "Sticky"
							W.Part0 = Main
							W.Part1 = Obj
							W.C0 = Main.CFrame:toObjectSpace(Obj.CFrame)
							W.Parent = Main
							
							Main.ChildRemoved:connect(function(C)
								if C.Name == "Sticky" then
									local W = Instance.new("Weld")
									W.Name = "Sticky"
									W.Part0 = Main
									W.Part1 = Obj
									W.C0 = Main.CFrame:toObjectSpace(Obj.CFrame)
									W.Parent = Main
								end
							end)
							
							if Obj then
								if Obj.Parent.ClassName == "Hat" then
									local HitHumanoid = FindFirstClass(Obj.Parent.Parent, "Humanoid")
									if HitHumanoid and IsEnemy(HitHumanoid) then
										local CreatorTag = Instance.new("ObjectValue")
										CreatorTag.Name = "creator"
										CreatorTag.Value = Player
										CreatorTag.Parent = HitHumanoid
										HitHumanoid:TakeDamage(HitHumanoid.MaxHealth)
										MarkHit()
									end
								else
									local HitHumanoid = FindFirstClass(Obj.Parent, "Humanoid")
									if HitHumanoid and IsEnemy(HitHumanoid) then
										local CreatorTag = Instance.new("ObjectValue")
										CreatorTag.Name = "creator"
										CreatorTag.Value = Player
										CreatorTag.Parent = HitHumanoid
										HitHumanoid:TakeDamage(HitHumanoid.MaxHealth)
										MarkHit()
									end
								end
							end
							
							wait(3)
							
							Main:Destroy()
						end)
					end
				elseif Type == 2 then
					if S.TacticalGrenadeType == 1 then
						if S.TimerStartOnHit then
							local Detonated = false
							Main.Touched:connect(function(Obj)
								if IsIgnored(Obj) or Detonated then return end
								Main.Velocity = Main.Velocity / 2
								Detonated = true
								
								wait(S.DetonationTime)
								
								DetonateSmoke(Main)
							end)
						else
							spawn(function()
								local Touched = false
								Main.Touched:connect(function(Obj)
									if IsIgnored(Obj) or Touched then return end
									Touched = true
									Main.Velocity = Main.Velocity / 2
								end)
							end)
							wait(S.DetonationTime)
							DetonateSmoke(Main)
						end
					end
				end
			end)
			
			if S.GrenadeTrail and S.GrenadeTrailTransparency ~= 1 then
				spawn(function()
					local LastPos = nil
					while true do
						if LastPos then
							if (not Main:IsDescendantOf(game))
							or (Main.Name == "Knife" and FindFirstClass(Main, "Weld")) then
								break
							end
							local Trail = Instance.new("Part")
							Trail.BrickColor = S.GrenadeTrailColor
							Trail.Transparency = S.GrenadeTrailTransparency
							Trail.Anchored = true
							Trail.CanCollide = false
							Trail.Size = VEC3(1, 1, 1)
							local Mesh = Instance.new("BlockMesh")
							Mesh.Offset = VEC3(0, 0, -(Main.Position - LastPos).magnitude / 2)
							Mesh.Scale = VEC3(S.GrenadeTrailThickness, S.GrenadeTrailThickness, (Main.Position - LastPos).magnitude)
							Mesh.Parent = Trail
							Trail.Parent = Gun_Ignore
							Trail.CFrame = CF(LastPos, Main.Position)
							delay(S.GrenadeTrailVisibleTime, function()
								if S.GrenadeTrailDisappearTime > 0 then
									local X = 0
									while true do
										if X == 90 then break end
										if (not Selected) then break end
										local NewX = X + (1.5 / S.GrenadeTrailDisappearTime)
										X = (NewX > 90 and 90 or NewX)
										local Alpha = X / 90
										Trail.Transparency = NumLerp(S.GrenadeTrailTransparency, 1, Alpha)
										RS:wait()
									end
									Trail:Destroy()
								else
									Trail:Destroy()
								end
							end)
							LastPos = Main.Position
						else
							LastPos = Main.Position
						end
						RS:wait()
					end
				end)
			end
			wait(0.2)
		end;
	
		function()
			TweenJoint(RWeld, CF(1.5, 0, 0) * CFANG(0, 0, RAD(-10)), CF(0, 0.6, 0), Linear, 0.2)
			wait(0.3)
		end;
	
		function()
			Grip.Part0 = RArm
			Grip.C1 = CFANG(0, RAD(20), 0)
			
			TweenJoint(LWeld, ArmC0[1], S.ArmC1_UnAimed.Left, Linear, 0.2)
			TweenJoint(RWeld, ArmC0[2], S.ArmC1_UnAimed.Right, Linear, 0.2)
			wait(0.2)
		end;
	}
	
	for _,F in pairs(AnimTable) do
		if (not Selected) then
			break
		end
		F()
	end
	
	if (not Selected) and Grenade0 then
		Grenade0:Destroy()
	end
end