Corridor Generation issues

Hello! I’m making a map generator for my game. I’ve tried to generate corridors as in the sense of it placing tiles and drawing around them, like an outline. However, I’ve encountered many issues, such as these tiles going off the allocated space, or colliding with eachother. It doesn’t show it in the image, but it is happening.

This is a current output:


I would also like the origin (the red part) to be in the center and expand the tiles from there. The gray parts are blank tiles, and the outline on the grid will be removed later, as it’s just a border for the main level grid.

The corridors are supposed to be 1 tile wide, as in 8 studs wide, as that’s my tile size.

Current Code: (i know it’s bad :sob:)

function ConstructHallways()
    local blankTiles = {}
	
	for i, x in pairs(workspace:GetChildren()) do
		if x.Name == "Blank" then
			table.insert(blankTiles, x)
		end
	end
	
	for i = 1, 4 do
		local RandomTile = blankTiles[math.random(1, #blankTiles)]
		local SizeX = math.random(3, 9)
		local SizeZ = math.random(3, 9)
		
		local InitialX = 0
		local CurrentZ = 0
		local CurrentX = InitialX
		
		print(SizeX, SizeZ)
		
		for px = 1, SizeX do
			for pz = 1, SizeZ do
				local NewTile = Tiles:WaitForChild("Floor"):Clone()
				NewTile.Parent = workspace
				
				if px == 1 and pz == 1 then
					NewTile.Texture2.ImageLabel.ImageColor3 = Color3.new(1, 0, 0)
				end
				
				NewTile.Position = Vector3.new(CurrentX + RandomTile.Position.X, 0, CurrentZ + RandomTile.Position.Z)
				
				for p, n in pairs(workspace:GetChildren()) do
					if n:IsA('Part') and (n.Position == NewTile.Position and n ~= NewTile) then
						table.remove(blankTiles, table.find(blankTiles, n))
						n:Destroy()
					end
				end
				
				CurrentX += 8
			end
			
			CurrentZ += 8
			CurrentX = InitialX
		end
	end
end
1 Like