function ZoneController.getTouchingZones(t,F,A,W)local Q,I if W then Q=W.exitDetections[t]W.exitDetections[t]=nil end I=Q or A local h,g local Y=t:IsA("\066\097\115\101\080\097\114\116")local y=not Y local m={}if Y then h,g=t.Size,t.CFrame table.insert(m,t)elseif I==enum.Detection.WholeBody then h,g=Tracker.getCharacterSize(t)m=t:GetChildren()else local F=t:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")if F then h,g=F.Size,F.CFrame table.insert(m,F)end end if not h or not g then return{}end local c=F and activeParts or allParts local R=F and activePartToZone or allPartToZone local H=OverlapParams.new()H.FilterType=Enum.RaycastFilterType.Whitelist H.MaxParts=#c H.FilterDescendantsInstances=c local L={}local d={}local v=CollectiveWorldModel:GetPartBoundsInBox(g,h,H)local B={}for t,F in pairs(v)do local A=R[F]if A and A.allZonePartsAreBlocks then d[A]=true L[F]=A else table.insert(B,F)end end local j=#B local M=0 if j>0 then local F=OverlapParams.new()F.FilterType=Enum.RaycastFilterType.Whitelist F.MaxParts=j F.FilterDescendantsInstances=B local A=t for t,A in pairs(m)do local W=false if not A:IsA("\066\097\115\101\080\097\114\116")or y and Tracker.bodyPartsToIgnore[A.Name]then continue end local Q=CollectiveWorldModel:GetPartsInPart(A,F)for t,F in pairs(Q)do if not L[F]then local t=R[F]if t then d[t]=true L[F]=t M+=1 end if M==j then W=true break end end end if W then break end end end local f={}local w for t,F in pairs(d)do if w==nil or t._currentExitDetection<w then w=t._currentExitDetection end table.insert(f,t)end if w and W then W.exitDetections[t]=w end return f,L end local t={}function ZoneController.setGroup(F,A)local W=t[F]if not W then W={}t[F]=W end W.onlyEnterOnceExitedAll=true W._name=F W._memberZones={}if typeof(A)=="\116\097\098\108\101"then for t,F in pairs(A)do W[t]=F end end return W end function ZoneController.getGroup(F)return t[F]end local F local A=string.format("\090\111\110\101\080\108\117\115\037\115\067\111\110\116\097\105\110\101\114",runService:IsClient()and"\067\108\105\101\110\116"or"\083\101\114\118\101\114")function ZoneController.getWorkspaceContainer()local t=F or workspace:FindFirstChild(A)if not t then t=Instance.new("\070\111\108\100\101\114")t.Name=A t.Parent=workspace F=t end return t end return ZoneController


