Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.5 KiB

  1. /* see copyright notice in squirrel.h */
  2. #include <squirrel.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include <sqstdmath.h>
  6. #define SINGLE_ARG_FUNC(_funcname) static SQInteger math_##_funcname(HSQUIRRELVM v){ \
  7. SQFloat f; \
  8. sq_getfloat(v,2,&f); \
  9. sq_pushfloat(v,(SQFloat)_funcname(f)); \
  10. return 1; \
  11. }
  12. #define TWO_ARGS_FUNC(_funcname) static SQInteger math_##_funcname(HSQUIRRELVM v){ \
  13. SQFloat p1,p2; \
  14. sq_getfloat(v,2,&p1); \
  15. sq_getfloat(v,3,&p2); \
  16. sq_pushfloat(v,(SQFloat)_funcname(p1,p2)); \
  17. return 1; \
  18. }
  19. static SQInteger math_srand(HSQUIRRELVM v)
  20. {
  21. SQInteger i;
  22. if(SQ_FAILED(sq_getinteger(v,2,&i)))
  23. return sq_throwerror(v,_SC("invalid param"));
  24. srand((unsigned int)i);
  25. return 0;
  26. }
  27. static SQInteger math_rand(HSQUIRRELVM v)
  28. {
  29. sq_pushinteger(v,rand());
  30. return 1;
  31. }
  32. static SQInteger math_abs(HSQUIRRELVM v)
  33. {
  34. SQInteger n;
  35. sq_getinteger(v,2,&n);
  36. sq_pushinteger(v,(SQInteger)abs((int)n));
  37. return 1;
  38. }
  39. SINGLE_ARG_FUNC(sqrt)
  40. SINGLE_ARG_FUNC(fabs)
  41. SINGLE_ARG_FUNC(sin)
  42. SINGLE_ARG_FUNC(cos)
  43. SINGLE_ARG_FUNC(asin)
  44. SINGLE_ARG_FUNC(acos)
  45. SINGLE_ARG_FUNC(log)
  46. SINGLE_ARG_FUNC(log10)
  47. SINGLE_ARG_FUNC(tan)
  48. SINGLE_ARG_FUNC(atan)
  49. TWO_ARGS_FUNC(atan2)
  50. TWO_ARGS_FUNC(pow)
  51. SINGLE_ARG_FUNC(floor)
  52. SINGLE_ARG_FUNC(ceil)
  53. SINGLE_ARG_FUNC(exp)
  54. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name),math_##name,nparams,tycheck}
  55. static SQRegFunction mathlib_funcs[] = {
  56. _DECL_FUNC(sqrt,2,_SC(".n")),
  57. _DECL_FUNC(sin,2,_SC(".n")),
  58. _DECL_FUNC(cos,2,_SC(".n")),
  59. _DECL_FUNC(asin,2,_SC(".n")),
  60. _DECL_FUNC(acos,2,_SC(".n")),
  61. _DECL_FUNC(log,2,_SC(".n")),
  62. _DECL_FUNC(log10,2,_SC(".n")),
  63. _DECL_FUNC(tan,2,_SC(".n")),
  64. _DECL_FUNC(atan,2,_SC(".n")),
  65. _DECL_FUNC(atan2,3,_SC(".nn")),
  66. _DECL_FUNC(pow,3,_SC(".nn")),
  67. _DECL_FUNC(floor,2,_SC(".n")),
  68. _DECL_FUNC(ceil,2,_SC(".n")),
  69. _DECL_FUNC(exp,2,_SC(".n")),
  70. _DECL_FUNC(srand,2,_SC(".n")),
  71. _DECL_FUNC(rand,1,NULL),
  72. _DECL_FUNC(fabs,2,_SC(".n")),
  73. _DECL_FUNC(abs,2,_SC(".n")),
  74. {0,0},
  75. };
  76. #ifndef M_PI
  77. #define M_PI (3.14159265358979323846)
  78. #endif
  79. SQRESULT sqstd_register_mathlib(HSQUIRRELVM v)
  80. {
  81. SQInteger i=0;
  82. while(mathlib_funcs[i].name!=0) {
  83. sq_pushstring(v,mathlib_funcs[i].name,-1);
  84. sq_newclosure(v,mathlib_funcs[i].f,0);
  85. sq_setparamscheck(v,mathlib_funcs[i].nparamscheck,mathlib_funcs[i].typemask);
  86. sq_setnativeclosurename(v,-1,mathlib_funcs[i].name);
  87. sq_createslot(v,-3);
  88. i++;
  89. }
  90. sq_pushstring(v,_SC("RAND_MAX"),-1);
  91. sq_pushinteger(v,RAND_MAX);
  92. sq_createslot(v,-3);
  93. sq_pushstring(v,_SC("PI"),-1);
  94. sq_pushfloat(v,(SQFloat)M_PI);
  95. sq_createslot(v,-3);
  96. return SQ_OK;
  97. }