From 1d11a2cb0a2bebb9368d6e168a982b9dae333643 Mon Sep 17 00:00:00 2001 From: FosterProgramming Date: Mon, 22 Sep 2025 20:44:12 +0200 Subject: [PATCH] Improve how test involving ball throw work (#7774) --- include/random.h | 2 ++ include/test/battle.h | 2 ++ src/battle_script_commands.c | 16 ++++++---------- test/battle/ability/ball_fetch.c | 11 ++++++----- test/test_runner_battle.c | 3 +++ 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/include/random.h b/include/random.h index de0f406fe5..df3e1579ad 100644 --- a/include/random.h +++ b/include/random.h @@ -215,6 +215,8 @@ enum RandomTag RNG_AI_REFRESH_TRICK_ROOM_ON_LAST_TURN, RNG_AI_APPLY_TAILWIND_ON_LAST_TURN_OF_TRICK_ROOM, RNG_WRAP, + RNG_BALLTHROW_CRITICAL, + RNG_BALLTHROW_SHAKE, }; #define RandomWeighted(tag, ...) \ diff --git a/include/test/battle.h b/include/test/battle.h index d909aa0332..9cd1d23b1f 100644 --- a/include/test/battle.h +++ b/include/test/battle.h @@ -1005,6 +1005,8 @@ struct ItemContext u16 explicitPartyIndex:1; u16 move; u16 explicitMove:1; + struct TurnRNG rng; + u16 explicitRNG:1; }; void OpenTurn(u32 sourceLine); diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 03c9d028fd..5ad5a1c642 100755 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -13943,19 +13943,16 @@ static void Cmd_handleballthrow(void) { odds = Sqrt(Sqrt(16711680 / odds)); odds = 1048560 / odds; - for (shakes = 0; shakes < maxShakes && Random() < odds; shakes++); + for (shakes = 0; shakes < maxShakes; shakes++) + { + if (RandomUniform(RNG_BALLTHROW_SHAKE, 0, MAX_u16) < odds) + break; + } } BtlController_EmitBallThrowAnim(gBattlerAttacker, B_COMM_TO_CONTROLLER, shakes); MarkBattlerForControllerExec(gBattlerAttacker); - #if TESTING - if (gTestRunnerEnabled) - { - shakes = 0; // Force failure for tests. TODO: make capture RNG flag - } - #endif - if (shakes == maxShakes) // mon caught, copy of the code above { if (IsCriticalCapture()) @@ -14752,8 +14749,7 @@ static bool32 CriticalCapture(u32 odds) odds = (odds * (100 + B_CATCHING_CHARM_BOOST)) / 100; odds /= 6; - - if ((Random() % 255) < odds) + if (RandomUniform(RNG_BALLTHROW_CRITICAL, 0, MAX_u8) < odds) return TRUE; return FALSE; diff --git a/test/battle/ability/ball_fetch.c b/test/battle/ability/ball_fetch.c index 040aa7cb08..a7513a701c 100644 --- a/test/battle/ability/ball_fetch.c +++ b/test/battle/ability/ball_fetch.c @@ -15,7 +15,8 @@ WILD_BATTLE_TEST("Ball Fetch causes the Pokémon to pick up the last failed Ball PLAYER(SPECIES_YAMPER) { Ability(ABILITY_BALL_FETCH); } OPPONENT(SPECIES_METAGROSS); } WHEN { - TURN { USE_ITEM(player, item); } + TURN { USE_ITEM(player, item, WITH_RNG(RNG_BALLTHROW_SHAKE, 0) );} + TURN {} } SCENE { if (item != ITEM_X_ACCURACY) ABILITY_POPUP(player, ABILITY_BALL_FETCH); @@ -40,7 +41,7 @@ WILD_BATTLE_TEST("Ball Fetch doesn't trigger if the Pokémon is already holding PLAYER(SPECIES_YAMPER) { Ability(ABILITY_BALL_FETCH); Item(item); } OPPONENT(SPECIES_METAGROSS); } WHEN { - TURN { USE_ITEM(player, ITEM_GREAT_BALL); } + TURN { USE_ITEM(player, ITEM_GREAT_BALL, WITH_RNG(RNG_BALLTHROW_SHAKE, 0)); } } SCENE { if (item == ITEM_NONE) { @@ -79,9 +80,9 @@ WILD_BATTLE_TEST("Ball Fetch only picks up the first failed ball, once per battl PLAYER(SPECIES_YAMPER) { Ability(ABILITY_BALL_FETCH); } OPPONENT(SPECIES_METAGROSS); } WHEN { - TURN { USE_ITEM(player, item); } + TURN { USE_ITEM(player, item, WITH_RNG(RNG_BALLTHROW_SHAKE, 0)); } TURN { MOVE(player, MOVE_BESTOW); } - TURN { USE_ITEM(player, item2); } + TURN { USE_ITEM(player, item2, WITH_RNG(RNG_BALLTHROW_SHAKE, 0)); } } SCENE { MESSAGE("You used Great Ball!"); ABILITY_POPUP(player, ABILITY_BALL_FETCH); @@ -109,7 +110,7 @@ SINGLE_BATTLE_TEST("Ball Fetch doesn't trigger in Trainer Battles") PLAYER(SPECIES_YAMPER) { Ability(ABILITY_BALL_FETCH); } OPPONENT(SPECIES_METAGROSS); } WHEN { - TURN { USE_ITEM(player, item); } + TURN { USE_ITEM(player, item, WITH_RNG(RNG_BALLTHROW_SHAKE, 0)); } } SCENE { NOT ABILITY_POPUP(player, ABILITY_BALL_FETCH); } THEN { diff --git a/test/test_runner_battle.c b/test/test_runner_battle.c index c71e74bb48..2f9dd037fb 100644 --- a/test/test_runner_battle.c +++ b/test/test_runner_battle.c @@ -2520,6 +2520,9 @@ void UseItem(u32 sourceLine, struct BattlePokemon *battler, struct ItemContext c { i = 0; } + + if (ctx.explicitRNG) + DATA.battleRecordTurns[DATA.turns][battlerId].rng = ctx.rng; PushBattlerAction(sourceLine, battlerId, RECORDED_ACTION_TYPE, B_ACTION_USE_ITEM); PushBattlerAction(sourceLine, battlerId, RECORDED_ITEM_ID, (ctx.itemId >> 8) & 0xFF); PushBattlerAction(sourceLine, battlerId, RECORDED_ITEM_ID, ctx.itemId & 0xFF);