----------------------------------------------------------------
--\\DOORS & BREACHING SYSTEM
----------------------------------------------------------------

local DoorsFolder 		= ACS_Workspace:FindFirstChild("Doors")
local DoorsFolderClone 	= DoorsFolder:Clone()
local BreachClone 		= ACS_Workspace.Breach:Clone()
BreachClone.Parent 		= ServerStorage
DoorsFolderClone.Parent = ServerStorage

function ToggleDoor(Door)
	local Hinge = Door.Door:FindFirstChild("Hinge")
	if not Hinge then return end
	local HingeConstraint = Hinge.HingeConstraint

	if HingeConstraint.TargetAngle == 0 then
		HingeConstraint.TargetAngle = -90
	elseif HingeConstraint.TargetAngle == -90 then
		HingeConstraint.TargetAngle = 0
	end	
end

Evt.DoorEvent.OnServerEvent:Connect(function(Player,Door,Mode,Key)
	if Door ~= nil then
		if Mode == 1 then
			if Door:FindFirstChild("Locked") ~= nil and Door.Locked.Value == true then
				if Door:FindFirstChild("RequiresKey") then
					local Character = Player.Character
					if Character:FindFirstChild(Key) ~= nil or Player.Backpack:FindFirstChild(Key) ~= nil then
						if Door.Locked.Value == true then
							Door.Locked.Value = false
						end
						ToggleDoor(Door)
					end	
				end
			else
				ToggleDoor(Door)
			end
		elseif Mode == 2 then
			if Door:FindFirstChild("Locked") == nil or (Door:FindFirstChild("Locked") ~= nil and Door.Locked.Value == false) then
				ToggleDoor(Door)
			end
		elseif Mode == 3 then
			if Door:FindFirstChild("RequiresKey") then
				local Character = Player.Character
				Key = Door.RequiresKey.Value
				if Character:FindFirstChild(Key) ~= nil or Player.Backpack:FindFirstChild(Key) ~= nil then
					if Door:FindFirstChild("Locked") ~= nil and Door.Locked.Value == true then
						Door.Locked.Value = false
					else
						Door.Locked.Value = true
					end
				end
			end
		elseif Mode == 4 then
			if Door:FindFirstChild("Locked") ~= nil and Door.Locked.Value == true then
				Door.Locked.Value = false
			end
		end
	end
end)

Evt.MedSys.Collapse.OnServerEvent:Connect(function(Player)

	if not Player or not Player.Character then return; end;

	local Human 	= Player.Character:FindFirstChild("Humanoid") 
	local PClient 	= Player.Character:FindFirstChild("ACS_Client") 

	if not Human or not PClient then return; end;

	local Dor 		= PClient.Variaveis.Dor
	local Sangue 	= PClient.Variaveis.Sangue

	if (Sangue.Value <= 3500) or (Dor.Value >= 200) or PClient:GetAttribute("Collapsed") then -- Man this Guy's Really wounded,
		Human.PlatformStand = true
		Human.AutoRotate = false	
		PClient:SetAttribute("Collapsed",true)
	elseif (Sangue.Value > 3500) and (Dor.Value < 200) and not PClient:GetAttribute("Collapsed")  then -- YAY A MEDIC ARRIVED! =D
		Human.PlatformStand = false
		Human.AutoRotate = true	
		PClient:SetAttribute("Collapsed",false)
	end
end)

