From 42076897d3c77e461b9b181be482c610d5981a94 Mon Sep 17 00:00:00 2001 From: Frank DeBlasio <35279583+fdeblasio@users.noreply.github.com> Date: Sat, 18 Nov 2023 10:53:56 -0500 Subject: [PATCH] Made function for time-based evolutions (#3369) * Made function for time-based evolutions * Incorporated Edu's comments and time variables * Moved time of day begin/end variables to rtc.h * Fixed if/else statement to pass building with modern * Added morning * Added ability to check if end time > beginning time * Updated times to match SwSh --------- Co-authored-by: Bassoonian --- include/rtc.h | 19 +++++++++++++++++++ src/pokemon.c | 42 +++++++++++------------------------------- src/rtc.c | 21 +++++++++++++++++++++ 3 files changed, 51 insertions(+), 31 deletions(-) diff --git a/include/rtc.h b/include/rtc.h index 7a3d3d524b..d94beafcdc 100644 --- a/include/rtc.h +++ b/include/rtc.h @@ -17,6 +17,23 @@ #define RTC_ERR_FLAG_MASK 0x0FF0 +#define MORNING_EVO_HOUR_BEGIN 6 +#define MORNING_EVO_HOUR_END 10 + +#define DAY_EVO_HOUR_BEGIN 10 +#define DAY_EVO_HOUR_END 19 + +#define DUSK_EVO_HOUR_BEGIN 19 +#define DUSK_EVO_HOUR_END 20 + +#define NIGHT_EVO_HOUR_BEGIN 20 +#define NIGHT_EVO_HOUR_END 6 + +#define TIME_MORNING 0 +#define TIME_DAY 1 +#define TIME_DUSK 2 +#define TIME_NIGHT 3 + extern struct Time gLocalTime; void RtcDisableInterrupts(void); @@ -40,6 +57,8 @@ void FormatDecimalDate(u8 *dest, s32 year, s32 month, s32 day); void FormatHexDate(u8 *dest, s32 year, s32 month, s32 day); void RtcCalcTimeDifference(struct SiiRtcInfo *rtc, struct Time *result, struct Time *t); void RtcCalcLocalTime(void); +bool8 IsBetweenHours(s32 hours, s32 begin, s32 end); +u8 GetTimeOfDay(void); void RtcInitLocalTimeOffset(s32 hour, s32 minute); void RtcCalcLocalTimeOffset(s32 days, s32 hours, s32 minutes, s32 seconds); void CalcTimeDifference(struct Time *result, struct Time *t1, struct Time *t2); diff --git a/src/pokemon.c b/src/pokemon.c index 636acdf156..15c4a3d2be 100644 --- a/src/pokemon.c +++ b/src/pokemon.c @@ -53,15 +53,6 @@ #include "constants/union_room.h" #include "constants/weather.h" -#define DAY_EVO_HOUR_BEGIN 12 -#define DAY_EVO_HOUR_END HOURS_PER_DAY - -#define DUSK_EVO_HOUR_BEGIN 17 -#define DUSK_EVO_HOUR_END 18 - -#define NIGHT_EVO_HOUR_BEGIN 0 -#define NIGHT_EVO_HOUR_END 12 - #if P_FRIENDSHIP_EVO_THRESHOLD >= GEN_9 #define FRIENDSHIP_EVO_THRESHOLD 160 #else @@ -7053,28 +7044,23 @@ u16 GetEvolutionTargetSpecies(struct Pokemon *mon, u8 mode, u16 evolutionItem, s targetSpecies = gEvolutionTable[species][i].targetSpecies; break; case EVO_FRIENDSHIP_DAY: - RtcCalcLocalTime(); - if (gLocalTime.hours >= DAY_EVO_HOUR_BEGIN && gLocalTime.hours < DAY_EVO_HOUR_END && friendship >= FRIENDSHIP_EVO_THRESHOLD) + if (GetTimeOfDay() != TIME_NIGHT && friendship >= FRIENDSHIP_EVO_THRESHOLD) targetSpecies = gEvolutionTable[species][i].targetSpecies; break; case EVO_LEVEL_DAY: - RtcCalcLocalTime(); - if (gLocalTime.hours >= DAY_EVO_HOUR_BEGIN && gLocalTime.hours < DAY_EVO_HOUR_END && gEvolutionTable[species][i].param <= level) + if (GetTimeOfDay() != TIME_NIGHT && gEvolutionTable[species][i].param <= level) targetSpecies = gEvolutionTable[species][i].targetSpecies; break; case EVO_FRIENDSHIP_NIGHT: - RtcCalcLocalTime(); - if (gLocalTime.hours >= NIGHT_EVO_HOUR_BEGIN && gLocalTime.hours < NIGHT_EVO_HOUR_END && friendship >= FRIENDSHIP_EVO_THRESHOLD) + if (GetTimeOfDay() == TIME_NIGHT && friendship >= FRIENDSHIP_EVO_THRESHOLD) targetSpecies = gEvolutionTable[species][i].targetSpecies; break; case EVO_LEVEL_NIGHT: - RtcCalcLocalTime(); - if (gLocalTime.hours >= NIGHT_EVO_HOUR_BEGIN && gLocalTime.hours < NIGHT_EVO_HOUR_END && gEvolutionTable[species][i].param <= level) + if (GetTimeOfDay() == TIME_NIGHT && gEvolutionTable[species][i].param <= level) targetSpecies = gEvolutionTable[species][i].targetSpecies; break; case EVO_ITEM_HOLD_NIGHT: - RtcCalcLocalTime(); - if (gLocalTime.hours >= NIGHT_EVO_HOUR_BEGIN && gLocalTime.hours < NIGHT_EVO_HOUR_END && heldItem == gEvolutionTable[species][i].param) + if (GetTimeOfDay() == TIME_NIGHT && heldItem == gEvolutionTable[species][i].param) { heldItem = ITEM_NONE; SetMonData(mon, MON_DATA_HELD_ITEM, &heldItem); @@ -7082,8 +7068,7 @@ u16 GetEvolutionTargetSpecies(struct Pokemon *mon, u8 mode, u16 evolutionItem, s } break; case EVO_ITEM_HOLD_DAY: - RtcCalcLocalTime(); - if (gLocalTime.hours >= DAY_EVO_HOUR_BEGIN && gLocalTime.hours < DAY_EVO_HOUR_END && heldItem == gEvolutionTable[species][i].param) + if (GetTimeOfDay() != TIME_NIGHT && heldItem == gEvolutionTable[species][i].param) { heldItem = ITEM_NONE; SetMonData(mon, MON_DATA_HELD_ITEM, &heldItem); @@ -7091,8 +7076,7 @@ u16 GetEvolutionTargetSpecies(struct Pokemon *mon, u8 mode, u16 evolutionItem, s } break; case EVO_LEVEL_DUSK: - RtcCalcLocalTime(); - if (gLocalTime.hours >= DUSK_EVO_HOUR_BEGIN && gLocalTime.hours < DUSK_EVO_HOUR_END && gEvolutionTable[species][i].param <= level) + if (GetTimeOfDay() == TIME_DUSK && gEvolutionTable[species][i].param <= level) targetSpecies = gEvolutionTable[species][i].targetSpecies; break; case EVO_LEVEL: @@ -7317,13 +7301,11 @@ u16 GetEvolutionTargetSpecies(struct Pokemon *mon, u8 mode, u16 evolutionItem, s targetSpecies = gEvolutionTable[species][i].targetSpecies; break; case EVO_ITEM_NIGHT: - RtcCalcLocalTime(); - if (gLocalTime.hours >= NIGHT_EVO_HOUR_BEGIN && gLocalTime.hours < NIGHT_EVO_HOUR_END && gEvolutionTable[species][i].param == evolutionItem) + if (GetTimeOfDay() == TIME_NIGHT && gEvolutionTable[species][i].param == evolutionItem) targetSpecies = gEvolutionTable[species][i].targetSpecies; break; case EVO_ITEM_DAY: - RtcCalcLocalTime(); - if (gLocalTime.hours >= DAY_EVO_HOUR_BEGIN && gLocalTime.hours < DAY_EVO_HOUR_END && gEvolutionTable[species][i].param == evolutionItem) + if (GetTimeOfDay() != TIME_NIGHT && gEvolutionTable[species][i].param == evolutionItem) targetSpecies = gEvolutionTable[species][i].targetSpecies; break; } @@ -9049,13 +9031,11 @@ u16 GetFormChangeTargetSpeciesBoxMon(struct BoxPokemon *boxMon, u16 method, u32 switch (formChanges[i].param2) { case DAY: - RtcCalcLocalTime(); - if (gLocalTime.hours >= DAY_EVO_HOUR_BEGIN && gLocalTime.hours < DAY_EVO_HOUR_END) + if (GetTimeOfDay() != TIME_NIGHT) targetSpecies = formChanges[i].targetSpecies; break; case NIGHT: - RtcCalcLocalTime(); - if (gLocalTime.hours >= NIGHT_EVO_HOUR_BEGIN && gLocalTime.hours < NIGHT_EVO_HOUR_END) + if (GetTimeOfDay() == TIME_NIGHT) targetSpecies = formChanges[i].targetSpecies; break; default: diff --git a/src/rtc.c b/src/rtc.c index b79f62a3c4..085a05b24c 100644 --- a/src/rtc.c +++ b/src/rtc.c @@ -293,6 +293,27 @@ void RtcCalcLocalTime(void) RtcCalcTimeDifference(&sRtc, &gLocalTime, &gSaveBlock2Ptr->localTimeOffset); } +bool8 IsBetweenHours(s32 hours, s32 begin, s32 end) +{ + if (end < begin) + return hours > begin || hours < end; + else + return hours >= begin && hours < end; +} + +u8 GetTimeOfDay(void) +{ + RtcCalcLocalTime(); + if (IsBetweenHours(gLocalTime.hours, MORNING_EVO_HOUR_BEGIN, MORNING_EVO_HOUR_END)) + return TIME_MORNING; + else if (IsBetweenHours(gLocalTime.hours, DUSK_EVO_HOUR_BEGIN, DUSK_EVO_HOUR_END)) + return TIME_DUSK; + else if (IsBetweenHours(gLocalTime.hours, NIGHT_EVO_HOUR_BEGIN, NIGHT_EVO_HOUR_END)) + return TIME_NIGHT; + else + return TIME_DAY; +} + void RtcInitLocalTimeOffset(s32 hour, s32 minute) { RtcCalcLocalTimeOffset(0, hour, minute, 0);