diff --git a/include/battle.h b/include/battle.h index a6e201a23..17fd49e7e 100644 --- a/include/battle.h +++ b/include/battle.h @@ -294,6 +294,9 @@ struct BattleResources u8 bufferB[MAX_BATTLERS_COUNT][0x200]; }; +#define AI_THINKING_STRUCT ((struct AI_ThinkingStruct *)(gBattleResources->ai)) +#define BATTLE_HISTORY ((struct BattleHistory *)(gBattleResources->battleHistory)) + struct BattleResults { u8 playerFaintCounter; // 0x0 diff --git a/include/battle_ai_script_commands.h b/include/battle_ai_script_commands.h index be3787398..eb57d613f 100644 --- a/include/battle_ai_script_commands.h +++ b/include/battle_ai_script_commands.h @@ -24,4 +24,6 @@ void ClearBattlerAbilityHistory(u8 battlerId); void RecordItemEffectBattle(u8 battlerId, u8 itemEffect); void ClearBattlerItemEffectHistory(u8 battlerId); +extern u8 sBattler_AI; + #endif // GUARD_BATTLE_AI_SCRIPT_COMMANDS_H diff --git a/include/battle_ai_util.h b/include/battle_ai_util.h index 2772c6621..dd9fe2a8a 100644 --- a/include/battle_ai_util.h +++ b/include/battle_ai_util.h @@ -1,8 +1,30 @@ #ifndef GUARD_BATTLE_AI_UTIL_H #define GUARD_BATTLE_AI_UTIL_H +// for IsBattlerFaster +#define AI_CHECK_FASTER 0 // if_user_faster +#define AI_CHECK_SLOWER 1 // if_target_faster + +void RecordLastUsedMoveByTarget(void); +bool32 IsBattlerAIControlled(u32 battlerId); +void ClearBattlerMoveHistory(u8 battlerId); +void RecordLastUsedMoveBy(u32 battlerId, u32 move); +void RecordKnownMove(u8 battlerId, u32 move); +void RecordAbilityBattle(u8 battlerId, u16 abilityId); +void ClearBattlerAbilityHistory(u8 battlerId); +void RecordItemEffectBattle(u8 battlerId, u8 itemEffect); +void ClearBattlerItemEffectHistory(u8 battlerId); +void SaveBattlerData(u8 battlerId); +void SetBattlerData(u8 battlerId); +void RestoreBattlerData(u8 battlerId); + u32 GetHealthPercentage(u8 battler); bool32 IsBattlerTrapped(u8 battler, bool8 switching); - +u8 GetMovePowerResult(u16 move); +u16 AI_GetTypeEffectiveness(u16 move, u8 battlerAtk, u8 battlerDef); +u8 GetMoveEffectiveness(u16 move); +bool32 IsBattlerFaster(u8 battler); +bool32 CanTargetFaintAi(void); +s32 AI_GetAbility(u32 battlerId, bool32 guess); #endif //GUARD_BATTLE_AI_UTIL_H \ No newline at end of file diff --git a/ld_script.txt b/ld_script.txt index 2bf411e62..c6276922a 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -590,6 +590,7 @@ SECTIONS { src/slot_machine.o(.rodata); src/contest_painting.o(.rodata); src/battle_ai_script_commands.o(.rodata); + src/battle_ai_util.o(.rodata); src/trader.o(.rodata); src/starter_choose.o(.rodata); src/wallclock.o(.rodata); diff --git a/src/battle_ai_script_commands.c b/src/battle_ai_script_commands.c index 695844bb2..1e0b414d0 100644 --- a/src/battle_ai_script_commands.c +++ b/src/battle_ai_script_commands.c @@ -27,9 +27,6 @@ #define AI_ACTION_UNK7 0x0040 #define AI_ACTION_UNK8 0x0080 -#define AI_THINKING_STRUCT ((struct AI_ThinkingStruct *)(gBattleResources->ai)) -#define BATTLE_HISTORY ((struct BattleHistory *)(gBattleResources->battleHistory)) - // AI states enum { @@ -51,12 +48,10 @@ extern const u8 *const gBattleAI_ScriptsTable[]; static u8 ChooseMoveOrAction_Singles(void); static u8 ChooseMoveOrAction_Doubles(void); -static void RecordLastUsedMoveByTarget(void); static void BattleAI_DoAIProcessing(void); static void AIStackPushVar(const u8 *); static bool8 AIStackPop(void); static s32 CountUsablePartyMons(u8 battlerId); -static s32 AI_GetAbility(u32 battlerId, bool32 guess); static void Cmd_if_random_less_than(void); static void Cmd_if_random_greater_than(void); @@ -183,7 +178,7 @@ static void Cmd_if_has_move_with_accuracy_lt(void); // ewram EWRAM_DATA const u8 *gAIScriptPtr = NULL; -EWRAM_DATA static u8 sBattler_AI = 0; +EWRAM_DATA u8 sBattler_AI = 0; // const rom data typedef void (*BattleAICmdFunc)(void); @@ -363,22 +358,6 @@ static const BattleAICmdFunc sBattleAICmdTable[] = Cmd_if_has_move_with_accuracy_lt, // 0x79 }; -static const u16 sDiscouragedPowerfulMoveEffects[] = -{ - EFFECT_EXPLOSION, - EFFECT_DREAM_EATER, - EFFECT_RECHARGE, - EFFECT_SKULL_BASH, - EFFECT_SOLARBEAM, - EFFECT_SPIT_UP, - EFFECT_FOCUS_PUNCH, - EFFECT_SUPERPOWER, - EFFECT_ERUPTION, - EFFECT_OVERHEAT, - EFFECT_MIND_BLOWN, - 0xFFFF -}; - // code void BattleAI_SetupItems(void) { @@ -794,139 +773,6 @@ static void BattleAI_DoAIProcessing(void) } } -static void RecordLastUsedMoveByTarget(void) -{ - RecordKnownMove(gBattlerTarget, gLastMoves[gBattlerTarget]); -} - -bool32 IsBattlerAIControlled(u32 battlerId) -{ - switch (GetBattlerPosition(battlerId)) - { - case B_POSITION_PLAYER_LEFT: - default: - return FALSE; - case B_POSITION_OPPONENT_LEFT: - return TRUE; - case B_POSITION_PLAYER_RIGHT: - return ((gBattleTypeFlags & BATTLE_TYPE_INGAME_PARTNER) != 0); - case B_POSITION_OPPONENT_RIGHT: - return TRUE; - } -} - -void ClearBattlerMoveHistory(u8 battlerId) -{ - memset(BATTLE_HISTORY->usedMoves[battlerId], 0, sizeof(BATTLE_HISTORY->usedMoves[battlerId])); - memset(BATTLE_HISTORY->moveHistory[battlerId], 0, sizeof(BATTLE_HISTORY->moveHistory[battlerId])); - BATTLE_HISTORY->moveHistoryIndex[battlerId] = 0; -} - -void RecordLastUsedMoveBy(u32 battlerId, u32 move) -{ - u8 *index = &BATTLE_HISTORY->moveHistoryIndex[battlerId]; - - if (++(*index) >= AI_MOVE_HISTORY_COUNT) - *index = 0; - BATTLE_HISTORY->moveHistory[battlerId][*index] = move; -} - -void RecordKnownMove(u8 battlerId, u32 move) -{ - s32 i; - for (i = 0; i < MAX_MON_MOVES; i++) - { - if (BATTLE_HISTORY->usedMoves[battlerId][i] == move) - break; - if (BATTLE_HISTORY->usedMoves[battlerId][i] == MOVE_NONE) - { - BATTLE_HISTORY->usedMoves[battlerId][i] = move; - break; - } - } -} - -void RecordAbilityBattle(u8 battlerId, u16 abilityId) -{ - BATTLE_HISTORY->abilities[battlerId] = abilityId; -} - -void ClearBattlerAbilityHistory(u8 battlerId) -{ - BATTLE_HISTORY->abilities[battlerId] = ABILITY_NONE; -} - -void RecordItemEffectBattle(u8 battlerId, u8 itemEffect) -{ - BATTLE_HISTORY->itemEffects[battlerId] = itemEffect; -} - -void ClearBattlerItemEffectHistory(u8 battlerId) -{ - BATTLE_HISTORY->itemEffects[battlerId] = 0; -} - -static void SaveBattlerData(u8 battlerId) -{ - if (!IsBattlerAIControlled(battlerId)) - { - u32 i; - - 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; - for (i = 0; i < 4; i++) - AI_THINKING_STRUCT->saved[battlerId].moves[i] = gBattleMons[battlerId].moves[i]; - } -} - -static void SetBattlerData(u8 battlerId) -{ - if (!IsBattlerAIControlled(battlerId)) - { - struct Pokemon *illusionMon; - u32 i; - - // Use the known battler's ability. - if (BATTLE_HISTORY->abilities[battlerId] != ABILITY_NONE) - gBattleMons[battlerId].ability = BATTLE_HISTORY->abilities[battlerId]; - // Check if mon can only have one ability. - else if (gBaseStats[gBattleMons[battlerId].species].abilities[1] == ABILITY_NONE - || gBaseStats[gBattleMons[battlerId].species].abilities[1] == gBaseStats[gBattleMons[battlerId].species].abilities[0]) - gBattleMons[battlerId].ability = gBaseStats[gBattleMons[battlerId].species].abilities[0]; - // The ability is unknown. - else - gBattleMons[battlerId].ability = ABILITY_NONE; - - if (BATTLE_HISTORY->itemEffects[battlerId] == 0) - gBattleMons[battlerId].item = 0; - - for (i = 0; i < 4; i++) - { - if (BATTLE_HISTORY->usedMoves[battlerId][i] == 0) - gBattleMons[battlerId].moves[i] = 0; - } - - // Simulate Illusion - if ((illusionMon = GetIllusionMonPtr(battlerId)) != NULL) - gBattleMons[battlerId].species = GetMonData(illusionMon, MON_DATA_SPECIES2); - } -} - -static void RestoreBattlerData(u8 battlerId) -{ - if (!IsBattlerAIControlled(battlerId)) - { - u32 i; - - 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; - for (i = 0; i < 4; i++) - gBattleMons[battlerId].moves[i] = AI_THINKING_STRUCT->saved[battlerId].moves[i]; - } -} - static bool32 AI_GetIfCrit(u32 move, u8 battlerAtk, u8 battlerDef) { bool32 isCrit; @@ -1003,27 +849,6 @@ s32 AI_CalcPartyMonDamage(u16 move, u8 battlerAtk, u8 battlerDef, struct Pokemon return dmg; } -u16 AI_GetTypeEffectiveness(u16 move, u8 battlerAtk, u8 battlerDef) -{ - u16 typeEffectiveness, moveType; - - SaveBattlerData(battlerAtk); - SaveBattlerData(battlerDef); - - SetBattlerData(battlerAtk); - SetBattlerData(battlerDef); - - gBattleStruct->dynamicMoveType = 0; - SetTypeBeforeUsingMove(move, battlerAtk); - GET_MOVE_TYPE(move, moveType); - typeEffectiveness = CalcTypeEffectivenessMultiplier(move, moveType, battlerAtk, battlerDef, FALSE); - - RestoreBattlerData(battlerAtk); - RestoreBattlerData(battlerDef); - - return typeEffectiveness; -} - static void Cmd_if_random_less_than(void) { u16 random = Random(); @@ -1459,141 +1284,11 @@ static void Cmd_get_considered_move_power(void) gAIScriptPtr += 1; } -// Checks if one of the moves has side effects or perks -static u32 WhichMoveBetter(u32 move1, u32 move2) -{ - s32 defAbility = AI_GetAbility(gBattlerTarget, FALSE); - // Check if physical moves hurt. - if (GetBattlerHoldEffect(sBattler_AI, TRUE) != HOLD_EFFECT_PROTECTIVE_PADS - && (BATTLE_HISTORY->itemEffects[gBattlerTarget] == HOLD_EFFECT_ROCKY_HELMET - || defAbility == ABILITY_IRON_BARBS || defAbility == ABILITY_ROUGH_SKIN)) - { - if (IS_MOVE_PHYSICAL(move1) && !IS_MOVE_PHYSICAL(move2)) - return 1; - if (IS_MOVE_PHYSICAL(move2) && !IS_MOVE_PHYSICAL(move1)) - return 0; - } - // Check recoil - if (GetBattlerAbility(sBattler_AI) != ABILITY_ROCK_HEAD) - { - if (((gBattleMoves[move1].effect == EFFECT_RECOIL_25 - || gBattleMoves[move1].effect == EFFECT_RECOIL_IF_MISS - || gBattleMoves[move1].effect == EFFECT_RECOIL_50 - || gBattleMoves[move1].effect == EFFECT_RECOIL_33 - || gBattleMoves[move1].effect == EFFECT_RECOIL_33_STATUS) - && (gBattleMoves[move2].effect != EFFECT_RECOIL_25 - && gBattleMoves[move2].effect != EFFECT_RECOIL_IF_MISS - && gBattleMoves[move2].effect != EFFECT_RECOIL_50 - && gBattleMoves[move2].effect != EFFECT_RECOIL_33 - && gBattleMoves[move2].effect != EFFECT_RECOIL_33_STATUS - && gBattleMoves[move2].effect != EFFECT_RECHARGE))) - return 1; - - if (((gBattleMoves[move2].effect == EFFECT_RECOIL_25 - || gBattleMoves[move2].effect == EFFECT_RECOIL_IF_MISS - || gBattleMoves[move2].effect == EFFECT_RECOIL_50 - || gBattleMoves[move2].effect == EFFECT_RECOIL_33 - || gBattleMoves[move2].effect == EFFECT_RECOIL_33_STATUS) - && (gBattleMoves[move1].effect != EFFECT_RECOIL_25 - && gBattleMoves[move1].effect != EFFECT_RECOIL_IF_MISS - && gBattleMoves[move1].effect != EFFECT_RECOIL_50 - && gBattleMoves[move1].effect != EFFECT_RECOIL_33 - && gBattleMoves[move1].effect != EFFECT_RECOIL_33_STATUS - && gBattleMoves[move1].effect != EFFECT_RECHARGE))) - return 0; - } - // Check recharge - if (gBattleMoves[move1].effect == EFFECT_RECHARGE && gBattleMoves[move2].effect != EFFECT_RECHARGE) - return 1; - if (gBattleMoves[move2].effect == EFFECT_RECHARGE && gBattleMoves[move1].effect != EFFECT_RECHARGE) - return 0; - // Check additional effect. - if (gBattleMoves[move1].effect == 0 && gBattleMoves[move2].effect != 0) - return 1; - if (gBattleMoves[move2].effect == 0 && gBattleMoves[move1].effect != 0) - return 0; - - return 2; -} static void Cmd_get_how_powerful_move_is(void) { - s32 i, checkedMove, bestId, currId, hp; - s32 moveDmgs[MAX_MON_MOVES]; - - for (i = 0; sDiscouragedPowerfulMoveEffects[i] != 0xFFFF; i++) - { - if (gBattleMoves[AI_THINKING_STRUCT->moveConsidered].effect == sDiscouragedPowerfulMoveEffects[i]) - break; - } - - if (gBattleMoves[AI_THINKING_STRUCT->moveConsidered].power != 0 - && sDiscouragedPowerfulMoveEffects[i] == 0xFFFF) - { - for (checkedMove = 0; checkedMove < MAX_MON_MOVES; checkedMove++) - { - for (i = 0; sDiscouragedPowerfulMoveEffects[i] != 0xFFFF; i++) - { - if (gBattleMoves[gBattleMons[sBattler_AI].moves[checkedMove]].effect == sDiscouragedPowerfulMoveEffects[i]) - break; - } - - if (gBattleMons[sBattler_AI].moves[checkedMove] != MOVE_NONE - && sDiscouragedPowerfulMoveEffects[i] == 0xFFFF - && gBattleMoves[gBattleMons[sBattler_AI].moves[checkedMove]].power != 0) - { - moveDmgs[checkedMove] = AI_THINKING_STRUCT->simulatedDmg[sBattler_AI][gBattlerTarget][checkedMove]; - } - else - { - moveDmgs[checkedMove] = 0; - } - } - - hp = gBattleMons[gBattlerTarget].hp + (20 * gBattleMons[gBattlerTarget].hp / 100); // 20 % add to make sure the battler is always fainted - // If a move can faint battler, it doesn't matter how much damage it does - for (i = 0; i < MAX_MON_MOVES; i++) - { - if (moveDmgs[i] > hp) - moveDmgs[i] = hp; - } - - for (bestId = 0, i = 1; i < MAX_MON_MOVES; i++) - { - if (moveDmgs[i] > moveDmgs[bestId]) - bestId = i; - if (moveDmgs[i] == moveDmgs[bestId]) - { - switch (WhichMoveBetter(gBattleMons[sBattler_AI].moves[bestId], gBattleMons[sBattler_AI].moves[i])) - { - case 2: - if (Random() & 1) - break; - case 1: - bestId = i; - break; - } - } - } - - currId = AI_THINKING_STRUCT->movesetIndex; - if (currId == bestId) - AI_THINKING_STRUCT->funcResult = MOVE_POWER_BEST; - // Compare percentage difference. - else if ((moveDmgs[currId] >= hp || moveDmgs[bestId] < hp) // If current move can faint as well, or if neither can - && (moveDmgs[bestId] * 100 / hp) - (moveDmgs[currId] * 100 / hp) <= 30 - && WhichMoveBetter(gBattleMons[sBattler_AI].moves[bestId], gBattleMons[sBattler_AI].moves[currId]) != 0) - AI_THINKING_STRUCT->funcResult = MOVE_POWER_GOOD; - else - AI_THINKING_STRUCT->funcResult = MOVE_POWER_WEAK; - } - else - { - AI_THINKING_STRUCT->funcResult = MOVE_POWER_DISCOURAGED; // Highly discouraged in terms of power. - } - - gAIScriptPtr++; + // GetMovePowerResult } static void Cmd_get_last_used_battler_move(void) @@ -1620,48 +1315,7 @@ static void Cmd_if_not_equal_u32(void) static void Cmd_if_user_goes(void) { - u32 fasterAI = 0, fasterPlayer = 0, i; - s8 prioAI, prioPlayer; - - // Check move priorities first. - prioAI = GetMovePriority(sBattler_AI, AI_THINKING_STRUCT->moveConsidered); - SaveBattlerData(gBattlerTarget); - SetBattlerData(gBattlerTarget); - for (i = 0; i < MAX_MON_MOVES; i++) - { - if (gBattleMons[gBattlerTarget].moves[i] == 0 || gBattleMons[gBattlerTarget].moves[i] == 0xFFFF) - continue; - - prioPlayer = GetMovePriority(gBattlerTarget, gBattleMons[gBattlerTarget].moves[i]); - if (prioAI > prioPlayer) - fasterAI++; - else if (prioPlayer > prioAI) - fasterPlayer++; - } - RestoreBattlerData(gBattlerTarget); - - if (fasterAI > fasterPlayer) - { - if (gAIScriptPtr[1] == 0) - gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 2); - else - gAIScriptPtr += 6; - } - else if (fasterAI < fasterPlayer) - { - if (gAIScriptPtr[1] == 1) - gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 2); - else - gAIScriptPtr += 6; - } - else - { - // Priorities are the same(at least comparing to moves the AI is aware of), decide by speed. - if (GetWhoStrikesFirst(sBattler_AI, gBattlerTarget, TRUE) == gAIScriptPtr[1]) - gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 2); - else - gAIScriptPtr += 6; - } + // IsBattlerFaster } static void Cmd_nullsub_2A(void) @@ -1726,37 +1380,6 @@ static void Cmd_get_considered_move_effect(void) gAIScriptPtr += 1; } -static s32 AI_GetAbility(u32 battlerId, bool32 guess) -{ - // The AI knows its own ability. - if (IsBattlerAIControlled(battlerId)) - return gBattleMons[battlerId].ability; - - if (BATTLE_HISTORY->abilities[battlerId] != 0) - return BATTLE_HISTORY->abilities[battlerId]; - - // Abilities that prevent fleeing. - if (gBattleMons[battlerId].ability == ABILITY_SHADOW_TAG - || gBattleMons[battlerId].ability == ABILITY_MAGNET_PULL - || gBattleMons[battlerId].ability == ABILITY_ARENA_TRAP) - return gBattleMons[battlerId].ability; - - if (gBaseStats[gBattleMons[battlerId].species].abilities[0] != ABILITY_NONE) - { - if (gBaseStats[gBattleMons[battlerId].species].abilities[1] != ABILITY_NONE) - { - // AI has no knowledge of opponent, so it guesses which ability. - if (guess) - return gBaseStats[gBattleMons[battlerId].species].abilities[Random() & 1]; - } - else - { - return gBaseStats[gBattleMons[battlerId].species].abilities[0]; // It's definitely ability 1. - } - } - return -1; // Unknown. -} - static void Cmd_get_ability(void) { AI_THINKING_STRUCT->funcResult = AI_GetAbility(BattleAI_GetWantedBattler(gAIScriptPtr[1]), TRUE); @@ -1824,39 +1447,7 @@ static void Cmd_get_highest_type_effectiveness(void) static void Cmd_if_type_effectiveness(void) { - u8 damageVar; - u32 effectivenessMultiplier; - - gMoveResultFlags = 0; - gCurrentMove = AI_THINKING_STRUCT->moveConsidered; - effectivenessMultiplier = AI_GetTypeEffectiveness(gCurrentMove, sBattler_AI, gBattlerTarget); - switch (effectivenessMultiplier) - { - case UQ_4_12(0.0): - default: - damageVar = AI_EFFECTIVENESS_x0; - break; - case UQ_4_12(0.25): - damageVar = AI_EFFECTIVENESS_x0_25; - break; - case UQ_4_12(0.5): - damageVar = AI_EFFECTIVENESS_x0_5; - break; - case UQ_4_12(1.0): - damageVar = AI_EFFECTIVENESS_x1; - break; - case UQ_4_12(2.0): - damageVar = AI_EFFECTIVENESS_x2; - break; - case UQ_4_12(4.0): - damageVar = AI_EFFECTIVENESS_x4; - break; - } - - if (damageVar == gAIScriptPtr[1]) - gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 2); - else - gAIScriptPtr += 6; + // GetMoveEffectiveness } static void Cmd_nullsub_32(void) @@ -2725,21 +2316,7 @@ static void Cmd_if_physical_moves_unusable(void) // Check if target has means to faint ai mon. static void Cmd_if_ai_can_go_down(void) { - s32 i, dmg; - u32 unusable = CheckMoveLimitations(gBattlerTarget, 0, 0xFF & ~MOVE_LIMITATION_PP); - u16 *moves = gBattleResources->battleHistory->usedMoves[gBattlerTarget]; - - for (i = 0; i < MAX_MON_MOVES; i++) - { - if (moves[i] != MOVE_NONE && moves[i] != 0xFFFF && !(unusable & gBitTable[i]) - && AI_CalcDamage(moves[i], gBattlerTarget, sBattler_AI) >= gBattleMons[sBattler_AI].hp) - { - gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 1); - return; - } - } - - gAIScriptPtr += 5; + // CanTargetFaintAi } static void Cmd_if_cant_use_belch(void) @@ -2940,6 +2517,48 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, u8 originalSco static u8 AI_TryToFaint(u8 battlerAtk, u8 battlerDef, u16 move, u8 originalScore) { + s32 dmg; + u8 result; + u8 score = originalScore; + + if (GetBattlerSide(battlerAtk) == GetBattlerSide(battlerDef)) + return originalScore; // don't try to faint your ally + + if (gBattleMoves[AI_THINKING_STRUCT->moveConsidered].power == 0) + return originalScore; // can't make anything faint with no power + + dmg = AI_THINKING_STRUCT->simulatedDmg[sBattler_AI][gBattlerTarget][AI_THINKING_STRUCT->movesetIndex]; + if (gBattleMons[gBattlerTarget].hp <= dmg && gBattleMoves[move].effect != EFFECT_EXPLOSION) + { + // AI_TryToFaint_Can + if (IsBattlerFaster(AI_CHECK_FASTER) || gBattleMoves[move].flags & FLAG_HIGH_CRIT) + score += 4; + else + score += 2; + } + else + { + if (GetMovePowerResult(move) == MOVE_POWER_DISCOURAGED) + return (score - 1); + + if (GetMoveEffectiveness(move) == AI_EFFECTIVENESS_x4) + { + // AI_TryToFaint_DoubleSuperEffective + if ((Random() % 256) >= 80) + score += 2; + } + } + + //AI_TryToFaint_CheckIfDanger + if (!IsBattlerFaster(AI_CHECK_FASTER) && CanTargetFaintAi()) + { // AI_TryToFaint_Danger + if (GetMovePowerResult(move) != MOVE_POWER_BEST) + score--; + else + score++; + } + + return score; } static u8 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, u8 originalScore) diff --git a/src/battle_ai_util.c b/src/battle_ai_util.c index 8b9778668..3a62c02b5 100644 --- a/src/battle_ai_util.c +++ b/src/battle_ai_util.c @@ -18,7 +18,157 @@ #include "constants/hold_effects.h" #include "constants/moves.h" +// Const Data +static const u16 sDiscouragedPowerfulMoveEffects[] = +{ + EFFECT_EXPLOSION, + EFFECT_DREAM_EATER, + EFFECT_RECHARGE, + EFFECT_SKULL_BASH, + EFFECT_SOLARBEAM, + EFFECT_SPIT_UP, + EFFECT_FOCUS_PUNCH, + EFFECT_SUPERPOWER, + EFFECT_ERUPTION, + EFFECT_OVERHEAT, + EFFECT_MIND_BLOWN, + 0xFFFF +}; + // Functions +void RecordLastUsedMoveByTarget(void) +{ + RecordKnownMove(gBattlerTarget, gLastMoves[gBattlerTarget]); +} + +bool32 IsBattlerAIControlled(u32 battlerId) +{ + switch (GetBattlerPosition(battlerId)) + { + case B_POSITION_PLAYER_LEFT: + default: + return FALSE; + case B_POSITION_OPPONENT_LEFT: + return TRUE; + case B_POSITION_PLAYER_RIGHT: + return ((gBattleTypeFlags & BATTLE_TYPE_INGAME_PARTNER) != 0); + case B_POSITION_OPPONENT_RIGHT: + return TRUE; + } +} + +void ClearBattlerMoveHistory(u8 battlerId) +{ + memset(BATTLE_HISTORY->usedMoves[battlerId], 0, sizeof(BATTLE_HISTORY->usedMoves[battlerId])); + memset(BATTLE_HISTORY->moveHistory[battlerId], 0, sizeof(BATTLE_HISTORY->moveHistory[battlerId])); + BATTLE_HISTORY->moveHistoryIndex[battlerId] = 0; +} + +void RecordLastUsedMoveBy(u32 battlerId, u32 move) +{ + u8 *index = &BATTLE_HISTORY->moveHistoryIndex[battlerId]; + + if (++(*index) >= AI_MOVE_HISTORY_COUNT) + *index = 0; + BATTLE_HISTORY->moveHistory[battlerId][*index] = move; +} + +void RecordKnownMove(u8 battlerId, u32 move) +{ + s32 i; + for (i = 0; i < MAX_MON_MOVES; i++) + { + if (BATTLE_HISTORY->usedMoves[battlerId][i] == move) + break; + if (BATTLE_HISTORY->usedMoves[battlerId][i] == MOVE_NONE) + { + BATTLE_HISTORY->usedMoves[battlerId][i] = move; + break; + } + } +} + +void RecordAbilityBattle(u8 battlerId, u16 abilityId) +{ + BATTLE_HISTORY->abilities[battlerId] = abilityId; +} + +void ClearBattlerAbilityHistory(u8 battlerId) +{ + BATTLE_HISTORY->abilities[battlerId] = ABILITY_NONE; +} + +void RecordItemEffectBattle(u8 battlerId, u8 itemEffect) +{ + BATTLE_HISTORY->itemEffects[battlerId] = itemEffect; +} + +void ClearBattlerItemEffectHistory(u8 battlerId) +{ + BATTLE_HISTORY->itemEffects[battlerId] = 0; +} + +void SaveBattlerData(u8 battlerId) +{ + if (!IsBattlerAIControlled(battlerId)) + { + u32 i; + + 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; + for (i = 0; i < 4; i++) + AI_THINKING_STRUCT->saved[battlerId].moves[i] = gBattleMons[battlerId].moves[i]; + } +} + +void SetBattlerData(u8 battlerId) +{ + if (!IsBattlerAIControlled(battlerId)) + { + struct Pokemon *illusionMon; + u32 i; + + // Use the known battler's ability. + if (BATTLE_HISTORY->abilities[battlerId] != ABILITY_NONE) + gBattleMons[battlerId].ability = BATTLE_HISTORY->abilities[battlerId]; + // Check if mon can only have one ability. + else if (gBaseStats[gBattleMons[battlerId].species].abilities[1] == ABILITY_NONE + || gBaseStats[gBattleMons[battlerId].species].abilities[1] == gBaseStats[gBattleMons[battlerId].species].abilities[0]) + gBattleMons[battlerId].ability = gBaseStats[gBattleMons[battlerId].species].abilities[0]; + // The ability is unknown. + else + gBattleMons[battlerId].ability = ABILITY_NONE; + + if (BATTLE_HISTORY->itemEffects[battlerId] == 0) + gBattleMons[battlerId].item = 0; + + for (i = 0; i < 4; i++) + { + if (BATTLE_HISTORY->usedMoves[battlerId][i] == 0) + gBattleMons[battlerId].moves[i] = 0; + } + + // Simulate Illusion + if ((illusionMon = GetIllusionMonPtr(battlerId)) != NULL) + gBattleMons[battlerId].species = GetMonData(illusionMon, MON_DATA_SPECIES2); + } +} + +void RestoreBattlerData(u8 battlerId) +{ + if (!IsBattlerAIControlled(battlerId)) + { + u32 i; + + 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; + for (i = 0; i < 4; i++) + gBattleMons[battlerId].moves[i] = AI_THINKING_STRUCT->saved[battlerId].moves[i]; + } +} + u32 GetHealthPercentage(u8 battlerId) { return (u32)(100 * gBattleMons[battlerId].hp / gBattleMons[battlerId].maxHP); @@ -44,4 +194,295 @@ bool32 IsBattlerTrapped(u8 battler, bool8 switching) } return FALSE; -} \ No newline at end of file +} + +// Checks if one of the moves has side effects or perks +static u32 WhichMoveBetter(u32 move1, u32 move2) +{ + s32 defAbility = AI_GetAbility(gBattlerTarget, FALSE); + + // Check if physical moves hurt. + if (GetBattlerHoldEffect(sBattler_AI, TRUE) != HOLD_EFFECT_PROTECTIVE_PADS + && (BATTLE_HISTORY->itemEffects[gBattlerTarget] == HOLD_EFFECT_ROCKY_HELMET + || defAbility == ABILITY_IRON_BARBS || defAbility == ABILITY_ROUGH_SKIN)) + { + if (IS_MOVE_PHYSICAL(move1) && !IS_MOVE_PHYSICAL(move2)) + return 1; + if (IS_MOVE_PHYSICAL(move2) && !IS_MOVE_PHYSICAL(move1)) + return 0; + } + // Check recoil + if (GetBattlerAbility(sBattler_AI) != ABILITY_ROCK_HEAD) + { + if (((gBattleMoves[move1].effect == EFFECT_RECOIL_25 + || gBattleMoves[move1].effect == EFFECT_RECOIL_IF_MISS + || gBattleMoves[move1].effect == EFFECT_RECOIL_50 + || gBattleMoves[move1].effect == EFFECT_RECOIL_33 + || gBattleMoves[move1].effect == EFFECT_RECOIL_33_STATUS) + && (gBattleMoves[move2].effect != EFFECT_RECOIL_25 + && gBattleMoves[move2].effect != EFFECT_RECOIL_IF_MISS + && gBattleMoves[move2].effect != EFFECT_RECOIL_50 + && gBattleMoves[move2].effect != EFFECT_RECOIL_33 + && gBattleMoves[move2].effect != EFFECT_RECOIL_33_STATUS + && gBattleMoves[move2].effect != EFFECT_RECHARGE))) + return 1; + + if (((gBattleMoves[move2].effect == EFFECT_RECOIL_25 + || gBattleMoves[move2].effect == EFFECT_RECOIL_IF_MISS + || gBattleMoves[move2].effect == EFFECT_RECOIL_50 + || gBattleMoves[move2].effect == EFFECT_RECOIL_33 + || gBattleMoves[move2].effect == EFFECT_RECOIL_33_STATUS) + && (gBattleMoves[move1].effect != EFFECT_RECOIL_25 + && gBattleMoves[move1].effect != EFFECT_RECOIL_IF_MISS + && gBattleMoves[move1].effect != EFFECT_RECOIL_50 + && gBattleMoves[move1].effect != EFFECT_RECOIL_33 + && gBattleMoves[move1].effect != EFFECT_RECOIL_33_STATUS + && gBattleMoves[move1].effect != EFFECT_RECHARGE))) + return 0; + } + // Check recharge + if (gBattleMoves[move1].effect == EFFECT_RECHARGE && gBattleMoves[move2].effect != EFFECT_RECHARGE) + return 1; + if (gBattleMoves[move2].effect == EFFECT_RECHARGE && gBattleMoves[move1].effect != EFFECT_RECHARGE) + return 0; + // Check additional effect. + if (gBattleMoves[move1].effect == 0 && gBattleMoves[move2].effect != 0) + return 1; + if (gBattleMoves[move2].effect == 0 && gBattleMoves[move1].effect != 0) + return 0; + + return 2; +} + +u8 GetMovePowerResult(u16 move) +{ + s32 i, checkedMove, bestId, currId, hp; + s32 moveDmgs[MAX_MON_MOVES]; + u8 result; + + for (i = 0; sDiscouragedPowerfulMoveEffects[i] != 0xFFFF; i++) + { + if (gBattleMoves[move].effect == sDiscouragedPowerfulMoveEffects[i]) + break; + } + + if (gBattleMoves[move].power != 0 && sDiscouragedPowerfulMoveEffects[i] == 0xFFFF) + { + for (checkedMove = 0; checkedMove < MAX_MON_MOVES; checkedMove++) + { + for (i = 0; sDiscouragedPowerfulMoveEffects[i] != 0xFFFF; i++) + { + if (gBattleMoves[gBattleMons[sBattler_AI].moves[checkedMove]].effect == sDiscouragedPowerfulMoveEffects[i]) + break; + } + + if (gBattleMons[sBattler_AI].moves[checkedMove] != MOVE_NONE + && sDiscouragedPowerfulMoveEffects[i] == 0xFFFF + && gBattleMoves[gBattleMons[sBattler_AI].moves[checkedMove]].power != 0) + { + moveDmgs[checkedMove] = AI_THINKING_STRUCT->simulatedDmg[sBattler_AI][gBattlerTarget][checkedMove]; + } + else + { + moveDmgs[checkedMove] = 0; + } + } + + hp = gBattleMons[gBattlerTarget].hp + (20 * gBattleMons[gBattlerTarget].hp / 100); // 20 % add to make sure the battler is always fainted + // If a move can faint battler, it doesn't matter how much damage it does + for (i = 0; i < MAX_MON_MOVES; i++) + { + if (moveDmgs[i] > hp) + moveDmgs[i] = hp; + } + + for (bestId = 0, i = 1; i < MAX_MON_MOVES; i++) + { + if (moveDmgs[i] > moveDmgs[bestId]) + bestId = i; + if (moveDmgs[i] == moveDmgs[bestId]) + { + switch (WhichMoveBetter(gBattleMons[sBattler_AI].moves[bestId], gBattleMons[sBattler_AI].moves[i])) + { + case 2: + if (Random() & 1) + break; + case 1: + bestId = i; + break; + } + } + } + + currId = AI_THINKING_STRUCT->movesetIndex; + if (currId == bestId) + AI_THINKING_STRUCT->funcResult = MOVE_POWER_BEST; + // Compare percentage difference. + else if ((moveDmgs[currId] >= hp || moveDmgs[bestId] < hp) // If current move can faint as well, or if neither can + && (moveDmgs[bestId] * 100 / hp) - (moveDmgs[currId] * 100 / hp) <= 30 + && WhichMoveBetter(gBattleMons[sBattler_AI].moves[bestId], gBattleMons[sBattler_AI].moves[currId]) != 0) + AI_THINKING_STRUCT->funcResult = MOVE_POWER_GOOD; + else + AI_THINKING_STRUCT->funcResult = MOVE_POWER_WEAK; + } + else + { + AI_THINKING_STRUCT->funcResult = MOVE_POWER_DISCOURAGED; // Highly discouraged in terms of power. + } + + return AI_THINKING_STRUCT->funcResult; +} + +u16 AI_GetTypeEffectiveness(u16 move, u8 battlerAtk, u8 battlerDef) +{ + u16 typeEffectiveness, moveType; + + SaveBattlerData(battlerAtk); + SaveBattlerData(battlerDef); + + SetBattlerData(battlerAtk); + SetBattlerData(battlerDef); + + gBattleStruct->dynamicMoveType = 0; + SetTypeBeforeUsingMove(move, battlerAtk); + GET_MOVE_TYPE(move, moveType); + typeEffectiveness = CalcTypeEffectivenessMultiplier(move, moveType, battlerAtk, battlerDef, FALSE); + + RestoreBattlerData(battlerAtk); + RestoreBattlerData(battlerDef); + + return typeEffectiveness; +} + +u8 GetMoveEffectiveness(u16 move) +{ + u8 damageVar; + u32 effectivenessMultiplier; + + gMoveResultFlags = 0; + gCurrentMove = move; + effectivenessMultiplier = AI_GetTypeEffectiveness(gCurrentMove, sBattler_AI, gBattlerTarget); + switch (effectivenessMultiplier) + { + case UQ_4_12(0.0): + default: + damageVar = AI_EFFECTIVENESS_x0; + break; + case UQ_4_12(0.25): + damageVar = AI_EFFECTIVENESS_x0_25; + break; + case UQ_4_12(0.5): + damageVar = AI_EFFECTIVENESS_x0_5; + break; + case UQ_4_12(1.0): + damageVar = AI_EFFECTIVENESS_x1; + break; + case UQ_4_12(2.0): + damageVar = AI_EFFECTIVENESS_x2; + break; + case UQ_4_12(4.0): + damageVar = AI_EFFECTIVENESS_x4; + break; + } + + return damageVar; +} + +// 0: is user(ai) faster +// 1: is target faster +bool32 IsBattlerFaster(u8 battler) +{ + u32 fasterAI = 0, fasterPlayer = 0, i; + s8 prioAI, prioPlayer; + + // Check move priorities first. + prioAI = GetMovePriority(sBattler_AI, AI_THINKING_STRUCT->moveConsidered); + SaveBattlerData(gBattlerTarget); + SetBattlerData(gBattlerTarget); + for (i = 0; i < MAX_MON_MOVES; i++) + { + if (gBattleMons[gBattlerTarget].moves[i] == 0 || gBattleMons[gBattlerTarget].moves[i] == 0xFFFF) + continue; + + prioPlayer = GetMovePriority(gBattlerTarget, gBattleMons[gBattlerTarget].moves[i]); + if (prioAI > prioPlayer) + fasterAI++; + else if (prioPlayer > prioAI) + fasterPlayer++; + } + RestoreBattlerData(gBattlerTarget); + + if (fasterAI > fasterPlayer) + { + if (battler == 0) // is user (ai) faster + return TRUE; + else + return FALSE; + } + else if (fasterAI < fasterPlayer) + { + if (battler == 1) // is target (player) faster + return TRUE; + else + return FALSE; + } + else + { + // Priorities are the same(at least comparing to moves the AI is aware of), decide by speed. + if (GetWhoStrikesFirst(sBattler_AI, gBattlerTarget, TRUE) == battler) + return TRUE; + else + return FALSE; + } +} + +// Check if target has means to faint ai mon. +bool32 CanTargetFaintAi(void) +{ + s32 i, dmg; + u32 unusable = CheckMoveLimitations(gBattlerTarget, 0, 0xFF & ~MOVE_LIMITATION_PP); + u16 *moves = gBattleResources->battleHistory->usedMoves[gBattlerTarget]; + + for (i = 0; i < MAX_MON_MOVES; i++) + { + if (moves[i] != MOVE_NONE && moves[i] != 0xFFFF && !(unusable & gBitTable[i]) + && AI_CalcDamage(moves[i], gBattlerTarget, sBattler_AI) >= gBattleMons[sBattler_AI].hp) + { + return TRUE; + } + } + + return FALSE; +} + +s32 AI_GetAbility(u32 battlerId, bool32 guess) +{ + // The AI knows its own ability. + if (IsBattlerAIControlled(battlerId)) + return gBattleMons[battlerId].ability; + + if (BATTLE_HISTORY->abilities[battlerId] != 0) + return BATTLE_HISTORY->abilities[battlerId]; + + // Abilities that prevent fleeing. + if (gBattleMons[battlerId].ability == ABILITY_SHADOW_TAG + || gBattleMons[battlerId].ability == ABILITY_MAGNET_PULL + || gBattleMons[battlerId].ability == ABILITY_ARENA_TRAP) + return gBattleMons[battlerId].ability; + + if (gBaseStats[gBattleMons[battlerId].species].abilities[0] != ABILITY_NONE) + { + if (gBaseStats[gBattleMons[battlerId].species].abilities[1] != ABILITY_NONE) + { + // AI has no knowledge of opponent, so it guesses which ability. + if (guess) + return gBaseStats[gBattleMons[battlerId].species].abilities[Random() & 1]; + } + else + { + return gBaseStats[gBattleMons[battlerId].species].abilities[0]; // It's definitely ability 1. + } + } + return -1; // Unknown. +} +