-------------------------------------------------------------------------------






function tagHumanoid(humanoid)
	local plr=game.Players:playerFromCharacter(sp.Parent)
	if plr~=nil then
		local tag=Instance.new("ObjectValue")
		tag.Value=plr
		tag.Name="creator"
		tag.Parent=humanoid
		delay(2,function()
			if tag~=nil then
				tag.Parent=nil
			end
		end)
	end
end


function reload(mouse)
	reloading=true
	mouse.Icon=ReloadCursor
	while sp.Ammo.Value<ClipSize and reloading and enabled do
		wait(ReloadTime/ClipSize)
		if reloading then
			sp.Ammo.Value=sp.Ammo.Value+1
			check()
		else
			break
		end
	end
	check()
	mouse.Icon=Cursors[1]
	reloading=false
end

function onKeyDown(key,mouse)
	key=key:lower()
	if key=="r" and not reloading then
		reload(mouse)
	end
end

function movecframe(p,pos)
	p.Parent=game.Lighting
	p.Position=pos
	p.Parent=game.Workspace
end


function fire(aim)
	sp.Handle.Fire:Play()

	t=r.Stepped:wait()
	last6=last5
	last5=last4
	last4=last3
	last3=last2
	last2=last
	last=t

	local bullet=Bullet:clone()
	local bt2=game.Lighting.BulletTexture:clone()
	bt2.BrickColor=BrickColor.new("Bright red")
	bt2.Mesh.Scale=Vector3.new(.5,.5,2)
	local totalDist=0
	Lengthdist=-RayLength/.5
	local startpoint=sp.Handle.CFrame*BarrlePos
	local dir=(aim)-startpoint
	dir=computeDirection(dir)
	local cfrm=CFrame.new(startpoint, dir+startpoint)
	local hit,pos,normal,time=raycast(game.Workspace, startpoint, cfrm*Vector3.new(0,0,Lengthdist)-startpoint, function(brick)
		if brick.Name=="Glass" then
			return true
		elseif brick.Name=="Bullet" or brick.Name=="BulletTexture" then
			return false
		elseif brick:IsDescendantOf(sp.Parent) then
			return false
		elseif brick.Name=="Handle" then
			if brick.Parent:IsDescendantOf(sp.Parent) then
				return false
			else
				return true
			end
		end
		return true
	end)
	bullet.Parent=game.Workspace
	bt2.Parent=game.Workspace
	if hit~=nil then
		local humanoid=hit.Parent:FindFirstChild("Humanoid")
		if humanoid~=nil then
			local damage=math.random(BaseDamage-(BaseDamage*.25),BaseDamage+(BaseDamage*.25))
			if hit.Name=="Head" then
				damage=damage*1.25
			elseif hit.Name=="Torso" then
			else
				damage=damage*.75
			end
			if humanoid.Health>0 then
				local eplr=game.Players:playerFromCharacter(humanoid.Parent)
				local plr=game.Players:playerFromCharacter(sp.Parent)
				if eplr~=nil and plr~=nil then
				--	if eplr.TeamColor~=plr.TeamColor or eplr.Neutral or plr.Neutral then
						tagHumanoid(humanoid)
						humanoid:TakeDamage(damage)
				--	end
				else
					tagHumanoid(humanoid)
					humanoid:TakeDamage(damage)
				end
			end
		end
		if (hit.Name == "Part10") or (hit.Name == "Part11") or (hit.Name == "Part21") or (hit.Name == "Part23") or (hit.Name == "Part24") or (hit.Name == "Part8") then
			rand = math.random(1,5)
			if rand == 1 then
				workspace.GlassSound:play()
				hit:breakJoints()
			end
		end
		if (hit.Parent:findFirstChild("Hit")) then
			hit.Parent.Health.Value = hit.Parent.Health.Value - BaseDamage/3
		end
		distance=(startpoint-pos).magnitude
		bullet.CFrame=cfrm*CFrame.new(0,0,-distance/2)
		bullet.Mesh.Scale=Vector3.new(.15,.15,distance)
	else
		bullet.CFrame=cfrm*CFrame.new(0,0,-RayLength/2) 
		bullet.Mesh.Scale=Vector3.new(.15,.15,RayLength)
	end
	if pos~=nil then
		bt2.CFrame=bullet.CFrame
		movecframe(bt2,pos)
	end
	local deb=game:FindFirstChild("Debris")
	if deb==nil then
		local debris=Instance.new("Debris")
		debris.Parent=game
	end
	check()
	game.Debris:AddItem(bullet,.05)
	game.Debris:AddItem(bt2,.5)
end

function onButton1Up(mouse)
	down=false
end

