From e0a60dcd6fa40f686d37586612bd4b0889ab24be Mon Sep 17 00:00:00 2001 From: Vladislav Khorev Date: Sat, 17 Jan 2026 13:59:02 +0300 Subject: [PATCH] Change clock to system clock, fix bugs --- server/server.cpp | 14 ++++++++------ src/Game.cpp | 14 ++++++++------ src/network/ClientState.h | 18 ++++++------------ src/network/WebSocketClient.cpp | 9 ++++++--- 4 files changed, 28 insertions(+), 27 deletions(-) diff --git a/server/server.cpp b/server/server.cpp index 067076a..7eb849d 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -40,7 +40,7 @@ class Session : public std::enable_shared_from_this { ClientState state_; void process_message(const std::string& msg) { - auto now_server = std::chrono::steady_clock::now(); + auto now_server = std::chrono::system_clock::now(); // : // ROT:ANGLE:MAG:TIMESTAMP @@ -65,7 +65,7 @@ class Session : public std::enable_shared_from_this { if (deltaMs > 0) state_.simulate_physics(deltaMs); - std::chrono::steady_clock::time_point uptime_timepoint{ std::chrono::duration_cast(std::chrono::milliseconds(clientTimestamp)) }; + std::chrono::system_clock::time_point uptime_timepoint{ std::chrono::duration_cast(std::chrono::milliseconds(clientTimestamp)) }; state_.lastUpdateServerTime = uptime_timepoint; @@ -73,6 +73,7 @@ class Session : public std::enable_shared_from_this { if (parts[0] == "ROT") { state_.discreteAngle = std::stoi(parts[2]); state_.discreteMag = std::stof(parts[3]); + std::cout << "ROT id = " << this->id_ << " discreteMag=" << state_.discreteMag << std::endl; state_.apply_lag_compensation(now_server); state_.lastUpdateServerTime = now_server; retranslateMessage(msg); @@ -85,6 +86,7 @@ class Session : public std::enable_shared_from_this { } else if (parts[0] == "PING") { state_.handle_full_sync(parts); + std::cout << "PING id = " << this->id_ <<" discreteMag=" << state_.discreteMag << std::endl; state_.apply_lag_compensation(now_server); state_.lastUpdateServerTime = now_server; } @@ -110,7 +112,7 @@ public: void init() { - state_.lastUpdateServerTime = std::chrono::steady_clock::now(); + state_.lastUpdateServerTime = std::chrono::system_clock::now(); } std::string get_state_string() { @@ -133,7 +135,7 @@ public: }); } - void tick_physics_global(std::chrono::steady_clock::time_point now) { + void tick_physics_global(std::chrono::system_clock::time_point now) { long long deltaMs = 0; // , @@ -181,7 +183,7 @@ private: }; void update_world(net::steady_timer& timer, net::io_context& ioc) { - auto now = std::chrono::steady_clock::now(); + auto now = std::chrono::system_clock::now(); { std::lock_guard lock(g_sessions_mutex); @@ -196,7 +198,7 @@ void update_world(net::steady_timer& timer, net::io_context& ioc) { last_broadcast = now; auto now_ms = std::chrono::duration_cast( - std::chrono::steady_clock::now().time_since_epoch() + std::chrono::system_clock::now().time_since_epoch() ).count(); // diff --git a/src/Game.cpp b/src/Game.cpp index 1d726cf..ad01455 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -242,7 +242,7 @@ namespace ZL if (newVel != Environment::shipSelectedVelocity) { Environment::shipSelectedVelocity = newVel; auto now_ms = std::chrono::duration_cast( - std::chrono::steady_clock::now().time_since_epoch() + std::chrono::system_clock::now().time_since_epoch() ).count(); std::string msg = "VEL:" + std::to_string(now_ms) + ":" + std::to_string(Environment::shipSelectedVelocity); networkClient->Send(msg); @@ -634,7 +634,7 @@ namespace ZL if (pingTimer >= 1000.0f) { auto now_ms = std::chrono::duration_cast( - std::chrono::steady_clock::now().time_since_epoch() + std::chrono::system_clock::now().time_since_epoch() ).count(); // 1. Извлекаем кватернион из матрицы поворота @@ -707,12 +707,14 @@ namespace ZL int discreteAngle = static_cast(radians * 180.0f / M_PI); if (discreteAngle < 0) discreteAngle += 360; + std::cout << "OUTPUT discreteAngle=" << discreteAngle << std::endl; + // 3. Проверяем, изменились ли параметры значимо для отправки на сервер if (discreteAngle != Environment::lastSentAngle || discreteMag != Environment::lastSentMagnitude) { Environment::lastSentAngle = discreteAngle; Environment::lastSentMagnitude = discreteMag; auto now_ms = std::chrono::duration_cast( - std::chrono::steady_clock::now().time_since_epoch() + std::chrono::system_clock::now().time_since_epoch() ).count(); // Формируем сетевой пакет @@ -776,7 +778,7 @@ namespace ZL Environment::lastSentAngle = discreteAngle; Environment::lastSentMagnitude = discreteMag; auto now_ms = std::chrono::duration_cast( - std::chrono::steady_clock::now().time_since_epoch() + std::chrono::system_clock::now().time_since_epoch() ).count(); // Формируем сетевой пакет @@ -809,7 +811,7 @@ namespace ZL } } - std::cout << "shipVelocity=" << Environment::shipVelocity << " delta=" << delta << std::endl; + //std::cout << "shipVelocity=" << Environment::shipVelocity << " delta=" << delta << std::endl; // Движение вперед (существующая логика) if (fabs(Environment::shipVelocity) > 0.01f) { @@ -1102,7 +1104,7 @@ namespace ZL void Game::extrapolateRemotePlayers() { - auto now = std::chrono::steady_clock::now(); + auto now = std::chrono::system_clock::now(); latestRemotePlayers = networkClient->getRemotePlayers(); diff --git a/src/network/ClientState.h b/src/network/ClientState.h index 75c9c09..4d29b8f 100644 --- a/src/network/ClientState.h +++ b/src/network/ClientState.h @@ -24,7 +24,7 @@ struct ClientState { int discreteAngle = -1; // Для расчета лага - std::chrono::steady_clock::time_point lastUpdateServerTime; + std::chrono::system_clock::time_point lastUpdateServerTime; void simulate_physics(size_t delta) { // Константы из Game.cpp, приведенные к секундам (умножаем на 1000) @@ -87,6 +87,10 @@ struct ClientState { Eigen::Quaternionf rotateQuat(Eigen::AngleAxisf(deltaAlpha, axis)); rotation = rotation * rotateQuat.toRotationMatrix(); + //std::cout << "Rotating ship. d="<< delta <<" DeltaAlpha: " << deltaAlpha << ", Axis: [" << axis.x() << ", " << axis.y() << ", " << axis.z() << "]\n"; + } + else { + //std::cout << "NOT Rotating ship. speedScale=" << speedScale << " discreteMag=" << discreteMag << "\n"; } /* @@ -154,7 +158,6 @@ struct ClientState { velocity = shipDesiredVelocity; } } - std::cout << "velocity=" << velocity << " delta=" << delta << std::endl; if (fabs(velocity) > 0.01f) { @@ -164,16 +167,7 @@ struct ClientState { } } - void apply_lag_compensation(std::chrono::steady_clock::time_point nowTime) { - - // 1. Получаем текущее время сервера в той же шкале (мс) - /*uto now_ms = std::chrono::duration_cast( - std::chrono::steady_clock::now().time_since_epoch() - ).count(); - - long long deltaMs = std::chrono::duration_cast( - std::chrono::steady_clock::now().time_since_epoch() - lastUpdateServerTime - ).count();*/ + void apply_lag_compensation(std::chrono::system_clock::time_point nowTime) { // 2. Вычисляем задержку long long deltaMs = 0; diff --git a/src/network/WebSocketClient.cpp b/src/network/WebSocketClient.cpp index 3bf868a..477e6ba 100644 --- a/src/network/WebSocketClient.cpp +++ b/src/network/WebSocketClient.cpp @@ -69,7 +69,7 @@ namespace ZL { void WebSocketClient::Poll() { std::lock_guard lock(queueMutex); - auto nowTime = std::chrono::steady_clock::now(); + auto nowTime = std::chrono::system_clock::now(); auto now_ms = std::chrono::duration_cast( nowTime.time_since_epoch() @@ -98,6 +98,7 @@ namespace ZL { rp.discreteAngle = std::stoi(parts[4]); rp.discreteMag = std::stof(parts[5]); } + std::cout << "EVENT Received discreteMag=" << rp.discreteMag << std::endl; // --- --- // @@ -160,9 +161,11 @@ namespace ZL { rp.velocity = std::stof(vals[8]); rp.currentAngularVelocity = { std::stof(vals[9]), std::stof(vals[10]), std::stof(vals[11]) }; rp.selectedVelocity = std::stoi(vals[12]); - rp.discreteMag = std::stoi(vals[13]); + rp.discreteMag = std::stof(vals[13]); rp.discreteAngle = std::stoi(vals[14]); - std::chrono::steady_clock::time_point uptime_timepoint{ std::chrono::duration_cast(std::chrono::milliseconds(serverTime)) }; + std::chrono::system_clock::time_point uptime_timepoint{ std::chrono::duration_cast(std::chrono::milliseconds(serverTime)) }; + + std::cout << "PING Received discreteMag=" << rp.discreteMag << std::endl; rp.lastUpdateServerTime = uptime_timepoint;