function compareWhitelist(dir)
	if dir == "up" then
		if not turtle.detectUp() then
			return "empty"
		end
	elseif dir == "down" then
		if not turtle.detectDown() then
			return "empty"
		end
	elseif dir == "forward" then
		if not turtle.detect() then
			return "empty"
		end
	end
	local i
	for i = whitelistSlotStart,whitelistSlotEnd do
		if turtle.getItemCount(i) > 0 then
			turtle.select(i)
			if dir == "up" and turtle.compareUp() then
				return "good"
			elseif dir == "down" and turtle.compareDown() then
				return "good"
			elseif dir == "forward" and turtle.compare() then
				return "good"
			end
		end
	end
	return "bad"
end

function isForwardInVeinRange()
	local x = currentX
	local z = currentZ
	if currentDirection == DIR_POS_X then x = x + 1
	elseif currentDirection == DIR_NEG_X then x = x - 1
	elseif currentDirection == DIR_POS_Z then z = z + 1
	elseif currentDirection == DIR_NEG_Z then z = z - 1
	end
	if maxVeinX > 0 and math.abs(x) > maxVeinX then
		return false
	end
	if maxVeinZ > 0 and math.abs(z) > maxVeinZ then
		return false
	end
	return true
end

function checkMineVein()
	if math.abs(currentX) < originNoVeinRadius and math.abs(currentY) < originNoVeinRadius and math.abs(currentZ) < originNoVeinRadius then
		return "good"
	end
	turnLeft()
	if isForwardInVeinRange() and compareWhitelist("forward") == "good" and turtle.dig() and goForward() then
		checkMineVein()
		goBack()
	end
	turnRight()
	if isForwardInVeinRange() and compareWhitelist("forward") == "good" and turtle.dig() and goForward() then
		checkMineVein()
		goBack()
	end
	if compareWhitelist("up") == "good" and turtle.digUp() and goUp() then
		checkMineVein()
		goDown()
	end
	if compareWhitelist("down") == "good" and turtle.digDown() and goDown() then
		checkMineVein()
		goUp()
	end
	turnRight()
	if isForwardInVeinRange() and compareWhitelist("forward") == "good" and turtle.dig() and goForward() then
		checkMineVein()
		goBack()
	end
	turnLeft()
end

function goBack()
	checkFuel(1)
	if turtle.back() then
		if currentDirection == DIR_POS_X then
			currentX = currentX - 1
		elseif currentDirection == DIR_POS_Z then
			currentZ = currentZ - 1
		elseif currentDirection == DIR_NEG_X then
			currentX = currentX + 1
		elseif currentDirection == DIR_NEG_Z then
			currentZ = currentZ + 1
		end
		return true
	else
		return false
	end
end

function goUp()
	if turtle.up() then
		currentY = currentY + 1
		return true
	else
		return false
	end
end

function goDown()
	if turtle.down() then
		currentY = currentY - 1
		return true
	else
		return false
	end
end

function turnRight()
	turtle.turnRight()
	if currentDirection == DIR_POS_X then
		currentDirection = DIR_POS_Z
	elseif currentDirection == DIR_POS_Z then
		currentDirection = DIR_NEG_X
	elseif currentDirection == DIR_NEG_X then
		currentDirection = DIR_NEG_Z
	elseif currentDirection == DIR_NEG_Z then
		currentDirection = DIR_POS_X
	end
end

function turnLeft()
	turtle.turnLeft()
	if currentDirection == DIR_POS_X then
		currentDirection = DIR_NEG_Z
	elseif currentDirection == DIR_NEG_Z then
		currentDirection = DIR_NEG_X
	elseif currentDirection == DIR_NEG_X then
		currentDirection = DIR_POS_Z
	elseif currentDirection == DIR_POS_Z then
		currentDirection = DIR_POS_X
	end
end
