---------------------------------------------------------------|
---------------------------------------------------------------|
---------------------------------------------------------------|
local Debris = game.Debris

local Engine = game.ReplicatedStorage:WaitForChild("ACS_Engine")
local Evt = Engine:WaitForChild("Events")
local ServerConfig = require(Engine.GameRules:WaitForChild("Config"))

local Settings = script.Parent.ACS_Modulo:WaitForChild("Config")
local Weapon = script.Parent:FindFirstChildOfClass("Tool")
local WeaponData = require(Weapon:WaitForChild("ACS_Settings"))

local ACS_Storage = workspace:WaitForChild("ACS_WorkSpace")

local Modules = Engine:WaitForChild("Modules")

local Hitmarker = require(Modules:WaitForChild("Hitmarker"))
local Ragdoll = require(Modules:WaitForChild("Ragdoll"))

local Ignore_Model = ACS_Storage:FindFirstChild("Server")

local BulletModel = ACS_Storage:FindFirstChild("Client")

local Ray_Ignore = {Ignore_Model, BulletModel, ACS_Storage}

local Dead = false
task.wait(3)
script.Parent.ForceField:Destroy()

script.Parent.Humanoid.Died:connect(function()
	Ragdoll(script.Parent)
	if script.Parent:FindFirstChild("Gun") ~= nil then
		script.Parent.Gun.CanCollide = true
	end
	Dead = true
	Debris:AddItem(script.Parent,game.Players.RespawnTime)
end)

local CanSee = false
local CurrentPart = nil

function onTouched(hit)
	if hit.Parent == nil then
		return
	end

	local humanoid = hit.Parent:findFirstChild("Humanoid")

	if humanoid == nil then
		CurrentPart = hit
	end
end

function waitForChild(parent, childName)
	local child = parent:findFirstChild(childName)

	if child then
		return child
	end

	while true do
		--print(childName)
		child = parent.ChildAdded:wait()

		if child.Name==childName then
			return child
		end
	end
end

local Figure = script.Parent
local Humanoid = waitForChild(Figure, "Humanoid")
local Torso = waitForChild(Figure, "Torso")
local Left = waitForChild(Figure, "Left Leg")
local Right = waitForChild(Figure, "Right Leg")

Left.Touched:connect(onTouched)
Right.Touched:connect(onTouched)

function getHumanoid(model)
	for _, v in pairs(model:GetChildren())do
		if v:IsA'Humanoid' then
			return v
		end
	end
end


local zombie = script.Parent
local human = getHumanoid(zombie)
local hroot = zombie.Torso
local head = zombie:FindFirstChild'Head'

local pfs = game:GetService("PathfindingService")
local players = game:GetService('Players')

local RecoilSpread = Spread/100
local perception = 0
local Memory = 0


task.wait(.1)

local ammo=Settings.Ammo.Value -- How much ammo the Enemy has
local w=.14
local RPM = 1/(WeaponData.ShootRate/60)
local r=false
local t=script.Parent


local h=t:WaitForChild'Grip'

function Hitmaker(HitPart, Position, Normal, Material)

	Evt.HitEffect:FireAllClients(nil, Position, HitPart, Normal, Material, WeaponData)

end


function CheckForHumanoid(L_225_arg1)
	local L_226_ = false
	local L_227_ = nil
	if L_225_arg1 then
		if (L_225_arg1.Parent:FindFirstChild("Humanoid") or L_225_arg1.Parent.Parent:FindFirstChild("Humanoid")) then
			L_226_ = true
			if L_225_arg1.Parent:FindFirstChild('Humanoid') then
				L_227_ = L_225_arg1.Parent.Humanoid
			elseif L_225_arg1.Parent.Parent:FindFirstChild('Humanoid') then
				L_227_ = L_225_arg1.Parent.Parent.Humanoid
			end
		else
			L_226_ = false
		end	
	end
	return L_226_, L_227_
end

