local v=game:GetService("\083\101\114\118\101\114\083\099\114\105\112\116\083\101\114\118\105\099\101")local m=game:GetService("\083\101\114\118\101\114\083\116\111\114\097\103\101")local u=game:GetService("\087\111\114\107\115\112\097\099\101")local q=v:WaitForChild("\070\105\110\100\101\114\115",2)local d=require(q:WaitForChild("\068\111\109\097\105\110\070\105\110\100\101\114",2))local X=d:FindDomain("\101\120\110\105\104\105\108\111")local T=v:WaitForChild("\065\103\101\110\116\115",2)local F=require(T:WaitForChild("\080\097\116\104\102\105\110\100\105\110\103\065\105\066\097\115\101"))local s=require(T:WaitForChild("\078\112\099\078\097\109\101\115"))local p=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local N=require(p:WaitForChild("\078\101\118\101\114\109\111\114\101"))local Y=N("\114\111\100\097\115\104")local B=N("\100\101\115\116\105\110\097\116\105\111\110\115")local j=N("\083\116\100")local b=j.rquery local x=j.randumb local V={}function V.ChooseRandomSpawnLocation()local v=u:GetChildren()local m={}for v,u in pairs(v)do if u.Name=="\083\112\097\119\110\076\111\099\097\116\105\111\110"then table.insert(m,u)end end local q=B.GetRandomCFrameFromTableOfParts(m)+Vector3.new(0,10,0)return q end function V.SpawnPersonage(v,q,d,T)local F=Instance.new("\077\111\100\101\108")local s=(m:WaitForChild("\072\117\109\097\110\111\105\100\115")):WaitForChild(v)X.CreateFromFolder(s,q,d,function(v)F=v F.Parent=u T(v)end)end function V.SpawnMaleHumanoid(v,m,u)local q="\077\097\108\101\072\117\109\097\110\111\105\100\115"V.SpawnPersonage(q,v,m,u)end function V.SpawnFemaleHumanoid(v,m,u)local q="\070\101\109\097\108\101\072\117\109\097\110\111\105\100\115"V.SpawnPersonage(q,v,m,u)end function V.FindRandomZombie()local v=(u:WaitForChild("\078\112\099\115")):GetChildren()local m=Y.filter(v,function(v)return Y.endsWith(v.Name,"\090\111\109\098\105\101")end)local q=x:GetOneAtRandom(m)return q end function V.GetRandomZombieRootPart()local v=V.FindRandomZombie()local m=v:WaitForChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")return m end function V.CreateRunner(v,m,u)local q=V.ChooseRandomSpawnLocation()local d=V.GetRandomZombieRootPart()local X=function(v)local u=F.new(v)v.Name=m print("\080\101\114\115\111\110\097\103\101 \115\112\097\119\110\101\100\058 "..v.Name)local q=u:MoveTo(d,true)end local T=""if v=="\070"then T="\070\101\109\097\108\101\072\117\109\097\110\111\105\100\115"else T="\077\097\108\101\072\117\109\097\110\111\105\100\115"end V.SpawnPersonage(T,u,q,X)end function V.CreateFemaleRunner()local v="\071\101\110\101\114\105\099\070\101\109\097\108\101\051"local m=(s.GetFemaleName()).FullName V.CreateRunner("\070",m,v)end function V.CreateMaleRunner()local v="\071\101\110\101\114\105\099\077\097\108\101"local m=(s.GetMaleName()).FullName V.CreateRunner("\077",m,v)end return V
local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")
local Workspace = game:GetService("Workspace")

local findersFolder = ServerScriptService:WaitForChild("Finders", 2)
local DomainFinder = require(findersFolder:WaitForChild("DomainFinder", 2))
local exNihilo = DomainFinder:FindDomain("exnihilo")

