001 package net.minecraft.util;
002
003 import cpw.mods.fml.relauncher.Side;
004 import cpw.mods.fml.relauncher.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 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 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 /**
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 var2 = par1;
228
229 try
230 {
231 var2 = Integer.parseInt(par0Str);
232 }
233 catch (Throwable var4)
234 {
235 ;
236 }
237
238 return var2;
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 var3 = par1;
247
248 try
249 {
250 var3 = Integer.parseInt(par0Str);
251 }
252 catch (Throwable var5)
253 {
254 ;
255 }
256
257 if (var3 < par2)
258 {
259 var3 = par2;
260 }
261
262 return var3;
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 var3 = par1;
271
272 try
273 {
274 var3 = Double.parseDouble(par0Str);
275 }
276 catch (Throwable var6)
277 {
278 ;
279 }
280
281 return var3;
282 }
283
284 public static double func_82713_a(String par0Str, double par1, double par3)
285 {
286 double var5 = par1;
287
288 try
289 {
290 var5 = Double.parseDouble(par0Str);
291 }
292 catch (Throwable var8)
293 {
294 ;
295 }
296
297 if (var5 < par3)
298 {
299 var5 = par3;
300 }
301
302 return var5;
303 }
304
305 static
306 {
307 for (int var0 = 0; var0 < 65536; ++var0)
308 {
309 SIN_TABLE[var0] = (float)Math.sin((double)var0 * Math.PI * 2.0D / 65536.0D);
310 }
311 }
312 }