From d2201fb4a65c8458dd1ba1f2c9ffd2badfaa9024 Mon Sep 17 00:00:00 2001 From: Eduardo Quezada Date: Thu, 12 Dec 2024 17:35:19 -0300 Subject: [PATCH 1/7] Initial version of Gen config struct --- include/constants/generational_changes.h | 9 +++++++ include/generational_changes.h | 9 +++++++ include/global.h | 6 +++++ include/test/battle.h | 1 + src/battle_main.c | 3 ++- src/generational_changes.c | 33 ++++++++++++++++++++++++ test/battle/ability/gale_wings.c | 13 ++++++---- 7 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 include/constants/generational_changes.h create mode 100644 include/generational_changes.h create mode 100644 src/generational_changes.c diff --git a/include/constants/generational_changes.h b/include/constants/generational_changes.h new file mode 100644 index 000000000000..dba329f1401c --- /dev/null +++ b/include/constants/generational_changes.h @@ -0,0 +1,9 @@ +#ifndef GUARD_CONSTANTS_GENERATIONAL_CHANGES_H +#define GUARD_CONSTANTS_GENERATIONAL_CHANGES_H + +enum GenConfigTag +{ + GEN_CONFIG_GALE_WINGS, +}; + +#endif // GUARD_CONSTANTS_GENERATIONAL_CHANGES_H diff --git a/include/generational_changes.h b/include/generational_changes.h new file mode 100644 index 000000000000..dbb2be86e85b --- /dev/null +++ b/include/generational_changes.h @@ -0,0 +1,9 @@ +#ifndef GUARD_GENERATIONAL_CHANGES_H +#define GUARD_GENERATIONAL_CHANGES_H + +#include "constants/generational_changes.h" + +u32 GetGenConfig(enum GenConfigTag configTag); +void SetGenConfig(enum GenConfigTag configTag, u32 value); + +#endif // GUARD_GENERATIONAL_CHANGES_H diff --git a/include/global.h b/include/global.h index 5846f09359d0..84af15cf23ce 100644 --- a/include/global.h +++ b/include/global.h @@ -1129,4 +1129,10 @@ struct MapPosition s8 elevation; }; +struct GenerationalChanges +{ + bool32 galeWingsFullHealth:1; + u32 padding:31; +}; + #endif // GUARD_GLOBAL_H diff --git a/include/test/battle.h b/include/test/battle.h index 7cf70539881a..f1ef25447563 100644 --- a/include/test/battle.h +++ b/include/test/battle.h @@ -488,6 +488,7 @@ #include "battle.h" #include "battle_anim.h" #include "data.h" +#include "generational_changes.h" #include "item.h" #include "random.h" #include "recorded_battle.h" diff --git a/src/battle_main.c b/src/battle_main.c index 075dd3dd3c78..62f5a312029e 100644 --- a/src/battle_main.c +++ b/src/battle_main.c @@ -24,6 +24,7 @@ #include "event_data.h" #include "evolution_scene.h" #include "field_weather.h" +#include "generational_changes.h" #include "graphics.h" #include "gpu_regs.h" #include "international_string_util.h" @@ -4849,7 +4850,7 @@ s8 GetMovePriority(u32 battler, u16 move) return gMovesInfo[MOVE_MAX_GUARD].priority; if (ability == ABILITY_GALE_WINGS - && (B_GALE_WINGS < GEN_7 || BATTLER_MAX_HP(battler)) + && (GetGenConfig(GEN_CONFIG_GALE_WINGS) < GEN_7 || BATTLER_MAX_HP(battler)) && gMovesInfo[move].type == TYPE_FLYING) { priority++; diff --git a/src/generational_changes.c b/src/generational_changes.c new file mode 100644 index 000000000000..9f042cbfd70f --- /dev/null +++ b/src/generational_changes.c @@ -0,0 +1,33 @@ +#include "global.h" +#include "generational_changes.h" +#include "malloc.h" +#include "constants/generational_changes.h" + +#if TESTING +EWRAM_INIT struct GenerationalChanges gGenerationalChanges = +#else +const struct GenerationalChanges gGenerationalChanges = +#endif +{ + .galeWingsFullHealth = B_GALE_WINGS >= GEN_7, +}; + +u32 GetGenConfig(enum GenConfigTag configTag) +{ + switch (configTag) + { + case GEN_CONFIG_GALE_WINGS: return gGenerationalChanges.galeWingsFullHealth ? GEN_7 : GEN_6; + default: return GEN_LATEST; + } +} + +void SetGenConfig(enum GenConfigTag configTag, u32 value) +{ +#if TESTING + switch (configTag) + { + case GEN_CONFIG_GALE_WINGS: gGenerationalChanges.galeWingsFullHealth = (value >= GEN_7); + } +#else +#endif +} diff --git a/test/battle/ability/gale_wings.c b/test/battle/ability/gale_wings.c index eff458964927..d8e5d9e39784 100644 --- a/test/battle/ability/gale_wings.c +++ b/test/battle/ability/gale_wings.c @@ -1,12 +1,15 @@ #include "global.h" #include "test/battle.h" -SINGLE_BATTLE_TEST("Gale Wings only grants priority at full HP") +SINGLE_BATTLE_TEST("Gale Wings only grants priority at full HP (Gen 7+)") { - u16 hp; - PARAMETRIZE { hp = 100; } - PARAMETRIZE { hp = 99; } + u32 hp, config; + PARAMETRIZE { hp = 100; config = GEN_7; } + PARAMETRIZE { hp = 99; config = GEN_7; } + PARAMETRIZE { hp = 100; config = GEN_6; } + PARAMETRIZE { hp = 99; config = GEN_6; } GIVEN { + SetGenConfig(GEN_CONFIG_GALE_WINGS, config); ASSUME(B_GALE_WINGS >= GEN_7); ASSUME(gMovesInfo[MOVE_AERIAL_ACE].type == TYPE_FLYING); PLAYER(SPECIES_TALONFLAME) { Ability(ABILITY_GALE_WINGS); HP(hp); MaxHP(100); Speed(1);} @@ -14,7 +17,7 @@ SINGLE_BATTLE_TEST("Gale Wings only grants priority at full HP") } WHEN { TURN { MOVE(player, MOVE_AERIAL_ACE); } } SCENE { - if (hp == 100) { + if (hp == 100 || config <= GEN_6) { MESSAGE("Talonflame used Aerial Ace!"); MESSAGE("The opposing Wobbuffet used Celebrate!"); } From 102ef33fcf021c74e4060a2a0563214b4f47ae95 Mon Sep 17 00:00:00 2001 From: sbird Date: Fri, 13 Dec 2024 15:18:09 +0100 Subject: [PATCH 2/7] refactor to use inline static array --- include/constants/generational_changes.h | 1 + include/generational_changes.h | 36 ++++++++++++++++++++++-- src/generational_changes.c | 28 +++++------------- test/test_runner_battle.c | 2 ++ 4 files changed, 44 insertions(+), 23 deletions(-) diff --git a/include/constants/generational_changes.h b/include/constants/generational_changes.h index dba329f1401c..557b34b6537e 100644 --- a/include/constants/generational_changes.h +++ b/include/constants/generational_changes.h @@ -4,6 +4,7 @@ enum GenConfigTag { GEN_CONFIG_GALE_WINGS, + GEN_CONFIG_COUNT }; #endif // GUARD_CONSTANTS_GENERATIONAL_CHANGES_H diff --git a/include/generational_changes.h b/include/generational_changes.h index dbb2be86e85b..5a726007c36e 100644 --- a/include/generational_changes.h +++ b/include/generational_changes.h @@ -2,8 +2,40 @@ #define GUARD_GENERATIONAL_CHANGES_H #include "constants/generational_changes.h" +#include "config/battle.h" -u32 GetGenConfig(enum GenConfigTag configTag); -void SetGenConfig(enum GenConfigTag configTag, u32 value); +static const u8 sGenerationalChanges[GEN_CONFIG_COUNT] = +{ + [GEN_CONFIG_GALE_WINGS] = B_GALE_WINGS, +}; + +#if TESTING +extern u8 *gGenerationalChangesTestOverride; +#endif + +static inline u32 GetGenConfig(enum GenConfigTag configTag) +{ + if (configTag >= GEN_CONFIG_COUNT) return GEN_LATEST; +#if TESTING + if (gGenerationalChangesTestOverride == NULL) return sGenerationalChanges[configTag]; + return gGenerationalChangesTestOverride[configTag]; +#else + return sGenerationalChanges[configTag]; +#endif +} + +static inline void SetGenConfig(enum GenConfigTag configTag, u32 value) +{ +#if TESTING + if (configTag >= GEN_CONFIG_COUNT) return; + if (gGenerationalChangesTestOverride == NULL) return; + gGenerationalChangesTestOverride[configTag] = value; +#endif +} + +#if TESTING +void TestInitConfigData(void); +void TestFreeConfigData(void); +#endif #endif // GUARD_GENERATIONAL_CHANGES_H diff --git a/src/generational_changes.c b/src/generational_changes.c index 9f042cbfd70f..8548ef72de3b 100644 --- a/src/generational_changes.c +++ b/src/generational_changes.c @@ -4,30 +4,16 @@ #include "constants/generational_changes.h" #if TESTING -EWRAM_INIT struct GenerationalChanges gGenerationalChanges = -#else -const struct GenerationalChanges gGenerationalChanges = -#endif -{ - .galeWingsFullHealth = B_GALE_WINGS >= GEN_7, -}; +EWRAM_DATA u8 *gGenerationalChangesTestOverride = NULL; -u32 GetGenConfig(enum GenConfigTag configTag) +void TestInitConfigData(void) { - switch (configTag) - { - case GEN_CONFIG_GALE_WINGS: return gGenerationalChanges.galeWingsFullHealth ? GEN_7 : GEN_6; - default: return GEN_LATEST; - } + gGenerationalChangesTestOverride = Alloc(sizeof(sGenerationalChanges)); + memcpy(gGenerationalChangesTestOverride, sGenerationalChanges, sizeof(sGenerationalChanges)); } -void SetGenConfig(enum GenConfigTag configTag, u32 value) +void TestFreeConfigData(void) { -#if TESTING - switch (configTag) - { - case GEN_CONFIG_GALE_WINGS: gGenerationalChanges.galeWingsFullHealth = (value >= GEN_7); - } -#else -#endif + TRY_FREE_AND_SET_NULL(gGenerationalChangesTestOverride) } +#endif \ No newline at end of file diff --git a/test/test_runner_battle.c b/test/test_runner_battle.c index c55ef489b3e3..e122d56a2b51 100644 --- a/test/test_runner_battle.c +++ b/test/test_runner_battle.c @@ -160,6 +160,7 @@ static void BattleTest_SetUp(void *data) { const struct BattleTest *test = data; memset(STATE, 0, sizeof(*STATE)); + TestInitConfigData(); InvokeTestFunction(test); STATE->parameters = STATE->parametersCount; if (STATE->parametersCount == 0 && test->resultsSize > 0) @@ -1417,6 +1418,7 @@ static void BattleTest_TearDown(void *data) // Free resources that aren't cleaned up when the battle was // aborted unexpectedly. ClearFlagAfterTest(); + TestFreeConfigData(); if (STATE->tearDownBattle) TearDownBattle(); } From 6828da292642af2b68a4782fc5d7d2334d9c31f8 Mon Sep 17 00:00:00 2001 From: sbird Date: Sun, 15 Dec 2024 15:02:56 +0100 Subject: [PATCH 3/7] implement WITH_CONFIG for tests --- include/test/battle.h | 12 ++++++++++++ test/battle/ability/gale_wings.c | 3 +-- test/test_runner_battle.c | 6 ++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/include/test/battle.h b/include/test/battle.h index f1ef25447563..0b44ea02fb92 100644 --- a/include/test/battle.h +++ b/include/test/battle.h @@ -287,6 +287,16 @@ * GIVEN { * FLAG_SET(FLAG_SYS_EXAMPLE_FLAG); * + * WITH_CONFIG(configTag, value) + * Runs the test with a specified config override. `configTag` must be + * of `enum GenConfigTag` + * Example: + * GIVEN { + * WITH_CONFIG(GEN_CONFIG_GALE_WINGS, GEN_6); + * } + * The `value` may be inferred from a local variable, e.g. set by + * PARAMETRIZE. + * * PLAYER(species) and OPPONENT(species) * Adds the species to the player's or opponent's party respectively. * The Pokémon can be further customized with the following functions: @@ -823,6 +833,7 @@ struct moveWithPP { #define AI_LOG AILogScores(__LINE__) #define FLAG_SET(flagId) SetFlagForTest(__LINE__, flagId) +#define WITH_CONFIG(configTag, value) TestSetConfig(__LINE__, configTag, value) #define PLAYER(species) for (OpenPokemon(__LINE__, B_SIDE_PLAYER, species); gBattleTestRunnerState->data.currentMon; ClosePokemon(__LINE__)) #define OPPONENT(species) for (OpenPokemon(__LINE__, B_SIDE_OPPONENT, species); gBattleTestRunnerState->data.currentMon; ClosePokemon(__LINE__)) @@ -856,6 +867,7 @@ struct moveWithPP { #define Shadow(isShadow) Shadow_(__LINE__, shadow) void SetFlagForTest(u32 sourceLine, u16 flagId); +void TestSetConfig(u32 sourceLine, enum GenConfigTag configTag, u32 value); void ClearFlagAfterTest(void); void OpenPokemon(u32 sourceLine, u32 side, u32 species); void ClosePokemon(u32 sourceLine); diff --git a/test/battle/ability/gale_wings.c b/test/battle/ability/gale_wings.c index d8e5d9e39784..8d0695e62258 100644 --- a/test/battle/ability/gale_wings.c +++ b/test/battle/ability/gale_wings.c @@ -9,8 +9,7 @@ SINGLE_BATTLE_TEST("Gale Wings only grants priority at full HP (Gen 7+)") PARAMETRIZE { hp = 100; config = GEN_6; } PARAMETRIZE { hp = 99; config = GEN_6; } GIVEN { - SetGenConfig(GEN_CONFIG_GALE_WINGS, config); - ASSUME(B_GALE_WINGS >= GEN_7); + WITH_CONFIG(GEN_CONFIG_GALE_WINGS, config); ASSUME(gMovesInfo[MOVE_AERIAL_ACE].type == TYPE_FLYING); PLAYER(SPECIES_TALONFLAME) { Ability(ABILITY_GALE_WINGS); HP(hp); MaxHP(100); Speed(1);} OPPONENT(SPECIES_WOBBUFFET) { Speed(100);}; diff --git a/test/test_runner_battle.c b/test/test_runner_battle.c index e122d56a2b51..9484e6df6cf4 100644 --- a/test/test_runner_battle.c +++ b/test/test_runner_battle.c @@ -1515,6 +1515,12 @@ void SetFlagForTest(u32 sourceLine, u16 flagId) FlagSet(flagId); } +void TestSetConfig(u32 sourceLine, enum GenConfigTag configTag, u32 value) +{ + INVALID_IF(!STATE->runGiven, "WITH_CONFIG outside of GIVEN"); + SetGenConfig(configTag, value); +} + void ClearFlagAfterTest(void) { if (DATA.flagId != 0) From b58838ad7c0d568d303c6ed71a9e252594bb011a Mon Sep 17 00:00:00 2001 From: sbird Date: Sun, 15 Dec 2024 15:03:15 +0100 Subject: [PATCH 4/7] enable -ftoplevel-reorder for tests --- Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile b/Makefile index 71d2a48af9a3..656b0847dd3d 100644 --- a/Makefile +++ b/Makefile @@ -350,6 +350,8 @@ $(C_BUILDDIR)/pokedex_plus_hgss.o: CFLAGS := -mthumb -mthumb-interwork -O2 -mabi # Annoyingly we can't turn this on just for src/data/trainers.h $(C_BUILDDIR)/data.o: CFLAGS += -fno-show-column -fno-diagnostics-show-caret +$(TEST_BUILDDIR)/%.o: CFLAGS := -mthumb -mthumb-interwork -O2 -mabi=apcs-gnu -mtune=arm7tdmi -march=armv4t -Wno-pointer-to-int-cast -Werror -Wall -Wno-strict-aliasing -Wno-attribute-alias -Woverride-init + # Dependency rules (for the *.c & *.s sources to .o files) # Have to be explicit or else missing files won't be reported. From 00b0596fc6e974362b02cdf8fa663d6ef92f7418 Mon Sep 17 00:00:00 2001 From: sbird Date: Sun, 15 Dec 2024 15:07:11 +0100 Subject: [PATCH 5/7] fixup! newline at EOF --- src/generational_changes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/generational_changes.c b/src/generational_changes.c index 8548ef72de3b..1ad29aa675e7 100644 --- a/src/generational_changes.c +++ b/src/generational_changes.c @@ -16,4 +16,4 @@ void TestFreeConfigData(void) { TRY_FREE_AND_SET_NULL(gGenerationalChangesTestOverride) } -#endif \ No newline at end of file +#endif From 23e6530797ff8d653a95ed4c93babaed4e93a64e Mon Sep 17 00:00:00 2001 From: Eduardo Quezada Date: Sun, 15 Dec 2024 12:18:54 -0300 Subject: [PATCH 6/7] Forgot to remove assumes --- test/battle/ability/gale_wings.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/battle/ability/gale_wings.c b/test/battle/ability/gale_wings.c index 8d0695e62258..c7a03d09148b 100644 --- a/test/battle/ability/gale_wings.c +++ b/test/battle/ability/gale_wings.c @@ -33,7 +33,6 @@ SINGLE_BATTLE_TEST("Gale Wings only grants priority to Flying-type moves") PARAMETRIZE { move = MOVE_AERIAL_ACE; } PARAMETRIZE { move = MOVE_FLARE_BLITZ; } GIVEN { - ASSUME(B_GALE_WINGS >= GEN_7); ASSUME(gMovesInfo[MOVE_AERIAL_ACE].type == TYPE_FLYING); ASSUME(gMovesInfo[MOVE_FLARE_BLITZ].type == TYPE_FIRE); PLAYER(SPECIES_TALONFLAME) { Ability(ABILITY_GALE_WINGS); HP(100); MaxHP(100); Speed(1);} @@ -60,7 +59,6 @@ SINGLE_BATTLE_TEST("Gale Wings doesn't increase priority of Flying-type Natural PARAMETRIZE { move = MOVE_JUDGMENT; heldItem = ITEM_SKY_PLATE; } PARAMETRIZE { move = MOVE_HIDDEN_POWER; heldItem = ITEM_NONE; } GIVEN { - ASSUME(B_GALE_WINGS >= GEN_7); ASSUME(gMovesInfo[MOVE_NATURAL_GIFT].effect == EFFECT_NATURAL_GIFT); ASSUME(gMovesInfo[MOVE_JUDGMENT].effect == EFFECT_CHANGE_TYPE_ON_ITEM); // IV combinations sourced from https://www.smogon.com/forums/threads/hidden-power-iv-combinations.78083/ From ccff83489fc5c4a0b0daa3128b38e5323aba547c Mon Sep 17 00:00:00 2001 From: Eduardo Quezada Date: Sun, 15 Dec 2024 23:15:08 -0300 Subject: [PATCH 7/7] Update global.h --- include/global.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/include/global.h b/include/global.h index 84af15cf23ce..5846f09359d0 100644 --- a/include/global.h +++ b/include/global.h @@ -1129,10 +1129,4 @@ struct MapPosition s8 elevation; }; -struct GenerationalChanges -{ - bool32 galeWingsFullHealth:1; - u32 padding:31; -}; - #endif // GUARD_GLOBAL_H