Index: trunk/src/lua/lua_area.cc
===================================================================
--- trunk/src/lua/lua_area.cc	(revision 489)
+++ trunk/src/lua/lua_area.cc	(revision 490)
@@ -462,5 +462,5 @@
 {
 	int stack = lua_gettop( L );
-	luaL_requiref(L, "area", luaopen_area, 1);
+	nlua_requiref(L, "area", luaopen_area, 1);
 	lua_settop( L, stack );
 }
Index: trunk/src/lua/lua_aux.cc
===================================================================
--- trunk/src/lua/lua_aux.cc	(revision 489)
+++ trunk/src/lua/lua_aux.cc	(revision 490)
@@ -110,5 +110,5 @@
 			lua_Integer arg1 = luaL_checkinteger( L, 1 );
 			if ( arg1 < 1 ) arg1 = 1;
-			lua_pushunsigned( L, nv::random::get().urange( 1, static_cast<nv::uint32>( arg1 ) ) );
+			nlua_pushunsigned( L, nv::random::get().urange( 1, static_cast<nv::uint32>( arg1 ) ) );
 		}
 		else
@@ -125,5 +125,5 @@
 static int nluaaux_math_randomseed( lua_State* L )
 {
-	nv::random::get().set_seed( lua_tounsigned( L, 1 ) );
+	nv::random::get().set_seed( nlua_tounsigned( L, 1 ) );
 	return 0;
 }
Index: trunk/src/lua/lua_map_area.cc
===================================================================
--- trunk/src/lua/lua_map_area.cc	(revision 489)
+++ trunk/src/lua/lua_map_area.cc	(revision 490)
@@ -21,5 +21,5 @@
 		return map->string_to_id( lua_tostring( L, index ) );
 	else
-		return lua_tounsigned( L, index );
+		return nlua_tounsigned( L, index );
 }
 
@@ -49,5 +49,5 @@
 bool nv::lua::detail::is_map_area( lua_State* L, int index )
 {
-	return luaL_testudata( L, index, NLUA_MAP_AREA_METATABLE ) != 0;
+	return nlua_testudata( L, index, NLUA_MAP_AREA_METATABLE ) != 0;
 }
 
@@ -151,5 +151,5 @@
 {
 	nv::map_area* ma = to_map_area( L, 1 );
-	lua_pushunsigned( L, ma->get_cell( to_coord( L, 2 ) ) );
+	nlua_pushunsigned( L, ma->get_cell( to_coord( L, 2 ) ) );
 	return 1;
 }
@@ -158,5 +158,5 @@
 {
 	nv::map_area* ma = to_map_area( L, 1 );
-	ma->set_cell( to_coord( L, 2 ), lua_tounsigned( L, 3 ) );
+	ma->set_cell( to_coord( L, 2 ), nlua_tounsigned( L, 3 ) );
 	return 0;
 }
@@ -173,5 +173,5 @@
 	else
 	{
-		lua_pushunsigned( L, ma->get_cell( to_coord( L, 2 ) ) );
+		nlua_pushunsigned( L, ma->get_cell( to_coord( L, 2 ) ) );
 	}
 	return 1;
@@ -181,5 +181,5 @@
 {
 	nv::map_area* ma = to_map_area( L, 1 );
-	ma->set_cell( to_coord( L, 2 ), lua_tounsigned( L, 3 ) );
+	ma->set_cell( to_coord( L, 2 ), nlua_tounsigned( L, 3 ) );
 	return 0;
 }
Index: trunk/src/lua/lua_map_tile.cc
===================================================================
--- trunk/src/lua/lua_map_tile.cc	(revision 489)
+++ trunk/src/lua/lua_map_tile.cc	(revision 490)
@@ -47,5 +47,5 @@
 	map_tile* result = reinterpret_cast<map_tile*>( lua_newuserdata( L, sizeof(map_tile) ) );
 	*result = tile;
-	luaL_setmetatable( L, NLUA_MAP_TILE_METATABLE );
+	nlua_setmetatable( L, NLUA_MAP_TILE_METATABLE );
 }
 
Index: trunk/src/lua/lua_math.cc
===================================================================
--- trunk/src/lua/lua_math.cc	(revision 489)
+++ trunk/src/lua/lua_math.cc	(revision 490)
@@ -381,11 +381,11 @@
 	int stack = lua_gettop( L );
 
