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