From 644d0ab2bb44df8a3aefb387736930150b7d0aed Mon Sep 17 00:00:00 2001 From: paramat Date: Wed, 7 Mar 2018 03:40:35 +0000 Subject: Spawn level: Add 'get_spawn_level(x, z)' API Returns a suitable player spawn y co-ordinate for unmodified terrain. --- src/script/lua_api/l_mapgen.cpp | 22 ++++++++++++++++++++++ src/script/lua_api/l_mapgen.h | 3 +++ 2 files changed, 25 insertions(+) (limited to 'src') diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 11d963b4d..f5c76ec6e 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -757,6 +757,27 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L) } +// get_spawn_level(x = num, z = num) +int ModApiMapgen::l_get_spawn_level(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + s16 x = luaL_checkinteger(L, 1); + s16 z = luaL_checkinteger(L, 2); + + EmergeManager *emerge = getServer(L)->getEmergeManager(); + int spawn_level = emerge->getSpawnLevelAtPoint(v2s16(x, z)); + // Unsuitable spawn point + if (spawn_level == MAX_MAP_GENERATION_LIMIT) + return 0; + + // 'findSpawnPos()' in server.cpp adds at least 1 + lua_pushinteger(L, spawn_level + 1); + + return 1; +} + + int ModApiMapgen::l_get_mapgen_params(lua_State *L) { NO_MAP_LOCK_REQUIRED; @@ -1714,6 +1735,7 @@ void ModApiMapgen::Initialize(lua_State *L, int top) API_FCT(get_humidity); API_FCT(get_biome_data); API_FCT(get_mapgen_object); + API_FCT(get_spawn_level); API_FCT(get_mapgen_params); API_FCT(set_mapgen_params); diff --git a/src/script/lua_api/l_mapgen.h b/src/script/lua_api/l_mapgen.h index 713069633..aebbcbac6 100644 --- a/src/script/lua_api/l_mapgen.h +++ b/src/script/lua_api/l_mapgen.h @@ -44,6 +44,9 @@ private: // returns the requested object used during map generation static int l_get_mapgen_object(lua_State *L); + // get_spawn_level(x = num, z = num) + static int l_get_spawn_level(lua_State *L); + // get_mapgen_params() // returns the currently active map generation parameter set static int l_get_mapgen_params(lua_State *L); -- cgit v1.2.3