Finally fixed bugs, now need to do cleanup

This commit is contained in:
Vladislav Khorev 2026-01-17 20:06:57 +03:00
parent 64385ba15c
commit 579f4886d1
3 changed files with 91 additions and 102 deletions

View File

@ -240,8 +240,8 @@ namespace ZL
uiManager.setSliderCallback("velocitySlider", [this](const std::string& name, float value) { uiManager.setSliderCallback("velocitySlider", [this](const std::string& name, float value) {
int newVel = roundf(value * 10); int newVel = roundf(value * 10);
if (newVel != Environment::shipSelectedVelocity) { if (newVel != Environment::shipSelectedVelocity) {
velocityChanged = true; newShipVelocity = newVel;
Environment::shipSelectedVelocity = newVel; //Environment::shipSelectedVelocity = newVel;
/*auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>( /*auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch() std::chrono::system_clock::now().time_since_epoch()
).count(); ).count();
@ -649,49 +649,37 @@ namespace ZL
void Game::processTickCount() { void Game::processTickCount() {
if (lastTickCount == 0) { if (lastTickCount == 0) {
lastTickCount = SDL_GetTicks64(); //lastTickCount = SDL_GetTicks64();
lastTickCount = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()
).count();
return; return;
} }
newTickCount = SDL_GetTicks64(); //newTickCount = SDL_GetTicks64();
newTickCount = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()
).count();
if (newTickCount - lastTickCount > CONST_TIMER_INTERVAL) { if (newTickCount - lastTickCount > CONST_TIMER_INTERVAL) {
size_t delta = (newTickCount - lastTickCount > CONST_MAX_TIME_INTERVAL) ?
CONST_MAX_TIME_INTERVAL : newTickCount - lastTickCount; size_t delta = newTickCount - lastTickCount;
if (delta > CONST_MAX_TIME_INTERVAL)
{
throw std::runtime_error("Synchronization is lost");
}
auto now_ms = newTickCount;
sparkEmitter.update(static_cast<float>(delta)); sparkEmitter.update(static_cast<float>(delta));
planetObject.update(static_cast<float>(delta)); planetObject.update(static_cast<float>(delta));
extrapolateRemotePlayers(); extrapolateRemotePlayers();
//bool sendRotation = false;
static float pingTimer = 0.0f; static float pingTimer = 0.0f;
pingTimer += delta; pingTimer += delta;
if (pingTimer >= 1000.0f) { if (pingTimer >= 1000.0f) {
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()
).count();
// 1. Извлекаем кватернион из матрицы поворота
/*
Eigen::Quaternionf q(Environment::shipMatrix);
// 2. Формируем строку PING согласно протоколу сервера
// Формат: PING:timestamp:posX:posY:posZ:qW:qX:qY:qZ:angVelX:angVelY:angVelZ:vel:selectedVel:discMag:discAngle
std::string pingMsg = "PING:" + std::to_string(now_ms) + ":"
+ std::to_string(Environment::shipPosition.x()) + ":"
+ std::to_string(Environment::shipPosition.y()) + ":"
+ std::to_string(Environment::shipPosition.z()) + ":"
+ std::to_string(q.w()) + ":"
+ std::to_string(q.x()) + ":"
+ std::to_string(q.y()) + ":"
+ std::to_string(q.z()) + ":"
+ std::to_string(Environment::currentAngularVelocity.x()) + ":"
+ std::to_string(Environment::currentAngularVelocity.y()) + ":"
+ std::to_string(Environment::currentAngularVelocity.z()) + ":"
+ std::to_string(Environment::shipVelocity) + ":"
+ std::to_string(Environment::shipSelectedVelocity) + ":"
+ std::to_string(Environment::lastSentMagnitude) + ":" // Используем те же static переменные из блока ROT
+ std::to_string(Environment::lastSentAngle);
*/
std::string pingMsg = "PING:" + std::to_string(now_ms) + ":" + formPingMessageContent(); std::string pingMsg = "PING:" + std::to_string(now_ms) + ":" + formPingMessageContent();
networkClient->Send(pingMsg); networkClient->Send(pingMsg);
@ -699,39 +687,10 @@ namespace ZL
pingTimer = 0.0f; pingTimer = 0.0f;
} }
//static const float CONST_ACCELERATION = 1.f; if (newShipVelocity != Environment::shipSelectedVelocity)
{
Environment::shipSelectedVelocity = newShipVelocity;
float shipDesiredVelocity = Environment::shipSelectedVelocity * 100.f;
if (!gameOver)
{
if (Environment::shipVelocity < shipDesiredVelocity)
{
Environment::shipVelocity += delta * SHIP_ACCEL;
if (Environment::shipVelocity > shipDesiredVelocity)
{
Environment::shipVelocity = shipDesiredVelocity;
}
}
else if (Environment::shipVelocity > shipDesiredVelocity)
{
Environment::shipVelocity -= delta * SHIP_ACCEL;
if (Environment::shipVelocity < shipDesiredVelocity)
{
Environment::shipVelocity = shipDesiredVelocity;
}
}
}
if (velocityChanged)
{
velocityChanged = false;
//if (newVel != Environment::shipSelectedVelocity) {
// velocityChanged = true;
//Environment::shipSelectedVelocity = newVel;
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()
).count();
std::string msg = "VEL:" + std::to_string(now_ms) + ":" + std::to_string(Environment::shipSelectedVelocity); std::string msg = "VEL:" + std::to_string(now_ms) + ":" + std::to_string(Environment::shipSelectedVelocity);
msg = msg + ":" + formPingMessageContent(); msg = msg + ":" + formPingMessageContent();
networkClient->Send(msg); networkClient->Send(msg);
@ -739,10 +698,6 @@ namespace ZL
} }
//static const float ANGULAR_ACCEL = 0.005f;
if (Environment::tapDownHold) { if (Environment::tapDownHold) {
float diffx = Environment::tapDownCurrentPos(0) - Environment::tapDownStartPos(0); float diffx = Environment::tapDownCurrentPos(0) - Environment::tapDownStartPos(0);
float diffy = Environment::tapDownCurrentPos(1) - Environment::tapDownStartPos(1); float diffy = Environment::tapDownCurrentPos(1) - Environment::tapDownStartPos(1);
@ -770,7 +725,12 @@ namespace ZL
Environment::lastSentAngle = discreteAngle; Environment::lastSentAngle = discreteAngle;
Environment::lastSentMagnitude = discreteMag; Environment::lastSentMagnitude = discreteMag;
sendRotation = true; std::string msg = "ROT:" + std::to_string(now_ms) + ":" + std::to_string(Environment::lastSentAngle) + ":" + std::to_string(Environment::lastSentMagnitude);
msg = msg + ":" + formPingMessageContent();
networkClient->Send(msg);
std::cout << "Sending: " << msg << std::endl;
//sendRotation = true;
/*auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>( /*auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch() std::chrono::system_clock::now().time_since_epoch()
).count(); ).count();
@ -828,19 +788,6 @@ namespace ZL
Environment::inverseShipMatrix = Environment::shipMatrix.inverse(); Environment::inverseShipMatrix = Environment::shipMatrix.inverse();
} }
if (sendRotation)
{
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()
).count();
// Формируем сетевой пакет
// Нам нужно отправить: дискретный угол, дискретную силу и текущую матрицу/позицию для синхронизации
std::string msg = "ROT:" + std::to_string(now_ms) + ":" + std::to_string(discreteAngle) + ":" + std::to_string(discreteMag);
msg = msg + ":" + formPingMessageContent();
networkClient->Send(msg);
std::cout << "Sending: " << msg << std::endl;
}
} }
} }
else { else {
@ -848,13 +795,16 @@ namespace ZL
int discreteAngle = -1; int discreteAngle = -1;
float discreteMag = 0.0f; float discreteMag = 0.0f;
bool sendRotation = false;
if (discreteAngle != Environment::lastSentAngle || discreteMag != Environment::lastSentMagnitude) { if (discreteAngle != Environment::lastSentAngle || discreteMag != Environment::lastSentMagnitude) {
Environment::lastSentAngle = discreteAngle; Environment::lastSentAngle = discreteAngle;
Environment::lastSentMagnitude = discreteMag; Environment::lastSentMagnitude = discreteMag;
sendRotation = true; std::string msg = "ROT:" + std::to_string(now_ms) + ":" + std::to_string(Environment::lastSentAngle) + ":" + std::to_string(Environment::lastSentMagnitude);
msg = msg + ":" + formPingMessageContent();
networkClient->Send(msg);
std::cout << "Sending: " << msg << std::endl;
//sendRotation = true;
/* /*
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>( auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch() std::chrono::system_clock::now().time_since_epoch()
@ -890,19 +840,24 @@ namespace ZL
Environment::shipMatrix = Environment::shipMatrix * rotateQuat.toRotationMatrix(); Environment::shipMatrix = Environment::shipMatrix * rotateQuat.toRotationMatrix();
Environment::inverseShipMatrix = Environment::shipMatrix.inverse(); Environment::inverseShipMatrix = Environment::shipMatrix.inverse();
} }
}
if (sendRotation) float shipDesiredVelocity = Environment::shipSelectedVelocity * 100.f;
if (Environment::shipVelocity < shipDesiredVelocity)
{ {
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>( Environment::shipVelocity += delta * SHIP_ACCEL;
std::chrono::system_clock::now().time_since_epoch() if (Environment::shipVelocity > shipDesiredVelocity)
).count(); {
Environment::shipVelocity = shipDesiredVelocity;
// Формируем сетевой пакет }
// Нам нужно отправить: дискретный угол, дискретную силу и текущую матрицу/позицию для синхронизации }
std::string msg = "ROT:" + std::to_string(now_ms) + ":" + std::to_string(discreteAngle) + ":" + std::to_string(discreteMag); else if (Environment::shipVelocity > shipDesiredVelocity)
msg = msg + ":" + formPingMessageContent(); {
networkClient->Send(msg); Environment::shipVelocity -= delta * SHIP_ACCEL;
std::cout << "Sending: " << msg << std::endl; if (Environment::shipVelocity < shipDesiredVelocity)
{
Environment::shipVelocity = shipDesiredVelocity;
} }
} }
@ -915,6 +870,20 @@ namespace ZL
Environment::shipPosition = Environment::shipPosition + velocityDirectionAdjusted; Environment::shipPosition = Environment::shipPosition + velocityDirectionAdjusted;
} }
/*
if (sendRotation)
{
sendRotation = false;
// Формируем сетевой пакет
// Нам нужно отправить: дискретный угол, дискретную силу и текущую матрицу/позицию для синхронизации
std::string msg = "ROT:" + std::to_string(now_ms) + ":" + std::to_string(Environment::lastSentAngle) + ":" + std::to_string(Environment::lastSentMagnitude);
msg = msg + ":" + formPingMessageContent();
networkClient->Send(msg);
std::cout << "Sending: " << msg << std::endl;
}
*/
for (auto& p : projectiles) { for (auto& p : projectiles) {
if (p && p->isActive()) { if (p && p->isActive()) {
p->update(static_cast<float>(delta), renderer); p->update(static_cast<float>(delta), renderer);

View File

@ -67,7 +67,7 @@ namespace ZL {
std::unordered_map<int, RemotePlayer> latestRemotePlayers; std::unordered_map<int, RemotePlayer> latestRemotePlayers;
bool velocityChanged = false; float newShipVelocity = 0;
static const size_t CONST_TIMER_INTERVAL = 10; static const size_t CONST_TIMER_INTERVAL = 10;
static const size_t CONST_MAX_TIME_INTERVAL = 1000; static const size_t CONST_MAX_TIME_INTERVAL = 1000;

View File

@ -166,7 +166,19 @@ namespace ZL {
{ {
std::lock_guard<std::mutex> pLock(playersMutex); std::lock_guard<std::mutex> pLock(playersMutex);
auto& rp = remotePlayers[remoteId]; auto& rp = remotePlayers[remoteId];
if (rp.timedRemoteStates.size() > 0 && rp.timedRemoteStates[rp.timedRemoteStates.size() - 1].lastUpdateServerTime == remoteState.lastUpdateServerTime)
{
rp.timedRemoteStates[rp.timedRemoteStates.size() - 1] = remoteState;
}
else
{
rp.timedRemoteStates.push_back(remoteState); rp.timedRemoteStates.push_back(remoteState);
}
//rp.timedRemoteStates.push_back(remoteState);
auto cutoff_time = nowTime - std::chrono::milliseconds(CUTOFF_TIME); auto cutoff_time = nowTime - std::chrono::milliseconds(CUTOFF_TIME);
@ -304,7 +316,15 @@ namespace ZL {
remoteState.lastUpdateServerTime = uptime_timepoint; remoteState.lastUpdateServerTime = uptime_timepoint;
auto& rp = remotePlayers[id]; auto& rp = remotePlayers[id];
if (rp.timedRemoteStates.size() > 0 && rp.timedRemoteStates[rp.timedRemoteStates.size() - 1].lastUpdateServerTime == remoteState.lastUpdateServerTime)
{
rp.timedRemoteStates[rp.timedRemoteStates.size() - 1] = remoteState;
}
else
{
rp.timedRemoteStates.push_back(remoteState); rp.timedRemoteStates.push_back(remoteState);
}
auto cutoff_time = now_ms - std::chrono::milliseconds(CUTOFF_TIME); auto cutoff_time = now_ms - std::chrono::milliseconds(CUTOFF_TIME);