Ignore:
Timestamp:
07/27/13 23:11:45 (12 years ago)
Author:
epyon
Message:
  • lua/aux - now overrides for math.random and math.randomseed
  • lua/aux - math.dieroll added
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lua/lua_aux.cc

    r85 r180  
    77#include "nv/lua/lua_aux.hh"
    88
     9#include <utility>
    910#include "nv/lua/lua_raw.hh"
     11#include "nv/random.hh"
    1012
    1113static int nluaaux_table_copy( lua_State* L )
     
    8789}
    8890
     91static 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
     101static 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
     126static int nluaaux_math_randomseed( lua_State* L )
     127{
     128        nv::random::get().set_seed( static_cast< nv::uint32 > ( L, 1 ) );
     129        return 0;
     130}
    89131
    90132static const struct luaL_Reg nluaaux_math_aux_f [] = {
    91133        { "clamp",     nluaaux_math_clamp },
     134        { "random",    nluaaux_math_random },
     135        { "randomseed",nluaaux_math_randomseed },
     136        { "dieroll",   nluaaux_math_dieroll },
    92137        { NULL, NULL }
    93138};
Note: See TracChangeset for help on using the changeset viewer.