[319] | 1 | // Copyright (C) 2012-2014 ChaosForge Ltd
|
---|
[56] | 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 |
|
---|
[85] | 9 | #include "nv/lua/lua_raw.hh"
|
---|
[319] | 10 | #include "nv/core/random.hh"
|
---|
[85] | 11 |
|
---|
| 12 | static int nluaaux_table_copy( lua_State* L )
|
---|
[56] | 13 | {
|
---|
| 14 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
| 15 | lua_settop( L, 1 );
|
---|
[85] | 16 | nlua_shallowcopy( L, 1 );
|
---|
[56] | 17 | return 1;
|
---|
| 18 | }
|
---|
| 19 |
|
---|
[85] | 20 | static int nluaaux_table_deepcopy( lua_State* L )
|
---|
[56] | 21 | {
|
---|
| 22 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
| 23 | lua_settop( L, 1 );
|
---|
[85] | 24 | nlua_deepcopy( L, 1 );
|
---|
| 25 | return 0;
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | static 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 );
|
---|
[56] | 33 | return 1;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[85] | 36 | static int nluaaux_table_merge( lua_State* L )
|
---|
[56] | 37 | {
|
---|
| 38 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
| 39 | luaL_checktype( L, 2, LUA_TTABLE );
|
---|
| 40 | lua_settop( L, 2 );
|
---|
[85] | 41 | nlua_shallowmerge( L, 1 );
|
---|
[56] | 42 | return 0;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[85] | 45 | static int nluaaux_table_reversed( lua_State* L )
|
---|
[56] | 46 | {
|
---|
| 47 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
[85] | 48 | lua_settop( L, 1 );
|
---|
| 49 | nlua_pushreversed( L, 1 );
|
---|
| 50 | return 1;
|
---|
[56] | 51 | }
|
---|
| 52 |
|
---|
[85] | 53 | static int nluaaux_table_toset( lua_State* L )
|
---|
[56] | 54 | {
|
---|
| 55 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
| 56 | lua_settop( L, 1 );
|
---|
[85] | 57 | nlua_toset( L, 1 );
|
---|
[56] | 58 | return 1;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[85] | 61 | static int nluaaux_table_tokeyset( lua_State* L )
|
---|
[56] | 62 | {
|
---|
| 63 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
| 64 | lua_settop( L, 1 );
|
---|
[85] | 65 | nlua_tokeyset( L, 1 );
|
---|
[56] | 66 | return 1;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[85] | 69 | static 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 },
|
---|
[56] | 77 | { NULL, NULL }
|
---|
| 78 | };
|
---|
| 79 |
|
---|
[85] | 80 | static 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 |
|
---|
[180] | 90 | static int nluaaux_math_dieroll( lua_State* L )
|
---|
| 91 | {
|
---|
[204] | 92 | lua_Integer dice = luaL_checkinteger( L, 1 );
|
---|
| 93 | lua_Integer sides = luaL_checkinteger( L, 2 );
|
---|
[180] | 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!" );
|
---|
[198] | 96 | lua_pushnumber( L, nv::random::get().dice( static_cast< nv::uint32 >( dice ), static_cast< nv::uint32 >( sides ) ) );
|
---|
[180] | 97 | return 1;
|
---|
| 98 | }
|
---|
[85] | 99 |
|
---|
[180] | 100 | static 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 | {
|
---|
[204] | 110 | lua_Integer arg1 = luaL_checkinteger( L, 1 );
|
---|
[180] | 111 | if ( arg1 < 1 ) arg1 = 1;
|
---|
[198] | 112 | lua_pushunsigned( L, nv::random::get().urange( 1, static_cast<nv::uint32>( arg1 ) ) );
|
---|
[180] | 113 | }
|
---|
| 114 | else
|
---|
| 115 | {
|
---|
[204] | 116 | int arg1 = static_cast< int >( luaL_checkinteger( L, 1 ) );
|
---|
| 117 | int arg2 = static_cast< int >( luaL_checkinteger( L, 2 ) );
|
---|
[367] | 118 | int result = ( arg2 >= arg1 ? nv::random::get().srange( arg1, arg2 ) : nv::random::get().srange( arg2, arg1 ) );
|
---|
| 119 | lua_pushinteger( L, result );
|
---|
[180] | 120 | }
|
---|
| 121 | }
|
---|
| 122 | return 1;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | static int nluaaux_math_randomseed( lua_State* L )
|
---|
| 126 | {
|
---|
[198] | 127 | nv::random::get().set_seed( lua_tounsigned( L, 1 ) );
|
---|
[180] | 128 | return 0;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[85] | 131 | static const struct luaL_Reg nluaaux_math_aux_f [] = {
|
---|
| 132 | { "clamp", nluaaux_math_clamp },
|
---|
[180] | 133 | { "random", nluaaux_math_random },
|
---|
| 134 | { "randomseed",nluaaux_math_randomseed },
|
---|
| 135 | { "dieroll", nluaaux_math_dieroll },
|
---|
[56] | 136 | { NULL, NULL }
|
---|
| 137 | };
|
---|
| 138 |
|
---|
[207] | 139 | void nv::lua::register_aux( lua_State* L )
|
---|
[56] | 140 | {
|
---|
[85] | 141 | nlua_register( L, "table", nluaaux_table_aux_f );
|
---|
| 142 | nlua_register( L, "math", nluaaux_math_aux_f );
|
---|
[56] | 143 | }
|
---|
| 144 |
|
---|