From 102e3556cf778fa46d24e8183ab16538fceb76e1 Mon Sep 17 00:00:00 2001 From: David Markowitz <39972741+EmosewaMC@users.noreply.github.com> Date: Sat, 26 Oct 2024 20:59:50 -0700 Subject: [PATCH] feat: add millisecond tracking for racing (#1615) * add millisecond tracking Update RacingControlComponent.cpp * remove const ig? * is this what you wanted --- dGame/dComponents/RacingControlComponent.cpp | 40 ++++++++++---------- dGame/dComponents/RacingControlComponent.h | 9 +++-- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/dGame/dComponents/RacingControlComponent.cpp b/dGame/dComponents/RacingControlComponent.cpp index c701080d..ab6d5d17 100644 --- a/dGame/dComponents/RacingControlComponent.cpp +++ b/dGame/dComponents/RacingControlComponent.cpp @@ -45,7 +45,6 @@ RacingControlComponent::RacingControlComponent(Entity* parent) m_LoadedPlayers = 0; m_LoadTimer = 0; m_Finished = 0; - m_StartTime = 0; m_EmptyTimer = 0; m_SoloRacing = Game::config->GetValue("solo_racing") == "1"; @@ -427,9 +426,9 @@ void RacingControlComponent::Serialize(RakNet::BitStream& outBitStream, bool bIs outBitStream.Write(player.playerID); outBitStream.Write(player.data[0]); - if (player.finished != 0) outBitStream.Write(player.raceTime); + if (player.finished != 0) outBitStream.Write(player.raceTime.count() / 1000.0f); else outBitStream.Write(player.data[1]); - if (player.finished != 0) outBitStream.Write(player.bestLapTime); + if (player.finished != 0) outBitStream.Write(player.bestLapTime.count() / 1000.0f); else outBitStream.Write(player.data[2]); if (player.finished == 1) outBitStream.Write(1.0f); else outBitStream.Write(player.data[3]); @@ -490,8 +489,8 @@ void RacingControlComponent::Serialize(RakNet::BitStream& outBitStream, bool bIs if (player.finished == 0) continue; outBitStream.Write1(); // Has more data outBitStream.Write(player.playerID); - outBitStream.Write(player.bestLapTime); - outBitStream.Write(player.raceTime); + outBitStream.Write(player.bestLapTime.count() / 1000.0f); + outBitStream.Write(player.raceTime.count() / 1000.0f); } outBitStream.Write0(); // No more data @@ -721,7 +720,7 @@ void RacingControlComponent::Update(float deltaTime) { Game::entityManager->SerializeEntity(m_Parent); - m_StartTime = std::time(nullptr); + m_StartTime = std::chrono::high_resolution_clock::now(); } m_StartTimer += deltaTime; @@ -810,46 +809,45 @@ void RacingControlComponent::Update(float deltaTime) { // Reached the start point, lapped if (respawnIndex == 0) { - time_t lapTime = std::time(nullptr) - (player.lap == 0 ? m_StartTime : player.lapTime); + const auto now = std::chrono::high_resolution_clock::now(); + const auto lapTime = std::chrono::duration_cast(now - (player.lap == 0 ? m_StartTime : player.lapTime)); // Cheating check - if (lapTime < 40) { + if (lapTime.count() < 40000) { continue; } - player.lap++; + player.lapTime = now; - player.lapTime = std::time(nullptr); - - if (player.bestLapTime == 0 || player.bestLapTime > lapTime) { + if (player.bestLapTime > lapTime || player.lap == 0) { player.bestLapTime = lapTime; LOG("Best lap time (%llu)", lapTime); } + player.lap++; + auto* missionComponent = playerEntity->GetComponent(); if (missionComponent != nullptr) { // Progress lap time tasks - missionComponent->Progress(eMissionTaskType::RACING, (lapTime) * 1000, static_cast(eRacingTaskParam::LAP_TIME)); + missionComponent->Progress(eMissionTaskType::RACING, lapTime.count(), static_cast(eRacingTaskParam::LAP_TIME)); if (player.lap == 3) { m_Finished++; player.finished = m_Finished; - const auto raceTime = - (std::time(nullptr) - m_StartTime); + const auto raceTime = std::chrono::duration_cast(now - m_StartTime); player.raceTime = raceTime; - LOG("Completed time %llu, %llu", - raceTime, raceTime * 1000); + LOG("Completed time %llums %fs", raceTime.count(), raceTime.count() / 1000.0f); - LeaderboardManager::SaveScore(playerEntity->GetObjectID(), m_ActivityID, static_cast(player.raceTime), static_cast(player.bestLapTime), static_cast(player.finished == 1)); + LeaderboardManager::SaveScore(playerEntity->GetObjectID(), m_ActivityID, static_cast(player.raceTime.count()) / 1000, static_cast(player.bestLapTime.count()) / 1000, static_cast(player.finished == 1)); // Entire race time - missionComponent->Progress(eMissionTaskType::RACING, (raceTime) * 1000, static_cast(eRacingTaskParam::TOTAL_TRACK_TIME)); + missionComponent->Progress(eMissionTaskType::RACING, player.raceTime.count(), static_cast(eRacingTaskParam::TOTAL_TRACK_TIME)); missionComponent->Progress(eMissionTaskType::RACING, 0, static_cast(eRacingTaskParam::COMPETED_IN_RACE)); // Progress task for competing in a race missionComponent->Progress(eMissionTaskType::RACING, player.smashedTimes, static_cast(eRacingTaskParam::SAFE_DRIVER)); // Finish a race without being smashed. @@ -873,8 +871,8 @@ void RacingControlComponent::Update(float deltaTime) { } } - LOG("Lapped (%i) in (%llu)", player.lap, - lapTime); + LOG("Lapped (%i) in (%llums %fs)", player.lap, + lapTime.count(), lapTime.count() / 1000.0f); } LOG("Reached point (%i)/(%i)", player.respawnIndex, diff --git a/dGame/dComponents/RacingControlComponent.h b/dGame/dComponents/RacingControlComponent.h index 790459e3..43c7e158 100644 --- a/dGame/dComponents/RacingControlComponent.h +++ b/dGame/dComponents/RacingControlComponent.h @@ -8,6 +8,7 @@ #include "Entity.h" #include "Component.h" #include "eReplicaComponentType.h" +#include /** * Information for each player in the race @@ -72,12 +73,12 @@ struct RacingPlayerInfo { /** * The fastest lap time of the player */ - time_t bestLapTime = 0; + std::chrono::milliseconds bestLapTime; /** * The current lap time of the player */ - time_t lapTime = 0; + std::chrono::high_resolution_clock::time_point lapTime; /** * The number of times this player smashed their car @@ -97,7 +98,7 @@ struct RacingPlayerInfo { /** * Unused */ - time_t raceTime = 0; + std::chrono::milliseconds raceTime; }; /** @@ -231,7 +232,7 @@ private: /** * The time the race was started */ - time_t m_StartTime; + std::chrono::high_resolution_clock::time_point m_StartTime; /** * Timer for tracking how long a player was alone in this race