Skip to content

Commit 21789cc

Browse files
celeron55kwolekr
authored andcommittedNov 17, 2013
Revert patch 58f036a that causes object duplication (which tried to fix objects getting hidden from client) and fix the original problem correctly. This fixes the second distinct object duplication bug.
1 parent b2d9205 commit 21789cc

File tree

1 file changed

+60
-23
lines changed

1 file changed

+60
-23
lines changed
 

‎src/environment.cpp

+60-23
Original file line numberDiff line numberDiff line change
@@ -1417,8 +1417,8 @@ void ServerEnvironment::getAddedActiveObjects(v3s16 pos, s16 radius,
14171417
ServerActiveObject *object = i->second;
14181418
if(object == NULL)
14191419
continue;
1420-
// Discard if removed
1421-
if(object->m_removed)
1420+
// Discard if removed or deactivating
1421+
if(object->m_removed || object->m_pending_deactivation)
14221422
continue;
14231423
if(object->unlimitedTransferDistance() == false){
14241424
// Discard if too far
@@ -1468,7 +1468,7 @@ void ServerEnvironment::getRemovedActiveObjects(v3s16 pos, s16 radius,
14681468
continue;
14691469
}
14701470

1471-
if(object->m_removed)
1471+
if(object->m_removed || object->m_pending_deactivation)
14721472
{
14731473
removed_objects.insert(id);
14741474
continue;
@@ -1556,21 +1556,19 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
15561556
StaticObject s_obj(object->getType(), objectpos, staticdata);
15571557
// Add to the block where the object is located in
15581558
v3s16 blockpos = getNodeBlockPos(floatToInt(objectpos, BS));
1559-
MapBlock *block = m_map->getBlockNoCreateNoEx(blockpos);
1560-
if(block)
1561-
{
1559+
MapBlock *block = m_map->emergeBlock(blockpos);
1560+
if(block){
15621561
block->m_static_objects.m_active[object->getId()] = s_obj;
15631562
object->m_static_exists = true;
15641563
object->m_static_block = blockpos;
15651564

15661565
if(set_changed)
15671566
block->raiseModified(MOD_STATE_WRITE_NEEDED,
15681567
"addActiveObjectRaw");
1569-
}
1570-
else{
1568+
} else {
15711569
v3s16 p = floatToInt(objectpos, BS);
15721570
errorstream<<"ServerEnvironment::addActiveObjectRaw(): "
1573-
<<"could not find block for storing id="<<object->getId()
1571+
<<"could not emerge block for storing id="<<object->getId()
15741572
<<" statically (pos="<<PP(p)<<")"<<std::endl;
15751573
}
15761574
}
@@ -1616,18 +1614,39 @@ void ServerEnvironment::removeRemovedObjects()
16161614
if (block) {
16171615
block->m_static_objects.remove(id);
16181616
block->raiseModified(MOD_STATE_WRITE_NEEDED,
1619-
"removeRemovedObjects");
1617+
"removeRemovedObjects/remove");
16201618
obj->m_static_exists = false;
16211619
} else {
1622-
infostream << "failed to emerge block from which "
1623-
"an object to be removed was loaded from. id="<<id<<std::endl;
1620+
infostream<<"Failed to emerge block from which an object to "
1621+
<<"be removed was loaded from. id="<<id<<std::endl;
16241622
}
16251623
}
16261624

1627-
// If m_known_by_count > 0, don't actually remove.
1625+
// If m_known_by_count > 0, don't actually remove. On some future
1626+
// invocation this will be 0, which is when removal will continue.
16281627
if(obj->m_known_by_count > 0)
16291628
continue;
1630-
1629+
1630+
/*
1631+
Move static data from active to stored if not marked as removed
1632+
*/
1633+
if(obj->m_static_exists && !obj->m_removed){
1634+
MapBlock *block = m_map->emergeBlock(obj->m_static_block, false);
1635+
if (block) {
1636+
std::map<u16, StaticObject>::iterator i =
1637+
block->m_static_objects.m_active.find(id);
1638+
if(i != block->m_static_objects.m_active.end()){
1639+
block->m_static_objects.m_stored.push_back(i->second);
1640+
block->m_static_objects.m_active.erase(id);
1641+
block->raiseModified(MOD_STATE_WRITE_NEEDED,
1642+
"removeRemovedObjects/deactivate");
1643+
}
1644+
} else {
1645+
infostream<<"Failed to emerge block from which an object to "
1646+
<<"be deactivated was loaded from. id="<<id<<std::endl;
1647+
}
1648+
}
1649+
16311650
// Tell the object about removal
16321651
obj->removingFromEnvironment();
16331652
// Deregister in scripting api
@@ -1708,10 +1727,9 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
17081727
"large amount of objects");
17091728
return;
17101729
}
1711-
// A list for objects that couldn't be converted to active for some
1712-
// reason. They will be stored back.
1730+
1731+
// Activate stored objects
17131732
std::list<StaticObject> new_stored;
1714-
// Loop through stored static objects
17151733
for(std::list<StaticObject>::iterator
17161734
i = block->m_static_objects.m_stored.begin();
17171735
i != block->m_static_objects.m_stored.end(); ++i)
@@ -1750,6 +1768,19 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
17501768
StaticObject &s_obj = *i;
17511769
block->m_static_objects.m_stored.push_back(s_obj);
17521770
}
1771+
1772+
// Turn the active counterparts of activated objects not pending for
1773+
// deactivation
1774+
for(std::map<u16, StaticObject>::iterator
1775+
i = block->m_static_objects.m_active.begin();
1776+
i != block->m_static_objects.m_active.end(); ++i)
1777+
{
1778+
u16 id = i->first;
1779+
ServerActiveObject *object = getActiveObject(id);
1780+
assert(object);
1781+
object->m_pending_deactivation = false;
1782+
}
1783+
17531784
/*
17541785
Note: Block hasn't really been modified here.
17551786
The objects have just been activated and moved from the stored
@@ -1910,6 +1941,8 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
19101941
block = m_map->emergeBlock(blockpos);
19111942
} catch(InvalidPositionException &e){
19121943
// Handled via NULL pointer
1944+
// NOTE: emergeBlock's failure is usually determined by it
1945+
// actually returning NULL
19131946
}
19141947

19151948
if(block)
@@ -1923,17 +1956,21 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
19231956
<<" Forcing delete."<<std::endl;
19241957
force_delete = true;
19251958
} else {
1926-
// If static counterpart already exists, remove it first.
1927-
// This shouldn't happen, but happens rarely for some
1928-
// unknown reason. Unsuccessful attempts have been made to
1929-
// find said reason.
1959+
// If static counterpart already exists in target block,
1960+
// remove it first.
1961+
// This shouldn't happen because the object is removed from
1962+
// the previous block before this according to
1963+
// obj->m_static_block, but happens rarely for some unknown
1964+
// reason. Unsuccessful attempts have been made to find
1965+
// said reason.
19301966
if(id && block->m_static_objects.m_active.find(id) != block->m_static_objects.m_active.end()){
19311967
infostream<<"ServerEnv: WARNING: Performing hack #83274"
19321968
<<std::endl;
19331969
block->m_static_objects.remove(id);
19341970
}
1935-
//store static data
1936-
block->m_static_objects.insert(0, s_obj);
1971+
// Store static data
1972+
u16 store_id = pending_delete ? id : 0;
1973+
block->m_static_objects.insert(store_id, s_obj);
19371974

19381975
// Only mark block as modified if data changed considerably
19391976
if(shall_be_written)

0 commit comments

Comments
 (0)
Please sign in to comment.