From c9144ae5e22ee041fed2512cd3055608c6e9a4bc Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Sun, 30 May 2021 20:24:12 +0200 Subject: Add core.compare_block_status function (#11247) Makes it possible to check the status of the mapblock in a future-extensible way. --- src/emerge.cpp | 7 +++++++ src/emerge.h | 2 ++ src/map.cpp | 5 +++++ src/map.h | 2 ++ src/mapblock.h | 2 +- src/script/lua_api/l_env.cpp | 30 +++++++++++++++++++++++++++++- src/script/lua_api/l_env.h | 6 +++++- src/serverenvironment.cpp | 15 +++++++++++++++ src/serverenvironment.h | 11 ++++++++++- 9 files changed, 76 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/emerge.cpp b/src/emerge.cpp index 32e7d9f24..3a2244d7e 100644 --- a/src/emerge.cpp +++ b/src/emerge.cpp @@ -358,6 +358,13 @@ bool EmergeManager::enqueueBlockEmergeEx( } +bool EmergeManager::isBlockInQueue(v3s16 pos) +{ + MutexAutoLock queuelock(m_queue_mutex); + return m_blocks_enqueued.find(pos) != m_blocks_enqueued.end(); +} + + // // Mapgen-related helper functions // diff --git a/src/emerge.h b/src/emerge.h index aac3e7dd3..b060226f8 100644 --- a/src/emerge.h +++ b/src/emerge.h @@ -174,6 +174,8 @@ public: EmergeCompletionCallback callback, void *callback_param); + bool isBlockInQueue(v3s16 pos); + v3s16 getContainingChunk(v3s16 blockpos); Mapgen *getCurrentMapgen(); diff --git a/src/map.cpp b/src/map.cpp index 7c59edbaa..641287c3d 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -1549,6 +1549,11 @@ MapBlock *ServerMap::getBlockOrEmerge(v3s16 p3d) return block; } +bool ServerMap::isBlockInQueue(v3s16 pos) +{ + return m_emerge && m_emerge->isBlockInQueue(pos); +} + // N.B. This requires no synchronization, since data will not be modified unless // the VoxelManipulator being updated belongs to the same thread. void ServerMap::updateVManip(v3s16 pos) diff --git a/src/map.h b/src/map.h index e68795c4a..8d20c4a44 100644 --- a/src/map.h +++ b/src/map.h @@ -365,6 +365,8 @@ public: */ MapBlock *getBlockOrEmerge(v3s16 p3d); + bool isBlockInQueue(v3s16 pos); + /* Database functions */ diff --git a/src/mapblock.h b/src/mapblock.h index 7b82301e9..2e3eb0d76 100644 --- a/src/mapblock.h +++ b/src/mapblock.h @@ -140,7 +140,7 @@ public: //// Flags //// - inline bool isDummy() + inline bool isDummy() const { return !data; } diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 39c229ee4..98f8861fa 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -46,13 +46,22 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "client/client.h" #endif -struct EnumString ModApiEnvMod::es_ClearObjectsMode[] = +const EnumString ModApiEnvMod::es_ClearObjectsMode[] = { {CLEAR_OBJECTS_MODE_FULL, "full"}, {CLEAR_OBJECTS_MODE_QUICK, "quick"}, {0, NULL}, }; +const EnumString ModApiEnvMod::es_BlockStatusType[] = +{ + {ServerEnvironment::BS_UNKNOWN, "unknown"}, + {ServerEnvironment::BS_EMERGING, "emerging"}, + {ServerEnvironment::BS_LOADED, "loaded"}, + {ServerEnvironment::BS_ACTIVE, "active"}, + {0, NULL}, +}; + /////////////////////////////////////////////////////////////////////////////// @@ -1389,6 +1398,24 @@ int ModApiEnvMod::l_forceload_block(lua_State *L) return 0; } +// compare_block_status(nodepos) +int ModApiEnvMod::l_compare_block_status(lua_State *L) +{ + GET_ENV_PTR; + + v3s16 nodepos = check_v3s16(L, 1); + std::string condition_s = luaL_checkstring(L, 2); + auto status = env->getBlockStatus(getNodeBlockPos(nodepos)); + + int condition_i = -1; + if (!string_to_enum(es_BlockStatusType, condition_i, condition_s)) + return 0; // Unsupported + + lua_pushboolean(L, status >= condition_i); + return 1; +} + + // forceload_free_block(blockpos) // blockpos = {x=num, y=num, z=num} int ModApiEnvMod::l_forceload_free_block(lua_State *L) @@ -1462,6 +1489,7 @@ void ModApiEnvMod::Initialize(lua_State *L, int top) API_FCT(transforming_liquid_add); API_FCT(forceload_block); API_FCT(forceload_free_block); + API_FCT(compare_block_status); API_FCT(get_translated_string); } diff --git a/src/script/lua_api/l_env.h b/src/script/lua_api/l_env.h index 42c2d64f8..979b13c40 100644 --- a/src/script/lua_api/l_env.h +++ b/src/script/lua_api/l_env.h @@ -195,6 +195,9 @@ private: // stops forceloading a position static int l_forceload_free_block(lua_State *L); + // compare_block_status(nodepos) + static int l_compare_block_status(lua_State *L); + // Get a string translated server side static int l_get_translated_string(lua_State * L); @@ -207,7 +210,8 @@ public: static void Initialize(lua_State *L, int top); static void InitializeClient(lua_State *L, int top); - static struct EnumString es_ClearObjectsMode[]; + static const EnumString es_ClearObjectsMode[]; + static const EnumString es_BlockStatusType[]; }; class LuaABM : public ActiveBlockModifier { diff --git a/src/serverenvironment.cpp b/src/serverenvironment.cpp index 3d9ba132b..413a785e6 100644 --- a/src/serverenvironment.cpp +++ b/src/serverenvironment.cpp @@ -1542,6 +1542,21 @@ void ServerEnvironment::step(float dtime) m_server->sendDetachedInventories(PEER_ID_INEXISTENT, true); } +ServerEnvironment::BlockStatus ServerEnvironment::getBlockStatus(v3s16 blockpos) +{ + if (m_active_blocks.contains(blockpos)) + return BS_ACTIVE; + + const MapBlock *block = m_map->getBlockNoCreateNoEx(blockpos); + if (block && !block->isDummy()) + return BS_LOADED; + + if (m_map->isBlockInQueue(blockpos)) + return BS_EMERGING; + + return BS_UNKNOWN; +} + u32 ServerEnvironment::addParticleSpawner(float exptime) { // Timers with lifetime 0 do not expire diff --git a/src/serverenvironment.h b/src/serverenvironment.h index a11c814ed..c5ca463ee 100644 --- a/src/serverenvironment.h +++ b/src/serverenvironment.h @@ -342,7 +342,16 @@ public: void reportMaxLagEstimate(float f) { m_max_lag_estimate = f; } float getMaxLagEstimate() { return m_max_lag_estimate; } - std::set* getForceloadedBlocks() { return &m_active_blocks.m_forceloaded_list; }; + std::set* getForceloadedBlocks() { return &m_active_blocks.m_forceloaded_list; } + + // Sorted by how ready a mapblock is + enum BlockStatus { + BS_UNKNOWN, + BS_EMERGING, + BS_LOADED, + BS_ACTIVE // always highest value + }; + BlockStatus getBlockStatus(v3s16 blockpos); // Sets the static object status all the active objects in the specified block // This is only really needed for deleting blocks from the map -- cgit v1.2.3