public double ai(int x, int y, int vx, int vy, int l) {
		double[] e = new double[5];

		double d = Math.atan2(-(cy - y), (cx - x)) + Math.PI; // Calculate the
																// distance

		// The arc-distance values range from 0 to 2 PI. To solve border-cases
		// in the from 1.5 PI to 0.5 PI, checking is needed.
		if ((d - distance) > Math.PI)
			d -= 2 * Math.PI;
		if ((d - distance) < -Math.PI)
			d += 2 * Math.PI;

		if (l > 0) {
			// Check borders
			if (circ.terrain(x, y) <= 0) {
				vx = 0;
				vy = 0;
				if (!grass)
					l -= 2; // Penalty to get quicker results.
			}

			// If we're not done yet, check all possible routes.
			if (l > 0) {
				e[0] = ai(x + vx - 1, y + vy, vx - 1, vy, l - 1);
				e[1] = ai(x + vx + 1, y + vy, vx + 1, vy, l - 1);
				e[2] = ai(x + vx, y + vy - 1, vx, vy - 1, l - 1);
				e[3] = ai(x + vx, y + vy + 1, vx, vy + 1, l - 1);
				e[4] = ai(x + vx, y + vy, vx, vy, l - 1);

				// Next get the largest distance
				for (i = 0; i < 5; i++) {
					if (e[i] > d) {
						d = e[i];
						j = i;
					}
				}
			}
		}

		if (l == level)
			return j; // If this was the top-thread, return the best route
		return d; // Else return the distance
	}