Evt.MedSys.MedHandler.OnServerEvent:Connect(function(Player, Victim, Mode)
	if not Player or not Player.Character then return; end;
	if not Player.Character:FindFirstChild("Humanoid") or Player.Character.Humanoid.Health <= 0 then return; end;

	local P1_Client = Player.Character:FindFirstChild("ACS_Client")
	if not P1_Client then warn(Player.Name.."'s Action Failed: Missing ACS_Client"); return; end;

	if P1_Client:GetAttribute("Collapsed") then return; end;

	if Victim then --Multiplayer functions
		if not Victim.Character or not Victim.Character:FindFirstChild("HumanoidRootPart") or not Player.Character:FindFirstChild("HumanoidRootPart") then return; end;
		if (Player.Character.HumanoidRootPart.Position - Victim.Character.HumanoidRootPart.Position).Magnitude > 15 then warn(Player.Name.." is too far to treat "..Victim.Name); return; end;


		local P2_Client = Victim.Character:FindFirstChild("ACS_Client")
		if not P2_Client then warn(Player.Name.."'s Action Failed: Missing "..Victim.Name.."'s ACS_Client"); return; end;

		if Mode == 1 and P1_Client.Kit.Bandage.Value > 0 and P2_Client:GetAttribute("Bleeding") then
			P1_Client.Kit.Bandage.Value = P1_Client.Kit.Bandage.Value - 1
			P2_Client:SetAttribute("Bleeding",false)
			return;

		elseif Mode == 2 and P1_Client.Kit.Splint.Value > 0 and P2_Client:GetAttribute("Injured") then
			P1_Client.Kit.Splint.Value = P1_Client.Kit.Splint.Value - 1
			P2_Client:SetAttribute("Injured",false)
			return;

		elseif Mode == 3 and (P2_Client:GetAttribute("Bleeding") == true or P2_Client:GetAttribute("Tourniquet")) then --Tourniquet works a little different :T
			if P2_Client:GetAttribute("Tourniquet")then
				P1_Client.Kit.Tourniquet.Value = P1_Client.Kit.Tourniquet.Value + 1
				P2_Client:SetAttribute("Tourniquet",false)
				return;
			end

			if P1_Client.Kit.Tourniquet.Value <= 0 then return; end;
			P1_Client.Kit.Tourniquet.Value = P1_Client.Kit.Tourniquet.Value - 1
			P2_Client:SetAttribute("Tourniquet",true)
			return;

		elseif Mode == 4 and P1_Client.Kit.PainRelief.Value > 0 and P2_Client.Variaveis.Dor.Value > 0 then
			P1_Client.Kit.PainRelief.Value = P1_Client.Kit.PainRelief.Value - 1
			P2_Client.Variaveis.Dor.Value = math.clamp(P2_Client.Variaveis.Dor.Value - math.random(35,65),0,300)
			Evt.MedSys.MedHandler:FireClient(Victim,4)
			return;

		elseif Mode == 5 and P1_Client.Kit.EnergyShot.Value > 0 and Victim.Character.Humanoid.Health < Victim.Character.Humanoid.MaxHealth then
			P1_Client.Kit.EnergyShot.Value = P1_Client.Kit.EnergyShot.Value - 1
			local HealValue = math.random(15,25)
			if Victim.Character.Humanoid.Health + HealValue < Victim.Character.Humanoid.MaxHealth then
				Victim.Character.Humanoid:TakeDamage(-HealValue)
			else
				Victim.Character.Humanoid.Health = Victim.Character.Humanoid.MaxHealth
			end
			Evt.MedSys.MedHandler:FireClient(Victim,5)
			return;

		elseif Mode == 6 and P1_Client.Kit.Suppressant.Value > 0 and P2_Client.Variaveis.Dor.Value > 0 then
			P1_Client.Kit.Suppressant.Value = P1_Client.Kit.Suppressant.Value - 1
			P2_Client.Variaveis.Dor.Value = 0
			Evt.MedSys.MedHandler:FireClient(Victim,6)
			return;

		elseif Mode == 7 and P1_Client.Kit.EnergyShot.Value > 0 and P2_Client:GetAttribute("Collapsed")then
			P1_Client.Kit.EnergyShot.Value = P1_Client.Kit.EnergyShot.Value - 1
			local HealValue = math.random(45,55)
			if Victim.Character.Humanoid.Health + HealValue < Victim.Character.Humanoid.MaxHealth then
				Victim.Character.Humanoid:TakeDamage(-HealValue)
			else
				Victim.Character.Humanoid.Health = Victim.Character.Humanoid.MaxHealth
			end
			P2_Client:SetAttribute("Collapsed",false)
			Evt.MedSys.MedHandler:FireClient(Victim,7)
			return;

		elseif Mode == 8 and P1_Client.Kit.BloodBag.Value > 0 and P2_Client.Variaveis.Sangue.Value < P2_Client.Variaveis.Sangue.MaxValue then
			P1_Client.Kit.BloodBag.Value = P1_Client.Kit.BloodBag.Value - 1
			P2_Client.Variaveis.Sangue.Value = P2_Client.Variaveis.Sangue.Value + 2000
			return;
		else
			warn(Player.Name.."'s Action Failed: Unknow Method")
		end
		return;
	end 
	--Self treat
	if Mode == 1 and P1_Client.Kit.Bandage.Value > 0 and P1_Client:GetAttribute("Bleeding") then
		P1_Client.Kit.Bandage.Value = P1_Client.Kit.Bandage.Value - 1
		P1_Client:SetAttribute("Bleeding",false)
		return;

	elseif Mode == 2 and P1_Client.Kit.Splint.Value > 0 and P1_Client:GetAttribute("Injured") then
		P1_Client.Kit.Splint.Value = P1_Client.Kit.Splint.Value - 1
		P1_Client:SetAttribute("Injured",false)
		return;

	elseif Mode == 3 and (P1_Client:GetAttribute("Bleeding") or P1_Client:GetAttribute("Tourniquet")) then --Tourniquet works a little diferent :T
		if P1_Client:GetAttribute("Tourniquet") then
			P1_Client.Kit.Tourniquet.Value = P1_Client.Kit.Tourniquet.Value + 1
			P1_Client:SetAttribute("Tourniquet",false)
			return;
		end

		if P1_Client.Kit.Tourniquet.Value <= 0 then return; end;
		P1_Client.Kit.Tourniquet.Value = P1_Client.Kit.Tourniquet.Value - 1
		P1_Client:SetAttribute("Tourniquet",true)
		return;

	elseif Mode == 4 and P1_Client.Kit.PainRelief.Value > 0 and P1_Client.Variaveis.Dor.Value > 0 then
		P1_Client.Kit.PainRelief.Value = P1_Client.Kit.PainRelief.Value - 1
		P1_Client.Variaveis.Dor.Value = math.clamp(P1_Client.Variaveis.Dor.Value - math.random(35,65),0,300)
		Evt.MedSys.MedHandler:FireClient(Player,4)
		return;

	elseif Mode == 5 and P1_Client.Kit.EnergyShot.Value > 0 and Player.Character.Humanoid.Health < Player.Character.Humanoid.MaxHealth then
		P1_Client.Kit.EnergyShot.Value = P1_Client.Kit.EnergyShot.Value - 1
		local HealValue = math.random(15,25)
		if Player.Character.Humanoid.Health + HealValue < Player.Character.Humanoid.MaxHealth then
			Player.Character.Humanoid:TakeDamage(-HealValue)
		else
			Player.Character.Humanoid.Health = Player.Character.Humanoid.MaxHealth
		end
		Evt.MedSys.MedHandler:FireClient(Player,5)
		return;

	elseif Mode == 6 and P1_Client.Kit.Suppressant.Value > 0 and P1_Client.Variaveis.Dor.Value > 0 then
		P1_Client.Kit.Suppressant.Value = P1_Client.Kit.Suppressant.Value - 1
		P1_Client.Variaveis.Dor.Value = math.clamp(P1_Client.Variaveis.Dor.Value - math.random(125,175),0,300)
		Evt.MedSys.MedHandler:FireClient(Player,6)
		return;

	elseif Mode == 7 and P1_Client.Kit.EnergyShot.Value > 0 and Player.Character.Humanoid.Health < Player.Character.Humanoid.MaxHealth then
		P1_Client.Kit.EnergyShot.Value = P1_Client.Kit.EnergyShot.Value - 1
		local HealValue = math.random(45,55)
		if Player.Character.Humanoid.Health + HealValue < Player.Character.Humanoid.MaxHealth then
			Player.Character.Humanoid:TakeDamage(-HealValue)
		else
			Player.Character.Humanoid.Health = Player.Character.Humanoid.MaxHealth
		end
		Evt.MedSys.MedHandler:FireClient(Player,7)
		return;

	elseif Mode == 8 and P1_Client.Kit.BloodBag.Value > 0 and P1_Client.Variaveis.Sangue.Value < P1_Client.Variaveis.Sangue.MaxValue then
		P1_Client.Kit.BloodBag.Value = P1_Client.Kit.BloodBag.Value - 1
		P1_Client.Variaveis.Sangue.Value = P1_Client.Variaveis.Sangue.Value + 2000
		return;
	else
		warn(Player.Name.."'s Action Failed: Unknow Method")
	end
	return;
end)

