Room intersection prevention not working (RNG generation)

Hello, I am currently making a game that has a completely randomly generated map, in the style of dungeon generation using pre-built rooms that are attached together.

However, when I try to generate, sometimes room go inside each other as a result of 100% randomness. Up until now, it’s totally normal.

To counter this issue, I tried making it so that when the generation starts tilting too much to the right, I prevent it from generating any rooms that go any more to the right so that it’s impossible for it to intersect. This seemed fine at first until a couple generations where the problem was still present and rooms still generated inside each other, and now I am completely stuck.

Here is my code if you would like to help:

local currentroom = 1
local requiredrooms = 40

local r = Random.new()
local turningtoomuch = false


local rooms = workspace:WaitForChild("Rooms")
local map = workspace:WaitForChild("Map")
local roomstable = rooms:GetChildren()
local spawnroom = workspace:WaitForChild("Spawn")
local lastroom = spawnroom

task.wait(5)


repeat 
	currentroom += 1

	local newroom = roomstable[r:NextInteger(1, #roomstable)]:Clone()
	newroom.Parent = map
	newroom:PivotTo(lastroom.Nodes.Exit.CFrame)
	for i,v in pairs(newroom:GetChildren()) do
		if v.Name == "Door" then
			v.SurfaceGui.TextLabel.Text = currentroom
		end
	end

	lastroom = newroom

	-- Handling intersecting rooms
	if newroom.Nodes.Exit.Orientation == Vector3.new(0,-90,0) then
		turningtoomuch = true
		print("Turning too much (Right)")
		for i,v in pairs(roomstable) do
			if v.Name == "THall2" then -- Removing possibility of going right any further
				table.remove(roomstable, i)
			end
		end
	elseif newroom.Nodes.Exit.Orientation == Vector3.new(0,0,0) and turningtoomuch == true then
		turningtoomuch = false
		print("Going back") -- Inserting the rooms back into the table
		table.insert(roomstable, rooms.THall2) 
		table.insert(roomstable, rooms.THall3)
	elseif newroom.Nodes.Exit.Orientation == Vector3.new(0,90,0) then
		turningtoomuch = true
		print("Turning too much (Left)")
		for i,v in pairs(roomstable) do
			if v.Name == "THall3" then -- Removing possibility of going left any further
				table.remove(roomstable, i)
			end
		end
	end

	task.wait(.1)


until currentroom == requiredrooms

Any help is appreciated :smiley:

There are definitely multiple ways of going about dungeon-like generation. Without going into a ton of details right now, one simple (albeit potentially inefficient) solution would be to just do collision detection to see if the next space is clear or not. If it is then continue, put a wall or closed section if not. You can use spacial queries like Raycasting or GetPartBoundsInBox. Or perhaps you have a way to directly reference check for a specific section in that location.

1 Like

Although this was not really the fix I needed, I am still going to mark it as the answer as “GetPartsBoundsInBox” did help me on debugging certain things with room generation and killing the process/attempting fixes if intersecting rooms were detected.

Thank you for this resource! :+1:

1 Like