Connecting dead end hallways in a map generator?

I’m working on a randomly generated facility, and I already have connecting rooms in place, however you may notice that they never really “Connect” with each other, often having unintentional dead end rooms. They might sometimes connect by chance but it’s unlikely. I already know how to make sure that they don’t collide with each other, however I always end up with dead ends in rooms that aren’t supposed to have any.

I checked out the Dungeonify module and it seems like it has a similar problem, where the map is one long curvy line that doesn’t connect to itself.

local function connectTwoPoints(point1, point2)
	return (point1.WorldCFrame * CFrame.Angles(0,math.pi,0)) * point2.CFrame:Inverse()
end

function Layout.new(maxSegments)
	local self = setmetatable({}, Layout)
	
	self.maxSegments = maxSegments or DEFAULT_MAX_SEGMENTS
	self.segments = {}
	self.connectedConnectors = {}
	self.previousSegment = nil
	
	return self
end

function Layout:Generate()
	
	local startingSegment = self:GenerateSegment()
	self:MoveSegment(startingSegment, STARTING_POSITION)
	self.previousSegment = startingSegment
	
	repeat
		for _, segment in pairs(WORKSPACE_SEGMENTS:GetChildren()) do
			if #self.segments >= self.maxSegments then return end
			for _, connector in pairs(segment.Base:GetChildren()) do
				if connector:GetAttribute("Connected") == true then continue end
				local newSegment = self:GenerateSegment()
				local newConnector = newSegment.Base:FindFirstChild("Connector")
				self:MoveSegment(newSegment, connectTwoPoints(connector, newConnector))
				connector:SetAttribute("Connected", true)
				newConnector:SetAttribute("Connected", true)
				task.wait(1)
			end
		end
	until #self.segments >= self.maxSegments
	
	print("Done")
end

function Layout:GenerateSegment()
	math.randomseed(os.clock())
	local randomNumber = Random.new(tick()):NextInteger(1, #SEGMENTS:GetChildren())
	local newSegment = SEGMENTS:GetChildren()[randomNumber]:Clone()
	newSegment.Parent = WORKSPACE_SEGMENTS
	table.insert(self.segments, newSegment)
	return newSegment
end

function Layout:MoveSegment(segment, position)
	segment:SetPrimaryPartCFrame(position)
end



1 Like

There’s countless ways you could do it. One option is to check every time that you place a tile if there are any tiles in the adjacent spots, and then having some chance of replacing the placed tile and the adjacent tile with tiles that actually connect the two.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.