How does procedural generation work?

i want to make random room generation. i have a basic idea of how it works, but i have no idea how i would go about arranging the pieces. i have it set up like so, and i would just like a couple of pointers.

image

-- workspace.room_test:PivotTo(workspace.Part.CFrame)
local collection_service =  game:GetService("CollectionService")

local function generate_piece(piece_type)
	local folder = script.pieces[piece_type]
	local pieces = folder:GetChildren()
	local selected_piece = pieces[math.random(1, #pieces)]
	local clone = selected_piece:Clone()
	
	local end_folder = clone:FindFirstChild("end_pieces")
	local end_pieces = end_folder:GetChildren()
	
	for _, end_piece in ipairs(end_pieces) do
		-- how can i arrange them seamlessly
	end
	collection_service:AddTag(clone, tostring(piece_type))	
end

local room_criteria = {
	{room_type = "hall", count = 1},
	{room_type = "labyrinth", count = math.random(0, 1)},
	{room_type = "flesh & bones", count = 1},
	{room_type = "puzzle", count = math.random(0, 1)},
	{room_type = "exit", count = 1},
	{room_type = "add-ons", count = 1}
}

-- generate room based on criteria
local function generate_room()
	for _, criteria in ipairs(room_criteria) do
		for i = 1, criteria.count do
			generate_piece(criteria.room_type)
		end
	end
end

-- example function just to test room generation
while task.wait(10) do
	generate_room()

	task.delay(7, function()
		workspace.generated_room:ClearAllChildren()
	end)
end
1 Like

if you want to keep it basic

set the seed to os.clock (if no seed given) using math.randomseed
or if the seed is given use that

and just use math.random and do all the possibilities of how the room can be using the random numbers (based on the seed)

if the seed is same math.random will produce the same numbers

1 Like

If you mean setting the position and orientation of the pieces, the method I use is to build the models with a specific ‘placement part’ and set this as the models primary part, and also have ‘connector parts’ which is where subsequent rooms would pivotTo during generation.

If you have a linear layout (ie players have only one path option when moving through the rooms) you just need to check turns don’t double back into a previously generated room, and pick a different option if they do.

It gets more complex if you want dendritic layouts with multiple paths, loops, level changes, and for all this to persist throughout the game so players can double back to the start.

1 Like