-	luaL_requiref(L, "coord", luaopen_vec<nv::ivec2>, 1);
-	luaL_requiref(L, "ivec2", luaopen_vec<nv::ivec2>, 1);
-	luaL_requiref(L, "ivec3", luaopen_vec<nv::ivec3>, 1);
-	luaL_requiref(L, "ivec4", luaopen_vec<nv::ivec4>, 1);
-	luaL_requiref(L, "vec2", luaopen_vec<nv::vec2>, 1);
-	luaL_requiref(L, "vec3", luaopen_vec<nv::vec3>, 1);
-	luaL_requiref(L, "vec4", luaopen_vec<nv::vec4>, 1);
+	nlua_requiref(L, "coord", luaopen_vec<nv::ivec2>, 1);
+	nlua_requiref(L, "ivec2", luaopen_vec<nv::ivec2>, 1);
+	nlua_requiref(L, "ivec3", luaopen_vec<nv::ivec3>, 1);
+	nlua_requiref(L, "ivec4", luaopen_vec<nv::ivec4>, 1);
+	nlua_requiref(L, "vec2", luaopen_vec<nv::vec2>, 1);
+	nlua_requiref(L, "vec3", luaopen_vec<nv::vec3>, 1);
+	nlua_requiref(L, "vec4", luaopen_vec<nv::vec4>, 1);
 	lua_settop( L, stack );
 }
