ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

lmathlib.cpp

Go to the documentation of this file.
00001 /*
00002 ** Lua standard mathematical library
00003 ** See Copyright Notice in lua.h
00004 */
00005 
00006 #define FORBIDDEN_SYMBOL_EXCEPTION_rand
00007 #define FORBIDDEN_SYMBOL_EXCEPTION_srand
00008 
00009 #include "engines/grim/lua/lauxlib.h"
00010 #include "engines/grim/lua/lua.h"
00011 #include "engines/grim/lua/lualib.h"
00012 
00013 namespace Grim {
00014 
00015 #ifndef LOCAL_PI
00016 #define LOCAL_PI          (3.14159265358979323846)
00017 #endif
00018 
00019 #define FROMRAD(a) ((a) * (180.0 / LOCAL_PI))
00020 #define TORAD(a)    ((a) * (LOCAL_PI / 180.0))
00021 
00022 static void math_abs() {
00023     float d = luaL_check_number(1);
00024     if (d < 0)
00025         d = -d;
00026     lua_pushnumber(d);
00027 }
00028 
00029 static void math_sin() {
00030     lua_pushnumber(sin(TORAD(luaL_check_number(1))));
00031 }
00032 
00033 static void math_cos() {
00034     lua_pushnumber(cos(TORAD(luaL_check_number(1))));
00035 }
00036 
00037 static void math_tan() {
00038     lua_pushnumber(tan(TORAD(luaL_check_number(1))));
00039 }
00040 
00041 static void math_asin() {
00042     lua_pushnumber(FROMRAD(asin(luaL_check_number(1))));
00043 }
00044 
00045 static void math_acos() {
00046     lua_pushnumber(FROMRAD(acos(luaL_check_number(1))));
00047 }
00048 
00049 static void math_atan() {
00050     lua_pushnumber(FROMRAD(atan(luaL_check_number(1))));
00051 }
00052 
00053 static void math_ceil() {
00054     lua_pushnumber(ceil(luaL_check_number(1)));
00055 }
00056 
00057 static void math_floor() {
00058     lua_pushnumber(floor(luaL_check_number(1)));
00059 }
00060 
00061 static void math_mod() {
00062     lua_pushnumber(fmod(luaL_check_number(1), luaL_check_number(2)));
00063 }
00064 
00065 static void math_sqrt() {
00066     lua_pushnumber(sqrt(luaL_check_number(1)));
00067 }
00068 
00069 static void math_pow() {
00070     lua_pushnumber(pow(luaL_check_number(1), luaL_check_number(2)));
00071 }
00072 
00073 static void math_deg() {
00074     lua_pushnumber(luaL_check_number(1) * (180.0/LOCAL_PI));
00075 }
00076 
00077 static void math_rad() {
00078     lua_pushnumber(luaL_check_number(1) * (LOCAL_PI / 180.0));
00079 }
00080 
00081 static void math_min() {
00082     int32 i = 1;
00083     float dmin = luaL_check_number(i);
00084     while (lua_getparam(++i) != LUA_NOOBJECT) {
00085         float d = luaL_check_number(i);
00086         if (d < dmin)
00087             dmin = d;
00088     }
00089     lua_pushnumber(dmin);
00090 }
00091 
00092 static void math_max() {
00093     int32 i = 1;
00094     float dmax = luaL_check_number(i);
00095     while (lua_getparam(++i) != LUA_NOOBJECT) {
00096         float d = luaL_check_number(i);
00097         if (d > dmax)
00098             dmax = d;
00099     }
00100     lua_pushnumber(dmax);
00101 }
00102 
00103 static void math_random() {
00104     float r = (float)(rand() % RAND_MAX) / (float)RAND_MAX;
00105     float l = luaL_opt_number(1, 0);
00106     if (l == 0)
00107         lua_pushnumber(r);
00108     else
00109         lua_pushnumber((int32)(r * l) + 1);
00110 }
00111 
00112 static void math_randomseed() {
00113     srand((unsigned int)luaL_check_number(1));
00114 }
00115 
00116 static struct luaL_reg mathlib[] = {
00117     { "abs",    math_abs},
00118     { "sin",    math_sin},
00119     { "cos",    math_cos},
00120     { "tan",    math_tan},
00121     { "asin",   math_asin},
00122     { "acos",   math_acos},
00123     { "atan",   math_atan},
00124     { "ceil",   math_ceil},
00125     { "floor",  math_floor},
00126     { "mod",    math_mod},
00127     { "sqrt",   math_sqrt},
00128     { "min",    math_min},
00129     { "max",    math_max},
00130     { "deg",    math_deg},
00131     { "rad",    math_rad},
00132     { "random",     math_random},
00133     { "randomseed", math_randomseed}
00134 };
00135 
00136 static luaL_reg powFunc[] = {
00137     { "math_pow",   math_pow }
00138 };
00139 
00140 /*
00141 ** Open math library
00142 */
00143 void lua_mathlibopen() {
00144     luaL_openlib(mathlib, (sizeof(mathlib) / sizeof(mathlib[0])));
00145     luaL_addlibtolist(powFunc, (sizeof(powFunc) / sizeof(powFunc[0])));
00146     lua_pushstring("deg");
00147     lua_setglobal("_TRIGMODE");
00148     lua_pushcfunction(math_pow);
00149     lua_pushnumber(0);  // to get its tag
00150     lua_settagmethod(lua_tag(lua_pop()), "pow");
00151     lua_pushnumber((float)LOCAL_PI);
00152     lua_setglobal("PI");
00153 }
00154 
00155 } // end of namespace Grim


Generated on Sat Mar 16 2019 05:01:42 for ResidualVM by doxygen 1.7.1
curved edge   curved edge