function RoomPackager.CategoriseModel(J,E)if E:FindFirstChild("\069\110\100\080\097\114\116\115")then return game.ReplicatedStorage.PathModules.Branch elseif E.Start.Position.Y<E.End.Position.Y-5 then return game.ReplicatedStorage.PathModules.GoingUp elseif E.Start.Position.Y>E.End.Position.Y+5 then return game.ReplicatedStorage.PathModules.GoingDown else return game.ReplicatedStorage.PathModules.SameHeight end end local function E(J,Z)local y=J:GetChildren()for J=1,#y,1 do if y[J]:isA("\066\097\115\101\080\097\114\116")then(Z:Clone()).Parent=y[J]else E(y[J],Z)end end end RoomPackager.setUpBehaviours=function(J)if J:FindFirstChild("\066\101\104\097\118\105\111\117\114\115")then E(J,J.Behaviours)return end local Z=J:GetChildren()for E=1,#Z,1 do RoomPackager.setUpBehaviours(Z[E])end end local function J(E,J,Z)if J.Parent==E then return end if J.Name=="\069\110\100"and E:FindFirstChild("\069\110\100")then local Z=Instance.new("\077\111\100\101\108")Z.Name="\069\110\100\080\097\114\116\115"Z.Parent=E J.Parent=Z;(E:FindFirstChild("\069\110\100")).Parent=Z elseif J.Name=="\069\110\100"and E:FindFirstChild("\069\110\100\080\097\114\116\115")then J.Parent=E:FindFirstChild("\069\110\100\080\097\114\116\115")elseif J.Name=="\069\110\100"then J.Parent=E else local y=closestParentToWorkspace(J,E)if y~=nil then if y:isA("\066\097\115\101\080\097\114\116")then local J=y:GetConnectedParts(true)for J,p in pairs(J)do if p.Name=="\069\110\100"then table.insert(Z,p)else local J=closestParentToWorkspace(p,E)if J and J~=y then J.Parent=E end end end end y.Parent=E end end end function RoomPackager.PackageRoom(Z,E)local y=Instance.new("\077\111\100\101\108")y.Name="\080\097\116\104"y.Parent=game.ReplicatedStorage.PathModules local p=Z:RegionsFromBasePlate(E)for E=1,#p,1 do while true do local Z=game.Workspace:FindPartsInRegion3(p[E],nil,200)if#Z==0 then break end for E,p in pairs(Z)do J(y,p,Z)end end end E.Transparency=1 E.Parent=y;(y:FindFirstChild("\083\116\097\114\116",true)).Parent=y;(y:FindFirstChild("\083\116\097\114\116",true)).Transparency=1 if y:FindFirstChild("\069\110\100\080\097\114\116\115")then local E=(y:FindFirstChild("\069\110\100\080\097\114\116\115")):GetChildren()for J=1,#E,1 do E[J].Transparency=1 end else y.End.Transparency=1 end y.PrimaryPart=E y.Parent=Z:CategoriseModel(y)RoomPackager.setUpBehaviours(y)return y end return RoomPackager

function RoomPackager:CategoriseModel(pathModel)
	if pathModel:FindFirstChild("EndParts") then
		return game.ReplicatedStorage.PathModules.Branch
	elseif pathModel.Start.Position.Y < pathModel.End.Position.Y - 5 then
		return game.ReplicatedStorage.PathModules.GoingUp
	elseif pathModel.Start.Position.Y > pathModel.End.Position.Y + 5 then
		return game.ReplicatedStorage.PathModules.GoingDown
	else
		return game.ReplicatedStorage.PathModules.SameHeight
	end
end

local function addBehavioursRecur(model, behaviourFolder)
	local children = model:GetChildren()
	for i = 1, #children do
		if children[i]:isA("BasePart") then
			behaviourFolder:Clone().Parent = children[i]
		else
			addBehavioursRecur(children[i], behaviourFolder)
		end
	end
end

RoomPackager.setUpBehaviours = function(roomModel)
	if roomModel:FindFirstChild("Behaviours") then
		addBehavioursRecur(roomModel, roomModel.Behaviours)
		return
	end
	local children = roomModel:GetChildren()
	for i = 1, #children do
		RoomPackager.setUpBehaviours(children[i])
	end
end

local function processPart(roomModel, part, parts)
	if part.Parent == roomModel then return end
	if part.Name == "End" and roomModel:FindFirstChild("End") then
		local endsModel = Instance.new("Model") --Used for branching the path
		endsModel.Name = "EndParts"
		endsModel.Parent = roomModel
		part.Parent = endsModel
		roomModel:FindFirstChild("End").Parent = endsModel
	elseif part.Name == "End" and roomModel:FindFirstChild("EndParts") then
		part.Parent = roomModel:FindFirstChild("EndParts")
	elseif part.Name == "End" then
		part.Parent = roomModel
	else
		local topLevelParent = closestParentToWorkspace(part, roomModel)
		if topLevelParent ~= nil then
			if topLevelParent:isA("BasePart") then
				local connectedParts = topLevelParent:GetConnectedParts(true)
				for _, connectedPart in pairs(connectedParts) do
					if connectedPart.Name == "End" then
						table.insert(parts, connectedPart)
					else
						local conTopLevelParent = closestParentToWorkspace(connectedPart, roomModel)
						if conTopLevelParent and conTopLevelParent ~= topLevelParent then
							conTopLevelParent.Parent = roomModel
						end
					end
				end
			end
			topLevelParent.Parent = roomModel
		end
	end
end

function RoomPackager:PackageRoom(roomBasePlate)
	local roomModel = Instance.new("Model")
	roomModel.Name = "Path"
	roomModel.Parent = game.ReplicatedStorage.PathModules

	local regions = self:RegionsFromBasePlate(roomBasePlate)
	
	for i = 1, #regions do
		--Repeatedly finds 200 parts in the region until none are left
		while true do
			local parts = game.Workspace:FindPartsInRegion3(regions[i], nil, 200)
			if #parts == 0 then
				break
			end
			for _, part in pairs(parts) do
				processPart(roomModel, part, parts)
			end
		end
	end

	--Set-up model for use in the path (parts for locating the path are made transparent)
	roomBasePlate.Transparency = 1
	roomBasePlate.Parent = roomModel
	roomModel:FindFirstChild("Start", true).Parent = roomModel
	roomModel:FindFirstChild("Start", true).Transparency = 1
	if roomModel:FindFirstChild("EndParts") then
		local ends = roomModel:FindFirstChild("EndParts"):GetChildren()
		for i = 1, #ends do
			ends[i].Transparency = 1
		end
	else
		roomModel.End.Transparency = 1
	end
	roomModel.PrimaryPart = roomBasePlate
	roomModel.Parent = self:CategoriseModel(roomModel)
	RoomPackager.setUpBehaviours(roomModel)
	return roomModel
end

return RoomPackager
