mConsoleFunctions.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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( mSqrt, F32, 2, 2, "( val ) Use the mSqrt function to calculated the square root of val.\n"
  106. "@param val A numeric value.\n"
  107. "@return Returns the the squareroot of val")
  108. {
  109. return(mSqrt(dAtof(argv[1])));
  110. }
  111. ConsoleFunction( mPow, F32, 3, 3, "( val , power ) Use the mPow function to calculated val raised to the power of power.\n"
  112. "@param val A numeric (integer or floating-point) value to be raised to a power.\n"
  113. "@param power A numeric (integer or floating-point) power to raise val to.\n"
  114. "@return Returns val^power")
  115. {
  116. return(mPow(dAtof(argv[1]), dAtof(argv[2])));
  117. }
  118. ConsoleFunction( mLog, F32, 2, 2, "( val ) Use the mLog function to calculate the natural logarithm of val.\n"
  119. "@param val A numeric value.\n"
  120. "@return Returns the natural logarithm of val")
  121. {
  122. return(mLog(dAtof(argv[1])));
  123. }
  124. ConsoleFunction( mSin, F32, 2, 2, "( val ) Use the mSin function to get the sine of the radian angle val.\n"
  125. "@param val A value between -3.14159 and 3.14159.\n"
  126. "@return Returns the sine of val. This value will be in the range [ -1.0 , 1.0 ].\n"
  127. "@sa mAsin")
  128. {
  129. return(mSin(dAtof(argv[1])));
  130. }
  131. ConsoleFunction( mCos, F32, 2, 2, "( val ) Use the mCos function to get the cosine of the radian angle val.\n"
  132. "@param val A value between -3.14159 and 3.14159.\n"
  133. "@return Returns the cosine of val. This value will be in the range [ -1.0 , 1.0 ].\n"
  134. "@sa mAcos")
  135. {
  136. return(mCos(dAtof(argv[1])));
  137. }
  138. ConsoleFunction( mTan, F32, 2, 2, "( val ) Use the mTan function to get the tangent of the radian angle val.\n"
  139. "@param val A value between -3.14159/2 and 3.14159/2.\n"
  140. "@return Returns the tangent of val. This value will be in the range [ -inf.0 , inf.0 ].\n"
  141. "@sa mAtan")
  142. {
  143. return(mTan(dAtof(argv[1])));
  144. }
  145. ConsoleFunction( mAsin, F32, 2, 2, "( val ) Use the mAsin function to get the inverse sine of val in radians.\n"
  146. "@param val A value between -1.0 and 1.0 equal to the sine of some angle theta.\n"
  147. "@return Returns the inverse sine of val in radians. This value will be in the range [ - 3.14159/2 , 3.14159/2 ].\n"
  148. "@sa mSin")
  149. {
  150. return(mAsin(dAtof(argv[1])));
  151. }
  152. ConsoleFunction( mAcos, F32, 2, 2, "( val ) Use the mAcos function to get the inverse cosine of val in radians.\n"
  153. "@param val A value between -1.0 and 1.0 equal to the cosine of some angle theta.\n"
  154. "@return Returns the inverse cosine of val in radians. This value will be in the range [ 0 , 3.14159 ].\n"
  155. "@sa mCos")
  156. {
  157. return(mAcos(dAtof(argv[1])));
  158. }
  159. ConsoleFunction( mAtan, F32, 3, 3, "( val ) Use the mAtan function to get the inverse tangent of rise/run in radians.\n"
  160. "@param rise Vertical component of a line.\n"
  161. "@param run Horizontal component of a line.\n"
  162. "@return Returns the slope in radians (the arc-tangent) of a line with the given rise and run.\n"
  163. "@sa mTan")
  164. {
  165. return(mAtan(dAtof(argv[1]), dAtof(argv[2])));
  166. }
  167. ConsoleFunction( mRadToDeg, F32, 2, 2, "( val ) Use the mRadToDeg function to convert radians to degrees.\n"
  168. "@param val A floating-point number representing some number of radians.\n"
  169. "@return Returns the equivalent of the radian value val in degrees.\n"
  170. "@sa mDegToRad")
  171. {
  172. return(mRadToDeg(dAtof(argv[1])));
  173. }
  174. ConsoleFunction( mDegToRad, F32, 2, 2, "( val ) Use the mDegToRad function to convert degrees to radians.\n"
  175. "@param val A floating-point number representing some number of degrees.\n"
  176. "@return Returns the equivalent of the degree value val in radians.\n"
  177. "@sa mRadToDeg")
  178. {
  179. return(mDegToRad(dAtof(argv[1])));
  180. }
  181. ConsoleFunction( mClamp, F32, 4, 4, "(float number, float min, float max) Clamp a value between two other values.\n"
  182. "@param number A float value representing the number to clamp\n"
  183. "@param min The lower bound\n"
  184. "@param max The upper bound\n"
  185. "@return A float value the is within the given range")
  186. {
  187. F32 value = dAtof( argv[1] );
  188. F32 min = dAtof( argv[2] );
  189. F32 max = dAtof( argv[3] );
  190. return mClampF( value, min, max );
  191. }
  192. //-----------------------------------------------------------------------------
  193. ConsoleFunction( mGetMin, F32, 3, 3, "(a, b) - Returns the Minimum of two values.")
  194. {
  195. return getMin(dAtof(argv[1]), dAtof(argv[2]));
  196. }
  197. //-----------------------------------------------------------------------------
  198. ConsoleFunction( mGetMax, F32, 3, 3, "(a, b) - Returns the Maximum of two values.")
  199. {
  200. return getMax(dAtof(argv[1]), dAtof(argv[2]));
  201. }
  202. //-----------------------------------------------------------------------------
  203. ConsoleFunction( bits, const char*, 2, 2, "Converts a list of bit-positions into a value." )
  204. {
  205. // Calculate Element Count.
  206. U32 elementCount = StringUnit::getUnitCount( argv[1], " \t\n" );
  207. // Return nothing if there's no elements.
  208. if ( elementCount < 1 )
  209. {
  210. // Error!
  211. Con::printf("bits() - Invalid number of parameters!");
  212. return "0";
  213. }
  214. // Reset Bit Value.
  215. U32 bitValue = 0;
  216. // Parse Bits.
  217. for ( U32 n = 0; n < elementCount; n++ )
  218. {
  219. // Merge Bit Value.
  220. bitValue |= (U32)BIT(dAtoi(StringUnit::getUnit(argv[1], n, " \t\n")));
  221. }
  222. // Create Returnable Buffer.
  223. char* pBuffer = Con::getReturnBuffer(16);
  224. // Format Output.
  225. dSprintf( pBuffer, 16, "%u", bitValue );
  226. // Return Buffer.
  227. return pBuffer;
  228. }
  229. //-----------------------------------------------------------------------------
  230. ConsoleFunction( bit, const char*, 2, 2, "Converts a bit-position into a value." )
  231. {
  232. // Create Returnable Buffer.
  233. char* pBuffer = Con::getReturnBuffer(16);
  234. // Format Output.
  235. dSprintf( pBuffer, 16, "%u", U32(BIT(dAtoi(argv[1]))) );
  236. // Return Buffer.
  237. return pBuffer;
  238. }
  239. ConsoleFunction( bitInverse, const char*, 2, 2, "Returns the ones complement of a bit." )
  240. {
  241. // Create Returnable Buffer.
  242. char* pBuffer = Con::getReturnBuffer(16);
  243. // Format Output.
  244. dSprintf( pBuffer, 16, "%u", U32(~BIT(dAtoi(argv[1]))) );
  245. // Return Buffer.
  246. return pBuffer;
  247. }
  248. ConsoleFunction( addBitToMask, S32, 3, 3, "( mask, bit ) - Returns the mask with a bit added to it" )
  249. {
  250. U32 mask;
  251. dSscanf( argv[1], "%u", &mask );
  252. U32 bit = BIT( dAtoi( argv[2] ) );
  253. return mask | bit;
  254. }
  255. ConsoleFunction( removeBitFromMask, S32, 3, 3, "( mask, bit ) - Returns the mask with a bit removed from it" )
  256. {
  257. U32 mask;
  258. dSscanf( argv[1], "%u", &mask );
  259. U32 bit = BIT( dAtoi( argv[2] ) );
  260. return mask & ~bit;
  261. }
  262. ConsoleFunctionGroupEnd( GeneralMath );