// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.server.npc.util;

import com.hypixel.hytale.math.util.TrigMathUtil;
import javax.annotation.Nonnull;

public class AimingHelper
{
    public static final int MIN_GRAVITY_FOR_PARABOLA = 3;
    
    public static double ensurePossibleThrowSpeed(final double distance, final double y, final double gravity, final double throwSpeed) {
        final double x2 = distance * distance;
        if (x2 >= 1.0000000000000002E-10) {
            final double c = (Math.sqrt(y * y + x2) - y) / x2;
            final double minThrowSpeed = Math.sqrt(gravity / c);
            return Math.max(minThrowSpeed, throwSpeed);
        }
        final double t = y * gravity;
        if (t <= 0.0) {
            return throwSpeed;
        }
        final double minThrowSpeed = Math.sqrt(2.0 * t);
        return Math.max(minThrowSpeed, throwSpeed);
    }
    
    public static boolean computePitch(final double distance, final double height, final double velocity, final double gravity, @Nonnull final float[] resultingPitch) {
        if (distance * distance < 1.0000000000000002E-10) {
            final double k = height * gravity;
            if (k <= 0.0) {
                resultingPitch[0] = ((height > 0.0) ? 1.5707964f : -1.5707964f);
                resultingPitch[1] = -resultingPitch[0];
                return true;
            }
            final double peak = 0.5 * velocity * velocity / gravity;
            float pitch;
            if (height > 0.0) {
                if (peak < height - 1.0E-5) {
                    return false;
                }
                pitch = 1.5707964f;
            }
            else {
                if (peak > height + 1.0E-5) {
                    return false;
                }
                pitch = -1.5707964f;
            }
            resultingPitch[0] = (resultingPitch[1] = pitch);
            return true;
        }
        else if (gravity < 3.0) {
            if (height == 0.0 && distance == 0.0) {
                return false;
            }
            resultingPitch[0] = (resultingPitch[1] = TrigMathUtil.atan2(height, distance));
            return true;
        }
        else {
            if (resultingPitch.length != 2) {
                throw new IllegalArgumentException("computePitch requires a result array of size 2 for storing the resulting pitch");
            }
            final double c = gravity / (velocity * velocity);
            final double cx = c * distance - 1.0E-5;
            final double cy = c * height - 1.0E-5;
            double i = 1.0 - cx * cx - 2.0 * cy;
            if (i < 0.0) {
                return false;
            }
            i = Math.sqrt(i);
            resultingPitch[0] = TrigMathUtil.atan((1.0 - i) / cx);
            resultingPitch[1] = TrigMathUtil.atan((1.0 + i) / cx);
            return true;
        }
    }
}
