Skip to content

Commit

Permalink
Avoid resending near blocks unnecessarily.
Browse files Browse the repository at this point in the history
  • Loading branch information
lhofhansl committed Oct 27, 2020
1 parent 61a1963 commit 68cd93b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 46 deletions.
79 changes: 37 additions & 42 deletions src/clientiface.cpp
Expand Up @@ -138,26 +138,6 @@ void RemoteClient::GetNextBlocks (
camera_dir.rotateYZBy(sao->getLookPitch());
camera_dir.rotateXZBy(sao->getRotation().Y);

/*infostream<<"camera_dir=("<<camera_dir.X<<","<<camera_dir.Y<<","
<<camera_dir.Z<<")"<<std::endl;*/

/*
Get the starting value of the block finder radius.
*/

if (m_last_center != center) {
m_nearest_unsent_d = 0;
m_last_center = center;
}

/*infostream<<"m_nearest_unsent_reset_timer="
<<m_nearest_unsent_reset_timer<<std::endl;*/

//s16 last_nearest_unsent_d = m_nearest_unsent_d;
s16 d_start = m_nearest_unsent_d;

//infostream<<"d_start="<<d_start<<std::endl;

u16 max_simul_sends_usually = m_max_simul_sends;

/*
Expand Down Expand Up @@ -189,6 +169,29 @@ void RemoteClient::GetNextBlocks (
s16 wanted_range = sao->getWantedRange() + 1;
float camera_fov = sao->getFov();

/*
Get the starting value of the block finder radius.
*/
if (m_last_center != center) {
m_nearest_unsent_d = 0;
m_last_center = center;
}
// reset the unsent distance if the view angle has changed more that 10% of the fov
// (this matches isBlockInSight which allows for an extra 10%)
if (camera_dir.dotProduct(m_last_camera_dir) < std::cos(camera_fov * 0.1f)) {
m_nearest_unsent_d = 0;
m_last_camera_dir = camera_dir;
}
if (m_nearest_unsent_d > 0) {
// make sure any blocks modified since the last time we sent blocks are resent
for (const v3s16 &p : m_blocks_modified) {
m_nearest_unsent_d = std::min(m_nearest_unsent_d, center.getDistanceFrom(p));
}
}
m_blocks_modified.clear();

s16 d_start = m_nearest_unsent_d;

// Distrust client-sent FOV and get server-set player object property
// zoom FOV (degrees) as a check to avoid hacked clients using FOV to load
// distant world.
Expand Down Expand Up @@ -393,21 +396,18 @@ void RemoteClient::GetNextBlocks (

void RemoteClient::GotBlock(v3s16 p)
{
if (m_blocks_modified.find(p) == m_blocks_modified.end()) {
if (m_blocks_sending.find(p) != m_blocks_sending.end())
m_blocks_sending.erase(p);
else
m_excess_gotblocks++;

if (m_blocks_sending.find(p) != m_blocks_sending.end()) {
m_blocks_sending.erase(p);
// only add to sent blocks if it actually was sending
// (it might have been modified since)
m_blocks_sent.insert(p);
} else {
m_excess_gotblocks++;
}
}

void RemoteClient::SentBlock(v3s16 p)
{
if (m_blocks_modified.find(p) != m_blocks_modified.end())
m_blocks_modified.erase(p);

if (m_blocks_sending.find(p) == m_blocks_sending.end())
m_blocks_sending[p] = 0.0f;
else
Expand All @@ -417,29 +417,24 @@ void RemoteClient::SentBlock(v3s16 p)

void RemoteClient::SetBlockNotSent(v3s16 p)
{
m_nearest_unsent_d = 0;
m_nothing_to_send_pause_timer = 0;

if (m_blocks_sending.find(p) != m_blocks_sending.end())
m_blocks_sending.erase(p);
if (m_blocks_sent.find(p) != m_blocks_sent.end())
m_blocks_sent.erase(p);
m_blocks_modified.insert(p);
// remove the block from sending and sent sets,
// and mark as modified if found
if (m_blocks_sending.erase(p) + m_blocks_sent.erase(p) > 0)
m_blocks_modified.insert(p);
}

void RemoteClient::SetBlocksNotSent(std::map<v3s16, MapBlock*> &blocks)
{
m_nearest_unsent_d = 0;
m_nothing_to_send_pause_timer = 0;

for (auto &block : blocks) {
v3s16 p = block.first;
m_blocks_modified.insert(p);

if (m_blocks_sending.find(p) != m_blocks_sending.end())
m_blocks_sending.erase(p);
if (m_blocks_sent.find(p) != m_blocks_sent.end())
m_blocks_sent.erase(p);
// remove the block from sending and sent sets,
// and mark as modified if found
if (m_blocks_sending.erase(p) + m_blocks_sent.erase(p) > 0)
m_blocks_modified.insert(p);
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/clientiface.h
Expand Up @@ -364,6 +364,7 @@ class RemoteClient
std::set<v3s16> m_blocks_sent;
s16 m_nearest_unsent_d = 0;
v3s16 m_last_center;
v3f m_last_camera_dir;

const u16 m_max_simul_sends;
const float m_min_time_from_building;
Expand All @@ -383,10 +384,10 @@ class RemoteClient
std::map<v3s16, float> m_blocks_sending;

/*
Blocks that have been modified since last sending them.
These blocks will not be marked as sent, even if the
client reports it has received them to account for blocks
that are being modified while on the line.
Blocks that have been modified since blocks were
sent to the client last (getNextBlocks()).
This is used to reset the unsent distance, so that
modified blocks are resent to the client.
List of block positions.
*/
Expand Down

0 comments on commit 68cd93b

Please sign in to comment.