Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
proller committed Oct 5, 2024
1 parent 2e93d55 commit 5d19eb5
Show file tree
Hide file tree
Showing 23 changed files with 231 additions and 125 deletions.
1 change: 1 addition & 0 deletions src/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ Client::Client(
control.cell_size = m_mesh_grid.cell_size;
control.cell_size_pow = log(control.cell_size) / log(2);
control.farmesh_quality = g_settings->getU16("farmesh_quality");
control.farmesh_quality_pow = log(control.farmesh_quality) / log(2);
control.farmesh_stable = g_settings->getU16("farmesh_stable");
}

Expand Down
6 changes: 4 additions & 2 deletions src/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,12 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
unsigned int overload = 0;

void handleCommand_FreeminerInit(NetworkPacket *pkt);
void handleCommand_BlockDatas(NetworkPacket *pkt);
void handleCommand_BlockDatas(NetworkPacket *pkt);
void sendInitFm();
void sendDrawControl();
void sendGetBlocks();
void updateMeshTimestampWithEdge(const v3bpos_t &blockpos);
void createFarMesh(MapBlockP &block);

std::unique_ptr<Server> m_localserver;
std::string m_world_path;
Expand All @@ -154,10 +155,11 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
//concurrent_unordered_map<v3bpos_t, bool> farmesh_remake;
f32 fog_range = 0;
size_t m_new_meshes = 0;
size_t m_new_farmeshes = 0;
ChatBackend *chat_backend = nullptr;
FarContainer far_container;

// ==
// ==

