Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
proller committed Oct 12, 2024
1 parent 42f3bd0 commit b5ecbd5
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 101 deletions.
19 changes: 8 additions & 11 deletions src/client/clientmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ void ClientMap::updateDrawList(float dtime, unsigned int max_cycle_ms)
auto mesh = block ? block->getLodMesh(mesh_step, true) : nullptr;
if (!mesh && block) {
int fmesh_step = getFarStep(
m_control, getNodeBlockPos(m_far_blocks_last_cam_pos), block_coord);
m_control, getNodeBlockPos(far_blocks_last_cam_pos), block_coord);
mesh = block->getFarMesh(fmesh_step);
}
if (!mesh)
Expand Down Expand Up @@ -490,7 +490,7 @@ void ClientMap::updateDrawList(float dtime, unsigned int max_cycle_ms)
auto mesh = block ? block->getLodMesh(mesh_step, true) : nullptr;
if (!mesh && block) {
int fmesh_step = getFarStep(
m_control, getNodeBlockPos(m_far_blocks_last_cam_pos), block_coord);
m_control, getNodeBlockPos(far_blocks_last_cam_pos), block_coord);
mesh = block->getFarMesh(fmesh_step);
}
//if (!mesh)
Expand Down Expand Up @@ -1031,9 +1031,9 @@ void ClientMap::updateDrawListFm(float dtime, unsigned int max_cycle_ms)

{
const auto fmesh_step = getFarStep(
m_control, getNodeBlockPos(m_far_blocks_last_cam_pos), bp);
m_control, getNodeBlockPos(far_blocks_last_cam_pos), bp);
blocks_skip_farmesh.emplace(
getFarActual(bp, getNodeBlockPos(m_far_blocks_last_cam_pos),
getFarActual(bp, getNodeBlockPos(far_blocks_last_cam_pos),
fmesh_step, m_control));
}

Expand Down Expand Up @@ -1066,11 +1066,11 @@ void ClientMap::updateDrawListFm(float dtime, unsigned int max_cycle_ms)
const auto lock = m_far_blocks.lock_unique_rec();
for (auto it = m_far_blocks.begin(); it != m_far_blocks.end();) {
const auto &block = it->second;
if (m_far_blocks_clean_timestamp > 0 &&
block->getTimestamp() < m_far_blocks_clean_timestamp) {
if (far_iteration_clean &&
block->far_iteration < far_iteration_clean) {
m_far_blocks_delete.emplace_back(block);
it = m_far_blocks.erase(it);
} else if (block->getTimestamp() >= m_far_blocks_use_timestamp) {
} else if (block->far_iteration >= far_iteration_use) {
if (!blocks_skip_farmesh.contains(it->first)) {
drawlist.emplace(it->first, block);
++farblocks_drawn;
Expand Down Expand Up @@ -1196,10 +1196,7 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
int &fmesh_step = mesh_step;

fmesh_step = getFarStep(
m_control, getNodeBlockPos(m_far_blocks_last_cam_pos), block->getPos());
if (fmesh_step > 1 && !inFarGrid(block_pos, getNodeBlockPos(m_far_blocks_last_cam_pos), fmesh_step, m_control)) {
continue;
}
m_control, getNodeBlockPos(far_blocks_last_cam_pos), block->getPos());
block_mesh = block->getFarMesh(fmesh_step);
is_far = true;
}
Expand Down
1 change: 1 addition & 0 deletions src/client/clientmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct MapDrawControl
MapDrawControl() {
fm_init();
}
// ==

// Wanted drawing range
float wanted_range = 0.0f;
Expand Down
55 changes: 51 additions & 4 deletions src/client/fm_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,20 +251,67 @@ void Client::handleCommand_BlockDatas(NetworkPacket *pkt)
const auto &control = client_map.getControl();
const auto bpos = block->getPos();
int fmesh_step_ = getFarStep(control,
getNodeBlockPos(client_map.m_far_blocks_last_cam_pos),
block->getPos());
getNodeBlockPos(client_map.far_blocks_last_cam_pos), block->getPos());
if (!inFarGrid(block->getPos(),
getNodeBlockPos(client_map.m_far_blocks_last_cam_pos),
fmesh_step_, control)) {
getNodeBlockPos(client_map.far_blocks_last_cam_pos), fmesh_step_,
control)) {
return;
}
auto &far_blocks = client_map.m_far_blocks;
if (const auto &it = far_blocks.find(bpos); it != far_blocks.end()) {
if (it->second->far_step != block->far_step) {
return;
}
block->far_iteration = it->second->far_iteration;
far_blocks.at(bpos) = block;
}
});

