From ab569cc466879939689b3ed945657a13a444bf66 Mon Sep 17 00:00:00 2001 From: PikalaxALT Date: Fri, 24 Nov 2017 14:57:00 -0500 Subject: [PATCH] clock.s --- asm/clock.s | 175 ---------------------------------------- include/dewford_trend.h | 1 + include/field_screen.h | 1 + include/rom6.h | 6 ++ include/tv.h | 1 + include/wallclock.h | 6 ++ ld_script.txt | 2 +- src/clock.c | 96 ++++++++++++++++++++++ 8 files changed, 112 insertions(+), 176 deletions(-) delete mode 100644 asm/clock.s create mode 100644 include/wallclock.h create mode 100644 src/clock.c diff --git a/asm/clock.s b/asm/clock.s deleted file mode 100644 index 5d771a8b6..000000000 --- a/asm/clock.s +++ /dev/null @@ -1,175 +0,0 @@ - .include "asm/macros.inc" - .include "constants/constants.inc" - - .syntax unified - - .text - - thumb_func_start sub_809E778 -sub_809E778: @ 809E778 - push {lr} - ldr r0, =0x00000895 - bl FlagSet - bl RtcCalcLocalTime - ldr r0, =gSaveBlock2Ptr - ldr r2, [r0] - adds r2, 0xA0 - ldr r3, =gLocalTime - ldr r0, [r3] - ldr r1, [r3, 0x4] - str r0, [r2] - str r1, [r2, 0x4] - ldr r0, =0x00004040 - ldrh r1, [r3] - bl VarSet - pop {r0} - bx r0 - .pool - thumb_func_end sub_809E778 - - thumb_func_start DoTimeBasedEvents -DoTimeBasedEvents: @ 809E7B0 - push {r4,lr} - ldr r0, =0x00000895 - bl FlagGet - lsls r0, 24 - cmp r0, 0 - beq _0809E7DA - bl sub_813B9C0 - lsls r0, 24 - cmp r0, 0 - bne _0809E7DA - bl RtcCalcLocalTime - ldr r4, =gLocalTime - adds r0, r4, 0 - bl sub_809E7E8 - adds r0, r4, 0 - bl sub_809E858 -_0809E7DA: - pop {r4} - pop {r0} - bx r0 - .pool - thumb_func_end DoTimeBasedEvents - - thumb_func_start sub_809E7E8 -sub_809E7E8: @ 809E7E8 - push {r4-r6,lr} - adds r5, r0, 0 - ldr r0, =0x00004040 - bl GetVarPointer - adds r6, r0, 0 - ldrh r0, [r6] - movs r2, 0 - ldrsh r1, [r5, r2] - cmp r0, r1 - beq _0809E84E - cmp r0, r1 - bgt _0809E84E - ldrh r4, [r5] - subs r4, r0 - lsls r4, 16 - lsrs r4, 16 - bl ClearUpperFlags - adds r0, r4, 0 - bl sub_8122580 - adds r0, r4, 0 - bl sub_80ED888 - adds r0, r4, 0 - bl sub_80AEFBC - adds r0, r4, 0 - bl UpdatePartyPokerusTime - adds r0, r4, 0 - bl sub_8137904 - adds r0, r4, 0 - bl sub_8137A20 - adds r0, r4, 0 - bl sub_8139EF4 - adds r0, r4, 0 - bl sub_813A7F4 - adds r0, r4, 0 - bl sub_813945C - adds r0, r4, 0 - bl SetRandomLotteryNumber - ldrh r0, [r5] - strh r0, [r6] -_0809E84E: - pop {r4-r6} - pop {r0} - bx r0 - .pool - thumb_func_end sub_809E7E8 - - thumb_func_start sub_809E858 -sub_809E858: @ 809E858 - push {r4-r6,lr} - sub sp, 0x8 - adds r5, r0, 0 - mov r4, sp - ldr r6, =gSaveBlock2Ptr - ldr r1, [r6] - adds r1, 0xA0 - mov r0, sp - adds r2, r5, 0 - bl CalcTimeDifference - mov r0, sp - movs r2, 0 - ldrsh r1, [r0, r2] - lsls r0, r1, 1 - adds r0, r1 - lsls r1, r0, 4 - subs r1, r0 - lsls r1, 5 - movs r2, 0x2 - ldrsb r2, [r4, r2] - lsls r0, r2, 4 - subs r0, r2 - lsls r0, 2 - adds r1, r0 - movs r0, 0x3 - ldrsb r0, [r4, r0] - adds r0, r1, r0 - cmp r0, 0 - beq _0809E8A8 - cmp r0, 0 - blt _0809E8A8 - bl BerryTreeTimeUpdate - ldr r2, [r6] - adds r2, 0xA0 - ldr r0, [r5] - ldr r1, [r5, 0x4] - str r0, [r2] - str r1, [r2, 0x4] -_0809E8A8: - add sp, 0x8 - pop {r4-r6} - pop {r0} - bx r0 - .pool - thumb_func_end sub_809E858 - - thumb_func_start sub_809E8B4 -sub_809E8B4: @ 809E8B4 - push {lr} - bl sub_809E778 - ldr r0, =c2_exit_to_overworld_1_continue_scripts_restart_music - bl SetMainCallback2 - pop {r0} - bx r0 - .pool - thumb_func_end sub_809E8B4 - - thumb_func_start Special_StartWallClock -Special_StartWallClock: @ 809E8C8 - push {lr} - ldr r0, =Cb2_StartWallClock - bl SetMainCallback2 - ldr r1, =gMain - ldr r0, =sub_809E8B4 - str r0, [r1, 0x8] - pop {r0} - bx r0 - .pool - thumb_func_end Special_StartWallClock - - .align 2, 0 @ Don't pad with nop. diff --git a/include/dewford_trend.h b/include/dewford_trend.h index 01e5d9762..3972ad143 100644 --- a/include/dewford_trend.h +++ b/include/dewford_trend.h @@ -3,5 +3,6 @@ void InitDewfordTrend(void); void UpdateDewfordTrendPerDay(u16); +void sub_8122580(u16 days); #endif // GUARD_DEWFORDTREND_H diff --git a/include/field_screen.h b/include/field_screen.h index 089e79cc4..f01760fcb 100644 --- a/include/field_screen.h +++ b/include/field_screen.h @@ -9,5 +9,6 @@ void pal_fill_black(void); bool8 sub_80ABDFC(void); void sub_80AF168(void); +void sub_80AEFBC(u16 days); #endif //GUARD_FIELD_SCREEN_H diff --git a/include/rom6.h b/include/rom6.h index b4a972794..014422b3d 100644 --- a/include/rom6.h +++ b/include/rom6.h @@ -5,5 +5,11 @@ void sub_81357FC(u8, void(void)); u8 GetLeadMonIndex(void); u8 GetSSTidalLocation(u8 *grp, u8 *num, s16 *x, s16 *y); void sub_813BADC(bool8); +bool8 sub_813B9C0(void); +void sub_8137904(u16 days); +void sub_8137A20(u16 days); +void sub_8139EF4(u16 days); +void sub_813A7F4(u16 days); +void sub_813945C(u16 days); #endif //GUARD_ROM6_H diff --git a/include/tv.h b/include/tv.h index 3f48af569..80828faf1 100644 --- a/include/tv.h +++ b/include/tv.h @@ -14,5 +14,6 @@ void sub_80EEA70(void); void sub_80F14F8(TVShow *shows); size_t sub_80EF370(int value); void SetPokemonAnglerSpecies(u16 species); +void sub_80ED888(u16 days); #endif //GUARD_TV_H diff --git a/include/wallclock.h b/include/wallclock.h new file mode 100644 index 000000000..48697c19f --- /dev/null +++ b/include/wallclock.h @@ -0,0 +1,6 @@ +#ifndef GUARD_WALLCLOCK_H +#define GUARD_WALLCLOCK_H + +void Cb2_StartWallClock(void); + +#endif //GUARD_WALLCLOCK_H diff --git a/ld_script.txt b/ld_script.txt index e9c810adc..66c981f9c 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -107,7 +107,7 @@ SECTIONS { src/event_data.o(.text); asm/coord_event_weather.o(.text); asm/field_tasks.o(.text); - asm/clock.o(.text); + src/clock.o(.text); asm/reset_rtc_screen.o(.text); src/start_menu.o(.text); asm/start_menu.o(.text); diff --git a/src/clock.c b/src/clock.c new file mode 100644 index 000000000..2ba42f70d --- /dev/null +++ b/src/clock.c @@ -0,0 +1,96 @@ +#include "global.h" +#include "rom6.h" +#include "event_data.h" +#include "rtc.h" +#include "lottery_corner.h" +#include "dewford_trend.h" +#include "tv.h" +#include "field_screen.h" +#include "berry.h" +#include "main.h" +#include "overworld.h" +#include "wallclock.h" + +// static types + +// static declarations + +static void sub_809E7E8(struct Time *localTime); +static void sub_809E858(struct Time *localTime); + +// rodata + +// text + +static void sub_809E778(void) +{ + FlagSet(FLAG_SYS_CLOCK_SET); + RtcCalcLocalTime(); + gSaveBlock2Ptr->lastBerryTreeUpdate = gLocalTime; + VarSet(VAR_DAYS, gLocalTime.days); +} + +void DoTimeBasedEvents(void) +{ + if (FlagGet(FLAG_SYS_CLOCK_SET) && !sub_813B9C0()) + { + RtcCalcLocalTime(); + sub_809E7E8(&gLocalTime); + sub_809E858(&gLocalTime); + } +} + +static void sub_809E7E8(struct Time *localTime) +{ + u16 *days = GetVarPointer(VAR_DAYS); + u16 daysSince; + + if (*days != localTime->days) + { + if (*days <= localTime->days) + { + daysSince = localTime->days - *days; + ClearUpperFlags(); + sub_8122580(daysSince); + sub_80ED888(daysSince); + sub_80AEFBC(daysSince); + UpdatePartyPokerusTime(daysSince); + sub_8137904(daysSince); + sub_8137A20(daysSince); + sub_8139EF4(daysSince); + sub_813A7F4(daysSince); + sub_813945C(daysSince); + SetRandomLotteryNumber(daysSince); + *days = localTime->days; + } + } +} + +static void sub_809E858(struct Time *localTime) +{ + struct Time difference; + int minutes; + + CalcTimeDifference(&difference, &gSaveBlock2Ptr->lastBerryTreeUpdate, localTime); + minutes = 24 * 60 * difference.days + 60 * difference.hours + difference.minutes; + if (minutes != 0) + { + if (minutes >= 0) + { + BerryTreeTimeUpdate(minutes); + gSaveBlock2Ptr->lastBerryTreeUpdate = *localTime; + } + } +} + +void sub_809E8B4(void) +{ + sub_809E778(); + SetMainCallback2(c2_exit_to_overworld_1_continue_scripts_restart_music); +} + +void Special_StartWallClock(void) +{ + SetMainCallback2(Cb2_StartWallClock); + gMain.savedCallback = sub_809E8B4; +}