local agentsFolder = ServerScriptService:WaitForChild("Agents", 2)
local pathfindingAi = require(agentsFolder:WaitForChild("PathfindingAiBase"))
local npcNames = require(agentsFolder:WaitForChild("NpcNames"))

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local require = require(ReplicatedStorage:WaitForChild("Nevermore"))

local _ = require("rodash")
local destinations = require("destinations")
local std = require("Std")
local rq = std.rquery
local randumb = std.randumb

local module = {}

function module.ChooseRandomSpawnLocation()
    local wsChildren = Workspace:GetChildren()
	local spawnLocations = {}

	for _, child in pairs(wsChildren) do
		if child.Name == "SpawnLocation" then
			table.insert(spawnLocations, child)
		end
	end

    local foundLocation = destinations.GetRandomCFrameFromTableOfParts(spawnLocations) + Vector3.new(0, 10, 0)
    
	return foundLocation
end

function module.SpawnPersonage(storageFolder,
	personagePrototypeId,
	spawnLocation,
	onSpawnCompleteCallback)

    local spawnedPersonage = Instance.new("Model")
	local storageFolderInstance = ServerStorage:WaitForChild("Humanoids"):WaitForChild(storageFolder)

	exNihilo.CreateFromFolder(storageFolderInstance,
        personagePrototypeId,
        spawnLocation,
        function(createdPersonage)
            spawnedPersonage = createdPersonage
            spawnedPersonage.Parent = Workspace
            onSpawnCompleteCallback(createdPersonage)
        end)
end

function module.SpawnMaleHumanoid(
	personagePrototypeId,
	spawnLocation,
	onSpawnCompleteCallback)

	local storageFolder = "MaleHumanoids"
	
    module.SpawnPersonage(storageFolder,
		personagePrototypeId,
		spawnLocation,
		onSpawnCompleteCallback
	)
end

function module.SpawnFemaleHumanoid(
	personagePrototypeId,
	spawnLocation,
	onSpawnCompleteCallback)

	local storageFolder = "FemaleHumanoids"

    module.SpawnPersonage(storageFolder,
        personagePrototypeId,
		spawnLocation,
		onSpawnCompleteCallback
	)
end

function module.FindRandomZombie()
    local npcFolderContents = Workspace:WaitForChild("Npcs"):GetChildren()
	local zombies = _.filter(npcFolderContents, function ( entry )
		return _.endsWith(entry.Name, "Zombie")
	end)

    local selectedZombie = randumb:GetOneAtRandom(zombies)
    
    return selectedZombie
end

function module.GetRandomZombieRootPart()

    local selectedZombie = module.FindRandomZombie()
    local rootPart =  selectedZombie:WaitForChild("HumanoidRootPart")
    return rootPart
end

function module.CreateRunner(gender, npcName, personagePrototypeId)
    
	local spawnLocation = module.ChooseRandomSpawnLocation()
    
    local destination = module.GetRandomZombieRootPart()
	
	local onSpawnCompleteCallback = function (createdPersonage)
		local aiInstance = pathfindingAi.new(createdPersonage)
		createdPersonage.Name = npcName
		print("Personage spawned: " .. createdPersonage.Name)
		local goGoGo = aiInstance:MoveTo(destination , true)
	end

    local storageFolder = "" 
    if gender == "F" then 
        storageFolder = "FemaleHumanoids"
    else
        storageFolder = "MaleHumanoids"
    end

    module.SpawnPersonage(storageFolder,
        personagePrototypeId,
		spawnLocation,
		onSpawnCompleteCallback
	)
end

function module.CreateFemaleRunner()
    local personagePrototypeId = "GenericFemale3"
    local name = npcNames.GetFemaleName().FullName
    
    module.CreateRunner("F", name, personagePrototypeId)
end


function module.CreateMaleRunner()
    local personagePrototypeId = "GenericMale"
    local name = npcNames.GetMaleName().FullName
    
    module.CreateRunner("M", name, personagePrototypeId)
end

return module