// if decide to generate empty areas on server:
#if 0
class BlockContainer : public NodeContainer
{
MapBlockP block;

public:
Mapgen *m_mg{};
BlockContainer(Client *client, MapBlockP block_) :
//m_client{client},
block{std::move(block_)} {};
const MapNode &getNodeRefUnsafe(const v3pos_t &pos) override
{
auto bpos = getNodeBlockPos(pos);
const auto fmesh_step = block->far_step;
const auto &shift = fmesh_step; // + cell_size_pow;
v3bpos_t bpos_aligned((bpos.X >> shift) << shift,
(bpos.Y >> shift) << shift, (bpos.Z >> shift) << shift);

v3pos_t relpos = pos - bpos_aligned * MAP_BLOCKSIZE;

const auto &relpos_shift = fmesh_step; // + 1;
auto relpos_shifted = v3pos_t(relpos.X >> relpos_shift,
relpos.Y >> relpos_shift, relpos.Z >> relpos_shift);

const auto &n = block->getNodeNoLock(relpos_shifted);
return n;
};
};

BlockContainer bc{this, block};

const auto &m_client = this;
const auto &blockpos_actual = bpos;
MeshMakeData mdat(m_client, false, 0, step, &bc);
mdat.m_blockpos = blockpos_actual;
const auto m_camera_offset = getCamera()->getOffset();
auto mbmsh = std::make_shared<MapBlockMesh>(&mdat, m_camera_offset);
block->setFarMesh(mbmsh, step, m_client->m_uptime);
block->setTimestampNoChangedFlag(
getEnv().getClientMap().m_far_blocks_use_timestamp);
getEnv().getClientMap().far_blocks_storage[step].insert_or_assign(
bpos, block);
++m_client->m_new_meshes;
}
#endif
}
}
2 changes: 1 addition & 1 deletion src/client/fm_far_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const MapNode &FarContainer::getNodeRefUnsafe(const v3pos_t &pos)
auto bpos = getNodeBlockPos(pos);

int fmesh_step = getFarStep(m_client->getEnv().getClientMap().getControl(),
getNodeBlockPos(m_client->getEnv().getClientMap().m_far_blocks_last_cam_pos),
getNodeBlockPos(m_client->getEnv().getClientMap().far_blocks_last_cam_pos),
bpos);

