mConsoleFunctions.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "console/console.h"
  24. #include "math/mMathFn.h"
  25. #include "math/mRandom.h"
  26. #include "string/stringUnit.h"
  27. ConsoleFunctionGroupBegin( GeneralMath, "General math functions. Use these whenever possible, as they'll run much faster than script equivalents.");
  28. ConsoleFunction( mSolveQuadratic, const char *, 4, 4, "(float a, float b, float c)"
  29. "Solve a quadratic equation of form a*x^2 + b*x + c = 0.\n\n"
  30. "@returns A triple, contanining: sol x0 x1. sol is the number of"
  31. " solutions (being 0, 1, or 2), and x0 and x1 are the solutions, if any."
  32. " Unused x's are undefined.")
  33. {
  34. char * retBuffer = Con::getReturnBuffer(256);
  35. F32 x[2];
  36. U32 sol = mSolveQuadratic(dAtof(argv[1]), dAtof(argv[2]), dAtof(argv[3]), x);
  37. dSprintf(retBuffer, 256, "%d %g %g", sol, x[0], x[1]);
  38. return retBuffer;
  39. }
  40. ConsoleFunction( mSolveCubic, const char *, 5, 5, "(float a, float b, float c, float d)"
  41. "Solve a cubic equation of form a*x^3 + b*x^2 + c*x + d = 0.\n\n"
  42. "@returns A 4-tuple, contanining: sol x0 x1 x2. sol is the number of"
  43. " solutions (being 0, 1, 2, or 3), and x0, x1, x2 are the solutions, if any."
  44. " Unused x's are undefined.")
  45. {
  46. char * retBuffer = Con::getReturnBuffer(256);
  47. F32 x[3];
  48. U32 sol = mSolveCubic(dAtof(argv[1]), dAtof(argv[2]), dAtof(argv[3]), dAtof(argv[4]), x);
  49. dSprintf(retBuffer, 256, "%d %g %g %g", sol, x[0], x[1], x[2]);
  50. return retBuffer;
  51. }
  52. ConsoleFunction( mSolveQuartic, const char *, 6, 6, "(float a, float b, float c, float d, float e)"
  53. "Solve a quartic equation of form a*x^4 + b*x^3 + c*x^2 + d*x + e = 0.\n\n"
  54. "@returns A 5-tuple, contanining: sol x0 x1 x2 x3. sol is the number of"
  55. " solutions (ranging from 0-4), and x0, x1, x2 and x3 are the solutions, if any."
  56. " Unused x's are undefined.")
  57. {
  58. char * retBuffer = Con::getReturnBuffer(256);
  59. F32 x[4];
  60. U32 sol = mSolveQuartic(dAtof(argv[1]), dAtof(argv[2]), dAtof(argv[3]), dAtof(argv[4]), dAtof(argv[5]), x);
  61. dSprintf(retBuffer, 256, "%d %g %g %g %g", sol, x[0], x[1], x[2], x[3]);
  62. return retBuffer;
  63. }
  64. ConsoleFunction( mFloor, S32, 2, 2, "( val ) Use the mFloor function to calculate the next lowest integer value from val.\n"
  65. "@param val A floating-point value.\n"
  66. "@return Returns an integer representing the next lowest integer from val.\n"
  67. "@sa mCeil")
  68. {
  69. return (S32)mFloor(dAtof(argv[1]));
  70. }
  71. ConsoleFunction( mRound, F32, 2, 2, "(float v) Rounds a number. 0.5 is rounded up.\n"
  72. "@param val A floating-point value\n"
  73. "@return Returns the integer value closest to the given float")
  74. {
  75. return mRound( dAtof(argv[1]) );
  76. }
  77. ConsoleFunction( mCeil, S32, 2, 2, "( val ) Use the mCeil function to calculate the next highest integer value from val.\n"
  78. "@param val A floating-point value.\n"
  79. "@return Returns an integer representing the next highest integer from val.\n"
  80. "@sa mFloor")
  81. {
  82. return (S32)mCeil(dAtof(argv[1]));
  83. }
  84. ConsoleFunction( mFloatLength, const char *, 3, 3, "( val , numDecimals ) Use the mFloatLength function to limit the number of decimal places in val to numDecimals.\n"
  85. "@param val A floating-point value.\n"
  86. "@param numDecimals An integer between 0 and inf representing the number of decimal places to allow val to have.\n"
  87. "@return Returns a floating-point value equivalent to a truncated version of val, where the new version has numDecimals decimal places")
  88. {
  89. char * outBuffer = Con::getReturnBuffer(256);
  90. char fmtString[8] = "%.0f";
  91. U32 precision = dAtoi(argv[2]);
  92. if (precision > 9)
  93. precision = 9;
  94. fmtString[2] = '0' + precision;
  95. dSprintf(outBuffer, 255, fmtString, dAtof(argv[1]));
  96. return outBuffer;
  97. }
  98. //------------------------------------------------------------------------------
  99. ConsoleFunction( mAbs, F32, 2, 2, "( val ) Use the mAbs function to get the magnitude of val.\n"
  100. "@param val An integer or a floating-point value.\n"
  101. "@return Returns the magnitude of val")
  102. {
  103. return(mFabs(dAtof(argv[1])));
  104. }
  105. ConsoleFunction( mSign, F32, 2, 2, "( val ) Use the mSign function to get the signum of a number.\n"
  106. "@param val An integer or a floating-point value.\n"
  107. "@return Returns +1 if the number is >= 0, or -1 if it's < 0")
  108. {
  109. return(mFsign(dAtof(argv[1])));
  110. }
  111. ConsoleFunction( mSqrt, F32, 2, 2, "( val ) Use the mSqrt function to calculated the square root of val.\n"
  112. "@param val A numeric value.\n"
  113. "@return Returns the the squareroot of val")
  114. {
  115. return(mSqrt(dAtof(argv[1])));
  116. }
  117. ConsoleFunction( mPow, F32, 3, 3, "( val , power ) Use the mPow function to calculated val raised to the power of power.\n"
  118. "@param val A numeric (integer or floating-point) value to be raised to a power.\n"
  119. "@param power A numeric (integer or floating-point) power to raise val to.\n"
  120. "@return Returns val^power")
  121. {
  122. return(mPow(dAtof(argv[1]), dAtof(argv[2])));
  123. }
  124. ConsoleFunction( mLog, F32, 2, 2, "( val ) Use the mLog function to calculate the natural logarithm of val.\n"
  125. "@param val A numeric value.\n"
  126. "@return Returns the natural logarithm of val")
  127. {
  128. return(mLog(dAtof(argv[1])));
  129. }
  130. ConsoleFunction( mSin, F32, 2, 2, "( val ) Use the mSin function to get the sine of the radian angle val.\n"
  131. "@param val A value between -3.14159 and 3.14159.\n"
  132. "@return Returns the sine of val. This value will be in the range [ -1.0 , 1.0 ].\n"
  133. "@sa mAsin")
  134. {
  135. return(mSin(dAtof(argv[1])));
  136. }
  137. ConsoleFunction( mCos, F32, 2, 2, "( val ) Use the mCos function to get the cosine of the radian angle val.\n"
  138. "@param val A value between -3.14159 and 3.14159.\n"
  139. "@return Returns the cosine of val. This value will be in the range [ -1.0 , 1.0 ].\n"
  140. "@sa mAcos")
  141. {
  142. return(mCos(dAtof(argv[1])));
  143. }
  144. ConsoleFunction( mTan, F32, 2, 2, "( val ) Use the mTan function to get the tangent of the radian angle val.\n"
  145. "@param val A value between -3.14159/2 and 3.14159/2.\n"
  146. "@return Returns the tangent of val. This value will be in the range [ -inf.0 , inf.0 ].\n"
  147. "@sa mAtan")
  148. {
  149. return(mTan(dAtof(argv[1])));
  150. }
  151. ConsoleFunction( mAsin, F32, 2, 2, "( val ) Use the mAsin function to get the inverse sine of val in radians.\n"
  152. "@param val A value between -1.0 and 1.0 equal to the sine of some angle theta.\n"
  153. "@return Returns the inverse sine of val in radians. This value will be in the range [ - 3.14159/2 , 3.14159/2 ].\n"
  154. "@sa mSin")
  155. {
  156. return(mAsin(dAtof(argv[1])));
  157. }
  158. ConsoleFunction( mAcos, F32, 2, 2, "( val ) Use the mAcos function to get the inverse cosine of val in radians.\n"
  159. "@param val A value between -1.0 and 1.0 equal to the cosine of some angle theta.\n"
  160. "@return Returns the inverse cosine of val in radians. This value will be in the range [ 0 , 3.14159 ].\n"
  161. "@sa mCos")
  162. {
  163. return(mAcos(dAtof(argv[1])));
  164. }
  165. ConsoleFunction( mAtan, F32, 3, 3, "( val ) Use the mAtan function to get the inverse tangent of rise/run in radians.\n"
  166. "@param rise Vertical component of a line.\n"
  167. "@param run Horizontal component of a line.\n"
  168. "@return Returns the slope in radians (the arc-tangent) of a line with the given rise and run.\n"
  169. "@sa mTan")
  170. {
  171. return(mAtan(dAtof(argv[1]), dAtof(argv[2])));
  172. }
  173. ConsoleFunction( mRadToDeg, F32, 2, 2, "( val ) Use the mRadToDeg function to convert radians to degrees.\n"
  174. "@param val A floating-point number representing some number of radians.\n"
  175. "@return Returns the equivalent of the radian value val in degrees.\n"
  176. "@sa mDegToRad")
  177. {
  178. return(mRadToDeg(dAtof(argv[1])));
  179. }
  180. ConsoleFunction( mDegToRad, F32, 2, 2, "( val ) Use the mDegToRad function to convert degrees to radians.\n"
  181. "@param val A floating-point number representing some number of degrees.\n"
  182. "@return Returns the equivalent of the degree value val in radians.\n"
  183. "@sa mRadToDeg")
  184. {
  185. return(mDegToRad(dAtof(argv[1])));
  186. }
  187. ConsoleFunction( mClamp, F32, 4, 4, "(float number, float min, float max) Clamp a value between two other values.\n"
  188. "@param number A float value representing the number to clamp\n"
  189. "@param min The lower bound\n"
  190. "@param max The upper bound\n"
  191. "@return A float value the is within the given range")
  192. {
  193. F32 value = dAtof( argv[1] );
  194. F32 min = dAtof( argv[2] );
  195. F32 max = dAtof( argv[3] );
  196. return mClampF( value, min, max );
  197. }
  198. //-----------------------------------------------------------------------------
  199. ConsoleFunction( mGetMin, F32, 3, 3, "(a, b) - Returns the Minimum of two values.")
  200. {
  201. return getMin(dAtof(argv[1]), dAtof(argv[2]));
  202. }
  203. //-----------------------------------------------------------------------------
  204. ConsoleFunction( mGetMax, F32, 3, 3, "(a, b) - Returns the Maximum of two values.")
  205. {
  206. return getMax(dAtof(argv[1]), dAtof(argv[2]));
  207. }
  208. //-----------------------------------------------------------------------------
  209. ConsoleFunction( bits, const char*, 2, 2, "Converts a list of bit-positions into a value." )
  210. {
  211. // Calculate Element Count.
  212. U32 elementCount = StringUnit::getUnitCount( argv[1], " \t\n" );
  213. // Return nothing if there's no elements.
  214. if ( elementCount < 1 )
  215. {
  216. // Error!
  217. Con::printf("bits() - Invalid number of parameters!");
  218. return "0";
  219. }
  220. // Reset Bit Value.
  221. U32 bitValue = 0;
  222. // Parse Bits.
  223. for ( U32 n = 0; n < elementCount; n++ )
  224. {
  225. // Merge Bit Value.
  226. bitValue |= (U32)BIT(dAtoi(StringUnit::getUnit(argv[1], n, " \t\n")));
  227. }
  228. // Create Returnable Buffer.
  229. char* pBuffer = Con::getReturnBuffer(16);
  230. // Format Output.
  231. dSprintf( pBuffer, 16, "%u", bitValue );
  232. // Return Buffer.
  233. return pBuffer;
  234. }
  235. //-----------------------------------------------------------------------------
  236. ConsoleFunction( bit, const char*, 2, 2, "Converts a bit-position into a value." )
  237. {
  238. // Create Returnable Buffer.
  239. char* pBuffer = Con::getReturnBuffer(16);
  240. // Format Output.
  241. dSprintf( pBuffer, 16, "%u", U32(BIT(dAtoi(argv[1]))) );
  242. // Return Buffer.
  243. return pBuffer;
  244. }
  245. ConsoleFunction( bitInverse, const char*, 2, 2, "Returns the ones complement of a bit." )
  246. {
  247. // Create Returnable Buffer.
  248. char* pBuffer = Con::getReturnBuffer(16);
  249. // Format Output.
  250. dSprintf( pBuffer, 16, "%u", U32(~BIT(dAtoi(argv[1]))) );
  251. // Return Buffer.
  252. return pBuffer;
  253. }
  254. ConsoleFunction( addBitToMask, S32, 3, 3, "( mask, bit ) - Returns the mask with a bit added to it" )
  255. {
  256. U32 mask;
  257. dSscanf( argv[1], "%u", &mask );
  258. U32 bit = BIT( dAtoi( argv[2] ) );
  259. return mask | bit;
  260. }
  261. ConsoleFunction( removeBitFromMask, S32, 3, 3, "( mask, bit ) - Returns the mask with a bit removed from it" )
  262. {
  263. U32 mask;
  264. dSscanf( argv[1], "%u", &mask );
  265. U32 bit = BIT( dAtoi( argv[2] ) );
  266. return mask & ~bit;
  267. }
  268. ConsoleFunctionGroupEnd( GeneralMath );