From 14178edfe34f3a6d0d0d150ae5a5ef59df0e4930 Mon Sep 17 00:00:00 2001 From: tertu Date: Mon, 3 Feb 2025 13:03:13 -0600 Subject: [PATCH] Don't use SeedRng some places where it isn't necessary (#6156) --- include/random.h | 9 +++++++-- src/battle_pyramid.c | 18 ++++++++++++------ src/contest_painting.c | 2 -- src/daycare.c | 8 ++++---- src/record_mixing.c | 7 ++----- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/include/random.h b/include/random.h index 1a735097d3..80e4ea8a24 100644 --- a/include/random.h +++ b/include/random.h @@ -30,7 +30,7 @@ struct Sfc32State { typedef struct Sfc32State rng_value_t; -#define RNG_VALUE_EMPTY {} +#define RNG_VALUE_EMPTY {0} // Calling this function directly is discouraged. // Use LocalRandom() instead. @@ -43,9 +43,14 @@ static inline u32 _SFC32_Next(struct Sfc32State *state) return result; } +static inline u32 LocalRandom32(rng_value_t *val) +{ + return _SFC32_Next(val); +} + static inline u16 LocalRandom(rng_value_t *val) { - return _SFC32_Next(val) >> 16; + return LocalRandom32(val) >> 16; } u32 Random32(void); diff --git a/src/battle_pyramid.c b/src/battle_pyramid.c index c881dab38f..33f2717597 100644 --- a/src/battle_pyramid.c +++ b/src/battle_pyramid.c @@ -983,8 +983,10 @@ static void SetPickupItem(void) { int i; int itemIndex; - int rand; + int randVal; + u32 randSeedIndex, randSeed; u8 id; + rng_value_t rand; u32 lvlMode = gSaveBlock2Ptr->frontier.lvlMode; u32 floor = gSaveBlock2Ptr->frontier.curChallengeBattleNum; u32 round = (gSaveBlock2Ptr->frontier.pyramidWinStreaks[lvlMode] / FRONTIER_STAGES_PER_CHALLENGE) % TOTAL_PYRAMID_ROUNDS; @@ -994,15 +996,19 @@ static void SetPickupItem(void) id = GetPyramidFloorTemplateId(); itemIndex = (gSpecialVar_LastTalked - sPyramidFloorTemplates[id].numTrainers) - 1; - rand = gSaveBlock2Ptr->frontier.pyramidRandoms[itemIndex / 2]; - SeedRng2(rand); + randSeedIndex = (itemIndex & 1) * 2; + randSeed = (u32)gSaveBlock2Ptr->frontier.pyramidRandoms[randSeedIndex + 1] << 16; + randSeed |= gSaveBlock2Ptr->frontier.pyramidRandoms[randSeedIndex]; + rand = LocalRandomSeed(randSeed); - for (i = 0; i < itemIndex + 1; i++) - rand = Random2() % 100; + for (i = 0; i < itemIndex / 2; i++) + LocalRandom(&rand); + + randVal = LocalRandom(&rand) % 100; for (i = sPickupItemOffsets[floor]; i < ARRAY_COUNT(sPickupItemSlots); i++) { - if (rand < sPickupItemSlots[i][0]) + if (randVal < sPickupItemSlots[i][0]) break; } diff --git a/src/contest_painting.c b/src/contest_painting.c index 8b647be12f..1911e70689 100644 --- a/src/contest_painting.c +++ b/src/contest_painting.c @@ -212,7 +212,6 @@ static void ShowContestPainting(void) gMain.state++; break; case 2: - SeedRng(gMain.vblankCounter1); InitKeys(); InitContestPaintingWindow(); gMain.state++; @@ -595,4 +594,3 @@ static void CreateContestPaintingPicture(u8 contestWinnerId, bool8 isForArtist) InitPaintingMonOamData(contestWinnerId); LoadContestPaintingFrame(contestWinnerId, isForArtist); } - diff --git a/src/daycare.c b/src/daycare.c index 6b6b84ac58..4e69dd9012 100644 --- a/src/daycare.c +++ b/src/daycare.c @@ -549,14 +549,15 @@ static void _TriggerPendingDaycareEgg(struct DayCare *daycare) { s32 parent; s32 natureTries = 0; + rng_value_t personalityRand; - SeedRng2(gMain.vblankCounter2); + personalityRand = LocalRandomSeed(gMain.vblankCounter2); parent = GetParentToInheritNature(daycare); // don't inherit nature if (parent < 0) { - daycare->offspringPersonality = (Random2() << 16) | ((Random() % 0xfffe) + 1); + daycare->offspringPersonality = (LocalRandom(&personalityRand) << 16) | ((Random() % 0xfffe) + 1); } // inherit nature else @@ -566,7 +567,7 @@ static void _TriggerPendingDaycareEgg(struct DayCare *daycare) do { - personality = (Random2() << 16) | (Random()); + personality = (LocalRandom(&personalityRand) << 16) | (Random()); if (wantedNature == GetNatureFromPersonality(personality) && personality != 0) break; // found a personality with the same nature @@ -1566,4 +1567,3 @@ static u8 ModifyBreedingScoreForOvalCharm(u8 score) return score; } - diff --git a/src/record_mixing.c b/src/record_mixing.c index 7d78119f86..1ff36d1fe8 100644 --- a/src/record_mixing.c +++ b/src/record_mixing.c @@ -770,14 +770,12 @@ static void ReceiveDaycareMailData(struct RecordMixingDaycareMail *records, size bool8 canHoldItem[MAX_LINK_PLAYERS][DAYCARE_MON_COUNT]; u8 idxs[MAX_LINK_PLAYERS][2]; u8 numDaycareCanHold; - u16 oldSeed; bool32 anyRS; + rng_value_t localRngState = LocalRandomSeed(gLinkPlayers[0].trainerId); // Seed RNG to the first player's trainer id so that // every player has the same random swap occur // (see the other use of Random2 in this function) - oldSeed = Random2(); - SeedRng2(gLinkPlayers[0].trainerId); linkPlayerCount = GetLinkPlayerCount(); for (i = 0; i < MAX_LINK_PLAYERS; i++) { @@ -907,7 +905,7 @@ static void ReceiveDaycareMailData(struct RecordMixingDaycareMail *records, size itemId2 = GetDaycareMailItemId(&mixMail->mail[1]); if ((!itemId1 && !itemId2) || (itemId1 && itemId2)) - idxs[j][DAYCARE_SLOT] = Random2() % 2; + idxs[j][DAYCARE_SLOT] = LocalRandom32(&localRngState) % 2; else if (itemId1 && !itemId2) idxs[j][DAYCARE_SLOT] = 0; else if (!itemId1 && itemId2) @@ -958,7 +956,6 @@ static void ReceiveDaycareMailData(struct RecordMixingDaycareMail *records, size mixMail = (void *)records + multiplayerId * recordSize; memcpy(&gSaveBlock1Ptr->daycare.mons[0].mail, &mixMail->mail[0], sizeof(struct DaycareMail)); memcpy(&gSaveBlock1Ptr->daycare.mons[1].mail, &mixMail->mail[1], sizeof(struct DaycareMail)); - SeedRng(oldSeed); }