Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/wip5.8.0' into wip5.8.0-32
Browse files Browse the repository at this point in the history
  • Loading branch information
proller committed Oct 18, 2024
2 parents 8b4641e + f96531d commit 63e009e
Show file tree
Hide file tree
Showing 25 changed files with 450 additions and 230 deletions.
2 changes: 1 addition & 1 deletion games/default
Submodule default updated 1 files
+1 −1 mods/mobs_sky
23 changes: 23 additions & 0 deletions src/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ along with Freeminer. If not, see <http://www.gnu.org/licenses/>.
#include "database/database.h"
#include "server.h"
#include "emerge.h"
#include "fm_world_merge.h"

#include "network/fm_connection_use.h"
#if !MINETEST_PROTO
#include "network/fm_clientpacketsender.cpp"
Expand Down Expand Up @@ -359,6 +361,8 @@ void Client::Stop()
if (m_mods_loaded)
delete m_script;

merger.reset(); // before m_localdb

if (m_localdb)
delete m_localdb;
}
Expand Down Expand Up @@ -413,6 +417,7 @@ Client::~Client()
for (auto &csp : m_sounds_client_to_server)
m_sound->freeId(csp.first);
m_sounds_client_to_server.clear();
last_async.wait();
}

void Client::connect(Address address, bool is_local_server)
Expand Down Expand Up @@ -1062,8 +1067,26 @@ void Client::initLocalMapSaving(const Address &address,

m_localdb->beginSave();
actionstream << "Local map saving started, map will be saved at '" << world_path << "'" << std::endl;

if (!m_simple_singleplayer_mode) {
far_dbases[0].reset(m_localdb, [](auto) {});
if (!merger) {
merger = std::make_unique<WorldMerger>(WorldMerger{
.get_time_func{[this]() {
return m_uptime.load(std::memory_order::relaxed);
}}, // find client game time == server time?
.partial{true},
.ndef{getNodeDefManager()},
.smap{&getEnv().getClientMap()},
.far_dbases{far_dbases},
.dbase{m_localdb},
.save_dir{m_world_path},
});
}
}
}


