source: trunk/src/lua/lua_aux.cc @ 503

Last change on this file since 503 was 503, checked in by epyon, 9 years ago
  • nv::random - support for different rng sources
  • nv::types - fixes and better support
  • nv::mesh_creator - full range transform
  • nv::context - buffer mask as separate type
  • nv::lua - initializations from lua::state instead of lua_State
  • nv::lua - initial support for rtti
File size: 3.7 KB
Line 
1// Copyright (C) 2012-2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
7#include "nv/lua/lua_aux.hh"
8
9#include "nv/lua/lua_raw.hh"
10#include "nv/core/random.hh"
11
12static int nluaaux_table_copy( lua_State* L )
13{
14        luaL_checktype( L, 1, LUA_TTABLE );
15        lua_settop( L, 1 );
16        nlua_shallowcopy( L, 1 );
17        return 1;
18}
19
20static int nluaaux_table_deepcopy( lua_State* L )
21{
22        luaL_checktype( L, 1, LUA_TTABLE );
23        lua_settop( L, 1 );
24        nlua_deepcopy( L, 1 );
25        return 0;
26}
27
28static int nluaaux_table_icopy( lua_State* L )
29{
30        luaL_checktype( L, 1, LUA_TTABLE );
31        lua_settop( L, 1 );
32        nlua_shallowicopy( L, 1 );
33        return 1;
34}
35
36static int nluaaux_table_merge( lua_State* L )
37{
38        luaL_checktype( L, 1, LUA_TTABLE );
39        luaL_checktype( L, 2, LUA_TTABLE );
40        lua_settop( L, 2 );
41        nlua_shallowmerge( L, 1 );
42        return 0;
43}
44
45static int nluaaux_table_reversed( lua_State* L )
46{
47        luaL_checktype( L, 1, LUA_TTABLE );
48        lua_settop( L, 1 );
49        nlua_pushreversed( L, 1 );
50        return 1;
51}
52
53static int nluaaux_table_toset( lua_State* L )
54{
55        luaL_checktype( L, 1, LUA_TTABLE );
56        lua_settop( L, 1 );
57        nlua_toset( L, 1 );
58        return 1;
59}
60
61static int nluaaux_table_tokeyset( lua_State* L )
62{
63        luaL_checktype( L, 1, LUA_TTABLE );
64        lua_settop( L, 1 );
65        nlua_tokeyset( L, 1 );
66        return 1;
67}
68
69static const struct luaL_Reg nluaaux_table_aux_f [] = {
70        { "copy",      nluaaux_table_copy },
71        { "deepcopy",  nluaaux_table_deepcopy },
72        { "icopy",     nluaaux_table_icopy },
73        { "merge",     nluaaux_table_merge },
74        { "reversed",  nluaaux_table_reversed },
75        { "toset",     nluaaux_table_toset },
76        { "tokeyset",  nluaaux_table_tokeyset },
77        { NULL, NULL }
78};
79
80static int nluaaux_math_clamp( lua_State* L )
81{
82        double v   = luaL_checknumber( L,  1 );
83        double min = luaL_optnumber( L, 2, 0 );
84        double max = luaL_optnumber( L, 3, 1 ); 
85        if ( min > max ) luaL_argerror( L, 2, "min is larger than max!" );
86        lua_pushnumber( L, v < min ? 2 : ( v > max ? 3 : 1 ) );
87        return 1;
88}
89
90static int nluaaux_math_dieroll( lua_State* L )
91{
92        lua_Integer dice  = luaL_checkinteger( L,  1 );
93        lua_Integer sides = luaL_checkinteger( L,  2 );
94        if ( dice < 1 )  luaL_argerror( L, 1, "die count lower than 1!" );
95        if ( sides < 1 ) luaL_argerror( L, 2, "side count lower than 1!" );
96        lua_pushnumber( L, nv::random::get().dice( static_cast< nv::uint32 >( dice ), static_cast< nv::uint32 >( sides ) ) );
97        return 1;
98}
99
100static int nluaaux_math_random( lua_State* L )
101{
102        if ( lua_gettop( L ) == 0 )
103        {
104                lua_pushnumber( L, nv::random::get().frand(1.0f) );
105        }
106        else
107        {
108                if ( lua_gettop( L ) == 1 )
109                {
110                        lua_Integer arg1 = luaL_checkinteger( L, 1 );
111                        if ( arg1 < 1 ) arg1 = 1;
112                        nlua_pushunsigned( L, nv::random::get().urange( 1, static_cast<nv::uint32>( arg1 ) ) );
113                }
114                else
115                {
116                        int arg1 = static_cast< int >( luaL_checkinteger( L, 1 ) );
117                        int arg2 = static_cast< int >( luaL_checkinteger( L, 2 ) );
118                        int result = ( arg2 >= arg1 ? nv::random::get().srange( arg1, arg2 ) : nv::random::get().srange( arg2, arg1 ) );
119                        lua_pushinteger( L, result );
120                }
121        }
122        return 1;
123}
124
125static int nluaaux_math_randomseed( lua_State* L )
126{
127        nv::random::get().set_seed( nlua_tounsigned( L, 1 ) );
128        return 0;
129}
130
131static const struct luaL_Reg nluaaux_math_aux_f [] = {
132        { "clamp",     nluaaux_math_clamp },
133        { "random",    nluaaux_math_random },
134        { "randomseed",nluaaux_math_randomseed },
135        { "dieroll",   nluaaux_math_dieroll },
136        { NULL, NULL }
137};
138
139void nv::lua::register_aux( lua::state* state )
140{
141        nlua_register( state->get_raw(), "table", nluaaux_table_aux_f );
142        nlua_register( state->get_raw(), "math", nluaaux_math_aux_f );
143}
144
Note: See TracBrowser for help on using the repository browser.