[56] | 1 | // Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
|
---|
| 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 |
|
---|
[180] | 9 | #include <utility>
|
---|
[85] | 10 | #include "nv/lua/lua_raw.hh"
|
---|
[180] | 11 | #include "nv/random.hh"
|
---|
[85] | 12 |
|
---|
| 13 | static int nluaaux_table_copy( lua_State* L )
|
---|
[56] | 14 | {
|
---|
| 15 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
| 16 | lua_settop( L, 1 );
|
---|
[85] | 17 | nlua_shallowcopy( L, 1 );
|
---|
[56] | 18 | return 1;
|
---|
| 19 | }
|
---|
| 20 |
|
---|
[85] | 21 | static int nluaaux_table_deepcopy( lua_State* L )
|
---|
[56] | 22 | {
|
---|
| 23 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
| 24 | lua_settop( L, 1 );
|
---|
[85] | 25 | nlua_deepcopy( L, 1 );
|
---|
| 26 | return 0;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | static 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 );
|
---|
[56] | 34 | return 1;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[85] | 37 | static int nluaaux_table_merge( lua_State* L )
|
---|
[56] | 38 | {
|
---|
| 39 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
| 40 | luaL_checktype( L, 2, LUA_TTABLE );
|
---|
| 41 | lua_settop( L, 2 );
|
---|
[85] | 42 | nlua_shallowmerge( L, 1 );
|
---|
[56] | 43 | return 0;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[85] | 46 | static int nluaaux_table_reversed( lua_State* L )
|
---|
[56] | 47 | {
|
---|
| 48 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
[85] | 49 | lua_settop( L, 1 );
|
---|
| 50 | nlua_pushreversed( L, 1 );
|
---|
| 51 | return 1;
|
---|
[56] | 52 | }
|
---|
| 53 |
|
---|
[85] | 54 | static int nluaaux_table_toset( lua_State* L )
|
---|
[56] | 55 | {
|
---|
| 56 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
| 57 | lua_settop( L, 1 );
|
---|
[85] | 58 | nlua_toset( L, 1 );
|
---|
[56] | 59 | return 1;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[85] | 62 | static int nluaaux_table_tokeyset( lua_State* L )
|
---|
[56] | 63 | {
|
---|
| 64 | luaL_checktype( L, 1, LUA_TTABLE );
|
---|
| 65 | lua_settop( L, 1 );
|
---|
[85] | 66 | nlua_tokeyset( L, 1 );
|
---|
[56] | 67 | return 1;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[85] | 70 | static 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 },
|
---|
[56] | 78 | { NULL, NULL }
|
---|
| 79 | };
|
---|
| 80 |
|
---|
[85] | 81 | static 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 |
|
---|
[180] | 91 | static 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 | }
|
---|
[85] | 100 |
|
---|
[180] | 101 | static 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 |
|
---|
| 126 | static int nluaaux_math_randomseed( lua_State* L )
|
---|
| 127 | {
|
---|
| 128 | nv::random::get().set_seed( static_cast< nv::uint32 > ( L, 1 ) );
|
---|
| 129 | return 0;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[85] | 132 | static const struct luaL_Reg nluaaux_math_aux_f [] = {
|
---|
| 133 | { "clamp", nluaaux_math_clamp },
|
---|
[180] | 134 | { "random", nluaaux_math_random },
|
---|
| 135 | { "randomseed",nluaaux_math_randomseed },
|
---|
| 136 | { "dieroll", nluaaux_math_dieroll },
|
---|
[56] | 137 | { NULL, NULL }
|
---|
| 138 | };
|
---|
| 139 |
|
---|
| 140 | void nlua_register_aux( lua_State* L )
|
---|
| 141 | {
|
---|
[85] | 142 | nlua_register( L, "table", nluaaux_table_aux_f );
|
---|
| 143 | nlua_register( L, "math", nluaaux_math_aux_f );
|
---|
[56] | 144 | }
|
---|
| 145 |
|
---|