Changeset 180
- Timestamp:
- 07/27/13 23:11:45 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lua/lua_aux.cc
r85 r180 7 7 #include "nv/lua/lua_aux.hh" 8 8 9 #include <utility> 9 10 #include "nv/lua/lua_raw.hh" 11 #include "nv/random.hh" 10 12 11 13 static int nluaaux_table_copy( lua_State* L ) … … 87 89 } 88 90 91 static int nluaaux_math_dieroll( lua_State* L ) 92 { 93 int dice = luaL_checkinteger( L, 1 ); 94 int sides = luaL_checkinteger( L, 2 ); 95 if ( dice < 1 ) luaL_argerror( L, 1, "die count lower than 1!" ); 96 if ( sides < 1 ) luaL_argerror( L, 2, "side count lower than 1!" ); 97 lua_pushnumber( L, nv::random::get().dice( dice, sides ) ); 98 return 1; 99 } 100 101 static int nluaaux_math_random( lua_State* L ) 102 { 103 if ( lua_gettop( L ) == 0 ) 104 { 105 lua_pushnumber( L, nv::random::get().frand(1.0f) ); 106 } 107 else 108 { 109 if ( lua_gettop( L ) == 1 ) 110 { 111 int arg1 = luaL_checkinteger( L, 1 ); 112 if ( arg1 < 1 ) arg1 = 1; 113 lua_pushunsigned( L, nv::random::get().urange( 1, arg1 ) ); 114 } 115 else 116 { 117 int arg1 = luaL_checkinteger( L, 1 ); 118 int arg2 = luaL_checkinteger( L, 2 ); 119 if (arg2 < arg1) std::swap( arg2, arg1 ); 120 lua_pushinteger( L, nv::random::get().srange( arg1, arg2 ) ); 121 } 122 } 123 return 1; 124 } 125 126 static int nluaaux_math_randomseed( lua_State* L ) 127 { 128 nv::random::get().set_seed( static_cast< nv::uint32 > ( L, 1 ) ); 129 return 0; 130 } 89 131 90 132 static const struct luaL_Reg nluaaux_math_aux_f [] = { 91 133 { "clamp", nluaaux_math_clamp }, 134 { "random", nluaaux_math_random }, 135 { "randomseed",nluaaux_math_randomseed }, 136 { "dieroll", nluaaux_math_dieroll }, 92 137 { NULL, NULL } 93 138 };
Note: See TracChangeset
for help on using the changeset viewer.