From d13b12b791d8a423a9acaeffc44c27edb7cdb95e Mon Sep 17 00:00:00 2001 From: Jude Melton-Houghton Date: Sat, 24 Dec 2022 12:21:59 -0500 Subject: [PATCH] Store `MapEditEvent` blocks in a vector (#13071) --- src/emerge.cpp | 4 +--- src/map.cpp | 15 +++------------ src/map.h | 13 +++++++++++-- src/mapgen/mg_schematic.cpp | 3 +-- src/mapgen/treegen.cpp | 3 +-- src/script/lua_api/l_env.cpp | 5 ++--- src/script/lua_api/l_vmanip.cpp | 3 +-- src/server.cpp | 4 +--- 8 files changed, 21 insertions(+), 29 deletions(-) diff --git a/src/emerge.cpp b/src/emerge.cpp index 34c13cbd5..123b44b54 100644 --- a/src/emerge.cpp +++ b/src/emerge.cpp @@ -725,9 +725,7 @@ void *EmergeThread::run() if (!modified_blocks.empty()) { MapEditEvent event; event.type = MEET_OTHER; - for (const auto &pair : modified_blocks) { - event.modified_blocks.insert(pair.first); - } + event.setModifiedBlocks(modified_blocks); MutexAutoLock envlock(m_server->m_env_mutex); m_map->dispatchEvent(event); } diff --git a/src/map.cpp b/src/map.cpp index 21a562030..cfe5f126d 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -262,10 +262,7 @@ bool Map::addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata) std::map modified_blocks; addNodeAndUpdate(p, n, modified_blocks, remove_metadata); - // Copy modified_blocks to event - for (auto &modified_block : modified_blocks) { - event.modified_blocks.insert(modified_block.first); - } + event.setModifiedBlocks(modified_blocks); } catch(InvalidPositionException &e){ succeeded = false; @@ -287,10 +284,7 @@ bool Map::removeNodeWithEvent(v3s16 p) std::map modified_blocks; removeNodeAndUpdate(p, modified_blocks); - // Copy modified_blocks to event - for (auto &modified_block : modified_blocks) { - event.modified_blocks.insert(modified_block.first); - } + event.setModifiedBlocks(modified_blocks); } catch(InvalidPositionException &e){ succeeded = false; @@ -1873,10 +1867,7 @@ MapBlock* ServerMap::loadBlock(v3s16 blockpos) //Modified lighting, send event MapEditEvent event; event.type = MEET_OTHER; - std::map::iterator it; - for (it = modified_blocks.begin(); - it != modified_blocks.end(); ++it) - event.modified_blocks.insert(it->first); + event.setModifiedBlocks(modified_blocks); dispatchEvent(event); } } diff --git a/src/map.h b/src/map.h index e04958871..9a9586fc6 100644 --- a/src/map.h +++ b/src/map.h @@ -74,7 +74,7 @@ struct MapEditEvent MapEditEventType type = MEET_OTHER; v3s16 p; MapNode n = CONTENT_AIR; - std::set modified_blocks; + std::vector modified_blocks; // Represents a set bool is_private_change = false; MapEditEvent() = default; @@ -82,8 +82,17 @@ struct MapEditEvent // Sets the event's position and marks the block as modified. void setPositionModified(v3s16 pos) { + assert(modified_blocks.empty()); // only meant for initialization (once) p = pos; - modified_blocks.insert(getNodeBlockPos(pos)); + modified_blocks.push_back(getNodeBlockPos(pos)); + } + + void setModifiedBlocks(const std::map blocks) + { + assert(modified_blocks.empty()); // only meant for initialization (once) + modified_blocks.reserve(blocks.size()); + for (const auto &block : blocks) + modified_blocks.push_back(block.first); } VoxelArea getArea() const diff --git a/src/mapgen/mg_schematic.cpp b/src/mapgen/mg_schematic.cpp index 8fbbb821e..1e44ae34e 100644 --- a/src/mapgen/mg_schematic.cpp +++ b/src/mapgen/mg_schematic.cpp @@ -273,8 +273,7 @@ void Schematic::placeOnMap(ServerMap *map, v3s16 p, u32 flags, //// Create & dispatch map modification events to observers MapEditEvent event; event.type = MEET_OTHER; - for (it = modified_blocks.begin(); it != modified_blocks.end(); ++it) - event.modified_blocks.insert(it->first); + event.setModifiedBlocks(modified_blocks); map->dispatchEvent(event); } diff --git a/src/mapgen/treegen.cpp b/src/mapgen/treegen.cpp index 653657752..2c1266431 100644 --- a/src/mapgen/treegen.cpp +++ b/src/mapgen/treegen.cpp @@ -137,8 +137,7 @@ treegen::error spawn_ltree(ServerMap *map, v3s16 p0, // Send a MEET_OTHER event MapEditEvent event; event.type = MEET_OTHER; - for (auto &modified_block : modified_blocks) - event.modified_blocks.insert(modified_block.first); + event.setModifiedBlocks(modified_blocks); map->dispatchEvent(event); return SUCCESS; } diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 45d444d55..4a9c9b75e 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -1131,8 +1131,7 @@ int ModApiEnvMod::l_fix_light(lua_State *L) if (!modified_blocks.empty()) { MapEditEvent event; event.type = MEET_OTHER; - for (auto &modified_block : modified_blocks) - event.modified_blocks.insert(modified_block.first); + event.setModifiedBlocks(modified_blocks); map.dispatchEvent(event); } @@ -1238,7 +1237,7 @@ int ModApiEnvMod::l_delete_area(lua_State *L) v3s16 bp(x, y, z); if (map.deleteBlock(bp)) { env->setStaticForActiveObjectsInBlock(bp, false); - event.modified_blocks.insert(bp); + event.modified_blocks.push_back(bp); } else { success = false; } diff --git a/src/script/lua_api/l_vmanip.cpp b/src/script/lua_api/l_vmanip.cpp index f6d11088e..90552a239 100644 --- a/src/script/lua_api/l_vmanip.cpp +++ b/src/script/lua_api/l_vmanip.cpp @@ -128,8 +128,7 @@ int LuaVoxelManip::l_write_to_map(lua_State *L) MapEditEvent event; event.type = MEET_OTHER; - for (const auto &it : modified_blocks) - event.modified_blocks.insert(it.first); + event.setModifiedBlocks(modified_blocks); map->dispatchEvent(event); return 0; diff --git a/src/server.cpp b/src/server.cpp index a20e82856..5c3496569 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -699,9 +699,7 @@ void Server::AsyncRunStep(bool initial_step) if (!modified_blocks.empty()) { MapEditEvent event; event.type = MEET_OTHER; - for (const auto &pair : modified_blocks) { - event.modified_blocks.insert(pair.first); - } + event.setModifiedBlocks(modified_blocks); m_env->getMap().dispatchEvent(event); } }