    001package net.minecraft.util;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.util.Random;
    006
    007public class MathHelper
    008{
    009    /**
    010     * A table of sin values computed from 0 (inclusive) to 2*pi (exclusive), with steps of 2*PI / 65536.
    011     */
    012    private static float[] SIN_TABLE = new float[65536];
    013
    014    /**
    015     * sin looked up in a table
    016     */
    017    public static final float sin(float par0)
    018    {
    019        return SIN_TABLE[(int)(par0 * 10430.378F) & 65535];
    020    }
    021
    022    /**
    023     * cos looked up in the sin table with the appropriate offset
    024     */
    025    public static final float cos(float par0)
    026    {
    027        return SIN_TABLE[(int)(par0 * 10430.378F + 16384.0F) & 65535];
    028    }
    029
    030    public static final float sqrt_float(float par0)
    031    {
    032        return (float)Math.sqrt((double)par0);
    033    }
    034
    035    public static final float sqrt_double(double par0)
    036    {
    037        return (float)Math.sqrt(par0);
    038    }
    039
    040    /**
    041     * Returns the greatest integer less than or equal to the float argument
    042     */
    043    public static int floor_float(float par0)
    044    {
    045        int i = (int)par0;
    046        return par0 < (float)i ? i - 1 : i;
    047    }
    048
    049    @SideOnly(Side.CLIENT)
    050
    051    /**
    052     * returns par0 cast as an int, and no greater than Integer.MAX_VALUE-1024
    053     */
    054    public static int truncateDoubleToInt(double par0)
    055    {
    056        return (int)(par0 + 1024.0D) - 1024;
    057    }
    058
    059    /**
    060     * Returns the greatest integer less than or equal to the double argument
    061     */
    062    public static int floor_double(double par0)
    063    {
    064        int i = (int)par0;
    065        return par0 < (double)i ? i - 1 : i;
    066    }
    067
    068    /**
    069     * Long version of floor_double
    070     */
    071    public static long floor_double_long(double par0)
    072    {
    073        long i = (long)par0;
    074        return par0 < (double)i ? i - 1L : i;
    075    }
    076
    077    public static float abs(float par0)
    078    {
    079        return par0 >= 0.0F ? par0 : -par0;
    080    }
    081
    082    public static int abs_int(int par0)
    083    {
    084        return par0 >= 0 ? par0 : -par0;
    085    }
    086
    087    public static int ceiling_float_int(float par0)
    088    {
    089        int i = (int)par0;
    090        return par0 > (float)i ? i + 1 : i;
    091    }
    092
    093    public static int ceiling_double_int(double par0)
    094    {
    095        int i = (int)par0;
    096        return par0 > (double)i ? i + 1 : i;
    097    }
    098
    099    /**
    100     * Returns the value of the first parameter, clamped to be within the lower and upper limits given by the second and
    101     * third parameters.
    102     */
    103    public static int clamp_int(int par0, int par1, int par2)
    104    {
    105        return par0 < par1 ? par1 : (par0 > par2 ? par2 : par0);
    106    }
    107
    108    @SideOnly(Side.CLIENT)
    109
    110    /**
    111     * Returns the value of the first parameter, clamped to be within the lower and upper limits given by the second and
    112     * third parameters
    113     */
    114    public static float clamp_float(float par0, float par1, float par2)
    115    {
    116        return par0 < par1 ? par1 : (par0 > par2 ? par2 : par0);
    117    }
    118
    119    /**
    120     * Maximum of the absolute value of two numbers.
    121     */
    122    public static double abs_max(double par0, double par2)
    123    {
    124        if (par0 < 0.0D)
    125        {
    126            par0 = -par0;
    127        }
    128
    129        if (par2 < 0.0D)
    130        {
    131            par2 = -par2;
    132        }
    133
    134        return par0 > par2 ? par0 : par2;
    135    }
    136
    137    @SideOnly(Side.CLIENT)
    138
    139    /**
    140     * Buckets an integer with specifed bucket sizes.  Args: i, bucketSize
    141     */
    142    public static int bucketInt(int par0, int par1)
    143    {
    144        return par0 < 0 ? -((-par0 - 1) / par1) - 1 : par0 / par1;
    145    }
    146
    147    @SideOnly(Side.CLIENT)
    148
    149    /**
    150     * Tests if a string is null or of length zero
    151     */
    152    public static boolean stringNullOrLengthZero(String par0Str)
    153    {
    154        return par0Str == null || par0Str.length() == 0;
    155    }
    156
    157    public static int getRandomIntegerInRange(Random par0Random, int par1, int par2)
    158    {
    159        return par1 >= par2 ? par1 : par0Random.nextInt(par2 - par1 + 1) + par1;
    160    }
    161
    162    public static double getRandomDoubleInRange(Random par0Random, double par1, double par3)
    163    {
    164        return par1 >= par3 ? par1 : par0Random.nextDouble() * (par3 - par1) + par1;
    165    }
    166
    167    public static double average(long[] par0ArrayOfLong)
    168    {
    169        long i = 0L;
    170        long[] along1 = par0ArrayOfLong;
    171        int j = par0ArrayOfLong.length;
    172
    173        for (int k = 0; k < j; ++k)
    174        {
    175            long l = along1[k];
    176            i += l;
    177        }
    178
    179        return (double)i / (double)par0ArrayOfLong.length;
    180    }
    181
    182    /**
    183     * the angle is reduced to an angle between -180 and +180 by mod, and a 360 check
    184     */
    185    public static float wrapAngleTo180_float(float par0)
    186    {
    187        par0 %= 360.0F;
    188
    189        if (par0 >= 180.0F)
    190        {
    191            par0 -= 360.0F;
    192        }
    193
    194        if (par0 < -180.0F)
    195        {
    196            par0 += 360.0F;
    197        }
    198
    199        return par0;
    200    }
    201
    202    /**
    203     * the angle is reduced to an angle between -180 and +180 by mod, and a 360 check
    204     */
    205    public static double wrapAngleTo180_double(double par0)
    206    {
    207        par0 %= 360.0D;
    208
    209        if (par0 >= 180.0D)
    210        {
    211            par0 -= 360.0D;
    212        }
    213
    214        if (par0 < -180.0D)
    215        {
    216            par0 += 360.0D;
    217        }
    218
    219        return par0;
    220    }
    221
    222    /**
    223     * parses the string as integer or returns the second parameter if it fails
    224     */
    225    public static int parseIntWithDefault(String par0Str, int par1)
    226    {
    227        int j = par1;
    228
    229        try
    230        {
    231            j = Integer.parseInt(par0Str);
    232        }
    233        catch (Throwable throwable)
    234        {
    235            ;
    236        }
    237
    238        return j;
    239    }
    240
    241    /**
    242     * parses the string as integer or returns the second parameter if it fails. this value is capped to par2
    243     */
    244    public static int parseIntWithDefaultAndMax(String par0Str, int par1, int par2)
    245    {
    246        int k = par1;
    247
    248        try
    249        {
    250            k = Integer.parseInt(par0Str);
    251        }
    252        catch (Throwable throwable)
    253        {
    254            ;
    255        }
    256
    257        if (k < par2)
    258        {
    259            k = par2;
    260        }
    261
    262        return k;
    263    }
    264
    265    /**
    266     * parses the string as double or returns the second parameter if it fails.
    267     */
    268    public static double parseDoubleWithDefault(String par0Str, double par1)
    269    {
    270        double d1 = par1;
    271
    272        try
    273        {
    274            d1 = Double.parseDouble(par0Str);
    275        }
    276        catch (Throwable throwable)
    277        {
    278            ;
    279        }
    280
    281        return d1;
    282    }
    283
    284    public static double func_82713_a(String par0Str, double par1, double par3)
    285    {
    286        double d2 = par1;
    287
    288        try
    289        {
    290            d2 = Double.parseDouble(par0Str);
    291        }
    292        catch (Throwable throwable)
    293        {
    294            ;
    295        }
    296
    297        if (d2 < par3)
    298        {
    299            d2 = par3;
    300        }
    301
    302        return d2;
    303    }
    304
    305    static
    306    {
    307        for (int i = 0; i < 65536; ++i)
    308        {
    309            SIN_TABLE[i] = (float)Math.sin((double)i * Math.PI * 2.0D / 65536.0D);
    310        }
    311    }
    312}