Skip to content

Commit

Permalink
Fix ShouldSwitch result
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawkkie committed Jan 18, 2025
1 parent 1252186 commit 6a5ae80
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
19 changes: 9 additions & 10 deletions src/battle_ai_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,24 +488,23 @@ void SetAiLogicDataForTurn(struct AiLogicData *aiData)
AI_DATA->aiCalcInProgress = FALSE;
}

void BattleAI_DoAIProcessing_PredictedSwitchin(struct AI_ThinkingStruct *aiThink, u32 battlerAtk, u32 battlerDef)
void BattleAI_DoAIProcessing_PredictedSwitchin(struct AI_ThinkingStruct *aiThink, struct AiLogicData *aiData, u32 battlerAtk, u32 battlerDef)
{
struct Pokemon *party = GetBattlerParty(battlerDef);
struct BattlePokemon *savedBattleMons = AllocSaveBattleMons();
struct BattlePokemon switchinCandidate;
PokemonToBattleMon(&party[AI_DATA->mostSuitableMonId[battlerDef]], &switchinCandidate);

// Saves, sets, restores
struct BattlePokemon *savedBattleMons = AllocSaveBattleMons();
PokemonToBattleMon(&party[aiData->mostSuitableMonId[battlerDef]], &switchinCandidate);
gBattleMons[battlerDef] = switchinCandidate;

SetBattlerAiData(battlerAtk, AI_DATA);
CalcBattlerAiMovesData(AI_DATA, battlerAtk, battlerDef, AI_GetWeather(AI_DATA));
SetBattlerAiData(battlerDef, aiData);
CalcBattlerAiMovesData(aiData, battlerAtk, battlerDef, AI_GetWeather(aiData));

// Regular processing with new battler
BattleAI_DoAIProcessing(aiThink, battlerAtk, battlerDef);

FreeRestoreBattleMons(savedBattleMons);
SetBattlerAiData(battlerAtk, AI_DATA);
SetBattlerAiData(battlerDef, aiData);
CalcBattlerAiMovesData(aiData, battlerAtk, battlerDef, AI_GetWeather(aiData));
}

static u32 ChooseMoveOrAction_Singles(u32 battlerAi)
Expand All @@ -522,7 +521,7 @@ static u32 ChooseMoveOrAction_Singles(u32 battlerAi)
if (flags & 1)
{
if (IsBattlerPredictedToSwitch(gBattlerTarget))
BattleAI_DoAIProcessing_PredictedSwitchin(AI_THINKING_STRUCT, battlerAi, gBattlerTarget);
BattleAI_DoAIProcessing_PredictedSwitchin(AI_THINKING_STRUCT, AI_DATA, battlerAi, gBattlerTarget);
else
BattleAI_DoAIProcessing(AI_THINKING_STRUCT, battlerAi, gBattlerTarget);
}
Expand Down Expand Up @@ -605,7 +604,7 @@ static u32 ChooseMoveOrAction_Doubles(u32 battlerAi)
if (flags & 1)
{
if (IsBattlerPredictedToSwitch(gBattlerTarget))
BattleAI_DoAIProcessing_PredictedSwitchin(AI_THINKING_STRUCT, battlerAi, gBattlerTarget);
BattleAI_DoAIProcessing_PredictedSwitchin(AI_THINKING_STRUCT, AI_DATA, battlerAi, gBattlerTarget);
else
BattleAI_DoAIProcessing(AI_THINKING_STRUCT, battlerAi, gBattlerTarget);
}
Expand Down
4 changes: 2 additions & 2 deletions src/battle_ai_switch_items.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ static bool32 ShouldSwitchIfHasBadOdds(u32 battler)
{
// 50% chance to stay in regardless
if (RandomPercentage(RNG_AI_SWITCH_HASBADODDS, 50) || AI_DATA->aiSwitchPredictionInProgress)
return FALSE;
return TRUE;

// Switch mon out
return SetSwitchinAndSwitch(battler, PARTY_SIZE);
Expand All @@ -218,7 +218,7 @@ static bool32 ShouldSwitchIfHasBadOdds(u32 battler)

// 50% chance to stay in regardless
if (RandomPercentage(RNG_AI_SWITCH_HASBADODDS, 50) || AI_DATA->aiSwitchPredictionInProgress)
return FALSE;
return TRUE;

// Switch mon out
return SetSwitchinAndSwitch(battler, PARTY_SIZE);
Expand Down
2 changes: 1 addition & 1 deletion test/battle/ai/ai_flag_predict_switch.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ AI_SINGLE_BATTLE_TEST("AI_FLAG_PREDICT_SWITCH: AI would switch out in Focus Punc

AI_SINGLE_BATTLE_TEST("AI_FLAG_PREDICT_SWITCH: AI will score against predicted incoming mon when switch predicted")
{
PASSES_RANDOMLY(5, 10, RNG_AI_PREDICT_SWITCH);
GIVEN {
AI_FLAGS(AI_FLAG_CHECK_BAD_MOVE | AI_FLAG_TRY_TO_FAINT | AI_FLAG_CHECK_VIABILITY | AI_FLAG_OMNISCIENT | AI_FLAG_SMART_SWITCHING | AI_FLAG_SMART_MON_CHOICES | AI_FLAG_PREDICT_SWITCH);
PLAYER(SPECIES_GENGAR) { Moves(MOVE_SHADOW_BALL); }
Expand All @@ -148,7 +149,6 @@ AI_SINGLE_BATTLE_TEST("AI_FLAG_PREDICT_SWITCH: AI will score against predicted i

AI_SINGLE_BATTLE_TEST("AI_FLAG_PREDICT_SWITCH: AI would switch out in predicted-incoming-mon scenario")
{
PASSES_RANDOMLY(5, 10, RNG_AI_SWITCH_HASBADODDS);
GIVEN {
AI_FLAGS(AI_FLAG_CHECK_BAD_MOVE | AI_FLAG_TRY_TO_FAINT | AI_FLAG_CHECK_VIABILITY | AI_FLAG_OMNISCIENT | AI_FLAG_SMART_SWITCHING | AI_FLAG_SMART_MON_CHOICES);
PLAYER(SPECIES_TYRANITAR) { Moves(MOVE_CRUNCH, MOVE_SPORE); }
Expand Down

0 comments on commit 6a5ae80

Please sign in to comment.