Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FakeRtc datetime and ResetRtcScreen day increments #5695

Open
wants to merge 5 commits into
base: upcoming
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions include/fake_rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(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);

Expand Down
3 changes: 2 additions & 1 deletion include/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <limits.h>
#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"
Expand Down Expand Up @@ -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];
Expand Down
1 change: 1 addition & 0 deletions include/rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
#define TIME_NIGHT 3

extern struct Time gLocalTime;
extern const s32 sNumDaysInMonths[12];

void RtcDisableInterrupts(void);
void RtcRestoreInterrupts(void);
Expand Down
12 changes: 12 additions & 0 deletions include/siirtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
83 changes: 57 additions & 26 deletions src/fake_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,11 +27,9 @@ struct Time *FakeRtc_GetCurrentTime(void)

void FakeRtc_GetRawInfo(struct SiiRtcInfo *rtc)
{
struct Time* time = FakeRtc_GetCurrentTime();
rtc->second = time->seconds;
rtc->minute = time->minutes;
rtc->hour = time->hours;
rtc->day = time->days;
struct SiiRtcInfo *fakeRtc = FakeRtc_GetCurrentTime();
if (fakeRtc != NULL)
memcpy(rtc, fakeRtc, sizeof(struct SiiRtcInfo));
}

void FakeRtc_TickTimeForward(void)
Expand All @@ -32,15 +40,16 @@ void FakeRtc_TickTimeForward(void)
if (FlagGet(OW_FLAG_PAUSE_TIME))
return;

FakeRtc_AdvanceTimeBy(0, 0, FakeRtc_GetSecondsRatio());
FakeRtc_AdvanceTimeBy(0, 0, 0, FakeRtc_GetSecondsRatio());
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static void FakeRtc_AdvanceSeconds(struct SiiRtcInfo *rtc, u32 *days, u32*hours, u32 *minutes, u32 *seconds)
{
*seconds += rtc->second;
*minutes += rtc->minute;
*hours += rtc->hour;
while (*seconds >= SECONDS_PER_MINUTE)
{
*minutes++;
*seconds -= SECONDS_PER_MINUTE;
}
while (*minutes >= MINUTES_PER_HOUR)
{
*hours++;
*minutes -= MINUTES_PER_HOUR;
}
while (*hours >= HOURS_PER_DAY)
{
*days++;
*hours -= HOURS_PER_DAY;
}
rtc->second = *seconds;
rtc->minute = *minutes;
rtc->hour = *hours;
}
static void FakeRtc_SetDayOfWeek(struct SiiRtcInfo *rtc, u32 daysToAdd)
{
rtc->dayOfWeek = (rtc->dayOfWeek + daysToAdd) % WEEKDAY_COUNT;
}
static void FakeRtc_AdvanceDays(struct SiiRtcInfo *rtc, u32 *days)
{
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);
FakeRtc_SetDayOfWeek(rtc, remainingDaysInMonth + 1);
}
else
{
rtc->day += *days;
FakeRtc_SetDayOfWeek(rtc, *days);
*days = 0;
}
}

