Skip to content

Commit

Permalink
Should switch refactor to facilitate switch prediction (rh-hideout#5466)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Griffin <martinrgriffin@gmail.com>
  • Loading branch information
Pawkkie and mrgriffin authored Oct 25, 2024
1 parent afa7ab9 commit 7413d10
Show file tree
Hide file tree
Showing 13 changed files with 584 additions and 457 deletions.
2 changes: 1 addition & 1 deletion include/battle.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ struct AiLogicData
u16 items[MAX_BATTLERS_COUNT];
u16 holdEffects[MAX_BATTLERS_COUNT];
u8 holdEffectParams[MAX_BATTLERS_COUNT];
u16 predictedMoves[MAX_BATTLERS_COUNT];
u16 lastUsedMove[MAX_BATTLERS_COUNT];
u8 hpPercents[MAX_BATTLERS_COUNT];
u16 partnerMove;
u16 speedStats[MAX_BATTLERS_COUNT]; // Speed stats for all battles, calculated only once, same way as damages
Expand Down
1 change: 0 additions & 1 deletion include/battle_ai_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ typedef s32 (*AiScoreFunc)(u32, u32, u32, s32);
return score; \
}

u32 ComputeBattleAiScores(u32 battler);
void BattleAI_SetupItems(void);
void BattleAI_SetupFlags(void);
void BattleAI_SetupAIData(u8 defaultScoreMoves, u32 battler);
Expand Down
2 changes: 1 addition & 1 deletion include/battle_ai_switch_items.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
void GetAIPartyIndexes(u32 battlerId, s32 *firstId, s32 *lastId);
void AI_TrySwitchOrUseItem(u32 battler);
u32 GetMostSuitableMonToSwitchInto(u32 battler, bool32 switchAfterMonKOd);
bool32 ShouldSwitch(u32 battler, bool32 emitResult);
bool32 ShouldSwitch(u32 battler);
bool32 IsMonGrounded(u16 heldItemEffect, u32 ability, u8 type1, u8 type2);

#endif // GUARD_BATTLE_AI_SWITCH_ITEMS_H
7 changes: 6 additions & 1 deletion include/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,16 @@ enum RandomTag
RNG_FICKLE_BEAM,
RNG_AI_ABILITY,
RNG_AI_SWITCH_HASBADODDS,
RNG_AI_SWITCH_WONDER_GUARD,
RNG_AI_SWITCH_BADLY_POISONED,
RNG_AI_SWITCH_CURSED,
RNG_AI_SWITCH_NIGHTMARE,
RNG_AI_SWITCH_SEEDED,
RNG_AI_SWITCH_ABSORBING,
RNG_AI_SWITCH_NATURAL_CURE,
RNG_AI_SWITCH_REGENERATOR,
RNG_AI_SWITCH_ENCORE,
RNG_AI_SWITCH_STATS_LOWERED,
RNG_AI_SWITCH_SE_DEFENSIVE,
RNG_SHELL_SIDE_ARM,
RNG_RANDOM_TARGET,
RNG_HEALER,
Expand Down
21 changes: 6 additions & 15 deletions src/battle_ai_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ void BattleAI_SetupFlags(void)
}
}

// sBattler_AI set in ComputeBattleAiScores
void BattleAI_SetupAIData(u8 defaultScoreMoves, u32 battler)
{
s32 i;
Expand Down Expand Up @@ -287,14 +286,6 @@ u32 BattleAI_ChooseMoveOrAction(void)
return ret;
}

// damages/other info computed in GetAIDataAndCalcDmg
u32 ComputeBattleAiScores(u32 battler)
{
sBattler_AI = battler;
BattleAI_SetupAIData(0xF, sBattler_AI);
return BattleAI_ChooseMoveOrAction();
}

