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

Last change on this file since 319 was 319, checked in by epyon, 11 years ago
  • created core module and moved all free source files there
  • took the opportunity to update all copyright lines and minor cleanup
  • minor fixes
  • not all examples are up to date
File size: 3.6 KB
Line 
1// Copyright (C) 2012-2014 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of NV Libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
6
7#include "nv/lua/lua_aux.hh"
8
9#include <utility>
10#include "nv/lua/lua_raw.hh"
11#include "nv/core/random.hh"
12
13static int nluaaux_table_copy( lua_State* L )
14{
15        luaL_checktype( L, 1, LUA_TTABLE );
16        lua_settop( L, 1 );
17        nlua_shallowcopy( L, 1 );
18        return 1;
19}
20
21static int nluaaux_table_deepcopy( lua_State* L )
22{
23        luaL_checktype( L, 1, LUA_TTABLE );
24        lua_settop( L, 1 );
25        nlua_deepcopy( L, 1 );
26        return 0;
27}
28
29static int nluaaux_table_icopy( lua_State* L )
30{
31        luaL_checktype( L, 1, LUA_TTABLE );
32        lua_settop( L, 1 );
33        nlua_shallowicopy( L, 1 );
34        return 1;
35}
36
37static int nluaaux_table_merge( lua_State* L )
38{
39        luaL_checktype( L, 1, LUA_TTABLE );
40        luaL_checktype( L, 2, LUA_TTABLE );
41        lua_settop( L, 2 );
42        nlua_shallowmerge( L, 1 );
43        return 0;
44}
45
46static int nluaaux_table_reversed( lua_State* L )
47{
48        luaL_checktype( L, 1, LUA_TTABLE );
49        lua_settop( L, 1 );
50        nlua_pushreversed( L, 1 );
51        return 1;
52}
53
54static int nluaaux_table_toset( lua_State* L )
55{
56        luaL_checktype( L, 1, LUA_TTABLE );
57        lua_settop( L, 1 );
58        nlua_toset( L, 1 );
59        return 1;
60}
61
62static int nluaaux_table_tokeyset( lua_State* L )
63{
64        luaL_checktype( L, 1, LUA_TTABLE );
65        lua_settop( L, 1 );
66        nlua_tokeyset( L, 1 );
67        return 1;
68}
69
70static const struct luaL_Reg nluaaux_table_aux_f [] = {
71        { "copy",      nluaaux_table_copy },
72        { "deepcopy",  nluaaux_table_deepcopy },
73        { "icopy",     nluaaux_table_icopy },
74        { "merge",     nluaaux_table_merge },
75        { "reversed",  nluaaux_table_reversed },
76        { "toset",     nluaaux_table_toset },
77        { "tokeyset",  nluaaux_table_tokeyset },
78        { NULL, NULL }
79};
80
81static int nluaaux_math_clamp( lua_State* L )
82{
83        double v   = luaL_checknumber( L,  1 );
84        double min = luaL_optnumber( L, 2, 0 );
85        double max = luaL_optnumber( L, 3, 1 ); 
86        if ( min > max ) luaL_argerror( L, 2, "min is larger than max!" );
87        lua_pushnumber( L, v < min ? 2 : ( v > max ? 3 : 1 ) );
88        return 1;
89}
90
91static int nluaaux_math_dieroll( lua_State* L )
92{
93        lua_Integer dice  = luaL_checkinteger( L,  1 );
94        lua_Integer 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( static_cast< nv::uint32 >( dice ), static_cast< nv::uint32 >( 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                        lua_Integer arg1 = luaL_checkinteger( L, 1 );
112                        if ( arg1 < 1 ) arg1 = 1;
113                        lua_pushunsigned( L, nv::random::get().urange( 1, static_cast<nv::uint32>( arg1 ) ) );
114                }
115                else
116                {
117                        int arg1 = static_cast< int >( luaL_checkinteger( L, 1 ) );
118                        int arg2 = static_cast< int >( 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( lua_tounsigned( L, 1 ) );
129        return 0;
130}
131
132static const struct luaL_Reg nluaaux_math_aux_f [] = {
133        { "clamp",     nluaaux_math_clamp },
134        { "random",    nluaaux_math_random },
135        { "randomseed",nluaaux_math_randomseed },
136        { "dieroll",   nluaaux_math_dieroll },
137        { NULL, NULL }
138};
139
140void nv::lua::register_aux( lua_State* L )
141{
142        nlua_register( L, "table", nluaaux_table_aux_f );
143        nlua_register( L, "math", nluaaux_math_aux_f );
144}
145
Note: See TracBrowser for help on using the repository browser.