mirror of https://github.com/OpenRCT2/OpenRCT2.git
Turn Date into struct
This commit is contained in:
parent
fef1a27342
commit
7b072808ee
|
@ -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<uint16_t>(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<uint16_t>(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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -328,7 +328,7 @@ namespace OpenRCT2
|
|||
auto day = gameState.Date.GetDay();
|
||||
#endif
|
||||
|
||||
gameState.Date.Update();
|
||||
DateUpdate(gameState);
|
||||
|
||||
ScenarioUpdate(gameState);
|
||||
ClimateUpdate();
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<uint16_t>(value) };
|
||||
}
|
||||
|
||||
uint32_t yearsElapsed_get() const
|
||||
|
|
|
@ -51,7 +51,7 @@ struct rct_ride;
|
|||
|
||||
namespace OpenRCT2
|
||||
{
|
||||
class Date;
|
||||
struct Date;
|
||||
|
||||
class Park final
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue