diff --git a/src/lua_terrainlib.c b/src/lua_terrainlib.c index 25aaaf706..3ffe504dc 100644 --- a/src/lua_terrainlib.c +++ b/src/lua_terrainlib.c @@ -222,6 +222,75 @@ static int splash_num(lua_State *L) return 1; } +static int lib_iterateSplashes(lua_State *L) +{ + size_t i; + + if (lua_gettop(L) < 2) + { + lua_pushcfunction(L, lib_iterateSplashes); + return 1; + } + + lua_settop(L, 2); + lua_remove(L, 1); // state is unused. + + if (!lua_isnil(L, 1)) + i = K_GetSplashHeapIndex(*(t_splash_t **)luaL_checkudata(L, 1, META_SPLASH)) + 1; + else + i = 0; + + // terrains are always valid, only added, never removed + if (i < K_GetNumSplashDefs()) + { + LUA_PushUserdata(L, K_GetSplashByIndex(i), META_SPLASH); + return 1; + } + + return 0; +} + +static int lib_getSplash(lua_State *L) +{ + const char *field; + size_t i; + + // find terrain by number + if (lua_type(L, 2) == LUA_TNUMBER) + { + i = luaL_checkinteger(L, 2); + if (i >= K_GetNumSplashDefs()) + return luaL_error(L, "splashes[] index %d out of range (0 - %d)", i, K_GetNumSplashDefs()-1); + LUA_PushUserdata(L, K_GetSplashByIndex(i), META_SPLASH); + return 1; + } + + field = luaL_checkstring(L, 2); + + // special function iterate + if (fastcmp(field,"iterate")) + { + lua_pushcfunction(L, lib_iterateSplashes); + return 1; + } + + // find terrain by name + t_splash_t *byname = K_GetSplashByName(field); + if (byname != NULL) + { + LUA_PushUserdata(L, byname, META_SPLASH); + return 1; + } + + return 0; +} + +static int lib_numSplashes(lua_State *L) +{ + lua_pushinteger(L, K_GetNumSplashDefs()); + return 1; +} + static int footstep_get(lua_State *L) { t_footstep_t *footstep = *((t_footstep_t **)luaL_checkudata(L, 1, META_FOOTSTEP)); @@ -685,6 +754,16 @@ int LUA_TerrainLib(lua_State *L) lua_setfield(L, -2, "__len"); lua_pop(L,1); + lua_newuserdata(L, 0); + lua_createtable(L, 0, 2); + lua_pushcfunction(L, lib_getSplash); + lua_setfield(L, -2, "__index"); + + lua_pushcfunction(L, lib_numSplashes); + lua_setfield(L, -2, "__len"); + lua_setmetatable(L, -2); + lua_setglobal(L, "splashes"); + luaL_newmetatable(L, META_FOOTSTEP); lua_pushcfunction(L, footstep_get); lua_setfield(L, -2, "__index");