Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move RestoreBattlerData out of Ai_CalcDmg and add SaveBattlerData Protections #4156

Merged
merged 6 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/battle.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ struct AI_SavedBattleMon
u16 ability;
u16 moves[MAX_MON_MOVES];
u16 heldItem;
u16 species;
u16 species:15;
u16 saved:1;
AlexOn1ine marked this conversation as resolved.
Show resolved Hide resolved
u8 types[3];
};

Expand Down
5 changes: 5 additions & 0 deletions src/battle_ai_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,16 @@ static void SetBattlerAiMovesData(struct AiLogicData *aiData, u32 battlerAtk, u3
moves = GetMovesArray(battlerAtk);
weather = AI_GetWeather(aiData);

SetBattlerData(battlerAtk);

// Simulate dmg for both ai controlled mons and for player controlled mons.
for (battlerDef = 0; battlerDef < battlersCount; battlerDef++)
{
if (battlerAtk == battlerDef)
continue;

SaveBattlerData(battlerDef);
SetBattlerData(battlerDef);
for (i = 0; i < MAX_MON_MOVES; i++)
{
s32 dmg = 0;
Expand All @@ -429,7 +432,9 @@ static void SetBattlerAiMovesData(struct AiLogicData *aiData, u32 battlerAtk, u3
aiData->simulatedDmg[battlerAtk][battlerDef][i] = dmg;
aiData->effectiveness[battlerAtk][battlerDef][i] = effectiveness;
}
RestoreBattlerData(battlerDef);
}
RestoreBattlerData(battlerAtk);
}

void SetAiLogicDataForTurn(struct AiLogicData *aiData)
Expand Down
22 changes: 12 additions & 10 deletions src/battle_ai_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,11 @@ void ClearBattlerItemEffectHistory(u32 battlerId)

void SaveBattlerData(u32 battlerId)
{
if (!BattlerHasAi(battlerId))
if (!BattlerHasAi(battlerId) && !AI_THINKING_STRUCT->saved[battlerId].saved)
{
u32 i;

AI_THINKING_STRUCT->saved[battlerId].saved = TRUE;
AI_THINKING_STRUCT->saved[battlerId].ability = gBattleMons[battlerId].ability;
AI_THINKING_STRUCT->saved[battlerId].heldItem = gBattleMons[battlerId].item;
AI_THINKING_STRUCT->saved[battlerId].species = gBattleMons[battlerId].species;
Expand Down Expand Up @@ -196,7 +197,7 @@ static bool32 ShouldFailForIllusion(u32 illusionSpecies, u32 battlerId)

void SetBattlerData(u32 battlerId)
{
if (!BattlerHasAi(battlerId))
if (!BattlerHasAi(battlerId) && AI_THINKING_STRUCT->saved[battlerId].saved)
{
u32 i, species, illusionSpecies, side;
side = GetBattlerSide(battlerId);
Expand Down Expand Up @@ -240,10 +241,11 @@ void SetBattlerData(u32 battlerId)

void RestoreBattlerData(u32 battlerId)
{
if (!BattlerHasAi(battlerId))
if (!BattlerHasAi(battlerId) && AI_THINKING_STRUCT->saved[battlerId].saved)
{
u32 i;

AI_THINKING_STRUCT->saved[battlerId].saved = FALSE;
gBattleMons[battlerId].ability = AI_THINKING_STRUCT->saved[battlerId].ability;
gBattleMons[battlerId].item = AI_THINKING_STRUCT->saved[battlerId].heldItem;
gBattleMons[battlerId].species = AI_THINKING_STRUCT->saved[battlerId].species;
Expand Down Expand Up @@ -354,9 +356,15 @@ bool32 MovesWithCategoryUnusable(u32 attacker, u32 target, u32 category)
// To save computation time this function has 2 variants. One saves, sets and restores battlers, while the other doesn't.
s32 AI_CalcDamageSaveBattlers(u32 move, u32 battlerAtk, u32 battlerDef, u8 *typeEffectiveness, bool32 considerZPower)
{
s32 dmg = 0;
SaveBattlerData(battlerAtk);
SaveBattlerData(battlerDef);
return AI_CalcDamage(move, battlerAtk, battlerDef, typeEffectiveness, considerZPower, AI_GetWeather(AI_DATA));
SetBattlerData(battlerAtk);
SetBattlerData(battlerDef);
dmg = AI_CalcDamage(move, battlerAtk, battlerDef, typeEffectiveness, considerZPower, AI_GetWeather(AI_DATA));
RestoreBattlerData(battlerAtk);
RestoreBattlerData(battlerDef);
return dmg;
}

static inline s32 LowestRollDmg(s32 dmg)
Expand All @@ -372,9 +380,6 @@ s32 AI_CalcDamage(u32 move, u32 battlerAtk, u32 battlerDef, u8 *typeEffectivenes
uq4_12_t effectivenessMultiplier;
struct AiLogicData *aiData = AI_DATA;

SetBattlerData(battlerAtk);
SetBattlerData(battlerDef);

AlexOn1ine marked this conversation as resolved.
Show resolved Hide resolved
if (considerZPower && IsViableZMove(battlerAtk, move))
{
//temporarily enable z moves for damage calcs
Expand Down Expand Up @@ -492,9 +497,6 @@ s32 AI_CalcDamage(u32 move, u32 battlerAtk, u32 battlerDef, u8 *typeEffectivenes
dmg = 0;
}

RestoreBattlerData(battlerAtk);
RestoreBattlerData(battlerDef);

// convert multiper to AI_EFFECTIVENESS_xX
*typeEffectiveness = AI_GetEffectiveness(effectivenessMultiplier);

Expand Down
Loading