void FakeRtc_AdvanceTimeBy(u32 hours, u32 minutes, u32 seconds)
void FakeRtc_AdvanceTimeBy(u32 days, u32 hours, u32 minutes, u32 seconds)
{
struct Time* time = FakeRtc_GetCurrentTime();
seconds += time->seconds;
minutes += time->minutes;
hours += time->hours;
struct SiiRtcInfo *rtc = FakeRtc_GetCurrentTime();

seconds += rtc->second;
minutes += rtc->minute;
hours += rtc->hour;

while(seconds >= SECONDS_PER_MINUTE)
{
Expand All @@ -56,27 +65,49 @@ void FakeRtc_AdvanceTimeBy(u32 hours, u32 minutes, u32 seconds)

while(hours >= HOURS_PER_DAY)
{
time->days++;
days++;
hours -= HOURS_PER_DAY;
}

time->seconds = seconds;
time->minutes = minutes;
time->hours = hours;
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;
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Github is being stupid but I'm trying to leave a suggestion to replace this function with

void FakeRtc_AdvanceTimeBy(u32 days, u32 hours, u32 minutes, u32 seconds)
{
    struct SiiRtcInfo *rtc = FakeRtc_GetCurrentTime();
    FakeRtc_AdvanceSeconds(rtc, &days, &hours, &minutes, &seconds);

    while (days > 0)
        FakeRtc_AdvanceDays(rtc, &days);
}


void FakeRtc_ManuallySetTime(u32 hour, u32 minute, u32 second)
{
struct Time diff, target;
RtcCalcLocalTime();

target.hours = hour;
target.minutes = minute;
target.seconds = second;
target.days = gLocalTime.days;
void FakeRtc_ManuallySetTime(u32 day, u32 hour, u32 minute, u32 second)
{
FakeRtc_Reset();
FakeRtc_AdvanceTimeBy(day, hour, minute, second);
}

CalcTimeDifference(&diff, &gLocalTime, &target);
FakeRtc_AdvanceTimeBy(diff.hours, diff.minutes, diff.seconds);
void AdvanceScript(void)
{
FakeRtc_AdvanceTimeBy(300, 0, 0, 0);
}

u32 FakeRtc_GetSecondsRatio(void)
Expand Down
2 changes: 2 additions & 0 deletions src/load_save.c
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -63,6 +64,7 @@ void CheckForFlashMemory(void)
void ClearSav3(void)
{
CpuFill16(0, &gSaveblock3, sizeof(struct SaveBlock3));
FakeRtc_Reset();
}

void ClearSav2(void)
Expand Down
109 changes: 94 additions & 15 deletions src/reset_rtc_screen.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "global.h"
#include "reset_rtc_screen.h"
#include "event_data.h"
#include "fake_rtc.h"
#include "main.h"
#include "menu.h"
#include "palette.h"
Expand Down Expand Up @@ -41,7 +42,10 @@ enum {
#define tWindowId data[8]

enum {
SELECTION_DAYS = 1,
SELECTION_DAYS_1000 = 1,
SELECTION_DAYS_100,
SELECTION_DAYS_10,
SELECTION_DAYS_1,
SELECTION_HOURS,
SELECTION_MINS,
SELECTION_SECS,
Expand All @@ -59,6 +63,7 @@ struct ResetRtcInputMap
/*0x0*/ u8 dataIndex;
/*0x2*/ u16 minVal;
/*0x4*/ u16 maxVal;
u16 increment;
/*0x6*/ u8 left;
/*0x7*/ u8 right;
/*0x8*/ u8 unk; // never read
Expand Down Expand Up @@ -117,43 +122,75 @@ static const struct WindowTemplate sInputTimeWindow = {

static const struct ResetRtcInputMap sInputMap[] =
{
[SELECTION_DAYS - 1] = {
[SELECTION_DAYS_1000 - 1] = {
.dataIndex = DATAIDX_DAYS,
.minVal = 1,
.maxVal = 9999,
.increment = 1000,
.left = 0,
.right = 2,
.unk = 0,
},
[SELECTION_DAYS_100 - 1] = {
.dataIndex = DATAIDX_DAYS,
.minVal = 1,
.maxVal = 9999,
.increment = 100,
.left = 1,
.right = 3,
.unk = 0,
},
[SELECTION_DAYS_10 - 1] = {
.dataIndex = DATAIDX_DAYS,
.minVal = 1,
.maxVal = 9999,
.increment = 10,
.left = 2,
.right = 4,
.unk = 0,
},
[SELECTION_DAYS_1 - 1] = {
.dataIndex = DATAIDX_DAYS,
.minVal = 1,
.maxVal = 9999,
.increment = 1,
.left = 3,
.right = 5,
.unk = 0,
},
[SELECTION_HOURS - 1] = {
.dataIndex = DATAIDX_HOURS,
.minVal = 0,
.maxVal = 23,
.left = 1,
.right = 3,
.increment = 1,
.left = 4,
.right = 6,
.unk = 0,
},
[SELECTION_MINS - 1] = {
.dataIndex = DATAIDX_MINS,
.minVal = 0,
.maxVal = 59,
.left = 2,
.right = 4,
.increment = 1,
.left = 5,
.right = 7,
.unk = 0,
},
[SELECTION_SECS - 1] = {
.dataIndex = DATAIDX_SECS,
.minVal = 0,
.maxVal = 59,
.left = 3,
.right = 5,
.increment = 1,
.left = 6,
.right = 8,
.unk = 0,
},
[SELECTION_CONFIRM - 1] = {
.dataIndex = DATAIDX_CONFIRM,
.minVal = 0,
.maxVal = 0,
.left = 4,
.increment = 1,
.left = 7,
.right = 0,
.unk = 6,
},
Expand Down Expand Up @@ -244,7 +281,28 @@ static void SpriteCB_Cursor_UpOrRight(struct Sprite *sprite)
sprite->sState = state;
switch (state)
{
case SELECTION_DAYS:
case SELECTION_DAYS_1000:
sprite->invisible = FALSE;
sprite->animNum = ARROW_UP;
sprite->animDelayCounter = 0;
sprite->x = 35;
sprite->y = 68;
break;
case SELECTION_DAYS_100:
sprite->invisible = FALSE;
sprite->animNum = ARROW_UP;
sprite->animDelayCounter = 0;
sprite->x = 41;
sprite->y = 68;
break;
case SELECTION_DAYS_10:
sprite->invisible = FALSE;
sprite->animNum = ARROW_UP;
sprite->animDelayCounter = 0;
sprite->x = 47;
sprite->y = 68;
break;
case SELECTION_DAYS_1:
sprite->invisible = FALSE;
sprite->animNum = ARROW_UP;
sprite->animDelayCounter = 0;
Expand Down Expand Up @@ -294,7 +352,28 @@ static void SpriteCB_Cursor_Down(struct Sprite *sprite)
sprite->sState = state;
switch (state)
{
case SELECTION_DAYS:
case SELECTION_DAYS_1000:
sprite->invisible = FALSE;
sprite->animNum = ARROW_DOWN;
sprite->animDelayCounter = 0;
sprite->x = 35;
sprite->y = 92;
break;
case SELECTION_DAYS_100:
sprite->invisible = FALSE;
sprite->animNum = ARROW_DOWN;
sprite->animDelayCounter = 0;
sprite->x = 41;
sprite->y = 92;
break;
case SELECTION_DAYS_10:
sprite->invisible = FALSE;
sprite->animNum = ARROW_DOWN;
sprite->animDelayCounter = 0;
sprite->x = 47;
sprite->y = 92;
break;
case SELECTION_DAYS_1:
sprite->invisible = FALSE;
sprite->animNum = ARROW_DOWN;
sprite->animDelayCounter = 0;
Expand Down Expand Up @@ -397,17 +476,17 @@ static void ShowChooseTimeWindow(u8 windowId, u16 days, u8 hours, u8 minutes, u8
ScheduleBgCopyTilemapToVram(0);
}

static bool32 MoveTimeUpDown(s16 *val, int minVal, int maxVal, u16 keys)
static bool32 MoveTimeUpDown(s16 *val, int minVal, int maxVal, int increment, u16 keys)
{
if (keys & DPAD_DOWN)
{
*val -= 1;
*val -= increment;
if (*val < minVal)
*val = maxVal;
}
else if (keys & DPAD_UP)
{
*val += 1;
*val += increment;
if (*val > maxVal)
*val = minVal;
}
Expand Down Expand Up @@ -494,7 +573,7 @@ static void Task_ResetRtc_HandleInput(u8 taskId)
tSelection = SELECTION_NONE;
}
}
else if (MoveTimeUpDown(&data[selectionInfo->dataIndex], selectionInfo->minVal, selectionInfo->maxVal, JOY_REPEAT(DPAD_UP | DPAD_DOWN)))
else if (MoveTimeUpDown(&data[selectionInfo->dataIndex], selectionInfo->minVal, selectionInfo->maxVal, selectionInfo->increment, JOY_REPEAT(DPAD_UP | DPAD_DOWN)))
{
PlaySE(SE_SELECT);
PrintTime(tWindowId, 0, 1, tDays, tHours, tMinutes, tSeconds);
Expand Down
Loading
Loading