Index: trunk/src/lua/lua_nova.cc
===================================================================
--- trunk/src/lua/lua_nova.cc	(revision 489)
+++ trunk/src/lua/lua_nova.cc	(revision 490)
@@ -52,8 +52,8 @@
 static bool nova_check_type_raw( lua_State * L, int iid, int ifield, int ivalue, int itype )
 {
-	iid    = lua_absindex( L, iid );
-	ifield = lua_absindex( L, ifield );
-	ivalue = lua_absindex( L, ivalue );
-	itype  = lua_absindex( L, itype );
+	iid    = nlua_absindex( L, iid );
+	ifield = nlua_absindex( L, ifield );
+	ivalue = nlua_absindex( L, ivalue );
+	itype  = nlua_absindex( L, itype );
 
 	switch ( lua_type( L, itype ) )
@@ -101,8 +101,8 @@
 static void nova_apply_blueprint_values_raw( lua_State * L, int ibase, int iproto, int iset, int iid )
 {
-	ibase  = lua_absindex( L, ibase );
-	iproto = lua_absindex( L, iproto );
-	iset   = lua_absindex( L, iset );
-	iid    = lua_absindex( L, iid );
+	ibase  = nlua_absindex( L, ibase );
+	iproto = nlua_absindex( L, iproto );
+	iset   = nlua_absindex( L, iset );
+	iid    = nlua_absindex( L, iid );
 
 	lua_pushnil( L );
@@ -110,6 +110,6 @@
 	{
 		// Key -2, Value -1
-		int ikey   = lua_absindex( L, -2 );
-		int ivalue = lua_absindex( L, -1 );
+		int ikey   = nlua_absindex( L, -2 );
+		int ivalue = nlua_absindex( L, -1 );
 
 		// Base[Key]
@@ -202,9 +202,9 @@
 static void nova_apply_blueprint_raw( lua_State * L, int ibase, int iproto, int iid )
 {
-	ibase    = lua_absindex( L, ibase );
-	iproto   = lua_absindex( L, iproto );
-	iid      = lua_absindex( L, iid );
+	ibase    = nlua_absindex( L, ibase );
+	iproto   = nlua_absindex( L, iproto );
+	iid      = nlua_absindex( L, iid );
 	nlua_tokeyset( L, ibase );
-	int iset = lua_absindex( L, -1 );
+	int iset = nlua_absindex( L, -1 );
 
 	nova_apply_blueprint_values_raw( L, ibase, iproto, iset, iid );
@@ -344,5 +344,5 @@
 	if ( lua_gettop( L ) == 1 ) lua_pushboolean( L, false );
 	lua_settop( L, 2 );
-	lua_pushglobaltable( L );
+	nlua_pushglobaltable( L );
 	lua_insert( L, 1 );
 	lua_rawset( L, -3 );
@@ -483,5 +483,5 @@
 	if ( lua_type( L, -1 ) == LUA_TSTRING )
 	{
-		lua_pushglobaltable( L );
+		nlua_pushglobaltable( L );
 		lua_pushvalue( L, -2 );
 		lua_rawget( L, -2 );
@@ -680,5 +680,5 @@
 	lua_settop( L, 3 );
 
-	lua_pushglobaltable( L );
+	nlua_pushglobaltable( L );
 	lua_pushvalue( L, 1 );
 	lua_rawget( L, -2 );
@@ -731,5 +731,5 @@
 	}
 	lua_settop( L, 3 );
-	lua_pushglobaltable( L );
+	nlua_pushglobaltable( L );
 
 	lua_pushvalue( L, 1 );
@@ -806,5 +806,5 @@
 	lua_setfield( L, LUA_REGISTRYINDEX, NV_BLUEPRINTS );
 
-	luaL_requiref( L, "nova", luaopen_nova, 1 );
+	nlua_requiref( L, "nova", luaopen_nova, 1 );
 	lua_settop( L, stack );
 }
Index: trunk/src/lua/lua_path.cc
===================================================================
--- trunk/src/lua/lua_path.cc	(revision 489)
+++ trunk/src/lua/lua_path.cc	(revision 490)
@@ -49,5 +49,5 @@
 {
 	if (m_count == 0) return false;
-	if (global) lua_pushglobaltable( L );
+	if (global) nlua_pushglobaltable( L );
 	for ( uint32 i = 0; i < m_count; ++i )
 	{
@@ -60,5 +60,5 @@
 			else
 			{
-				lua_pushunsigned( L, m_elements[i].value );
+				nlua_pushunsigned( L, m_elements[i].value );
 			}
 			lua_gettable( L, -2 );
Index: trunk/src/lua/lua_raw.cc
===================================================================
--- trunk/src/lua/lua_raw.cc	(revision 489)
+++ trunk/src/lua/lua_raw.cc	(revision 490)
@@ -38,6 +38,6 @@
 void nlua_pushreversed( lua_State *L, int index )
 {
-	index = lua_absindex( L, index );
-	int len = static_cast<int>( lua_rawlen( L, index ) );
+	index = nlua_absindex( L, index );
+	int len = static_cast<int>( nlua_rawlen( L, index ) );
 	int i   = len;
 	lua_createtable( L, len, 0 );
@@ -53,5 +53,5 @@
 void nlua_shallowcopy( lua_State *L, int index )
 {
-	index = lua_absindex( L, index );
+	index = nlua_absindex( L, index );
 	lua_createtable( L, 0, 0 );
 	lua_pushnil( L );
@@ -67,5 +67,5 @@
 void nlua_shallowicopy( lua_State *L, int index )
 {
-	index = lua_absindex( L, index );
+	index = nlua_absindex( L, index );
 	lua_createtable( L, 0, 0 );
 	int i = 0;
@@ -85,5 +85,5 @@
 void nlua_shallowmerge( lua_State *L, int index )
 {
-	index = lua_absindex( L, index );
+	index = nlua_absindex( L, index );
 	lua_pushnil( L );
 
@@ -98,5 +98,5 @@
 void nlua_deepcopy( lua_State *L, int index )
 {
-	index = lua_absindex( L, index );
+	index = nlua_absindex( L, index );
 	lua_createtable( L, 0, 0 );
 	lua_pushnil( L );
@@ -118,5 +118,5 @@
 void nlua_toset( lua_State *L, int index )
 {
-	index = lua_absindex( L, index );
+	index = nlua_absindex( L, index );
 	lua_createtable( L, 0, 0 );
 	int i = 0;
@@ -137,5 +137,5 @@
 void nlua_tokeyset( lua_State *L, int index )
 {
-	index = lua_absindex( L, index );
+	index = nlua_absindex( L, index );
 	lua_createtable( L, 0, 0 );
 	lua_pushnil( L );
@@ -151,5 +151,5 @@
 void nlua_register( lua_State *L, const char* fname, lua_CFunction func, int index )
 {
-	index = lua_absindex( L, index );
+	index = nlua_absindex( L, index );
 	lua_pushstring( L, fname );
 	lua_pushcfunction( L, func );
@@ -159,5 +159,5 @@
 void nlua_register( lua_State *L, const luaL_Reg *l, int index )
 {
-	index = lua_absindex( L, index );
+	index = nlua_absindex( L, index );
 	for (; l->name != NULL; l++) 
 	{
@@ -200,5 +200,5 @@
 void nlua_toflags( lua_State *L, int index, nv::uint8* data, nv::uint32 count )
 {
-	index = lua_absindex( L, index );
+	index = nlua_absindex( L, index );
 	nv::uint32 flag;
 	if ( lua_istable( L, index ) )
@@ -222,5 +222,5 @@
 void nlua_toflags_set( lua_State *L, int index, nv::uint8* data, nv::uint32 count )
 {
-	index = lua_absindex( L, index );
+	index = nlua_absindex( L, index );
 	nv::uint32 flag;
 	if ( lua_istable( L, index ) )
@@ -241,5 +241,5 @@
 void nlua_toflags_array( lua_State *L, int index, nv::uint8* data, nv::uint32 count )
 {
-	index = lua_absindex( L, index );
+	index = nlua_absindex( L, index );
 	nv::uint32 flag;
 	if ( lua_istable( L, index ) )
@@ -260,5 +260,5 @@
 nv::vector<nv::uint8> nlua_tobytearray( lua_State *L, int index )
 {
-	index = lua_absindex( L, index );
+	index = nlua_absindex( L, index );
 	nv::vector<nv::uint8> result;
 	if ( lua_istable( L, index ) )
@@ -273,2 +273,52 @@
 	return result;
 }
+
+void * nlua_testudata( lua_State *L, int ud, const char *tname )
+{
+	void *p = lua_touserdata( L, ud );
+	if ( p != NULL )
+	{
+		if ( lua_getmetatable( L, ud ) )
+		{
+			luaL_getmetatable( L, tname );
+			if ( !lua_rawequal( L, -1, -2 ) )
+				p = NULL;
+			lua_pop( L, 2 );
+			return p;
+		}
+	}
+	return NULL;
+}
+
+void nlua_setmetatable( lua_State *L, const char *tname )
+{
+	int only_works_for_51;
+	luaL_getmetatable( L, tname );
+	lua_setmetatable( L, -2 );
+}
+
+void nlua_requiref( lua_State *L, const char *modname, lua_CFunction openf, int glb )
+{
+	int only_works_for_51;
+	lua_pushcfunction( L, openf );
+	lua_pushstring( L, modname );
+	lua_call( L, 1, 1 );
+	if ( glb != 0 )
+	{
+		lua_pushvalue( L, LUA_GLOBALSINDEX );
+		lua_pushvalue( L, -2 );
+		lua_setfield( L, -2, modname );
+		lua_pop( L, 1 );
+	}
+}
+
+int nlua_rawlen( lua_State* L, int index )
+{
+	return lua_objlen( L, index );
+}
+
+void nlua_pushglobaltable( lua_State* L )
+{
+	int only_works_for_51;
+	lua_pushvalue( L, LUA_GLOBALSINDEX );
+}
Index: trunk/src/lua/lua_state.cc
===================================================================
--- trunk/src/lua/lua_state.cc	(revision 489)
+++ trunk/src/lua/lua_state.cc	(revision 490)
@@ -67,5 +67,5 @@
 void nv::lua::state_wrapper::push_global_table()
 {
-	lua_pushglobaltable( m_state );
+	nlua_pushglobaltable( m_state );
 }
 
@@ -173,5 +173,5 @@
 nv::size_t lua::table_guard::get_size()
 {
-	return lua_rawlen( m_state, -1 );
+	return nlua_rawlen( m_state, -1 );
 }
 
@@ -272,5 +272,5 @@
 {
 	lua_getfield( m_state, -1, element.data() );
-	char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && lua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval;
+	char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && nlua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval;
 	lua_pop( m_state, 1 );
 	return result;
@@ -288,5 +288,5 @@
 {
 	lua_getfield( m_state, -1, element.data() );
-	unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tounsigned( m_state, -1 ) : defval;
+	unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? nlua_tounsigned( m_state, -1 ) : defval;
 	lua_pop( m_state, 1 );
 	return result;
@@ -364,5 +364,5 @@
 
 	lua_pushcfunction(m_state, luaopen_base);
-	lua_pushliteral(m_state, LUA_TABLIBNAME);
+	lua_pushliteral(m_state, "");
 	lua_call(m_state, 1, 0);
 
@@ -524,5 +524,5 @@
 void lua::state::deep_pointer_copy( int index, void* obj )
 {
-	index = lua_absindex( m_state, index );
+	index = nlua_absindex( m_state, index );
 	lua_newtable( m_state );
 	lua_pushnil( m_state );
Index: trunk/src/lua/lua_values.cc
===================================================================
--- trunk/src/lua/lua_values.cc	(revision 489)
+++ trunk/src/lua/lua_values.cc	(revision 490)
@@ -20,10 +20,10 @@
 bool nv::lua::detail::is_userdata( lua_State *L, int index, const char* metatable )
 {
-	return luaL_testudata( L, index, metatable ) != 0;
+	return nlua_testudata( L, index, metatable ) != 0;
 }
 
 void* nv::lua::detail::raw_check_userdata( lua_State *L, int index, const char* metatable )
 {
-	return luaL_testudata( L, index, metatable );
+	return nlua_testudata( L, index, metatable );
 }
 
@@ -31,5 +31,5 @@
 {
 	void* result = lua_newuserdata(L, size);
-	luaL_setmetatable( L, metatable );
+	nlua_setmetatable( L, metatable );
 	return result;
 }
@@ -47,5 +47,5 @@
 void nv::lua::detail::push_unsigned( lua_State *L, lunsigned v )
 {
-	lua_pushunsigned( L, v );
+	nlua_pushunsigned( L, v );
 }
 
@@ -83,5 +83,5 @@
 lunsigned   nv::lua::detail::to_unsigned( lua_State *L, int index )
 {
-	return lua_tounsigned( L, index );
+	return nlua_tounsigned( L, index );
 }
 
@@ -131,5 +131,5 @@
 lunsigned   nv::lua::detail::to_unsigned( lua_State *L, int index, lunsigned def )
 {
-	return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tounsigned( L, index ) : def );
+	return ( lua_type( L, index ) == LUA_TNUMBER ? nlua_tounsigned( L, index ) : def );
 }
 
