From 31ec3e84799719edc4c492601f7a10d647876b5f Mon Sep 17 00:00:00 2001 From: Evan Date: Sat, 19 Dec 2020 21:58:23 -0700 Subject: [PATCH] non-partner good move checks --- data/battle_ai_scripts.s | 62 - include/battle.h | 6 + include/battle_ai_switch_items.h | 1 + include/battle_ai_util.h | 66 +- include/constants/battle.h | 3 +- include/constants/battle_ai.h | 2 + include/item.h | 1 + src/battle_ai_script_commands.c | 2195 ++++++++++++++++++++++-------- src/battle_ai_switch_items.c | 2 +- src/battle_ai_util.c | 1575 ++++++++++++++++++++- src/item.c | 23 + 11 files changed, 3258 insertions(+), 678 deletions(-) diff --git a/data/battle_ai_scripts.s b/data/battle_ai_scripts.s index c71c6b3c1..0ea757374 100644 --- a/data/battle_ai_scripts.s +++ b/data/battle_ai_scripts.s @@ -2301,68 +2301,6 @@ AI_CV_Encore_End: end AI_CV_Encore_EncouragedMovesToEncore: - .byte EFFECT_DREAM_EATER - .byte EFFECT_ATTACK_UP - .byte EFFECT_DEFENSE_UP - .byte EFFECT_SPEED_UP - .byte EFFECT_SPECIAL_ATTACK_UP - .byte EFFECT_HAZE - .byte EFFECT_ROAR - .byte EFFECT_CONVERSION - .byte EFFECT_TOXIC - .byte EFFECT_LIGHT_SCREEN - .byte EFFECT_REST - .byte EFFECT_SUPER_FANG - .byte EFFECT_SPECIAL_DEFENSE_UP_2 - .byte EFFECT_CONFUSE - .byte EFFECT_POISON - .byte EFFECT_PARALYZE - .byte EFFECT_LEECH_SEED - .byte EFFECT_DO_NOTHING - .byte EFFECT_ATTACK_UP_2 - .byte EFFECT_ENCORE - .byte EFFECT_CONVERSION_2 - .byte EFFECT_LOCK_ON - .byte EFFECT_HEAL_BELL - .byte EFFECT_MEAN_LOOK - .byte EFFECT_NIGHTMARE - .byte EFFECT_PROTECT - .byte EFFECT_SKILL_SWAP - .byte EFFECT_FORESIGHT - .byte EFFECT_PERISH_SONG - .byte EFFECT_SANDSTORM - .byte EFFECT_ENDURE - .byte EFFECT_SWAGGER - .byte EFFECT_ATTRACT - .byte EFFECT_SAFEGUARD - .byte EFFECT_RAIN_DANCE - .byte EFFECT_SUNNY_DAY - .byte EFFECT_BELLY_DRUM - .byte EFFECT_PSYCH_UP - .byte EFFECT_FUTURE_SIGHT - .byte EFFECT_FAKE_OUT - .byte EFFECT_STOCKPILE - .byte EFFECT_SPIT_UP - .byte EFFECT_SWALLOW - .byte EFFECT_HAIL - .byte EFFECT_TORMENT - .byte EFFECT_WILL_O_WISP - .byte EFFECT_FOLLOW_ME - .byte EFFECT_CHARGE - .byte EFFECT_TRICK - .byte EFFECT_ROLE_PLAY - .byte EFFECT_INGRAIN - .byte EFFECT_RECYCLE - .byte EFFECT_KNOCK_OFF - .byte EFFECT_SKILL_SWAP - .byte EFFECT_IMPRISON - .byte EFFECT_REFRESH - .byte EFFECT_GRUDGE - .byte EFFECT_TEETER_DANCE - .byte EFFECT_MUD_SPORT - .byte EFFECT_WATER_SPORT - .byte EFFECT_DRAGON_DANCE - .byte EFFECT_CAMOUFLAGE .byte -1 AI_CV_PainSplit: diff --git a/include/battle.h b/include/battle.h index 07b828972..12fe42f8f 100644 --- a/include/battle.h +++ b/include/battle.h @@ -239,20 +239,26 @@ struct AiLogicData { //attacker data u16 atkAbility; + u16 atkItem; u16 atkHoldEffect; u8 atkParam; + u16 atkSpecies; // target data u16 defAbility; + u16 defItem; u16 defHoldEffect; u8 defParam; + u16 defSpecies; // attacker partner data u8 battlerAtkPartner; u16 partnerMove; u16 atkPartnerAbility; + u16 atkPartnerHoldEffect; bool32 targetSameSide; // target partner data u8 battlerDefPartner; u16 defPartnerAbility; + u16 defPartnerHoldEffect; }; struct AI_ThinkingStruct diff --git a/include/battle_ai_switch_items.h b/include/battle_ai_switch_items.h index 9780fae70..141138450 100644 --- a/include/battle_ai_switch_items.h +++ b/include/battle_ai_switch_items.h @@ -14,5 +14,6 @@ enum void GetAIPartyIndexes(u32 battlerId, s32 *firstId, s32 *lastId); void AI_TrySwitchOrUseItem(void); u8 GetMostSuitableMonToSwitchInto(void); +bool32 ShouldSwitch(void); #endif // GUARD_BATTLE_AI_SWITCH_ITEMS_H diff --git a/include/battle_ai_util.h b/include/battle_ai_util.h index 043235fa8..7fd991bf2 100644 --- a/include/battle_ai_util.h +++ b/include/battle_ai_util.h @@ -1,7 +1,7 @@ #ifndef GUARD_BATTLE_AI_UTIL_H #define GUARD_BATTLE_AI_UTIL_H -// for IsBattlerFaster +// for IsAiFaster #define AI_CHECK_FASTER 0 // if_user_faster #define AI_CHECK_SLOWER 1 // if_target_faster @@ -20,25 +20,36 @@ void SaveBattlerData(u8 battlerId); void SetBattlerData(u8 battlerId); void RestoreBattlerData(u8 battlerId); +bool32 AtMaxHp(u8 battler); u32 GetHealthPercentage(u8 battler); bool32 IsBattlerTrapped(u8 battler, bool8 switching); -bool32 IsBattlerFaster(u8 battler); +bool32 IsAiFaster(u8 battler); bool32 CanTargetFaintAi(u8 battlerDef, u8 battlerAtk); +bool32 CanMoveFaintBattler(u16 move, u8 battlerDef, u8 battlerAtk, u8 nHits); bool32 CanTargetFaintAiWithMod(u8 battlerDef, u8 battlerAtk, s32 hpMod, s32 dmgMod); s32 AI_GetAbility(u32 battlerId); u16 AI_GetHoldEffect(u32 battlerId); u32 AI_GetMoveAccuracy(u8 battlerAtk, u8 battlerDef, u16 atkAbility, u16 defAbility, u8 atkHoldEffect, u8 defHoldEffect, u16 move); bool32 DoesBattlerIgnoreAbilityChecks(u16 atkAbility, u16 move); bool32 AI_WeatherHasEffect(void); -bool32 CanAttackerFaintTarget(u8 battlerAtk, u8 battlerDef, u8 index); +bool32 CanAttackerFaintTarget(u8 battlerAtk, u8 battlerDef, u8 index, u8 numHits); bool32 AI_IsBattlerGrounded(u8 battlerId); -bool32 BattlerHasDamagingMove(u8 battlerId); -bool32 BattlerHasSecondaryDamage(u8 battlerId); +bool32 HasDamagingMove(u8 battlerId); +bool32 HasDamagingMoveOfType(u8 battlerId, u8 type); +u32 GetBattlerSecondaryDamage(u8 battlerId); bool32 BattlerWillFaintFromWeather(u8 battler, u16 ability); +bool32 BattlerWillFaintFromSecondaryDamage(u8 battler, u16 ability); bool32 ShouldTryOHKO(u8 battlerAtk, u8 battlerDef, u16 atkAbility, u16 defAbility, u32 accuracy, u16 move); bool32 ShouldUseRecoilMove(u8 battlerAtk, u8 battlerDef, u32 recoilDmg, u8 moveIndex); u16 GetBattlerSideSpeedAverage(u8 battler); -bool32 ShouldRecover(u8 battlerAtk, u8 battlerDef, u16 move, s32 damage); +bool32 ShouldAbsorb(u8 battlerAtk, u8 battlerDef, u16 move, s32 damage); +bool32 ShouldRecover(u8 battlerAtk, u8 battlerDef, u16 move, u8 healPercent); +bool32 ShouldSetScreen(u8 battlerAtk, u8 battlerDef, u16 moveEffect); +bool32 ShouldPivot(u8 battlerAtk, u8 battlerDef, u16 defAbility, u16 move, u8 moveIndex); +bool32 IsRecycleEncouragedItem(u16 item); +bool32 CanKnockOffItem(u8 battler, u16 item); +bool32 IsAbilityOfRating(u16 ability, s8 rating); +s8 GetAbilityRating(u16 ability); // stat stage checks bool32 AnyStatIsRaised(u8 battlerId); @@ -50,8 +61,15 @@ u32 CountPositiveStatStages(u8 battlerId); u32 CountNegativeStatStages(u8 battlerId); bool32 BattlerShouldRaiseAttacks(u8 battlerId, u16 ability); bool32 ShouldLowerAttack(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 moveIndex); +bool32 ShouldLowerDefense(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 moveIndex); +bool32 ShouldLowerSpeed(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 moveIndex); +bool32 ShouldLowerSpAtk(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 moveIndex); +bool32 ShouldLowerSpDef(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 moveIndex); +bool32 ShouldLowerAccuracy(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 moveIndex); +bool32 ShouldLowerEvasion(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 moveIndex); // move checks +bool32 IsAffectedByPowder(u8 battler, u16 ability, u16 holdEffect); bool32 MovesWithSplitUnusable(u32 attacker, u32 target, u32 split); s32 AI_CalcDamage(u16 move, u8 battlerAtk, u8 battlerDef); u8 GetMoveDamageResult(u16 move); @@ -59,10 +77,12 @@ u16 AI_GetTypeEffectiveness(u16 move, u8 battlerAtk, u8 battlerDef); u8 AI_GetMoveEffectiveness(u16 move); u16 *GetMovesArray(u32 battler); bool32 IsConfusionMoveEffect(u16 moveEffect); +bool32 HasMove(u32 battlerId, u32 move); bool32 HasMoveWithSplit(u32 battler, u32 split); bool32 HasMoveWithType(u32 battler, u8 type); +bool32 HasMoveWithTypeAndSplit(u32 battler, u8 type, u8 split); bool32 HasMoveEffect(u32 battlerId, u16 moveEffect); -bool32 HasMoveWithLowAccuracy(u8, u8, u8, bool32, u16, u16, u16, u16, u16); +bool32 HasMoveWithLowAccuracy(u8, u8, u8, bool32, u16, u16, u16, u16); bool32 TestMoveFlagsInMoveset(u8 battler, u32 flags); bool32 IsAromaVeilProtectedMove(u16 move); bool32 IsNonVolatileStatusMoveEffect(u16 moveEffect); @@ -73,15 +93,36 @@ bool32 IsHazardMoveEffect(u16 moveEffect); bool32 MoveCallsOtherMove(u16 move); bool32 MoveRequiresRecharging(u16 move); bool32 IsInstructBannedMove(u16 move); +bool32 IsEncoreEncouragedEffect(u16 moveEffect); +void ProtectChecks(u8 battlerAtk, u8 battlerDef, u16 move, u16 predictedMove, s16 *score); +bool32 ShouldSetSandstorm(u8 battler, u16 ability, u16 holdEffect); +bool32 ShouldSetHail(u8 battler, u16 ability, u16 holdEffect); +bool32 ShouldSetRain(u8 battlerAtk, u16 ability, u16 holdEffect); +bool32 ShouldSetSun(u8 battlerAtk, u16 atkAbility, u16 holdEffect); +bool32 HasSleepMoveWithLowAccuracy(u8 battlerAtk, u8 battlerDef); +bool32 IsHealingMoveEffect(u16 effect); +bool32 HasHealingEffect(u32 battler); +bool32 ShouldFakeOut(u8 battlerAtk, u8 battlerDef, u16 move); +bool32 IsThawingMove(u16 move); +bool32 HasThawingMove(u8 battlerId); // status checks +bool32 CanBeBurned(u8 battler, u16 ability); +bool32 CanBePoisoned(u8 battler, u16 ability); +bool32 IsBattlerIncapacitated(u8 battler, u16 ability); +bool32 CanSleep(u8 battler, u16 ability); bool32 AI_CanPutToSleep(u8 battlerAtk, u8 battlerDef, u16 defAbility, u16 move, u16 partnerMove); +bool32 ShouldPoisonSelf(u8 battler, u16 ability); bool32 AI_CanPoison(u8 battlerAtk, u8 battlerDef, u16 defAbility, u16 move, u16 partnerMove); bool32 AI_CanParalyze(u8 battlerAtk, u8 battlerDef, u16 defAbility, u16 move, u16 partnerMove); bool32 AI_CanConfuse(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 battlerAtkPartner, u16 move, u16 partnerMove); +bool32 ShouldBurnSelf(u8 battler, u16 ability); bool32 AI_CanBurn(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 battlerAtkPartner, u16 move, u16 partnerMove); bool32 AI_CanBeInfatuated(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 atkGender, u8 defGender); bool32 AnyPartyMemberStatused(u8 battlerId, bool32 checkSoundproof); +u32 ShouldTryToFlinch(u8 battlerAtk, u8 battlerDef, u16 atkAbility, u16 defAbility, u16 move); +bool32 ShouldTrap(u8 battlerAtk, u8 battlerDef, u16 move); +bool32 IsWakeupTurn(u8 battler); // partner logic u16 GetAllyChosenMove(void); @@ -95,10 +136,21 @@ bool32 PartnerMoveEffectIsTerrain(u8 battlerAtkPartner, u16 partnerMove); bool32 PartnerMoveIs(u8 battlerAtkPartner, u16 partnerMove, u16 moveCheck); bool32 PartnerMoveIsSameAsAttacker(u8 battlerAtkPartner, u8 battlerDef, u16 move, u16 partnerMove); bool32 PartnerMoveIsSameNoTarget(u8 battlerAtkPartner, u16 move, u16 partnerMove); +bool32 ShouldUseWishAromatherapy(u8 battlerAtk, u8 battlerDef, u16 move); // party logic s32 AI_CalcPartyMonDamage(u16 move, u8 battlerAtk, u8 battlerDef, struct Pokemon *mon); s32 CountUsablePartyMons(u8 battlerId); bool32 IsPartyFullyHealedExceptBattler(u8 battler); +bool32 PartyHasMoveSplit(u8 battlerId, u8 split); +bool32 SideHasMoveSplit(u8 battlerId, u8 split); + +// score increases +void IncreaseStatUpScore(u8 battlerAtk, u8 battlerDef, u8 statId, s16 *score); +void IncreasePoisonScore(u8 battlerAtk, u8 battlerdef, u16 move, s16 *score); +void IncreaseBurnScore(u8 battlerAtk, u8 battlerdef, u16 move, s16 *score); +void IncreaseParalyzeScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score); +void IncreaseSleepScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score); +void IncreaseConfusionScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score); #endif //GUARD_BATTLE_AI_UTIL_H \ No newline at end of file diff --git a/include/constants/battle.h b/include/constants/battle.h index 0bc06b0ad..d836117ea 100644 --- a/include/constants/battle.h +++ b/include/constants/battle.h @@ -220,7 +220,8 @@ #define SIDE_STATUS_CRAFTY_SHIELD (1 << 20) #define SIDE_STATUS_MAT_BLOCK (1 << 21) -#define SIDE_HAZARDS_ANY (SIDE_STATUS_SPIKES | SIDE_STATUS_STICKY_WEB | SIDE_STATUS_TOXIC_SPIKES | SIDE_STATUS_STEALTH_ROCK) +#define SIDE_STATUS_HAZARDS_ANY (SIDE_STATUS_SPIKES | SIDE_STATUS_STICKY_WEB | SIDE_STATUS_TOXIC_SPIKES | SIDE_STATUS_STEALTH_ROCK) +#define SIDE_STATUS_SCREEEN_ANY (SIDE_STATUS_REFLECT | SIDE_STATUS_LIGHTSCREEN | SIDE_STATUS_AURORA_VEIL) // Field affecting statuses. #define STATUS_FIELD_MAGIC_ROOM 0x1 diff --git a/include/constants/battle_ai.h b/include/constants/battle_ai.h index 5e97ab6aa..3dac82bc4 100644 --- a/include/constants/battle_ai.h +++ b/include/constants/battle_ai.h @@ -50,6 +50,8 @@ #define AI_FLAG_HELP_PARTNER (1 << 10) // AI can try to help partner. If not set, will tend not to target partner #define AI_FLAG_WILL_SUICIDE (1 << 11) // AI will use explosion / self destruct / final gambit / etc #define AI_FLAG_PREFER_STATUS_MOVES (1 << 12) // AI gets a score bonus for status moves. Should be combined with AI_FLAG_CHECK_BAD_MOVE to prevent using only status moves +#define AI_FLAG_STALL (1 << 13) // AI stalls battle and prefers secondary damage/trapping/etc. TODO not finished +#define AI_FLAG_SCREENER (1 << 14) // AI prefers screening effects like reflect, mist, etc. TODO unfinished // 'other' ai logic flags #define AI_FLAG_ROAMING (1 << 29) diff --git a/include/item.h b/include/item.h index 87ff57bc7..90a9c2577 100644 --- a/include/item.h +++ b/include/item.h @@ -74,5 +74,6 @@ ItemUseFunc ItemId_GetFieldFunc(u16 itemId); u8 ItemId_GetBattleUsage(u16 itemId); ItemUseFunc ItemId_GetBattleFunc(u16 itemId); u8 ItemId_GetSecondaryId(u16 itemId); +bool32 IsPinchBerryItemEffect(u16 holdEffect); #endif // GUARD_ITEM_H diff --git a/src/battle_ai_script_commands.c b/src/battle_ai_script_commands.c index 2b666a16b..0c1382a51 100644 --- a/src/battle_ai_script_commands.c +++ b/src/battle_ai_script_commands.c @@ -17,6 +17,7 @@ #include "constants/battle_move_effects.h" #include "constants/hold_effects.h" #include "constants/moves.h" +#include "constants/items.h" #define AI_ACTION_DONE 0x0001 #define AI_ACTION_FLEE 0x0002 @@ -182,20 +183,20 @@ EWRAM_DATA u8 sBattler_AI = 0; // const rom data typedef void (*BattleAICmdFunc)(void); -static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); -static u8 AI_TryToFaint(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); -static u8 AI_CheckGoodMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); -static u8 AI_SetupFirstTurn(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); -static u8 AI_Risky(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); -static u8 AI_PreferStrongestMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); -static u8 AI_PreferBatonPass(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); -static u8 AI_DoubleBattle(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); -static u8 AI_HPAware(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); -static u8 AI_Roaming(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); -static u8 AI_Safari(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); -static u8 AI_FirstBattle(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); +static s16 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); +static s16 AI_TryToFaint(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); +static s16 AI_CheckGoodMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); +static s16 AI_SetupFirstTurn(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); +static s16 AI_Risky(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); +static s16 AI_PreferStrongestMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); +static s16 AI_PreferBatonPass(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); +static s16 AI_DoubleBattle(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); +static s16 AI_HPAware(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); +static s16 AI_Roaming(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); +static s16 AI_Safari(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); +static s16 AI_FirstBattle(u8 battlerAtk, u8 battlerDef, u16 move, s16 score); -static u8 (*const sBattleAiFuncTable[])(u8, u8, u16, s16) = +static s16 (*const sBattleAiFuncTable[])(u8, u8, u16, s16) = { [0] = AI_CheckBadMove, // AI_FLAG_CHECK_BAD_MOVE [1] = AI_TryToFaint, // AI_FLAG_TRY_TO_FAINT @@ -507,20 +508,26 @@ static void GetAiLogicData(u8 battlerAtk, u8 battlerDef) { // attacker data AI_DATA->atkAbility = AI_GetAbility(battlerAtk); + AI_DATA->atkItem = gBattleMons[battlerAtk].item; AI_DATA->atkHoldEffect = AI_GetHoldEffect(battlerAtk); AI_DATA->atkParam = GetBattlerHoldEffectParam(battlerAtk); + AI_DATA->atkSpecies = gBattleMons[battlerAtk].species; // target data AI_DATA->defAbility = AI_GetAbility(battlerDef); + AI_DATA->defItem = (AI_GetHoldEffect(battlerDef) == HOLD_EFFECT_NONE) ? ITEM_NONE : gBattleMons[battlerDef].item; AI_DATA->defHoldEffect = AI_GetHoldEffect(battlerDef); AI_DATA->defParam = GetBattlerHoldEffectParam(battlerDef); + AI_DATA->defSpecies = gBattleMons[battlerDef].species; // attacker partner data AI_DATA->battlerAtkPartner = BATTLE_PARTNER(battlerAtk); AI_DATA->partnerMove = GetAllyChosenMove(); AI_DATA->atkPartnerAbility = AI_GetAbility(AI_DATA->battlerAtkPartner); + AI_DATA->atkPartnerHoldEffect = AI_GetHoldEffect(AI_DATA->battlerAtkPartner); AI_DATA->targetSameSide = IsTargetingPartner(battlerAtk, battlerDef); // target partner data AI_DATA->battlerDefPartner = BATTLE_PARTNER(battlerDef); AI_DATA->defPartnerAbility = AI_GetAbility(AI_DATA->battlerDefPartner); + AI_DATA->defPartnerHoldEffect = AI_GetHoldEffect(AI_DATA->battlerDefPartner); } static u8 ChooseMoveOrAction_Singles(void) @@ -557,7 +564,7 @@ static u8 ChooseMoveOrAction_Singles(void) gActiveBattler = sBattler_AI; // If can switch. - if (CountUsablePartyMons(sBattler_AI) >= 1 + if (CountUsablePartyMons(sBattler_AI) > 0 && !IsAbilityPreventingEscape(sBattler_AI) && !(gBattleMons[gActiveBattler].status2 & (STATUS2_WRAPPED | STATUS2_ESCAPE_PREVENTION)) && !(gStatuses3[gActiveBattler] & STATUS3_ROOTED) @@ -1163,7 +1170,7 @@ static void Cmd_if_user_has_attacking_move(void) static void Cmd_if_user_has_no_attacking_moves(void) { - //BattlerHasDamagingMove + //HasDamagingMove } static void Cmd_get_turn_count(void) @@ -1246,7 +1253,7 @@ static void Cmd_if_not_equal_u32(void) static void Cmd_if_user_goes(void) { - // IsBattlerFaster + // IsAiFaster } static void Cmd_nullsub_2A(void) @@ -2292,27 +2299,9 @@ static void Cmd_compare_speeds(void) gAIScriptPtr += 3; } -static u32 FindMoveUsedXTurnsAgo(u32 battlerId, u32 x) -{ - s32 i, index = BATTLE_HISTORY->moveHistoryIndex[battlerId]; - for (i = 0; i < x; i++) - { - if (--index < 0) - index = AI_MOVE_HISTORY_COUNT - 1; - } - return BATTLE_HISTORY->moveHistory[battlerId][index]; -} - static void Cmd_is_wakeup_turn(void) { - u32 battler = BattleAI_GetWantedBattler(gAIScriptPtr[1]); - // Check if rest was used 2 turns ago - if ((gBattleMons[battler].status1 & STATUS1_SLEEP) == 1 && FindMoveUsedXTurnsAgo(battler, 2) == MOVE_REST) - AI_THINKING_STRUCT->funcResult = TRUE; - else - AI_THINKING_STRUCT->funcResult = FALSE; - - gAIScriptPtr += 2; + //IsWakeupTurn } static void Cmd_if_has_move_with_accuracy_lt(void) @@ -2341,7 +2330,7 @@ static void Cmd_if_has_move_with_accuracy_lt(void) // AI Functions -static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) +static s16 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) { // move data u8 atkPriority = GetMovePriority(battlerAtk, move); @@ -2350,6 +2339,9 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) u8 moveTarget = gBattleMoves[move].target; u16 accuracy = AI_GetMoveAccuracy(battlerAtk, battlerDef, AI_DATA->atkAbility, AI_DATA->defAbility, AI_DATA->atkHoldEffect, AI_DATA->defHoldEffect, move); u8 effectiveness = AI_GetMoveEffectiveness(move); + bool32 isDoubleBattle = IsValidDoubleBattle(battlerAtk); + u32 i; + u16 predictedMove = gLastMoves[battlerDef]; // TODO better move prediction if (!(AI_THINKING_STRUCT->aiFlags & AI_FLAG_HELP_PARTNER) && AI_DATA->targetSameSide) return score; // don't consider ally presence @@ -2360,9 +2352,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) // check powder moves if (TestMoveFlags(move, FLAG_POWDER)) { - if ((B_POWDER_GRASS >= GEN_6 && IS_BATTLER_OF_TYPE(gBattlerTarget, TYPE_GRASS)) - || AI_DATA->defAbility == ABILITY_OVERCOAT - || GetBattlerHoldEffect(gBattlerTarget, TRUE) == HOLD_EFFECT_SAFETY_GOOGLES) + if (!IsAffectedByPowder(battlerDef, AI_DATA->defAbility, AI_DATA->defHoldEffect)) score -= 10; } @@ -2499,7 +2489,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } // def ability checks // target partner ability checks - if (IsValidDoubleBattle(battlerAtk) && !IsTargetingPartner(battlerAtk, battlerDef)) + if (isDoubleBattle && !IsTargetingPartner(battlerAtk, battlerDef)) { switch (AI_DATA->defPartnerAbility) { @@ -2599,15 +2589,15 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) { score -= 10; } - else if (CountUsablePartyMons(battlerDef) == 1 && CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex)) + else if (CountUsablePartyMons(battlerDef) == 0 && CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) { ; // target has 1 pkmn left but you can faint it -> good to use } - else if (IsValidDoubleBattle(battlerAtk)) + else if (isDoubleBattle) { - if (CountUsablePartyMons(battlerDef) == 2 - && CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex) - && CanAttackerFaintTarget(AI_DATA->battlerAtkPartner, BATTLE_PARTNER(battlerDef), *(gBattleStruct->chosenMovePositions + AI_DATA->battlerAtkPartner))) + if (CountUsablePartyMons(battlerDef) != 0 + && CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0) + && CanAttackerFaintTarget(AI_DATA->battlerAtkPartner, BATTLE_PARTNER(battlerDef), *(gBattleStruct->chosenMovePositions + AI_DATA->battlerAtkPartner), 0)) { ; // good } @@ -2618,9 +2608,9 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } else { - if (CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex)) + if (CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) { - if (CountUsablePartyMons(battlerDef) == 1) + if (CountUsablePartyMons(battlerDef) == 0) { ; //Good to use move } @@ -2641,20 +2631,17 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) score -= 10; break; case EFFECT_COPYCAT: - //TODO - predict def move - break; case EFFECT_MIRROR_MOVE: - //TODO - predict def move - break; + return AI_CheckBadMove(battlerAtk, battlerDef, predictedMove, score); case EFFECT_TELEPORT: if (gBattleTypeFlags & BATTLE_TYPE_TRAINER) { - if (CountUsablePartyMons(battlerAtk) == 1) + if (CountUsablePartyMons(battlerAtk) == 0) score -= 10; } else if (GetBattlerSide(battlerAtk) == B_SIDE_OPPONENT) { - if (IsValidDoubleBattle(battlerAtk) || IsBattlerTrapped(battlerAtk, FALSE)) + if (isDoubleBattle || IsBattlerTrapped(battlerAtk, FALSE)) score -= 10; } break; @@ -2701,7 +2688,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) break; case EFFECT_FLOWER_SHIELD: if (!IS_BATTLER_OF_TYPE(battlerAtk, TYPE_GRASS) - && !(IsValidDoubleBattle(battlerAtk) && IS_BATTLER_OF_TYPE(AI_DATA->battlerAtkPartner, TYPE_GRASS))) + && !(isDoubleBattle && IS_BATTLER_OF_TYPE(AI_DATA->battlerAtkPartner, TYPE_GRASS))) score -= 10; break; case EFFECT_MAGNETIC_FLUX: @@ -2711,12 +2698,12 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) && gBattleMons[battlerAtk].statStages[STAT_SPDEF] >= MAX_STAT_STAGE) score -= 10; } - else if (!IsValidDoubleBattle(battlerAtk)) + else if (!isDoubleBattle) { score -= 10; // our stats wont rise from this move } - if (IsValidDoubleBattle(battlerAtk)) + if (isDoubleBattle) { if (AI_DATA->atkPartnerAbility == ABILITY_PLUS || AI_DATA->atkPartnerAbility == ABILITY_MINUS) { @@ -2731,7 +2718,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } break; case EFFECT_AROMATIC_MIST: - if (!IsValidDoubleBattle(battlerAtk) || gBattleMons[AI_DATA->battlerAtkPartner].hp == 0 || !BattlerStatCanRise(AI_DATA->battlerAtkPartner, AI_DATA->atkPartnerAbility, STAT_SPDEF)) + if (!isDoubleBattle || gBattleMons[AI_DATA->battlerAtkPartner].hp == 0 || !BattlerStatCanRise(AI_DATA->battlerAtkPartner, AI_DATA->atkPartnerAbility, STAT_SPDEF)) score -= 10; break; case EFFECT_SPEED_UP: @@ -2752,7 +2739,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) score -= 10; break; case EFFECT_ROTOTILLER: - if (IsValidDoubleBattle(battlerAtk)) + if (isDoubleBattle) { if (!(IS_BATTLER_OF_TYPE(battlerAtk, TYPE_GRASS) && AI_IsBattlerGrounded(battlerAtk) @@ -2781,12 +2768,12 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) if (!BattlerShouldRaiseAttacks(battlerAtk, AI_DATA->atkAbility)) score -= 10; } - else if (!IsValidDoubleBattle(battlerAtk)) + else if (!isDoubleBattle) { score -= 10; // no partner and our stats wont rise, so don't use } - if (IsValidDoubleBattle(battlerAtk)) + if (isDoubleBattle) { if (AI_DATA->atkPartnerAbility == ABILITY_PLUS || AI_DATA->atkPartnerAbility == ABILITY_MINUS) { @@ -2904,7 +2891,6 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } else { - u32 i; for (i = STAT_ATK; i < NUM_BATTLE_STATS; i++) { if (gBattleMons[battlerAtk].statStages[i] > DEFAULT_STAT_STAGE || gBattleMons[AI_DATA->battlerAtkPartner].statStages[i] > DEFAULT_STAT_STAGE) @@ -2918,7 +2904,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } break; case EFFECT_BIDE: - if (!BattlerHasDamagingMove(battlerDef) + if (!HasDamagingMove(battlerDef) || GetHealthPercentage(battlerAtk) < 30 //Close to death || gBattleMons[battlerDef].status1 & (STATUS1_SLEEP | STATUS1_FREEZE)) //No point in biding if can't take damage score -= 10; @@ -2928,12 +2914,12 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) score -= 10; // don't scare away pokemon twice //Don't blow out a Pokemon that'll faint soon or is taking a a lot of secondary damage - if (GetHealthPercentage(battlerDef) < 10 && BattlerHasSecondaryDamage(battlerDef)) + if (GetHealthPercentage(battlerDef) < 10 && GetBattlerSecondaryDamage(battlerDef)) score -= 10; else if (gStatuses3[battlerDef] & STATUS3_PERISH_SONG) score -= 10; - if (CountUsablePartyMons(battlerDef) == 1 + if (CountUsablePartyMons(battlerDef) == 0 || AI_DATA->defAbility == ABILITY_SUCTION_CUPS || gStatuses3[battlerDef] & STATUS3_ROOTED) score -= 10; @@ -2941,7 +2927,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) case EFFECT_HIT_SWITCH_TARGET: if (DoesPartnerHaveSameMoveEffect(AI_DATA->battlerAtkPartner, battlerDef, move, AI_DATA->partnerMove)) score -= 10; // don't scare away pokemon twice - else if (GetHealthPercentage(battlerDef) < 10 && BattlerHasSecondaryDamage(battlerDef)) + else if (GetHealthPercentage(battlerDef) < 10 && GetBattlerSecondaryDamage(battlerDef)) score -= 10; // don't blow away mon that will faint soon else if (gStatuses3[battlerDef] & STATUS3_PERISH_SONG) score -= 10; @@ -2951,10 +2937,24 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) if (IS_BATTLER_OF_TYPE(battlerAtk, gBattleMoves[gBattleMons[battlerAtk].moves[0]].type)) score -= 10; break; - case EFFECT_RESTORE_HP: case EFFECT_REST: + if (!CanSleep(battlerAtk, AI_DATA->atkAbility)) + score -= 10; + //fallthrough + case EFFECT_RESTORE_HP: + case EFFECT_SOFTBOILED: + case EFFECT_ROOST: + if (AtMaxHp(battlerAtk)) + score -= 10; + else if (GetHealthPercentage(battlerAtk) >= 90) + score -= 9; //No point in healing, but should at least do it if nothing better + break; case EFFECT_MORNING_SUN: - if (GetHealthPercentage(battlerAtk) == 100) + case EFFECT_SYNTHESIS: + case EFFECT_MOONLIGHT: + if (AI_WeatherHasEffect() && (gBattleWeather & (WEATHER_RAIN_ANY | WEATHER_SANDSTORM_ANY | WEATHER_HAIL_ANY))) + score -= 3; + else if (AtMaxHp(battlerAtk)) score -= 10; else if (GetHealthPercentage(battlerAtk) >= 90) score -= 9; //No point in healing, but should at least do it if nothing better @@ -2964,7 +2964,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) score -= 10; else if (battlerDef == AI_DATA->battlerAtkPartner) break; //Always heal your ally - else if (GetHealthPercentage(battlerAtk) == 100) + else if (AtMaxHp(battlerAtk)) score -= 10; else if (GetHealthPercentage(battlerAtk) >= 90) score -= 8; //No point in healing, but should at least do it if nothing better @@ -3008,6 +3008,10 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) if (!ShouldTryOHKO(battlerAtk, battlerDef, AI_DATA->atkAbility, AI_DATA->defAbility, accuracy, move)) score -= 10; break; + case EFFECT_SUPER_FANG: + if (GetHealthPercentage(battlerDef) < 50) + score -= 4; + break; case EFFECT_RECOIL_IF_MISS: if (AI_DATA->atkAbility != ABILITY_MAGIC_GUARD && accuracy < 75) score -= 6; @@ -3074,7 +3078,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) case EFFECT_PARALYZE: if (move != MOVE_GLARE && gMoveResultFlags & MOVE_RESULT_NO_EFFECT) score -= 10; - else if (AI_CanParalyze(battlerAtk, battlerDef, AI_DATA->defAbility, move, AI_DATA->partnerMove)) + else if (!AI_CanParalyze(battlerAtk, battlerDef, AI_DATA->defAbility, move, AI_DATA->partnerMove)) score -= 10; break; case EFFECT_TWO_TURNS_ATTACK: @@ -3091,7 +3095,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) break; case EFFECT_RECHARGE: if (AI_DATA->atkAbility != ABILITY_TRUANT - && !CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex)) + && !CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) score -= 2; break; case EFFECT_SPITE: @@ -3102,7 +3106,11 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) || gLastMoves[battlerDef] == 0xFFFF) score -= 10; } - // TODO - if no predicted move, decrease viability + else if (predictedMove == MOVE_NONE) + { + // TODO predicted move separate from gLastMoves + score -= 10; + } break; case EFFECT_METRONOME: break; @@ -3123,7 +3131,10 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) if (gLastMoves[battlerDef] == MOVE_NONE || gLastMoves[battlerDef] == 0xFFFF) score -= 10; } - //TODO - if predictive move = MOVE_NONE, score -= 10 + else if (predictedMove == MOVE_NONE) + { + score -= 10; + } } else { @@ -3132,11 +3143,11 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) break; case EFFECT_COUNTER: case EFFECT_MIRROR_COAT: - //TODO - predicted move - /*if (GetBattleMoveSplit(predictedMove) == SPLIT_STATUS - || predictedMove == MOVE_NONE - || DoesSubstituteBlockMove(battlerAtk, AI_DATA->battlerDefPartner, predictedMove) - score -= 10;*/ + if (IsBattlerIncapacitated(battlerDef, AI_DATA->defAbility) || gBattleMons[battlerDef].status2 & (STATUS2_INFATUATION | STATUS2_CONFUSION)) + score--; + if (predictedMove == MOVE_NONE || GetBattleMoveSplit(predictedMove) == SPLIT_STATUS + || DoesSubstituteBlockMove(battlerAtk, AI_DATA->battlerDefPartner, predictedMove)) + score -= 10; break; case EFFECT_ENCORE: if (gDisableStructs[battlerDef].encoreTimer == 0 @@ -3149,9 +3160,10 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) || gLastMoves[battlerDef] == 0xFFFF) score -= 10; } - //TODO predicted moves - //else if (predictedMove == MOVE_NONE) - //score -= 10; + else if (predictedMove == MOVE_NONE) + { + score -= 10; + } } else { @@ -3165,11 +3177,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) break; case EFFECT_SNORE: case EFFECT_SLEEP_TALK: - // AI shouldn't really know if its about to wake up since its random - /*if (((gBattleMons[battlerAtk].status1 & STATUS1_SLEEP) == 1 || !(gBattleMons[battlerAtk].status1 & STATUS1_SLEEP)) - && AI_DATA->atkAbility != ABILITY_COMATOSE) - score -= 10;*/ - if (!(gBattleMons[battlerAtk].status1 & STATUS1_SLEEP) && AI_DATA->atkAbility != ABILITY_COMATOSE) + if (IsWakeupTurn(battlerAtk) || (!(gBattleMons[battlerAtk].status1 & STATUS1_SLEEP) && AI_DATA->atkAbility != ABILITY_COMATOSE)) score -= 10; break; case EFFECT_CONVERSION_2: @@ -3234,7 +3242,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } break; case EFFECT_ENDURE: - if (gBattleMons[battlerAtk].hp == 1 || BattlerHasSecondaryDamage(battlerAtk)) //Don't use Endure if you'll die after using it + if (gBattleMons[battlerAtk].hp == 1 || GetBattlerSecondaryDamage(battlerAtk)) //Don't use Endure if you'll die after using it score -= 10; break; case EFFECT_PROTECT: @@ -3245,7 +3253,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) case MOVE_QUICK_GUARD: case MOVE_WIDE_GUARD: case MOVE_CRAFTY_SHIELD: - if (!IsValidDoubleBattle(battlerAtk)) + if (!isDoubleBattle) { score -= 10; decreased = TRUE; @@ -3273,7 +3281,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) && move != MOVE_WIDE_GUARD && move != MOVE_CRAFTY_SHIELD) //These moves have infinite usage { - if (BattlerHasSecondaryDamage(battlerAtk) + if (GetBattlerSecondaryDamage(battlerAtk) && AI_DATA->defAbility != ABILITY_MOXIE && AI_DATA->defAbility != ABILITY_BEAST_BOOST) { @@ -3281,7 +3289,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } else if (gDisableStructs[battlerAtk].protectUses == 1 && Random() % 100 < 50) { - if (!IsValidDoubleBattle(battlerAtk)) + if (!isDoubleBattle) score -= 6; else score -= 10; //Don't try double protecting in doubles @@ -3349,12 +3357,12 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) score -= 9; break; case EFFECT_PERISH_SONG: - if (IsValidDoubleBattle(battlerAtk)) + if (isDoubleBattle) { - if (CountUsablePartyMons(battlerAtk) <= 2 + if (CountUsablePartyMons(battlerAtk) == 0 && AI_DATA->atkAbility != ABILITY_SOUNDPROOF && AI_DATA->atkPartnerAbility != ABILITY_SOUNDPROOF - && CountUsablePartyMons(FOE(battlerAtk)) >= 3) + && CountUsablePartyMons(FOE(battlerAtk)) >= 1) { score -= 10; //Don't wipe your team if you're going to lose } @@ -3370,8 +3378,8 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } else { - if (CountUsablePartyMons(battlerAtk) == 1 && AI_DATA->atkAbility != ABILITY_SOUNDPROOF - && CountUsablePartyMons(battlerDef) >= 2) + if (CountUsablePartyMons(battlerAtk) == 0 && AI_DATA->atkAbility != ABILITY_SOUNDPROOF + && CountUsablePartyMons(battlerDef) >= 1) score -= 10; if (gStatuses3[FOE(battlerAtk)] & STATUS3_PERISH_SONG || AI_GetAbility(FOE(battlerAtk)) == ABILITY_SOUNDPROOF) @@ -3423,7 +3431,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) score -= 10; break; case EFFECT_PARTING_SHOT: - if (CountUsablePartyMons(battlerAtk) == 1) + if (CountUsablePartyMons(battlerAtk) == 0) score -= 10; break; case EFFECT_BATON_PASS: @@ -3441,14 +3449,14 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) break; // check damage/accuracy //Spin checks - if (!(gSideStatuses[GetBattlerSide(battlerAtk)] & SIDE_HAZARDS_ANY)) + if (!(gSideStatuses[GetBattlerSide(battlerAtk)] & SIDE_STATUS_HAZARDS_ANY)) score -= 6; break; case EFFECT_DEFOG: if (gSideStatuses[GetBattlerSide(battlerDef)] & (SIDE_STATUS_REFLECT | SIDE_STATUS_LIGHTSCREEN | SIDE_STATUS_AURORA_VEIL | SIDE_STATUS_SAFEGUARD | SIDE_STATUS_MIST) || gSideTimers[GetBattlerSide(battlerDef)].auroraVeilTimer != 0 - || gSideStatuses[GetBattlerSide(battlerAtk)] & SIDE_HAZARDS_ANY) + || gSideStatuses[GetBattlerSide(battlerAtk)] & SIDE_STATUS_HAZARDS_ANY) { if (PartnerHasSameMoveEffectWithoutTarget(AI_DATA->battlerAtkPartner, move, AI_DATA->partnerMove)) { @@ -3457,13 +3465,13 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } } - if (gSideStatuses[GetBattlerSide(battlerDef)] & SIDE_HAZARDS_ANY) + if (gSideStatuses[GetBattlerSide(battlerDef)] & SIDE_STATUS_HAZARDS_ANY) { score -= 10; //Don't blow away opposing hazards break; } - if (IsValidDoubleBattle(battlerAtk)) + if (isDoubleBattle) { if (IsHazardMoveEffect(gBattleMoves[AI_DATA->partnerMove].effect) // partner is going to set up hazards && GetWhoStrikesFirst(AI_DATA->battlerAtkPartner, battlerAtk, FALSE)) // partner is going to set up before the potential Defog @@ -3486,7 +3494,6 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) break; case EFFECT_PSYCH_UP: // haze stats check { - u32 i; for (i = STAT_ATK; i < NUM_BATTLE_STATS; i++) { if (gBattleMons[battlerAtk].statStages[i] > DEFAULT_STAT_STAGE || gBattleMons[AI_DATA->battlerAtkPartner].statStages[i] > DEFAULT_STAT_STAGE) @@ -3513,11 +3520,10 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) score -= 4; break; case EFFECT_SEMI_INVULNERABLE: - //TODO - predicted moves - /*if (predictedMove != MOVE_NONE - && MoveWouldHitFirst(move, battlerAtk, battlerDef) + if (predictedMove != MOVE_NONE + && GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1 && gBattleMoves[predictedMove].effect == EFFECT_SEMI_INVULNERABLE) - score -= 10;*/ // Don't Fly if opponent is going to fly after you + score -= 10; // Don't Fly/dig/etc if opponent is going to fly/dig/etc after you if (BattlerWillFaintFromWeather(battlerAtk, AI_DATA->atkAbility) && (move == MOVE_FLY || move == MOVE_BOUNCE)) @@ -3532,9 +3538,9 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) else if (move == MOVE_FAKE_OUT) { if ((AI_DATA->atkHoldEffect == HOLD_EFFECT_CHOICE_BAND || AI_DATA->atkAbility == ABILITY_GORILLA_TACTICS) - && (CountUsablePartyMons(battlerDef) >= 2 || !CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex))) + && (CountUsablePartyMons(battlerDef) > 0 || !CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0))) { - if (CountUsablePartyMons(battlerAtk) == 1) + if (CountUsablePartyMons(battlerAtk) == 0) score -= 10; // Don't lock the attacker into Fake Out if they can't switch out afterwards. } } @@ -3556,7 +3562,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } else { - if (GetHealthPercentage(battlerAtk) == 100) + if (AtMaxHp(battlerAtk)) score -= 10; else if (GetHealthPercentage(battlerAtk) >= 80) score -= 5; // do it if nothing better @@ -3584,32 +3590,28 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) score -= 10; break; case EFFECT_MEMENTO: - if (CountUsablePartyMons(battlerAtk) == 1 || DoesPartnerHaveSameMoveEffect(AI_DATA->battlerAtkPartner, battlerDef, move, AI_DATA->partnerMove)) + if (CountUsablePartyMons(battlerAtk) == 0 || DoesPartnerHaveSameMoveEffect(AI_DATA->battlerAtkPartner, battlerDef, move, AI_DATA->partnerMove)) score -= 10; else if (gBattleMons[battlerDef].statStages[STAT_ATK] == MIN_STAT_STAGE && gBattleMons[battlerDef].statStages[STAT_SPATK] == MIN_STAT_STAGE) score -= 10; break; case EFFECT_HEALING_WISH: //healing wish, lunar dance - if (CountUsablePartyMons(battlerAtk) == 1 || DoesPartnerHaveSameMoveEffect(AI_DATA->battlerAtkPartner, battlerDef, move, AI_DATA->partnerMove)) + if (CountUsablePartyMons(battlerAtk) == 0 || DoesPartnerHaveSameMoveEffect(AI_DATA->battlerAtkPartner, battlerDef, move, AI_DATA->partnerMove)) score -= 10; else if (IsPartyFullyHealedExceptBattler(battlerAtk)) score -= 10; break; case EFFECT_FINAL_GAMBIT: - if (CountUsablePartyMons(battlerAtk) == 1 || DoesPartnerHaveSameMoveEffect(AI_DATA->battlerAtkPartner, battlerDef, move, AI_DATA->partnerMove)) + if (CountUsablePartyMons(battlerAtk) == 0 || DoesPartnerHaveSameMoveEffect(AI_DATA->battlerAtkPartner, battlerDef, move, AI_DATA->partnerMove)) score -= 10; break; case EFFECT_FOCUS_PUNCH: - //TODO predicted moves - /*if (predictedMove != MOVE_NONE - && !DoesSubstituteBlockMove(battlerAtk, battlerDef, predictedMove) - && SPLIT(predictedMove) != SPLIT_STATUS + if (predictedMove != MOVE_NONE + && !DoesSubstituteBlockMove(battlerAtk, battlerDef, move) + && !IS_MOVE_STATUS(predictedMove) && gBattleMoves[predictedMove].power != 0) - score -= 10;*/ //Probably better not to use it + score -= 10; // Probably better not to use it break; - //TODO - // case EFFECT_SHELL_TRAP: - // case EFFECT_BEAK_BLAST: case EFFECT_NATURE_POWER: return AI_CheckBadMove(battlerAtk, battlerDef, GetNaturePowerMove(), score); case EFFECT_CHARGE: @@ -3631,7 +3633,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) break; case EFFECT_FOLLOW_ME: case EFFECT_HELPING_HAND: - if (!IsValidDoubleBattle(battlerAtk) + if (!isDoubleBattle || !IsBattlerAlive(AI_DATA->battlerAtkPartner) || PartnerHasSameMoveEffectWithoutTarget(AI_DATA->battlerAtkPartner, move, AI_DATA->partnerMove) || (AI_DATA->partnerMove != MOVE_NONE && IS_MOVE_STATUS(AI_DATA->partnerMove)) @@ -3658,13 +3660,15 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) || IsRolePlayBannedAbilityAtk(AI_DATA->atkAbility) || IsRolePlayBannedAbility(AI_DATA->defAbility)) score -= 10; + else if (IsAbilityOfRating(AI_DATA->atkAbility, 5)) + score -= 4; break; case EFFECT_WISH: if (gWishFutureKnock.wishCounter[battlerAtk] != 0) score -= 10; break; case EFFECT_ASSIST: - if (CountUsablePartyMons(battlerAtk) == 1) + if (CountUsablePartyMons(battlerAtk) == 0) score -= 10; // no teammates to assist from break; case EFFECT_INGRAIN: @@ -3680,6 +3684,8 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) if (move == MOVE_HYPERSPACE_FURY && gBattleMons[battlerAtk].species != SPECIES_HOOPA_UNBOUND) score -= 10; #endif + if (effectiveness <= AI_EFFECTIVENESS_x0_5) + score -= 2; // really don't waste the stat loss on a weak attack break; case EFFECT_MAGIC_COAT: if (!TestMoveFlagsInMoveset(battlerDef, FLAG_MAGICCOAT_AFFECTED)) @@ -3787,19 +3793,6 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) && gBattleMons[battlerAtk].statStages[STAT_SPDEF] >= MAX_STAT_STAGE) score -= 10; break; - // TODO - /*case EFFECT_NO_RETREAT: - if (TrappedByNoRetreat(battlerAtk)) - score -= 10; - break; - case EFFECT_EXTREME_EVOBOOST: - if (MainStatsMaxed(battlerAtk)) - score -= 10; - break; - case EFFECT_CLANGOROUS_SOUL: - if (gBattleMons[battlerAtk].hp <= gBattleMons[battlerAtk].maxHP / 3) - score -= 10; - break;*/ case EFFECT_BULK_UP: if (AI_DATA->atkAbility == ABILITY_CONTRARY) score -= 10; @@ -3940,16 +3933,17 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } break; case EFFECT_ME_FIRST: - //TODO - predicted move - /*if (predictedMove != MOVE_NONE) + if (predictedMove != MOVE_NONE) { - if (!MoveWouldHitFirst(move, battlerAtk, battlerDef)) - score -= 10; + if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1) + score -= 10; // Target is predicted to go first, Me First will fail else - return AIScript_Negatives(battlerAtk, battlerDef, predictedMove, originalViability, data); + return AI_CheckBadMove(battlerAtk, battlerDef, predictedMove, score); + } + else + { + score -= 10; //Target is predicted to switch most likely } - else //Target is predicted to switch most likely - score -= 10;*/ break; case EFFECT_NATURAL_GIFT: if (AI_DATA->atkAbility == ABILITY_KLUTZ @@ -3974,7 +3968,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) score -= 10; break; case EFFECT_PLEDGE: - if (IsValidDoubleBattle(battlerAtk) && gBattleMons[AI_DATA->battlerAtkPartner].hp > 0) + if (isDoubleBattle && gBattleMons[AI_DATA->battlerAtkPartner].hp > 0) { if (AI_DATA->partnerMove != MOVE_NONE && gBattleMoves[AI_DATA->partnerMove].effect == EFFECT_PLEDGE @@ -4022,15 +4016,14 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) || PartnerMoveIsSameNoTarget(AI_DATA->battlerAtkPartner, move, AI_DATA->partnerMove)) score -= 10; break; - //TODO - //case EFFECT_PLASMA_FISTS: - //break; case EFFECT_FLING: if (!CanFling(battlerAtk)) + { score -= 10; + } else { - /* TODO + /* TODO Fling u8 effect = gFlingTable[gBattleMons[battlerAtk].item].effect; switch (effect) { @@ -4108,7 +4101,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) case EFFECT_HIT_ENEMY_HEAL_ALLY: // pollen puff if (AI_DATA->targetSameSide) { - if (GetHealthPercentage(battlerDef) == 100) + if (AtMaxHp(battlerDef)) score -= 10; else if (gBattleMons[battlerDef].hp > gBattleMons[battlerDef].maxHP / 2) score -= 5; @@ -4122,7 +4115,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } else { - if (GetHealthPercentage(battlerDef) == 100) + if (AtMaxHp(battlerDef)) score -= 10; else if (gBattleMons[battlerDef].hp > gBattleMons[battlerDef].maxHP / 2) score -= 5; @@ -4159,7 +4152,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) { u16 instructedMove; if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1) - instructedMove = MOVE_NONE; //TODO instructedMove = predictedMove; + instructedMove = predictedMove; else instructedMove = gLastMoves[battlerDef]; @@ -4176,7 +4169,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) { score -= 10; } - else if (IsValidDoubleBattle(battlerAtk)) + else if (isDoubleBattle) { if (!AI_DATA->targetSameSide) score -= 10; @@ -4197,29 +4190,24 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } break; case EFFECT_QUASH: - if (!IsValidDoubleBattle(battlerAtk) + if (!isDoubleBattle || GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1 || PartnerMoveIsSameAsAttacker(AI_DATA->battlerAtkPartner, battlerDef, move, AI_DATA->partnerMove)) score -= 10; break; case EFFECT_AFTER_YOU: if (!AI_DATA->targetSameSide - || !IsValidDoubleBattle(battlerAtk) + || !isDoubleBattle || GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1 || PartnerMoveIsSameAsAttacker(AI_DATA->battlerAtkPartner, battlerDef, move, AI_DATA->partnerMove)) score -= 10; break; case EFFECT_SUCKER_PUNCH: - /*TODO predicted move if (predictedMove != MOVE_NONE) { - if (IS_MOVE_STATUS(predictedMove) - || GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1 - { + if (IS_MOVE_STATUS(predictedMove) || GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1) // opponent going first score -= 10; - break; - } - }*/ + } break; case EFFECT_TAILWIND: if (gSideTimers[GetBattlerSide(battlerAtk)].tailwindTimer != 0 @@ -4248,16 +4236,6 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) if (!CanUseLastResort(battlerAtk)) score -= 10; break; - //TODO - /*case EFFECT_SKY_DROP: - if (IS_BATTLER_OF_TYPE(battlerDef, TYPE_FLYING)) - score -= 10; - if (WillFaintFromWeather(battlerAtk) - || MoveBlockedBySubstitute(move, battlerAtk, battlerDef) - || GetSpeciesWeight(gBattleMons[battlerDef].species, AI_DATA->defAbility, AI_DATA->defHoldEffect, battlerDef, TRUE) >= 2000) //200.0 kg - score -= 10; - break; - */ case EFFECT_SYNCHRONOISE: //Check holding ring target or is of same type if (AI_DATA->defHoldEffect == HOLD_EFFECT_RING_TARGET @@ -4268,7 +4246,49 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) else score -= 10; break; - + case EFFECT_ERUPTION: + if (effectiveness <= AI_EFFECTIVENESS_x0_5) + score--; + if (GetHealthPercentage(battlerDef) < 50) + score--; + break; + case EFFECT_VITAL_THROW: + if (IsAiFaster(AI_CHECK_FASTER) && GetHealthPercentage(battlerAtk) < 40) + score--; // don't want to move last + break; + case EFFECT_FLAIL: + if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1 // opponent should go first + || GetHealthPercentage(battlerAtk) > 50) + score -= 4; + break; + //TODO + //case EFFECT_PLASMA_FISTS: + //break; + //case EFFECT_SHELL_TRAP: + //break; + //case EFFECT_BEAK_BLAST: + //break; + /*case EFFECT_SKY_DROP: + if (IS_BATTLER_OF_TYPE(battlerDef, TYPE_FLYING)) + score -= 10; + if (WillFaintFromWeather(battlerAtk) + || MoveBlockedBySubstitute(move, battlerAtk, battlerDef) + || GetSpeciesWeight(gBattleMons[battlerDef].species, AI_DATA->defAbility, AI_DATA->defHoldEffect, battlerDef, TRUE) >= 2000) //200.0 kg + score -= 10; + break; + */ + /*case EFFECT_NO_RETREAT: + if (TrappedByNoRetreat(battlerAtk)) + score -= 10; + break; + case EFFECT_EXTREME_EVOBOOST: + if (MainStatsMaxed(battlerAtk)) + score -= 10; + break; + case EFFECT_CLANGOROUS_SOUL: + if (gBattleMons[battlerAtk].hp <= gBattleMons[battlerAtk].maxHP / 3) + score -= 10; + break;*/ } // move effect checks // substitute check @@ -4286,7 +4306,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } // helping hand check - if (IsValidDoubleBattle(battlerAtk) && AI_DATA->partnerMove != MOVE_NONE + if (isDoubleBattle && AI_DATA->partnerMove != MOVE_NONE && gBattleMoves[AI_DATA->partnerMove].effect == EFFECT_HELPING_HAND && IS_MOVE_STATUS(move)) score -= 10; //Don't use a status move if partner wants to help @@ -4294,7 +4314,7 @@ static u8 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) return score; } -static u8 AI_TryToFaint(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) +static s16 AI_TryToFaint(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) { s32 dmg; u8 result; @@ -4305,10 +4325,10 @@ static u8 AI_TryToFaint(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) if (gBattleMoves[AI_THINKING_STRUCT->moveConsidered].power == 0) return score; // can't make anything faint with no power - if (CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex) && gBattleMoves[move].effect != EFFECT_EXPLOSION) + if (CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0) && gBattleMoves[move].effect != EFFECT_EXPLOSION) { // AI_TryToFaint_Can - if (IsBattlerFaster(AI_CHECK_FASTER) || TestMoveFlags(move, FLAG_HIGH_CRIT)) + if (IsAiFaster(AI_CHECK_FASTER) || TestMoveFlags(move, FLAG_HIGH_CRIT)) score += 4; else score += 2; @@ -4327,7 +4347,7 @@ static u8 AI_TryToFaint(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } //AI_TryToFaint_CheckIfDanger - if (!IsBattlerFaster(AI_CHECK_FASTER) && CanTargetFaintAi(battlerDef, battlerAtk)) + if (!IsAiFaster(AI_CHECK_FASTER) && CanTargetFaintAi(battlerDef, battlerAtk)) { // AI_TryToFaint_Danger if (GetMoveDamageResult(move) != MOVE_POWER_BEST) score--; @@ -4338,21 +4358,24 @@ static u8 AI_TryToFaint(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) return score; } -static u8 AI_CheckPartner(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) +static s16 AI_CheckPartner(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) { //TODO return score; } -static u8 AI_CheckGoodMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) +static s16 AI_CheckGoodMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) { // move data u16 moveEffect = gBattleMoves[move].effect; u8 effectiveness = AI_GetMoveEffectiveness(move); u8 atkPriority = GetMovePriority(battlerAtk, move); + u16 predictedMove = gLastMoves[battlerDef]; //for now + bool32 isDoubleBattle = IsValidDoubleBattle(battlerAtk); + u32 i; // targeting partner, check benefits of doing that instead - if (IsValidDoubleBattle(battlerAtk) && AI_DATA->targetSameSide) + if (isDoubleBattle && AI_DATA->targetSameSide) return AI_CheckPartner(battlerAtk, AI_DATA->battlerAtkPartner, move, score); // check move results @@ -4362,29 +4385,23 @@ static u8 AI_CheckGoodMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) // if target goes first and can kill us, lets try to use a priority move to at least do something if ((gBattleMons[battlerDef].status2 & (STATUS2_RECHARGE | STATUS2_BIDE)) && CanTargetFaintAi(battlerAtk, battlerDef) - && IsBattlerFaster(AI_CHECK_SLOWER) + && IsAiFaster(AI_CHECK_SLOWER) && atkPriority > 0) score += 5; - // if target is evasive and this move damages/always hits, use it + // if target is evasive (or we have low accuracy)) and this move always hits, boost its score if (!IS_MOVE_STATUS(move) && gBattleMoves[move].accuracy == 0) { - if (gBattleMons[battlerDef].statStages[STAT_EVASION] >= 10) + if (gBattleMons[battlerDef].statStages[STAT_EVASION] >= 8) score++; - else if (gBattleMons[battlerAtk].statStages[STAT_ACC] <= 3) - score++; - else if (gBattleMons[battlerDef].statStages[STAT_EVASION] >= 7 && (Random() % 256) < 100) - score++; - else if (gBattleMons[battlerAtk].statStages[STAT_ACC] <= 4 && (Random() % 256 < 100)) + else if (gBattleMons[battlerAtk].statStages[STAT_ACC] <= 4) score++; } // prefer good damaging moves if (GetMoveDamageResult(move) == MOVE_POWER_BEST) score += 2; - else if (GetMoveDamageResult(move) == MOVE_POWER_GOOD && (Random() % 256) < 100) - score++; - + // check status move preference if (AI_THINKING_STRUCT->aiFlags & AI_FLAG_PREFER_STATUS_MOVES && IS_MOVE_STATUS(move)) score++; @@ -4393,26 +4410,42 @@ static u8 AI_CheckGoodMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) if (TestMoveFlags(move, FLAG_HIGH_CRIT) && effectiveness >= AI_EFFECTIVENESS_x2) score++; + // check thawing moves + if ((gBattleMons[battlerAtk].status1 & STATUS1_FREEZE) && IsThawingMove(move)) + score += (gBattleTypeFlags & BATTLE_TYPE_DOUBLE) ? 20 : 10; + + // ability checks + switch (AI_DATA->atkAbility) + { + case ABILITY_MOXIE: + case ABILITY_BEAST_BOOST: + if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0) // attacker should go first + { + if (CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) + score += 8; // prioritize killing target for stat boost + } + break; + } // ability checks + + // move effect checks switch (moveEffect) { case EFFECT_HIT: break; case EFFECT_SLEEP: case EFFECT_YAWN: - if (AI_CanPutToSleep(battlerAtk, battlerDef, AI_DATA->defAbility, move, AI_DATA->partnerMove)) - score++; - if ((HasMoveEffect(battlerAtk, EFFECT_DREAM_EATER) || HasMoveEffect(battlerAtk, EFFECT_NIGHTMARE)) && - !(HasMoveEffect(battlerDef, EFFECT_SNORE) || HasMoveEffect(battlerDef, EFFECT_SLEEP_TALK))) - score++; + IncreaseSleepScore(battlerAtk, battlerDef, move, &score); break; case EFFECT_DREAM_EATER: if (!(gBattleMons[battlerDef].status1 & STATUS1_SLEEP)) break; - score++; // if target is asleep, dream eater is a pretty good move already + score++; // if target is asleep, dream eater is a pretty good move even without draining // fallthrough case EFFECT_ABSORB: - if (ShouldRecover(battlerAtk, battlerDef, move, AI_THINKING_STRUCT->simulatedDmg[battlerAtk][battlerDef][AI_THINKING_STRUCT->movesetIndex])) + if (ShouldAbsorb(battlerAtk, battlerDef, move, AI_THINKING_STRUCT->simulatedDmg[battlerAtk][battlerDef][AI_THINKING_STRUCT->movesetIndex])) score += 2; + if (AI_DATA->atkHoldEffect == HOLD_EFFECT_BIG_ROOT) + score++; break; case EFFECT_EXPLOSION: case EFFECT_MEMENTO: @@ -4428,432 +4461,1478 @@ static u8 AI_CheckGoodMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) break; case EFFECT_ATTACK_UP: case EFFECT_ATTACK_UP_2: - if (HasMoveWithSplit(battlerAtk, SPLIT_PHYSICAL) && GetHealthPercentage(battlerAtk) > 40 && AI_DATA->atkAbility != ABILITY_CONTRARY) - { - if (gBattleMons[battlerAtk].statStages[STAT_ATK] < 8) - score += 2; - else if (gBattleMons[battlerAtk].statStages[STAT_ATK] < 11) - score++; - } + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_ATK, &score); break; case EFFECT_DEFENSE_UP: case EFFECT_DEFENSE_UP_2: - if ((HasMoveWithSplit(battlerDef, SPLIT_PHYSICAL) || IS_MOVE_PHYSICAL(gLastMoves[battlerDef])) && GetHealthPercentage(battlerAtk) > 70 && AI_DATA->atkAbility != ABILITY_CONTRARY) - { - if (gBattleMons[battlerAtk].statStages[STAT_DEF] < 8) - score += 2; // seems better to raise def at higher HP - else if (gBattleMons[battlerAtk].statStages[STAT_DEF] < 10) - score++; - } + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_DEF, &score); break; - case EFFECT_SPEED_UP: + case EFFECT_SPEED_UP: case EFFECT_SPEED_UP_2: - if (IsBattlerFaster(AI_CHECK_SLOWER) && AI_DATA->atkAbility != ABILITY_CONTRARY) - score += 3; + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_SPEED, &score); break; - case EFFECT_SPECIAL_ATTACK_UP: + case EFFECT_SPECIAL_ATTACK_UP: case EFFECT_SPECIAL_ATTACK_UP_2: - if (HasMoveWithSplit(battlerAtk, SPLIT_SPECIAL) && GetHealthPercentage(battlerAtk) > 40 && AI_DATA->atkAbility != ABILITY_CONTRARY) - { - if (gBattleMons[battlerAtk].statStages[STAT_SPATK] < 8) - score += 2; - else if (gBattleMons[battlerAtk].statStages[STAT_SPATK] < 11) - score++; - } + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_SPATK, &score); break; - case EFFECT_SPECIAL_DEFENSE_UP: + case EFFECT_SPECIAL_DEFENSE_UP: case EFFECT_SPECIAL_DEFENSE_UP_2: - if ((HasMoveWithSplit(battlerDef, SPLIT_SPECIAL) || IS_MOVE_SPECIAL(gLastMoves[battlerDef])) && GetHealthPercentage(battlerAtk) > 70 && AI_DATA->atkAbility != ABILITY_CONTRARY) - { - if (gBattleMons[battlerAtk].statStages[STAT_SPDEF] < 8) - score += 2; // seems better to raise spdef at higher HP - else if (gBattleMons[battlerAtk].statStages[STAT_SPDEF] < 10) - score++; - } + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_SPDEF, &score); break; case EFFECT_ACCURACY_UP: case EFFECT_ACCURACY_UP_2: - if (HasMoveWithLowAccuracy(battlerAtk, battlerDef, 80, TRUE, AI_DATA->atkAbility, AI_DATA->defAbility, AI_DATA->atkHoldEffect, AI_DATA->defHoldEffect, move)) - score += 3; // has moves with less than 80% accuracy - else if (HasMoveWithLowAccuracy(battlerAtk, battlerDef, 90, TRUE, AI_DATA->atkAbility, AI_DATA->defAbility, AI_DATA->atkHoldEffect, AI_DATA->defHoldEffect, move)) - score += 2; + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_ACC, &score); break; - case EFFECT_EVASION_UP: + case EFFECT_EVASION_UP: case EFFECT_EVASION_UP_2: case EFFECT_MINIMIZE: - if (AI_DATA->atkAbility != ABILITY_CONTRARY && !BattlerWillFaintFromWeather(battlerAtk, AI_DATA->atkAbility)) - { - if (!BattlerHasSecondaryDamage(battlerAtk) && !(gStatuses3[battlerAtk] & STATUS3_ROOTED)) - score += 3; - else - score += 2; - } + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_EVASION, &score); break; case EFFECT_ACUPRESSURE: break; - case EFFECT_ATTACK_ACCURACY_UP: // hone claws + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_ATK, &score); + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_ACC, &score); break; - case EFFECT_GROWTH: case EFFECT_ATTACK_SPATK_UP: // work up - break; + if (GetHealthPercentage(battlerAtk) <= 40 || AI_DATA->atkAbility == ABILITY_CONTRARY) + break; - case EFFECT_ATTACK_DOWN: + if (HasMoveWithSplit(battlerAtk, SPLIT_PHYSICAL)) + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_ATK, &score); + else if (HasMoveWithSplit(battlerAtk, SPLIT_SPECIAL)) + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_SPATK, &score); + break; + case EFFECT_ATTACK_DOWN: case EFFECT_ATTACK_DOWN_2: if (ShouldLowerAttack(battlerAtk, battlerDef, AI_DATA->defAbility, AI_THINKING_STRUCT->movesetIndex)) score += 2; break; - case EFFECT_DEFENSE_DOWN: + case EFFECT_DEFENSE_DOWN: case EFFECT_DEFENSE_DOWN_2: - //AI_CV_DefenseDown + if (ShouldLowerDefense(battlerAtk, battlerDef, AI_DATA->defAbility, AI_THINKING_STRUCT->movesetIndex)) + score += 2; break; - case EFFECT_SPEED_DOWN: + case EFFECT_SPEED_DOWN: case EFFECT_SPEED_DOWN_2: - //AI_CV_SpeedDown + if (ShouldLowerSpeed(battlerAtk, battlerDef, AI_DATA->defAbility, AI_THINKING_STRUCT->movesetIndex)) + score += 3; break; - case EFFECT_SPECIAL_ATTACK_DOWN: + case EFFECT_SPECIAL_ATTACK_DOWN: case EFFECT_SPECIAL_ATTACK_DOWN_2: - //AI_CV_SpAtkDown + if (ShouldLowerSpAtk(battlerAtk, battlerDef, AI_DATA->defAbility, AI_THINKING_STRUCT->movesetIndex)) + score += 2; break; - case EFFECT_SPECIAL_DEFENSE_DOWN: + case EFFECT_SPECIAL_DEFENSE_DOWN: case EFFECT_SPECIAL_DEFENSE_DOWN_2: - //AI_CV_SpDefDown + if (ShouldLowerSpDef(battlerAtk, battlerDef, AI_DATA->defAbility, AI_THINKING_STRUCT->movesetIndex)) + score += 2; break; - case EFFECT_ACCURACY_DOWN: + case EFFECT_ACCURACY_DOWN: case EFFECT_ACCURACY_DOWN_2: - //AI_CV_AccuracyDown + if (ShouldLowerAccuracy(battlerAtk, battlerDef, AI_DATA->defAbility, AI_THINKING_STRUCT->movesetIndex)) + score += 4; break; - case EFFECT_EVASION_DOWN: + case EFFECT_EVASION_DOWN: case EFFECT_EVASION_DOWN_2: - //AI_CV_EvasionDown + if (ShouldLowerEvasion(battlerAtk, battlerDef, AI_DATA->defAbility, AI_THINKING_STRUCT->movesetIndex)) + { + // kinda meh effect, so let's make sure we really want to + if (gBattleMons[battlerDef].statStages[STAT_EVASION] > 7 + || HasMoveWithLowAccuracy(battlerAtk, battlerDef, 90, TRUE, AI_DATA->atkAbility, AI_DATA->defAbility, AI_DATA->atkHoldEffect, AI_DATA->defHoldEffect)) + score += 2; // encourage lowering evasion if they are evasive or we have a move with low accuracy + else + score++; + } break; - case EFFECT_HAZE: - //AI_CV_Hazes + case EFFECT_HAZE: + if (AnyStatIsRaised(AI_DATA->battlerAtkPartner) + || PartnerHasSameMoveEffectWithoutTarget(AI_DATA->battlerAtkPartner, move, AI_DATA->partnerMove)) + break; + // fallthrough + case EFFECT_ROAR: + case EFFECT_CLEAR_SMOG: + if (isDoubleBattle) + score += min(CountPositiveStatStages(battlerDef) + CountPositiveStatStages(AI_DATA->battlerDefPartner), 7); + else + score += min(CountPositiveStatStages(battlerDef), 4); break; - case EFFECT_BIDE: - //AI_CV_Bide + case EFFECT_BIDE: break; - case EFFECT_ROAR: - //AI_CV_Roar + case EFFECT_MULTI_HIT: + case EFFECT_DOUBLE_HIT: + case EFFECT_TRIPLE_KICK: break; - case EFFECT_CONVERSION: - //AI_CV_Conversion + case EFFECT_CONVERSION: + if (!IS_BATTLER_OF_TYPE(battlerAtk, gBattleMoves[gBattleMons[battlerAtk].moves[0]].type)) + score++; break; - case EFFECT_RESTORE_HP: - //AI_CV_Heal + case EFFECT_FLINCH_HIT: + score += ShouldTryToFlinch(battlerAtk, battlerDef, AI_DATA->atkAbility, AI_DATA->defAbility, move); break; - case EFFECT_SOFTBOILED: - //AI_CV_Heal + case EFFECT_SWALLOW: + if (gDisableStructs[battlerAtk].stockpileCounter == 0) + { + break; + } + else + { + u32 healPercent = 0; + switch (gDisableStructs[battlerAtk].stockpileCounter) + { + case 1: + healPercent = 25; + break; + case 2: + healPercent = 50; + break; + case 3: + healPercent = 100; + break; + default: + break; + } + + if (ShouldRecover(battlerAtk, battlerDef, move, healPercent)) + score += 2; + } break; - case EFFECT_SWALLOW: - //AI_CV_Heal + case EFFECT_RESTORE_HP: + case EFFECT_SOFTBOILED: + case EFFECT_ROOST: + case EFFECT_MORNING_SUN: + case EFFECT_SYNTHESIS: + case EFFECT_MOONLIGHT: + if (ShouldRecover(battlerAtk, battlerDef, move, 50)) + score += 3; + if (AI_DATA->atkHoldEffect == HOLD_EFFECT_BIG_ROOT) + score++; break; - case EFFECT_ROOST: - //AI_CV_Heal - break; - case EFFECT_TOXIC: - //AI_CV_Toxic - break; - case EFFECT_LIGHT_SCREEN: - //AI_CV_LightScreen - break; - case EFFECT_REST: - //AI_CV_Rest - break; - case EFFECT_OHKO: - //AI_CV_OneHitKO - break; - case EFFECT_SUPER_FANG: - //AI_CV_SuperFang - break; - case EFFECT_TRAP: - //AI_CV_Trap - break; - case EFFECT_CONFUSE: - //AI_CV_Confuse - break; - case EFFECT_FOCUS_ENERGY: - //AI_CV_FocusEnergy + case EFFECT_TOXIC: + case EFFECT_POISON: + IncreasePoisonScore(battlerAtk, battlerDef, move, &score); break; + case EFFECT_LIGHT_SCREEN: case EFFECT_REFLECT: - //AI_CV_Reflect - break; - case EFFECT_AURORA_VEIL: - //AI_CV_AuroraVeil - break; - case EFFECT_POISON: - //AI_CV_Poison - break; - case EFFECT_TOXIC_THREAD: - //AI_CV_ToxicThread - break; - case EFFECT_PARALYZE: - //AI_CV_Paralyze - break; - case EFFECT_SWAGGER: - //AI_CV_Swagger - break; - case EFFECT_SPEED_DOWN_HIT: - //AI_CV_SpeedDownFromChance - break; - case EFFECT_TWO_TURNS_ATTACK: - //AI_CV_ChargeUpMove - break; - case EFFECT_VITAL_THROW: - //AI_CV_VitalThrow - break; - case EFFECT_SUBSTITUTE: - //AI_CV_Substitute - break; - case EFFECT_RECHARGE: - //AI_CV_Recharge - break; - case EFFECT_LEECH_SEED: - //AI_CV_LeechSeed - break; - case EFFECT_DISABLE: - //AI_CV_Disable - break; - case EFFECT_COUNTER: - //AI_CV_Counter - break; - case EFFECT_ENCORE: - //AI_CV_Encore - break; - case EFFECT_PAIN_SPLIT: - //AI_CV_PainSplit - break; - case EFFECT_LOCK_ON: - //AI_CV_LockOn - break; - case EFFECT_SLEEP_TALK: - //AI_CV_SleepTalk - break; - case EFFECT_SNORE: - //AI_CV_SleepTalk - break; - case EFFECT_DESTINY_BOND: - //AI_CV_DestinyBond - break; - case EFFECT_FLAIL: - //AI_CV_Flail - break; - case EFFECT_HEAL_BELL: - //AI_CV_HealBell - break; - case EFFECT_THIEF: - //AI_CV_Thief - break; - case EFFECT_MEAN_LOOK: - //AI_CV_Trap - break; - case EFFECT_CURSE: - //AI_CV_Curse - break; - case EFFECT_PROTECT: - //AI_CV_Protect - break; - case EFFECT_FORESIGHT: - //AI_CV_Foresight - break; - case EFFECT_ENDURE: - //AI_CV_Endure - break; - case EFFECT_BATON_PASS: - //AI_CV_BatonPass - break; - case EFFECT_PURSUIT: - //AI_CV_Pursuit - break; - case EFFECT_MORNING_SUN: - //AI_CV_HealWeather - break; - case EFFECT_SYNTHESIS: - //AI_CV_HealWeather - break; - case EFFECT_MOONLIGHT: - //AI_CV_HealWeather - break; - case EFFECT_SHORE_UP: - //AI_CV_Heal - break; - case EFFECT_RAIN_DANCE: - //AI_CV_RainDance - break; - case EFFECT_SUNNY_DAY: - //AI_CV_SunnyDay - break; - case EFFECT_BELLY_DRUM: - //AI_CV_BellyDrum - break; - case EFFECT_PSYCH_UP: - //AI_CV_PsychUp - break; - case EFFECT_MIRROR_COAT: - //AI_CV_MirrorCoat - break; - case EFFECT_SKULL_BASH: - //AI_CV_ChargeUpMove - break; - case EFFECT_SOLARBEAM: - //AI_CV_ChargeUpMove - break; - break; - case EFFECT_GEOMANCY: - //AI_CV_Geomancy - break; - case EFFECT_SEMI_INVULNERABLE: - //AI_CV_SemiInvulnerable - break; - case EFFECT_FAKE_OUT: - //AI_CV_FakeOut - break; - case EFFECT_SPIT_UP: - //AI_CV_SpitUp - break; - case EFFECT_HAIL: - //AI_CV_Sandstorm - break; - case EFFECT_SANDSTORM: - //AI_CV_Sandstorm - break; - case EFFECT_FLATTER: - //AI_CV_Flatter - break; - case EFFECT_FACADE: - //AI_CV_Facade - break; - case EFFECT_FOCUS_PUNCH: - //AI_CV_FocusPunch - break; - case EFFECT_SMELLINGSALT: - //AI_CV_SmellingSalt - break; - case EFFECT_TRICK: - //AI_CV_Trick - break; - case EFFECT_ROLE_PLAY: - //AI_CV_ChangeSelfAbility - break; - case EFFECT_SUPERPOWER: - //AI_CV_Superpower - break; - case EFFECT_MAGIC_COAT: - //AI_CV_MagicCoat - break; - case EFFECT_RECYCLE: - //AI_CV_Recycle - break; - case EFFECT_REVENGE: - //AI_CV_Revenge - break; - case EFFECT_BRICK_BREAK: - //AI_CV_BrickBreak - break; - case EFFECT_KNOCK_OFF: - //AI_CV_KnockOff - break; - case EFFECT_ENDEAVOR: - //AI_CV_Endeavor - break; - case EFFECT_ERUPTION: - //AI_CV_Eruption - break; - case EFFECT_SKILL_SWAP: - //AI_CV_ChangeSelfAbility - break; - case EFFECT_IMPRISON: - //AI_CV_Imprison - break; - case EFFECT_REFRESH: - //AI_CV_Refresh - break; - case EFFECT_SNATCH: - //AI_CV_Snatch - break; - case EFFECT_MUD_SPORT: - //AI_CV_MudSport - break; - case EFFECT_OVERHEAT: - //AI_CV_Overheat - break; - case EFFECT_TICKLE: - //AI_CV_DefenseDown - break; - case EFFECT_COSMIC_POWER: - //AI_CV_SpDefUp - break; - case EFFECT_BULK_UP: - //AI_CV_DefenseUp - break; - case EFFECT_WATER_SPORT: - //AI_CV_WaterSport - break; - case EFFECT_CALM_MIND: - //AI_CV_SpDefUp - break; - case EFFECT_DRAGON_DANCE: - //AI_CV_DragonDance - break; - case EFFECT_POWDER: - //AI_CV_Powder - break; - case EFFECT_MISTY_TERRAIN: - //AI_CV_MistyTerrain - break; - case EFFECT_GRASSY_TERRAIN: - //AI_CV_GrassyTerrain - break; - case EFFECT_ELECTRIC_TERRAIN: - //AI_CV_ElectricTerrain - break; - case EFFECT_PSYCHIC_TERRAIN: - //AI_CV_PsychicTerrain - break; - case EFFECT_STEALTH_ROCK: - //AI_CV_Hazards - break; - case EFFECT_SPIKES: - //AI_CV_Hazards - break; - case EFFECT_STICKY_WEB: - //AI_CV_Hazards - break; - case EFFECT_TOXIC_SPIKES: - //AI_CV_Hazards - break; - case EFFECT_PERISH_SONG: - //AI_CV_PerishSong - break; + case EFFECT_AURORA_VEIL: + if (ShouldSetScreen(battlerAtk, battlerDef, move)) + { + score += 5; + if (AI_DATA->atkHoldEffect == HOLD_EFFECT_LIGHT_CLAY) + score += 2; + if (AI_THINKING_STRUCT->aiFlags & AI_FLAG_SCREENER) + score += 2; + } + break; + case EFFECT_REST: + if (!(CanSleep(battlerAtk, AI_DATA->atkAbility))) + { + break; + } + else if (ShouldRecover(battlerAtk, battlerDef, move, 100)) + { + if (AI_DATA->atkHoldEffect == HOLD_EFFECT_CURE_SLP + || AI_DATA->atkHoldEffect == HOLD_EFFECT_CURE_STATUS + || HasMoveEffect(EFFECT_SLEEP_TALK, battlerAtk) + || HasMoveEffect(EFFECT_SNORE, battlerAtk) + || AI_DATA->atkAbility == ABILITY_SHED_SKIN + || AI_DATA->atkAbility == ABILITY_EARLY_BIRD + || (gBattleWeather & WEATHER_RAIN_ANY && gWishFutureKnock.weatherDuration != 1 && AI_DATA->atkAbility == ABILITY_HYDRATION && AI_DATA->atkHoldEffect != HOLD_EFFECT_UTILITY_UMBRELLA)) + { + score += 2; + } + else + { + score++; + } + } + break; + case EFFECT_OHKO: + if (gStatuses3[battlerAtk] & STATUS3_ALWAYS_HITS) + score += 5; + break; + case EFFECT_TRAP: + case EFFECT_MEAN_LOOK: + if (HasMoveEffect(battlerDef, EFFECT_RAPID_SPIN) + || IS_BATTLER_OF_TYPE(battlerDef, TYPE_GHOST) + || gBattleMons[battlerDef].status2 & STATUS2_WRAPPED) + { + break; // in this case its a bad attacking move + } + else if (ShouldTrap(battlerAtk, battlerDef, move)) + { + score += 5; + } + break; + case EFFECT_MIST: + if (AI_THINKING_STRUCT->aiFlags & AI_FLAG_SCREENER) + score += 2; + break; + case EFFECT_FOCUS_ENERGY: + case EFFECT_LASER_FOCUS: + if (AI_DATA->atkAbility == ABILITY_SUPER_LUCK + || AI_DATA->atkAbility == ABILITY_SNIPER + || AI_DATA->atkHoldEffect == HOLD_EFFECT_SCOPE_LENS + || TestMoveFlagsInMoveset(battlerAtk, FLAG_HIGH_CRIT)) + score += 2; + break; + case EFFECT_CONFUSE_HIT: + if (AI_DATA->atkAbility == ABILITY_SERENE_GRACE) + score++; + //fallthrough + case EFFECT_CONFUSE: + IncreaseConfusionScore(battlerAtk, battlerDef, move, &score); + break; + case EFFECT_PARALYZE: + IncreaseParalyzeScore(battlerAtk, battlerDef, move, &score); + break; + case EFFECT_ATTACK_DOWN_HIT: + case EFFECT_DEFENSE_DOWN_HIT: + case EFFECT_SPECIAL_ATTACK_DOWN_HIT: + case EFFECT_SPECIAL_DEFENSE_DOWN_HIT: + case EFFECT_ACCURACY_DOWN_HIT: + case EFFECT_EVASION_DOWN_HIT: + if (AI_DATA->atkAbility == ABILITY_SERENE_GRACE && AI_DATA->defAbility != ABILITY_CONTRARY) + score += 2; + break; + case EFFECT_SPEED_DOWN_HIT: + if (ShouldLowerSpeed(battlerAtk, battlerDef, AI_DATA->defAbility, AI_THINKING_STRUCT->movesetIndex)) + { + if (AI_DATA->atkAbility == ABILITY_SERENE_GRACE && AI_DATA->defAbility != ABILITY_CONTRARY) + score += 4; + else + score += 2; + } + break; + case EFFECT_SUBSTITUTE: + if (gStatuses3[battlerDef] & STATUS3_PERISH_SONG) + score += 3; + if (gBattleMons[battlerDef].status1 & (STATUS1_BURN | STATUS1_PSN_ANY)) + score++; + if (HasMoveEffect(battlerDef, EFFECT_SLEEP) + || HasMoveEffect(battlerDef, EFFECT_TOXIC) + || HasMoveEffect(battlerDef, EFFECT_POISON) + || HasMoveEffect(battlerDef, EFFECT_PARALYZE) + || HasMoveEffect(battlerDef, EFFECT_WILL_O_WISP) + || HasMoveEffect(battlerDef, EFFECT_CONFUSE) + || HasMoveEffect(battlerDef, EFFECT_LEECH_SEED)) + score += 2; + if (!gBattleMons[battlerDef].status2 & (STATUS2_WRAPPED | STATUS2_ESCAPE_PREVENTION && GetHealthPercentage(battlerAtk) > 70)) + score++; + break; + case EFFECT_MIMIC: + if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0) + { + if (gLastMoves[battlerDef] != MOVE_NONE && gLastMoves[battlerDef] != 0xFFFF) + return AI_CheckGoodMove(battlerAtk, battlerDef, gLastMoves[battlerDef], score); + } + break; + case EFFECT_LEECH_SEED: + if (IS_BATTLER_OF_TYPE(battlerDef, TYPE_GRASS) + || gStatuses3[battlerDef] & STATUS3_LEECHSEED + || HasMoveEffect(battlerDef, EFFECT_RAPID_SPIN) + || AI_DATA->defAbility == ABILITY_LIQUID_OOZE + || AI_DATA->defAbility == ABILITY_MAGIC_GUARD) + break; + score += 3; + if (!HasDamagingMove(battlerDef) || IsBattlerTrapped(battlerDef, FALSE)) + score += 2; + break; + case EFFECT_DO_NOTHING: + //todo - check z splash, z celebrate, z happy hour (lol) + break; + case EFFECT_TELEPORT: + if (!(gBattleTypeFlags & BATTLE_TYPE_TRAINER) || GetBattlerSide(battlerAtk) != B_SIDE_PLAYER) + break; + //fallthrough + case EFFECT_HIT_ESCAPE: + case EFFECT_PARTING_SHOT: + if (!IsDoubleBattle()) + { + switch (ShouldPivot(battlerAtk, battlerDef, AI_DATA->defAbility, move, AI_THINKING_STRUCT->movesetIndex)) + { + case 0: // no + score -= 10; // technically should go in CheckBadMove, but this is easier/less computationally demanding + break; + case 1: // maybe + break; + case 2: // yes + score += 7; + break; + } + } + else //Double Battle + { + if (CountUsablePartyMons(battlerAtk) == 0) + break; // Can't switch + + //if (switchAbility == ABILITY_INTIMIDATE && PartyHasMoveSplit(battlerDef, SPLIT_PHYSICAL)) + //score += 7; + } + break; + case EFFECT_BATON_PASS: + if (ShouldSwitch() && (gBattleMons[battlerAtk].status2 & STATUS2_SUBSTITUTE + || (gStatuses3[battlerAtk] & (STATUS3_ROOTED | STATUS3_AQUA_RING | STATUS3_MAGNET_RISE | STATUS3_POWER_TRICK)) + || AnyStatIsRaised(battlerAtk))) + score += 5; + break; + case EFFECT_DISABLE: + if (gDisableStructs[battlerDef].disableTimer == 0 + && (B_MENTAL_HERB >= GEN_5 && AI_DATA->defHoldEffect != HOLD_EFFECT_CURE_ATTRACT)) // mental herb + { + if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0) // AI goes first + { + if (gLastMoves[battlerDef] != MOVE_NONE + && gLastMoves[battlerDef] != 0xFFFF) + { + /* TODO predicted moves + if (gLastMoves[battlerDef] == predictedMove) + score += 3; + else */if (CanMoveFaintBattler(gLastMoves[battlerDef], battlerDef, battlerAtk, 1)) + score += 2;; //Disable move that can kill attacker + } + } + else if (predictedMove != MOVE_NONE && IS_MOVE_STATUS(predictedMove)) + { + score++; // Disable annoying status moves + } + } + break; + case EFFECT_ENCORE: + if (gDisableStructs[battlerDef].encoreTimer == 0 + && (B_MENTAL_HERB >= GEN_5 && AI_DATA->defHoldEffect != HOLD_EFFECT_CURE_ATTRACT)) // mental herb + { + if (IsEncoreEncouragedEffect(gBattleMoves[gLastMoves[battlerDef]].effect)) + score += 3; + } + break; + case EFFECT_PAIN_SPLIT: + { + u16 newHp = (gBattleMons[battlerAtk].hp + gBattleMons[battlerDef].hp) / 2; + u16 healthBenchmark = (gBattleMons[battlerAtk].hp * 12) / 10; + if (newHp > healthBenchmark && ShouldAbsorb(battlerAtk, battlerDef, move, AI_THINKING_STRUCT->simulatedDmg[battlerAtk][battlerDef][AI_THINKING_STRUCT->movesetIndex])) + score += 2; + } + break; + case EFFECT_SLEEP_TALK: + case EFFECT_SNORE: + if (!IsWakeupTurn(battlerAtk) && gBattleMons[battlerAtk].status1 & STATUS1_SLEEP) + score += 10; + break; + case EFFECT_LOCK_ON: + if (HasMoveEffect(battlerAtk, EFFECT_OHKO)) + score += 3; + else if (AI_DATA->atkAbility == ABILITY_COMPOUND_EYES && HasMoveWithLowAccuracy(battlerAtk, battlerDef, 80, TRUE, AI_DATA->atkAbility, AI_DATA->defAbility, AI_DATA->atkHoldEffect, AI_DATA->defHoldEffect)) + score += 3; + else if (HasMoveWithLowAccuracy(battlerAtk, battlerDef, 85, TRUE, AI_DATA->atkAbility, AI_DATA->defAbility, AI_DATA->atkHoldEffect, AI_DATA->defHoldEffect)) + score += 3; + else if (HasMoveWithLowAccuracy(battlerAtk, battlerDef, 90, TRUE, AI_DATA->atkAbility, AI_DATA->defAbility, AI_DATA->atkHoldEffect, AI_DATA->defHoldEffect)) + score++; + break; + case EFFECT_SPEED_UP_HIT: + if (AI_DATA->atkAbility == ABILITY_SERENE_GRACE && AI_DATA->defAbility != ABILITY_CONTRARY && IsAiFaster(AI_CHECK_SLOWER)) + score += 3; + break; + case EFFECT_DESTINY_BOND: + if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0 && CanTargetFaintAi(battlerDef, battlerAtk)) + score += 3; + break; + case EFFECT_SPITE: + //TODO - predicted move + break; + case EFFECT_WISH: + case EFFECT_HEAL_BELL: + if (ShouldUseWishAromatherapy(battlerAtk, battlerDef, move)) + score += 7; + break; + case EFFECT_THIEF: + { + bool32 canSteal = FALSE; + + #if defined B_TRAINERS_KNOCK_OFF_ITEMS && B_TRAINERS_KNOCK_OFF_ITEMS == TRUE + canSteal = TRUE; + #endif + if (gBattleTypeFlags & BATTLE_TYPE_FRONTIER || GetBattlerSide(battlerAtk) == B_SIDE_PLAYER) + canSteal = TRUE; + + if (canSteal && AI_DATA->atkItem == ITEM_NONE + && AI_DATA->defItem != ITEM_NONE + && CanBattlerGetOrLoseItem(battlerDef, AI_DATA->defItem) + && CanBattlerGetOrLoseItem(battlerAtk, AI_DATA->defItem) + && !HasMoveEffect(battlerAtk, EFFECT_ACROBATICS) + && AI_DATA->defAbility != ABILITY_STICKY_HOLD) + { + switch (AI_DATA->defHoldEffect) + { + case HOLD_EFFECT_NONE: + break; + case HOLD_EFFECT_CHOICE_BAND: + case HOLD_EFFECT_CHOICE_SCARF: + case HOLD_EFFECT_CHOICE_SPECS: + score += 2; + break; + case HOLD_EFFECT_TOXIC_ORB: + if (ShouldPoisonSelf(battlerAtk, AI_DATA->atkAbility)) + score += 2; + break; + case HOLD_EFFECT_FLAME_ORB: + if (ShouldBurnSelf(battlerAtk, AI_DATA->atkAbility)) + score += 2; + break; + case HOLD_EFFECT_BLACK_SLUDGE: + if (IS_BATTLER_OF_TYPE(battlerAtk, TYPE_POISON)) + score += 2; + break; + case HOLD_EFFECT_IRON_BALL: + if (HasMoveEffect(battlerAtk, EFFECT_FLING)) + score += 2; + break; + case HOLD_EFFECT_LAGGING_TAIL: + case HOLD_EFFECT_STICKY_BARB: + break; + default: + score++; + break; + } + } + break; + } + break; + case EFFECT_NIGHTMARE: + if (AI_DATA->defAbility != ABILITY_MAGIC_GUARD + && !(gBattleMons[battlerDef].status2 & STATUS2_NIGHTMARE) + && (AI_DATA->defAbility == ABILITY_COMATOSE || gBattleMons[battlerDef].status1 & STATUS1_SLEEP)) + { + score += 5; + if (IsBattlerTrapped(battlerDef, TRUE)) + score += 3; + } + break; + case EFFECT_CURSE: + if (IS_BATTLER_OF_TYPE(battlerAtk, TYPE_GHOST)) + { + if (IsBattlerTrapped(battlerDef, TRUE)) + score += 3; + else + score++; + break; + } + else + { + if (AI_DATA->atkAbility == ABILITY_CONTRARY || AI_DATA->defAbility == ABILITY_MAGIC_GUARD) + break; + else if (gBattleMons[battlerAtk].statStages[STAT_ATK] < 8) + score += (8 - gBattleMons[battlerAtk].statStages[STAT_ATK]); + else if (gBattleMons[battlerAtk].statStages[STAT_SPEED] < 3) + break; + else if (gBattleMons[battlerAtk].statStages[STAT_DEF] < 8) + score += (8 - gBattleMons[battlerAtk].statStages[STAT_DEF]); + } + break; + case EFFECT_PROTECT: + if (predictedMove == 0xFFFF) + predictedMove = MOVE_NONE; + switch (move) + { + case MOVE_QUICK_GUARD: + if (predictedMove != MOVE_NONE && gBattleMoves[predictedMove].priority > 0) + ProtectChecks(battlerAtk, battlerDef, move, predictedMove, &score); + break; + case MOVE_WIDE_GUARD: + if (predictedMove != MOVE_NONE && gBattleMoves[predictedMove].target & (MOVE_TARGET_FOES_AND_ALLY | MOVE_TARGET_BOTH)) + { + ProtectChecks(battlerAtk, battlerDef, move, predictedMove, &score); + } + else if (isDoubleBattle && gBattleMoves[AI_DATA->partnerMove].target & MOVE_TARGET_FOES_AND_ALLY) + { + if (AI_DATA->atkAbility != ABILITY_TELEPATHY) + ProtectChecks(battlerAtk, battlerDef, move, predictedMove, &score); + } + break; + case MOVE_CRAFTY_SHIELD: + if (predictedMove != MOVE_NONE && IS_MOVE_STATUS(predictedMove) && !(gBattleMoves[predictedMove].target & MOVE_TARGET_USER)) + ProtectChecks(battlerAtk, battlerDef, move, predictedMove, &score); + break; + + case MOVE_MAT_BLOCK: + if (gDisableStructs[battlerAtk].isFirstTurn && predictedMove != MOVE_NONE + && !IS_MOVE_STATUS(predictedMove) && !(gBattleMoves[predictedMove].target & MOVE_TARGET_USER)) + ProtectChecks(battlerAtk, battlerDef, move, predictedMove, &score); + break; + case MOVE_KINGS_SHIELD: + #if (defined SPECIES_AEGISLASH && defined SPECIES_AEGISLASH_BLADE) + if (AI_DATA->atkAbility == ABILITY_STANCE_CHANGE //Special logic for Aegislash + && AI_DATA->atkSpecies == SPECIES_AEGISLASH_BLADE + && !IsBattlerIncapacitated(battlerDef, AI_DATA->defAbility)) + { + score += 3; + break; + } + #endif + //fallthrough + default: // protect + ProtectChecks(battlerAtk, battlerDef, move, predictedMove, &score); + break; + } + break; + case EFFECT_ENDURE: + if (CanTargetFaintAi(battlerDef, battlerAtk)) + { + if (gBattleMons[battlerAtk].hp > gBattleMons[battlerAtk].maxHP / 4 // Pinch berry couldn't have activated yet + && IsPinchBerryItemEffect(AI_DATA->atkHoldEffect)) + { + score += 3; + } + else if (gBattleMons[battlerAtk].hp > 1) // Only spam endure for Flail/Reversal if you're not at Min Health + { + if (HasMoveEffect(battlerAtk, EFFECT_FLAIL) || HasMoveEffect(battlerAtk, EFFECT_ENDEAVOR)) + score += 3; + } + } + break; + + case EFFECT_SPIKES: + case EFFECT_STEALTH_ROCK: + case EFFECT_STICKY_WEB: + case EFFECT_TOXIC_SPIKES: + if (AI_DATA->defAbility == ABILITY_MAGIC_BOUNCE || CountUsablePartyMons(battlerDef) == 0) + break; + if (gDisableStructs[battlerAtk].isFirstTurn) + score += 2; + //TODO - track entire opponent party data to determine hazard effectiveness + break; + case EFFECT_FORESIGHT: + if (AI_DATA->atkAbility == ABILITY_SCRAPPY) + break; + else if (gBattleMons[battlerDef].statStages[STAT_EVASION] > DEFAULT_STAT_STAGE + || (IS_BATTLER_OF_TYPE(battlerDef, TYPE_GHOST) + && (HasMoveWithType(battlerAtk, TYPE_NORMAL) + || HasMoveWithType(battlerAtk, TYPE_FIGHTING)))) + score += 2; + break; + case EFFECT_MIRACLE_EYE: + if (gBattleMons[battlerDef].statStages[STAT_EVASION] > DEFAULT_STAT_STAGE + || (IS_BATTLER_OF_TYPE(battlerDef, TYPE_DARK) && (HasMoveWithType(battlerAtk, TYPE_PSYCHIC)))) + score += 2; + break; + case EFFECT_PERISH_SONG: + if (IsBattlerTrapped(battlerDef, TRUE)) + score += 3; + break; + case EFFECT_SANDSTORM: + if (ShouldSetSandstorm(battlerAtk, AI_DATA->atkHoldEffect, AI_DATA->atkHoldEffect)) + { + score++; + if (AI_DATA->atkHoldEffect == HOLD_EFFECT_SMOOTH_ROCK) + score++; + if (isDoubleBattle && IsBattlerAlive(AI_DATA->battlerAtkPartner) + && ShouldSetSandstorm(AI_DATA->battlerAtkPartner, AI_DATA->atkPartnerAbility, AI_DATA->atkPartnerHoldEffect)) + score += 2; + } + break; + case EFFECT_HAIL: + if (ShouldSetHail(battlerAtk, AI_DATA->atkAbility, AI_DATA->atkHoldEffect)) + { + if ((HasMoveEffect(battlerAtk, EFFECT_AURORA_VEIL) || HasMoveEffect(AI_DATA->battlerAtkPartner, EFFECT_AURORA_VEIL)) + && ShouldSetScreen(battlerAtk, battlerDef, EFFECT_AURORA_VEIL)) + score += 3; + + score++; + if (AI_DATA->atkHoldEffect == HOLD_EFFECT_ICY_ROCK) + score++; + if (isDoubleBattle && IsBattlerAlive(AI_DATA->battlerAtkPartner) + && ShouldSetHail(AI_DATA->battlerAtkPartner, AI_DATA->atkPartnerAbility, AI_DATA->atkPartnerHoldEffect)) + score += 2; + if (HasMoveEffect(battlerDef, EFFECT_MORNING_SUN) + || HasMoveEffect(battlerDef, EFFECT_SYNTHESIS) + || HasMoveEffect(battlerDef, EFFECT_MOONLIGHT)) + score += 2; + } + break; + case EFFECT_RAIN_DANCE: + if (ShouldSetRain(battlerAtk, AI_DATA->atkAbility, AI_DATA->atkHoldEffect)) + { + score++; + if (AI_DATA->atkHoldEffect == HOLD_EFFECT_DAMP_ROCK) + score++; + if (isDoubleBattle && IsBattlerAlive(AI_DATA->battlerAtkPartner) + && ShouldSetRain(AI_DATA->battlerAtkPartner, AI_DATA->atkPartnerAbility, AI_DATA->atkPartnerHoldEffect)) + score += 2; + if (HasMoveEffect(battlerDef, EFFECT_MORNING_SUN) + || HasMoveEffect(battlerDef, EFFECT_SYNTHESIS) + || HasMoveEffect(battlerDef, EFFECT_MOONLIGHT)) + score += 2; + if (HasMoveWithType(battlerDef, TYPE_FIRE) || HasMoveWithType(AI_DATA->battlerDefPartner, TYPE_FIRE)) + score++; + } + break; + case EFFECT_SUNNY_DAY: + if (ShouldSetSun(battlerAtk, AI_DATA->atkAbility, AI_DATA->atkHoldEffect)) + { + score++; + if (isDoubleBattle && IsBattlerAlive(AI_DATA->battlerAtkPartner) + && ShouldSetSun(AI_DATA->battlerAtkPartner, AI_DATA->atkPartnerAbility, AI_DATA->atkPartnerHoldEffect)) + score += 2; // partner also gets sunlight benefit + if (AI_DATA->atkHoldEffect == HOLD_EFFECT_HEAT_ROCK) + score++; + if (HasMoveWithType(battlerDef, TYPE_WATER) || HasMoveWithType(AI_DATA->battlerDefPartner, TYPE_WATER)) + score++; + if (HasMoveEffect(battlerDef, EFFECT_THUNDER) || HasMoveEffect(AI_DATA->battlerDefPartner, EFFECT_THUNDER)) + score++; + } + break; + case EFFECT_ATTACK_UP_HIT: + if (AI_DATA->atkAbility == ABILITY_SERENE_GRACE) + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_ATK, &score); + break; + case EFFECT_FELL_STINGER: + if (gBattleMons[battlerAtk].statStages[STAT_ATK] < MAX_STAT_STAGE + && AI_DATA->atkAbility != ABILITY_CONTRARY + && CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) + { + if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0) // Attacker goes first + score += 9; + else + score += 3; + } + break; + case EFFECT_BELLY_DRUM: + if (!CanTargetFaintAi(battlerDef, battlerAtk) && HasMoveWithSplit(battlerAtk, SPLIT_PHYSICAL) && AI_DATA->atkAbility != ABILITY_CONTRARY) + score += (MAX_STAT_STAGE - gBattleMons[battlerAtk].statStages[STAT_ATK]); + break; + case EFFECT_PSYCH_UP: + case EFFECT_SPECTRAL_THIEF: + // Want to copy positive stat changes + for (i = STAT_ATK; i < NUM_BATTLE_STATS; i++) + { + if (gBattleMons[battlerDef].statStages[i] > gBattleMons[battlerAtk].statStages[i]) + { + switch (i) + { + case STAT_ATK: + if (HasMoveWithSplit(battlerAtk, SPLIT_PHYSICAL)) + score++; + break; + case STAT_SPATK: + if (HasMoveWithSplit(battlerAtk, SPLIT_SPECIAL)) + score++; + break; + case STAT_ACC: + case STAT_EVASION: + case STAT_SPEED: + score++; + break; + case STAT_DEF: + case STAT_SPDEF: + if (AI_THINKING_STRUCT->aiFlags & AI_FLAG_STALL) + score++; + break; + } + } + } + break; + case EFFECT_SEMI_INVULNERABLE: + score++; + if (predictedMove != MOVE_NONE && !isDoubleBattle) + { + if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0) // Attacker goes first + { + if (gBattleMoves[predictedMove].effect == EFFECT_EXPLOSION + || gBattleMoves[predictedMove].effect == EFFECT_PROTECT) + score += 3; + } + else if (gBattleMoves[predictedMove].effect == EFFECT_SEMI_INVULNERABLE && !(gStatuses3[battlerDef] & STATUS3_SEMI_INVULNERABLE)) + { + score += 3; + } + } + break; + case EFFECT_DEFENSE_CURL: + if (HasMoveEffect(battlerAtk, EFFECT_ROLLOUT) && !(gBattleMons[battlerAtk].status2 & STATUS2_DEFENSE_CURL)) + score++; + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_DEF, &score); + break; + case EFFECT_FAKE_OUT: + if (move == MOVE_FAKE_OUT // filter out first impression + && ShouldFakeOut(battlerAtk, battlerDef, move)) + score += 8; + break; + case EFFECT_STOCKPILE: + if (AI_DATA->atkAbility == ABILITY_CONTRARY) + break; + if (HasMoveEffect(battlerAtk, EFFECT_SWALLOW) + || HasMoveEffect(battlerAtk, EFFECT_SPIT_UP)) + score += 2; + + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_DEF, &score); + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_SPDEF, &score); + break; + case EFFECT_SPIT_UP: + if (gDisableStructs[battlerAtk].stockpileCounter >= 2) + score++; + break; + case EFFECT_ROLLOUT: + if (gBattleMons[battlerAtk].status2 & STATUS2_DEFENSE_CURL) + score += 8; + break; + case EFFECT_SWAGGER: + if (HasMoveEffect(battlerAtk, EFFECT_FOUL_PLAY) + || HasMoveEffect(battlerAtk, EFFECT_PSYCH_UP) + || HasMoveEffect(battlerAtk, EFFECT_SPECTRAL_THIEF)) + score++; + + if (AI_DATA->defAbility == ABILITY_CONTRARY) + score += 2; + + IncreaseConfusionScore(battlerAtk, battlerDef, move, &score); + break; + case EFFECT_FLATTER: + if (HasMoveEffect(battlerAtk, EFFECT_PSYCH_UP) + || HasMoveEffect(battlerAtk, EFFECT_SPECTRAL_THIEF)) + score += 2; + + if (AI_DATA->defAbility == ABILITY_CONTRARY) + score += 2; + + IncreaseConfusionScore(battlerAtk, battlerDef, move, &score); + break; + case EFFECT_FURY_CUTTER: + if (!isDoubleBattle && AI_DATA->atkHoldEffect == HOLD_EFFECT_METRONOME) + score += 3; + break; + case EFFECT_ATTRACT: + if (!isDoubleBattle && BattlerWillFaintFromSecondaryDamage(battlerDef, AI_DATA->defAbility) + && GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1) // Target goes first + break; // Don't use if the attract won't have a change to activate + + if (gBattleMons[battlerDef].status1 & STATUS1_ANY + || (gBattleMons[battlerDef].status2 & STATUS2_CONFUSION) + || IsBattlerTrapped(battlerDef, TRUE)) + score += 2; + else + score++; + break; + case EFFECT_SAFEGUARD: + if (!(gFieldStatuses & STATUS_FIELD_MISTY_TERRAIN) || !IsBattlerGrounded(battlerAtk)) + score++; + //if (CountUsablePartyMons(battlerDef) != 0) + //score += 8; + break; + case EFFECT_PURSUIT: + /*TODO + if (IsPredictedToSwitch(battlerDef, battlerAtk)) + score += 3; + else if (IsPredictedToUsePursuitableMove(battlerDef, battlerAtk) && !MoveWouldHitFirst(move, battlerAtk, battlerDef)) //Pursuit against fast U-Turn + score += 3;*/ + break; + case EFFECT_RAPID_SPIN: + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_SPEED, &score); // Gen 8 increases speed + //fallthrough + case EFFECT_DEFOG: + if (gSideStatuses[GetBattlerSide(battlerAtk)] & SIDE_STATUS_HAZARDS_ANY && CountUsablePartyMons(battlerAtk) != 0) + { + score += 3; + break; + } + + switch (move) + { + case MOVE_DEFOG: + if (gSideStatuses[GetBattlerSide(battlerDef)] & (SIDE_STATUS_SCREEEN_ANY | SIDE_STATUS_SAFEGUARD | SIDE_STATUS_MIST)) + { + score += 3; + } + else if (!(gSideStatuses[GetBattlerSide(battlerDef)] & SIDE_STATUS_SPIKES)) //Don't blow away hazards if you set them up + { + if (isDoubleBattle) + { + if (IsHazardMoveEffect(gBattleMoves[AI_DATA->partnerMove].effect) // Partner is going to set up hazards + && GetWhoStrikesFirst(battlerAtk, AI_DATA->battlerAtkPartner, TRUE) == 1) // Partner going first + break; // Don't use Defog if partner is going to set up hazards + } + + // check defog lowering evasion + if (ShouldLowerEvasion(battlerAtk, battlerDef, AI_DATA->defAbility, AI_THINKING_STRUCT->movesetIndex)) + { + if (gBattleMons[battlerDef].statStages[STAT_EVASION] > 7 + || HasMoveWithLowAccuracy(battlerAtk, battlerDef, 90, TRUE, AI_DATA->atkAbility, AI_DATA->defAbility, AI_DATA->atkHoldEffect, AI_DATA->defHoldEffect)) + score += 2; // encourage lowering evasion if they are evasive or we have a move with low accuracy + else + score++; + } + } + break; + case MOVE_RAPID_SPIN: + if (gStatuses3[battlerAtk] & STATUS3_LEECHSEED || gBattleMons[battlerAtk].status2 & STATUS2_WRAPPED) + score += 3; + break; + } + break; + case EFFECT_TORMENT: + break; + case EFFECT_WILL_O_WISP: + IncreaseBurnScore(battlerAtk, battlerDef, move, &score); + break; + case EFFECT_FOLLOW_ME: + if (isDoubleBattle + && move != MOVE_SPOTLIGHT + && !IsBattlerIncapacitated(battlerDef, AI_DATA->defAbility) + && (move != MOVE_RAGE_POWDER || IsAffectedByPowder(battlerDef, AI_DATA->defAbility, AI_DATA->defHoldEffect)) // Rage Powder doesn't affect powder immunities + && IsBattlerAlive(AI_DATA->battlerAtkPartner)) + { + u16 predictedMoveOnPartner = gLastMoves[AI_DATA->battlerAtkPartner]; + if (predictedMoveOnPartner != MOVE_NONE && !IS_MOVE_STATUS(predictedMoveOnPartner)) + score += 3; + } + break; + case EFFECT_NATURE_POWER: + return AI_CheckGoodMove(battlerAtk, battlerDef, GetNaturePowerMove(), score); + case EFFECT_CHARGE: + if (HasDamagingMoveOfType(battlerAtk, TYPE_ELECTRIC)) + score += 2; + + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_SPDEF, &score); + break; + case EFFECT_TAUNT: + if (IS_MOVE_STATUS(predictedMove)) + score += 3; + else if (HasMoveWithSplit(battlerDef, SPLIT_STATUS)) + score += 2; + break; + case EFFECT_TRICK: + case EFFECT_BESTOW: + switch (AI_DATA->atkHoldEffect) + { + case HOLD_EFFECT_CHOICE_SCARF: + score += 2; // assume its beneficial + break; + case HOLD_EFFECT_CHOICE_BAND: + if (!HasMoveWithSplit(battlerDef, SPLIT_PHYSICAL)) + score += 2; + break; + case HOLD_EFFECT_CHOICE_SPECS: + if (!HasMoveWithSplit(battlerDef, SPLIT_SPECIAL)) + score += 2; + break; + case HOLD_EFFECT_TOXIC_ORB: + if (!ShouldPoisonSelf(battlerAtk, AI_DATA->atkAbility) && CanBePoisoned(battlerDef, AI_DATA->defAbility)) + score += 2; + break; + case HOLD_EFFECT_FLAME_ORB: + if (!ShouldBurnSelf(battlerAtk, AI_DATA->atkAbility) && CanBeBurned(battlerAtk, AI_DATA->defAbility)) + score += 2; + break; + case HOLD_EFFECT_BLACK_SLUDGE: + if (!IS_BATTLER_OF_TYPE(battlerDef, TYPE_POISON) && AI_DATA->defAbility != ABILITY_MAGIC_GUARD) + score += 3; + break; + case HOLD_EFFECT_IRON_BALL: + if (!HasMoveEffect(battlerDef, EFFECT_FLING) || !IsBattlerGrounded(battlerDef)) + score += 2; + break; + case HOLD_EFFECT_LAGGING_TAIL: + case HOLD_EFFECT_STICKY_BARB: + score += 3; + break; + case HOLD_EFFECT_UTILITY_UMBRELLA: + if (AI_DATA->atkAbility != ABILITY_SOLAR_POWER && AI_DATA->atkAbility != ABILITY_DRY_SKIN && AI_WeatherHasEffect()) + { + switch (AI_DATA->defAbility) + { + case ABILITY_SWIFT_SWIM: + if (gBattleWeather & WEATHER_RAIN_ANY) + score += 3; // Slow 'em down + break; + case ABILITY_CHLOROPHYLL: + case ABILITY_FLOWER_GIFT: + if (gBattleWeather & WEATHER_SUN_ANY) + score += 3; // Slow 'em down + break; + } + } + break; + case HOLD_EFFECT_EJECT_BUTTON: + //if (!IsRaidBattle() && IsDynamaxed(battlerDef) && gNewBS->dynamaxData.timer[battlerDef] > 1 && + if (HasDamagingMove(battlerAtk) + || (isDoubleBattle && IsBattlerAlive(AI_DATA->battlerAtkPartner) && HasDamagingMove(AI_DATA->battlerAtkPartner))) + score += 2; // Force 'em out next turn + break; + default: + if (move != MOVE_BESTOW && AI_DATA->atkItem == ITEM_NONE) + { + switch (AI_DATA->defHoldEffect) + { + case HOLD_EFFECT_CHOICE_BAND: + break; + case HOLD_EFFECT_TOXIC_ORB: + if (ShouldPoisonSelf(battlerAtk, AI_DATA->atkAbility)) + score += 2; + break; + case HOLD_EFFECT_FLAME_ORB: + if (ShouldBurnSelf(battlerAtk, AI_DATA->atkAbility)) + score += 2; + break; + case HOLD_EFFECT_BLACK_SLUDGE: + if (IS_BATTLER_OF_TYPE(battlerAtk, TYPE_POISON) || AI_DATA->atkAbility == ABILITY_MAGIC_GUARD) + score += 3; + break; + case HOLD_EFFECT_IRON_BALL: + if (HasMoveEffect(battlerAtk, EFFECT_FLING)) + score += 2; + break; + case HOLD_EFFECT_LAGGING_TAIL: + case HOLD_EFFECT_STICKY_BARB: + break; + default: + score++; //other hold effects generally universally good + break; + } + } + } + break; + case EFFECT_ROLE_PLAY: + if (!IsRolePlayBannedAbilityAtk(AI_DATA->atkAbility) + && !IsRolePlayBannedAbility(AI_DATA->defAbility) + && !IsAbilityOfRating(AI_DATA->atkAbility, 5) + && IsAbilityOfRating(AI_DATA->defAbility, 5)) + score += 2; + break; + case EFFECT_INGRAIN: + if (AI_DATA->atkHoldEffect == HOLD_EFFECT_BIG_ROOT) + score += 3; + else + score++; + break; + case EFFECT_SUPERPOWER: + case EFFECT_OVERHEAT: + if (AI_DATA->atkAbility == ABILITY_CONTRARY) + score += 10; + break; + case EFFECT_MAGIC_COAT: + if (IS_MOVE_STATUS(predictedMove) && gBattleMoves[predictedMove].target & (MOVE_TARGET_SELECTED | MOVE_TARGET_OPPONENTS_FIELD | MOVE_TARGET_BOTH)) + score += 3; + break; + case EFFECT_RECYCLE: + if (gBattleStruct->usedHeldItems[battlerAtk] != ITEM_NONE) + score++; + if (IsRecycleEncouragedItem(gBattleStruct->usedHeldItems[battlerAtk])) + score++; + break; + case EFFECT_BRICK_BREAK: + if (gSideStatuses[GetBattlerSide(battlerDef)] & SIDE_STATUS_REFLECT) + score++; + if (gSideStatuses[GetBattlerSide(battlerDef)] & SIDE_STATUS_LIGHTSCREEN) + score++; + if (gSideStatuses[GetBattlerSide(battlerDef)] & SIDE_STATUS_AURORA_VEIL) + score++; + break; + case EFFECT_KNOCK_OFF: + if (CanKnockOffItem(battlerDef, AI_DATA->defItem)) + { + switch (AI_DATA->defHoldEffect) + { + case HOLD_EFFECT_IRON_BALL: + if (HasMoveEffect(battlerDef, EFFECT_FLING)) + score += 4; + break; + case HOLD_EFFECT_LAGGING_TAIL: + case HOLD_EFFECT_STICKY_BARB: + break; + default: + score += 3; + break; + } + } + break; + case EFFECT_SKILL_SWAP: + if (GetAbilityRating(AI_DATA->defAbility) > GetAbilityRating(AI_DATA->atkAbility)) + score++; + break; + case EFFECT_WORRY_SEED: + case EFFECT_GASTRO_ACID: + case EFFECT_SIMPLE_BEAM: + if (IsAbilityOfRating(AI_DATA->defAbility, 5)) + score += 2; + break; + case EFFECT_ENTRAINMENT: + if (IsAbilityOfRating(AI_DATA->defAbility, 5) || GetAbilityRating(AI_DATA->atkAbility) <= 0) + { + if (AI_DATA->defAbility != AI_DATA->atkAbility && !(gStatuses3[battlerDef] & STATUS3_GASTRO_ACID)) + score += 2; + } + break; + case EFFECT_IMPRISON: + if (predictedMove != MOVE_NONE && HasMove(battlerAtk, predictedMove)) + score += 3; + else if (gDisableStructs[battlerAtk].isFirstTurn == 0) + score++; + break; + case EFFECT_REFRESH: + if (gBattleMons[battlerAtk].status1 & STATUS1_ANY) + score += 2; + break; + case EFFECT_PSYCHO_SHIFT: + if (gBattleMons[battlerAtk].status1 & STATUS1_PSN_ANY) + IncreasePoisonScore(battlerAtk, battlerDef, move, &score); + else if (gBattleMons[battlerAtk].status1 & STATUS1_BURN) + IncreaseBurnScore(battlerAtk, battlerDef, move, &score); + else if (gBattleMons[battlerAtk].status1 & STATUS1_PARALYSIS) + IncreaseParalyzeScore(battlerAtk, battlerDef, move, &score); + else if (gBattleMons[battlerAtk].status1 & STATUS1_SLEEP) + IncreaseSleepScore(battlerAtk, battlerDef, move, &score); + break; + case EFFECT_GRUDGE: + break; + case EFFECT_SNATCH: + if (predictedMove != MOVE_NONE && TestMoveFlags(predictedMove, FLAG_SNATCH_AFFECTED)) + score += 3; // Steal move + break; + case EFFECT_MUD_SPORT: + if (!HasMoveWithType(battlerAtk, TYPE_ELECTRIC) && HasMoveWithType(battlerDef, TYPE_ELECTRIC)) + score++; + break; + case EFFECT_WATER_SPORT: + if (!HasMoveWithType(battlerAtk, TYPE_FIRE) && (HasMoveWithType(battlerDef, TYPE_FIRE))) + score++; + break; + case EFFECT_TICKLE: + if (gBattleMons[battlerDef].statStages[STAT_DEF] > 4 && HasMoveWithSplit(battlerAtk, SPLIT_PHYSICAL) + && AI_DATA->defAbility != ABILITY_CONTRARY && ShouldLowerDefense(battlerAtk, battlerDef, AI_DATA->defAbility, AI_THINKING_STRUCT->movesetIndex)) + { + score += 2; + } + else if (ShouldLowerAttack(battlerAtk, battlerDef, AI_DATA->defAbility, AI_THINKING_STRUCT->movesetIndex)) + { + score += 2; + } + break; + case EFFECT_COSMIC_POWER: + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_DEF, &score); + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_SPDEF, &score); + break; + case EFFECT_BULK_UP: + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_ATK, &score); + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_DEF, &score); + break; + case EFFECT_CALM_MIND: + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_SPATK, &score); + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_SPDEF, &score); + break; + case EFFECT_GEOMANCY: + if (AI_DATA->atkHoldEffect == HOLD_EFFECT_POWER_HERB) + score += 3; + //fallthrough + case EFFECT_QUIVER_DANCE: + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_SPEED, &score); + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_SPATK, &score); + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_SPDEF, &score); + break; + case EFFECT_SHELL_SMASH: + if (AI_DATA->atkHoldEffect == HOLD_EFFECT_POWER_HERB) + score += 3; + + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_SPEED, &score); + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_SPATK, &score); + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_ATK, &score); + break; + case EFFECT_DRAGON_DANCE: + case EFFECT_SHIFT_GEAR: + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_SPEED, &score); + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_ATK, &score); + break; + case EFFECT_GUARD_SWAP: + if (gBattleMons[battlerDef].statStages[STAT_DEF] > gBattleMons[battlerAtk].statStages[STAT_DEF] + && gBattleMons[battlerDef].statStages[STAT_SPDEF] >= gBattleMons[battlerAtk].statStages[STAT_SPDEF]) + score++; + else if (gBattleMons[battlerDef].statStages[STAT_SPDEF] > gBattleMons[battlerAtk].statStages[STAT_SPDEF] + && gBattleMons[battlerDef].statStages[STAT_DEF] >= gBattleMons[battlerAtk].statStages[STAT_DEF]) + score++; + break; + case EFFECT_POWER_SWAP: + if (gBattleMons[battlerDef].statStages[STAT_ATK] > gBattleMons[battlerAtk].statStages[STAT_ATK] + && gBattleMons[battlerDef].statStages[STAT_SPATK] >= gBattleMons[battlerAtk].statStages[STAT_SPATK]) + score++; + else if (gBattleMons[battlerDef].statStages[STAT_SPATK] > gBattleMons[battlerAtk].statStages[STAT_SPATK] + && gBattleMons[battlerDef].statStages[STAT_ATK] >= gBattleMons[battlerAtk].statStages[STAT_ATK]) + score++; + break; + case EFFECT_POWER_TRICK: + if (!(gStatuses3[battlerAtk] & STATUS3_POWER_TRICK)) + { + if (gBattleMons[battlerAtk].defense > gBattleMons[battlerAtk].attack && HasMoveWithSplit(battlerAtk, SPLIT_PHYSICAL)) + score += 2; + break; + } + break; + case EFFECT_HEART_SWAP: + { + bool32 hasHigherStat = FALSE; + //Only use if all target stats are >= attacker stats to prevent infinite loop + for (i = STAT_ATK; i < NUM_BATTLE_STATS; i++) + { + if (gBattleMons[battlerDef].statStages[i] < gBattleMons[battlerAtk].statStages[i]) + break; + if (gBattleMons[battlerDef].statStages[i] > gBattleMons[battlerAtk].statStages[i]) + hasHigherStat = TRUE; + } + if (hasHigherStat && i == NUM_BATTLE_STATS) + score++; + } + break; + case EFFECT_SPEED_SWAP: + // TODO this is cheating a bit... + if (gBattleMons[battlerDef].speed > gBattleMons[battlerAtk].speed) + score += 3; + break; + case EFFECT_GUARD_SPLIT: + { + // TODO also kind of cheating... + u16 newDefense = (gBattleMons[battlerAtk].defense + gBattleMons[battlerDef].defense) / 2; + u16 newSpDef = (gBattleMons[battlerAtk].spDefense + gBattleMons[battlerDef].spDefense) / 2; + + if ((newDefense > gBattleMons[battlerAtk].defense && newSpDef >= gBattleMons[battlerAtk].spDefense) + || (newSpDef > gBattleMons[battlerAtk].spDefense && newDefense >= gBattleMons[battlerAtk].defense)) + score++; + } + break; + case EFFECT_POWER_SPLIT: + { + u16 newAttack = (gBattleMons[battlerAtk].attack + gBattleMons[battlerDef].attack) / 2; + u16 newSpAtk = (gBattleMons[battlerAtk].spAttack + gBattleMons[battlerDef].spAttack) / 2; + + if ((newAttack > gBattleMons[battlerAtk].attack && newSpAtk >= gBattleMons[battlerAtk].spAttack) + || (newSpAtk > gBattleMons[battlerAtk].spAttack && newAttack >= gBattleMons[battlerAtk].attack)) + score++; + } + break; + case EFFECT_BUG_BITE: // And pluck + if (gBattleMons[battlerDef].status2 & STATUS2_SUBSTITUTE || AI_DATA->defAbility == ABILITY_STICKY_HOLD) + break; + else if (ItemId_GetPocket(AI_DATA->defItem) == POCKET_BERRIES) + score += 3; + break; + case EFFECT_INCINERATE: + if (gBattleMons[battlerDef].status2 & STATUS2_SUBSTITUTE || AI_DATA->defAbility == ABILITY_STICKY_HOLD) + break; + else if (ItemId_GetPocket(AI_DATA->defItem) == POCKET_BERRIES || AI_DATA->defHoldEffect == HOLD_EFFECT_GEMS) + score += 3; + break; + case EFFECT_SMACK_DOWN: + if (!IsBattlerGrounded(battlerDef)) + score += 3; + break; + case EFFECT_SLEEP_HIT: // Relic Song + #if (defined SPECIES_MELOETTA && defined SPECIES_MELOETTA_PIROUETTE) + if (AI_DATA->atkSpecies == SPECIES_MELOETTA && gBattleMons[battlerDef].defense < gBattleMons[battlerDef].spDefense) + score += 3; // Change to pirouette if can do more damage + else if (AI_DATA->atkSpecies == SPECIES_MELOETTA_PIROUETTE && gBattleMons[battlerDef].spDefense < gBattleMons[battlerDef].defense) + score += 3; // Change to Aria if can do more damage + #endif + break; + case EFFECT_ELECTRIC_TERRAIN: + case EFFECT_MISTY_TERRAIN: + if (gStatuses3[battlerAtk] & STATUS3_YAWN && IsBattlerGrounded(battlerAtk)) + score += 10; + //fallthrough + case EFFECT_GRASSY_TERRAIN: + case EFFECT_PSYCHIC_TERRAIN: + score += 2; + if (AI_DATA->atkHoldEffect == HOLD_EFFECT_TERRAIN_EXTENDER) + score += 2; + break; + case EFFECT_PLEDGE: + if (isDoubleBattle) + { + if (HasMoveEffect(AI_DATA->battlerAtkPartner, EFFECT_PLEDGE)) + score += 3; // Partner might use pledge move + } + break; + case EFFECT_TRICK_ROOM: + if (!(gFieldStatuses & STATUS_FIELD_TRICK_ROOM) && GetBattlerSideSpeedAverage(battlerAtk) < GetBattlerSideSpeedAverage(battlerDef)) + score += 3; + else if ((gFieldStatuses & STATUS_FIELD_TRICK_ROOM) && GetBattlerSideSpeedAverage(battlerAtk) >= GetBattlerSideSpeedAverage(battlerDef)) + score += 3; + break; + case EFFECT_MAGIC_ROOM: + score++; + if (AI_DATA->atkHoldEffect == HOLD_EFFECT_NONE && AI_DATA->defHoldEffect != HOLD_EFFECT_NONE) + score++; + if (isDoubleBattle && AI_DATA->atkPartnerHoldEffect == HOLD_EFFECT_NONE && AI_DATA->defPartnerHoldEffect != HOLD_EFFECT_NONE) + score++; + break; + case EFFECT_WONDER_ROOM: + if ((HasMoveWithSplit(battlerDef, SPLIT_PHYSICAL) && gBattleMons[battlerAtk].defense < gBattleMons[battlerAtk].spDefense) + || (HasMoveWithSplit(battlerDef, SPLIT_SPECIAL) && gBattleMons[battlerAtk].spDefense < gBattleMons[battlerAtk].defense)) + score += 2; + break; + case EFFECT_GRAVITY: + if (!(gFieldStatuses & STATUS_FIELD_GRAVITY)) + { + if (HasSleepMoveWithLowAccuracy(battlerAtk, battlerDef)) // Has Gravity for a move like Hypnosis + IncreaseSleepScore(battlerAtk, battlerDef, move, &score); + else if (HasMoveWithLowAccuracy(battlerAtk, battlerDef, 90, FALSE, AI_DATA->atkAbility, AI_DATA->defAbility, AI_DATA->atkHoldEffect, AI_DATA->defHoldEffect)) + score += 2; + else + score++; + } + break; + case EFFECT_ION_DELUGE: + if ((AI_DATA->atkAbility == ABILITY_VOLT_ABSORB + || AI_DATA->atkAbility == ABILITY_MOTOR_DRIVE + || AI_DATA->atkAbility == ABILITY_LIGHTNING_ROD) + && gBattleMoves[predictedMove].type == TYPE_NORMAL) + score += 2; + break; + case EFFECT_FLING: + /* TODO + switch (gFlingTable[AI_DATA->atkItem].effect) + { + case MOVE_EFFECT_BURN: + IncreaseBurnScore(battlerAtk, battlerDef, move, &score); + break; + case MOVE_EFFECT_FLINCH: + score += ShouldTryToFlinch(battlerAtk, battlerDef, AI_DATA->atkAbility, AI_DATA->defAbility, move); + break; + case MOVE_EFFECT_PARALYSIS: + IncreaseParalyzeScore(battlerAtk, battlerDef, move, &score); + break; + case MOVE_EFFECT_POISON: + case MOVE_EFFECT_TOXIC: + IncreasePoisonScore(battlerAtk, battlerDef, move, &score); + break; + case MOVE_EFFECT_FREEZE: + if (AI_CanFreeze(battlerAtk, battlerDef)) + score += 3; + break; + }*/ + break; + case EFFECT_FEINT: + if (gBattleMoves[predictedMove].effect == EFFECT_PROTECT) + score += 3; + break; + case EFFECT_EMBARGO: + if (AI_DATA->defHoldEffect != HOLD_EFFECT_NONE) + score++; + break; + case EFFECT_POWDER: + if (predictedMove != MOVE_NONE && !IS_MOVE_STATUS(predictedMove) && gBattleMoves[predictedMove].type == TYPE_FIRE) + score += 3; + break; + case EFFECT_TELEKINESIS: + if (HasMoveWithLowAccuracy(battlerAtk, battlerDef, 90, FALSE, AI_DATA->atkAbility, AI_DATA->defAbility, AI_DATA->atkHoldEffect, AI_DATA->defHoldEffect) + || !IsBattlerGrounded(battlerDef)) + score++; + break; + case EFFECT_THROAT_CHOP: + if (predictedMove != MOVE_NONE && TestMoveFlags(predictedMove, FLAG_SOUND) && GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0) + score += 3; // Ai goes first and predicts the target will use a sound move + else if (TestMoveFlagsInMoveset(battlerDef, FLAG_SOUND)) + score += 3; + break; + case EFFECT_HEAL_BLOCK: + if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0 && predictedMove != MOVE_NONE && IsHealingMoveEffect(gBattleMoves[predictedMove].effect)) + score += 3; // Try to cancel healing move + else if (HasHealingEffect(battlerDef) || AI_DATA->defHoldEffect == HOLD_EFFECT_LEFTOVERS + || (AI_DATA->defHoldEffect == HOLD_EFFECT_BLACK_SLUDGE && IS_BATTLER_OF_TYPE(battlerDef, TYPE_POISON))) + score += 2; + break; + case EFFECT_SOAK: + if (HasMoveWithType(battlerAtk, TYPE_ELECTRIC) || HasMoveWithType(battlerAtk, TYPE_GRASS) || HasMoveEffect(battlerAtk, EFFECT_FREEZE_DRY)) + score += 2; // Get some super effective moves + break; + case EFFECT_THIRD_TYPE: + if (AI_DATA->defAbility == ABILITY_WONDER_GUARD) + score += 2; // Give target more weaknesses + break; + case EFFECT_ELECTRIFY: + if (predictedMove != MOVE_NONE && gBattleMoves[predictedMove].type == TYPE_NORMAL + && (AI_DATA->atkAbility == ABILITY_VOLT_ABSORB + || AI_DATA->atkAbility == ABILITY_MOTOR_DRIVE + || AI_DATA->atkAbility == ABILITY_LIGHTNING_ROD)) + { + score += 3; + } + break; + case EFFECT_TOPSY_TURVY: + if (CountPositiveStatStages(battlerDef) > CountNegativeStatStages(battlerDef)) + score++; + break; + case EFFECT_FAIRY_LOCK: + if (!IsBattlerTrapped(battlerDef, TRUE)) + { + if (ShouldTrap(battlerAtk, battlerDef, move)) + score += 8; + } + break; + case EFFECT_QUASH: + if (isDoubleBattle + && GetWhoStrikesFirst(AI_DATA->battlerAtkPartner, battlerDef, TRUE) == 1) // Attacker partner wouldn't go before target + score++; + break; + case EFFECT_TAILWIND: + if (GetBattlerSideSpeedAverage(battlerAtk) < GetBattlerSideSpeedAverage(battlerDef)) + score += 2; + break; + case EFFECT_LUCKY_CHANT: + if (!isDoubleBattle) + { + score++; + } + else + { + if (CountUsablePartyMons(battlerDef) > 0) + score += 8; + } + 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 + { + if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0) // Attacker goes first + { + if (gBattleMoves[predictedMove].type == TYPE_GROUND) + score += 3; // Cause the enemy's move to fail + break; + } + else // Opponent Goes First + { + if (HasDamagingMoveOfType(battlerDef, TYPE_GROUND)) + score += 2; + break; + } + } + break; + case EFFECT_CAMOUFLAGE: + if (predictedMove != MOVE_NONE && GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0 // Attacker goes first + && !IS_MOVE_STATUS(move) && AI_GetTypeEffectiveness(predictedMove, battlerDef, battlerAtk) != AI_EFFECTIVENESS_x0) + score++; + break; + case EFFECT_FLAME_BURST: + if (isDoubleBattle) + { + if (IsBattlerAlive(AI_DATA->battlerDefPartner) + && GetHealthPercentage(AI_DATA->battlerDefPartner) < 12 + && AI_DATA->defPartnerAbility != ABILITY_MAGIC_GUARD + && !IS_BATTLER_OF_TYPE(AI_DATA->battlerDefPartner, TYPE_FIRE)) + score++; + } + break; + case EFFECT_TOXIC_THREAD: + IncreasePoisonScore(battlerAtk, battlerDef, move, &score); + IncreaseStatUpScore(battlerAtk, battlerDef, STAT_SPEED, &score); + break; + case EFFECT_TWO_TURNS_ATTACK: + case EFFECT_SKULL_BASH: + case EFFECT_SOLARBEAM: + if (AI_DATA->atkHoldEffect == HOLD_EFFECT_POWER_HERB) + score += 2; + break; + case EFFECT_COUNTER: + if (!IsBattlerIncapacitated(battlerDef, AI_DATA->defAbility) && predictedMove != MOVE_NONE) + { + if (gDisableStructs[battlerDef].tauntTimer != 0) + score++; // target must use damaging move + if (GetMoveDamageResult(predictedMove) >= MOVE_POWER_GOOD && GetBattleMoveSplit(predictedMove) == SPLIT_PHYSICAL) + score += 3; + } + break; + case EFFECT_MIRROR_COAT: + if (!IsBattlerIncapacitated(battlerDef, AI_DATA->defAbility) && predictedMove != MOVE_NONE) + { + if (gDisableStructs[battlerDef].tauntTimer != 0) + score++; // target must use damaging move + if (GetMoveDamageResult(predictedMove) >= MOVE_POWER_GOOD && GetBattleMoveSplit(predictedMove) == SPLIT_SPECIAL) + score += 3; + } + break; + case EFFECT_FLAIL: + if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0) // Ai goes first + { + if (GetHealthPercentage(battlerAtk) < 20) + score++; + else if (GetHealthPercentage(battlerAtk) < 8) + score += 2; + } + break; + case EFFECT_SHORE_UP: + if (AI_WeatherHasEffect() && (gBattleWeather & WEATHER_SANDSTORM_ANY) + && ShouldRecover(battlerAtk, battlerDef, move, 67)) + score += 3; + else if (ShouldRecover(battlerAtk, battlerDef, move, 50)) + score += 2; + break; + case EFFECT_FACADE: + if (gBattleMons[battlerAtk].status1 & (STATUS1_POISON | STATUS1_BURN | STATUS1_PARALYSIS | STATUS1_TOXIC_POISON)) + score++; + break; + case EFFECT_FOCUS_PUNCH: + if (!isDoubleBattle && effectiveness > AI_EFFECTIVENESS_x0_5) + { + if (IsBattlerIncapacitated(battlerDef, AI_DATA->defAbility)) + score += 2; + else if (gBattleMons[battlerDef].status2 & (STATUS2_INFATUATION | STATUS2_CONFUSION)) + score++; + } + break; + case EFFECT_SMELLINGSALT: + if (gBattleMons[battlerDef].status1 & STATUS1_PARALYSIS) + score += 2; + break; + case EFFECT_WAKE_UP_SLAP: + if (gBattleMons[battlerDef].status1 & STATUS1_SLEEP) + score += 2; + break; + case EFFECT_REVENGE: + if (!(gBattleMons[battlerDef].status1 & STATUS1_SLEEP) + && !(gBattleMons[battlerDef].status2 & (STATUS2_INFATUATION | STATUS2_CONFUSION))) + score += 2; + break; + case EFFECT_ENDEAVOR: + if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1) // Opponent faster + { + if (GetHealthPercentage(battlerAtk) < 40) + score++; + } + else if (GetHealthPercentage(battlerAtk) < 50) + { + score++; + } + break; + //case EFFECT_EXTREME_EVOBOOST: // TODO + //break; + //case EFFECT_CLANGOROUS_SOUL: // TODO + //break; + //case EFFECT_NO_RETREAT: // TODO + //break; + //case EFFECT_SKY_DROP + //break; + } // move effect checks - - - - } // move effect switch + return score; } -static u8 AI_SetupFirstTurn(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) +static s16 AI_SetupFirstTurn(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) { } -static u8 AI_Risky(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) +static s16 AI_Risky(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) { } -static u8 AI_PreferStrongestMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) +static s16 AI_PreferStrongestMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) { } -static u8 AI_PreferBatonPass(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) +static s16 AI_PreferBatonPass(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) { } -static u8 AI_DoubleBattle(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) +static s16 AI_DoubleBattle(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) { } -static u8 AI_HPAware(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) +static s16 AI_HPAware(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) { } @@ -4867,7 +5946,7 @@ static void AI_Watch(void) AI_THINKING_STRUCT->aiAction |= (AI_ACTION_DONE | AI_ACTION_WATCH | AI_ACTION_DO_NOT_ATTACK); } -static u8 AI_Roaming(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) +static s16 AI_Roaming(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) { if (IsBattlerTrapped(battlerAtk, FALSE)) return score; @@ -4876,7 +5955,7 @@ static u8 AI_Roaming(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) return score; } -static u8 AI_Safari(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) +static s16 AI_Safari(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) { u8 safariFleeRate = gBattleStruct->safariEscapeFactor * 5; // Safari flee rate, from 0-20. @@ -4888,7 +5967,7 @@ static u8 AI_Safari(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) return score; } -static u8 AI_FirstBattle(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) +static s16 AI_FirstBattle(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) { if (GetHealthPercentage(battlerDef) <= 20) AI_Flee(); diff --git a/src/battle_ai_switch_items.c b/src/battle_ai_switch_items.c index 34a42b623..9002eaefe 100644 --- a/src/battle_ai_switch_items.c +++ b/src/battle_ai_switch_items.c @@ -417,7 +417,7 @@ static bool8 FindMonWithFlagsAndSuperEffective(u16 flags, u8 moduloPercent) return FALSE; } -static bool8 ShouldSwitch(void) +bool32 ShouldSwitch(void) { u8 battlerIn1, battlerIn2; s32 firstId; diff --git a/src/battle_ai_util.c b/src/battle_ai_util.c index fa8f38ea2..485d179fe 100644 --- a/src/battle_ai_util.c +++ b/src/battle_ai_util.c @@ -4,6 +4,7 @@ #include "battle_anim.h" #include "battle_ai_util.h" #include "battle_ai_script_commands.h" +#include "battle_ai_switch_items.h" #include "battle_factory.h" #include "battle_setup.h" #include "data.h" @@ -17,8 +18,337 @@ #include "constants/battle_move_effects.h" #include "constants/hold_effects.h" #include "constants/moves.h" +#include "constants/items.h" // Const Data +static const s8 sAiAbilityRatings[ABILITIES_COUNT] = +{ + [ABILITY_ADAPTABILITY] = 8, + [ABILITY_AFTERMATH] = 5, + [ABILITY_AERILATE] = 8, + [ABILITY_AIR_LOCK] = 5, + [ABILITY_ANALYTIC] = 5, + [ABILITY_ANGER_POINT] = 4, + [ABILITY_ANTICIPATION] = 2, + [ABILITY_ARENA_TRAP] = 9, + [ABILITY_AROMA_VEIL] = 3, + [ABILITY_AURA_BREAK] = 3, + [ABILITY_BAD_DREAMS] = 4, + [ABILITY_BATTERY] = 0, + [ABILITY_BATTLE_ARMOR] = 2, + [ABILITY_BATTLE_BOND] = 6, + [ABILITY_BEAST_BOOST] = 7, + [ABILITY_BERSERK] = 5, + [ABILITY_BIG_PECKS] = 1, + [ABILITY_BLAZE] = 5, + [ABILITY_BULLETPROOF] = 7, + [ABILITY_CHEEK_POUCH] = 4, + [ABILITY_CHLOROPHYLL] = 6, + [ABILITY_CLEAR_BODY] = 4, + [ABILITY_CLOUD_NINE] = 5, + [ABILITY_COLOR_CHANGE] = 2, + [ABILITY_COMATOSE] = 6, + [ABILITY_COMPETITIVE] = 5, + [ABILITY_COMPOUND_EYES] = 7, + [ABILITY_CONTRARY] = 8, + [ABILITY_CORROSION] = 5, + [ABILITY_CURSED_BODY] = 4, + [ABILITY_CUTE_CHARM] = 2, + [ABILITY_DAMP] = 2, + [ABILITY_DANCER] = 5, + [ABILITY_DARK_AURA] = 6, + [ABILITY_DAZZLING] = 5, + [ABILITY_DEFEATIST] = -1, + [ABILITY_DEFIANT] = 5, + [ABILITY_DELTA_STREAM] = 10, + [ABILITY_DESOLATE_LAND] = 10, + [ABILITY_DISGUISE] = 8, + [ABILITY_DOWNLOAD] = 7, + [ABILITY_DRIZZLE] = 9, + [ABILITY_DROUGHT] = 9, + [ABILITY_DRY_SKIN] = 6, + [ABILITY_EARLY_BIRD] = 4, + [ABILITY_EFFECT_SPORE] = 4, + [ABILITY_ELECTRIC_SURGE] = 8, + [ABILITY_EMERGENCY_EXIT] = 3, + [ABILITY_FAIRY_AURA] = 6, + [ABILITY_FILTER] = 6, + [ABILITY_FLAME_BODY] = 4, + [ABILITY_FLARE_BOOST] = 5, + [ABILITY_FLASH_FIRE] = 6, + [ABILITY_FLOWER_GIFT] = 4, + [ABILITY_FLOWER_VEIL] = 0, + [ABILITY_FLUFFY] = 5, + [ABILITY_FORECAST] = 6, + [ABILITY_FOREWARN] = 2, + [ABILITY_FRIEND_GUARD] = 0, + [ABILITY_FRISK] = 3, + [ABILITY_FULL_METAL_BODY] = 4, + [ABILITY_FUR_COAT] = 7, + [ABILITY_GALE_WINGS] = 6, + [ABILITY_GALVANIZE] = 8, + [ABILITY_GLUTTONY] = 3, + [ABILITY_GOOEY] = 5, + [ABILITY_GRASS_PELT] = 2, + [ABILITY_GRASSY_SURGE] = 8, + [ABILITY_GUTS] = 6, + [ABILITY_HARVEST] = 5, + [ABILITY_HEALER] = 0, + [ABILITY_HEATPROOF] = 5, + [ABILITY_HEAVY_METAL] = -1, + [ABILITY_HONEY_GATHER] = 0, + [ABILITY_HUGE_POWER] = 10, + [ABILITY_HUSTLE] = 7, + [ABILITY_HYDRATION] = 4, + [ABILITY_HYPER_CUTTER] = 3, + [ABILITY_ICE_BODY] = 3, + [ABILITY_ILLUMINATE] = 0, + [ABILITY_ILLUSION] = 8, + [ABILITY_IMMUNITY] = 4, + [ABILITY_IMPOSTER] = 9, + [ABILITY_INFILTRATOR] = 6, + [ABILITY_INNARDS_OUT] = 5, + [ABILITY_INNER_FOCUS] = 2, + [ABILITY_INSOMNIA] = 4, + [ABILITY_INTIMIDATE] = 7, + [ABILITY_IRON_BARBS] = 6, + [ABILITY_IRON_FIST] = 6, + [ABILITY_JUSTIFIED] = 4, + [ABILITY_KEEN_EYE] = 1, + [ABILITY_KLUTZ] = -1, + [ABILITY_LEAF_GUARD] = 2, + [ABILITY_LEVITATE] = 7, + [ABILITY_LIGHT_METAL] = 2, + [ABILITY_LIGHTNING_ROD] = 7, + [ABILITY_LIMBER] = 3, + [ABILITY_LIQUID_OOZE] = 3, + [ABILITY_LIQUID_VOICE] = 5, + [ABILITY_LONG_REACH] = 3, + [ABILITY_MAGIC_BOUNCE] = 9, + [ABILITY_MAGIC_GUARD] = 9, + [ABILITY_MAGICIAN] = 3, + [ABILITY_MAGMA_ARMOR] = 1, + [ABILITY_MAGNET_PULL] = 9, + [ABILITY_MARVEL_SCALE] = 5, + [ABILITY_MEGA_LAUNCHER] = 7, + [ABILITY_MERCILESS] = 4, + [ABILITY_MINUS] = 0, + [ABILITY_MISTY_SURGE] = 8, + [ABILITY_MOLD_BREAKER] = 7, + [ABILITY_MOODY] = 10, + [ABILITY_MOTOR_DRIVE] = 6, + [ABILITY_MOXIE] = 7, + [ABILITY_MULTISCALE] = 8, + [ABILITY_MULTITYPE] = 8, + [ABILITY_MUMMY] = 5, + [ABILITY_NATURAL_CURE] = 7, + [ABILITY_NEUROFORCE] = 6, + [ABILITY_NO_GUARD] = 8, + [ABILITY_NORMALIZE] = -1, + [ABILITY_OBLIVIOUS] = 2, + [ABILITY_OVERCOAT] = 5, + [ABILITY_OVERGROW] = 5, + [ABILITY_OWN_TEMPO] = 3, + [ABILITY_PARENTAL_BOND] = 10, + [ABILITY_PICKUP] = 1, + [ABILITY_PICKPOCKET] = 3, + [ABILITY_PIXILATE] = 8, + [ABILITY_PLUS] = 0, + [ABILITY_POISON_HEAL] = 8, + [ABILITY_POISON_POINT] = 4, + [ABILITY_POISON_TOUCH] = 4, + //[ABILITY_PORTAL_POWER] = 8, + [ABILITY_POWER_CONSTRUCT] = 10, + [ABILITY_POWER_OF_ALCHEMY] = 0, + [ABILITY_PRANKSTER] = 8, + [ABILITY_PRESSURE] = 5, + [ABILITY_PRIMORDIAL_SEA] = 10, + [ABILITY_PRISM_ARMOR] = 6, + [ABILITY_PROTEAN] = 8, + [ABILITY_PSYCHIC_SURGE] = 8, + [ABILITY_PURE_POWER] = 10, + [ABILITY_QUEENLY_MAJESTY] = 6, + [ABILITY_QUICK_FEET] = 5, + [ABILITY_RAIN_DISH] = 3, + [ABILITY_RATTLED] = 3, + [ABILITY_RECEIVER] = 0, + [ABILITY_RECKLESS] = 6, + [ABILITY_REFRIGERATE] = 8, + [ABILITY_REGENERATOR] = 8, + [ABILITY_RIVALRY] = 1, + [ABILITY_RKS_SYSTEM] = 8, + [ABILITY_ROCK_HEAD] = 5, + [ABILITY_ROUGH_SKIN] = 6, + [ABILITY_RUN_AWAY] = 0, + [ABILITY_SAND_FORCE] = 4, + [ABILITY_SAND_RUSH] = 6, + [ABILITY_SAND_STREAM] = 9, + [ABILITY_SAND_VEIL] = 3, + [ABILITY_SAP_SIPPER] = 7, + [ABILITY_SCHOOLING] = 6, + [ABILITY_SCRAPPY] = 6, + [ABILITY_SERENE_GRACE] = 8, + [ABILITY_SHADOW_SHIELD] = 8, + [ABILITY_SHADOW_TAG] = 10, + [ABILITY_SHED_SKIN] = 7, + [ABILITY_SHEER_FORCE] = 8, + [ABILITY_SHELL_ARMOR] = 2, + [ABILITY_SHIELD_DUST] = 5, + [ABILITY_SHIELDS_DOWN] = 6, + [ABILITY_SIMPLE] = 8, + [ABILITY_SKILL_LINK] = 7, + [ABILITY_SLOW_START] = -2, + [ABILITY_SLUSH_RUSH] = 5, + [ABILITY_SNIPER] = 3, + [ABILITY_SNOW_CLOAK] = 3, + [ABILITY_SNOW_WARNING] = 8, + [ABILITY_SOLAR_POWER] = 3, + [ABILITY_SOLID_ROCK] = 6, + [ABILITY_SOUL_HEART] = 7, + [ABILITY_SOUNDPROOF] = 4, + [ABILITY_SPEED_BOOST] = 9, + [ABILITY_STAKEOUT] = 6, + [ABILITY_STALL] = -1, + [ABILITY_STAMINA] = 6, + [ABILITY_STANCE_CHANGE] = 10, + [ABILITY_STATIC] = 4, + [ABILITY_STEADFAST] = 2, + [ABILITY_STEELWORKER] = 6, + [ABILITY_STENCH] = 1, + [ABILITY_STICKY_HOLD] = 3, + [ABILITY_STORM_DRAIN] = 7, + [ABILITY_STRONG_JAW] = 6, + [ABILITY_STURDY] = 6, + [ABILITY_SUCTION_CUPS] = 2, + [ABILITY_SUPER_LUCK] = 3, + [ABILITY_SURGE_SURFER] = 4, + [ABILITY_SWARM] = 5, + [ABILITY_SWEET_VEIL] = 4, + [ABILITY_SWIFT_SWIM] = 6, + [ABILITY_SYMBIOSIS] = 0, + [ABILITY_SYNCHRONIZE] = 4, + [ABILITY_TANGLED_FEET] = 2, + [ABILITY_TANGLING_HAIR] = 5, + [ABILITY_TECHNICIAN] = 8, + [ABILITY_TELEPATHY] = 0, + [ABILITY_TERAVOLT] = 7, + [ABILITY_THICK_FAT] = 7, + [ABILITY_TINTED_LENS] = 7, + [ABILITY_TORRENT] = 5, + [ABILITY_TOXIC_BOOST] = 6, + [ABILITY_TOUGH_CLAWS] = 7, + [ABILITY_TRACE] = 6, + [ABILITY_TRIAGE] = 7, + [ABILITY_TRUANT] = -2, + [ABILITY_TURBOBLAZE] = 7, + [ABILITY_UNAWARE] = 6, + [ABILITY_UNBURDEN] = 7, + [ABILITY_UNNERVE] = 3, + [ABILITY_VICTORY_STAR] = 6, + [ABILITY_VITAL_SPIRIT] = 4, + [ABILITY_VOLT_ABSORB] = 7, + [ABILITY_WATER_ABSORB] = 7, + [ABILITY_WATER_BUBBLE] = 8, + [ABILITY_WATER_COMPACTION] = 4, + [ABILITY_WATER_VEIL] = 4, + [ABILITY_WEAK_ARMOR] = 2, + [ABILITY_WHITE_SMOKE] = 4, + [ABILITY_WIMP_OUT] = 3, + [ABILITY_WONDER_GUARD] = 10, + [ABILITY_WONDER_SKIN] = 4, + [ABILITY_ZEN_MODE] = -1, + [ABILITY_INTREPID_SWORD] = 3, + [ABILITY_DAUNTLESS_SHIELD] = 3, + [ABILITY_BALL_FETCH] = 0, + [ABILITY_COTTON_DOWN] = 3, + [ABILITY_MIRROR_ARMOR] = 6, + [ABILITY_GULP_MISSILE] = 3, + [ABILITY_STALWART] = 2, + [ABILITY_PROPELLER_TAIL] = 2, + [ABILITY_STEAM_ENGINE] = 3, + [ABILITY_PUNK_ROCK] = 2, + [ABILITY_SAND_SPIT] = 5, + [ABILITY_ICE_SCALES] = 7, + [ABILITY_RIPEN] = 4, + [ABILITY_ICE_FACE] = 4, + [ABILITY_POWER_SPOT] = 2, + [ABILITY_MIMICRY] = 2, + [ABILITY_SCREEN_CLEANER] = 3, + [ABILITY_NEUTRALIZING_GAS] = 5, + [ABILITY_HUNGER_SWITCH] = 2, + [ABILITY_PASTEL_VEIL] = 4, + [ABILITY_STEELY_SPIRIT] = 2, + [ABILITY_PERISH_BODY] = -1, + [ABILITY_WANDERING_SPIRIT] = 2, + [ABILITY_GORILLA_TACTICS] = 4, +}; + +static const u16 sEncouragedEncoreEffects[] = +{ + EFFECT_DREAM_EATER, + EFFECT_ATTACK_UP, + EFFECT_DEFENSE_UP, + EFFECT_SPEED_UP, + EFFECT_SPECIAL_ATTACK_UP, + EFFECT_HAZE, + EFFECT_ROAR, + EFFECT_CONVERSION, + EFFECT_TOXIC, + EFFECT_LIGHT_SCREEN, + EFFECT_REST, + EFFECT_SUPER_FANG, + EFFECT_SPECIAL_DEFENSE_UP_2, + EFFECT_CONFUSE, + EFFECT_POISON, + EFFECT_PARALYZE, + EFFECT_LEECH_SEED, + EFFECT_DO_NOTHING, + EFFECT_ATTACK_UP_2, + EFFECT_ENCORE, + EFFECT_CONVERSION_2, + EFFECT_LOCK_ON, + EFFECT_HEAL_BELL, + EFFECT_MEAN_LOOK, + EFFECT_NIGHTMARE, + EFFECT_PROTECT, + EFFECT_SKILL_SWAP, + EFFECT_FORESIGHT, + EFFECT_PERISH_SONG, + EFFECT_SANDSTORM, + EFFECT_ENDURE, + EFFECT_SWAGGER, + EFFECT_ATTRACT, + EFFECT_SAFEGUARD, + EFFECT_RAIN_DANCE, + EFFECT_SUNNY_DAY, + EFFECT_BELLY_DRUM, + EFFECT_PSYCH_UP, + EFFECT_FUTURE_SIGHT, + EFFECT_FAKE_OUT, + EFFECT_STOCKPILE, + EFFECT_SPIT_UP, + EFFECT_SWALLOW, + EFFECT_HAIL, + EFFECT_TORMENT, + EFFECT_WILL_O_WISP, + EFFECT_FOLLOW_ME, + EFFECT_CHARGE, + EFFECT_TRICK, + EFFECT_ROLE_PLAY, + EFFECT_INGRAIN, + EFFECT_RECYCLE, + EFFECT_KNOCK_OFF, + EFFECT_SKILL_SWAP, + EFFECT_IMPRISON, + EFFECT_REFRESH, + EFFECT_GRUDGE, + EFFECT_TEETER_DANCE, + EFFECT_MUD_SPORT, + EFFECT_WATER_SPORT, + EFFECT_DRAGON_DANCE, + EFFECT_CAMOUFLAGE, +}; + static const u16 sDiscouragedPowerfulMoveEffects[] = { EFFECT_EXPLOSION, @@ -242,7 +572,14 @@ void RestoreBattlerData(u8 battlerId) u32 GetHealthPercentage(u8 battlerId) { return (u32)((100 * gBattleMons[battlerId].hp) / gBattleMons[battlerId].maxHP); -} +} + +bool32 AtMaxHp(u8 battlerId) +{ + if (GetHealthPercentage(battlerId) == 100) + return TRUE; + return FALSE; +} bool32 IsBattlerTrapped(u8 battler, bool8 checkSwitch) { @@ -267,6 +604,15 @@ bool32 IsBattlerTrapped(u8 battler, bool8 checkSwitch) } // move checks +bool32 IsAffectedByPowder(u8 battler, u16 ability, u16 holdEffect) +{ + if ((B_POWDER_GRASS >= GEN_6 && IS_BATTLER_OF_TYPE(battler, TYPE_GRASS)) + || ability == ABILITY_OVERCOAT + || GetBattlerHoldEffect(battler, TRUE) == HOLD_EFFECT_SAFETY_GOOGLES) + return FALSE; + return TRUE; +} + // This function checks if all physical/special moves are either unusable or unreasonable to use. // Consider a pokemon boosting their attack against a ghost pokemon having only normal-type physical attacks. bool32 MovesWithSplitUnusable(u32 attacker, u32 target, u32 split) @@ -543,7 +889,7 @@ u8 AI_GetMoveEffectiveness(u16 move) // AI_CHECK_FASTER: is user(ai) faster // AI_CHECK_SLOWER: is target faster -bool32 IsBattlerFaster(u8 battler) +bool32 IsAiFaster(u8 battler) { u32 fasterAI = 0, fasterPlayer = 0, i; s8 prioAI, prioPlayer; @@ -608,6 +954,17 @@ bool32 CanTargetFaintAi(u8 battlerDef, u8 battlerAtk) return FALSE; } +bool32 CanMoveFaintBattler(u16 move, u8 battlerDef, u8 battlerAtk, u8 nHits) +{ + s32 i, dmg; + u32 unusable = CheckMoveLimitations(battlerDef, 0, 0xFF & ~MOVE_LIMITATION_PP); + + if (move != MOVE_NONE && move != 0xFFFF && !(unusable & gBitTable[i]) && AI_CalcDamage(move, battlerDef, battlerAtk) >= gBattleMons[battlerAtk].hp) + return TRUE; + + return FALSE; +} + // Check if target has means to faint ai mon after modding hp/dmg bool32 CanTargetFaintAiWithMod(u8 battlerDef, u8 battlerAtk, s32 hpMod, s32 dmgMod) { @@ -617,8 +974,11 @@ bool32 CanTargetFaintAiWithMod(u8 battlerDef, u8 battlerAtk, s32 hpMod, s32 dmgM for (i = 0; i < MAX_MON_MOVES; i++) { - u32 dmg = AI_CalcDamage(moves[i], battlerDef, battlerAtk) + dmgMod; + u32 dmg = AI_CalcDamage(moves[i], battlerDef, battlerAtk); u32 hpCheck = gBattleMons[battlerAtk].hp + hpMod; + if (dmgMod) + dmg *= dmgMod; + if (moves[i] != MOVE_NONE && moves[i] != 0xFFFF && !(unusable & gBitTable[i]) && dmg >= hpCheck) { return TRUE; @@ -950,7 +1310,7 @@ bool32 ShouldTryOHKO(u8 battlerAtk, u8 battlerDef, u16 atkAbility, u16 defAbilit gPotentialItemEffectBattler = battlerDef; if (holdEffect == HOLD_EFFECT_FOCUS_BAND && (Random() % 100) < GetBattlerHoldEffectParam(battlerDef)) return FALSE; //probabilistically speaking, focus band should activate so dont OHKO - else if (holdEffect == HOLD_EFFECT_FOCUS_SASH && GetHealthPercentage(battlerDef) == 100) + else if (holdEffect == HOLD_EFFECT_FOCUS_SASH && AtMaxHp(battlerDef)) return FALSE; if (!DoesBattlerIgnoreAbilityChecks(atkAbility, move) && defAbility == ABILITY_STURDY) @@ -972,6 +1332,147 @@ bool32 ShouldTryOHKO(u8 battlerAtk, u8 battlerDef, u16 atkAbility, u16 defAbilit return FALSE; } +bool32 ShouldSetSandstorm(u8 battler, u16 ability, u16 holdEffect) +{ + if (!AI_WeatherHasEffect()) + return FALSE; + else if (gBattleWeather & WEATHER_SANDSTORM_ANY) + return FALSE; + + if (ability == ABILITY_SAND_VEIL + || ability == ABILITY_SAND_RUSH + || ability == ABILITY_SAND_FORCE + || ability == ABILITY_SAND_FORCE + || ability == ABILITY_OVERCOAT + || ability == ABILITY_MAGIC_GUARD + || holdEffect == HOLD_EFFECT_SAFETY_GOOGLES + || IS_BATTLER_OF_TYPE(battler, TYPE_ROCK) + || IS_BATTLER_OF_TYPE(battler, TYPE_STEEL) + || IS_BATTLER_OF_TYPE(battler, TYPE_GROUND) + || HasMoveEffect(battler, EFFECT_SHORE_UP) + || HasMoveEffect(battler, EFFECT_WEATHER_BALL)) + { + return TRUE; + } + return FALSE; +} + +bool32 ShouldSetHail(u8 battler, u16 ability, u16 holdEffect) +{ + if (!AI_WeatherHasEffect()) + return FALSE; + else if (gBattleWeather & WEATHER_HAIL_ANY) + return FALSE; + + if (ability == ABILITY_SNOW_CLOAK + || ability == ABILITY_ICE_BODY + || ability == ABILITY_FORECAST + || ability == ABILITY_SLUSH_RUSH + || ability == ABILITY_MAGIC_GUARD + || ability == ABILITY_OVERCOAT + || holdEffect == HOLD_EFFECT_SAFETY_GOOGLES + || IS_BATTLER_OF_TYPE(battler, TYPE_ICE) + || HasMove(battler, MOVE_BLIZZARD) + || HasMoveEffect(battler, EFFECT_AURORA_VEIL) + || HasMoveEffect(battler, EFFECT_WEATHER_BALL)) + { + return TRUE; + } + return FALSE; +} + +bool32 ShouldSetRain(u8 battlerAtk, u16 atkAbility, u16 holdEffect) +{ + if (!AI_WeatherHasEffect()) + return FALSE; + else if (gBattleWeather & WEATHER_RAIN_ANY) + return FALSE; + + if (holdEffect != HOLD_EFFECT_UTILITY_UMBRELLA + && (atkAbility == ABILITY_SWIFT_SWIM + || atkAbility == ABILITY_FORECAST + || atkAbility == ABILITY_HYDRATION + || atkAbility == ABILITY_RAIN_DISH + || atkAbility == ABILITY_DRY_SKIN + || HasMoveEffect(battlerAtk, EFFECT_THUNDER) + || HasMoveEffect(battlerAtk, EFFECT_HURRICANE) + || HasMoveEffect(battlerAtk, EFFECT_WEATHER_BALL) + || HasMoveWithType(battlerAtk, TYPE_WATER))) + { + return TRUE; + } + return FALSE; +} + +bool32 ShouldSetSun(u8 battlerAtk, u16 atkAbility, u16 holdEffect) +{ + if (!AI_WeatherHasEffect()) + return FALSE; + else if (gBattleWeather & WEATHER_SUN_ANY) + return FALSE; + + if (holdEffect != HOLD_EFFECT_UTILITY_UMBRELLA + && (atkAbility == ABILITY_CHLOROPHYLL + || atkAbility == ABILITY_FLOWER_GIFT + || atkAbility == ABILITY_FORECAST + || atkAbility == ABILITY_LEAF_GUARD + || atkAbility == ABILITY_SOLAR_POWER + || atkAbility == ABILITY_HARVEST + || HasMoveEffect(battlerAtk, EFFECT_SOLARBEAM) + || HasMoveEffect(battlerAtk, EFFECT_MORNING_SUN) + || HasMoveEffect(battlerAtk, EFFECT_SYNTHESIS) + || HasMoveEffect(battlerAtk, EFFECT_MOONLIGHT) + || HasMoveEffect(battlerAtk, EFFECT_WEATHER_BALL) + || HasMoveEffect(battlerAtk, EFFECT_GROWTH) + || HasMoveWithType(battlerAtk, TYPE_FIRE))) + { + return TRUE; + } + return FALSE; +} + + +void ProtectChecks(u8 battlerAtk, u8 battlerDef, u16 move, u16 predictedMove, s16 *score) +{ + // TODO more sophisticated logic + u16 predictedEffect = gBattleMoves[predictedMove].effect; + u8 defAbility = AI_GetAbility(battlerDef); + u32 uses = gDisableStructs[battlerAtk].protectUses; + + /*if (GetMoveResultFlags(predictedMove) & (MOVE_RESULT_NO_EFFECT | MOVE_RESULT_MISSED)) + { + (*score) -= 5; + return; + }*/ + + if (uses == 0) + { + if (predictedMove != MOVE_NONE && predictedMove != 0xFFFF && !IS_MOVE_STATUS(predictedMove)) + (*score) += 2; + else if (Random() % 256 < 100) + (*score)++; + } + else + { + if (IsDoubleBattle()) + (*score) -= 2 * min(uses, 3); + else + (*score) -= min(uses, 3); + } + + if (gBattleMons[battlerAtk].status1 & (STATUS1_PSN_ANY | STATUS1_BURN) + || gBattleMons[battlerAtk].status2 & (STATUS2_CURSED | STATUS2_INFATUATION) + || gStatuses3[battlerAtk] & (STATUS3_PERISH_SONG | STATUS3_LEECHSEED | STATUS3_YAWN)) + { + (*score)--; + } + + if (gBattleMons[battlerDef].status1 & STATUS1_TOXIC_POISON + || gBattleMons[battlerDef].status2 & (STATUS2_CURSED | STATUS2_INFATUATION) + || gStatuses3[battlerDef] & (STATUS3_PERISH_SONG | STATUS3_LEECHSEED | STATUS3_YAWN)) + (*score) += 2; +} + // stat stages bool32 BattlerStatCanFall(u8 battler, u16 battlerAbility, u8 stat) { @@ -1050,23 +1551,115 @@ bool32 BattlerShouldRaiseAttacks(u8 battlerId, u16 ability) bool32 ShouldLowerAttack(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 moveIndex) { - if (IsBattlerFaster(AI_CHECK_FASTER) && CanAttackerFaintTarget(battlerAtk, battlerDef, moveIndex)) - return FALSE; //Don't bother lowering stats if can kill enemy. + if (IsAiFaster(AI_CHECK_FASTER) && CanAttackerFaintTarget(battlerAtk, battlerDef, moveIndex, 0)) + return FALSE; // Don't bother lowering stats if can kill enemy. - if (gBattleMons[battlerDef].statStages[STAT_ATK] > 4 && HasMoveWithSplit(battlerDef, SPLIT_PHYSICAL) + if (gBattleMons[battlerDef].statStages[STAT_ATK] > 4 + && HasMoveWithSplit(battlerDef, SPLIT_PHYSICAL) && defAbility != ABILITY_CONTRARY && defAbility != ABILITY_CLEAR_BODY && defAbility != ABILITY_WHITE_SMOKE - //&& defAbility != ABILITY_FULLMETALBODY + //&& defAbility != ABILITY_FULL_METAL_BODY && defAbility != ABILITY_HYPER_CUTTER) return TRUE; return FALSE; } -bool32 CanAttackerFaintTarget(u8 battlerAtk, u8 battlerDef, u8 index) +bool32 ShouldLowerDefense(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 moveIndex) +{ + if (IsAiFaster(AI_CHECK_FASTER) && CanAttackerFaintTarget(battlerAtk, battlerDef, moveIndex, 0)) + return FALSE; // Don't bother lowering stats if can kill enemy. + + if (gBattleMons[battlerDef].statStages[STAT_DEF] > 4 + && HasMoveWithSplit(battlerAtk, SPLIT_PHYSICAL) + && defAbility != ABILITY_CONTRARY + && defAbility != ABILITY_CLEAR_BODY + && defAbility != ABILITY_WHITE_SMOKE + //&& defAbility != ABILITY_FULL_METAL_BODY + && defAbility != ABILITY_BIG_PECKS) + return TRUE; + return FALSE; +} + +bool32 ShouldLowerSpeed(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 moveIndex) +{ + if (IsAiFaster(AI_CHECK_FASTER) && CanAttackerFaintTarget(battlerAtk, battlerDef, moveIndex, 0)) + return FALSE; // Don't bother lowering stats if can kill enemy. + + if (IsAiFaster(AI_CHECK_SLOWER) + && defAbility != ABILITY_CONTRARY + && defAbility != ABILITY_CLEAR_BODY + //&& defAbility != ABILITY_FULL_METAL_BODY + && defAbility != ABILITY_WHITE_SMOKE) + return TRUE; + return FALSE; +} + +bool32 ShouldLowerSpAtk(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 moveIndex) +{ + if (IsAiFaster(AI_CHECK_FASTER) && CanAttackerFaintTarget(battlerAtk, battlerDef, moveIndex, 0)) + return FALSE; // Don't bother lowering stats if can kill enemy. + + if (gBattleMons[battlerDef].statStages[STAT_SPATK] > 4 + && HasMoveWithSplit(battlerDef, SPLIT_SPECIAL) + && defAbility != ABILITY_CONTRARY + && defAbility != ABILITY_CLEAR_BODY + //&& defAbility != ABILITY_FULL_METAL_BODY + && defAbility != ABILITY_WHITE_SMOKE) + return TRUE; + return FALSE; +} + +bool32 ShouldLowerSpDef(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 moveIndex) +{ + if (IsAiFaster(AI_CHECK_FASTER) && CanAttackerFaintTarget(battlerAtk, battlerDef, moveIndex, 0)) + return FALSE; // Don't bother lowering stats if can kill enemy. + + if (gBattleMons[battlerDef].statStages[STAT_SPDEF] > 4 + && HasMoveWithSplit(battlerAtk, SPLIT_SPECIAL) + && defAbility != ABILITY_CONTRARY + && defAbility != ABILITY_CLEAR_BODY + //&& defAbility != ABILITY_FULL_METAL_BODY + && defAbility != ABILITY_WHITE_SMOKE) + return TRUE; + return FALSE; +} + +bool32 ShouldLowerAccuracy(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 moveIndex) +{ + if (IsAiFaster(AI_CHECK_FASTER) && CanAttackerFaintTarget(battlerAtk, battlerDef, moveIndex, 0)) + return FALSE; // Don't bother lowering stats if can kill enemy. + + if (defAbility != ABILITY_CONTRARY + && defAbility != ABILITY_CLEAR_BODY + && defAbility != ABILITY_WHITE_SMOKE + //&& defAbility != ABILITY_FULL_METAL_BODY + && defAbility != ABILITY_KEEN_EYE) + return TRUE; + return FALSE; +} + +bool32 ShouldLowerEvasion(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 moveIndex) +{ + if (IsAiFaster(AI_CHECK_FASTER) && CanAttackerFaintTarget(battlerAtk, battlerDef, moveIndex, 0)) + return FALSE; // Don't bother lowering stats if can kill enemy. + + if (gBattleMons[battlerDef].statStages[STAT_EVASION] > DEFAULT_STAT_STAGE + && defAbility != ABILITY_CONTRARY + && defAbility != ABILITY_CLEAR_BODY + //&& defAbility != ABILITY_FULL_METAL_BODY + && defAbility != ABILITY_WHITE_SMOKE) + return TRUE; + return FALSE; +} + +bool32 CanAttackerFaintTarget(u8 battlerAtk, u8 battlerDef, u8 index, u8 numHits) { s32 dmg = AI_THINKING_STRUCT->simulatedDmg[battlerAtk][battlerDef][index]; + if (numHits) + dmg *= numHits; + if (gBattleMons[battlerDef].hp <= dmg) return TRUE; return FALSE; @@ -1122,7 +1715,21 @@ bool32 HasMoveEffect(u32 battlerId, u16 moveEffect) return FALSE; } -bool32 HasMoveWithLowAccuracy(u8 battlerAtk, u8 battlerDef, u8 accCheck, bool32 ignoreStatus, u16 atkAbility, u16 defAbility, u16 atkHoldEffect, u16 defHoldEffect, u16 move) +bool32 HasMove(u32 battlerId, u32 move) +{ + s32 i; + u16 *moves = GetMovesArray(battlerId); + + for (i = 0; i < MAX_MON_MOVES; i++) + { + if (moves[i] != MOVE_NONE && moves[i] != 0xFFFF && moves[i] == move) + return TRUE; + } + + return FALSE; +} + +bool32 HasMoveWithLowAccuracy(u8 battlerAtk, u8 battlerDef, u8 accCheck, bool32 ignoreStatus, u16 atkAbility, u16 defAbility, u16 atkHoldEffect, u16 defHoldEffect) { s32 i; u16 *moves = GetMovesArray(battlerAtk); @@ -1137,11 +1744,11 @@ bool32 HasMoveWithLowAccuracy(u8 battlerAtk, u8 battlerDef, u8 accCheck, bool32 { if (ignoreStatus && IS_MOVE_STATUS(moves[i])) continue; - else if ((!IS_MOVE_STATUS(moves[i]) && gBattleMoves[move].accuracy == 0) - || gBattleMoves[move].target & (MOVE_TARGET_USER | MOVE_TARGET_OPPONENTS_FIELD)) + else if ((!IS_MOVE_STATUS(moves[i]) && gBattleMoves[moves[i]].accuracy == 0) + || gBattleMoves[moves[i]].target & (MOVE_TARGET_USER | MOVE_TARGET_OPPONENTS_FIELD)) continue; - if (AI_GetMoveAccuracy(battlerAtk, battlerDef, atkAbility, defAbility, atkHoldEffect, defHoldEffect, move) <= accCheck) + if (AI_GetMoveAccuracy(battlerAtk, battlerDef, atkAbility, defAbility, atkHoldEffect, defHoldEffect, moves[i]) <= accCheck) return TRUE; } } @@ -1149,6 +1756,123 @@ bool32 HasMoveWithLowAccuracy(u8 battlerAtk, u8 battlerDef, u8 accCheck, bool32 return FALSE; } +bool32 HasSleepMoveWithLowAccuracy(u8 battlerAtk, u8 battlerDef) +{ + u8 moveLimitations = CheckMoveLimitations(battlerAtk, 0, 0xFF); + u32 i; + u16 *moves = GetMovesArray(battlerAtk); + + for (i = 0; i < MAX_MON_MOVES; i++) + { + if (moves[i] == MOVE_NONE) + break; + if (!(gBitTable[i] & moveLimitations)) + { + if (gBattleMoves[moves[i]].effect == EFFECT_SLEEP + && AI_GetMoveAccuracy(battlerAtk, battlerDef, AI_DATA->atkAbility, AI_DATA->defAbility, AI_DATA->atkHoldEffect, AI_DATA->defHoldEffect, moves[i]) < 85) + return TRUE; + } + } + return FALSE; +} + +bool32 IsHealingMoveEffect(u16 effect) +{ + switch (effect) + { + case EFFECT_RESTORE_HP: + case EFFECT_MORNING_SUN: + case EFFECT_SYNTHESIS: + case EFFECT_MOONLIGHT: + case EFFECT_SOFTBOILED: + case EFFECT_ROOST: + case EFFECT_SWALLOW: + case EFFECT_WISH: + case EFFECT_HEALING_WISH: + case EFFECT_HEAL_PULSE: + case EFFECT_REST: + return TRUE; + default: + return FALSE; + } +} + +bool32 HasHealingEffect(u32 battlerId) +{ + s32 i; + u16 *moves = GetMovesArray(battlerId); + + for (i = 0; i < MAX_MON_MOVES; i++) + { + if (moves[i] != MOVE_NONE && moves[i] != 0xFFFF && IsHealingMoveEffect(gBattleMoves[moves[i]].effect)) + return TRUE; + } + + return FALSE; +} + +bool32 IsThawingMove(u16 move) +{ + switch (move) + { + case MOVE_FLAME_WHEEL: + case MOVE_SACRED_FIRE: + case MOVE_FLARE_BLITZ: + case MOVE_SCALD: + case MOVE_SCORCHING_SANDS: + case MOVE_FUSION_FLARE: + case MOVE_STEAM_ERUPTION: + case MOVE_BURN_UP: + case MOVE_PYRO_BALL: + return TRUE; + default: + return FALSE; + } +} + +bool32 HasThawingMove(u8 battlerId) +{ + s32 i; + u16 *moves = GetMovesArray(battlerId); + + for (i = 0; i < MAX_MON_MOVES; i++) + { + if (moves[i] != MOVE_NONE && moves[i] != 0xFFFF && IsThawingMove(moves[i])) + return TRUE; + } + + return FALSE; +} + +bool32 HasDamagingMove(u8 battlerId) +{ + u32 i; + u16 *moves = GetMovesArray(battlerId); + + for (i = 0; i < MAX_MON_MOVES; i++) + { + if (moves[i] != MOVE_NONE && moves[i] != 0xFFFF && gBattleMoves[moves[i]].power != 0) + return TRUE; + } + + return FALSE; +} + +bool32 HasDamagingMoveOfType(u8 battlerId, u8 type) +{ + s32 i; + u16 *moves = GetMovesArray(battlerId); + + for (i = 0; i < MAX_MON_MOVES; i++) + { + if (moves[i] != MOVE_NONE && moves[i] != 0xFFFF + && gBattleMoves[moves[i]].type == type && gBattleMoves[moves[i]].power != 0) + return TRUE; + } + + return FALSE; +} + bool32 IsInstructBannedMove(u16 move) { u32 i; @@ -1160,6 +1884,18 @@ bool32 IsInstructBannedMove(u16 move) return FALSE; } +bool32 IsEncoreEncouragedEffect(u16 moveEffect) +{ + u32 i; + + for (i = 0; i < ARRAY_COUNT(sEncouragedEncoreEffects); i++) + { + if (moveEffect == sEncouragedEncoreEffects[i]) + return TRUE; + } + return FALSE; +} + bool32 MoveRequiresRecharging(u16 move) { u32 i; @@ -1195,20 +1931,6 @@ bool32 TestMoveFlagsInMoveset(u8 battler, u32 flags) return FALSE; } -bool32 BattlerHasDamagingMove(u8 battlerId) -{ - u32 i; - u16 *moves = GetMovesArray(battlerId); - - for (i = 0; i < MAX_MON_MOVES; i++) - { - if (moves[i] != MOVE_NONE && moves[i] != 0xFFFF && gBattleMoves[moves[i]].power != 0) - return TRUE; - } - - return FALSE; -} - static u32 GetLeechSeedDamage(u8 battlerId) { u32 damage = 0; @@ -1345,7 +2067,7 @@ static u32 GetWeatherDamage(u8 battlerId) return damage; } -bool32 BattlerHasSecondaryDamage(u8 battlerId) +u32 GetBattlerSecondaryDamage(u8 battlerId) { u32 secondaryDamage; @@ -1359,9 +2081,7 @@ bool32 BattlerHasSecondaryDamage(u8 battlerId) + GetPoisonDamage(battlerId) + GetWeatherDamage(battlerId); - if (secondaryDamage != 0) - return TRUE; - return FALSE; + return secondaryDamage; } bool32 BattlerWillFaintFromWeather(u8 battler, u16 ability) @@ -1373,26 +2093,327 @@ bool32 BattlerWillFaintFromWeather(u8 battler, u16 ability) return FALSE; } +bool32 BattlerWillFaintFromSecondaryDamage(u8 battler, u16 ability) +{ + if (GetBattlerSecondaryDamage(battler) != 0 + && gBattleMons[battler].hp <= gBattleMons[battler].maxHP / 16) + return TRUE; + return FALSE; +} + +static bool32 AnyUsefulStatIsRaised(u8 battler) +{ + u8 statId; + + for (statId = STAT_ATK; statId < NUM_BATTLE_STATS; statId++) + { + if (gBattleMons[battler].statStages[statId] > DEFAULT_STAT_STAGE) + { + switch (statId) + { + case STAT_ATK: + if (HasMoveWithSplit(battler, SPLIT_PHYSICAL)) + return TRUE; + break; + case STAT_SPATK: + if (HasMoveWithSplit(battler, SPLIT_SPECIAL)) + return TRUE; + break; + case STAT_SPEED: + return TRUE; + } + } + } + + return FALSE; +} + +static bool32 PartyBattlerShouldAvoidHazards(u8 currBattler, u8 switchBattler) +{ + struct Pokemon *mon = GetBattlerPartyData(switchBattler); + u16 ability = GetMonAbility(mon); // we know our own party data + u16 holdEffect = GetBattlerHoldEffect(GetMonData(mon, MON_DATA_HELD_ITEM), TRUE); + u32 flags = gSideStatuses[GetBattlerSide(currBattler)] & (SIDE_STATUS_SPIKES | SIDE_STATUS_STEALTH_ROCK | SIDE_STATUS_STICKY_WEB | SIDE_STATUS_TOXIC_SPIKES); + + if (flags == 0) + return FALSE; + + if (ability == ABILITY_MAGIC_GUARD || ability == ABILITY_LEVITATE + || holdEffect == HOLD_EFFECT_HEAVY_DUTY_BOOTS) + return FALSE; + + if (flags & (SIDE_STATUS_SPIKES | SIDE_STATUS_STEALTH_ROCK) && GetMonData(mon, MON_DATA_HP) < (GetMonData(mon, MON_DATA_MAX_HP) / 8)) + return TRUE; + + return FALSE; +} + +enum { + DONT_PIVOT, + CAN_TRY_PIVOT, + PIVOT, +}; +bool32 ShouldPivot(u8 battlerAtk, u8 battlerDef, u16 defAbility, u16 move, u8 moveIndex) +{ + bool8 hasStatBoost = AnyUsefulStatIsRaised(battlerAtk) || gBattleMons[battlerDef].statStages[STAT_EVASION] >= 9; //Significant boost in evasion for any class + u8 backupBattler = gActiveBattler; + bool32 shouldSwitch; + u8 battlerToSwitch; + + gActiveBattler = battlerAtk; + shouldSwitch = ShouldSwitch(); + battlerToSwitch = *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler); + gActiveBattler = backupBattler; + + if (PartyBattlerShouldAvoidHazards(battlerAtk, battlerToSwitch)) + return DONT_PIVOT; + + if (!IsDoubleBattle()) + { + if (CountUsablePartyMons(battlerAtk) == 0) + return CAN_TRY_PIVOT; // can't switch, but attack might still be useful + + //TODO - predict opponent switching + /*if (IsPredictedToSwitch(battlerDef, battlerAtk) && !hasStatBoost) + return PIVOT; // Try pivoting so you can switch to a better matchup to counter your new opponent*/ + + if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0) // Attacker goes first + { + if (!CanAttackerFaintTarget(battlerAtk, battlerDef, moveIndex, 0)) // Can't KO foe otherwise + { + if (CanAttackerFaintTarget(battlerAtk, battlerDef, moveIndex, 2)) + { + // attacker can kill target in two hits (theoretically) + if (CanTargetFaintAi(battlerDef, battlerAtk)) + return PIVOT; // Won't get the two turns, pivot + + if (!IS_MOVE_STATUS(move) && (shouldSwitch + || (AtMaxHp(battlerDef) && (AI_DATA->defHoldEffect == HOLD_EFFECT_FOCUS_SASH + || defAbility == ABILITY_STURDY || defAbility == ABILITY_MULTISCALE || defAbility == ABILITY_SHADOW_SHIELD)))) + return PIVOT; // pivot to break sash/sturdy/multiscale + } + else if (!hasStatBoost) + { + if (!IS_MOVE_STATUS(move) && (AtMaxHp(battlerDef) && (AI_DATA->defHoldEffect == HOLD_EFFECT_FOCUS_SASH + || defAbility == ABILITY_STURDY || defAbility == ABILITY_MULTISCALE || defAbility == ABILITY_SHADOW_SHIELD))) + return PIVOT; // pivot to break sash/sturdy/multiscale + + if (shouldSwitch) + return PIVOT; + + /* TODO - check if switchable mon unafffected by/will remove hazards + if (gSideStatuses[battlerAtk] & SIDE_STATUS_SPIKES && switchScore >= SWITCHING_INCREASE_CAN_REMOVE_HAZARDS) + return PIVOT;*/ + + /*if (BattlerWillFaintFromSecondaryDamage(battlerAtk, AI_DATA->atkAbility) && switchScore >= SWITCHING_INCREASE_WALLS_FOE) + return PIVOT;*/ + + /*if (IsClassDamager(class) && switchScore >= SWITCHING_INCREASE_HAS_SUPER_EFFECTIVE_MOVE) + { + bool8 physMoveInMoveset = PhysicalMoveInMoveset(battlerAtk); + bool8 specMoveInMoveset = SpecialMoveInMoveset(battlerAtk); + + //Pivot if attacking stats are bad + if (physMoveInMoveset && !specMoveInMoveset) + { + if (STAT_STAGE_ATK < 6) + return PIVOT; + } + else if (!physMoveInMoveset && specMoveInMoveset) + { + if (STAT_STAGE_SPATK < 6) + return PIVOT; + } + else if (physMoveInMoveset && specMoveInMoveset) + { + if (STAT_STAGE_ATK < 6 && STAT_STAGE_SPATK < 6) + return PIVOT; + } + + return CAN_TRY_PIVOT; + }*/ + } + } + } + else // Opponent Goes First + { + if (CanTargetFaintAi(battlerDef, battlerAtk)) + { + if (gBattleMoves[move].effect == EFFECT_TELEPORT) + return DONT_PIVOT; // If you're going to faint because you'll go second, use a different move + else + return CAN_TRY_PIVOT; // You're probably going to faint anyways so if for some reason you don't, better switch + } + else if (CanTargetFaintAiWithMod(battlerDef, battlerAtk, 0, 2)) // Foe can 2HKO AI + { + if (CanAttackerFaintTarget(battlerAtk, battlerDef, moveIndex, 0)) + { + if (!BattlerWillFaintFromSecondaryDamage(battlerAtk, AI_DATA->atkAbility)) + return CAN_TRY_PIVOT; // Use this move to KO if you must + } + else // Can't KO the foe + { + return PIVOT; + } + } + else // Foe can 3HKO+ AI + { + if (CanAttackerFaintTarget(battlerAtk, battlerDef, moveIndex, 0)) + { + if (!BattlerWillFaintFromSecondaryDamage(battlerAtk, AI_DATA->atkAbility) // This is the only move that can KO + && !hasStatBoost) //You're not wasting a valuable stat boost + { + return CAN_TRY_PIVOT; + } + } + else if (CanAttackerFaintTarget(battlerAtk, battlerDef, moveIndex, 2)) + { + // can knock out foe in 2 hits + if (IS_MOVE_STATUS(move) && (shouldSwitch //Damaging move + //&& (switchScore >= SWITCHING_INCREASE_RESIST_ALL_MOVES + SWITCHING_INCREASE_KO_FOE //remove hazards + || (AI_DATA->defHoldEffect == HOLD_EFFECT_FOCUS_SASH && AtMaxHp(battlerDef)))) + return DONT_PIVOT; // Pivot to break the sash + else + return CAN_TRY_PIVOT; + } + else + { + //if (IsClassDamager(class) && switchScore >= SWITCHING_INCREASE_KO_FOE) + //return PIVOT; //Only switch if way better matchup + + if (!hasStatBoost) + { + // TODO - check if switching prevents/removes hazards + //if (gSideStatuses[battlerAtk] & SIDE_STATUS_SPIKES && switchScore >= SWITCHING_INCREASE_CAN_REMOVE_HAZARDS) + //return PIVOT; + + // TODO - not always a good idea + //if (BattlerWillFaintFromSecondaryDamage(battlerAtk) && switchScore >= SWITCHING_INCREASE_HAS_SUPER_EFFECTIVE_MOVE) + //return PIVOT; + + /*if (IsClassDamager(class) && switchScore >= SWITCHING_INCREASE_HAS_SUPER_EFFECTIVE_MOVE) + { + bool8 physMoveInMoveset = PhysicalMoveInMoveset(battlerAtk); + bool8 specMoveInMoveset = SpecialMoveInMoveset(battlerAtk); + + //Pivot if attacking stats are bad + if (physMoveInMoveset && !specMoveInMoveset) + { + if (STAT_STAGE_ATK < 6) + return PIVOT; + } + else if (!physMoveInMoveset && specMoveInMoveset) + { + if (STAT_STAGE_SPATK < 6) + return PIVOT; + } + else if (physMoveInMoveset && specMoveInMoveset) + { + if (STAT_STAGE_ATK < 6 && STAT_STAGE_SPATK < 6) + return PIVOT; + } + }*/ + + return CAN_TRY_PIVOT; + } + } + } + } + } + + return DONT_PIVOT; +} + +bool32 CanKnockOffItem(u8 battler, u16 item) +{ + if (item == ITEM_NONE) + return FALSE; + + if (!(gBattleTypeFlags & (BATTLE_TYPE_EREADER_TRAINER + | BATTLE_TYPE_FRONTIER + | BATTLE_TYPE_LINK + | BATTLE_TYPE_x2000000 + | BATTLE_TYPE_SECRET_BASE + #if defined B_TRAINERS_KNOCK_OFF_ITEMS + | BATTLE_TYPE_TRAINER + #endif + )) && GetBattlerSide(battler) == B_SIDE_PLAYER) + return FALSE; + + if (AI_GetAbility(battler) == ABILITY_STICKY_HOLD) + return FALSE; + + if (!CanBattlerGetOrLoseItem(battler, item)) + return FALSE; + + return TRUE; +} + // status checks +bool32 IsBattlerIncapacitated(u8 battler, u16 ability) +{ + if ((gBattleMons[battler].status1 & STATUS1_FREEZE) && !HasThawingMove(battler)) + return TRUE; // if battler has thawing move we assume they will definitely use it, and thus being frozen should be neglected + + if (gBattleMons[battler].status1 & STATUS1_SLEEP) + return TRUE; + + if (gBattleMons[battler].status2 & STATUS2_RECHARGE || (ability == ABILITY_TRUANT && gDisableStructs[battler].truantCounter != 0)) + return TRUE; + + return FALSE; +} + +bool32 CanSleep(u8 battler, u16 ability) +{ + if (ability == ABILITY_INSOMNIA + || ability == ABILITY_VITAL_SPIRIT + || gBattleMons[battler].status1 & STATUS1_ANY + || gSideStatuses[GetBattlerSide(battler)] & SIDE_STATUS_SAFEGUARD + || (gFieldStatuses & (STATUS_FIELD_MISTY_TERRAIN | STATUS_FIELD_ELECTRIC_TERRAIN)) + || IsAbilityStatusProtected(battler)) + return FALSE; + return TRUE; +} + bool32 AI_CanPutToSleep(u8 battlerAtk, u8 battlerDef, u16 defAbility, u16 move, u16 partnerMove) { - if (defAbility == ABILITY_INSOMNIA - || defAbility == ABILITY_VITAL_SPIRIT - || gBattleMons[battlerDef].status1 & STATUS1_ANY - || gSideStatuses[GetBattlerSide(battlerDef)] & SIDE_STATUS_SAFEGUARD - || IsAbilityStatusProtected(battlerDef) + if (!CanSleep(battlerDef, defAbility) || DoesSubstituteBlockMove(battlerAtk, battlerDef, move) || PartnerMoveEffectIsStatusSameTarget(BATTLE_PARTNER(battlerAtk), battlerDef, partnerMove)) // shouldn't try to sleep mon that partner is trying to make sleep return FALSE; return TRUE; } +bool32 CanBePoisoned(u8 battler, u16 ability) +{ + if (ability == ABILITY_IMMUNITY + || ability == ABILITY_PASTEL_VEIL + || gBattleMons[battler].status1 & STATUS1_ANY + || IsAbilityStatusProtected(battler) + || gSideStatuses[GetBattlerSide(battler)] & SIDE_STATUS_SAFEGUARD) + return FALSE; + return TRUE; +} + +bool32 ShouldPoisonSelf(u8 battler, u16 ability) +{ + if (CanBePoisoned(battler, ability) && ( + ability == ABILITY_MARVEL_SCALE + || ability == ABILITY_POISON_HEAL + || ability == ABILITY_QUICK_FEET + || ability == ABILITY_MAGIC_GUARD + || (ability == ABILITY_TOXIC_BOOST && HasMoveWithSplit(battler, SPLIT_PHYSICAL)) + || (ability == ABILITY_GUTS && HasMoveWithSplit(battler, SPLIT_PHYSICAL)) + || HasMoveEffect(battler, EFFECT_FACADE) + || HasMoveEffect(battler, EFFECT_PSYCHO_SHIFT))) + return TRUE; + return FALSE; +} + bool32 AI_CanPoison(u8 battlerAtk, u8 battlerDef, u16 defAbility, u16 move, u16 partnerMove) { - if (defAbility == ABILITY_IMMUNITY - || defAbility == ABILITY_PASTEL_VEIL - || gBattleMons[battlerDef].status1 & STATUS1_ANY - || IsAbilityStatusProtected(battlerDef) + if (!CanBePoisoned(battlerDef, defAbility) || DoesSubstituteBlockMove(battlerAtk, battlerDef, move) || PartnerMoveEffectIsStatusSameTarget(BATTLE_PARTNER(battlerAtk), battlerDef, partnerMove)) return FALSE; @@ -1410,6 +2431,7 @@ bool32 AI_CanParalyze(u8 battlerAtk, u8 battlerDef, u16 defAbility, u16 move, u1 || IS_BATTLER_OF_TYPE(battlerDef, TYPE_ELECTRIC) || gBattleMons[battlerDef].status1 & STATUS1_ANY || IsAbilityStatusProtected(battlerDef) + || gSideStatuses[GetBattlerSide(battlerDef)] & SIDE_STATUS_SAFEGUARD || DoesSubstituteBlockMove(battlerAtk, battlerDef, move) || PartnerMoveEffectIsStatusSameTarget(BATTLE_PARTNER(battlerAtk), battlerDef, partnerMove)) return FALSE; @@ -1421,6 +2443,7 @@ bool32 AI_CanConfuse(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 battlerAtk if ((gBattleMons[battlerDef].status2 & STATUS2_CONFUSION) || (!DoesBattlerIgnoreAbilityChecks(battlerAtk, move) && defAbility == ABILITY_OWN_TEMPO) || (IsBattlerGrounded(battlerDef) && (gFieldStatuses & STATUS_FIELD_MISTY_TERRAIN)) + || gSideStatuses[GetBattlerSide(battlerDef)] & SIDE_STATUS_SAFEGUARD || DoesSubstituteBlockMove(battlerAtk, battlerDef, move) || DoesPartnerHaveSameMoveEffect(battlerAtkPartner, battlerDef, move, partnerMove)) { @@ -1430,13 +2453,35 @@ bool32 AI_CanConfuse(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 battlerAtk return TRUE; } +bool32 CanBeBurned(u8 battler, u16 ability) +{ + if (ability == ABILITY_WATER_VEIL + || ability == ABILITY_WATER_BUBBLE + || IS_BATTLER_OF_TYPE(battler, TYPE_FIRE) + || gBattleMons[battler].status1 & STATUS1_ANY + || IsAbilityStatusProtected(battler) + || gSideStatuses[GetBattlerSide(battler)] & SIDE_STATUS_SAFEGUARD) + return FALSE; + return TRUE; +} + +bool32 ShouldBurnSelf(u8 battler, u16 ability) +{ + if (CanBeBurned(battler, ability) && ( + ability == ABILITY_QUICK_FEET + || ability == ABILITY_HEATPROOF + || ability == ABILITY_MAGIC_GUARD + || (ability == ABILITY_FLARE_BOOST && HasMoveWithSplit(battler, SPLIT_SPECIAL)) + || (ability == ABILITY_GUTS && HasMoveWithSplit(battler, SPLIT_PHYSICAL)) + || HasMoveEffect(battler, EFFECT_FACADE) + || HasMoveEffect(battler, EFFECT_PSYCHO_SHIFT))) + return TRUE; + return FALSE; +} + bool32 AI_CanBurn(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 battlerAtkPartner, u16 move, u16 partnerMove) { - if (defAbility == ABILITY_WATER_VEIL - || defAbility == ABILITY_WATER_BUBBLE - || IS_BATTLER_OF_TYPE(battlerDef, TYPE_FIRE) - || gBattleMons[battlerDef].status1 & STATUS1_ANY - || IsAbilityStatusProtected(battlerDef) + if (!CanBeBurned(battlerDef, defAbility) || DoesSubstituteBlockMove(battlerAtk, battlerDef, move) || PartnerMoveEffectIsStatusSameTarget(battlerAtkPartner, battlerDef, partnerMove)) { @@ -1458,6 +2503,74 @@ bool32 AI_CanBeInfatuated(u8 battlerAtk, u8 battlerDef, u16 defAbility, u8 atkGe return TRUE; } +u32 ShouldTryToFlinch(u8 battlerAtk, u8 battlerDef, u16 atkAbility, u16 defAbility, u16 move) +{ + if (defAbility == ABILITY_INNER_FOCUS + || DoesSubstituteBlockMove(battlerAtk, battlerDef, move) + || GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1) + { + return 0; // don't try to flinch + } + else if ((gBattleMons[battlerDef].status1 & STATUS1_SLEEP) && !HasMoveEffect(battlerDef, EFFECT_SLEEP_TALK) && !HasMoveEffect(battlerDef, EFFECT_SNORE)) + { + return 0; // don't try to flinch sleeping pokemon + } + else if (atkAbility == ABILITY_SERENE_GRACE + || gBattleMons[battlerDef].status1 & STATUS1_PARALYSIS + || gBattleMons[battlerDef].status2 & STATUS2_INFATUATION + || gBattleMons[battlerDef].status2 & STATUS2_CONFUSION) + { + return 2; // good idea to flinch + } + return 1; // decent idea to flinch +} + +bool32 ShouldTrap(u8 battlerAtk, u8 battlerDef, u16 move) +{ + if (BattlerWillFaintFromSecondaryDamage(battlerDef, AI_DATA->defAbility)) + return TRUE; // battler is taking secondary damage with low HP + + if (AI_THINKING_STRUCT->aiFlags & AI_FLAG_STALL) + { + if (!CanTargetFaintAi(battlerDef, battlerAtk)) + return TRUE; // attacker goes first and opponent can't kill us + } + + return FALSE; +} + +bool32 ShouldFakeOut(u8 battlerAtk, u8 battlerDef, u16 move) +{ + if (AI_DATA->atkHoldEffect == HOLD_EFFECT_CHOICE_BAND && CountUsablePartyMons(battlerAtk) == 0) + return FALSE; // don't lock attacker into fake out if can't switch out + + if (gDisableStructs[battlerAtk].isFirstTurn + && ShouldTryToFlinch(battlerAtk, battlerDef, AI_DATA->atkAbility, AI_DATA->defAbility, move) + && !DoesSubstituteBlockMove(battlerAtk, battlerDef, move)) + return TRUE; + + return FALSE; +} + +static u32 FindMoveUsedXTurnsAgo(u32 battlerId, u32 x) +{ + s32 i, index = BATTLE_HISTORY->moveHistoryIndex[battlerId]; + for (i = 0; i < x; i++) + { + if (--index < 0) + index = AI_MOVE_HISTORY_COUNT - 1; + } + return BATTLE_HISTORY->moveHistory[battlerId][index]; +} + +bool32 IsWakeupTurn(u8 battler) +{ + // Check if rest was used 2 turns ago + if ((gBattleMons[battler].status1 & STATUS1_SLEEP) == 1 && FindMoveUsedXTurnsAgo(battler, 2) == MOVE_REST) + return TRUE; + else // no way to know + return FALSE; +} bool32 AnyPartyMemberStatused(u8 battlerId, bool32 checkSoundproof) { @@ -1505,9 +2618,9 @@ u16 GetBattlerSideSpeedAverage(u8 battler) bool32 ShouldUseRecoilMove(u8 battlerAtk, u8 battlerDef, u32 recoilDmg, u8 moveIndex) { if (recoilDmg >= gBattleMons[battlerAtk].hp //Recoil kills attacker - && CountUsablePartyMons(battlerDef) > 1) //Foe has more than 1 target left + && CountUsablePartyMons(battlerDef) != 0) //Foe has more than 1 target left { - if (recoilDmg >= gBattleMons[battlerDef].hp && !CanAttackerFaintTarget(battlerAtk, battlerDef, moveIndex)) + if (recoilDmg >= gBattleMons[battlerDef].hp && !CanAttackerFaintTarget(battlerAtk, battlerDef, moveIndex, 0)) return TRUE; //If it's the only KO move then just use it else return FALSE; //Not as good to use move if you'll faint and not win @@ -1516,14 +2629,17 @@ bool32 ShouldUseRecoilMove(u8 battlerAtk, u8 battlerDef, u32 recoilDmg, u8 moveI return TRUE; } -bool32 ShouldRecover(u8 battlerAtk, u8 battlerDef, u16 move, s32 damage) +bool32 ShouldAbsorb(u8 battlerAtk, u8 battlerDef, u16 move, s32 damage) { - if (move == 0xFFFF || GetWhoStrikesFirst(sBattler_AI, gBattlerTarget, TRUE) == 0) + if (move == 0xFFFF || GetWhoStrikesFirst(battlerAtk, gBattlerTarget, TRUE) == 0) { // using item or user goes first u8 healPercent = (gBattleMoves[move].argument == 0) ? 50 : gBattleMoves[move].argument; s32 healDmg = (healPercent * damage) / 100; + if (gStatuses3[battlerAtk] & STATUS3_HEAL_BLOCK) + healDmg = 0; + if (CanTargetFaintAi(battlerDef, battlerAtk) && !CanTargetFaintAiWithMod(battlerDef, battlerAtk, healDmg, 0)) return TRUE; // target can faint attacker unless they heal @@ -1540,6 +2656,66 @@ bool32 ShouldRecover(u8 battlerAtk, u8 battlerDef, u16 move, s32 damage) return FALSE; } +bool32 ShouldRecover(u8 battlerAtk, u8 battlerDef, u16 move, u8 healPercent) +{ + if (move == 0xFFFF || GetWhoStrikesFirst(battlerAtk, gBattlerTarget, TRUE) == 0) + { + // using item or user going first + s32 damage = AI_THINKING_STRUCT->simulatedDmg[battlerAtk][battlerDef][AI_THINKING_STRUCT->movesetIndex]; + s32 healAmount = (healPercent * damage) / 100; + if (gStatuses3[battlerAtk] & STATUS3_HEAL_BLOCK) + healAmount = 0; + + if (CanTargetFaintAi(battlerDef, battlerAtk) + && !CanTargetFaintAiWithMod(battlerDef, battlerAtk, healAmount, 0)) + return TRUE; // target can faint attacker unless they heal + else if (!CanTargetFaintAi(battlerDef, battlerAtk) && GetHealthPercentage(battlerAtk) < 60 && (Random() % 3)) + return TRUE; // target can't faint attacker at all, attacker health is about half, 2/3rds rate of encouraging healing + } + return FALSE; +} + +bool32 ShouldSetScreen(u8 battlerAtk, u8 battlerDef, u16 moveEffect) +{ + u8 atkSide = GetBattlerSide(battlerAtk); + + if (gSideTimers[atkSide].auroraVeilTimer != 0) + { + bool8 defHasPhysical = HasMoveWithSplit(battlerDef, SPLIT_PHYSICAL); + bool8 defHasSpecial = HasMoveWithSplit(battlerDef, SPLIT_SPECIAL); + + switch (moveEffect) + { + case EFFECT_AURORA_VEIL: + if (gBattleWeather & WEATHER_HAIL_ANY + && !((gSideStatuses[atkSide] & (SIDE_STATUS_REFLECT | SIDE_STATUS_LIGHTSCREEN)))) + return TRUE; + break; + case EFFECT_REFLECT: + if (SideHasMoveSplit(battlerDef, SPLIT_PHYSICAL)) + { + if (!defHasPhysical && !defHasSpecial) + return TRUE; // Target has no attacking moves so no point in doing Light Screen check + + if (defHasPhysical || !HasMoveEffect(battlerAtk, EFFECT_LIGHT_SCREEN) || !defHasSpecial) + return TRUE; + } + break; + case EFFECT_LIGHT_SCREEN: + if (SideHasMoveSplit(battlerDef, SPLIT_SPECIAL)) + { + if (!defHasPhysical && !defHasSpecial) + return TRUE; //Target has no attacking moves so no point in doing Light Screen check + + if (defHasSpecial || !HasMoveEffect(battlerAtk, EFFECT_REFLECT) || !defHasPhysical) + return TRUE; + } + break; + } + } + return FALSE; +} + // Partner Logic bool32 IsValidDoubleBattle(u8 battlerAtk) { @@ -1681,6 +2857,74 @@ bool32 PartnerMoveIsSameNoTarget(u8 battlerAtkPartner, u16 move, u16 partnerMove return FALSE; } +bool32 ShouldUseWishAromatherapy(u8 battlerAtk, u8 battlerDef, u16 move) +{ + u32 i; + u32 firstId, lastId; + struct Pokemon* party; + bool32 hasStatus = FALSE; + bool32 needHealing = FALSE; + + GetAIPartyIndexes(battlerAtk, &firstId, &lastId); + + if (GetBattlerSide(gActiveBattler) == B_SIDE_PLAYER) + party = gPlayerParty; + else + party = gEnemyParty; + + if (CountUsablePartyMons(battlerAtk) == 0 + && (CanTargetFaintAi(battlerDef, battlerAtk) || BattlerWillFaintFromSecondaryDamage(battlerAtk, AI_DATA->atkAbility))) + return FALSE; // Don't heal if last mon and will faint + + for (i = 0; i < PARTY_SIZE; i++) + { + u16 currHp = GetMonData(&party[i], MON_DATA_HP); + u16 maxHp = GetMonData(&party[i], MON_DATA_MAX_HP); + + if (!GetMonData(&party[i], MON_DATA_IS_EGG, NULL) && currHp > 0) + { + if ((currHp * 100) / maxHp < 65 // Less than 65% health remaining + && i >= firstId && i < lastId) // Can only switch to mon on your team + { + needHealing = TRUE; + } + + if (GetMonData(&party[i], MON_DATA_STATUS, NULL) != STATUS1_NONE) + { + if (move != MOVE_HEAL_BELL || GetMonAbility(&party[i]) != ABILITY_SOUNDPROOF) + hasStatus = TRUE; + } + } + } + + if (!IsDoubleBattle()) + { + switch (gBattleMoves[move].effect) + { + case EFFECT_WISH: + if (needHealing) + return TRUE; + break; + case EFFECT_HEAL_BELL: + if (hasStatus) + return TRUE; + } + } + else + { + switch (gBattleMoves[move].effect) + { + case EFFECT_WISH: + return ShouldRecover(battlerAtk, battlerDef, move, 50); // Switch recovery isn't good idea in doubles + case EFFECT_HEAL_BELL: + if (hasStatus) + return TRUE; + } + } + + return FALSE; +} + // party logic s32 AI_CalcPartyMonDamage(u16 move, u8 battlerAtk, u8 battlerDef, struct Pokemon *mon) { @@ -1760,3 +3004,236 @@ bool32 IsPartyFullyHealedExceptBattler(u8 battlerId) return TRUE; } +bool32 PartyHasMoveSplit(u8 battlerId, u8 split) +{ + u8 firstId, lastId; + struct Pokemon* party = GetBattlerPartyData(battlerId); + u32 i, j; + + for (i = 0; i < PARTY_SIZE; i++) + { + if (GetMonData(&party[i], MON_DATA_HP, NULL) == 0) + continue; + + for (j = 0; j < MAX_MON_MOVES; j++) + { + u16 move = GetMonData(&party[i], MON_DATA_MOVE1 + j, NULL); + u16 pp = GetMonData(&party[i], MON_DATA_PP1 + j, NULL); + + if (pp > 0 && move != MOVE_NONE) + { + //TODO - handle photon geyser, light that burns the sky + if (gBattleMoves[move].split == split) + return TRUE; + } + } + } + + return FALSE; +} + +bool32 SideHasMoveSplit(u8 battlerId, u8 split) +{ + if (IsDoubleBattle()) + { + if (HasMoveWithSplit(battlerId, split) || HasMoveWithSplit(BATTLE_PARTNER(battlerId), split)) + return TRUE; + } + else + { + if (HasMoveWithSplit(battlerId, split)) + return TRUE; + } + return FALSE; +} + +bool32 IsAbilityOfRating(u16 ability, s8 rating) +{ + if (sAiAbilityRatings[ability] >= rating) + return TRUE; +} + +s8 GetAbilityRating(u16 ability) +{ + return sAiAbilityRatings[ability]; +} + +static const u16 sRecycleEncouragedItems[] = +{ + ITEM_CHESTO_BERRY, + ITEM_LUM_BERRY, + ITEM_STARF_BERRY, + ITEM_SITRUS_BERRY, + ITEM_MICLE_BERRY, + ITEM_CUSTAP_BERRY, + ITEM_MENTAL_HERB, + #ifdef ITEM_EXPANSION + ITEM_FOCUS_SASH, + #endif + // TODO expand this +}; + +bool32 IsRecycleEncouragedItem(u16 item) +{ + u32 i; + for (i = 0; i < ARRAY_COUNT(sRecycleEncouragedItems); i++) + { + if (item == sRecycleEncouragedItems[i]) + return TRUE; + } + return FALSE; +} + +// score increases +#define STAT_UP_2_STAGE 8 +#define STAT_UP_STAGE 10 +void IncreaseStatUpScore(u8 battlerAtk, u8 battlerDef, u8 statId, s16 *score) +{ + if (AI_DATA->atkAbility == ABILITY_CONTRARY) + return; + + switch (statId) + { + case STAT_ATK: + if (HasMoveWithSplit(battlerAtk, SPLIT_PHYSICAL) && GetHealthPercentage(battlerAtk) > 40) + { + if (gBattleMons[battlerAtk].statStages[STAT_ATK] < STAT_UP_2_STAGE) + *score += 2; + else if (gBattleMons[battlerAtk].statStages[STAT_ATK] < STAT_UP_STAGE) + *(score)++; + } + if (HasMoveEffect(battlerAtk, EFFECT_FOUL_PLAY)) + *(score)++; + break; + case STAT_DEF: + if ((HasMoveWithSplit(battlerDef, SPLIT_PHYSICAL)|| IS_MOVE_PHYSICAL(gLastMoves[battlerDef])) + && GetHealthPercentage(battlerAtk) > 70) + { + if (gBattleMons[battlerAtk].statStages[STAT_DEF] < STAT_UP_2_STAGE) + *score += 2; // seems better to raise def at higher HP + else if (gBattleMons[battlerAtk].statStages[STAT_DEF] < STAT_UP_STAGE) + *(score)++; + } + break; + case STAT_SPEED: + if (IsAiFaster(AI_CHECK_SLOWER)) + *score += 3; + break; + case STAT_SPATK: + if (HasMoveWithSplit(battlerAtk, SPLIT_SPECIAL) && GetHealthPercentage(battlerAtk) > 40) + { + if (gBattleMons[battlerAtk].statStages[STAT_SPATK] < STAT_UP_2_STAGE) + *score += 2; + else if (gBattleMons[battlerAtk].statStages[STAT_SPATK] < STAT_UP_STAGE) + *(score)++; + } + break; + case STAT_SPDEF: + if ((HasMoveWithSplit(battlerDef, SPLIT_SPECIAL) || IS_MOVE_SPECIAL(gLastMoves[battlerDef])) + && GetHealthPercentage(battlerAtk) > 70) + { + if (gBattleMons[battlerAtk].statStages[STAT_SPDEF] < STAT_UP_2_STAGE) + *score += 2; // seems better to raise spdef at higher HP + else if (gBattleMons[battlerAtk].statStages[STAT_SPDEF] < STAT_UP_STAGE) + *(score)++; + } + break; + case STAT_ACC: + if (HasMoveWithLowAccuracy(battlerAtk, battlerDef, 80, TRUE, AI_DATA->atkAbility, AI_DATA->defAbility, AI_DATA->atkHoldEffect, AI_DATA->defHoldEffect)) + *score += 3; // has moves with less than 80% accuracy + else if (HasMoveWithLowAccuracy(battlerAtk, battlerDef, 90, TRUE, AI_DATA->atkAbility, AI_DATA->defAbility, AI_DATA->atkHoldEffect, AI_DATA->defHoldEffect)) + *score += 2; + break; + case STAT_EVASION: + if (!BattlerWillFaintFromWeather(battlerAtk, AI_DATA->atkAbility)) + { + if (!GetBattlerSecondaryDamage(battlerAtk) && !(gStatuses3[battlerAtk] & STATUS3_ROOTED)) + *score += 3; + else + *score += 2; + } + break; + } +} + +void IncreasePoisonScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) +{ + if (AI_CanPoison(battlerAtk, battlerDef, AI_DATA->defAbility, move, AI_DATA->partnerMove) && GetHealthPercentage(battlerDef) > 20) + { + if (!HasDamagingMove(battlerDef)) + *score += 2; + + if (HasMoveEffect(battlerAtk, EFFECT_PROTECT)) + (*score)++; // stall tactic + + if (HasMoveEffect(battlerAtk, EFFECT_VENOSHOCK) + || HasMoveEffect(battlerAtk, EFFECT_HEX) + || HasMoveEffect(battlerAtk, EFFECT_VENOM_DRENCH) + || AI_DATA->atkAbility == ABILITY_MERCILESS) + *score += 4; + else + *score += 2; + } +} + +void IncreaseBurnScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) +{ + if (AI_CanBurn(battlerAtk, battlerDef, AI_DATA->defAbility, AI_DATA->battlerAtkPartner, move, AI_DATA->partnerMove)) + { + (*score)++; // burning is good + if (HasMoveWithSplit(battlerDef, SPLIT_PHYSICAL)) + { + if (CanTargetFaintAi(battlerDef, battlerAtk)) + *score += 2; // burning the target to stay alive is cool + } + + if (HasMoveEffect(battlerAtk, EFFECT_HEX) || HasMoveEffect(AI_DATA->battlerAtkPartner, EFFECT_HEX)) + (*score)++; + } +} + +void IncreaseParalyzeScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) +{ + if (AI_CanParalyze(battlerAtk, battlerDef, AI_DATA->defAbility, move, AI_DATA->partnerMove)) + { + u8 atkSpeed = GetBattlerTotalSpeedStat(battlerAtk); + u8 defSpeed = GetBattlerTotalSpeedStat(battlerDef); + + if ((defSpeed >= atkSpeed && defSpeed / 2 < atkSpeed) // You'll go first after paralyzing foe + || HasMoveEffect(battlerAtk, EFFECT_HEX) + || HasMoveEffect(battlerAtk, EFFECT_FLINCH_HIT) + || gBattleMons[battlerDef].status2 & STATUS2_INFATUATION + || gBattleMons[battlerDef].status2 & STATUS2_CONFUSION) + *score += 4; + else + *score += 2; + } +} + +void IncreaseSleepScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) +{ + if (AI_CanPutToSleep(battlerAtk, battlerDef, AI_DATA->defAbility, move, AI_DATA->partnerMove)) + *score += 3; + + if ((HasMoveEffect(battlerAtk, EFFECT_DREAM_EATER) || HasMoveEffect(battlerAtk, EFFECT_NIGHTMARE)) + && !(HasMoveEffect(battlerDef, EFFECT_SNORE) || HasMoveEffect(battlerDef, EFFECT_SLEEP_TALK))) + (*score)++; + + if (HasMoveEffect(battlerAtk, EFFECT_HEX) || HasMoveEffect(AI_DATA->battlerAtkPartner, EFFECT_HEX)) + (*score)++; +} + +void IncreaseConfusionScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) +{ + if (AI_CanConfuse(battlerAtk, battlerDef, AI_DATA->defAbility, AI_DATA->battlerAtkPartner, move, AI_DATA->partnerMove) + && AI_DATA->defHoldEffect != HOLD_EFFECT_CURE_CONFUSION + && AI_DATA->defHoldEffect != HOLD_EFFECT_CURE_STATUS) + { + if (gBattleMons[battlerDef].status1 & STATUS1_PARALYSIS + || gBattleMons[battlerDef].status2 & STATUS2_INFATUATION + || (AI_DATA->atkAbility == ABILITY_SERENE_GRACE && HasMoveEffect(battlerAtk, EFFECT_FLINCH_HIT))) + *score += 3; + else + *score += 2; + } +} diff --git a/src/item.c b/src/item.c index 0b6366048..a0ab61779 100644 --- a/src/item.c +++ b/src/item.c @@ -957,3 +957,26 @@ u8 ItemId_GetSecondaryId(u16 itemId) { return gItems[SanitizeItemId(itemId)].secondaryId; } + +bool32 IsPinchBerryItemEffect(u16 holdEffect) +{ + switch (holdEffect) + { + case HOLD_EFFECT_ATTACK_UP: + case HOLD_EFFECT_DEFENSE_UP: + case HOLD_EFFECT_SPEED_UP: + case HOLD_EFFECT_SP_ATTACK_UP: + case HOLD_EFFECT_SP_DEFENSE_UP: + case HOLD_EFFECT_CRITICAL_UP: + case HOLD_EFFECT_RANDOM_STAT_UP: + #ifdef HOLD_EFFECT_CUSTAP_BERRY + case HOLD_EFFECT_CUSTAP_BERRY: + #endif + #ifdef HOLD_EFFECT_MICLE_BERRY + case HOLD_EFFECT_MICLE_BERRY: + #endif + return TRUE; + } + + return FALSE; +}