From 017eb4df72b909325dde6b90642091846afa922e Mon Sep 17 00:00:00 2001 From: Ted John Date: Wed, 25 Oct 2017 12:37:20 +0100 Subject: [PATCH] Refactor date update and checks to new functions --- src/openrct2/localisation/date.c | 41 ++++++++++++++++++++++++++++++++ src/openrct2/localisation/date.h | 5 ++++ src/openrct2/scenario/scenario.c | 25 +++++++++---------- 3 files changed, 57 insertions(+), 14 deletions(-) diff --git a/src/openrct2/localisation/date.c b/src/openrct2/localisation/date.c index 8ab6ccbad3..534f77c99a 100644 --- a/src/openrct2/localisation/date.c +++ b/src/openrct2/localisation/date.c @@ -67,6 +67,20 @@ void date_reset() gCurrentTicks = 0; } +void date_update() +{ + sint32 monthTicks = gDateMonthTicks + 4; + if (monthTicks >= 0x10000) + { + gDateMonthTicks = 0; + gDateMonthsElapsed++; + } + else + { + gDateMonthTicks = floor2((uint16)monthTicks, 4); + } +} + void date_update_real_time_of_day() { time_t timestamp = time(0); @@ -76,3 +90,30 @@ void date_update_real_time_of_day() gRealTimeOfDay.minute = now->tm_min; gRealTimeOfDay.hour = now->tm_hour; } + +bool date_is_day_start(sint32 monthTicks) +{ + if (monthTicks < 4) + { + return false; + } + sint32 prevMonthTick = monthTicks - 4; + sint32 currentMonth = date_get_month(gDateMonthsElapsed); + sint32 currentDaysInMonth = days_in_month[currentMonth]; + return ((currentDaysInMonth * monthTicks) >> 16 != (currentDaysInMonth * prevMonthTick) >> 16); +} + +bool date_is_week_start(sint32 monthTicks) +{ + return (monthTicks & 0x3FFF) == 0; +} + +bool date_is_fortnight_start(sint32 monthTicks) +{ + return (monthTicks & 0x7FFF) == 0; +} + +bool date_is_month_start(sint32 monthTicks) +{ + return (monthTicks == 0); +} diff --git a/src/openrct2/localisation/date.h b/src/openrct2/localisation/date.h index c64d25049e..8065a7b9bc 100644 --- a/src/openrct2/localisation/date.h +++ b/src/openrct2/localisation/date.h @@ -62,7 +62,12 @@ sint32 date_get_month(sint32 months); sint32 date_get_year(sint32 months); sint32 date_get_total_months(sint32 month, sint32 year); void date_reset(); +void date_update(); void date_update_real_time_of_day(); +bool date_is_day_start(sint32 monthTicks); +bool date_is_week_start(sint32 monthTicks); +bool date_is_fortnight_start(sint32 monthTicks); +bool date_is_month_start(sint32 monthTicks); #ifdef __cplusplus } diff --git a/src/openrct2/scenario/scenario.c b/src/openrct2/scenario/scenario.c index 198a25bdfd..86750037c8 100644 --- a/src/openrct2/scenario/scenario.c +++ b/src/openrct2/scenario/scenario.c @@ -394,29 +394,26 @@ static void scenario_update_daynight_cycle() */ void scenario_update() { - if (!(gScreenFlags & ~SCREEN_FLAGS_PLAYING)) { - uint32 currentMonthTick = floor2(gDateMonthTicks, 4); - uint32 nextMonthTick = currentMonthTick + 4; - uint8 currentMonth = gDateMonthsElapsed & 7; - uint8 currentDaysInMonth = (uint8)days_in_month[currentMonth]; - - if ((currentDaysInMonth * nextMonthTick) >> 16 != (currentDaysInMonth * currentMonthTick) >> 16) { + if (gScreenFlags == SCREEN_FLAGS_PLAYING) + { + date_update(); + if (date_is_day_start(gDateMonthTicks)) + { scenario_day_update(); } - if (nextMonthTick % 0x4000 == 0) { + if (date_is_week_start(gDateMonthTicks)) + { scenario_week_update(); } - if (nextMonthTick % 0x8000 == 0) { + if (date_is_fortnight_start(gDateMonthTicks)) + { scenario_fortnight_update(); } - - gDateMonthTicks = (uint16)nextMonthTick; - if (nextMonthTick >= 0x10000) { - gDateMonthsElapsed++; + if (date_is_month_start(gDateMonthTicks)) + { scenario_month_update(); } } - scenario_update_daynight_cycle(); }