public:
/*
Expand Down
1 change: 1 addition & 0 deletions src/client/clientmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct MapDrawControl
int32_t lodmesh = 4;
int cell_size = 1;
uint8_t cell_size_pow = 0;
uint8_t farmesh_quality_pow = 0;

float fps = 30;
float fps_avg = 30;
Expand Down
20 changes: 18 additions & 2 deletions src/client/fm_client.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <exception>
#include "client.h"
#include "client/mapblock_mesh.h"
#include "clientmap.h"
#include "emerge.h"
#include "irr_v3d.h"
Expand All @@ -12,7 +13,6 @@
#include "network/networkpacket.h"
#include "threading/lock.h"
#include "util/directiontables.h"
#include "util/hex.h"

void Client::updateMeshTimestampWithEdge(const v3bpos_t &blockpos)
{
Expand Down Expand Up @@ -61,7 +61,7 @@ void Client::sendGetBlocks()
MSGPACK_PACKET_INIT((int)TOSERVER_GET_BLOCKS, 1);

// DUMP("ask far blocks", far_blocks.size());

PACK(TOSERVER_GET_BLOCKS_BLOCKS,
static_cast<std::remove_reference_t<decltype(far_blocks)>::full_type>(
far_blocks));
Expand Down Expand Up @@ -145,6 +145,21 @@ void Client::handleCommand_FreeminerInit(NetworkPacket *pkt)
// packet[TOCLIENT_INIT_PROTOCOL_VERSION_FM].convert( not used );
}

void Client::createFarMesh(MapBlockP &block)
{
if (bool cmp = false; block->creating_far_mesh.compare_exchange_weak(cmp, true)) {
const auto &m_client = this;
const auto &blockpos_actual = block->getPos();
const auto &m_camera_offset = m_camera->getOffset();
const auto step = block->far_step;
MeshMakeData mdat(m_client, false, 0, step, &m_client->far_container);
mdat.m_blockpos = blockpos_actual;
auto mbmsh = std::make_shared<MapBlockMesh>(&mdat, m_camera_offset);
block->setFarMesh(mbmsh, step, m_client->m_uptime);
block->creating_far_mesh = false;
}
}

void Client::handleCommand_BlockDatas(NetworkPacket *pkt)
{
const auto str = std::string{pkt->getString(0), pkt->getSize()};
Expand Down Expand Up @@ -221,6 +236,7 @@ void Client::handleCommand_BlockDatas(NetworkPacket *pkt)
if (far_blocks.contains(bpos)) {
const auto &block = far_blocks.at(bpos);
block->farmesh_need_remake = m_uptime;
//block->setTimestampNoChangedFlag(-2);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/client/fm_far_calc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ auto align(auto pos, const auto amount)
v3bpos_t playerBlockAlign(
const MapDrawControl &draw_control, const v3bpos_t &playerblockpos)
{
const auto step_pow2 = draw_control.cell_size_pow + draw_control.farmesh_quality;
return align(playerblockpos, step_pow2) + (draw_control.cell_size >> 1);
const auto step_pow2 = draw_control.cell_size_pow + draw_control.farmesh_quality_pow;
return align(playerblockpos, step_pow2) + (step_pow2 >> 1);
}

#if 1
Expand Down Expand Up @@ -243,7 +243,7 @@ int getFarStep(const MapDrawControl &draw_control, const v3bpos_t &ppos,
return getFarStepCellSize(draw_control, ppos, blockpos, draw_control.cell_size_pow);
}

v3bpos_t getFarActual(const v3bpos_t& blockpos, const v3bpos_t &ppos, int step,
v3bpos_t getFarActual(const v3bpos_t &blockpos, const v3bpos_t &ppos, int step,
const MapDrawControl &draw_control)
{
const auto cell_size_pow = int(log(draw_control.cell_size) / log(2));
Expand Down
13 changes: 2 additions & 11 deletions src/client/fm_far_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,8 @@ const MapNode &FarContainer::getNodeRefUnsafe(const v3pos_t &pos)
}

const auto &v = m_mg->visible_content(pos);
if (v.getContent())
if (v.getContent()) {
return v;
}
return m_mg->visible_transparent;
};

MapNode FarContainer::getNodeNoExNoEmerge(const v3pos_t &p)
{
return getNodeRefUnsafe(p);
};

MapNode FarContainer::getNodeNoEx(const v3pos_t &p)
{
return getNodeRefUnsafe(p);
};
2 changes: 0 additions & 2 deletions src/client/fm_far_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@ class FarContainer : public NodeContainer

FarContainer(Client *client);
const MapNode &getNodeRefUnsafe(const v3pos_t &p) override;
MapNode getNodeNoExNoEmerge(const v3pos_t &p) override;
MapNode getNodeNoEx(const v3pos_t &p) override;
};
50 changes: 38 additions & 12 deletions src/client/fm_farmesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ along with Freeminer. If not, see <http://www.gnu.org/licenses/>.
#include "constants.h"
#include "emerge.h"
#include "irr_v3d.h"
#include "mapblock.h"
#include "mapnode.h"
#include "profiler.h"
#include "server.h"
Expand All @@ -39,11 +40,11 @@ along with Freeminer. If not, see <http://www.gnu.org/licenses/>.
const v3opos_t g_6dirso[6] = {
// +right, +top, +back
v3opos_t(0, 0, 1), // back
v3opos_t(0, 1, 0), // top
v3opos_t(1, 0, 0), // right
v3opos_t(0, 0, -1), // front
v3opos_t(0, -1, 0), // bottom
v3opos_t(-1, 0, 0), // left
v3opos_t(0, -1, 0), // bottom
v3opos_t(0, 1, 0), // top
};

void FarMesh::makeFarBlock(
Expand Down Expand Up @@ -91,10 +92,18 @@ void FarMesh::makeFarBlock(
}
}

void FarMesh::makeFarBlock7(const v3bpos_t &blockpos, MapBlock::block_step_t step)
void FarMesh::makeFarBlocks(const v3bpos_t &blockpos, MapBlock::block_step_t step)
{
const auto step_width = pow(2, step);
for (const auto &dir : g_7dirs) {
for (const auto &dir : {
v3pos_t(0, 0, 0), // self
v3pos_t(0, 0, 1), // back
v3pos_t(1, 0, 0), // right
v3pos_t(0, 0, -1), // front
v3pos_t(-1, 0, 0), // left
v3pos_t(0, 1, 0), // top
v3pos_t(0, -1, 0), // bottom
}) {
makeFarBlock(blockpos + dir * step_width, step);
}
}
Expand Down Expand Up @@ -207,10 +216,10 @@ int FarMesh::go_direction(const size_t dir_n)

int processed = 0;
for (uint16_t i = 0; i < grid_size_xy; ++i) {

auto &ray_cache = cache[i];
if (ray_cache.finished > last_distance_max)
if (ray_cache.finished > last_distance_max) {
continue;
}
//uint16_t y = uint16_t(process_order[i] / grid_size_x);
//uint16_t x = process_order[i] % grid_size_x;
uint16_t y = uint16_t(i / grid_size_x);
Expand Down Expand Up @@ -241,7 +250,10 @@ int FarMesh::go_direction(const size_t dir_n)
floatToInt(pos_last, BS) / MAP_BLOCKSIZE);
if (!block_step) {
// TODO: FIXME, should be not zero
break;
if (ray_cache.finished > 1000) {
//DUMP("fixme wrong step", ray_cache.finished, m_camera_pos_aligned, pos_last);
break;
}
}
const auto block_step_pow = pow(2, block_step - block_step_reduce);
const auto step_width = MAP_BLOCKSIZE * block_step_pow;
Expand Down Expand Up @@ -284,7 +296,7 @@ int FarMesh::go_direction(const size_t dir_n)
(pos_int_raw.Y / step_aligned) * step_aligned,
(pos_int_raw.Z / step_aligned) * step_aligned);

auto &visible = ray_cache.visible;
content_t visible = 0;

{
if (const auto &it = mg_cache.find(pos_int); it != mg_cache.end()) {
Expand All @@ -294,8 +306,15 @@ int FarMesh::go_direction(const size_t dir_n)
mg_cache[pos_int] = visible;
}
}

if (depth > MAP_BLOCKSIZE * 8) {
ray_cache.visible = visible;
}

if (visible) {
ray_cache.finished = -1;
if (depth > MAP_BLOCKSIZE * 8) {
ray_cache.finished = -1;
}
const auto blockpos = getNodeBlockPos(pos_int);
TimeTaker timer_step("makeFarBlock");

Expand Down Expand Up @@ -342,12 +361,15 @@ int FarMesh::go_direction(const size_t dir_n)
makeFarBlock(blockpos, block_step);
#else
// less holes, more unused meshes:
makeFarBlock7(blockpos, block_step);
makeFarBlocks(blockpos, block_step);
#endif
}
break;
if (ray_cache.finished == -1) {
break;
}
}
if (depth >= last_distance_max) {

if (radius_box(pos_int, m_camera_pos_aligned) > last_distance_max) {
break;
}
}
Expand Down Expand Up @@ -421,6 +443,10 @@ void FarMesh::update(v3opos_t camera_pos,
plane_processed.fill({});
last_distance_max = distance_max; // * 1.1;
}
if (m_client->m_new_farmeshes) {
m_client->m_new_farmeshes = 0;
plane_processed.fill({});
}

/*
if (mg->surface_2d()) {
Expand Down
7 changes: 3 additions & 4 deletions src/client/fm_farmesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class FarMesh
int render_range, float speed);
void makeFarBlock(
const v3bpos_t &blockpos, MapBlock::block_step_t step, bool near = false);
void makeFarBlock7(const v3bpos_t &blockpos, MapBlock::block_step_t step);
void makeFarBlocks(const v3bpos_t &blockpos, MapBlock::block_step_t step);
//void makeFarBlocks(const v3bpos_t &blockpos);

private:
Expand All @@ -67,7 +67,7 @@ class FarMesh
f32 m_camera_yaw;*/
Client *m_client;
MapDrawControl *m_control;
static constexpr pos_t distance_min = 8 * MAP_BLOCKSIZE;
pos_t distance_min{MAP_BLOCKSIZE * 9};
v3pos_t m_camera_offset;
float m_speed;

Expand All @@ -88,8 +88,7 @@ class FarMesh

struct ray_cache
{
unsigned int finished =
distance_min - MAP_BLOCKSIZE; /// last depth, -1 if visible
unsigned int finished = 0; // last depth, -1 if visible
content_t visible = {};
size_t step_num = {};
};
Expand Down
6 changes: 5 additions & 1 deletion src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4235,6 +4235,10 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
void Game::handlePointingAtObject(const PointedThing &pointed,
const ItemStack &tool_item, const v3f &player_position, bool show_debug)
{
if (!runData.selected_object) {
return;
}

std::wstring infotext = unescape_translate(
utf8_to_wide(runData.selected_object->infoText()));

Expand Down Expand Up @@ -4661,7 +4665,7 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,

const auto camera_position = camera->getPosition();
if (!runData.headless_optimize)
if ((client->m_new_meshes &&
if ((client->m_new_meshes ||
runData.update_draw_list_timer >= update_draw_list_delta) ||
runData.update_draw_list_last_cam_pos.getDistanceFrom(camera_position) >
MAP_BLOCKSIZE * BS * 1 ||
Expand Down
Loading

0 comments on commit 5d19eb5

Please sign in to comment.