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 average(long[] par0ArrayOfLong)
163 {
164 long var1 = 0L;
165 long[] var3 = par0ArrayOfLong;
166 int var4 = par0ArrayOfLong.length;
167
168 for (int var5 = 0; var5 < var4; ++var5)
169 {
170 long var6 = var3[var5];
171 var1 += var6;
172 }
173
174 return (double)var1 / (double)par0ArrayOfLong.length;
175 }
176
177 /**
178 * the angle is reduced to an angle between -180 and +180 by mod, and a 360 check
179 */
180 public static float wrapAngleTo180_float(float par0)
181 {
182 par0 %= 360.0F;
183
184 if (par0 >= 180.0F)
185 {
186 par0 -= 360.0F;
187 }
188
189 if (par0 < -180.0F)
190 {
191 par0 += 360.0F;
192 }
193
194 return par0;
195 }
196
197 /**
198 * the angle is reduced to an angle between -180 and +180 by mod, and a 360 check
199 */
200 public static double wrapAngleTo180_double(double par0)
201 {
202 par0 %= 360.0D;
203
204 if (par0 >= 180.0D)
205 {
206 par0 -= 360.0D;
207 }
208
209 if (par0 < -180.0D)
210 {
211 par0 += 360.0D;
212 }
213
214 return par0;
215 }
216
217 static
218 {
219 for (int var0 = 0; var0 < 65536; ++var0)
220 {
221 SIN_TABLE[var0] = (float)Math.sin((double)var0 * Math.PI * 2.0D / 65536.0D);
222 }
223 }
224 }