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