TunnelDescriptor = {

}

function TunnelDescriptor.CreateDungeon( Width, Height, Depth )
	local Map = {}

	local Factor = 1

	Width  = Width *Factor
	Height = Height*Factor

	PrepareDungeon( Width, Height )

	local HalfWidth  =  Width/2 - 1
	local HalfHeight = Height/2 - 1

	for X = 1, Width do
		Map[ X ] = {}

		for Y = 1, Height do
			Map[ X ][ Y ] = 1
		end
	end

	local StackX = {}
	local StackY = {}
	local StackIndex = 1

	local StartX = HalfWidth
	local StartY = HalfHeight

	Map[ StartX + 0 ][ StartY + 0 ] = 0
	Map[ StartX - 1 ][ StartY + 0 ] = 0
	Map[ StartX + 0 ][ StartY - 1 ] = 0
	Map[ StartX + 1 ][ StartY + 0 ] = 0
	Map[ StartX + 0 ][ StartY + 1 ] = 0

	local Cell1 = { X = StartX - 1, Y = StartY, Wall = 1 }
	local Cell2 = { X = StartX, Y = StartY - 1, Wall = 2, Prev = Cell1 }
	local Cell3 = { X = StartX + 1, Y = StartY, Wall = 3, Prev = Cell2 }
	local Cell4 = { X = StartX, Y = StartY + 1, Wall = 4, Prev = Cell3 }

	Cell1.Prev = Cell4
	Cell1.Succ = Cell2
	Cell2.Succ = Cell3
	Cell3.Succ = Cell4
	Cell4.Succ = Cell1

	local TopCell = Cell2

	for I = 1, 10000 do
		local WallCount = 1
		local WallX = { 0, 0, 0, 0 }
		local WallY = { 0, 0, 0, 0 }

		if TopCell.X > 2 and Map[ TopCell.X - 1 ][ TopCell.Y ] == 1 then WallX[ WallCount ] = TopCell.X - 1; WallY[ WallCount ] = TopCell.Y; WallCount = WallCount + 1 end
		if TopCell.Y > 2 and Map[ TopCell.X ][ TopCell.Y - 1 ] == 1 then WallY[ WallCount ] = TopCell.Y - 1; WallX[ WallCount ] = TopCell.X; WallCount = WallCount + 1 end

		if TopCell.X < Width  - 2 and Map[ TopCell.X + 1 ][ TopCell.Y ] == 1 then WallX[ WallCount ] = TopCell.X + 1; WallY[ WallCount ] = TopCell.Y; WallCount = WallCount + 1 end
		if TopCell.Y < Height - 2 and Map[ TopCell.X ][ TopCell.Y + 1 ] == 1 then WallY[ WallCount ] = TopCell.Y + 1; WallX[ WallCount ] = TopCell.X; WallCount = WallCount + 1 end

		if ( math.random( 60 ) > 20 or TopCell.Succ == TopCell ) and WallCount > 1 then
			local W = math.random( WallCount - 1 )

			Map[ WallX[ W ] ][ WallY[ W ] ] = 0

			local Cell = { X = WallX[ W ], Y = WallY[ W ], Wall = W, Succ = TopCell.Succ, Prev = TopCell }

			if math.random( 10 ) == 1 then
				TopCell.Succ.Prev = Cell
				TopCell.Succ = Cell
				TopCell = Cell
			else
				TopCell.Succ.Prev = Cell
				TopCell.Prev.Succ = Cell
				TopCell = Cell
			end
		else
			if TopCell.Succ == TopCell then
				Map[ TopCell.X ][ TopCell.Y ] = 3

				do break end
			else
				TopCell.Prev.Succ = TopCell.Succ
				TopCell.Succ.Prev = TopCell.Prev

				TopCell = TopCell.Succ
			end
		end

		if I == 10000 then Map[ TopCell.X ][ TopCell.Y ] = 3 end
	end

	local Result = ""

	for Y = 1, Height, 3 do
		for X = 1, Width, 3 do
			if Map[ X ][ Y ] == 0 then CreateObject( SelectMonster( Depth ), X - 1, Y - 1 ) end
		end
	end

	for Y = 1, Height do
		for X = 1, Width do
			if X == StartX and Y == StartY then
				Result = Result .. "^"
			elseif Map[ X ][ Y ] == 3 then
				Result = Result .. "v"
			elseif Map[ X ][ Y ] == 0 then
				Result = Result .. "."
			else
				Result = Result .. "#"
			end
		end

		Result = Result .. "\n"
	end

	return string.sub( Result, 1, string.len( Result ) - 1 )
end
