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 |
|
---|
12 | static nv::random lua_rng;
|
---|
13 |
|
---|
14 | static int nluaaux_table_copy( lua_State* L )
|
---|
15 | {
|
---|
16 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
17 | lua_settop( L, 1 );
|
---|
18 | nlua_shallowcopy( L, 1 );
|
---|
19 | return 1;
|
---|
20 | }
|
---|
21 |
|
---|
22 | static int nluaaux_table_deepcopy( lua_State* L )
|
---|
23 | {
|
---|
24 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
25 | lua_settop( L, 1 );
|
---|
26 | nlua_deepcopy( L, 1 );
|
---|
27 | return 0;
|
---|
28 | }
|
---|
29 |
|
---|
30 | static int nluaaux_table_icopy( lua_State* L )
|
---|
31 | {
|
---|
32 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
33 | lua_settop( L, 1 );
|
---|
34 | nlua_shallowicopy( L, 1 );
|
---|
35 | return 1;
|
---|
36 | }
|
---|
37 |
|
---|
38 | static int nluaaux_table_merge( lua_State* L )
|
---|
39 | {
|
---|
40 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
41 | luaL_checktype( L, 2, LUA_TTABLE );
|
---|
42 | lua_settop( L, 2 );
|
---|
43 | nlua_shallowmerge( L, 1 );
|
---|
44 | return 0;
|
---|
45 | }
|
---|
46 |
|
---|
47 | static int nluaaux_table_reversed( lua_State* L )
|
---|
48 | {
|
---|
49 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
50 | lua_settop( L, 1 );
|
---|
51 | nlua_pushreversed( L, 1 );
|
---|
52 | return 1;
|
---|
53 | }
|
---|
54 |
|
---|
55 | static int nluaaux_table_toset( lua_State* L )
|
---|
56 | {
|
---|
57 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
58 | lua_settop( L, 1 );
|
---|
59 | nlua_toset( L, 1 );
|
---|
60 | return 1;
|
---|
61 | }
|
---|
62 |
|
---|
63 | static int nluaaux_table_tokeyset( lua_State* L )
|
---|
64 | {
|
---|
65 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
66 | lua_settop( L, 1 );
|
---|
67 | nlua_tokeyset( L, 1 );
|
---|
68 | return 1;
|
---|
69 | }
|
---|
70 |
|
---|
71 | static const struct luaL_Reg nluaaux_table_aux_f [] = {
|
---|
72 | { "copy", nluaaux_table_copy },
|
---|
73 | { "deepcopy", nluaaux_table_deepcopy },
|
---|
74 | { "icopy", nluaaux_table_icopy },
|
---|
75 | { "merge", nluaaux_table_merge },
|
---|
76 | { "reversed", nluaaux_table_reversed },
|
---|
77 | { "toset", nluaaux_table_toset },
|
---|
78 | { "tokeyset", nluaaux_table_tokeyset },
|
---|
79 | { NULL, NULL }
|
---|
80 | };
|
---|
81 |
|
---|
82 | static int nluaaux_math_clamp( lua_State* L )
|
---|
83 | {
|
---|
84 | double v = luaL_checknumber( L, 1 );
|
---|
85 | double min = luaL_optnumber( L, 2, 0 );
|
---|
86 | double max = luaL_optnumber( L, 3, 1 );
|
---|
87 | if ( min > max ) luaL_argerror( L, 2, "min is larger than max!" );
|
---|
88 | lua_pushnumber( L, v < min ? 2 : ( v > max ? 3 : 1 ) );
|
---|
89 | return 1;
|
---|
90 | }
|
---|
91 |
|
---|
92 | static int nluaaux_math_dieroll( lua_State* L )
|
---|
93 | {
|
---|
94 | lua_Integer dice = luaL_checkinteger( L, 1 );
|
---|
95 | lua_Integer sides = luaL_checkinteger( L, 2 );
|
---|
96 | if ( dice < 1 ) luaL_argerror( L, 1, "die count lower than 1!" );
|
---|
97 | if ( sides < 1 ) luaL_argerror( L, 2, "side count lower than 1!" );
|
---|
98 | lua_pushnumber( L, lua_rng.dice( static_cast< nv::uint32 >( dice ), static_cast< nv::uint32 >( sides ) ) );
|
---|
99 | return 1;
|
---|
100 | }
|
---|
101 |
|
---|
102 | static int nluaaux_math_random( lua_State* L )
|
---|
103 | {
|
---|
104 | if ( lua_gettop( L ) == 0 )
|
---|
105 | {
|
---|
106 | lua_pushnumber( L, lua_rng.frand(1.0f) );
|
---|
107 | }
|
---|
108 | else
|
---|
109 | {
|
---|
110 | if ( lua_gettop( L ) == 1 )
|
---|
111 | {
|
---|
112 | lua_Integer arg1 = luaL_checkinteger( L, 1 );
|
---|
113 | if ( arg1 < 1 ) arg1 = 1;
|
---|
114 | nlua_pushunsigned( L, lua_rng.urange( 1, static_cast<nv::uint32>( arg1 ) ) );
|
---|
115 | }
|
---|
116 | else
|
---|
117 | {
|
---|
118 | int arg1 = static_cast< int >( luaL_checkinteger( L, 1 ) );
|
---|
119 | int arg2 = static_cast< int >( luaL_checkinteger( L, 2 ) );
|
---|
120 | int result = ( arg2 >= arg1 ? lua_rng.srange( arg1, arg2 ) : lua_rng.srange( arg2, arg1 ) );
|
---|
121 | lua_pushinteger( L, result );
|
---|
122 | }
|
---|
123 | }
|
---|
124 | return 1;
|
---|
125 | }
|
---|
126 |
|
---|
127 | static int nluaaux_math_randomseed( lua_State* L )
|
---|
128 | {
|
---|
129 | lua_rng.set_seed( nlua_tounsigned( L, 1 ) );
|
---|
130 | return 0;
|
---|
131 | }
|
---|
132 |
|
---|
133 | static const struct luaL_Reg nluaaux_math_aux_f [] = {
|
---|
134 | { "clamp", nluaaux_math_clamp },
|
---|
135 | { "random", nluaaux_math_random },
|
---|
136 | { "randomseed",nluaaux_math_randomseed },
|
---|
137 | { "dieroll", nluaaux_math_dieroll },
|
---|
138 | { NULL, NULL }
|
---|
139 | };
|
---|
140 |
|
---|
141 | nv::random & nv::lua::rng()
|
---|
142 | {
|
---|
143 | return lua_rng;
|
---|
144 | }
|
---|
145 |
|
---|
146 | void nv::lua::register_aux( lua::state* state )
|
---|
147 | {
|
---|
148 | nlua_register( state->get_raw(), "table", nluaaux_table_aux_f );
|
---|
149 | nlua_register( state->get_raw(), "math", nluaaux_math_aux_f );
|
---|
150 | }
|
---|
151 |
|
---|