Evt.Drag.OnServerEvent:Connect(function(player,Victim)
	if not player or not player.Character then return; end;

	local P1_Client = player.Character:FindFirstChild("ACS_Client")
	local Human 	= player.Character:FindFirstChild("Humanoid")

	if not P1_Client then return; end;

	if Victim then
		P1_Client:SetAttribute("DragPlayer",Victim.Name)
	else
		P1_Client:SetAttribute("DragPlayer","")
		P1_Client:SetAttribute("Dragging",false)
	end

	local target = P1_Client:GetAttribute("DragPlayer")

	if P1_Client:GetAttribute("Collapsed") or target == "" then return; end;

	local player2 = game.Players:FindFirstChild(target)
	if not player2 or not player2.Character then return; end;

	local PlHuman = player2.Character:FindFirstChild("Humanoid")
	local P2_Client = player2.Character:FindFirstChild("ACS_Client")
	if not PlHuman or not P2_Client then return; end;

	if P1_Client:GetAttribute("Dragging") then return; end;
	if not P2_Client:GetAttribute("Collapsed") then return; end;
	P1_Client:SetAttribute("Dragging",true)	
	while P1_Client:GetAttribute("Dragging") and target ~= "" and P2_Client:GetAttribute("Collapsed") and PlHuman.Health > 0 and Human.Health > 0 and not P1_Client:GetAttribute("Collapsed") do wait() 
		player2.Character.Torso.Anchored = true
		player2.Character.Torso.CFrame = Human.Parent.Torso.CFrame*CFrame.new(0,0.75,1.5)*CFrame.Angles(math.rad(0), math.rad(0), math.rad(90))
	end
	player2.Character.Torso.Anchored = false		
end)

