Skip to content

Commit

Permalink
Store calcs to avoid redoing them for speed
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawkkie committed Jan 18, 2025
1 parent 6ca405c commit 66cf6cc
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/battle_ai_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,20 @@ void BattleAI_DoAIProcessing_PredictedSwitchin(struct AI_ThinkingStruct *aiThink
struct Pokemon *party = GetBattlerParty(battlerDef);
struct BattlePokemon *savedBattleMons = AllocSaveBattleMons();
struct BattlePokemon switchinCandidate;
struct SimulatedDamage simulatedDamageSwitchout[4];
u8 effectivenessSwitchout[4];
u8 moveAccuracySwitchout[4];
u32 moveIndex;

// Store battler moves data to save time over recalculating it
for (moveIndex = 0; moveIndex < MAX_MON_MOVES; moveIndex++)
{
simulatedDamageSwitchout[moveIndex] = aiData->simulatedDmg[battlerAtk][battlerDef][moveIndex];
effectivenessSwitchout[moveIndex] = aiData->effectiveness[battlerAtk][battlerDef][moveIndex];
moveAccuracySwitchout[moveIndex] = aiData->moveAccuracy[battlerAtk][battlerDef][moveIndex];
}

// Get battler and move data for predicted switchin
PokemonToBattleMon(&party[aiData->mostSuitableMonId[battlerDef]], &switchinCandidate);
gBattleMons[battlerDef] = switchinCandidate;
SetBattlerAiData(battlerDef, aiData);
Expand All @@ -502,9 +515,15 @@ void BattleAI_DoAIProcessing_PredictedSwitchin(struct AI_ThinkingStruct *aiThink
// Regular processing with new battler
BattleAI_DoAIProcessing(aiThink, battlerAtk, battlerDef);

// Restore original battler data and moves
FreeRestoreBattleMons(savedBattleMons);
SetBattlerAiData(battlerDef, aiData);
CalcBattlerAiMovesData(aiData, battlerAtk, battlerDef, AI_GetWeather(aiData));
for (moveIndex = 0; moveIndex < MAX_MON_MOVES; moveIndex++)
{
aiData->simulatedDmg[battlerAtk][battlerDef][moveIndex] = simulatedDamageSwitchout[moveIndex];
aiData->effectiveness[battlerAtk][battlerDef][moveIndex] = effectivenessSwitchout[moveIndex];
aiData->moveAccuracy[battlerAtk][battlerDef][moveIndex] = moveAccuracySwitchout[moveIndex];
}
}

static u32 ChooseMoveOrAction_Singles(u32 battlerAi)
Expand Down

0 comments on commit 66cf6cc

Please sign in to comment.