function RoomPackager.CategoriseModel(G,o)if o:FindFirstChild("\069\110\100\080\097\114\116\115")then return game.ReplicatedStorage.PathModules.Branch elseif o.Start.Position.Y<o.End.Position.Y-5 then return game.ReplicatedStorage.PathModules.GoingUp elseif o.Start.Position.Y>o.End.Position.Y+5 then return game.ReplicatedStorage.PathModules.GoingDown else return game.ReplicatedStorage.PathModules.SameHeight end end local function o(G,m)local O=G:GetChildren()for G=1,#O,1 do if O[G]:isA("\066\097\115\101\080\097\114\116")then(m:Clone()).Parent=O[G]else o(O[G],m)end end end RoomPackager.setUpBehaviours=function(G)if G:FindFirstChild("\066\101\104\097\118\105\111\117\114\115")then o(G,G.Behaviours)return end local m=G:GetChildren()for o=1,#m,1 do RoomPackager.setUpBehaviours(m[o])end end local function G(o,G,m)if G.Parent==o then return end if G.Name=="\069\110\100"and o:FindFirstChild("\069\110\100")then local m=Instance.new("\077\111\100\101\108")m.Name="\069\110\100\080\097\114\116\115"m.Parent=o G.Parent=m;(o:FindFirstChild("\069\110\100")).Parent=m elseif G.Name=="\069\110\100"and o:FindFirstChild("\069\110\100\080\097\114\116\115")then G.Parent=o:FindFirstChild("\069\110\100\080\097\114\116\115")elseif G.Name=="\069\110\100"then G.Parent=o else local O=closestParentToWorkspace(G,o)if O~=nil then if O:isA("\066\097\115\101\080\097\114\116")then local G=O:GetConnectedParts(true)for G,W in pairs(G)do if W.Name=="\069\110\100"then table.insert(m,W)else local G=closestParentToWorkspace(W,o)if G and G~=O then G.Parent=o end end end end O.Parent=o end end end function RoomPackager.PackageRoom(m,o)local O=Instance.new("\077\111\100\101\108")O.Name="\080\097\116\104"O.Parent=game.ReplicatedStorage.PathModules local W=m:RegionsFromBasePlate(o)for o=1,#W,1 do while true do local m=game.Workspace:FindPartsInRegion3(W[o],nil,100)if#m==0 then break end for o,W in pairs(m)do G(O,W,m)end end end o.Transparency=1 o.Parent=O;(O:FindFirstChild("\083\116\097\114\116",true)).Parent=O;(O:FindFirstChild("\083\116\097\114\116",true)).Transparency=1 if O:FindFirstChild("\069\110\100\080\097\114\116\115")then local o=(O:FindFirstChild("\069\110\100\080\097\114\116\115")):GetChildren()for G=1,#o,1 do o[G].Transparency=1 end else O.End.Transparency=1 end O.PrimaryPart=o O.Parent=m:CategoriseModel(O)RoomPackager.setUpBehaviours(O)return O end return RoomPackager
--Categorises a model based on the location of the start and end points of the path in the model
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 100 parts in the region until none are left
		while true do
			local parts = game.Workspace:FindPartsInRegion3(regions[i], nil, 100)
			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
