Code Cleanup

This commit is contained in:
ExeVirus 2024-06-11 14:53:52 -04:00
parent 7e504cbd37
commit 42fdaa9838

@ -26,14 +26,15 @@ namespace server
// all inserted entires go into the uncached vector // all inserted entires go into the uncached vector
void SpatialMap::insert(u16 id, const v3f &pos, bool postIteration) void SpatialMap::insert(u16 id, const v3f &pos, bool postIteration)
{ {
if(!m_iterators_stopping_insertion_and_deletion) { if (m_iterators_stopping_insertion_and_deletion) {
m_pending_inserts.insert(SpatialKey(pos, id));
return;
}
SpatialKey key(pos); SpatialKey key(pos);
if (postIteration) if (postIteration)
key = SpatialKey(pos.X, pos.Y, pos.Z, true); key = SpatialKey(pos.X, pos.Y, pos.Z, true);
m_cached.insert({key, id}); m_cached.insert({key, id});
} else {
m_pending_inserts.insert(SpatialKey(pos,id));
}
} }
// Invalidates upon position update // Invalidates upon position update
@ -53,7 +54,11 @@ void SpatialMap::updatePosition(u16 id, const v3f &oldPos, const v3f &newPos)
void SpatialMap::remove(u16 id, const v3f &pos, bool postIteration) void SpatialMap::remove(u16 id, const v3f &pos, bool postIteration)
{ {
if(!m_iterators_stopping_insertion_and_deletion) { if (m_iterators_stopping_insertion_and_deletion) {
m_pending_deletes.insert(SpatialKey(pos, id));
return;
}
SpatialKey key(pos); SpatialKey key(pos);
if (postIteration) if (postIteration)
key = SpatialKey(pos.X, pos.Y, pos.Z, true); key = SpatialKey(pos.X, pos.Y, pos.Z, true);
@ -66,40 +71,40 @@ void SpatialMap::remove(u16 id, const v3f &pos, bool postIteration)
} }
} }
} }
} else {
m_pending_deletes.insert(SpatialKey(pos, id));
return;
}
remove(id); // should never be hit remove(id); // should never be hit
} }
void SpatialMap::remove(u16 id) void SpatialMap::remove(u16 id)
{ {
if(!m_iterators_stopping_insertion_and_deletion) { if(m_iterators_stopping_insertion_and_deletion) {
m_pending_deletes.insert(SpatialKey(v3f(), id));
return;
}
for (auto it = m_cached.begin(); it != m_cached.end(); ++it) { for (auto it = m_cached.begin(); it != m_cached.end(); ++it) {
if (it->second == id) { if (it->second == id) {
m_cached.erase(it); m_cached.erase(it);
break; // Erase and leave early return; // Erase and leave early
} }
} }
} else {
m_pending_deletes.insert(SpatialKey(v3f(), id));
}
} }
void SpatialMap::removeAll() void SpatialMap::removeAll()
{ {
if(!m_iterators_stopping_insertion_and_deletion) { if(m_iterators_stopping_insertion_and_deletion) {
m_cached.clear();
} else {
m_remove_all = true; m_remove_all = true;
} else {
m_cached.clear();
} }
} }
void SpatialMap::getRelevantObjectIds(const aabb3f &box, const std::function<void(u16 id)> &callback) void SpatialMap::getRelevantObjectIds(const aabb3f &box, const std::function<void(u16 id)> &callback)
{ {
if(!m_cached.empty()) { if (m_cached.empty()) return;
// when searching, we must round to maximum extent of relevant mapblock indexes
// When searching, we must round to maximum extent of relevant mapblock indexes.
// Since we're using floats, always assume +-1
auto low = [](f32 val) -> s16 { auto low = [](f32 val) -> s16 {
s16 _val = static_cast<s16>(val / BS); s16 _val = static_cast<s16>(val / BS);
return (_val >> 4) - 1; return (_val >> 4) - 1;
@ -143,11 +148,12 @@ void SpatialMap::getRelevantObjectIds(const aabb3f &box, const std::function<voi
handleInsertsAndDeletes(); handleInsertsAndDeletes();
} }
} }
}
void SpatialMap::handleInsertsAndDeletes() void SpatialMap::handleInsertsAndDeletes()
{ {
if(!m_iterators_stopping_insertion_and_deletion) { if (m_iterators_stopping_insertion_and_deletion)
return;
if(!m_remove_all) { if(!m_remove_all) {
for (auto key : m_pending_deletes) { for (auto key : m_pending_deletes) {
remove(key.padding_or_optional_id, v3f(key.x, key.y, key.z), true); remove(key.padding_or_optional_id, v3f(key.x, key.y, key.z), true);
@ -162,6 +168,5 @@ void SpatialMap::handleInsertsAndDeletes()
m_pending_inserts.clear(); m_pending_inserts.clear();
m_pending_deletes.clear(); m_pending_deletes.clear();
} }
}
} // namespace server } // namespace server