From 6aeb8076e4c6257124870483f104ce0db68631e0 Mon Sep 17 00:00:00 2001 From: Andrii Nemchenko <62670490+andriyndev@users.noreply.github.com> Date: Mon, 30 Dec 2024 19:57:46 +0200 Subject: [PATCH 1/4] Fix a bug when entity's location might not be updated in case of server crash --- src/serverenvironment.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/serverenvironment.cpp b/src/serverenvironment.cpp index 04104d517ea7b..d579d7dda1080 100644 --- a/src/serverenvironment.cpp +++ b/src/serverenvironment.cpp @@ -2161,11 +2161,13 @@ void ServerEnvironment::deactivateFarObjects(const bool _force_delete) v3s16 blockpos_o = getNodeBlockPos(floatToInt(objectpos, BS)); // If object's static data is stored in a deactivated block and object - // is actually located in an active block, re-save to the block in - // which the object is actually located in. + // is actually located in an active block, or the distance between stored + // and actual location of the object, re-save to the block in which + // the object is actually located in. if (!force_delete && obj->isStaticAllowed() && obj->m_static_exists && - !m_active_blocks.contains(obj->m_static_block) && - m_active_blocks.contains(blockpos_o)) { + m_active_blocks.contains(blockpos_o) && + (!m_active_blocks.contains(obj->m_static_block) || + blockpos_o.getDistanceFromSQ(obj->m_static_block) >= MAP_BLOCKSIZE * MAP_BLOCKSIZE)) { // Delete from block where object was located deleteStaticFromBlock(obj, id, MOD_REASON_STATIC_DATA_REMOVED, false); From b539a6395d19cab779d93ed930459dfc2d240f7e Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 5 Jan 2025 14:55:01 +0200 Subject: [PATCH 2/4] Fix the size --- src/serverenvironment.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/serverenvironment.cpp b/src/serverenvironment.cpp index d579d7dda1080..d90f1b13f20ab 100644 --- a/src/serverenvironment.cpp +++ b/src/serverenvironment.cpp @@ -2167,7 +2167,7 @@ void ServerEnvironment::deactivateFarObjects(const bool _force_delete) if (!force_delete && obj->isStaticAllowed() && obj->m_static_exists && m_active_blocks.contains(blockpos_o) && (!m_active_blocks.contains(obj->m_static_block) || - blockpos_o.getDistanceFromSQ(obj->m_static_block) >= MAP_BLOCKSIZE * MAP_BLOCKSIZE)) { + blockpos_o.getDistanceFromSQ(obj->m_static_block) >= 2 * 2)) { // Delete from block where object was located deleteStaticFromBlock(obj, id, MOD_REASON_STATIC_DATA_REMOVED, false); From 02317d3ef88954020cc6ed43a33bc2a5f3b7b0c3 Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 5 Jan 2025 20:23:21 +0200 Subject: [PATCH 3/4] Clarify comment --- src/serverenvironment.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/serverenvironment.cpp b/src/serverenvironment.cpp index d90f1b13f20ab..e92b3338fbc10 100644 --- a/src/serverenvironment.cpp +++ b/src/serverenvironment.cpp @@ -2161,9 +2161,10 @@ void ServerEnvironment::deactivateFarObjects(const bool _force_delete) v3s16 blockpos_o = getNodeBlockPos(floatToInt(objectpos, BS)); // If object's static data is stored in a deactivated block and object - // is actually located in an active block, or the distance between stored - // and actual location of the object, re-save to the block in which - // the object is actually located in. + // is actually located in an active block, or if the object is actually located + // in an active block which is not neighboring to the block in which + // the object's static data is stored (there is at least one block between + // them), re-save to the block in which the object is actually located in. if (!force_delete && obj->isStaticAllowed() && obj->m_static_exists && m_active_blocks.contains(blockpos_o) && (!m_active_blocks.contains(obj->m_static_block) || From c3d73e701eb356a1d19d18a562d29b9228053e41 Mon Sep 17 00:00:00 2001 From: Andrii Date: Wed, 8 Jan 2025 00:01:57 +0200 Subject: [PATCH 4/4] Fix according to comments --- src/serverenvironment.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/serverenvironment.cpp b/src/serverenvironment.cpp index e92b3338fbc10..7e498034128aa 100644 --- a/src/serverenvironment.cpp +++ b/src/serverenvironment.cpp @@ -43,6 +43,8 @@ // A number that is much smaller than the timeout for particle spawners should/could ever be #define PARTICLE_SPAWNER_NO_EXPIRY -1024.f +#define ACTIVE_OBJECT_RESAVE_DISTANCE_SQ 2 * 2 + /* ABMWithState */ @@ -2160,15 +2162,14 @@ void ServerEnvironment::deactivateFarObjects(const bool _force_delete) // The block in which the object resides in v3s16 blockpos_o = getNodeBlockPos(floatToInt(objectpos, BS)); - // If object's static data is stored in a deactivated block and object - // is actually located in an active block, or if the object is actually located - // in an active block which is not neighboring to the block in which - // the object's static data is stored (there is at least one block between - // them), re-save to the block in which the object is actually located in. + // If object's static data is stored in a deactivated block or it has moved a bunch + // then re-save to the block in which the object is now located in. + // This only applies if the object is in a currently active block, since deactivating + // is handled by the code further below. if (!force_delete && obj->isStaticAllowed() && obj->m_static_exists && m_active_blocks.contains(blockpos_o) && (!m_active_blocks.contains(obj->m_static_block) || - blockpos_o.getDistanceFromSQ(obj->m_static_block) >= 2 * 2)) { + blockpos_o.getDistanceFromSQ(obj->m_static_block) >= ACTIVE_OBJECT_RESAVE_DISTANCE_SQ)) { // Delete from block where object was located deleteStaticFromBlock(obj, id, MOD_REASON_STATIC_DATA_REMOVED, false);