private double getBestAngleOffset(final GunWave wave) {
		if (state.robotGunHeat / State.coolingRate > 4)
			return 0;
		
		double perfect = GunPerfectTargeting.getPerfectAim(wave, state);
		if(!Double.isNaN(perfect)) {
			bot.out.println("Perfect Aim: " + perfect);
			return perfect;
		}

		int size = (int) Tools.limit(1, tree.size() / 14, 80);
		final List<Entry<GunFormula>> list = tree.nearestNeighbor(wave.data.getArray(), size, false);
		size = list.size();
		double bestAngle = 0;
		double bestDensity = 0;
		final double bandwidth = 36 / wave.distance(state.targetPosition);
		final double[] angles = new double[size];
		final double[] weights = new double[size];
		for (int i = 0; i < size; ++i) {
			angles[i] = Utils.normalRelativeAngle(list.get(i).value.guessfactor * wave.escapeAngle);
			weights[i] = 1.0 / Math.sqrt(list.get(i).distance);
		}
		for (int a = 0; a < size; ++a) {
			double density = 0;
			for (int b = 0; b < size; ++b) {
				if (a == b)
					continue;
				final double ux = (angles[a] - angles[b]) / bandwidth;
				density += Math.exp(-0.5 * ux * ux) * weights[b];
			}
			if (density > bestDensity) {
				bestAngle = angles[a];
				bestDensity = density;
			}
		}
		return bestAngle;
	}