From 49184cd799e083b3b612f216da68ccafeb2cfb4f Mon Sep 17 00:00:00 2001 From: hondew Date: Sat, 2 May 2020 11:46:12 -0400 Subject: [PATCH 1/3] Document berry yield fix wording --- include/berry.h | 2 ++ src/berry.c | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/include/berry.h b/include/berry.h index afd1bc499..b583f3eb2 100644 --- a/include/berry.h +++ b/include/berry.h @@ -1,6 +1,8 @@ #ifndef GUARD_BERRY_H #define GUARD_BERRY_H +#define NUM_BERRY_STAGES 4 + void ClearEnigmaBerries(void); void SetEnigmaBerry(u8 *src); bool32 IsEnigmaBerryValid(void); diff --git a/src/berry.c b/src/berry.c index 7a8ab37fa..3f59639ad 100644 --- a/src/berry.c +++ b/src/berry.c @@ -1200,6 +1200,15 @@ static u8 GetNumStagesWateredByBerryTreeId(u8 id) return BerryTreeGetNumStagesWatered(GetBerryTreeInfo(id)); } +// Berries can be watered at 4 stages of growth. This function is likely meant +// to divide the berry yield range equally into quartiles. If you watered the +// tree n times, your yield is a random number in the nth quartile. +// +// However, this function actually skews towards higher berry yields, because +// it rounds `extraYield` to the nearest whole number. +// +// See resulting yields: https://pastebin.com/RLGnP9Ng, and +// bug fix: https://pastebin.com/cDjnUWL0. static u8 CalcBerryYieldInternal(u16 max, u16 min, u8 water) { u32 randMin; @@ -1215,10 +1224,11 @@ static u8 CalcBerryYieldInternal(u16 max, u16 min, u8 water) randMax = (max - min) * (water); rand = randMin + Random() % (randMax - randMin + 1); - if ((rand & 3) > 1) - extraYield = rand / 4 + 1; + // Round upwards + if ((rand % NUM_BERRY_STAGES) >= NUM_BERRY_STAGES / 2) + extraYield = rand / NUM_BERRY_STAGES + 1; else - extraYield = rand / 4; + extraYield = rand / NUM_BERRY_STAGES; return extraYield + min; } } From 048d1ffbdbe00e56ee4511d023885a11d018864b Mon Sep 17 00:00:00 2001 From: hondew Date: Sat, 2 May 2020 13:38:25 -0400 Subject: [PATCH 2/3] Clarify stages --- include/berry.h | 2 -- include/constants/berry.h | 7 +++++++ src/berry.c | 6 +++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/include/berry.h b/include/berry.h index b583f3eb2..afd1bc499 100644 --- a/include/berry.h +++ b/include/berry.h @@ -1,8 +1,6 @@ #ifndef GUARD_BERRY_H #define GUARD_BERRY_H -#define NUM_BERRY_STAGES 4 - void ClearEnigmaBerries(void); void SetEnigmaBerry(u8 *src); bool32 IsEnigmaBerryValid(void); diff --git a/include/constants/berry.h b/include/constants/berry.h index d413b9947..8f1e7cd82 100644 --- a/include/constants/berry.h +++ b/include/constants/berry.h @@ -25,4 +25,11 @@ #define BERRY_STAGE_BERRIES 5 #define BERRY_STAGE_SPARKLING 255 +// Berries can be watered in the following stages: +// - BERRY_STAGE_PLANTED +// - BERRY_STAGE_SPROUTED +// - BERRY_STAGE_TALLER +// - BERRY_STAGE_FLOWERING +#define NUM_WATER_STAGES 4 + #endif // GUARD_CONSTANTS_BERRY_H diff --git a/src/berry.c b/src/berry.c index 3f59639ad..e548d3c57 100644 --- a/src/berry.c +++ b/src/berry.c @@ -1225,10 +1225,10 @@ static u8 CalcBerryYieldInternal(u16 max, u16 min, u8 water) rand = randMin + Random() % (randMax - randMin + 1); // Round upwards - if ((rand % NUM_BERRY_STAGES) >= NUM_BERRY_STAGES / 2) - extraYield = rand / NUM_BERRY_STAGES + 1; + if ((rand % NUM_WATER_STAGES) >= NUM_WATER_STAGES / 2) + extraYield = rand / NUM_WATER_STAGES + 1; else - extraYield = rand / NUM_BERRY_STAGES; + extraYield = rand / NUM_WATER_STAGES; return extraYield + min; } } From 84f012fc6e653df33158e494bab95e18557a2161 Mon Sep 17 00:00:00 2001 From: hondew Date: Sat, 2 May 2020 13:59:28 -0400 Subject: [PATCH 3/3] Update external links --- src/berry.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/berry.c b/src/berry.c index e548d3c57..fe4a66af6 100644 --- a/src/berry.c +++ b/src/berry.c @@ -1207,8 +1207,8 @@ static u8 GetNumStagesWateredByBerryTreeId(u8 id) // However, this function actually skews towards higher berry yields, because // it rounds `extraYield` to the nearest whole number. // -// See resulting yields: https://pastebin.com/RLGnP9Ng, and -// bug fix: https://pastebin.com/cDjnUWL0. +// See resulting yields: https://gist.github.com/hondew/2a099dbe54aa91414decdbfaa524327d, +// and bug fix: https://gist.github.com/hondew/0f0164e5b9dadfd72d24f30f2c049a0b. static u8 CalcBerryYieldInternal(u16 max, u16 min, u8 water) { u32 randMin;