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    /**
083     * Returns the unsigned value of an int.
084     */
085    public static int abs_int(int par0)
086    {
087        return par0 >= 0 ? par0 : -par0;
088    }
089
090    public static int ceiling_float_int(float par0)
091    {
092        int i = (int)par0;
093        return par0 > (float)i ? i + 1 : i;
094    }
095
096    public static int ceiling_double_int(double par0)
097    {
098        int i = (int)par0;
099        return par0 > (double)i ? i + 1 : i;
100    }
101
102    /**
103     * Returns the value of the first parameter, clamped to be within the lower and upper limits given by the second and
104     * third parameters.
105     */
106    public static int clamp_int(int par0, int par1, int par2)
107    {
108        return par0 < par1 ? par1 : (par0 > par2 ? par2 : par0);
109    }
110
111    @SideOnly(Side.CLIENT)
112
113    /**
114     * Returns the value of the first parameter, clamped to be within the lower and upper limits given by the second and
115     * third parameters
116     */
117    public static float clamp_float(float par0, float par1, float par2)
118    {
119        return par0 < par1 ? par1 : (par0 > par2 ? par2 : par0);
120    }
121
122    /**
123     * Maximum of the absolute value of two numbers.
124     */
125    public static double abs_max(double par0, double par2)
126    {
127        if (par0 < 0.0D)
128        {
129            par0 = -par0;
130        }
131
132        if (par2 < 0.0D)
133        {
134            par2 = -par2;
135        }
136
137        return par0 > par2 ? par0 : par2;
138    }
139
140    @SideOnly(Side.CLIENT)
141
142    /**
143     * Buckets an integer with specifed bucket sizes.  Args: i, bucketSize
144     */
145    public static int bucketInt(int par0, int par1)
146    {
147        return par0 < 0 ? -((-par0 - 1) / par1) - 1 : par0 / par1;
148    }
149
150    @SideOnly(Side.CLIENT)
151
152    /**
153     * Tests if a string is null or of length zero
154     */
155    public static boolean stringNullOrLengthZero(String par0Str)
156    {
157        return par0Str == null || par0Str.length() == 0;
158    }
159
160    public static int getRandomIntegerInRange(Random par0Random, int par1, int par2)
161    {
162        return par1 >= par2 ? par1 : par0Random.nextInt(par2 - par1 + 1) + par1;
163    }
164
165    public static double getRandomDoubleInRange(Random par0Random, double par1, double par3)
166    {
167        return par1 >= par3 ? par1 : par0Random.nextDouble() * (par3 - par1) + par1;
168    }
169
170    public static double average(long[] par0ArrayOfLong)
171    {
172        long i = 0L;
173        long[] along1 = par0ArrayOfLong;
174        int j = par0ArrayOfLong.length;
175
176        for (int k = 0; k < j; ++k)
177        {
178            long l = along1[k];
179            i += l;
180        }
181
182        return (double)i / (double)par0ArrayOfLong.length;
183    }
184
185    /**
186     * the angle is reduced to an angle between -180 and +180 by mod, and a 360 check
187     */
188    public static float wrapAngleTo180_float(float par0)
189    {
190        par0 %= 360.0F;
191
192        if (par0 >= 180.0F)
193        {
194            par0 -= 360.0F;
195        }
196
197        if (par0 < -180.0F)
198        {
199            par0 += 360.0F;
200        }
201
202        return par0;
203    }
204
205    /**
206     * the angle is reduced to an angle between -180 and +180 by mod, and a 360 check
207     */
208    public static double wrapAngleTo180_double(double par0)
209    {
210        par0 %= 360.0D;
211
212        if (par0 >= 180.0D)
213        {
214            par0 -= 360.0D;
215        }
216
217        if (par0 < -180.0D)
218        {
219            par0 += 360.0D;
220        }
221
222        return par0;
223    }
224
225    /**
226     * parses the string as integer or returns the second parameter if it fails
227     */
228    public static int parseIntWithDefault(String par0Str, int par1)
229    {
230        int j = par1;
231
232        try
233        {
234            j = Integer.parseInt(par0Str);
235        }
236        catch (Throwable throwable)
237        {
238            ;
239        }
240
241        return j;
242    }
243
244    /**
245     * parses the string as integer or returns the second parameter if it fails. this value is capped to par2
246     */
247    public static int parseIntWithDefaultAndMax(String par0Str, int par1, int par2)
248    {
249        int k = par1;
250
251        try
252        {
253            k = Integer.parseInt(par0Str);
254        }
255        catch (Throwable throwable)
256        {
257            ;
258        }
259
260        if (k < par2)
261        {
262            k = par2;
263        }
264
265        return k;
266    }
267
268    /**
269     * parses the string as double or returns the second parameter if it fails.
270     */
271    public static double parseDoubleWithDefault(String par0Str, double par1)
272    {
273        double d1 = par1;
274
275        try
276        {
277            d1 = Double.parseDouble(par0Str);
278        }
279        catch (Throwable throwable)
280        {
281            ;
282        }
283
284        return d1;
285    }
286
287    public static double func_82713_a(String par0Str, double par1, double par3)
288    {
289        double d2 = par1;
290
291        try
292        {
293            d2 = Double.parseDouble(par0Str);
294        }
295        catch (Throwable throwable)
296        {
297            ;
298        }
299
300        if (d2 < par3)
301        {
302            d2 = par3;
303        }
304
305        return d2;
306    }
307
308    static
309    {
310        for (int i = 0; i < 65536; ++i)
311        {
312            SIN_TABLE[i] = (float)Math.sin((double)i * Math.PI * 2.0D / 65536.0D);
313        }
314    }
315}