diff --git a/src/openrct2/Date.cpp b/src/openrct2/Date.cpp index 8ea9fea9f7..5c81321702 100644 --- a/src/openrct2/Date.cpp +++ b/src/openrct2/Date.cpp @@ -31,12 +31,6 @@ static const int16_t days_in_month[MONTH_COUNT] = { RealWorldTime gRealTimeOfDay; -Date::Date(uint32_t monthsElapsed, uint16_t monthTicks) - : _monthTicks(monthTicks) - , _monthsElapsed(monthsElapsed) -{ -} - Date Date::FromYMD(int32_t year, int32_t month, int32_t day) { year = std::clamp(year, 0, kMaxYear - 1); @@ -44,83 +38,69 @@ Date Date::FromYMD(int32_t year, int32_t month, int32_t day) auto daysInMonth = days_in_month[month]; day = std::clamp(day, 0, daysInMonth - 1); - int32_t monthsElapsed = (year * MONTH_COUNT) + month; + uint32_t paramMonthsElapsed = (year * MONTH_COUNT) + month; // Day - int32_t monthTicks = 0; + uint16_t paramMonthTicks = 0; if (day != 0) { - monthTicks = ((day << 16) / daysInMonth) + kMonthTicksIncrement; + paramMonthTicks = ((day << 16) / daysInMonth) + kMonthTicksIncrement; } - return Date(monthsElapsed, monthTicks); -} - -void Date::Update() -{ - int32_t monthTicks = _monthTicks + kMonthTicksIncrement; - if (monthTicks > kMaskMonthTicks) - { - _monthTicks = 0; - _monthsElapsed++; - } - else - { - _monthTicks = static_cast(monthTicks); - } + return Date{ paramMonthsElapsed, paramMonthTicks }; } uint16_t Date::GetMonthTicks() const { - return _monthTicks; + return monthTicks; } uint32_t Date::GetMonthsElapsed() const { - return _monthsElapsed; + return monthsElapsed; } int32_t Date::GetDay() const { auto month = GetMonth(); auto daysInMonth = GetDaysInMonth(month); - return ((_monthTicks * daysInMonth) >> 16) & 0xFF; + return ((monthTicks * daysInMonth) >> 16) & 0xFF; } int32_t Date::GetMonth() const { - return _monthsElapsed % MONTH_COUNT; + return monthsElapsed % MONTH_COUNT; } int32_t Date::GetYear() const { - return _monthsElapsed / MONTH_COUNT; + return monthsElapsed / MONTH_COUNT; } bool Date::IsDayStart() const { - if (_monthTicks < 4) + if (monthTicks < 4) { return false; } - int32_t prevMonthTick = _monthTicks - 4; + int32_t prevMonthTick = monthTicks - 4; int32_t currentMonth = GetMonth(); int32_t currentDaysInMonth = GetDaysInMonth(currentMonth); - return ((currentDaysInMonth * _monthTicks) >> 16 != (currentDaysInMonth * prevMonthTick) >> 16); + return ((currentDaysInMonth * monthTicks) >> 16 != (currentDaysInMonth * prevMonthTick) >> 16); } bool Date::IsWeekStart() const { - return (_monthTicks & kMaskWeekTicks) == 0; + return (monthTicks & kMaskWeekTicks) == 0; } bool Date::IsFortnightStart() const { - return (_monthTicks & kMaskFortnightTicks) == 0; + return (monthTicks & kMaskFortnightTicks) == 0; } bool Date::IsMonthStart() const { - return (_monthTicks == 0); + return (monthTicks == 0); } int32_t Date::GetDaysInMonth(int32_t month) @@ -130,6 +110,20 @@ int32_t Date::GetDaysInMonth(int32_t month) return days_in_month[month]; } +void OpenRCT2::DateUpdate(GameState_t& gameState) +{ + int32_t monthTicks = gameState.Date.monthTicks + kMonthTicksIncrement; + if (monthTicks > kMaskMonthTicks) + { + gameState.Date.monthTicks = 0; + gameState.Date.monthsElapsed++; + } + else + { + gameState.Date.monthTicks = static_cast(monthTicks); + } +} + int32_t DateGetMonth(int32_t months) { return months % MONTH_COUNT; @@ -167,6 +161,6 @@ Date& GetDate() void ResetDate() { auto& gameState = GetGameState(); - gameState.Date = OpenRCT2::Date(); + gameState.Date = {}; gCurrentRealTimeTicks = 0; } diff --git a/src/openrct2/Date.h b/src/openrct2/Date.h index e3447e80c4..a300d981b2 100644 --- a/src/openrct2/Date.h +++ b/src/openrct2/Date.h @@ -38,23 +38,18 @@ enum namespace OpenRCT2 { + struct GameState_t; + /** * Represents the current day, month and year in OpenRCT2. */ - class Date final + struct Date final { - private: - uint16_t _monthTicks = 0; - uint32_t _monthsElapsed = 0; - - public: - Date() = default; - Date(uint32_t monthsElapsed, uint16_t monthTicks); + uint32_t monthsElapsed = 0; + uint16_t monthTicks = 0; static Date FromYMD(int32_t year, int32_t month = 0, int32_t day = 0); - void Update(); - uint16_t GetMonthTicks() const; uint32_t GetMonthsElapsed() const; int32_t GetDay() const; @@ -68,6 +63,8 @@ namespace OpenRCT2 static int32_t GetDaysInMonth(int32_t month); }; + + void DateUpdate(GameState_t& gameState); } // namespace OpenRCT2 struct RealWorldDate diff --git a/src/openrct2/GameState.cpp b/src/openrct2/GameState.cpp index 02fccbd2ca..1a15084111 100644 --- a/src/openrct2/GameState.cpp +++ b/src/openrct2/GameState.cpp @@ -328,7 +328,7 @@ namespace OpenRCT2 auto day = gameState.Date.GetDay(); #endif - gameState.Date.Update(); + DateUpdate(gameState); ScenarioUpdate(gameState); ClimateUpdate(); diff --git a/src/openrct2/park/ParkFile.cpp b/src/openrct2/park/ParkFile.cpp index 851da7182f..07df6d2f70 100644 --- a/src/openrct2/park/ParkFile.cpp +++ b/src/openrct2/park/ParkFile.cpp @@ -484,7 +484,7 @@ namespace OpenRCT2 uint32_t monthsElapsed; cs.ReadWrite(monthTicks); cs.ReadWrite(monthsElapsed); - gameState.Date = Date(monthsElapsed, monthTicks); + gameState.Date = Date{ monthsElapsed, monthTicks }; } else { diff --git a/src/openrct2/rct1/S4Importer.cpp b/src/openrct2/rct1/S4Importer.cpp index 7df50b82ea..80832515c8 100644 --- a/src/openrct2/rct1/S4Importer.cpp +++ b/src/openrct2/rct1/S4Importer.cpp @@ -2144,7 +2144,7 @@ namespace RCT1 // Date and srand gameState.CurrentTicks = _s4.Ticks; ScenarioRandSeed(_s4.RandomA, _s4.RandomB); - gameState.Date = Date(_s4.Month, _s4.Day); + gameState.Date = Date{ _s4.Month, _s4.Day }; // Park rating gameState.ParkRating = _s4.ParkRating; diff --git a/src/openrct2/rct2/S6Importer.cpp b/src/openrct2/rct2/S6Importer.cpp index b7ab65bcff..0e66eb6397 100644 --- a/src/openrct2/rct2/S6Importer.cpp +++ b/src/openrct2/rct2/S6Importer.cpp @@ -251,7 +251,7 @@ namespace RCT2 gameState.ScenarioDetails = loadMaybeUTF8(_s6.ScenarioDescription); } - gameState.Date = OpenRCT2::Date(_s6.ElapsedMonths, _s6.CurrentDay); + gameState.Date = OpenRCT2::Date{ _s6.ElapsedMonths, _s6.CurrentDay }; gameState.CurrentTicks = _s6.GameTicks1; ScenarioRandSeed(_s6.ScenarioSrand0, _s6.ScenarioSrand1); diff --git a/src/openrct2/scripting/bindings/world/ScDate.hpp b/src/openrct2/scripting/bindings/world/ScDate.hpp index 491272e411..7f4ca98786 100644 --- a/src/openrct2/scripting/bindings/world/ScDate.hpp +++ b/src/openrct2/scripting/bindings/world/ScDate.hpp @@ -43,10 +43,10 @@ namespace OpenRCT2::Scripting return date.GetMonthsElapsed(); } - void monthsElapsed_set(int32_t value) + void monthsElapsed_set(uint32_t value) { ThrowIfGameStateNotMutable(); - GetGameState().Date = Date(value, GetDate().GetMonthTicks()); + GetGameState().Date = Date{ value, GetDate().GetMonthTicks() }; } uint32_t monthProgress_get() const @@ -58,7 +58,7 @@ namespace OpenRCT2::Scripting void monthProgress_set(int32_t value) { ThrowIfGameStateNotMutable(); - GetGameState().Date = Date(GetDate().GetMonthsElapsed(), value); + GetGameState().Date = Date{ GetDate().GetMonthsElapsed(), static_cast(value) }; } uint32_t yearsElapsed_get() const diff --git a/src/openrct2/world/Park.h b/src/openrct2/world/Park.h index b11488dfa7..6ad9f7565b 100644 --- a/src/openrct2/world/Park.h +++ b/src/openrct2/world/Park.h @@ -51,7 +51,7 @@ struct rct_ride; namespace OpenRCT2 { - class Date; + struct Date; class Park final {