diff --git a/SQCSim-srv/connection.cpp b/SQCSim-srv/connection.cpp index 29d30f4..989a8ae 100644 --- a/SQCSim-srv/connection.cpp +++ b/SQCSim-srv/connection.cpp @@ -72,7 +72,9 @@ void Connection::getPacks(SOCKET sock) { lsPck.clear(); } -void Connection::sendPacks(SOCKET sock, std::unordered_map conns) { +void Connection::sendPacks(SOCKET sock, std::unordered_map conns, const uint32_t timer) { + static int outs = 0; + static Timestamp last = 0; while (!m_output_vector.empty()) { Output out = m_output_vector.front(); for (auto& [key, conn] : conns) { @@ -83,6 +85,26 @@ void Connection::sendPacks(SOCKET sock, std::unordered_map(sock, &out, &m_bufout, conn->getAddr()); } + ++outs; + + [[unlikely]] if (last == 0) // ! + last = out.timestamp; + + outs += out.timestamp + last; + + if (outs >= 1000) { + outs -= 1000; + Sync sync; + sync.hp = player.get()->GetHP(); + sync.timestamp = out.timestamp; + sync.position = out.position; + sync.sid = m_loginfo.sid; + sync.timer = timer; + sync.timestamp = out.timestamp; + sync.ammo = -1; + sendPackTo(sock, &sync, &m_bufout, &m_addr); + } + m_output_vector.pop_front(); } } diff --git a/SQCSim-srv/connection.h b/SQCSim-srv/connection.h index 24530de..a9d171a 100644 --- a/SQCSim-srv/connection.h +++ b/SQCSim-srv/connection.h @@ -34,7 +34,7 @@ public: sockaddr_in* getAddr() const; void getPacks(SOCKET sock); - void sendPacks(SOCKET sock, std::unordered_map conns); + void sendPacks(SOCKET sock, std::unordered_map conns, const uint32_t timer); void Run(World* world); diff --git a/SQCSim2021/engine.cpp b/SQCSim2021/engine.cpp index 15e2f08..240c8bb 100644 --- a/SQCSim2021/engine.cpp +++ b/SQCSim2021/engine.cpp @@ -1275,14 +1275,19 @@ void Engine::Render(float elapsedTime) { using namespace std::chrono; using namespace netprot; Timestamp tstamp = duration_cast(high_resolution_clock::now() - m_startTime).count(); + static Timestamp last = 0; Input input; Sync sync; uint64_t id = m_conn.getId(); static std::vector lsPck; - static uint64_t sync_acc = 0; - sync_acc += tstamp; + static int sync_acc = 0; - if (sync_acc >= 1000 || !has_synced) { + if (last == 0) + last = tstamp; + + sync_acc += tstamp - last; + + if (sync_acc >= 1000) { sync_acc -= 1000; sync.sid = id; sync.timestamp = tstamp; @@ -1322,13 +1327,15 @@ void Engine::Render(float elapsedTime) { case SYNC: if (Deserialize(&sync, pck, &bsize)) { if (sync.sid != m_conn.getId()) { - std::cout << "syncid be no good."; + std::cout << "syncsid be no good."; break; } if (m_syncs.contains(sync.timestamp)) { Sync comp = m_syncs[sync.timestamp]; m_player.InflictDamage(sync.hp - comp.hp); + std::cout << "SID: " << sync.sid << "HP: " << sync.hp << " Pos: " << sync.position << std::endl; + Vector3f diff = sync.position - comp.position; if (diff.Length() > .5) m_player.Move(diff); diff --git a/SQCSim2021/main.cpp b/SQCSim2021/main.cpp index 98dc399..a0111fd 100644 --- a/SQCSim2021/main.cpp +++ b/SQCSim2021/main.cpp @@ -5,5 +5,5 @@ int main() { Engine engine; engine.SetMaxFps(60); - engine.Start("Syndicat Quebecois de la Construction Simulator 2023", 1920, 1080, false); + engine.Start("Syndicat Quebecois de la Construction Simulator 2023", 800, 600, false); }