    001    package net.minecraft.src;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import java.util.Random;
    006
    007    public 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 var1 = (int)par0;
    046            return par0 < (float)var1 ? var1 - 1 : var1;
    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 var2 = (int)par0;
    065            return par0 < (double)var2 ? var2 - 1 : var2;
    066        }
    067
    068        /**
    069         * Long version of floor_double
    070         */
    071        public static long floor_double_long(double par0)
    072        {
    073            long var2 = (long)par0;
    074            return par0 < (double)var2 ? var2 - 1L : var2;
    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 var1 = (int)par0;
    090            return par0 > (float)var1 ? var1 + 1 : var1;
    091        }
    092
    093        public static int ceiling_double_int(double par0)
    094        {
    095            int var2 = (int)par0;
    096            return par0 > (double)var2 ? var2 + 1 : var2;
    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 func_82716_a(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 var1 = 0L;
    170            long[] var3 = par0ArrayOfLong;
    171            int var4 = par0ArrayOfLong.length;
    172
    173            for (int var5 = 0; var5 < var4; ++var5)
    174            {
    175                long var6 = var3[var5];
    176                var1 += var6;
    177            }
    178
    179            return (double)var1 / (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        public static int func_82715_a(String par0Str, int par1)
    223        {
    224            int var2 = par1;
    225
    226            try
    227            {
    228                var2 = Integer.parseInt(par0Str);
    229            }
    230            catch (Throwable var4)
    231            {
    232                ;
    233            }
    234
    235            return var2;
    236        }
    237
    238        public static int func_82714_a(String par0Str, int par1, int par2)
    239        {
    240            int var3 = par1;
    241
    242            try
    243            {
    244                var3 = Integer.parseInt(par0Str);
    245            }
    246            catch (Throwable var5)
    247            {
    248                ;
    249            }
    250
    251            if (var3 < par2)
    252            {
    253                var3 = par2;
    254            }
    255
    256            return var3;
    257        }
    258
    259        public static double func_82712_a(String par0Str, double par1)
    260        {
    261            double var3 = par1;
    262
    263            try
    264            {
    265                var3 = Double.parseDouble(par0Str);
    266            }
    267            catch (Throwable var6)
    268            {
    269                ;
    270            }
    271
    272            return var3;
    273        }
    274
    275        public static double func_82713_a(String par0Str, double par1, double par3)
    276        {
    277            double var5 = par1;
    278
    279            try
    280            {
    281                var5 = Double.parseDouble(par0Str);
    282            }
    283            catch (Throwable var8)
    284            {
    285                ;
    286            }
    287
    288            if (var5 < par3)
    289            {
    290                var5 = par3;
    291            }
    292
    293            return var5;
    294        }
    295
    296        static
    297        {
    298            for (int var0 = 0; var0 < 65536; ++var0)
    299            {
    300                SIN_TABLE[var0] = (float)Math.sin((double)var0 * Math.PI * 2.0D / 65536.0D);
    301            }
    302        }
    303    }