void Client::ReceiveAll()
{
NetworkPacket pkt;
Expand Down
14 changes: 11 additions & 3 deletions src/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ along with Freeminer. If not, see <http://www.gnu.org/licenses/>.

#pragma once

//fm:
// fm:
#include <future>
#include "client/fm_far_container.h"
#include "map.h"
#include "map_settings_manager.h"
#include "mapgen/mapgen.h"
#include "msgpack_fix.h"
#include "network/fm_connection_use.h"

constexpr const auto FARMESH_DEFAULT_MAPGEN = MAPGEN_FLAT;
// ==

#include "clientenvironment.h"
#include "irr_v3d.h"
Expand Down Expand Up @@ -126,6 +130,7 @@ class PacketCounter

class ClientScripting;
class GameUI;
class WorldMerger;

class Client : public con::PeerHandler, public InventoryManager, public IGameDef
{
Expand All @@ -146,6 +151,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
void sendDrawControl();
void sendGetBlocks();
void updateMeshTimestampWithEdge(const v3bpos_t &blockpos);
void MakeEmerge(const Settings &settings, const MapgenType& mgtype);
void createFarMesh(MapBlockP &block);

std::unique_ptr<Server> m_localserver;
Expand All @@ -159,7 +165,9 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
size_t m_new_farmeshes {};
ChatBackend *chat_backend {};
FarContainer far_container;

ServerMap::far_dbases_t far_dbases;
std::unique_ptr<WorldMerger> merger;
std::future<void> last_async;
// ==

public:
Expand Down
36 changes: 31 additions & 5 deletions src/client/clientmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,11 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
// Mesh animation
if (pass == scene::ESNRP_SOLID) {
// Pretty random but this should work somewhat nicely
bool faraway = false; //d >= BS * 50;
bool faraway = d >= BS * 50;

if (is_far)
faraway = false;

if (block_mesh->isAnimationForced() || !faraway ||
mesh_animate_count < (m_control.range_all ? 200 : 50)) {

Expand Down Expand Up @@ -1589,10 +1593,13 @@ void ClientMap::renderMapShadows(video::IVideoDriver *driver,
auto block = i.second;

// If the mesh of the block happened to get deleted, ignore it
auto mapBlockMesh = block->getLodMesh(getLodStep(m_control, getNodeBlockPos(m_camera_position_node), block->getPos(), speedf), true);
auto mapBlockMesh = block->getLodMesh(getLodStep(m_control, getNodeBlockPos(m_camera_position_node), block_pos, speedf), true);

//if (!mapBlockMesh)
// mapBlockMesh = block->getFarMesh(getFarStep(m_control, getNodeBlockPos(m_far_blocks_last_cam_pos), block->getPos()));
#if FARMESH_SHADOWS
if (!mapBlockMesh) {
mapBlockMesh = block->getFarMesh(getFarStep(m_control, getNodeBlockPos(far_blocks_last_cam_pos), block_pos ));
}
#endif

if (!mapBlockMesh)
continue;
Expand Down Expand Up @@ -1768,6 +1775,18 @@ void ClientMap::updateDrawListShadow(v3opos_t shadow_light_pos, v3opos_t shadow_
}
}

#if FARMESH_SHADOWS
{
const auto lock = m_far_blocks.lock_shared_rec();
for (const auto &[pos, block] : m_far_blocks) {
if (far_iteration_clean && block->far_iteration < far_iteration_clean) {
} else if (block->far_iteration >= far_iteration_use) {
m_drawlist_shadow.emplace(pos, block);
}
}
}
#endif

m_drawlist_shadow_current = !m_drawlist_shadow_current;

g_profiler->avg("SHADOW MapBlock meshes in range [#]", blocks_in_range_with_mesh);
Expand All @@ -1794,7 +1813,14 @@ void ClientMap::updateTransparentMeshBuffers()
// Update the order of transparent mesh buffers in each mesh
for (auto it = m_drawlist.begin(); it != m_drawlist.end(); it++) {
auto block = it->second;
const auto block_mesh = block->getLodMesh(getLodStep(m_control, getNodeBlockPos(m_camera_position_node), block->getPos(), speedf));
auto block_mesh = block->getLodMesh(getLodStep(m_control, getNodeBlockPos(m_camera_position_node), block->getPos(), speedf));

#if FARMESH_SHADOWS
if (!block_mesh) {
block_mesh = block->getFarMesh(getFarStep(m_control, getNodeBlockPos(far_blocks_last_cam_pos), block->getPos()));
}
#endif

if (!block_mesh)
continue;

Expand Down
103 changes: 62 additions & 41 deletions src/client/fm_client.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include <atomic>
#include <exception>
#include <future>
#include <memory>
#include "client.h"
#include "client/fm_far_calc.h"
#include "client/mapblock_mesh.h"
#include "clientmap.h"
#include "emerge.h"
#include "fm_world_merge.h"
#include "irr_v3d.h"
#include "mapblock.h"
#include "network/fm_networkprotocol.h"
Expand Down Expand Up @@ -82,7 +85,7 @@ void Client::handleCommand_FreeminerInit(NetworkPacket *pkt)

auto &packet = *(pkt->packet);

if (!m_world_path.empty() && packet.count(TOCLIENT_INIT_GAMEID)) {
if (!m_world_path.empty() && packet.contains(TOCLIENT_INIT_GAMEID)) {
std::string gameid;
packet[TOCLIENT_INIT_GAMEID].convert(gameid);
std::string conf_path = m_world_path + DIR_DELIM + "world.mt";
Expand All @@ -92,69 +95,80 @@ void Client::handleCommand_FreeminerInit(NetworkPacket *pkt)
conf.updateConfigFile(conf_path.c_str());
}

const thread_local static auto farmesh_range = g_settings->getS32("farmesh");

if (farmesh_range && !m_localserver) {
m_localserver = std::make_unique<Server>(
"farmesh", findSubgame("devtest"), false, Address{}, true);
}

{
Settings settings;
packet[TOCLIENT_INIT_MAP_PARAMS].convert(settings);

std::string mg_name;
MapgenType mgtype = settings.getNoEx("mg_name", mg_name)
? Mapgen::getMapgenType(mg_name)
: MAPGEN_DEFAULT;
: FARMESH_DEFAULT_MAPGEN;

if (mgtype == MAPGEN_INVALID) {
errorstream << "Client map save: mapgen '" << mg_name
<< "' not valid; falling back to "
<< Mapgen::getMapgenName(MAPGEN_DEFAULT) << std::endl;
mgtype = MAPGEN_DEFAULT;
<< Mapgen::getMapgenName(FARMESH_DEFAULT_MAPGEN) << std::endl;
mgtype = FARMESH_DEFAULT_MAPGEN;
far_container.use_weather = false;
} else {
far_container.have_params = true;
}

m_mapgen_params =
std::unique_ptr<MapgenParams>(Mapgen::createMapgenParams(mgtype));
m_mapgen_params->MapgenParams::readParams(&settings);
m_mapgen_params->readParams(&settings);

if (!m_simple_singleplayer_mode && farmesh_range) {
const auto num_emerge_threads = g_settings->get("num_emerge_threads");
g_settings->set("num_emerge_threads", "1");
m_emerge = std::make_unique<EmergeManager>(
m_localserver.get(), m_localserver->m_metrics_backend.get());
m_emerge->initMapgens(m_mapgen_params.get());
g_settings->set("num_emerge_threads", num_emerge_threads);
}

if (!m_world_path.empty()) {
m_settings_mgr = std::make_unique<MapSettingsManager>(
m_world_path + DIR_DELIM + "map_meta");
m_settings_mgr->mapgen_params = m_mapgen_params.release();
;
m_settings_mgr->saveMapMeta();
} else if (!m_emerge) {
m_mapgen_params.reset();
}
MakeEmerge(settings, mgtype);
}

if (packet.count(TOCLIENT_INIT_WEATHER))
if (packet.contains(TOCLIENT_INIT_WEATHER)) {
packet[TOCLIENT_INIT_WEATHER].convert(use_weather);
}

//if (packet.count(TOCLIENT_INIT_PROTOCOL_VERSION_FM))
// packet[TOCLIENT_INIT_PROTOCOL_VERSION_FM].convert( not used );
}

void Client::MakeEmerge(const Settings &settings, const MapgenType &mgtype)
{
const thread_local static auto farmesh_range = g_settings->getS32("farmesh");

if (farmesh_range && !m_localserver) {
m_localserver = std::make_unique<Server>(
"farmesh", findSubgame("devtest"), false, Address{}, true);
}
m_mapgen_params = std::unique_ptr<MapgenParams>(Mapgen::createMapgenParams(mgtype));
m_mapgen_params->MapgenParams::readParams(&settings);
m_mapgen_params->readParams(&settings);

if (!m_simple_singleplayer_mode && farmesh_range) {
const auto num_emerge_threads = g_settings->get("num_emerge_threads");
g_settings->set("num_emerge_threads", "1");
m_emerge = std::make_unique<EmergeManager>(
m_localserver.get(), m_localserver->m_metrics_backend.get());
m_emerge->initMapgens(m_mapgen_params.get());
g_settings->set("num_emerge_threads", num_emerge_threads);
}

if (!m_world_path.empty()) {
m_settings_mgr = std::make_unique<MapSettingsManager>(
m_world_path + DIR_DELIM + "map_meta");
m_settings_mgr->mapgen_params = m_mapgen_params.release();
m_settings_mgr->saveMapMeta();
} else if (!m_emerge) {
m_mapgen_params.reset();
}
}

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);
#if FARMESH_SHADOWS
static const auto m_cache_enable_shaders = g_settings->getBool("enable_shaders");
#else
static const auto m_cache_enable_shaders = false;
#endif
MeshMakeData mdat(
m_client, m_cache_enable_shaders, 0, step, &m_client->far_container);
mdat.m_blockpos = blockpos_actual;
const auto mbmsh = std::make_shared<MapBlockMesh>(&mdat, m_camera_offset);
block->setFarMesh(mbmsh, step);
Expand Down Expand Up @@ -217,10 +231,17 @@ void Client::handleCommand_BlockDataFm(NetworkPacket *pkt)
packet[TOCLIENT_BLOCKDATA_HUMIDITY].convert(h);
block->humidity = h;

if (!step) {
if (m_localdb) {
ServerMap::saveBlock(block.get(), m_localdb);
if (m_localdb && !is_simple_singleplayer_game) {
if (const auto db = GetFarDatabase({}, far_dbases, m_world_path, step); db) {
ServerMap::saveBlock(block.get(), db);

if (!step && !far_container.have_params) {
merger->add_changed(bpos);
}
}
}

if (!step) {
updateMeshTimestampWithEdge(bpos);
if (!overload && block->content_only != CONTENT_IGNORE &&
block->content_only != CONTENT_AIR) {
Expand All @@ -245,7 +266,7 @@ void Client::handleCommand_BlockDataFm(NetworkPacket *pkt)
++m_new_farmeshes;

//todo: step ordered thread pool
std::async(std::launch::async, [this, block]() mutable {
last_async = std::async(std::launch::async, [this, block]() mutable {
createFarMesh(block);
auto &client_map = getEnv().getClientMap();
const auto &control = client_map.getControl();
Expand Down
Loading

0 comments on commit 63e009e

Please sign in to comment.