function onButton1Down(mouse)
	h=sp.Parent:FindFirstChild("Humanoid")
	if not enabled or reloading or down or h==nil then
		return
	end
	if sp.Ammo.Value>0 and h.Health>0 then
		--[[if sp.Ammo.Value<=0 then
			if not reloading then
				reload(mouse)
			end
			return
		end]]
		down=true
		enabled=false
		while down do
			if sp.Ammo.Value<=0 then
				break
			end
			if burst then
				local startpoint=sp.Handle.CFrame*BarrlePos
				local mag=(mouse.Hit.p-startpoint).magnitude
				local rndm=Vector3.new(math.random(-(Spread/10)*mag,(Spread/10)*mag),math.random(-(Spread/10)*mag,(Spread/10)*mag),math.random(-(Spread/10)*mag,(Spread/10)*mag))
				fire(mouse.Hit.p+rndm)
				sp.Ammo.Value=sp.Ammo.Value-1
				if sp.Ammo.Value<=0 then
					break
				end
				wait(.05)
				local startpoint=sp.Handle.CFrame*BarrlePos
				local mag2=((mouse.Hit.p+rndm)-startpoint).magnitude
				local rndm2=Vector3.new(math.random(-(.1/10)*mag2,(.1/10)*mag2),math.random(-(.1/10)*mag2,(.1/10)*mag2),math.random(-(.1/10)*mag2,(.1/10)*mag2))
				fire(mouse.Hit.p+rndm+rndm2)
				sp.Ammo.Value=sp.Ammo.Value-1
				if sp.Ammo.Value<=0 then
					break
				end
				wait(.05)
				fire(mouse.Hit.p+rndm+rndm2+rndm2)
				sp.Ammo.Value=sp.Ammo.Value-1
			elseif shot then
				sp.Ammo.Value=sp.Ammo.Value-1
				local startpoint=sp.Handle.CFrame*BarrlePos
				local mag=(mouse.Hit.p-startpoint).magnitude
				local rndm=Vector3.new(math.random(-(Spread/10)*mag,(Spread/10)*mag),math.random(-(Spread/10)*mag,(Spread/10)*mag),math.random(-(Spread/10)*mag,(Spread/10)*mag))
				fire(mouse.Hit.p+rndm)
				local mag2=((mouse.Hit.p+rndm)-startpoint).magnitude
				local rndm2=Vector3.new(math.random(-(.2/10)*mag2,(.2/10)*mag2),math.random(-(.2/10)*mag2,(.2/10)*mag2),math.random(-(.2/10)*mag2,(.2/10)*mag2))
				fire(mouse.Hit.p+rndm+rndm2)
				local rndm3=Vector3.new(math.random(-(.2/10)*mag2,(.2/10)*mag2),math.random(-(.2/10)*mag2,(.2/10)*mag2),math.random(-(.2/10)*mag2,(.2/10)*mag2))
				fire(mouse.Hit.p+rndm+rndm3)
				local rndm4=Vector3.new(math.random(-(.2/10)*mag2,(.2/10)*mag2),math.random(-(.2/10)*mag2,(.2/10)*mag2),math.random(-(.2/10)*mag2,(.2/10)*mag2))
				fire(mouse.Hit.p+rndm+rndm4)
			else
				sp.Ammo.Value=sp.Ammo.Value-1
				local startpoint=sp.Handle.CFrame*BarrlePos
				local mag=(mouse.Hit.p-startpoint).magnitude
				local rndm=Vector3.new(math.random(-(Spread/10)*mag,(Spread/10)*mag),math.random(-(Spread/10)*mag,(Spread/10)*mag),math.random(-(Spread/10)*mag,(Spread/10)*mag))
				fire(mouse.Hit.p+rndm)
			end
			wait(Firerate)
			if not automatic then
				break
			end
		end	
		enabled=true
	else
		sp.Handle.Trigger:Play()
	end
end

function onEquippedLocal(mouse)
	if mouse==nil then
		print("Mouse not found")
		return 
	end
	mouse.Icon=Cursors[1]
	mouse.KeyDown:connect(function(key) onKeyDown(key,mouse) end)
	mouse.Button1Down:connect(function() onButton1Down(mouse) end)
	mouse.Button1Up:connect(function() onButton1Up(mouse) end)
	check()
	equiped=true
	if #Cursors>1 then
		while equiped do
			t=r.Stepped:wait()
			local action=sp.Parent:FindFirstChild("Pose")
			if action~=nil then
				if sp.Parent.Pose.Value=="Standing" then
					Spread=MinSpread
				else
					Spread=MinSpread+((1/10)*(MaxSpread-MinSpread))
				end
			else
				Spread=MinSpread
			end
			if t-last<SpreadRate then
				Spread=Spread+.1*(MaxSpread-MinSpread)
			end
			if t-last2<SpreadRate then
				Spread=Spread+.1*(MaxSpread-MinSpread)
			end
			if t-last3<SpreadRate then
				Spread=Spread+.1*(MaxSpread-MinSpread)
			end
			if t-last4<SpreadRate then
				Spread=Spread+.1*(MaxSpread-MinSpread)
			end
			if t-last5<SpreadRate then
				Spread=Spread+.1*(MaxSpread-MinSpread)
			end
			if t-last6<SpreadRate then
				Spread=Spread+.1*(MaxSpread-MinSpread)
			end
			if not reloading then
				local percent=(Spread-MinSpread)/(MaxSpread-MinSpread)
				for i=0,#Cursors-1 do
					if percent>(i/(#Cursors-1))-((1/(#Cursors-1))/2) and percent<(i/(#Cursors-1))+((1/(#Cursors-1))/2) then
						mouse.Icon=Cursors[i+1]
					end
				end
			end
			wait(Firerate*.9)
		end
	end
end
function onUnequippedLocal(mouse)
	equiped=false
	reloading=false
end
sp.Equipped:connect(onEquippedLocal)
sp.Unequipped:connect(onUnequippedLocal)
check()