Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not emerge blocks in the active_object_send_range_blocks range #14152

Merged
merged 3 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 35 additions & 14 deletions src/serverenvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,14 @@ void ActiveBlockList::update(std::vector<PlayerSAO*> &active_players,
s16 active_block_range,
s16 active_object_range,
std::set<v3s16> &blocks_removed,
std::set<v3s16> &blocks_added)
std::set<v3s16> &blocks_added,
std::set<v3s16> &extra_blocks_added)
{
/*
Create the new list
*/
std::set<v3s16> newlist = m_forceloaded_list;
std::set<v3s16> extralist;
m_abm_list = m_forceloaded_list;
for (const PlayerSAO *playersao : active_players) {
v3s16 pos = getNodeBlockPos(floatToInt(playersao->getBasePosition(), BS));
Expand All @@ -360,29 +362,36 @@ void ActiveBlockList::update(std::vector<PlayerSAO*> &active_players,
playersao->getEyePosition(),
camera_dir,
playersao->getFov(),
newlist);
extralist);
}
}

/*
Find out which blocks on the old list are not on the new list
*/
// Go through old list
for (v3s16 p : m_list) {
// If not on new list, it's been removed
if (newlist.find(p) == newlist.end())
blocks_removed.insert(p);
}

/*
Find out which blocks on the new list are not on the old list
*/
// Go through new list
for (v3s16 p : newlist) {
// also remove duplicate blocks from the extra list
extralist.erase(p);
// If not on old list, it's been added
if (m_list.find(p) == m_list.end())
blocks_added.insert(p);
}
/*
Find out which blocks on the extra list are not on the old list
*/
for (v3s16 p : extralist) {
// also make sure newlist has all blocks
newlist.insert(p);
// If not on old list, it's been added
if (m_list.find(p) == m_list.end())
extra_blocks_added.insert(p);
}

/*
Find out which blocks on the old list are not on the new + extra list
*/
std::set_difference(m_list.begin(), m_list.end(), newlist.begin(), newlist.end(),
std::inserter(blocks_removed, blocks_removed.end()));

Copy link
Member

@sfan5 sfan5 Dec 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exhaustively testing would be a waste but I'd do something like this to maybe catch the case where someone messes it up again in the future:

assert(newlist.size() >= extralist.size());
assert(blocks_removed.size() <= m_list.size());
if (!blocks_added.empty())
  assert(newlist.count(*blocks_added.begin()) > 0);
if (!extra_blocks_added.empty()) {
  assert(newlist.count(*extra_blocks_added.begin()) > 0);
  assert(extralist.count(*extra_blocks_added.begin()) > 0);
  assert(blocks_added.count(*extra_blocks_added.begin()) == 0);
}
if (!blocks_removed.empty()) {
  assert(newlist.count(*blocks_removed.begin()) == 0);
  assert(extralist.count(*blocks_removed.begin()) == 0);
  assert(m_list.count(*blocks_removed.begin()) > 0);
}

(should compile to no-op if NDEBUG)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you mean ... count(*....begin() ...

Copy link
Member

@sfan5 sfan5 Dec 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, sure

edit: also added two more

/*
Update m_list
Expand Down Expand Up @@ -1403,8 +1412,9 @@ void ServerEnvironment::step(float dtime)
g_settings->getS16("active_block_range");
std::set<v3s16> blocks_removed;
std::set<v3s16> blocks_added;
std::set<v3s16> extra_blocks_added;
m_active_blocks.update(players, active_block_range, active_object_range,
blocks_removed, blocks_added);
blocks_removed, blocks_added, extra_blocks_added);

/*
Handle removed blocks
Expand Down Expand Up @@ -1440,6 +1450,17 @@ void ServerEnvironment::step(float dtime)
activateBlock(block);
}

for (const v3s16 &p: extra_blocks_added) {
// only activate if the block is already loaded
MapBlock *block = m_map->getBlockNoCreateNoEx(p);
if (!block) {
m_active_blocks.remove(p);
continue;
}

activateBlock(block);
}

// Some blocks may be removed again by the code above so do this here
m_active_block_gauge->set(m_active_blocks.size());

Expand Down
3 changes: 2 additions & 1 deletion src/serverenvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ class ActiveBlockList
s16 active_block_range,
s16 active_object_range,
std::set<v3s16> &blocks_removed,
std::set<v3s16> &blocks_added);
std::set<v3s16> &blocks_added,
std::set<v3s16> &extra_blocks_added);

bool contains(v3s16 p) const {
return (m_list.find(p) != m_list.end());
Expand Down