From 09e7a5cc137267b2f1e0dadcc7138eb4c1f6fca7 Mon Sep 17 00:00:00 2001 From: hiker Date: Wed, 21 Feb 2018 22:46:35 +1100 Subject: [PATCH] Converted more times to using ticks; fixed bug that in case of a race restart the fastest lap was not reset. --- src/modes/linear_world.cpp | 28 ++++++++++++++++------------ src/modes/linear_world.hpp | 27 ++++++++++++++------------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/modes/linear_world.cpp b/src/modes/linear_world.cpp index 388df6a75..27397217d 100644 --- a/src/modes/linear_world.cpp +++ b/src/modes/linear_world.cpp @@ -38,6 +38,7 @@ #include "utils/string_utils.hpp" #include "utils/translation.hpp" +#include #include //----------------------------------------------------------------------------- @@ -49,7 +50,7 @@ LinearWorld::LinearWorld() : WorldWithRank() m_last_lap_sfx = SFXManager::get()->createSoundSource("last_lap_fanfare"); m_last_lap_sfx_played = false; m_last_lap_sfx_playing = false; - m_fastest_lap = 9999999.9f; + m_fastest_lap_ticks = INT_MAX; } // LinearWorld // ---------------------------------------------------------------------------- @@ -87,8 +88,9 @@ LinearWorld::~LinearWorld() void LinearWorld::reset() { WorldWithRank::reset(); - m_last_lap_sfx_played = false; + m_last_lap_sfx_played = false; m_last_lap_sfx_playing = false; + m_fastest_lap_ticks = INT_MAX; const unsigned int kart_amount = (unsigned int) m_karts.size(); for(unsigned int i=0; igetWorldKartId()==kart_index); - kart_info.m_time_at_last_lap=getTime(); + kart_info.m_ticks_at_last_lap=getTimeTicks(); kart_info.m_race_lap++; m_kart_info[kart_index].m_overall_distance = m_kart_info[kart_index].m_race_lap @@ -342,23 +344,24 @@ void LinearWorld::newLap(unsigned int kart_index) { kart->finishedRace(getTime()); } - float time_per_lap; + int ticks_per_lap; if (kart_info.m_race_lap == 1) // just completed first lap { - time_per_lap=getTime(); + ticks_per_lap = getTimeTicks(); } else //completing subsequent laps { - time_per_lap=getTime() - kart_info.m_lap_start_time; + ticks_per_lap = getTimeTicks() - kart_info.m_lap_start_ticks; } // if new fastest lap - if(time_per_lap < m_fastest_lap && raceHasLaps() && - kart_info.m_race_lap>0) + if(ticks_per_lap < m_fastest_lap_ticks && raceHasLaps() && + kart_info.m_race_lap>0 ) { - m_fastest_lap = time_per_lap; + m_fastest_lap_ticks = ticks_per_lap; - std::string s = StringUtils::timeToString(time_per_lap); + std::string s = StringUtils::timeToString( float(ticks_per_lap) + / stk_config->m_physics_fps); // Store the temporary string because clang would mess this up // (remove the stringw before the wchar_t* is used). @@ -376,7 +379,7 @@ void LinearWorld::newLap(unsigned int kart_index) } // end if new fastest lap - kart_info.m_lap_start_time = getTime(); + kart_info.m_lap_start_ticks = getTimeTicks(); kart->getController()->newLap(kart_info.m_race_lap); } // newLap @@ -422,7 +425,8 @@ float LinearWorld::getEstimatedFinishTime(const int kart_id) const float LinearWorld::getTimeAtLapForKart(const int kart_id) const { assert(kart_id < (int)m_kart_info.size()); - return m_kart_info[kart_id].m_time_at_last_lap; + return float(m_kart_info[kart_id].m_ticks_at_last_lap) + / stk_config->m_physics_fps; } // getTimeAtLapForKart //----------------------------------------------------------------------------- diff --git a/src/modes/linear_world.hpp b/src/modes/linear_world.hpp index 9137b8f72..a5206bc2f 100644 --- a/src/modes/linear_world.hpp +++ b/src/modes/linear_world.hpp @@ -18,11 +18,12 @@ #ifndef HEADER_LINEAR_WORLD_HPP #define HEADER_LINEAR_WORLD_HPP -#include - #include "modes/world_with_rank.hpp" #include "utils/aligned_array.hpp" +#include +#include + class SFXBase; /* @@ -42,8 +43,8 @@ private: bool m_last_lap_sfx_playing; - /** The fastest lap time. */ - float m_fastest_lap; + /** The fastest lap time, in ticks of physics dt. */ + int m_fastest_lap_ticks; /** The track length returned by Track::getLength() only covers the * distance from start line to finish line, i.e. it does not include @@ -65,10 +66,10 @@ private: int m_race_lap; /** Time at finishing last lap. */ - float m_time_at_last_lap; + int m_ticks_at_last_lap; /** Time at start of a new lap. */ - float m_lap_start_time; + int m_lap_start_ticks; /** During last lap only: estimated finishing time! */ float m_estimated_finish; @@ -83,11 +84,11 @@ private: /** Re-initialises all data. */ void reset() { - m_race_lap = -1; - m_lap_start_time = 0; - m_time_at_last_lap = 99999.9f; - m_estimated_finish = -1.0f; - m_overall_distance = 0.0f; + m_race_lap = -1; + m_lap_start_ticks = 0; + m_ticks_at_last_lap = INT_MAX; + m_estimated_finish = -1.0f; + m_overall_distance = 0.0f; } // reset }; // ------------------------------------------------------------------------ @@ -155,11 +156,11 @@ public: { return m_kart_info[kart_index].m_overall_distance; } // getOverallDistance - + // ------------------------------------------------------------------------ /** Returns time for the fastest laps */ float getFastestLap() const { - return m_fastest_lap; + return float(m_fastest_lap_ticks)/stk_config->m_physics_fps; } }; // LinearWorld