function init ()

	-- Initialise a few parameters.
	-- Variables default to global unless otherwise specified
	-- So we can create and initialise our variables at the same time
	-- In LUA, variables do not have an explicit type either

	-- Statuses
	cFLYING = 1
	cLOOKING = 2
	cTURNING = 3
	status = cFLYING
	
	-- The minimum distance we will allow before the character is stopped
	minDistance = 20
	
	-- The Thinking distance; the point at which we start considering a new direction
	-- We have also defined the distance considered to be a viable option.
	-- When the character needs to make a decision, it will use this value to decide
	-- whether turning in this direction gives it a long enough path to be viable.
	thinkDistance = 50
	validDistance = thinkDistance * 2
	
	-- Travelling speed; the normal speed of the character
	speed = 1.0
	
	-- Turning Speed; the number of degrees the character will turn in one cycle
	turnSpeed = 3
	
	-- Total Turn keeps track of an incremental turn around corners
	totalTurn = 0
	
	-- When there is more than one option, the character should pick one of the options at random
	-- To do this, we need a random function.  As in DB Pro, we need to randomise the
	-- random number first.  Unity includes the maths and OS libraries to assist us
	-- The following line is the exact equivalent of RANDOMIZE(TIMER())
	math.randomseed(os.time())

end

 
function nextMove (front, left, right, back)

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

		-- Check each side to ensure we aren't wandering into a wall
		if left < minDistance then
			directionChange = directionChange + 0.2
		end
		if right < minDistance then
			directionChange = directionChange - 0.2
		end

		-- 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

		-- firstly, count the options
		local count = 0
		if left > validDistance then count = count + 1 end
		-- if front > validDistance then count = count + 1 end
		if right > validDistance then count = count + 1 end

		-- Change direction according to the number of options
		-- 1 option
		if count == 1 then
			if left > validDistance then
				directionChange = - 1
				status = cTURNING
			else
				if right > validDistance then
					directionChange = 1
					status = cTURNING
				end
			end
		end

		
 
-- more than one option
		-- get a random direction and apply it
		if count > 1 then
		   local checks = 0
		   local option = math.random(count)
		   if left > validDistance then
		      checks = checks + 1
		      if checks == option then
			  	directionChange = -1
			  	status = cTURNING
			  end
		   end
		   if front > validDistance then
		      checks = checks + 1
		      if checks == option then directionChange = 0 end
		   end
		   if right > validDistance then
		      checks = checks + 1
		      if checks == option then
			  	directionChange = 1
			  	status = cTURNING
			  end
		   end
		end

		-- no options! Dead End
		-- Also, if minimum distance reached
		-- Make turn faster to get out without hitting wall
		if (count == 0) or (front < minDistance) then
			directionChange = 3
			status = cTURNING
		end
			
		
	end
	
	if status == cTURNING then
		turn = directionChange * turnSpeed
		totalTurn = totalTurn + turn
		
		-- at end of full turn, adjust to exactly 90 degrees
		-- set status to flying, and reset turn
		if totalTurn >= 90 then
			--turn = turn - (totalTurn - 90)
			status = cFLYING
			totalTurn = 0
		end
		if totalTurn <= -90 then
			--turn = turn + (totalTurn - 90)
			status = cFLYING
			totalTurn = 0
		end
	end
	

	return speed, turn
end