static void CopyBattlerDataToAIParty(u32 bPosition, u32 side)
{
u32 battler = GetBattlerAtPosition(bPosition);
Expand Down Expand Up @@ -397,7 +388,7 @@ void SetBattlerAiData(u32 battler, struct AiLogicData *aiData)
aiData->items[battler] = gBattleMons[battler].item;
holdEffect = aiData->holdEffects[battler] = AI_DecideHoldEffectForTurn(battler);
aiData->holdEffectParams[battler] = GetBattlerHoldEffectParam(battler);
aiData->predictedMoves[battler] = gLastMoves[battler];
aiData->lastUsedMove[battler] = gLastMoves[battler];
aiData->hpPercents[battler] = GetHealthPercentage(battler);
aiData->moveLimitations[battler] = CheckMoveLimitations(battler, 0, MOVE_LIMITATIONS_ALL);
aiData->speedStats[battler] = GetBattlerTotalSpeedStatArgs(battler, ability, holdEffect);
Expand Down Expand Up @@ -729,7 +720,7 @@ static s32 AI_CheckBadMove(u32 battlerAtk, u32 battlerDef, u32 move, s32 score)
bool32 isDoubleBattle = IsValidDoubleBattle(battlerAtk);
u32 i;
u32 weather;
u32 predictedMove = aiData->predictedMoves[battlerDef];
u32 predictedMove = aiData->lastUsedMove[battlerDef];

if (IS_TARGETING_PARTNER(battlerAtk, battlerDef))
return score;
Expand Down Expand Up @@ -2661,7 +2652,7 @@ static s32 AI_DoubleBattle(u32 battlerAtk, u32 battlerDef, u32 move, s32 score)
bool32 partnerProtecting = (gMovesInfo[aiData->partnerMove].effect == EFFECT_PROTECT);
bool32 attackerHasBadAbility = (gAbilitiesInfo[aiData->abilities[battlerAtk]].aiRating < 0);
bool32 partnerHasBadAbility = (gAbilitiesInfo[atkPartnerAbility].aiRating < 0);
u32 predictedMove = aiData->predictedMoves[battlerDef];
u32 predictedMove = aiData->lastUsedMove[battlerDef];

SetTypeBeforeUsingMove(move, battlerAtk);
moveType = GetMoveType(move);
Expand Down Expand Up @@ -3195,7 +3186,7 @@ static u32 AI_CalcMoveEffectScore(u32 battlerAtk, u32 battlerDef, u32 move)
u32 effectiveness = aiData->effectiveness[battlerAtk][battlerDef][movesetIndex];

s32 score = 0;
u32 predictedMove = aiData->predictedMoves[battlerDef];
u32 predictedMove = aiData->lastUsedMove[battlerDef];
u32 predictedMoveSlot = GetMoveSlot(GetMovesArray(battlerDef), predictedMove);
bool32 isDoubleBattle = IsValidDoubleBattle(battlerAtk);
u32 i;
Expand Down Expand Up @@ -4369,7 +4360,7 @@ static u32 AI_CalcMoveEffectScore(u32 battlerAtk, u32 battlerDef, u32 move)
break;
case EFFECT_MAGNET_RISE:
if (IsBattlerGrounded(battlerAtk) && HasDamagingMoveOfType(battlerDef, TYPE_ELECTRIC)
&& !(AI_GetTypeEffectiveness(MOVE_EARTHQUAKE, battlerDef, battlerAtk) == AI_EFFECTIVENESS_x0)) // Doesn't resist ground move
&& !(AI_GetMoveEffectiveness(MOVE_EARTHQUAKE, battlerDef, battlerAtk) == AI_EFFECTIVENESS_x0)) // Doesn't resist ground move
{
if (AI_IsFaster(battlerAtk, battlerDef, move)) // Attacker goes first
{
Expand All @@ -4387,7 +4378,7 @@ static u32 AI_CalcMoveEffectScore(u32 battlerAtk, u32 battlerDef, u32 move)
break;
case EFFECT_CAMOUFLAGE:
if (predictedMove != MOVE_NONE && AI_IsFaster(battlerAtk, battlerDef, move) // Attacker goes first
&& !IS_MOVE_STATUS(move) && AI_GetTypeEffectiveness(predictedMove, battlerDef, battlerAtk) != AI_EFFECTIVENESS_x0)
&& !IS_MOVE_STATUS(move) && AI_GetMoveEffectiveness(predictedMove, battlerDef, battlerAtk) != AI_EFFECTIVENESS_x0)
ADJUST_SCORE(DECENT_EFFECT);
break;
case EFFECT_TOXIC_THREAD:
Expand Down
Loading

0 comments on commit 7413d10

Please sign in to comment.