function nextMove (front, left, right, back)

	-- If we have more distance than the thinking distance,
	-- carry on as we are
	
	turn = 0
			
	if status == cFLYING then
	   directionChange = 0

		-- if the thinking distance has been reached, start considering other directions.
		-- This is the point at which the character knows he is going to hit a wall soon
		if front < thinkDistance then
	       status = cLOOKING
	   	end

	end
		
	if status == cLOOKING then

			if left > validDistance then
				directionChange = - 1
				status = cTURNING
			else
				if right > validDistance then
					directionChange = 1
					status = cTURNING
				end
			end
	end
	
	if status == cTURNING then
		turn = directionChange * 90
		status = cFLYING
	end
	

	return speed, turn
end