MazeDescriptor = {

}

function MazeDescriptor.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 = math.random( HalfWidth  )*2
	local StartY =
	math.random( HalfHeight )*2

	Map[ StartX ][ StartY ] = 0

	local CurrX = StartX
	local CurrY = StartY

	local FarEnd   = { { 0, 0 }, { 0, 0 } }
	local FarCount = { 1, 1 }

	local LastEnd = false

	while true do
		local WallCount = 1
		local WallX = { 0, 0, 0, 0 }
		local WallY = { 0, 0, 0, 0 }

		if CurrX > 2 and Map[ CurrX - 2 ][ CurrY ] == 1 then WallX[ WallCount ] = CurrX - 2; WallY[ WallCount ] = CurrY; WallCount = WallCount + 1 end
		if CurrY > 2 and Map[ CurrX ][ CurrY - 2 ] == 1 then WallY[ WallCount ] = CurrY - 2; WallX[ WallCount ] = CurrX; WallCount = WallCount + 1 end

		if CurrX < Width  - 2 and Map[ CurrX + 2 ][ CurrY ] == 1 then WallX[ WallCount ] = CurrX + 2; WallY[ WallCount ] = CurrY; WallCount = WallCount + 1 end
		if CurrY < Height - 2 and Map[ CurrX ][ CurrY + 2 ] == 1 then WallY[ WallCount ] = CurrY + 2; WallX[ WallCount ] = CurrX; WallCount = WallCount + 1 end

		if WallCount == 1 then
			if StackIndex == 1 then
				local Result = ""

				for Y = 1, Height do
					for X = 1, Width do
						if X == StartX and Y == StartY then
							Result = Result .. "^"
						elseif ( X == FarEnd[ 1 ][ 1 ] and Y == FarEnd[ 1 ][ 2 ] ) or ( X == FarEnd[ 2 ][ 1 ] and Y == FarEnd[ 2 ][ 2 ] ) 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 )
			else
				if not LastEnd then
					LastEnd = true

					if FarCount[ 1 ] < StackIndex then
						if FarCount[ 2 ] < StackIndex then
							FarEnd[ 1 ][ 1 ], FarEnd[ 1 ][ 2 ], FarCount[ 1 ] = FarEnd[ 2 ][ 1 ], FarEnd[ 2 ][ 2 ], FarCount[ 2 ]
							FarEnd[ 2 ][ 1 ], FarEnd[ 2 ][ 2 ], FarCount[ 2 ] = CurrX, CurrY, StackIndex
						else
							FarEnd[ 1 ][ 1 ], FarEnd[ 1 ][ 2 ], FarCount[ 1 ] = CurrX, CurrY, StackIndex
						end
					end
				end

				StackIndex = StackIndex - 1

				CurrX = StackX[ StackIndex ]
				CurrY = StackY[ StackIndex ]
			end
		else
			LastEnd = false

			local Passage = math.random( WallCount - 1 )

			Map[ WallX[ Passage ] ][ WallY[ Passage ] ] = 0
			Map[ ( CurrX + WallX[ Passage ] )/2 ][ ( CurrY + WallY[ Passage ] )/2 ] = 0

			CurrX = WallX[ Passage ]
			CurrY = WallY[ Passage ]

			StackX[ StackIndex ] = CurrX
			StackY[ StackIndex ] = CurrY

			StackIndex = StackIndex + 1

			if math.random( 100 ) < Max( 8*Factor, 20 ) then CreateObject( SelectMonster( Depth ), CurrX - 1, CurrY - 1 ) end
		end
	end
end