function CalcularDano(DanoBase,Dist,Vitima,Type)

	local damage = 0
	local VestDamage = 0
	local HelmetDamage = 0
	local Traveleddamage = DanoBase-(math.ceil(Dist)/40)*WeaponData.DamageFallOf
	if Vitima.Parent:FindFirstChild("Saude") ~= nil then

		local Vest =  Vitima.Parent.Saude.Protecao.VestVida
		local Vestfactor = Vitima.Parent.Saude.Protecao.VestProtect
		local Helmet = Vitima.Parent.Saude.Protecao.HelmetVida
		local Helmetfactor = Vitima.Parent.Saude.Protecao.HelmetProtect

		if Type == "Head" then
			if Helmet.Value > 0 and (WeaponData.BulletPenetration) < Helmetfactor.Value  then
				damage = Traveleddamage * ((WeaponData.BulletPenetration)/Helmetfactor.Value)
				HelmetDamage = (Traveleddamage * ((100 - WeaponData.BulletPenetration)/Helmetfactor.Value))

				if HelmetDamage <= 0 then
					HelmetDamage = 0.5
				end

			elseif Helmet.Value > 0 and (WeaponData.BulletPenetration) >= Helmetfactor.Value then
				damage = Traveleddamage
				HelmetDamage = (Traveleddamage * ((100 - WeaponData.BulletPenetration)/Helmetfactor.Value))

				if HelmetDamage <= 0 then
					HelmetDamage = 1
				end				

			elseif Helmet.Value <= 0 then
				damage = Traveleddamage

			end
		else
			if Vest.Value > 0 and (WeaponData.BulletPenetration) < Vestfactor.Value  then
				damage = Traveleddamage * ((WeaponData.BulletPenetration)/Vestfactor.Value)
				VestDamage = (Traveleddamage * ((100 - WeaponData.BulletPenetration)/Vestfactor.Value))

				if VestDamage <= 0 then
					VestDamage = 0.5
				end

			elseif Vest.Value > 0 and (WeaponData.BulletPenetration) >= Vestfactor.Value then
				damage = Traveleddamage
				VestDamage = (Traveleddamage * ((100 - WeaponData.BulletPenetration)/Vestfactor.Value))

				if VestDamage <= 0 then
					VestDamage =  1
				end				

			elseif Vest.Value <= 0 then
				damage = Traveleddamage

			end
		end
	else
		damage = Traveleddamage
	end			
	if damage <= 0 then
		damage = 1
	end

	return damage,VestDamage,HelmetDamage
end

function Damage(VitimaHuman,Dano,DanoColete,DanoCapacete)
	if VitimaHuman.Parent:FindFirstChild("Saude") ~= nil then
		local Colete = VitimaHuman.Parent.Saude.Protecao.VestVida
		local Capacete = VitimaHuman.Parent.Saude.Protecao.HelmetVida
		Colete.Value = Colete.Value - DanoColete
		Capacete.Value = Capacete.Value - DanoCapacete
	end
	VitimaHuman:TakeDamage(Dano)
end

local function reload(boo)
	if(boo and ammo ~= Settings.Ammo.Value)or ammo==0 then
		r=true
		if w then
			w=.03
		end
		h.Reload:Play()
		task.wait(3) -- How long the Enemy reloads
		ammo= Settings.Ammo.Value
		if w then
			w=.14
		end
		r=false
	elseif boo then
		task.wait(.1)
	end
end
local function near()
	if not Dead then
		local dis,pl= Settings.ShotDistance.Value ,nil -- Range of the Enemy
		for _,v in ipairs(game.Players:GetPlayers())do
			if v.Character and v.Character:FindFirstChild'Humanoid'and v:DistanceFromCharacter(h.Position)<dis then
				dis,pl=v:DistanceFromCharacter(h.Position),v
			end
		end
		if pl then
			return pl.Character:GetModelCFrame(),dis,CFrame.new(pl.Character.Humanoid.WalkToPoint).lookVector
		else
			return nil
		end
	end
end