diff --git a/src/util/time/CGameTime.cpp b/src/util/time/CGameTime.cpp index 94810ab..6cf165c 100644 --- a/src/util/time/CGameTime.cpp +++ b/src/util/time/CGameTime.cpp @@ -1,6 +1,47 @@ #include "util/time/CGameTime.hpp" #include "common/Time.hpp" +void CGameTime::GameTimeSetTime(const WowTime& time, bool shouldTick) { + WowTime biasTime = time; + + if (this->m_timeBias) { + auto minutes = biasTime.GetHourAndMinutes() + this->m_timeBias; + + if (minutes < 0) { + minutes += 1440; + } else { + minutes %= 1440; + } + + biasTime.SetHourAndMinutes(minutes); + } + + if (this->m_dateBias) { + biasTime.AddDays(this->m_dateBias, false); + } + + static_cast(*this) = biasTime; + + if (shouldTick) { + // Rewind time by exactly one minute + if (this->m_minute != 0) { + this->m_minute--; + } else { + this->m_minute = 59; + + if (this->m_hour != 0) { + this->m_hour--; + } else { + this->m_hour = 23; + this->AddDays(-1, false); + } + } + + // Tick ahead exactly one minute (ensures callbacks fire and various counters update) + this->TickMinute(); + } +} + void CGameTime::PerformCallbacks(int32_t minutes) { // TODO } diff --git a/src/util/time/CGameTime.hpp b/src/util/time/CGameTime.hpp index d8f53c0..445dbf9 100644 --- a/src/util/time/CGameTime.hpp +++ b/src/util/time/CGameTime.hpp @@ -6,8 +6,7 @@ class CGameTime : public WowTime { public: // Public member functions - void PerformCallbacks(int32_t minutes); - void TickMinute(); + void GameTimeSetTime(const WowTime& time, bool shouldTick); private: // Private member variables @@ -24,6 +23,10 @@ class CGameTime : public WowTime { float float48 = 0.0f; float float4C = 0.0f; // TODO m_callbackLists + + // Private member functions + void PerformCallbacks(int32_t minutes); + void TickMinute(); }; #endif diff --git a/test/util/time/CGameTime.cpp b/test/util/time/CGameTime.cpp index 825da49..88dfd6d 100644 --- a/test/util/time/CGameTime.cpp +++ b/test/util/time/CGameTime.cpp @@ -16,13 +16,24 @@ TEST_CASE("CGameTime::CGameTime", "[util]") { } } -TEST_CASE("CGameTime::TickMinute", "[util]") { - SECTION("ticks minute correctly") { - CGameTime time; - time.SetHourAndMinutes(0, 0); - time.TickMinute(); +TEST_CASE("CGameTime::GameTimeSetTime", "[util]") { + SECTION("sets game time correctly") { + WowTime time; + time.m_minute = 18; + time.m_hour = 11; + time.m_weekday = 3; + time.m_monthday = 27; + time.m_month = 0; + time.m_year = 26; - CHECK(time.m_hour == 0); - CHECK(time.m_minute == 1); + CGameTime gameTime; + gameTime.GameTimeSetTime(time, true); + + CHECK(gameTime.m_minute == 18); + CHECK(gameTime.m_hour == 11); + CHECK(gameTime.m_weekday == 3); + CHECK(gameTime.m_monthday == 27); + CHECK(gameTime.m_month == 0); + CHECK(gameTime.m_year == 26); } }