const auto &shift = fmesh_step; // + cell_size_pow;
Expand Down
70 changes: 31 additions & 39 deletions src/client/fm_farmesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ void FarMesh::makeFarBlock(
if (const auto &fbit = far_blocks.find(blockpos_actual);
fbit != far_blocks.end()) {
if (fbit->second.get() == block.get()) {
block->setTimestampNoChangedFlag(timestamp_complete);
block->far_iteration = far_iteration_complete;
return;
}
client_map.m_far_blocks_delete.emplace_back(fbit->second);
}
far_blocks.insert_or_assign(blockpos_actual, block);
++m_client->m_new_meshes;
}
block->setTimestampNoChangedFlag(timestamp_complete);
block->far_iteration = far_iteration_complete;
return;
}
MapBlockP block;
Expand All @@ -90,7 +90,7 @@ void FarMesh::makeFarBlock(
} else {
if (!block) {
m_client->getEnv().getClientMap().m_far_blocks_ask.emplace(
blockpos_actual, std::make_pair(step, timestamp_complete));
blockpos_actual, std::make_pair(step, far_iteration_complete));

new_block = true;
block = std::make_shared<MapBlock>(
Expand All @@ -99,9 +99,9 @@ void FarMesh::makeFarBlock(
far_blocks.insert_or_assign(blockpos_actual, block);
++m_client->m_new_meshes;
}
block->setTimestampNoChangedFlag(timestamp_complete);
}
}
block->far_iteration = far_iteration_complete;
if (new_block) {
std::async(std::launch::async, [this, block]() mutable {
m_client->createFarMesh(block);
Expand All @@ -113,11 +113,14 @@ void FarMesh::makeFarBlock(
void FarMesh::makeFarBlocks(const v3bpos_t &blockpos, MapBlock::block_step_t step)
{
#if FARMESH_DEBUG || FARMESH_FAST || 1

auto block_step_correct = getFarStep(m_client->getEnv().getClientMap().getControl(),
getNodeBlockPos(m_camera_pos_aligned), blockpos);

return makeFarBlock(blockpos, block_step_correct);
{
auto block_step_correct =
getFarStep(m_client->getEnv().getClientMap().getControl(),
getNodeBlockPos(m_camera_pos_aligned), blockpos);
if (!block_step_correct)
return;
return makeFarBlock(blockpos, block_step_correct);
}
#endif

// TODO: fix finding correct near blocks respecting their steps and enable:
Expand All @@ -141,6 +144,8 @@ void FarMesh::makeFarBlocks(const v3bpos_t &blockpos, MapBlock::block_step_t ste
auto block_step_correct =
getFarStep(m_client->getEnv().getClientMap().getControl(),
getNodeBlockPos(m_camera_pos_aligned), bpos);
if (!block_step_correct)
continue;
makeFarBlock(bpos, block_step_correct);
}
}
Expand Down Expand Up @@ -428,35 +433,34 @@ uint8_t FarMesh::update(v3opos_t camera_pos,
<< 7;

auto &clientMap = m_client->getEnv().getClientMap();
const auto far_fast = false;

/*
const auto far_fast =
!m_control->farmesh_stable &&
(
//m_client->getEnv().getClientMap().m_far_fast &&
m_speed > 200 * BS ||
m_camera_pos_aligned.getDistanceFrom(camera_pos_aligned_int) > 1000);
*/

const auto set_new_cam_pos = [&]() {
if (m_camera_pos_aligned == camera_pos_aligned_int)
return;
return false;

++far_iteration_complete;

m_camera_pos_aligned = camera_pos_aligned_int;
m_camera_pos = intToFloat(m_camera_pos_aligned, BS);
plane_processed.fill({});

direction_caches.fill({});
direction_caches_pos = m_camera_pos_aligned;
return true;
};

if (!timestamp_complete) {
if (!far_iteration_complete) {
if (!m_camera_pos_aligned.X && !m_camera_pos_aligned.Y &&
!m_camera_pos_aligned.Z) {
set_new_cam_pos();
}
clientMap.m_far_blocks_last_cam_pos = m_camera_pos_aligned;
clientMap.far_blocks_last_cam_pos = m_camera_pos_aligned;
if (!last_distance_max)
last_distance_max = distance_max;
}
Expand Down Expand Up @@ -498,31 +502,19 @@ uint8_t FarMesh::update(v3opos_t camera_pos,

if (planes_processed) {
complete_set = false;
} else if (!far_fast) {
set_new_cam_pos();
}
if (m_camera_pos_aligned != camera_pos_aligned_int) {
clientMap.m_far_blocks_last_cam_pos =
far_fast ? camera_pos_aligned_int : m_camera_pos_aligned;
if (far_fast) {
set_new_cam_pos();
}
}
if (!planes_processed && !complete_set) {
clientMap.m_far_blocks_last_cam_pos = m_camera_pos_aligned;
clientMap.m_far_blocks_use_timestamp = timestamp_complete;
if (!set_new_cam_pos()) {

// TODO: test correct times
#if FARMESH_DEBUG
constexpr auto clean_old_time = 2;
#else
constexpr auto clean_old_time = 30;
#endif
if (timestamp_complete > clean_old_time)
clientMap.m_far_blocks_clean_timestamp =
timestamp_complete - clean_old_time;
timestamp_complete = m_client->m_uptime;
complete_set = true;
if (!planes_processed && !complete_set) {
clientMap.far_blocks_last_cam_pos = m_camera_pos_aligned;
clientMap.far_iteration_use = far_iteration_complete;

if (far_iteration_complete)
clientMap.far_iteration_clean = far_iteration_complete - 1;
complete_set = true;
}
} else if (far_fast) {
clientMap.far_blocks_last_cam_pos = m_camera_pos_aligned;
}
/*
{
Expand Down
2 changes: 1 addition & 1 deletion src/client/fm_farmesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class FarMesh
std::array<plane_cache, 6> plane_processed;
std::atomic_uint last_distance_max{};
int go_direction(const size_t dir_n);
uint32_t timestamp_complete{};
uint32_t far_iteration_complete {};
bool complete_set = false;
uint8_t planes_processed_last{};
concurrent_shared_unordered_map<uint16_t, concurrent_unordered_set<v3bpos_t>>
Expand Down
21 changes: 13 additions & 8 deletions src/client/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -915,32 +915,37 @@ void Hud::drawBlockBounds()
//s8 radius = m_block_bounds_mode == BLOCK_BOUNDS_NEAR ? 2 : 0;

v3f halfNode = v3f(BS, BS, BS) / 2.0f;
const auto & far_blocks = client->getEnv().getClientMap().m_far_blocks;
const auto &client_map = client->getEnv().getClientMap();
const auto & far_blocks = client_map.m_far_blocks;

if (const auto lock = far_blocks.try_lock_shared_rec(); lock->owns_lock()) {
for (const auto &[blockPos, block] :
far_blocks) {
if (!block)
continue;
if (block->getTimestamp() <
client->getEnv().getClientMap().m_far_blocks_use_timestamp)
if (block->far_iteration <
client_map.far_iteration_use)
continue;
/*
const auto mesh_step_ = getFarStep(
client->getEnv().getClientMap().getControl(),
getNodeBlockPos(
client->getEnv()
.getClientMap()
.m_far_blocks_last_cam_pos),
blockPos);
*/
const auto &mesh_step = block->far_step;
int g = 0;

if (!inFarGrid(blockPos, getNodeBlockPos(
client->getEnv()
.getClientMap()
.m_far_blocks_last_cam_pos), mesh_step,
client->getEnv().getClientMap().getControl()))
client_map
.far_blocks_last_cam_pos), mesh_step,
client_map.getControl()))
{
// DUMP("Not in grid", blockPos, block->far_step, mesh_step, block->getTimestamp(), client->getEnv() .getClientMap() .m_far_blocks_last_cam_pos);
// continue;
g+=50;
}

int fscale = 1;
Expand All @@ -963,7 +968,7 @@ void Hud::drawBlockBounds()
BS) -
offset + halfNode - 1);
driver->draw3DBox(box, video::SColor(200 + b, 255 - lod_step * 10 + b,
255 - far_step * 10, fscale * 20));
255 -g - far_step * 10, fscale * 20));
}
}
} else if (m_block_bounds_mode == BLOCK_BOUNDS_FAR_REQUEST) {
Expand Down
Loading

0 comments on commit b5ecbd5

Please sign in to comment.