Update readme.md
[openttd-joker.git] / src / 3rdparty / squirrel / sqstdlib / sqstdmath.cpp
blob0ff0090209ea4423d3c1fc9be2f692e73adb7b35
1 /* see copyright notice in squirrel.h */
3 #include "../../../stdafx.h"
5 #include <squirrel.h>
6 #include <math.h>
7 #include <sqstdmath.h>
9 #include "../../../safeguards.h"
11 #define SINGLE_ARG_FUNC(_funcname, num_ops) static SQInteger math_##_funcname(HSQUIRRELVM v){ \
12 SQFloat f; \
13 sq_decreaseops(v,num_ops); \
14 sq_getfloat(v,2,&f); \
15 sq_pushfloat(v,(SQFloat)_funcname(f)); \
16 return 1; \
19 #define TWO_ARGS_FUNC(_funcname, num_ops) static SQInteger math_##_funcname(HSQUIRRELVM v){ \
20 SQFloat p1,p2; \
21 sq_decreaseops(v,num_ops); \
22 sq_getfloat(v,2,&p1); \
23 sq_getfloat(v,3,&p2); \
24 sq_pushfloat(v,(SQFloat)_funcname(p1,p2)); \
25 return 1; \
28 #ifdef EXPORT_DEFAULT_SQUIRREL_FUNCTIONS
29 static SQInteger math_srand(HSQUIRRELVM v)
31 SQInteger i;
32 if(SQ_FAILED(sq_getinteger(v,2,&i)))
33 return sq_throwerror(v,"invalid param");
34 srand((unsigned int)i);
35 return 0;
38 static SQInteger math_rand(HSQUIRRELVM v)
40 sq_pushinteger(v,rand());
41 return 1;
43 #endif /* EXPORT_DEFAULT_SQUIRREL_FUNCTIONS */
45 static SQInteger math_abs(HSQUIRRELVM v)
47 SQInteger n;
48 sq_getinteger(v,2,&n);
49 sq_pushinteger(v,(SQInteger)abs((int)n));
50 return 1;
53 SINGLE_ARG_FUNC(sqrt, 100)
54 SINGLE_ARG_FUNC(fabs, 1)
55 SINGLE_ARG_FUNC(sin, 100)
56 SINGLE_ARG_FUNC(cos, 100)
57 SINGLE_ARG_FUNC(asin, 100)
58 SINGLE_ARG_FUNC(acos, 100)
59 SINGLE_ARG_FUNC(log, 100)
60 SINGLE_ARG_FUNC(log10, 100)
61 SINGLE_ARG_FUNC(tan, 100)
62 SINGLE_ARG_FUNC(atan, 100)
63 TWO_ARGS_FUNC(atan2, 100)
64 TWO_ARGS_FUNC(pow, 100)
65 SINGLE_ARG_FUNC(floor, 1)
66 SINGLE_ARG_FUNC(ceil, 1)
67 SINGLE_ARG_FUNC(exp, 100)
69 #define _DECL_FUNC(name,nparams,tycheck) {#name,math_##name,nparams,tycheck}
70 static SQRegFunction mathlib_funcs[] = {
71 _DECL_FUNC(sqrt,2,".n"),
72 _DECL_FUNC(sin,2,".n"),
73 _DECL_FUNC(cos,2,".n"),
74 _DECL_FUNC(asin,2,".n"),
75 _DECL_FUNC(acos,2,".n"),
76 _DECL_FUNC(log,2,".n"),
77 _DECL_FUNC(log10,2,".n"),
78 _DECL_FUNC(tan,2,".n"),
79 _DECL_FUNC(atan,2,".n"),
80 _DECL_FUNC(atan2,3,".nn"),
81 _DECL_FUNC(pow,3,".nn"),
82 _DECL_FUNC(floor,2,".n"),
83 _DECL_FUNC(ceil,2,".n"),
84 _DECL_FUNC(exp,2,".n"),
85 #ifdef EXPORT_DEFAULT_SQUIRREL_FUNCTIONS
86 _DECL_FUNC(srand,2,".n"),
87 _DECL_FUNC(rand,1,NULL),
88 #endif /* EXPORT_DEFAULT_SQUIRREL_FUNCTIONS */
89 _DECL_FUNC(fabs,2,".n"),
90 _DECL_FUNC(abs,2,".n"),
91 {0,0,0,0},
94 #ifndef M_PI
95 #define M_PI (3.14159265358979323846)
96 #endif
98 SQRESULT sqstd_register_mathlib(HSQUIRRELVM v)
100 SQInteger i=0;
101 while(mathlib_funcs[i].name!=0) {
102 sq_pushstring(v,mathlib_funcs[i].name,-1);
103 sq_newclosure(v,mathlib_funcs[i].f,0);
104 sq_setparamscheck(v,mathlib_funcs[i].nparamscheck,mathlib_funcs[i].typemask);
105 sq_setnativeclosurename(v,-1,mathlib_funcs[i].name);
106 sq_createslot(v,-3);
107 i++;
109 #ifdef EXPORT_DEFAULT_SQUIRREL_FUNCTIONS
110 sq_pushstring(v,"RAND_MAX",-1);
111 sq_pushinteger(v,RAND_MAX);
112 sq_createslot(v,-3);
113 #endif /* EXPORT_DEFAULT_SQUIRREL_FUNCTIONS */
114 sq_pushstring(v,"PI",-1);
115 sq_pushfloat(v,(SQFloat)M_PI);
116 sq_createslot(v,-3);
117 return SQ_OK;