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

Last change on this file since 551 was 520, checked in by epyon, 9 years ago
  • ecs updates
  • animation updates
  • ragdoll manager
  • lua has own random engine
  • several minor fixes
  • particle engine/particle group
  • shitload of other stuff
  • bullet world
File size: 3.7 KB
RevLine 
[395]1// Copyright (C) 2012-2015 ChaosForge Ltd
[56]2// http://chaosforge.org/
3//
[395]4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
[56]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
[520]12static nv::random lua_rng;
13
[85]14static int nluaaux_table_copy( lua_State* L )
[56]15{
16        luaL_checktype( L, 1, LUA_TTABLE );
17        lua_settop( L, 1 );
[85]18        nlua_shallowcopy( L, 1 );
[56]19        return 1;
20}
21
[85]22static int nluaaux_table_deepcopy( lua_State* L )
[56]23{
24        luaL_checktype( L, 1, LUA_TTABLE );
25        lua_settop( L, 1 );
[85]26        nlua_deepcopy( L, 1 );
27        return 0;
28}
29
30static 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 );
[56]35        return 1;
36}
37
[85]38static int nluaaux_table_merge( lua_State* L )
[56]39{
40        luaL_checktype( L, 1, LUA_TTABLE );
41        luaL_checktype( L, 2, LUA_TTABLE );
42        lua_settop( L, 2 );
[85]43        nlua_shallowmerge( L, 1 );
[56]44        return 0;
45}
46
[85]47static int nluaaux_table_reversed( lua_State* L )
[56]48{
49        luaL_checktype( L, 1, LUA_TTABLE );
[85]50        lua_settop( L, 1 );
51        nlua_pushreversed( L, 1 );
52        return 1;
[56]53}
54
[85]55static int nluaaux_table_toset( lua_State* L )
[56]56{
57        luaL_checktype( L, 1, LUA_TTABLE );
58        lua_settop( L, 1 );
[85]59        nlua_toset( L, 1 );
[56]60        return 1;
61}
62
[85]63static int nluaaux_table_tokeyset( lua_State* L )
[56]64{
65        luaL_checktype( L, 1, LUA_TTABLE );
66        lua_settop( L, 1 );
[85]67        nlua_tokeyset( L, 1 );
[56]68        return 1;
69}
70
[85]71static 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 },
[56]79        { NULL, NULL }
80};
81
[85]82static 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
[180]92static int nluaaux_math_dieroll( lua_State* L )
93{
[204]94        lua_Integer dice  = luaL_checkinteger( L,  1 );
95        lua_Integer sides = luaL_checkinteger( L,  2 );
[180]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!" );
[520]98        lua_pushnumber( L, lua_rng.dice( static_cast< nv::uint32 >( dice ), static_cast< nv::uint32 >( sides ) ) );
[180]99        return 1;
100}
[85]101
[180]102static int nluaaux_math_random( lua_State* L )
103{
104        if ( lua_gettop( L ) == 0 )
105        {
[520]106                lua_pushnumber( L, lua_rng.frand(1.0f) );
[180]107        }
108        else
109        {
110                if ( lua_gettop( L ) == 1 )
111                {
[204]112                        lua_Integer arg1 = luaL_checkinteger( L, 1 );
[180]113                        if ( arg1 < 1 ) arg1 = 1;
[520]114                        nlua_pushunsigned( L, lua_rng.urange( 1, static_cast<nv::uint32>( arg1 ) ) );
[180]115                }
116                else
117                {
[204]118                        int arg1 = static_cast< int >( luaL_checkinteger( L, 1 ) );
119                        int arg2 = static_cast< int >( luaL_checkinteger( L, 2 ) );
[520]120                        int result = ( arg2 >= arg1 ? lua_rng.srange( arg1, arg2 ) : lua_rng.srange( arg2, arg1 ) );
[367]121                        lua_pushinteger( L, result );
[180]122                }
123        }
124        return 1;
125}
126
127static int nluaaux_math_randomseed( lua_State* L )
128{
[520]129        lua_rng.set_seed( nlua_tounsigned( L, 1 ) );
[180]130        return 0;
131}
132
[85]133static const struct luaL_Reg nluaaux_math_aux_f [] = {
134        { "clamp",     nluaaux_math_clamp },
[180]135        { "random",    nluaaux_math_random },
136        { "randomseed",nluaaux_math_randomseed },
137        { "dieroll",   nluaaux_math_dieroll },
[56]138        { NULL, NULL }
139};
140
[520]141nv::random & nv::lua::rng()
142{
143        return lua_rng;
144}
145
[503]146void nv::lua::register_aux( lua::state* state )
[56]147{
[503]148        nlua_register( state->get_raw(), "table", nluaaux_table_aux_f );
149        nlua_register( state->get_raw(), "math", nluaaux_math_aux_f );
[56]150}
151
Note: See TracBrowser for help on using the repository browser.