function BreachFunction(Player,Mode,BreachPlace,Pos,Norm,Hit)

	if Mode == 1 then
		if Player.Character.ACS_Client.Kit.BreachCharges.Value > 0 then
			Player.Character.ACS_Client.Kit.BreachCharges.Value = Player.Character.ACS_Client.Kit.BreachCharges.Value - 1
			BreachPlace.Destroyed.Value = true
			local C4 = Engine.FX.BreachCharge:Clone()

			C4.Parent = BreachPlace.Destroyable
			C4.Center.CFrame = CFrame.new(Pos, Pos + Norm) * CFrame.Angles(math.rad(-90),math.rad(0),math.rad(0))
			C4.Center.Place:play()

			local weld = Instance.new("WeldConstraint")
			weld.Parent = C4
			weld.Part0 = BreachPlace.Destroyable.Charge
			weld.Part1 = C4.Center

			wait(1)
			C4.Center.Beep:play()
			wait(4)
			if C4 and C4:FindFirstChild("Center") then
				local att = Instance.new("Attachment")
				att.CFrame = C4.Center.CFrame
				att.Parent = workspace.Terrain

				local aw = Engine.FX.ExpEffect:Clone()
				aw.Parent = att
				aw.Enabled = false
				aw:Emit(35)
				Debris:AddItem(aw,aw.Lifetime.Max)

				local Exp = Instance.new("Explosion")
				Exp.BlastPressure = 0
				Exp.BlastRadius = 0
				Exp.DestroyJointRadiusPercent = 0
				Exp.Position = C4.Center.Position
				Exp.Parent = workspace

				local S = Instance.new("Sound")
				S.EmitterSize = 10
				S.MaxDistance = 1000
				S.SoundId = "rbxassetid://"..Explosion[math.random(1, 7)]
				S.PlaybackSpeed = math.random(30,55)/40
				S.Volume = 2
				S.Parent = att
				S.PlayOnRemove = true
				S:Destroy()

				for SKP_001, SKP_002 in pairs(game.Players:GetChildren()) do
					if SKP_002:IsA('Player') and SKP_002.Character and SKP_002.Character:FindFirstChild('Head') and (SKP_002.Character.Head.Position - C4.Center.Position).magnitude <= 15 then
						local DistanceMultiplier = (((SKP_002.Character.Head.Position - C4.Center.Position).magnitude/35) - 1) * -1
						local intensidade = DistanceMultiplier
						local Tempo = 15 * DistanceMultiplier
						Evt.Suppression:FireClient(SKP_002,2,intensidade,Tempo)
					end
				end

				Debris:AddItem(BreachPlace.Destroyable,0)
			end
		end

	elseif Mode == 2 then

		local aw = Engine.FX.DoorBreachFX:Clone()
		aw.Parent = BreachPlace.Door.Door
		aw.RollOffMaxDistance = 100
		aw.RollOffMinDistance = 5
		aw:Play()

		BreachPlace.Destroyed.Value = true
		if BreachPlace.Door:FindFirstChild("Hinge") ~= nil then
			BreachPlace.Door.Hinge:Destroy()
		end
		if BreachPlace.Door:FindFirstChild("Knob") ~= nil then
			BreachPlace.Door.Knob:Destroy()
		end

		local forca = Instance.new("BodyForce")
		forca.Force = -Norm * BreachPlace.Door.Door:GetMass() * Vector3.new(50,0,50)
		forca.Parent = BreachPlace.Door.Door

		Debris:AddItem(BreachPlace,3)

	elseif Mode == 3 then
		if Player.Character.ACS_Client.Kit.Fortifications.Value > 0 then
			Player.Character.ACS_Client.Kit.Fortifications.Value = Player.Character.ACS_Client.Kit.Fortifications.Value - 1
			BreachPlace.Fortified.Value = true
			local C4 = Instance.new('Part')

			C4.Parent = BreachPlace.Destroyable
			C4.Size =  Vector3.new(Hit.Size.X + .05,Hit.Size.Y + .05,Hit.Size.Z + 0.5) 
			C4.Material = Enum.Material.DiamondPlate
			C4.Anchored = true
			C4.CFrame = Hit.CFrame

			local S = Engine.FX.FortFX:Clone()
			S.PlaybackSpeed = math.random(30,55)/40
			S.Volume = 1
			S.Parent = C4
			S.PlayOnRemove = true
			S:Destroy()
		end
	end
end

Evt.Breach.OnServerInvoke = BreachFunction

function UpdateLog(Player,humanoid)

	local tag = humanoid:findFirstChild("creator")

	if tag ~= nil then

		local hours = os.date("*t")["hour"]
		local mins = os.date("*t")["min"]
		local sec = os.date("*t")["sec"]
		local TagType = tag:findFirstChild("type")

		if tag.Value.Name == Player.Name then
			local String = Player.Name.." Died | "..hours..":"..mins..":"..sec
			table.insert(CombatLog,String)
		else
			local String = tag.Value.Name.." Killed "..Player.Name.." | "..hours..":"..mins..":"..sec
			table.insert(CombatLog,String)
		end

		if #CombatLog > 50 then
			Backup = Backup + 1
			warn("ACS: Cleaning Combat Log | Backup: "..Backup)
			warn(CombatLog)
			CombatLog = {}
		end
	end
end