function ZoneController.getTouchingZones(item, onlyActiveZones, recommendedDetection, tracker)
	local exitDetection, finalDetection
	if tracker then
		exitDetection = tracker.exitDetections[item]
		tracker.exitDetections[item] = nil
	end
	finalDetection = exitDetection or recommendedDetection

	local itemSize, itemCFrame
	local itemIsBasePart = item:IsA("BasePart")
	local itemIsCharacter = not itemIsBasePart
	local bodyPartsToCheck = {}
	if itemIsBasePart then
		itemSize, itemCFrame = item.Size, item.CFrame
		table.insert(bodyPartsToCheck, item)
	elseif finalDetection == enum.Detection.WholeBody then
		itemSize, itemCFrame = Tracker.getCharacterSize(item)
		bodyPartsToCheck = item:GetChildren()
	else
		local hrp = item:FindFirstChild("HumanoidRootPart")
		if hrp then
			itemSize, itemCFrame = hrp.Size, hrp.CFrame
			table.insert(bodyPartsToCheck, hrp)
		end
	end
	if not itemSize or not itemCFrame then return {} end

	--[[
	local part = Instance.new("Part")
	part.Size = itemSize
	part.CFrame = itemCFrame
	part.Anchored = true
	part.CanCollide = false
	part.Color = Color3.fromRGB(255, 0, 0)
	part.Transparency = 0.4
	part.Parent = workspace
	game:GetService("Debris"):AddItem(part, 2)
	--]]
	local partsTable = (onlyActiveZones and activeParts) or allParts
	local partToZoneDict = (onlyActiveZones and activePartToZone) or allPartToZone

	local boundParams = OverlapParams.new()
	boundParams.FilterType = Enum.RaycastFilterType.Whitelist
	boundParams.MaxParts = #partsTable
	boundParams.FilterDescendantsInstances = partsTable

	-- This retrieves the bounds (the rough shape) of all parts touching the item/character
	-- If the corresponding zone is made up of *entirely* blocks then the bound will
	-- be the actual shape of the part.
	local touchingPartsDictionary = {}
	local zonesDict = {}
	local boundParts = CollectiveWorldModel:GetPartBoundsInBox(itemCFrame, itemSize, boundParams)
	local boundPartsThatRequirePreciseChecks = {}
	for _, boundPart in pairs(boundParts) do
		local correspondingZone = partToZoneDict[boundPart]
		if correspondingZone and correspondingZone.allZonePartsAreBlocks then
			zonesDict[correspondingZone] = true
			touchingPartsDictionary[boundPart] = correspondingZone
		else
			table.insert(boundPartsThatRequirePreciseChecks, boundPart)
		end
	end

	-- If the bound parts belong to a zone that isn't entirely made up of blocks, then
	-- we peform additional checks using GetPartsInPart which enables shape
	-- geometries to be precisely determined for non-block baseparts.
	local totalRemainingBoundParts = #boundPartsThatRequirePreciseChecks
	local precisePartsCount = 0
	if totalRemainingBoundParts > 0 then
		
		local preciseParams = OverlapParams.new()
		preciseParams.FilterType = Enum.RaycastFilterType.Whitelist
		preciseParams.MaxParts = totalRemainingBoundParts
		preciseParams.FilterDescendantsInstances = boundPartsThatRequirePreciseChecks

		local character = item
		for _, bodyPart in pairs(bodyPartsToCheck) do
			local endCheck = false
			if not bodyPart:IsA("BasePart") or (itemIsCharacter and Tracker.bodyPartsToIgnore[bodyPart.Name]) then
				continue
			end
			local preciseParts = CollectiveWorldModel:GetPartsInPart(bodyPart, preciseParams)
			for _, precisePart in pairs(preciseParts) do
				if not touchingPartsDictionary[precisePart] then
					local correspondingZone = partToZoneDict[precisePart]
					if correspondingZone then
						zonesDict[correspondingZone] = true
						touchingPartsDictionary[precisePart] = correspondingZone
						precisePartsCount += 1
					end
					if precisePartsCount == totalRemainingBoundParts then
						endCheck = true
						break
					end
				end
			end
			if endCheck then
				break
			end
		end
	end
	
	local touchingZonesArray = {}
	local newExitDetection
	for zone, _ in pairs(zonesDict) do
		if newExitDetection == nil or zone._currentExitDetection < newExitDetection then
			newExitDetection = zone._currentExitDetection
		end
		table.insert(touchingZonesArray, zone)
	end
	if newExitDetection and tracker then
		tracker.exitDetections[item] = newExitDetection
	end
	return touchingZonesArray, touchingPartsDictionary
end

local settingsGroups = {}
function ZoneController.setGroup(settingsGroupName, properties)
	local group = settingsGroups[settingsGroupName]
	if not group then
		group = {}
		settingsGroups[settingsGroupName] = group
	end
	

	-- PUBLIC PROPERTIES --
	group.onlyEnterOnceExitedAll = true
	
	-- PRIVATE PROPERTIES --
	group._name = settingsGroupName
	group._memberZones = {}


	if typeof(properties) == "table" then
		for k, v in pairs(properties) do
			group[k] = v
		end
	end
	return group
end

function ZoneController.getGroup(settingsGroupName)
	return settingsGroups[settingsGroupName]
end

local workspaceContainer
local workspaceContainerName = string.format("ZonePlus%sContainer", (runService:IsClient() and "Client") or "Server")
function ZoneController.getWorkspaceContainer()
	local container = workspaceContainer or workspace:FindFirstChild(workspaceContainerName)
	if not container then
		container = Instance.new("Folder")
		container.Name = workspaceContainerName
		container.Parent = workspace
		workspaceContainer = container
	end
	return container
end



return ZoneController
