diff --git a/include/fake_rtc.h b/include/fake_rtc.h index 0b3df8b8dc..92599b987a 100644 --- a/include/fake_rtc.h +++ b/include/fake_rtc.h @@ -3,10 +3,11 @@ #include "siirtc.h" -struct Time* FakeRtc_GetCurrentTime(void); +void FakeRtc_Reset(void); +struct SiiRtcInfo* FakeRtc_GetCurrentTime(void); void FakeRtc_GetRawInfo(struct SiiRtcInfo *rtc); -void FakeRtc_AdvanceTimeBy(s16 days, u32 hours, u32 minutes, u32 seconds); -void FakeRtc_ManuallySetTime(u32 hour, u32 minute, u32 second); +void FakeRtc_AdvanceTimeBy(u32 days, u32 hours, u32 minutes, u32 seconds); +void FakeRtc_ManuallySetTime(u32 day, u32 hour, u32 minute, u32 second); void FakeRtc_TickTimeForward(void); u32 FakeRtc_GetSecondsRatio(void); diff --git a/include/global.h b/include/global.h index 5846f09359..da0f7480b3 100644 --- a/include/global.h +++ b/include/global.h @@ -5,6 +5,7 @@ #include #include "config/general.h" // we need to define config before gba headers as print stuff needs the functions nulled before defines. #include "gba/gba.h" +#include "siirtc.h" #include "fpmath.h" #include "metaprogram.h" #include "constants/global.h" @@ -206,7 +207,7 @@ struct Time struct SaveBlock3 { #if OW_USE_FAKE_RTC - struct Time fakeRTC; + struct SiiRtcInfo fakeRTC; #endif #if OW_SHOW_ITEM_DESCRIPTIONS == OW_ITEM_DESCRIPTIONS_FIRST_TIME u8 itemFlags[ITEM_FLAGS_COUNT]; diff --git a/include/siirtc.h b/include/siirtc.h index ad13fc62f3..52a365cfcc 100644 --- a/include/siirtc.h +++ b/include/siirtc.h @@ -13,6 +13,18 @@ #define MINUTES_PER_HOUR 60 #define SECONDS_PER_MINUTE 60 +enum +{ + WEEKDAY_SUN, + WEEKDAY_MON, + WEEKDAY_TUE, + WEEKDAY_WED, + WEEKDAY_THU, + WEEKDAY_FRI, + WEEKDAY_SAT, + WEEKDAY_COUNT, +}; + enum { MONTH_JAN = 1, diff --git a/src/fake_rtc.c b/src/fake_rtc.c index 771a98da4e..4647a11240 100644 --- a/src/fake_rtc.c +++ b/src/fake_rtc.c @@ -6,7 +6,17 @@ #include "fake_rtc.h" #include "event_data.h" -struct Time *FakeRtc_GetCurrentTime(void) +void FakeRtc_Reset(void) +{ +#if OW_USE_FAKE_RTC + memset(&gSaveBlock3Ptr->fakeRTC, 0, sizeof(gSaveBlock3Ptr->fakeRTC)); + gSaveBlock3Ptr->fakeRTC.month = MONTH_JAN; + gSaveBlock3Ptr->fakeRTC.day = 1; + gSaveBlock3Ptr->fakeRTC.dayOfWeek = WEEKDAY_SAT; +#endif +} + +struct SiiRtcInfo *FakeRtc_GetCurrentTime(void) { #if OW_USE_FAKE_RTC return &gSaveBlock3Ptr->fakeRTC; @@ -15,47 +25,11 @@ struct Time *FakeRtc_GetCurrentTime(void) #endif } -static void FakeRtc_ConvertTimeToRtc(struct SiiRtcInfo* rtc, struct Time *time) -{ - u8 day = 1; - u8 month = MONTH_JAN; - u16 year = 2000; - u16 days = time->days - 1; - - rtc->second = time->seconds; - rtc->minute = time->minutes; - rtc->hour = time->hours; - - while (days > (365 + ((month > MONTH_FEB && IsLeapYear(year + 1)) || (month <= MONTH_FEB && IsLeapYear(year))))) - { - days -= (365 + ((month > MONTH_FEB && IsLeapYear(year + 1)) || (month <= MONTH_FEB && IsLeapYear(year)))); - year++; - } - - while (days > ((sNumDaysInMonths[month - 1] + (month == MONTH_FEB && IsLeapYear(year))) - day)) - { - days -= (sNumDaysInMonths[month - 1] + (month == MONTH_FEB && IsLeapYear(year))); - day = 1; - month++; - } - - while (month > 12) - { - year++; - month -= 12; - } - - year -= 2000; - - rtc->day = day + days; - rtc->month = month; - rtc->year = year; -} - void FakeRtc_GetRawInfo(struct SiiRtcInfo *rtc) { - struct Time* time = FakeRtc_GetCurrentTime(); - FakeRtc_ConvertTimeToRtc(rtc, time); + struct SiiRtcInfo *fakeRtc = FakeRtc_GetCurrentTime(); + if (fakeRtc != NULL) + memcpy(rtc, fakeRtc, sizeof(struct SiiRtcInfo)); } void FakeRtc_TickTimeForward(void) @@ -69,15 +43,13 @@ void FakeRtc_TickTimeForward(void) FakeRtc_AdvanceTimeBy(0, 0, 0, FakeRtc_GetSecondsRatio()); } -void FakeRtc_AdvanceTimeBy(s16 days, u32 hours, u32 minutes, u32 seconds) +void FakeRtc_AdvanceTimeBy(u32 days, u32 hours, u32 minutes, u32 seconds) { - struct Time* time = FakeRtc_GetCurrentTime(); - if (time == NULL) - return; - seconds += time->seconds; - minutes += time->minutes; - hours += time->hours; - days += time->days; + struct SiiRtcInfo *rtc = FakeRtc_GetCurrentTime(); + + seconds += rtc->second; + minutes += rtc->minute; + hours += rtc->hour; while(seconds >= SECONDS_PER_MINUTE) { @@ -97,24 +69,45 @@ void FakeRtc_AdvanceTimeBy(s16 days, u32 hours, u32 minutes, u32 seconds) hours -= HOURS_PER_DAY; } - time->seconds = seconds; - time->minutes = minutes; - time->hours = hours; - time->days = days; + rtc->second = seconds; + rtc->minute = minutes; + rtc->hour = hours; + + while (days > 0) + { + u32 remainingDaysInMonth = (sNumDaysInMonths[rtc->month - 1] + (rtc->month == MONTH_FEB && IsLeapYear(rtc->year)) - rtc->day); + + if (days > remainingDaysInMonth) + { + rtc->day = 1; + rtc->month++; + if (rtc->month > MONTH_DEC) + { + rtc->month = MONTH_JAN; + rtc->year++; + } + days -= (remainingDaysInMonth + 1); + rtc->dayOfWeek = (rtc->dayOfWeek + remainingDaysInMonth + 1) % WEEKDAY_COUNT; + } + else + { + rtc->day += days; + rtc->dayOfWeek = (rtc->dayOfWeek + days) % WEEKDAY_COUNT; + days = 0; + } + } } -void FakeRtc_ManuallySetTime(u32 hour, u32 minute, u32 second) + +void FakeRtc_ManuallySetTime(u32 day, u32 hour, u32 minute, u32 second) { - struct Time diff, target; - RtcCalcLocalTime(); + FakeRtc_Reset(); + FakeRtc_AdvanceTimeBy(day, hour, minute, second); +} - target.hours = hour; - target.minutes = minute; - target.seconds = second; - target.days = gLocalTime.days; - - CalcTimeDifference(&diff, &gLocalTime, &target); - FakeRtc_AdvanceTimeBy(diff.days, diff.hours, diff.minutes, diff.seconds); +void AdvanceScript(void) +{ + FakeRtc_AdvanceTimeBy(300, 0, 0, 0); } u32 FakeRtc_GetSecondsRatio(void) diff --git a/src/load_save.c b/src/load_save.c index 6fde540b8c..d4ea39f39d 100644 --- a/src/load_save.c +++ b/src/load_save.c @@ -1,6 +1,7 @@ #include "global.h" #include "malloc.h" #include "berry_powder.h" +#include "fake_rtc.h" #include "item.h" #include "load_save.h" #include "main.h" @@ -63,6 +64,7 @@ void CheckForFlashMemory(void) void ClearSav3(void) { CpuFill16(0, &gSaveblock3, sizeof(struct SaveBlock3)); + FakeRtc_Reset(); } void ClearSav2(void) diff --git a/src/reset_rtc_screen.c b/src/reset_rtc_screen.c index 419ead4415..4d3015fa19 100644 --- a/src/reset_rtc_screen.c +++ b/src/reset_rtc_screen.c @@ -771,11 +771,6 @@ static void Task_ResetRtcScreen(u8 taskId) // Time has been chosen, reset rtc and save DestroyTask(tSubTaskId); RtcReset(); - FakeRtc_AdvanceTimeBy( - gLocalTime.days, - gLocalTime.hours, - gLocalTime.minutes, - gLocalTime.seconds); RtcCalcLocalTimeOffset( gLocalTime.days, gLocalTime.hours, diff --git a/src/rtc.c b/src/rtc.c index 009c6b765f..5f47e35c66 100644 --- a/src/rtc.c +++ b/src/rtc.c @@ -227,7 +227,7 @@ void RtcReset(void) { if (OW_USE_FAKE_RTC) { - memset(FakeRtc_GetCurrentTime(), 0, sizeof(struct Time)); + FakeRtc_Reset(); return; }