From e75a8fb67c1af191630a0a860b6e7d61f7d8c48b Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Mon, 27 Sep 2021 21:03:27 -0400 Subject: [PATCH 01/46] merge ai move and item decisions. AI will prefer to defeat player over healing --- include/battle_ai_main.h | 7 +- include/battle_ai_switch_items.h | 2 +- include/battle_ai_util.h | 9 +- include/battle_main.h | 3 + src/battle_ai_main.c | 136 +++++--------- src/battle_ai_switch_items.c | 250 ++++++++++++++++--------- src/battle_ai_util.c | 75 ++++---- src/battle_controller_opponent.c | 64 +++---- src/battle_controller_player_partner.c | 51 +++-- src/battle_main.c | 41 ++-- 10 files changed, 350 insertions(+), 288 deletions(-) diff --git a/include/battle_ai_main.h b/include/battle_ai_main.h index 66c614f78..c98cedbd9 100644 --- a/include/battle_ai_main.h +++ b/include/battle_ai_main.h @@ -3,9 +3,10 @@ // return values for BattleAI_ChooseMoveOrAction // 0 - 3 are move idx -#define AI_CHOICE_FLEE 4 -#define AI_CHOICE_WATCH 5 -#define AI_CHOICE_SWITCH 7 +#define AI_CHOICE_FLEE 4 +#define AI_CHOICE_WATCH 5 +#define AI_CHOICE_SWITCH 7 +#define AI_CHOICE_USE_ITEM 8 #define RETURN_SCORE_PLUS(val) \ { \ diff --git a/include/battle_ai_switch_items.h b/include/battle_ai_switch_items.h index 70dc41b34..6a8042cc9 100644 --- a/include/battle_ai_switch_items.h +++ b/include/battle_ai_switch_items.h @@ -32,7 +32,7 @@ enum { }; void GetAIPartyIndexes(u32 battlerId, s32 *firstId, s32 *lastId); -void AI_TrySwitchOrUseItem(void); +u8 AI_TrySwitchOrUseItem(u8 currAction); u8 GetMostSuitableMonToSwitchInto(void); bool32 ShouldSwitch(void); diff --git a/include/battle_ai_util.h b/include/battle_ai_util.h index b8cd53c07..0da11b568 100644 --- a/include/battle_ai_util.h +++ b/include/battle_ai_util.h @@ -1,9 +1,9 @@ #ifndef GUARD_BATTLE_AI_UTIL_H #define GUARD_BATTLE_AI_UTIL_H -// for IsAiFaster -#define AI_CHECK_FASTER 0 // if_user_faster -#define AI_CHECK_SLOWER 1 // if_target_faster +// for AI_WhoStrikesFirst +#define AI_IS_FASTER 0 +#define AI_IS_SLOWER 1 #define FOE(battler) ((battler ^ BIT_SIDE) & BIT_SIDE) @@ -21,12 +21,13 @@ void SaveBattlerData(u8 battlerId); void SetBattlerData(u8 battlerId); void RestoreBattlerData(u8 battlerId); +bool32 WillAIStrikeFirst(void); u32 GetTotalBaseStat(u32 species); bool32 IsTruantMonVulnerable(u32 battlerAI, u32 opposingBattler); bool32 AtMaxHp(u8 battler); u32 GetHealthPercentage(u8 battler); bool32 IsBattlerTrapped(u8 battler, bool8 switching); -bool32 IsAiFaster(u8 battler); +u8 AI_WhoStrikesFirst(u8 battlerAI, u8 battler2); 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); diff --git a/include/battle_main.h b/include/battle_main.h index bf95ca156..184786ea5 100644 --- a/include/battle_main.h +++ b/include/battle_main.h @@ -21,6 +21,8 @@ struct UnknownPokemonStruct4 /*0x1D*/ u8 language; }; +struct ChooseMoveStruct; + #define TYPE_NAME_LENGTH 6 #define ABILITY_NAME_LENGTH 12 @@ -70,6 +72,7 @@ void RunBattleScriptCommands(void); bool8 TryRunFromBattle(u8 battlerId); void SpecialStatusesClear(void); void SetTypeBeforeUsingMove(u16 move, u8 battlerAtk); +void FillChooseMoveStruct(struct ChooseMoveStruct *moveInfo); extern struct UnknownPokemonStruct4 gMultiPartnerParty[MULTI_PARTY_SIZE]; diff --git a/src/battle_ai_main.c b/src/battle_ai_main.c index 03472271d..8cab08bb3 100644 --- a/src/battle_ai_main.c +++ b/src/battle_ai_main.c @@ -210,6 +210,9 @@ u8 BattleAI_ChooseMoveOrAction(void) else ret = ChooseMoveOrAction_Doubles(); + AI_THINKING_STRUCT->movesetIndex = ret; + AI_THINKING_STRUCT->moveConsidered = gBattleMons[sBattler_AI].moves[AI_THINKING_STRUCT->movesetIndex]; + ret = AI_TrySwitchOrUseItem(ret); gCurrentMove = savedCurrentMove; return ret; } @@ -272,47 +275,6 @@ static u8 ChooseMoveOrAction_Singles(void) return AI_CHOICE_WATCH; gActiveBattler = sBattler_AI; - // If can switch. - if (CountUsablePartyMons(sBattler_AI) > 0 - && !IsAbilityPreventingEscape(sBattler_AI) - && !(gBattleMons[gActiveBattler].status2 & (STATUS2_WRAPPED | STATUS2_ESCAPE_PREVENTION)) - && !(gStatuses3[gActiveBattler] & STATUS3_ROOTED) - && !(gBattleTypeFlags & (BATTLE_TYPE_ARENA | BATTLE_TYPE_PALACE)) - && AI_THINKING_STRUCT->aiFlags & (AI_FLAG_CHECK_VIABILITY | AI_FLAG_CHECK_BAD_MOVE | AI_FLAG_TRY_TO_FAINT | AI_FLAG_PREFER_BATON_PASS)) - { - // Consider switching if all moves are worthless to use. - if (GetTotalBaseStat(gBattleMons[sBattler_AI].species) >= 310 // Mon is not weak. - && gBattleMons[sBattler_AI].hp >= gBattleMons[sBattler_AI].maxHP / 2) - { - s32 cap = AI_THINKING_STRUCT->aiFlags & (AI_FLAG_CHECK_VIABILITY) ? 95 : 93; - for (i = 0; i < MAX_MON_MOVES; i++) - { - if (AI_THINKING_STRUCT->score[i] > cap) - break; - } - - if (i == MAX_MON_MOVES && GetMostSuitableMonToSwitchInto() != PARTY_SIZE) - { - AI_THINKING_STRUCT->switchMon = TRUE; - return AI_CHOICE_SWITCH; - } - } - - // Consider switching if your mon with truant is bodied by Protect spam. - // Or is using a double turn semi invulnerable move(such as Fly) and is faster. - if (GetBattlerAbility(sBattler_AI) == ABILITY_TRUANT - && IsTruantMonVulnerable(sBattler_AI, gBattlerTarget) - && gDisableStructs[sBattler_AI].truantCounter - && gBattleMons[sBattler_AI].hp >= gBattleMons[sBattler_AI].maxHP / 2) - { - if (GetMostSuitableMonToSwitchInto() != PARTY_SIZE) - { - AI_THINKING_STRUCT->switchMon = TRUE; - return AI_CHOICE_SWITCH; - } - } - } - numOfBestMoves = 1; currentMoveArray[0] = AI_THINKING_STRUCT->score[0]; consideredMoveArray[0] = 0; @@ -546,7 +508,7 @@ static s16 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } // check off screen - if (IsSemiInvulnerable(battlerDef, move) && moveEffect != EFFECT_SEMI_INVULNERABLE && GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) != 1) + if (IsSemiInvulnerable(battlerDef, move) && moveEffect != EFFECT_SEMI_INVULNERABLE && AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_FASTER) RETURN_SCORE_MINUS(20); // if target off screen and we go first, don't use move // check if negates type @@ -1207,7 +1169,7 @@ static s16 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) && (B_MENTAL_HERB >= GEN_5 && AI_DATA->defHoldEffect != HOLD_EFFECT_MENTAL_HERB) && !PartnerHasSameMoveEffectWithoutTarget(AI_DATA->battlerAtkPartner, move, AI_DATA->partnerMove)) { - if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0) // attacker should go first + if (AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_FASTER) // Attacker should go first { if (gLastMoves[battlerDef] == MOVE_NONE || gLastMoves[battlerDef] == 0xFFFF) score -= 10; // no anticipated move to disable @@ -1227,7 +1189,7 @@ static s16 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) && (B_MENTAL_HERB >= GEN_5 && AI_DATA->defHoldEffect != HOLD_EFFECT_MENTAL_HERB) && !DoesPartnerHaveSameMoveEffect(AI_DATA->battlerAtkPartner, battlerDef, move, AI_DATA->partnerMove)) { - if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0) // attacker should go first + if (AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_FASTER) // Attacker should go first { if (gLastMoves[battlerDef] == MOVE_NONE || gLastMoves[battlerDef] == 0xFFFF) score -= 10; // no anticipated move to encore @@ -1668,7 +1630,7 @@ static s16 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) break; case EFFECT_SPITE: case EFFECT_MIMIC: - if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0) // attacker should go first + if (AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_FASTER) // Attacker should go first { if (gLastMoves[battlerDef] == MOVE_NONE || gLastMoves[battlerDef] == 0xFFFF) @@ -1825,7 +1787,7 @@ static s16 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) 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 + && AI_WhoStrikesFirst(AI_DATA->battlerAtkPartner, battlerAtk) == AI_IS_FASTER) // partner is going to set up before the potential Defog { score -= 10; break; // Don't use Defog if partner is going to set up hazards @@ -1863,7 +1825,7 @@ static s16 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) break; case EFFECT_SEMI_INVULNERABLE: if (predictedMove != MOVE_NONE - && GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1 + && AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_SLOWER && gBattleMoves[predictedMove].effect == EFFECT_SEMI_INVULNERABLE) score -= 10; // Don't Fly/dig/etc if opponent is going to fly/dig/etc after you @@ -2044,7 +2006,7 @@ static s16 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) case EFFECT_ME_FIRST: if (predictedMove != MOVE_NONE) { - if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1) + if (AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_SLOWER) score -= 10; // Target is predicted to go first, Me First will fail else return AI_CheckBadMove(battlerAtk, battlerDef, predictedMove, score); @@ -2231,7 +2193,7 @@ static s16 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } break; case EFFECT_ELECTRIFY: - if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0 + if (AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_FASTER //|| GetMoveTypeSpecial(battlerDef, predictedMove) == TYPE_ELECTRIC // Move will already be electric type || PartnerMoveIsSameAsAttacker(AI_DATA->battlerAtkPartner, battlerDef, move, AI_DATA->partnerMove)) score -= 10; @@ -2260,7 +2222,7 @@ static s16 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) case EFFECT_INSTRUCT: { u16 instructedMove; - if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1) + if (AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_SLOWER) instructedMove = predictedMove; else instructedMove = gLastMoves[battlerDef]; @@ -2300,21 +2262,21 @@ static s16 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) break; case EFFECT_QUASH: if (!isDoubleBattle - || GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1 + || AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_SLOWER || PartnerMoveIsSameAsAttacker(AI_DATA->battlerAtkPartner, battlerDef, move, AI_DATA->partnerMove)) score -= 10; break; case EFFECT_AFTER_YOU: if (!IsTargetingPartner(battlerAtk, battlerDef) || !isDoubleBattle - || GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1 + || AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_SLOWER || PartnerMoveIsSameAsAttacker(AI_DATA->battlerAtkPartner, battlerDef, move, AI_DATA->partnerMove)) score -= 10; break; case EFFECT_SUCKER_PUNCH: if (predictedMove != MOVE_NONE) { - if (IS_MOVE_STATUS(predictedMove) || GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1) // opponent going first + if (IS_MOVE_STATUS(predictedMove) || AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_SLOWER) // Opponent going first score -= 10; } break; @@ -2362,11 +2324,11 @@ static s16 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) score--; break; case EFFECT_VITAL_THROW: - if (IsAiFaster(AI_CHECK_FASTER) && GetHealthPercentage(battlerAtk) < 40) + if (WillAIStrikeFirst() && GetHealthPercentage(battlerAtk) < 40) score--; // don't want to move last break; case EFFECT_FLAIL: - if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1 // opponent should go first + if (AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_SLOWER // Opponent should go first || GetHealthPercentage(battlerAtk) > 50) score -= 4; break; @@ -2417,7 +2379,7 @@ static s16 AI_TryToFaint(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) if (CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0) && gBattleMoves[move].effect != EFFECT_EXPLOSION) { // this move can faint the target - if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0 || GetMovePriority(battlerAtk, move) > 0) + if (!WillAIStrikeFirst() || GetMovePriority(battlerAtk, move) > 0) score += 4; // we go first or we're using priority move else score += 2; @@ -2446,7 +2408,7 @@ static s16 AI_TryToFaint(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } //AI_TryToFaint_CheckIfDanger - if (!IsAiFaster(AI_CHECK_FASTER) && CanTargetFaintAi(battlerDef, battlerAtk)) + if (!WillAIStrikeFirst() && CanTargetFaintAi(battlerDef, battlerAtk)) { // AI_TryToFaint_Danger if (GetMoveDamageResult(move) != MOVE_POWER_BEST) score--; @@ -2493,7 +2455,7 @@ static s16 AI_DoubleBattle(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) // Ally decided to use Frost Breath on us. we must have Anger Point as our ability if (AI_DATA->atkAbility == ABILITY_ANGER_POINT) { - if (GetWhoStrikesFirst(battlerAtk, battlerAtkPartner, TRUE) == 1) // partner moving first + if (AI_WhoStrikesFirst(battlerAtk, battlerAtkPartner) == AI_IS_SLOWER) // Partner moving first { // discourage raising our attack since it's about to be maxed out if (IsAttackBoostMoveEffect(effect)) @@ -2760,22 +2722,22 @@ static s16 AI_DoubleBattle(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) case EFFECT_INSTRUCT: { u16 instructedMove; - if (GetWhoStrikesFirst(battlerAtk, battlerAtkPartner, TRUE) == 0) + if (AI_WhoStrikesFirst(battlerAtk, battlerAtkPartner) == AI_IS_FASTER) instructedMove = AI_DATA->partnerMove; else instructedMove = gLastMoves[battlerAtkPartner]; if (instructedMove != MOVE_NONE && !IS_MOVE_STATUS(instructedMove) - && gBattleMoves[instructedMove].target & (MOVE_TARGET_BOTH | MOVE_TARGET_FOES_AND_ALLY)) //Use instruct on multi-target moves + && gBattleMoves[instructedMove].target & (MOVE_TARGET_BOTH | MOVE_TARGET_FOES_AND_ALLY)) // Use instruct on multi-target moves { RETURN_SCORE_PLUS(1); } } break; case EFFECT_AFTER_YOU: - if (GetWhoStrikesFirst(battlerAtkPartner, FOE(battlerAtkPartner), TRUE) == 1 // opponent mon 1 goes before partner - || GetWhoStrikesFirst(battlerAtkPartner, BATTLE_PARTNER(FOE(battlerAtkPartner)), TRUE) == 1) // opponent mon 2 goes before partner + if (AI_WhoStrikesFirst(battlerAtkPartner, FOE(battlerAtkPartner) == AI_IS_SLOWER) // Opponent mon 1 goes before partner + || AI_WhoStrikesFirst(battlerAtkPartner, BATTLE_PARTNER(FOE(battlerAtkPartner)) == AI_IS_SLOWER)) // Opponent mon 2 goes before partner { if (gBattleMoves[AI_DATA->partnerMove].effect == EFFECT_COUNTER || gBattleMoves[AI_DATA->partnerMove].effect == EFFECT_MIRROR_COAT) break; // These moves need to go last @@ -2801,7 +2763,9 @@ static s16 AI_DoubleBattle(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) case EFFECT_EARTHQUAKE: case EFFECT_MAGNITUDE: if (!IsBattlerGrounded(battlerAtkPartner) - || (GetWhoStrikesFirst(battlerAtk, battlerAtkPartner, TRUE) == 1 && IsUngroundingEffect(gBattleMoves[AI_DATA->partnerMove].effect))) + || (IsBattlerGrounded(battlerAtkPartner) + && AI_WhoStrikesFirst(battlerAtk, battlerAtkPartner) == AI_IS_SLOWER + && IsUngroundingEffect(gBattleMoves[AI_DATA->partnerMove].effect))) score += 2; else if (IS_BATTLER_OF_TYPE(battlerAtkPartner, TYPE_FIRE) || IS_BATTLER_OF_TYPE(battlerAtkPartner, TYPE_ELECTRIC) @@ -2852,9 +2816,9 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) // check already dead if (!IsBattlerIncapacitated(battlerDef, AI_DATA->defAbility) && CanTargetFaintAi(battlerAtk, battlerDef) - && GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1) // opponent should go first + && AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_SLOWER) // Opponent should go first { - if (atkPriority > 0) + if (atkPriority > 0) score++; else score--; @@ -2896,7 +2860,7 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) { case ABILITY_MOXIE: case ABILITY_BEAST_BOOST: - if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0) // attacker should go first + if (AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_FASTER) // Attacker should go first { if (CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) score += 8; // prioritize killing target for stat boost @@ -2984,7 +2948,7 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) break; case EFFECT_SPEED_UP: case EFFECT_SPEED_UP_2: - if (IsAiFaster(AI_CHECK_SLOWER)) + if (!WillAIStrikeFirst()) { if (!AI_RandLessThan(70)) score += 3; @@ -3082,7 +3046,7 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) break; case EFFECT_SPEED_DOWN: case EFFECT_SPEED_DOWN_2: - if (IsAiFaster(AI_CHECK_FASTER)) + if (WillAIStrikeFirst()) score -= 3; else if (!AI_RandLessThan(70)) score += 2; @@ -3319,7 +3283,7 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) score += 2; break; case EFFECT_SPEED_DOWN_HIT: - if (IsAiFaster(AI_CHECK_FASTER)) + if (WillAIStrikeFirst()) score -= 2; else if (!AI_RandLessThan(70)) score++; @@ -3351,7 +3315,7 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) score++; break; case EFFECT_MIMIC: - if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0) + if (AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_FASTER) { if (gLastMoves[battlerDef] != MOVE_NONE && gLastMoves[battlerDef] != 0xFFFF) return AI_CheckViability(battlerAtk, battlerDef, gLastMoves[battlerDef], score); @@ -3410,7 +3374,7 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) if (gDisableStructs[battlerDef].disableTimer == 0 && (B_MENTAL_HERB >= GEN_5 && AI_DATA->defHoldEffect != HOLD_EFFECT_MENTAL_HERB)) // mental herb { - if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0) // AI goes first + if (AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_FASTER) // AI goes first { if (gLastMoves[battlerDef] != MOVE_NONE && gLastMoves[battlerDef] != 0xFFFF) @@ -3460,11 +3424,11 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) score++; break; case EFFECT_SPEED_UP_HIT: - if (AI_DATA->atkAbility == ABILITY_SERENE_GRACE && AI_DATA->defAbility != ABILITY_CONTRARY && IsAiFaster(AI_CHECK_SLOWER)) + if (AI_DATA->atkAbility == ABILITY_SERENE_GRACE && AI_DATA->defAbility != ABILITY_CONTRARY && !WillAIStrikeFirst()) score += 3; break; case EFFECT_DESTINY_BOND: - if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0 && CanTargetFaintAi(battlerDef, battlerAtk)) + if (AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_FASTER && CanTargetFaintAi(battlerDef, battlerAtk)) score += 3; break; case EFFECT_SPITE: @@ -3712,7 +3676,7 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) && AI_DATA->atkAbility != ABILITY_CONTRARY && CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) { - if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0) // Attacker goes first + if (AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_FASTER) // Attacker goes first score += 9; else score += 3; @@ -3757,7 +3721,7 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) score++; if (predictedMove != MOVE_NONE && !isDoubleBattle) { - if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0) // Attacker goes first + if (AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_FASTER) // Attacker goes first { if (gBattleMoves[predictedMove].effect == EFFECT_EXPLOSION || gBattleMoves[predictedMove].effect == EFFECT_PROTECT) @@ -3824,7 +3788,7 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) break; case EFFECT_ATTRACT: if (!isDoubleBattle && BattlerWillFaintFromSecondaryDamage(battlerDef, AI_DATA->defAbility) - && GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1) // Target goes first + && AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_SLOWER) // Target goes first break; // Don't use if the attract won't have a change to activate if (gBattleMons[battlerDef].status1 & STATUS1_ANY @@ -3869,7 +3833,7 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) 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 + && AI_WhoStrikesFirst(battlerAtk, AI_DATA->battlerAtkPartner) == AI_IS_SLOWER) // Partner going first break; // Don't use Defog if partner is going to set up hazards } @@ -4346,13 +4310,13 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) score++; break; case EFFECT_THROAT_CHOP: - if (predictedMove != MOVE_NONE && TestMoveFlags(predictedMove, FLAG_SOUND) && GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0) + if (predictedMove != MOVE_NONE && TestMoveFlags(predictedMove, FLAG_SOUND) && AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_FASTER) 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)) + if (AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_FASTER && 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))) @@ -4388,7 +4352,7 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) break; case EFFECT_QUASH: if (isDoubleBattle - && GetWhoStrikesFirst(AI_DATA->battlerAtkPartner, battlerDef, TRUE) == 1) // Attacker partner wouldn't go before target + && AI_WhoStrikesFirst(AI_DATA->battlerAtkPartner, battlerDef) == AI_IS_SLOWER) // Attacker partner wouldn't go before target score++; break; case EFFECT_TAILWIND: @@ -4410,8 +4374,8 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) 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 (AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_FASTER) // Attacker goes first + { if (gBattleMoves[predictedMove].type == TYPE_GROUND) score += 3; // Cause the enemy's move to fail break; @@ -4425,7 +4389,7 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } break; case EFFECT_CAMOUFLAGE: - if (predictedMove != MOVE_NONE && GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0 // Attacker goes first + if (predictedMove != MOVE_NONE && AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_FASTER // Attacker goes first && !IS_MOVE_STATUS(move) && AI_GetTypeEffectiveness(predictedMove, battlerDef, battlerAtk) != AI_EFFECTIVENESS_x0) score++; break; @@ -4468,7 +4432,7 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } break; case EFFECT_FLAIL: - if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0) // Ai goes first + if (AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_FASTER) // Ai goes first { if (GetHealthPercentage(battlerAtk) < 20) score++; @@ -4510,7 +4474,7 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) score += 2; break; case EFFECT_ENDEAVOR: - if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1) // Opponent faster + if (AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_SLOWER) // Opponent faster { if (GetHealthPercentage(battlerAtk) < 40) score++; @@ -4541,7 +4505,7 @@ static s16 AI_SetupFirstTurn(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) return score; if (AI_THINKING_STRUCT->aiFlags & AI_FLAG_SMART_SWITCHING - && GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1 + && AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_SLOWER && CanTargetFaintAi(battlerDef, battlerAtk) && GetMovePriority(battlerAtk, move) == 0) { diff --git a/src/battle_ai_switch_items.c b/src/battle_ai_switch_items.c index e8e399935..257094536 100644 --- a/src/battle_ai_switch_items.c +++ b/src/battle_ai_switch_items.c @@ -12,11 +12,82 @@ #include "constants/item_effects.h" #include "constants/items.h" #include "constants/moves.h" +#include "constants/battle_ai.h" // this file's functions static bool8 HasSuperEffectiveMoveAgainstOpponents(bool8 noRng); static bool8 FindMonWithFlagsAndSuperEffective(u16 flags, u8 moduloPercent); static bool8 ShouldUseItem(void); +static bool32 AI_ShouldHeal(u8 healAmount); + +// Functions +u8 AI_TrySwitchOrUseItem(u8 currAction) +{ + struct Pokemon *party; + u8 battlerIn1, battlerIn2; + s32 firstId; + s32 lastId; // + 1 + u8 battlerIdentity = GetBattlerPosition(gActiveBattler); + + if (!(gBattleTypeFlags & BATTLE_TYPE_TRAINER)) + return currAction; + + if (GetBattlerSide(gActiveBattler) == B_SIDE_PLAYER) + party = gPlayerParty; // Player's partner + else + party = gEnemyParty; // Enemy trainer + + // Switching logic + if (ShouldSwitch()) + { + if (*(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) == PARTY_SIZE) + { + s32 monToSwitchId = GetMostSuitableMonToSwitchInto(); + if (monToSwitchId == PARTY_SIZE) + { + if (!(gBattleTypeFlags & BATTLE_TYPE_DOUBLE)) + { + battlerIn1 = GetBattlerAtPosition(battlerIdentity); + battlerIn2 = battlerIn1; + } + else + { + battlerIn1 = GetBattlerAtPosition(battlerIdentity); + battlerIn2 = GetBattlerAtPosition(battlerIdentity ^ BIT_FLANK); + } + + GetAIPartyIndexes(gActiveBattler, &firstId, &lastId); + + for (monToSwitchId = firstId; monToSwitchId < lastId; monToSwitchId++) + { + if (GetMonData(&party[monToSwitchId], MON_DATA_HP) == 0) + continue; + if (monToSwitchId == gBattlerPartyIndexes[battlerIn1]) + continue; + if (monToSwitchId == gBattlerPartyIndexes[battlerIn2]) + continue; + if (monToSwitchId == *(gBattleStruct->monToSwitchIntoId + battlerIn1)) + continue; + if (monToSwitchId == *(gBattleStruct->monToSwitchIntoId + battlerIn2)) + continue; + + break; + } + } + + *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) = monToSwitchId; + } + + *(gBattleStruct->monToSwitchIntoId + gActiveBattler) = *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler); + return AI_CHOICE_SWITCH; + } + + // Item Logic + if (ShouldUseItem()) + return AI_CHOICE_USE_ITEM; + + return currAction; +} void GetAIPartyIndexes(u32 battlerId, s32 *firstId, s32 *lastId) { @@ -39,11 +110,52 @@ void GetAIPartyIndexes(u32 battlerId, s32 *firstId, s32 *lastId) static bool8 ShouldSwitchIfAllBadMoves(void) { - if (gBattleResources->ai->switchMon) + u32 i; + + if (AI_THINKING_STRUCT->aiFlags & (AI_FLAG_CHECK_VIABILITY | AI_FLAG_CHECK_BAD_MOVE | AI_FLAG_TRY_TO_FAINT | AI_FLAG_PREFER_BATON_PASS)) { - gBattleResources->ai->switchMon = 0; + if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE) + { + // TODO double battle bad move switching logic + } + else + { + // Single battle. gActiveBattler is the enemy's battler id + if (GetTotalBaseStat(gBattleMons[gActiveBattler].species) >= 310 // Mon is not weak. + && gBattleMons[gActiveBattler].hp >= gBattleMons[gActiveBattler].maxHP / 2) + { + s32 cap = AI_THINKING_STRUCT->aiFlags & (AI_FLAG_CHECK_VIABILITY) ? 95 : 93; + for (i = 0; i < MAX_MON_MOVES; i++) + { + if (AI_THINKING_STRUCT->score[i] > cap) + break; + } + + if (i == MAX_MON_MOVES && GetMostSuitableMonToSwitchInto() != PARTY_SIZE) + { + AI_THINKING_STRUCT->switchMon = TRUE; + } + } + + // Consider switching if your mon with truant is bodied by Protect spam. + // Or is using a double turn semi invulnerable move(such as Fly) and is faster. + if (GetBattlerAbility(gActiveBattler) == ABILITY_TRUANT + && IsTruantMonVulnerable(gActiveBattler, gBattlerTarget) + && gDisableStructs[gActiveBattler].truantCounter + && gBattleMons[gActiveBattler].hp >= gBattleMons[gActiveBattler].maxHP / 2) + { + if (GetMostSuitableMonToSwitchInto() != PARTY_SIZE) + { + AI_THINKING_STRUCT->switchMon = TRUE; + } + } + } + } + + if (AI_THINKING_STRUCT->switchMon) + { + AI_THINKING_STRUCT->switchMon = FALSE; *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) = PARTY_SIZE; - BtlController_EmitTwoReturnValues(1, B_ACTION_SWITCH, 0); return TRUE; } else @@ -58,7 +170,6 @@ static bool8 ShouldSwitchIfPerishSong(void) && gDisableStructs[gActiveBattler].perishSongTimer == 0) { *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) = PARTY_SIZE; - BtlController_EmitTwoReturnValues(1, B_ACTION_SWITCH, 0); return TRUE; } else @@ -125,7 +236,6 @@ static bool8 ShouldSwitchIfWonderGuard(void) { // We found a mon. *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) = i; - BtlController_EmitTwoReturnValues(1, B_ACTION_SWITCH, 0); return TRUE; } } @@ -216,7 +326,6 @@ static bool8 FindMonThatAbsorbsOpponentsMove(void) { // we found a mon. *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) = i; - BtlController_EmitTwoReturnValues(1, B_ACTION_SWITCH, 0); return TRUE; } } @@ -236,13 +345,11 @@ static bool8 ShouldSwitchIfNaturalCure(void) if ((gLastLandedMoves[gActiveBattler] == 0 || gLastLandedMoves[gActiveBattler] == 0xFFFF) && Random() & 1) { *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) = PARTY_SIZE; - BtlController_EmitTwoReturnValues(1, B_ACTION_SWITCH, 0); return TRUE; } else if (gBattleMoves[gLastLandedMoves[gActiveBattler]].power == 0 && Random() & 1) { *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) = PARTY_SIZE; - BtlController_EmitTwoReturnValues(1, B_ACTION_SWITCH, 0); return TRUE; } @@ -254,7 +361,6 @@ static bool8 ShouldSwitchIfNaturalCure(void) if (Random() & 1) { *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) = PARTY_SIZE; - BtlController_EmitTwoReturnValues(1, B_ACTION_SWITCH, 0); return TRUE; } @@ -407,7 +513,6 @@ static bool8 FindMonWithFlagsAndSuperEffective(u16 flags, u8 moduloPercent) if (AI_GetTypeEffectiveness(move, gActiveBattler, battlerIn1) >= UQ_4_12(2.0) && Random() % moduloPercent == 0) { *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) = i; - BtlController_EmitTwoReturnValues(1, B_ACTION_SWITCH, 0); return TRUE; } } @@ -436,6 +541,11 @@ bool32 ShouldSwitch(void) return FALSE; availableToSwitch = 0; + AI_THINKING_STRUCT->switchMon = FALSE; + + if (CountUsablePartyMons(gActiveBattler) == 0) // No pokemon to switch to + return FALSE; + if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE) { battlerIn1 = gActiveBattler; @@ -500,73 +610,6 @@ bool32 ShouldSwitch(void) return FALSE; } -void AI_TrySwitchOrUseItem(void) -{ - struct Pokemon *party; - u8 battlerIn1, battlerIn2; - s32 firstId; - s32 lastId; // + 1 - u8 battlerIdentity = GetBattlerPosition(gActiveBattler); - - if (GetBattlerSide(gActiveBattler) == B_SIDE_PLAYER) - party = gPlayerParty; - else - party = gEnemyParty; - - if (gBattleTypeFlags & BATTLE_TYPE_TRAINER) - { - if (ShouldSwitch()) - { - if (*(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) == PARTY_SIZE) - { - s32 monToSwitchId = GetMostSuitableMonToSwitchInto(); - if (monToSwitchId == PARTY_SIZE) - { - if (!(gBattleTypeFlags & BATTLE_TYPE_DOUBLE)) - { - battlerIn1 = GetBattlerAtPosition(battlerIdentity); - battlerIn2 = battlerIn1; - } - else - { - battlerIn1 = GetBattlerAtPosition(battlerIdentity); - battlerIn2 = GetBattlerAtPosition(battlerIdentity ^ BIT_FLANK); - } - - GetAIPartyIndexes(gActiveBattler, &firstId, &lastId); - - for (monToSwitchId = firstId; monToSwitchId < lastId; monToSwitchId++) - { - if (GetMonData(&party[monToSwitchId], MON_DATA_HP) == 0) - continue; - if (monToSwitchId == gBattlerPartyIndexes[battlerIn1]) - continue; - if (monToSwitchId == gBattlerPartyIndexes[battlerIn2]) - continue; - if (monToSwitchId == *(gBattleStruct->monToSwitchIntoId + battlerIn1)) - continue; - if (monToSwitchId == *(gBattleStruct->monToSwitchIntoId + battlerIn2)) - continue; - - break; - } - } - - *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) = monToSwitchId; - } - - *(gBattleStruct->monToSwitchIntoId + gActiveBattler) = *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler); - return; - } - else if (ShouldUseItem()) - { - return; - } - } - - BtlController_EmitTwoReturnValues(1, B_ACTION_USE_MOVE, (gActiveBattler ^ BIT_SIDE) << 8); -} - // If there are two(or more) mons to choose from, always choose one that has baton pass // as most often it can't do much on its own. static u32 GetBestMonBatonPass(struct Pokemon *party, int firstId, int lastId, u8 invalidMons, int aliveCount) @@ -839,20 +882,10 @@ static bool8 ShouldUseItem(void) switch (*(gBattleStruct->AI_itemType + gActiveBattler / 2)) { case AI_ITEM_FULL_RESTORE: - if (gBattleMons[gActiveBattler].hp >= gBattleMons[gActiveBattler].maxHP / 4) - break; - if (gBattleMons[gActiveBattler].hp == 0) - break; - shouldUse = TRUE; + shouldUse = AI_ShouldHeal(0); break; case AI_ITEM_HEAL_HP: - paramOffset = GetItemEffectParamOffset(item, 4, 4); - if (paramOffset == 0) - break; - if (gBattleMons[gActiveBattler].hp == 0) - break; - if (gBattleMons[gActiveBattler].hp < gBattleMons[gActiveBattler].maxHP / 4 || gBattleMons[gActiveBattler].maxHP - gBattleMons[gActiveBattler].hp > itemEffects[paramOffset]) - shouldUse = TRUE; + shouldUse = AI_ShouldHeal(itemEffects[GetItemEffectParamOffset(item, 4, 4)]); break; case AI_ITEM_CURE_CONDITION: *(gBattleStruct->AI_itemFlags + gActiveBattler / 2) = 0; @@ -934,7 +967,6 @@ static bool8 ShouldUseItem(void) if (shouldUse) { - BtlController_EmitTwoReturnValues(1, B_ACTION_USE_ITEM, 0); *(gBattleStruct->chosenItem + (gActiveBattler / 2) * 2) = item; gBattleResources->battleHistory->trainerItems[i] = 0; return shouldUse; @@ -943,3 +975,41 @@ static bool8 ShouldUseItem(void) return FALSE; } + +static bool32 AI_ShouldHeal(u8 healAmount) +{ + bool32 shouldHeal = FALSE; + u32 i; + + if (gBattleMons[gActiveBattler].hp < gBattleMons[gActiveBattler].maxHP / 4 + || gBattleMons[gActiveBattler].hp == 0 + || (healAmount != 0 && gBattleMons[gActiveBattler].maxHP - gBattleMons[gActiveBattler].hp > healAmount)) + { + // We have low enough HP to consider healing + shouldHeal = TRUE; + + // Check special cases to NOT heal + for (i = 0; i < gBattlersCount; i++) + { + if (GetBattlerSide(i) == B_SIDE_PLAYER) + { + if (CanTargetFaintAiWithMod(i, gActiveBattler, healAmount, 0)) + { + // Target is expected to faint us even after we heal. So why bother. + shouldHeal = FALSE; + break; + } + + // AI_THINKING_STRUCT->movesetIndex is the array index of the AI's chosen move + if (CanAttackerFaintTarget(gActiveBattler, i, AI_THINKING_STRUCT->movesetIndex, 0) && AI_WhoStrikesFirst(gActiveBattler, i) == AI_IS_FASTER) + { + // We can faint the target and move first -> don't heal + shouldHeal = FALSE; + break; + } + } + } + } + + return shouldHeal; +} diff --git a/src/battle_ai_util.c b/src/battle_ai_util.c index 95d979e94..248a2adff 100644 --- a/src/battle_ai_util.c +++ b/src/battle_ai_util.c @@ -439,6 +439,11 @@ static const u16 sOtherMoveCallingMoves[] = }; // Functions +bool32 WillAIStrikeFirst(void) +{ + return (AI_WhoStrikesFirst(sBattler_AI, gBattlerTarget) == AI_IS_FASTER); +} + bool32 AI_RandLessThan(u8 val) { if ((Random() % 0xFF) < val) @@ -632,7 +637,7 @@ bool32 IsTruantMonVulnerable(u32 battlerAI, u32 opposingBattler) u32 move = gBattleResources->battleHistory->usedMoves[opposingBattler][i]; if (gBattleMoves[move].effect == EFFECT_PROTECT && move != MOVE_ENDURE) return TRUE; - if (gBattleMoves[move].effect == EFFECT_SEMI_INVULNERABLE && GetWhoStrikesFirst(battlerAI, opposingBattler, TRUE) == 1) + if (gBattleMoves[move].effect == EFFECT_SEMI_INVULNERABLE && AI_WhoStrikesFirst(battlerAI, opposingBattler) == AI_IS_SLOWER) return TRUE; } return FALSE; @@ -964,48 +969,43 @@ u8 AI_GetMoveEffectiveness(u16 move, u8 battlerAtk, u8 battlerDef) return damageVar; } -// AI_CHECK_FASTER: is user(ai) faster -// AI_CHECK_SLOWER: is target faster -bool32 IsAiFaster(u8 battler) +/* Checks to see if AI will move ahead of another battler + * Output: + * AI_IS_FASTER: is user(ai) faster + * AI_IS_SLOWER: is target faster +*/ +u8 AI_WhoStrikesFirst(u8 battlerAI, u8 battler2) { u32 fasterAI = 0, fasterPlayer = 0, i; - s8 prioAI, prioPlayer; + s8 prioAI = 0; + s8 prioPlayer = 0; // Check move priorities first. - prioAI = GetMovePriority(sBattler_AI, AI_THINKING_STRUCT->moveConsidered); - SaveBattlerData(gBattlerTarget); - SetBattlerData(gBattlerTarget); + prioAI = GetMovePriority(battlerAI, AI_THINKING_STRUCT->moveConsidered); for (i = 0; i < MAX_MON_MOVES; i++) { - if (gBattleMons[gBattlerTarget].moves[i] == 0 || gBattleMons[gBattlerTarget].moves[i] == 0xFFFF) + if (gBattleMons[battler2].moves[i] == 0 || gBattleMons[battler2].moves[i] == 0xFFFF) continue; - prioPlayer = GetMovePriority(gBattlerTarget, gBattleMons[gBattlerTarget].moves[i]); + prioPlayer = GetMovePriority(battler2, gBattleMons[battler2].moves[i]); if (prioAI > prioPlayer) fasterAI++; else if (prioPlayer > prioAI) fasterPlayer++; } - RestoreBattlerData(gBattlerTarget); if (fasterAI > fasterPlayer) { - if (battler == 0) // is user (ai) faster - return TRUE; - else - return FALSE; + return AI_IS_FASTER; } else if (fasterAI < fasterPlayer) { - if (battler == 1) // is target (player) faster - return TRUE; - else - return FALSE; + return AI_IS_SLOWER; } else { // Priorities are the same(at least comparing to moves the AI is aware of), decide by speed. - if (GetWhoStrikesFirst(sBattler_AI, gBattlerTarget, TRUE) == battler) + if (GetWhoStrikesFirst(battlerAI, battler2, TRUE) == 0) return TRUE; else return FALSE; @@ -1045,14 +1045,17 @@ bool32 CanMoveFaintBattler(u16 move, u8 battlerDef, u8 battlerAtk, u8 nHits) // Check if target has means to faint ai mon after modding hp/dmg bool32 CanTargetFaintAiWithMod(u8 battlerDef, u8 battlerAtk, s32 hpMod, s32 dmgMod) { - u32 i; + u32 i, dmg; u32 unusable = CheckMoveLimitations(battlerDef, 0, 0xFF & ~MOVE_LIMITATION_PP); u16 *moves = gBattleResources->battleHistory->usedMoves[battlerDef]; + u32 hpCheck = gBattleMons[battlerAtk].hp + hpMod; + + if (hpCheck > gBattleMons[battlerAtk].maxHP) + hpCheck = gBattleMons[battlerAtk].maxHP; for (i = 0; i < MAX_MON_MOVES; i++) { - u32 dmg = AI_CalcDamage(moves[i], battlerDef, battlerAtk); - u32 hpCheck = gBattleMons[battlerAtk].hp + hpMod; + dmg = AI_THINKING_STRUCT->simulatedDmg[battlerAtk][battlerDef][i]; if (dmgMod) dmg *= dmgMod; @@ -1650,7 +1653,7 @@ u32 CountNegativeStatStages(u8 battlerId) bool32 ShouldLowerAttack(u8 battlerAtk, u8 battlerDef, u16 defAbility) { - if (IsAiFaster(AI_CHECK_FASTER) && CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) + if (WillAIStrikeFirst() && CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) return FALSE; // Don't bother lowering stats if can kill enemy. if (gBattleMons[battlerDef].statStages[STAT_ATK] > 4 @@ -1666,7 +1669,7 @@ bool32 ShouldLowerAttack(u8 battlerAtk, u8 battlerDef, u16 defAbility) bool32 ShouldLowerDefense(u8 battlerAtk, u8 battlerDef, u16 defAbility) { - if (IsAiFaster(AI_CHECK_FASTER) && CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) + if (WillAIStrikeFirst() && CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) return FALSE; // Don't bother lowering stats if can kill enemy. if (gBattleMons[battlerDef].statStages[STAT_DEF] > 4 @@ -1682,10 +1685,10 @@ bool32 ShouldLowerDefense(u8 battlerAtk, u8 battlerDef, u16 defAbility) bool32 ShouldLowerSpeed(u8 battlerAtk, u8 battlerDef, u16 defAbility) { - if (IsAiFaster(AI_CHECK_FASTER) && CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) + if (WillAIStrikeFirst() && CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) return FALSE; // Don't bother lowering stats if can kill enemy. - if (IsAiFaster(AI_CHECK_SLOWER) + if (!WillAIStrikeFirst() && defAbility != ABILITY_CONTRARY && defAbility != ABILITY_CLEAR_BODY //&& defAbility != ABILITY_FULL_METAL_BODY @@ -1696,7 +1699,7 @@ bool32 ShouldLowerSpeed(u8 battlerAtk, u8 battlerDef, u16 defAbility) bool32 ShouldLowerSpAtk(u8 battlerAtk, u8 battlerDef, u16 defAbility) { - if (IsAiFaster(AI_CHECK_FASTER) && CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) + if (WillAIStrikeFirst() && CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) return FALSE; // Don't bother lowering stats if can kill enemy. if (gBattleMons[battlerDef].statStages[STAT_SPATK] > 4 @@ -1711,7 +1714,7 @@ bool32 ShouldLowerSpAtk(u8 battlerAtk, u8 battlerDef, u16 defAbility) bool32 ShouldLowerSpDef(u8 battlerAtk, u8 battlerDef, u16 defAbility) { - if (IsAiFaster(AI_CHECK_FASTER) && CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) + if (WillAIStrikeFirst() && CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) return FALSE; // Don't bother lowering stats if can kill enemy. if (gBattleMons[battlerDef].statStages[STAT_SPDEF] > 4 @@ -1726,7 +1729,7 @@ bool32 ShouldLowerSpDef(u8 battlerAtk, u8 battlerDef, u16 defAbility) bool32 ShouldLowerAccuracy(u8 battlerAtk, u8 battlerDef, u16 defAbility) { - if (IsAiFaster(AI_CHECK_FASTER) && CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) + if (WillAIStrikeFirst() && CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) return FALSE; // Don't bother lowering stats if can kill enemy. if (defAbility != ABILITY_CONTRARY @@ -1740,7 +1743,7 @@ bool32 ShouldLowerAccuracy(u8 battlerAtk, u8 battlerDef, u16 defAbility) bool32 ShouldLowerEvasion(u8 battlerAtk, u8 battlerDef, u16 defAbility) { - if (IsAiFaster(AI_CHECK_FASTER) && CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) + if (WillAIStrikeFirst() && CanAttackerFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) return FALSE; // Don't bother lowering stats if can kill enemy. if (gBattleMons[battlerDef].statStages[STAT_EVASION] > DEFAULT_STAT_STAGE @@ -2404,7 +2407,7 @@ bool32 ShouldPivot(u8 battlerAtk, u8 battlerDef, u16 defAbility, u16 move, u8 mo /*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 (AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_FASTER) // Attacker goes first { if (!CanAttackerFaintTarget(battlerAtk, battlerDef, moveIndex, 0)) // Can't KO foe otherwise { @@ -2753,7 +2756,7 @@ u32 ShouldTryToFlinch(u8 battlerAtk, u8 battlerDef, u16 atkAbility, u16 defAbili { if (defAbility == ABILITY_INNER_FOCUS || DoesSubstituteBlockMove(battlerAtk, battlerDef, move) - || GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 1) // opponent goes first + || AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_SLOWER) // Opponent goes first { return 0; // don't try to flinch } @@ -2877,7 +2880,7 @@ bool32 ShouldUseRecoilMove(u8 battlerAtk, u8 battlerDef, u32 recoilDmg, u8 moveI bool32 ShouldAbsorb(u8 battlerAtk, u8 battlerDef, u16 move, s32 damage) { - if (move == 0xFFFF || GetWhoStrikesFirst(battlerAtk, gBattlerTarget, TRUE) == 0) + if (move == 0xFFFF || AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_FASTER) { // using item or user goes first u8 healPercent = (gBattleMoves[move].argument == 0) ? 50 : gBattleMoves[move].argument; @@ -2904,7 +2907,7 @@ bool32 ShouldAbsorb(u8 battlerAtk, u8 battlerDef, u16 move, s32 damage) bool32 ShouldRecover(u8 battlerAtk, u8 battlerDef, u16 move, u8 healPercent) { - if (move == 0xFFFF || GetWhoStrikesFirst(battlerAtk, gBattlerTarget, TRUE) == 0) + if (move == 0xFFFF || AI_WhoStrikesFirst(battlerAtk, battlerDef) == AI_IS_FASTER) { // using item or user going first s32 damage = AI_THINKING_STRUCT->simulatedDmg[battlerAtk][battlerDef][AI_THINKING_STRUCT->movesetIndex]; @@ -3350,7 +3353,7 @@ void IncreaseStatUpScore(u8 battlerAtk, u8 battlerDef, u8 statId, s16 *score) } break; case STAT_SPEED: - if (IsAiFaster(AI_CHECK_SLOWER)) + if (!WillAIStrikeFirst()) { if (gBattleMons[battlerAtk].statStages[STAT_SPEED] < STAT_UP_2_STAGE) *score += 2; diff --git a/src/battle_controller_opponent.c b/src/battle_controller_opponent.c index b43e80225..c965c4854 100644 --- a/src/battle_controller_opponent.c +++ b/src/battle_controller_opponent.c @@ -1544,47 +1544,34 @@ static void OpponentHandlePrintSelectionString(void) } static void OpponentHandleChooseAction(void) -{ - AI_TrySwitchOrUseItem(); - OpponentBufferExecCompleted(); -} - -static void OpponentHandleYesNoBox(void) -{ - OpponentBufferExecCompleted(); -} - -static void OpponentHandleChooseMove(void) { if (gBattleTypeFlags & BATTLE_TYPE_PALACE) { - BtlController_EmitTwoReturnValues(1, 10, ChooseMoveAndTargetInBattlePalace()); - OpponentBufferExecCompleted(); + BtlController_EmitTwoReturnValues(1, B_ACTION_USE_MOVE, ChooseMoveAndTargetInBattlePalace()); } else { u8 chosenMoveId; struct ChooseMoveStruct *moveInfo = (struct ChooseMoveStruct*)(&gBattleResources->bufferA[gActiveBattler][4]); - + if (gBattleTypeFlags & (BATTLE_TYPE_TRAINER | BATTLE_TYPE_FIRST_BATTLE | BATTLE_TYPE_SAFARI | BATTLE_TYPE_ROAMER)) { BattleAI_SetupAIData(0xF); chosenMoveId = BattleAI_ChooseMoveOrAction(); - switch (chosenMoveId) { + case AI_CHOICE_USE_ITEM: + BtlController_EmitTwoReturnValues(1, B_ACTION_USE_ITEM, 0); + break; + case AI_CHOICE_SWITCH: + BtlController_EmitTwoReturnValues(1, B_ACTION_SWITCH, 0); + break; case AI_CHOICE_WATCH: BtlController_EmitTwoReturnValues(1, B_ACTION_SAFARI_WATCH_CAREFULLY, 0); break; case AI_CHOICE_FLEE: BtlController_EmitTwoReturnValues(1, B_ACTION_RUN, 0); break; - case AI_CHOICE_SWITCH: - BtlController_EmitTwoReturnValues(1, 10, 0xFFFF); - break; - case 6: - BtlController_EmitTwoReturnValues(1, 15, gBattlerTarget); - break; default: if (gBattleMoves[moveInfo->moves[chosenMoveId]].target & (MOVE_TARGET_USER_OR_SELECTED | MOVE_TARGET_USER)) gBattlerTarget = gActiveBattler; @@ -1594,15 +1581,16 @@ static void OpponentHandleChooseMove(void) if (gAbsentBattlerFlags & gBitTable[gBattlerTarget]) gBattlerTarget = GetBattlerAtPosition(B_POSITION_PLAYER_RIGHT); } - if (CanMegaEvolve(gActiveBattler)) // If opponent can mega evolve, do it. - BtlController_EmitTwoReturnValues(1, 10, (chosenMoveId) | (RET_MEGA_EVOLUTION) | (gBattlerTarget << 8)); - else - BtlController_EmitTwoReturnValues(1, 10, (chosenMoveId) | (gBattlerTarget << 8)); + + if (CanMegaEvolve(gActiveBattler)) { // If opponent can mega evolve, do it. + BtlController_EmitTwoReturnValues(1, B_ACTION_USE_MOVE, (chosenMoveId) | (RET_MEGA_EVOLUTION) | (gBattlerTarget << 8)); + } else { + BtlController_EmitTwoReturnValues(1, B_ACTION_USE_MOVE, (chosenMoveId) | (gBattlerTarget << 8)); + } break; } - OpponentBufferExecCompleted(); } - else + else // Wild pokemon - use random move { u16 move; do @@ -1612,15 +1600,27 @@ static void OpponentHandleChooseMove(void) } while (move == MOVE_NONE); if (gBattleMoves[move].target & (MOVE_TARGET_USER_OR_SELECTED | MOVE_TARGET_USER)) - BtlController_EmitTwoReturnValues(1, 10, (chosenMoveId) | (gActiveBattler << 8)); + BtlController_EmitTwoReturnValues(1, B_ACTION_USE_MOVE, (chosenMoveId) | (gActiveBattler << 8)); else if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE) - BtlController_EmitTwoReturnValues(1, 10, (chosenMoveId) | (GetBattlerAtPosition(Random() & 2) << 8)); + BtlController_EmitTwoReturnValues(1, B_ACTION_USE_MOVE, (chosenMoveId) | (GetBattlerAtPosition(Random() & 2) << 8)); else - BtlController_EmitTwoReturnValues(1, 10, (chosenMoveId) | (GetBattlerAtPosition(B_POSITION_PLAYER_LEFT) << 8)); - - OpponentBufferExecCompleted(); + BtlController_EmitTwoReturnValues(1, B_ACTION_USE_MOVE, (chosenMoveId) | (GetBattlerAtPosition(B_POSITION_PLAYER_LEFT) << 8)); } } + + OpponentBufferExecCompleted(); +} + +static void OpponentHandleYesNoBox(void) +{ + OpponentBufferExecCompleted(); +} + +static void OpponentHandleChooseMove(void) +{ + u8 *bufferB = gBattleResources->bufferB[gActiveBattler]; + BtlController_EmitTwoReturnValues(1, 10, bufferB[2] | (bufferB[3] << 8)); + OpponentBufferExecCompleted(); } static void OpponentHandleChooseItem(void) diff --git a/src/battle_controller_player_partner.c b/src/battle_controller_player_partner.c index f0540e65f..b4052402e 100644 --- a/src/battle_controller_player_partner.c +++ b/src/battle_controller_player_partner.c @@ -1509,7 +1509,37 @@ static void PlayerPartnerHandlePrintSelectionString(void) static void PlayerPartnerHandleChooseAction(void) { - AI_TrySwitchOrUseItem(); + u8 chosenMoveId; + struct ChooseMoveStruct *moveInfo = (struct ChooseMoveStruct*)(&gBattleResources->bufferA[gActiveBattler][4]); + + BattleAI_SetupAIData(0xF); + chosenMoveId = BattleAI_ChooseMoveOrAction(); + + switch (chosenMoveId) + { + case AI_CHOICE_USE_ITEM: + BtlController_EmitTwoReturnValues(1, B_ACTION_USE_ITEM, 0); + break; + case AI_CHOICE_SWITCH: + BtlController_EmitTwoReturnValues(1, B_ACTION_SWITCH, 0); + break; + default: + if (gBattleMoves[moveInfo->moves[chosenMoveId]].target & (MOVE_TARGET_USER | MOVE_TARGET_USER_OR_SELECTED)) + gBattlerTarget = gActiveBattler; + if (gBattleMoves[moveInfo->moves[chosenMoveId]].target & MOVE_TARGET_BOTH) + { + gBattlerTarget = GetBattlerAtPosition(B_POSITION_OPPONENT_LEFT); + if (gAbsentBattlerFlags & gBitTable[gBattlerTarget]) + gBattlerTarget = GetBattlerAtPosition(B_POSITION_OPPONENT_RIGHT); + } + + if (CanMegaEvolve(gActiveBattler)) // If partner can mega evolve, do it. + BtlController_EmitTwoReturnValues(1, B_ACTION_USE_MOVE, (chosenMoveId) | (RET_MEGA_EVOLUTION) | (gBattlerTarget << 8)); + else + BtlController_EmitTwoReturnValues(1, B_ACTION_USE_MOVE, (chosenMoveId) | (gBattlerTarget << 8)); + break; + } + PlayerPartnerBufferExecCompleted(); } @@ -1520,25 +1550,6 @@ static void PlayerPartnerHandleYesNoBox(void) static void PlayerPartnerHandleChooseMove(void) { - u8 chosenMoveId; - struct ChooseMoveStruct *moveInfo = (struct ChooseMoveStruct*)(&gBattleResources->bufferA[gActiveBattler][4]); - - BattleAI_SetupAIData(0xF); - chosenMoveId = BattleAI_ChooseMoveOrAction(); - - if (gBattleMoves[moveInfo->moves[chosenMoveId]].target & (MOVE_TARGET_USER | MOVE_TARGET_USER_OR_SELECTED)) - gBattlerTarget = gActiveBattler; - if (gBattleMoves[moveInfo->moves[chosenMoveId]].target & MOVE_TARGET_BOTH) - { - gBattlerTarget = GetBattlerAtPosition(B_POSITION_OPPONENT_LEFT); - if (gAbsentBattlerFlags & gBitTable[gBattlerTarget]) - gBattlerTarget = GetBattlerAtPosition(B_POSITION_OPPONENT_RIGHT); - } - - if (CanMegaEvolve(gActiveBattler)) // If partner can mega evolve, do it. - BtlController_EmitTwoReturnValues(1, 10, (chosenMoveId) | (RET_MEGA_EVOLUTION) | (gBattlerTarget << 8)); - else - BtlController_EmitTwoReturnValues(1, 10, (chosenMoveId) | (gBattlerTarget << 8)); PlayerPartnerBufferExecCompleted(); } diff --git a/src/battle_main.c b/src/battle_main.c index 5c3f2ecd8..e0f033e2a 100644 --- a/src/battle_main.c +++ b/src/battle_main.c @@ -3828,22 +3828,7 @@ static void HandleTurnActionSelectionState(void) { struct ChooseMoveStruct moveInfo; - moveInfo.mega = gBattleStruct->mega; - moveInfo.species = gBattleMons[gActiveBattler].species; - moveInfo.monType1 = gBattleMons[gActiveBattler].type1; - moveInfo.monType2 = gBattleMons[gActiveBattler].type2; - moveInfo.monType3 = gBattleMons[gActiveBattler].type3; - - for (i = 0; i < MAX_MON_MOVES; i++) - { - moveInfo.moves[i] = gBattleMons[gActiveBattler].moves[i]; - moveInfo.currentPp[i] = gBattleMons[gActiveBattler].pp[i]; - moveInfo.maxPp[i] = CalculatePPWithBonus( - gBattleMons[gActiveBattler].moves[i], - gBattleMons[gActiveBattler].ppBonuses, - i); - } - + FillChooseMoveStruct(&moveInfo); BtlController_EmitChooseMove(0, (gBattleTypeFlags & BATTLE_TYPE_DOUBLE) != 0, FALSE, &moveInfo); MarkBattlerForControllerExec(gActiveBattler); } @@ -3945,6 +3930,9 @@ static void HandleTurnActionSelectionState(void) return; case B_ACTION_DEBUG: BtlController_EmitDebugMenu(0); + // fallthrough + case B_ACTION_SAFARI_WATCH_CAREFULLY: + case B_ACTION_RUN: MarkBattlerForControllerExec(gActiveBattler); break; } @@ -5158,3 +5146,24 @@ void SetTotemBoost(void) } } } + +void FillChooseMoveStruct(struct ChooseMoveStruct * moveInfo) +{ + int i; + + moveInfo->mega = gBattleStruct->mega; + moveInfo->species = gBattleMons[gActiveBattler].species; + moveInfo->monType1 = gBattleMons[gActiveBattler].type1; + moveInfo->monType2 = gBattleMons[gActiveBattler].type2; + moveInfo->monType3 = gBattleMons[gActiveBattler].type3; + + for (i = 0; i < MAX_MON_MOVES; i++) + { + moveInfo->moves[i] = gBattleMons[gActiveBattler].moves[i]; + moveInfo->currentPp[i] = gBattleMons[gActiveBattler].pp[i]; + moveInfo->maxPp[i] = CalculatePPWithBonus( + gBattleMons[gActiveBattler].moves[i], + gBattleMons[gActiveBattler].ppBonuses, + i); + } +} From c9743d1494b8a9cca4e1e83d6d5ca2fa0da10788 Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Wed, 6 Oct 2021 10:05:49 -0400 Subject: [PATCH 02/46] fix wild battles freezing --- src/battle_controller_opponent.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/battle_controller_opponent.c b/src/battle_controller_opponent.c index c965c4854..5975ab2a1 100644 --- a/src/battle_controller_opponent.c +++ b/src/battle_controller_opponent.c @@ -1552,8 +1552,9 @@ static void OpponentHandleChooseAction(void) else { u8 chosenMoveId; - struct ChooseMoveStruct *moveInfo = (struct ChooseMoveStruct*)(&gBattleResources->bufferA[gActiveBattler][4]); - + struct ChooseMoveStruct moveInfo; + + FillChooseMoveStruct(&moveInfo); if (gBattleTypeFlags & (BATTLE_TYPE_TRAINER | BATTLE_TYPE_FIRST_BATTLE | BATTLE_TYPE_SAFARI | BATTLE_TYPE_ROAMER)) { BattleAI_SetupAIData(0xF); @@ -1573,9 +1574,9 @@ static void OpponentHandleChooseAction(void) BtlController_EmitTwoReturnValues(1, B_ACTION_RUN, 0); break; default: - if (gBattleMoves[moveInfo->moves[chosenMoveId]].target & (MOVE_TARGET_USER_OR_SELECTED | MOVE_TARGET_USER)) + if (gBattleMoves[moveInfo.moves[chosenMoveId]].target & (MOVE_TARGET_USER_OR_SELECTED | MOVE_TARGET_USER)) gBattlerTarget = gActiveBattler; - if (gBattleMoves[moveInfo->moves[chosenMoveId]].target & MOVE_TARGET_BOTH) + if (gBattleMoves[moveInfo.moves[chosenMoveId]].target & MOVE_TARGET_BOTH) { gBattlerTarget = GetBattlerAtPosition(B_POSITION_PLAYER_LEFT); if (gAbsentBattlerFlags & gBitTable[gBattlerTarget]) @@ -1596,7 +1597,7 @@ static void OpponentHandleChooseAction(void) do { chosenMoveId = Random() & 3; - move = moveInfo->moves[chosenMoveId]; + move = moveInfo.moves[chosenMoveId]; } while (move == MOVE_NONE); if (gBattleMoves[move].target & (MOVE_TARGET_USER_OR_SELECTED | MOVE_TARGET_USER)) From 28bc7dc14d46744afafe35a666f2cd3397c0c3ff Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Fri, 29 Oct 2021 22:32:19 -0400 Subject: [PATCH 03/46] start ability --- data/battle_scripts_1.s | 17 ++++++++ include/battle.h | 14 ++++--- include/battle_scripts.h | 2 + include/battle_util.h | 32 +++++++------- include/constants/battle_string_ids.h | 5 ++- src/battle_main.c | 6 ++- src/battle_message.c | 6 ++- src/battle_script_commands.c | 60 +++++++++++++++++---------- src/battle_util.c | 60 +++++++++++++++++++++++++-- 9 files changed, 154 insertions(+), 48 deletions(-) diff --git a/data/battle_scripts_1.s b/data/battle_scripts_1.s index 009c3a882..e104db758 100644 --- a/data/battle_scripts_1.s +++ b/data/battle_scripts_1.s @@ -8091,6 +8091,12 @@ BattleScript_SwitchInAbilityMsg:: waitmessage B_WAIT_TIME_LONG end3 +BattleScript_SwitchInAbilityMsgRet:: + call BattleScript_AbilityPopUp + printfromtable gSwitchInAbilityStringIds + waitmessage B_WAIT_TIME_LONG + return + BattleScript_ActivateAsOne:: call BattleScript_AbilityPopUp printfromtable gSwitchInAbilityStringIds @@ -8909,3 +8915,14 @@ BattleScript_DarkTypePreventsPrankster:: waitmessage B_WAIT_TIME_LONG orhalfword gMoveResultFlags, MOVE_RESULT_NO_EFFECT goto BattleScript_MoveEnd + +BattleScript_NeutralizingGasExits:: + pause B_WAIT_TIME_SHORT + printstring STRINGID_NEUTRALIZINGGASOVER + waitmessage B_WAIT_TIME_LONG + setbyte gBattlerTarget, 0 +BattleScript_NeutralizingGasExitsLoop: + switchinabilities BS_TARGET + addbyte gBattlerTarget, 1 + jumpifbytenotequal gBattlerTarget, gBattlersCount, BattleScript_NeutralizingGasExitsLoop + return diff --git a/include/battle.h b/include/battle.h index 5077053e3..68a40c978 100644 --- a/include/battle.h +++ b/include/battle.h @@ -62,12 +62,13 @@ struct ResourceFlags u32 flags[4]; }; -#define RESOURCE_FLAG_FLASH_FIRE 0x1 -#define RESOURCE_FLAG_ROOST 0x2 -#define RESOURCE_FLAG_UNBURDEN 0x4 -#define RESOURCE_FLAG_INTIMIDATED 0x8 -#define RESOURCE_FLAG_TRACED 0x10 -#define RESOURCE_FLAG_EMERGENCY_EXIT 0x20 +#define RESOURCE_FLAG_FLASH_FIRE 0x1 +#define RESOURCE_FLAG_ROOST 0x2 +#define RESOURCE_FLAG_UNBURDEN 0x4 +#define RESOURCE_FLAG_INTIMIDATED 0x8 +#define RESOURCE_FLAG_TRACED 0x10 +#define RESOURCE_FLAG_EMERGENCY_EXIT 0x20 +#define RESOURCE_FLAG_NEUTRALIZING_GAS 0x40 struct DisableStruct { @@ -181,6 +182,7 @@ struct SpecialStatus u8 damagedMons:4; // Mons that have been damaged directly by using a move, includes substitute. u8 dancerUsedMove:1; u8 dancerOriginalTarget:3; + u8 announceNeutralizingGas:1; s32 dmg; s32 physicalDmg; s32 specialDmg; diff --git a/include/battle_scripts.h b/include/battle_scripts.h index ec95a4549..1c4880011 100644 --- a/include/battle_scripts.h +++ b/include/battle_scripts.h @@ -261,6 +261,7 @@ extern const u8 BattleScript_AttackerAbilityStatRaiseEnd3[]; extern const u8 BattleScript_PoisonHealActivates[]; extern const u8 BattleScript_BadDreamsActivates[]; extern const u8 BattleScript_SwitchInAbilityMsg[]; +extern const u8 BattleScript_SwitchInAbilityMsgRet[]; extern const u8 BattleScript_ToxicSpikesPoisoned[]; extern const u8 BattleScript_ToxicSpikesAbsorbed[]; extern const u8 BattleScript_StickyWebOnSwitchIn[]; @@ -402,5 +403,6 @@ extern const u8 BattleScript_BlockedByPrimalWeatherEnd3[]; extern const u8 BattleScript_BlockedByPrimalWeatherRet[]; extern const u8 BattleScript_PrimalReversion[]; extern const u8 BattleScript_HyperspaceFuryRemoveProtect[]; +extern const u8 BattleScript_NeutralizingGasExits[]; #endif // GUARD_BATTLE_SCRIPTS_H diff --git a/include/battle_util.h b/include/battle_util.h index ee7337ef2..32c7a53dc 100644 --- a/include/battle_util.h +++ b/include/battle_util.h @@ -8,21 +8,23 @@ #define MOVE_LIMITATION_TAUNT (1 << 4) #define MOVE_LIMITATION_IMPRISON (1 << 5) -#define ABILITYEFFECT_ON_SWITCHIN 0x0 -#define ABILITYEFFECT_ENDTURN 0x1 -#define ABILITYEFFECT_MOVES_BLOCK 0x2 -#define ABILITYEFFECT_ABSORBING 0x3 -#define ABILITYEFFECT_MOVE_END_ATTACKER 0x4 -#define ABILITYEFFECT_MOVE_END 0x5 -#define ABILITYEFFECT_IMMUNITY 0x6 -#define ABILITYEFFECT_FORECAST 0x7 -#define ABILITYEFFECT_SYNCHRONIZE 0x8 -#define ABILITYEFFECT_ATK_SYNCHRONIZE 0x9 -#define ABILITYEFFECT_INTIMIDATE1 0xA -#define ABILITYEFFECT_INTIMIDATE2 0xB -#define ABILITYEFFECT_TRACE1 0xC -#define ABILITYEFFECT_TRACE2 0xD -#define ABILITYEFFECT_MOVE_END_OTHER 0xE +#define ABILITYEFFECT_ON_SWITCHIN 0 +#define ABILITYEFFECT_ENDTURN 1 +#define ABILITYEFFECT_MOVES_BLOCK 2 +#define ABILITYEFFECT_ABSORBING 3 +#define ABILITYEFFECT_MOVE_END_ATTACKER 4 +#define ABILITYEFFECT_MOVE_END 5 +#define ABILITYEFFECT_IMMUNITY 6 +#define ABILITYEFFECT_FORECAST 7 +#define ABILITYEFFECT_SYNCHRONIZE 8 +#define ABILITYEFFECT_ATK_SYNCHRONIZE 9 +#define ABILITYEFFECT_INTIMIDATE1 10 +#define ABILITYEFFECT_INTIMIDATE2 11 +#define ABILITYEFFECT_TRACE1 12 +#define ABILITYEFFECT_TRACE2 13 +#define ABILITYEFFECT_MOVE_END_OTHER 14 +#define ABILITYEFFECT_NEUTRALIZINGGAS 15 +// Special cases #define ABILITYEFFECT_SWITCH_IN_TERRAIN 0xFE #define ABILITYEFFECT_SWITCH_IN_WEATHER 0xFF diff --git a/include/constants/battle_string_ids.h b/include/constants/battle_string_ids.h index 1d461e2a0..716729619 100644 --- a/include/constants/battle_string_ids.h +++ b/include/constants/battle_string_ids.h @@ -597,8 +597,10 @@ #define STRINGID_BUTPOKEMONCANTUSETHEMOVE 594 #define STRINGID_BUTHOOPACANTUSEIT 595 #define STRINGID_BROKETHROUGHPROTECTION 596 +#define STRINGID_NEUTRALIZINGGASENTERS 597 +#define STRINGID_NEUTRALIZINGGASOVER 598 -#define BATTLESTRINGS_COUNT 597 +#define BATTLESTRINGS_COUNT 599 // The below IDs are all indexes into battle message tables, // used to determine which of a set of messages to print. @@ -833,6 +835,7 @@ #define B_MSG_SWITCHIN_SCREENCLEANER 12 #define B_MSG_SWITCHIN_ASONE 13 #define B_MSG_SWITCHIN_CURIOUS_MEDICINE 14 +#define B_MSG_SWITCHIN_NEUTRALIZING_GAS 15 // gMentalHerbCureStringIds #define B_MSG_MENTALHERBCURE_INFATUATION 0 diff --git a/src/battle_main.c b/src/battle_main.c index 271e97731..19e0d4940 100644 --- a/src/battle_main.c +++ b/src/battle_main.c @@ -3533,7 +3533,11 @@ static void TryDoEventsBeforeFirstTurn(void) return; } } - + + // Check neutralizing gas + if (AbilityBattleEffects(ABILITYEFFECT_NEUTRALIZINGGAS, 0, 0, 0, 0) != 0) + return; + // Check all switch in abilities happening from the fastest mon to slowest. while (gBattleStruct->switchInAbilitiesCounter < gBattlersCount) { diff --git a/src/battle_message.c b/src/battle_message.c index f26726a69..48e7ab1a4 100644 --- a/src/battle_message.c +++ b/src/battle_message.c @@ -723,9 +723,12 @@ static const u8 sText_PkmnRevertedToPrimal[] = _("{B_ATK_NAME_WITH_PREFIX}'s Pri static const u8 sText_ButPokemonCantUseTheMove[] = _("But {B_ATK_NAME_WITH_PREFIX} can't\nuse the move!"); static const u8 sText_ButHoopaCantUseIt[] = _("But Hoopa can't use it\nthe way it is now!"); static const u8 sText_BrokeThroughProtection[] = _("It broke through the\n{B_DEF_NAME_WITH_PREFIX}'s protection!"); - +static const u8 sText_NeutralizingGasEnters[] = _("Neutralizing Gas filled the area!"); +static const u8 sText_NeutralizingGasOver[] = _("The effects of Neutralizing\nGas wore off!"); const u8 *const gBattleStringsTable[BATTLESTRINGS_COUNT] = { + [STRINGID_NEUTRALIZINGGASOVER - 12] = sText_NeutralizingGasOver, + [STRINGID_NEUTRALIZINGGASENTERS - 12] = sText_NeutralizingGasEnters, [STRINGID_BROKETHROUGHPROTECTION - 12] = sText_BrokeThroughProtection, [STRINGID_BUTPOKEMONCANTUSETHEMOVE - 12] = sText_ButPokemonCantUseTheMove, [STRINGID_BUTHOOPACANTUSEIT - 12] = sText_ButHoopaCantUseIt, @@ -1371,6 +1374,7 @@ const u16 gSwitchInAbilityStringIds[] = [B_MSG_SWITCHIN_SCREENCLEANER] = STRINGID_SCREENCLEANERENTERS, [B_MSG_SWITCHIN_ASONE] = STRINGID_ASONEENTERS, [B_MSG_SWITCHIN_CURIOUS_MEDICINE] = STRINGID_CURIOUSMEDICINEENTERS, + [B_MSG_SWITCHIN_NEUTRALIZING_GAS] = STRINGID_NEUTRALIZINGGASENTERS, }; const u16 gMissStringIds[] = diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index e41f0982a..e2c6aff02 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -5416,7 +5416,7 @@ static void Cmd_sethealblock(void) static void Cmd_returnatktoball(void) { - gActiveBattler = gBattlerAttacker; + gActiveBattler = gBattlerAttacker; if (!(gHitMarker & HITMARKER_FAINTED(gActiveBattler))) { BtlController_EmitReturnMonToBall(0, 0); @@ -6077,8 +6077,17 @@ static void Cmd_switchineffects(void) gHitMarker &= ~(HITMARKER_FAINTED(gActiveBattler)); gSpecialStatuses[gActiveBattler].flag40 = 0; - - if (!(gSideStatuses[GetBattlerSide(gActiveBattler)] & SIDE_STATUS_SPIKES_DAMAGED) + + // Neutralizing Gas announces itself before hazards + if (gBattleMons[gActiveBattler].ability == ABILITY_NEUTRALIZING_GAS && gSpecialStatuses[gActiveBattler].announceNeutralizingGas == 0) + { + gBattleCommunication[MULTISTRING_CHOOSER] = B_MSG_SWITCHIN_NEUTRALIZING_GAS; + gSpecialStatuses[gActiveBattler].announceNeutralizingGas = TRUE; + gBattlerAbility = gActiveBattler; + BattleScriptPushCursor(); + gBattlescriptCurrInstr = BattleScript_SwitchInAbilityMsgRet; + } + else if (!(gSideStatuses[GetBattlerSide(gActiveBattler)] & SIDE_STATUS_SPIKES_DAMAGED) && (gSideStatuses[GetBattlerSide(gActiveBattler)] & SIDE_STATUS_SPIKES) && GetBattlerAbility(gActiveBattler) != ABILITY_MAGIC_GUARD && IsBattlerAffectedByHazards(gActiveBattler, FALSE) @@ -6240,7 +6249,7 @@ static void Cmd_endlinkbattle(void) } static void Cmd_returntoball(void) -{ +{ gActiveBattler = GetBattlerForBattleScript(gBattlescriptCurrInstr[1]); BtlController_EmitReturnMonToBall(0, 1); MarkBattlerForControllerExec(gActiveBattler); @@ -12281,25 +12290,34 @@ static void Cmd_trygetintimidatetarget(void) static void Cmd_switchoutabilities(void) { gActiveBattler = GetBattlerForBattleScript(gBattlescriptCurrInstr[1]); - - switch (GetBattlerAbility(gActiveBattler)) + + if (gBattleMons[gActiveBattler].ability == ABILITY_NEUTRALIZING_GAS) { - case ABILITY_NATURAL_CURE: - gBattleMons[gActiveBattler].status1 = 0; - BtlController_EmitSetMonData(0, REQUEST_STATUS_BATTLE, gBitTable[*(gBattleStruct->field_58 + gActiveBattler)], 4, &gBattleMons[gActiveBattler].status1); - MarkBattlerForControllerExec(gActiveBattler); - break; - case ABILITY_REGENERATOR: - gBattleMoveDamage = gBattleMons[gActiveBattler].maxHP / 3; - gBattleMoveDamage += gBattleMons[gActiveBattler].hp; - if (gBattleMoveDamage > gBattleMons[gActiveBattler].maxHP) - gBattleMoveDamage = gBattleMons[gActiveBattler].maxHP; - BtlController_EmitSetMonData(0, REQUEST_HP_BATTLE, gBitTable[*(gBattleStruct->field_58 + gActiveBattler)], 2, &gBattleMoveDamage); - MarkBattlerForControllerExec(gActiveBattler); - break; + gBattleMons[gActiveBattler].ability = 0; + BattleScriptPushCursor(); + gBattlescriptCurrInstr = BattleScript_NeutralizingGasExits; + } + else + { + switch (GetBattlerAbility(gActiveBattler)) + { + case ABILITY_NATURAL_CURE: + gBattleMons[gActiveBattler].status1 = 0; + BtlController_EmitSetMonData(0, REQUEST_STATUS_BATTLE, gBitTable[*(gBattleStruct->field_58 + gActiveBattler)], 4, &gBattleMons[gActiveBattler].status1); + MarkBattlerForControllerExec(gActiveBattler); + break; + case ABILITY_REGENERATOR: + gBattleMoveDamage = gBattleMons[gActiveBattler].maxHP / 3; + gBattleMoveDamage += gBattleMons[gActiveBattler].hp; + if (gBattleMoveDamage > gBattleMons[gActiveBattler].maxHP) + gBattleMoveDamage = gBattleMons[gActiveBattler].maxHP; + BtlController_EmitSetMonData(0, REQUEST_HP_BATTLE, gBitTable[*(gBattleStruct->field_58 + gActiveBattler)], 2, &gBattleMoveDamage); + MarkBattlerForControllerExec(gActiveBattler); + break; + } + + gBattlescriptCurrInstr += 2; } - - gBattlescriptCurrInstr += 2; } static void Cmd_jumpifhasnohp(void) diff --git a/src/battle_util.c b/src/battle_util.c index 275139f4f..0125d362c 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -5436,6 +5436,20 @@ u8 AbilityBattleEffects(u8 caseID, u8 battler, u16 ability, u8 special, u16 move } } break; + case ABILITYEFFECT_NEUTRALIZINGGAS: + // for the start of battle only. The switch-in message plays in Cmd_switchineffects because it comes before the hazards + for (i = 0; i < gBattlersCount; i++) + { + if (gBattleMons[i].ability == ABILITY_NEUTRALIZING_GAS && !(gBattleResources->flags->flags[i] & RESOURCE_FLAG_NEUTRALIZING_GAS)) + { + gBattleResources->flags->flags[i] |= RESOURCE_FLAG_NEUTRALIZING_GAS; + gBattlerAbility = i; + gBattleCommunication[MULTISTRING_CHOOSER] = B_MSG_SWITCHIN_NEUTRALIZING_GAS; + BattleScriptPushCursorAndCallback(BattleScript_SwitchInAbilityMsg); + effect++; + } + } + break; } if (effect && gLastUsedAbility != 0xFF) @@ -5446,11 +5460,51 @@ u8 AbilityBattleEffects(u8 caseID, u8 battler, u16 ability, u8 special, u16 move return effect; } +static bool32 IsNeutralizingGasBannedAbility(u32 ability) +{ + switch (ability) + { + case ABILITY_MULTITYPE: + case ABILITY_ZEN_MODE: + case ABILITY_STANCE_CHANGE: + case ABILITY_POWER_CONSTRUCT: + case ABILITY_SCHOOLING: + case ABILITY_RKS_SYSTEM: + case ABILITY_SHIELDS_DOWN: + case ABILITY_COMATOSE: + case ABILITY_DISGUISE: + case ABILITY_GULP_MISSILE: + case ABILITY_ICE_FACE: + case ABILITY_AS_ONE_ICE_RIDER: + case ABILITY_AS_ONE_SHADOW_RIDER: + return TRUE; + default: + return FALSE; + } +} + +static bool32 IsNeutralizingGasOnField(void) +{ + u32 i; + + for (i = 0; i < gBattlersCount; i++) + { + if (IsBattlerAlive(i) && gBattleMons[i].ability == ABILITY_NEUTRALIZING_GAS) + return TRUE; + } + + return FALSE; +} + u32 GetBattlerAbility(u8 battlerId) { if (gStatuses3[battlerId] & STATUS3_GASTRO_ACID) return ABILITY_NONE; - else if ((((gBattleMons[gBattlerAttacker].ability == ABILITY_MOLD_BREAKER + + if (IsNeutralizingGasOnField() && !IsNeutralizingGasBannedAbility(gBattleMons[battlerId].ability)) + return ABILITY_NONE; + + if ((((gBattleMons[gBattlerAttacker].ability == ABILITY_MOLD_BREAKER || gBattleMons[gBattlerAttacker].ability == ABILITY_TERAVOLT || gBattleMons[gBattlerAttacker].ability == ABILITY_TURBOBLAZE) && !(gStatuses3[gBattlerAttacker] & STATUS3_GASTRO_ACID)) @@ -5460,8 +5514,8 @@ u32 GetBattlerAbility(u8 battlerId) && gActionsByTurnOrder[gBattlerByTurnOrder[gBattlerAttacker]] == B_ACTION_USE_MOVE && gCurrentTurnActionNumber < gBattlersCount) return ABILITY_NONE; - else - return gBattleMons[battlerId].ability; + + return gBattleMons[battlerId].ability; } u32 IsAbilityOnSide(u32 battlerId, u32 ability) From 4b0325dbe49cf5434cffde97e825ffc8337d7797 Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Sat, 30 Oct 2021 09:13:08 -0400 Subject: [PATCH 04/46] neutralizing gas switchout --- data/battle_scripts_1.s | 9 ++++++++- src/battle_script_commands.c | 5 +++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/data/battle_scripts_1.s b/data/battle_scripts_1.s index e104db758..9fdd54e71 100644 --- a/data/battle_scripts_1.s +++ b/data/battle_scripts_1.s @@ -8916,13 +8916,20 @@ BattleScript_DarkTypePreventsPrankster:: orhalfword gMoveResultFlags, MOVE_RESULT_NO_EFFECT goto BattleScript_MoveEnd +sByteFour: +.byte MAX_BATTLERS_COUNT + BattleScript_NeutralizingGasExits:: + savetarget pause B_WAIT_TIME_SHORT printstring STRINGID_NEUTRALIZINGGASOVER waitmessage B_WAIT_TIME_LONG setbyte gBattlerTarget, 0 BattleScript_NeutralizingGasExitsLoop: + jumpifbyteequal gEffectBattler, gBattlerTarget, BattleScript_NeutralizingGasExitsIncrement @ skip over battler switching out switchinabilities BS_TARGET +BattleScript_NeutralizingGasExitsIncrement: addbyte gBattlerTarget, 1 - jumpifbytenotequal gBattlerTarget, gBattlersCount, BattleScript_NeutralizingGasExitsLoop + jumpifbytenotequal gBattlerTarget, sByteFour, BattleScript_NeutralizingGasExitsLoop @ SOMEHOW, comparing to gBattlersCount is problematic. + restoretarget return diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index e2c6aff02..35b04fcd2 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -12293,8 +12293,9 @@ static void Cmd_switchoutabilities(void) if (gBattleMons[gActiveBattler].ability == ABILITY_NEUTRALIZING_GAS) { - gBattleMons[gActiveBattler].ability = 0; - BattleScriptPushCursor(); + gBattleMons[gActiveBattler].ability = ABILITY_NONE; + gEffectBattler = gActiveBattler; + BattleScriptPush(gBattlescriptCurrInstr); gBattlescriptCurrInstr = BattleScript_NeutralizingGasExits; } else From dc9e418155452ae38e1f6b9fe8a87be31e0d3339 Mon Sep 17 00:00:00 2001 From: ghoulslash <41651341+ghoulslash@users.noreply.github.com> Date: Sat, 30 Oct 2021 11:53:58 -0400 Subject: [PATCH 05/46] Update src/battle_script_commands.c Co-authored-by: LOuroboros --- src/battle_script_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 35b04fcd2..062f77876 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -5416,7 +5416,7 @@ static void Cmd_sethealblock(void) static void Cmd_returnatktoball(void) { - gActiveBattler = gBattlerAttacker; + gActiveBattler = gBattlerAttacker; if (!(gHitMarker & HITMARKER_FAINTED(gActiveBattler))) { BtlController_EmitReturnMonToBall(0, 0); From 915054e84e535d8a24d8fb456bfafd6246c0f244 Mon Sep 17 00:00:00 2001 From: ghoulslash <41651341+ghoulslash@users.noreply.github.com> Date: Sat, 30 Oct 2021 11:54:04 -0400 Subject: [PATCH 06/46] Update src/battle_script_commands.c Co-authored-by: LOuroboros --- src/battle_script_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 062f77876..7148ab865 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -6249,7 +6249,7 @@ static void Cmd_endlinkbattle(void) } static void Cmd_returntoball(void) -{ +{ gActiveBattler = GetBattlerForBattleScript(gBattlescriptCurrInstr[1]); BtlController_EmitReturnMonToBall(0, 1); MarkBattlerForControllerExec(gActiveBattler); From bcf272353ca8f9be51a9279fb48c91629defac8c Mon Sep 17 00:00:00 2001 From: ghoulslash <41651341+ghoulslash@users.noreply.github.com> Date: Sat, 30 Oct 2021 11:54:09 -0400 Subject: [PATCH 07/46] Update src/battle_script_commands.c Co-authored-by: LOuroboros --- src/battle_script_commands.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 7148ab865..3f53bd301 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -12290,7 +12290,6 @@ static void Cmd_trygetintimidatetarget(void) static void Cmd_switchoutabilities(void) { gActiveBattler = GetBattlerForBattleScript(gBattlescriptCurrInstr[1]); - if (gBattleMons[gActiveBattler].ability == ABILITY_NEUTRALIZING_GAS) { gBattleMons[gActiveBattler].ability = ABILITY_NONE; From ae0260fa625d852b0a2fc9870e43f2ed39ee6b10 Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Sat, 30 Oct 2021 12:06:02 -0400 Subject: [PATCH 08/46] fix run away check in TryRunFromBattle --- src/battle_util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/battle_util.c b/src/battle_util.c index 0125d362c..9497f7d18 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -627,7 +627,7 @@ bool8 TryRunFromBattle(u8 battler) effect++; } #endif - else if (gBattleMons[battler].ability == ABILITY_RUN_AWAY) + else if (GetBattlerAbility(battler) == ABILITY_RUN_AWAY) { if (InBattlePyramid()) { From a2b8f3f1693ddb2be5eaa9af9eaa9ff10fa05ee7 Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Sun, 31 Oct 2021 17:19:30 -0400 Subject: [PATCH 09/46] replace gBattleMons[x].ability w GetBattlerAbility --- src/battle_ai_switch_items.c | 6 +++--- src/battle_ai_util.c | 18 +++++++++++------ src/battle_script_commands.c | 9 +++++---- src/battle_util.c | 38 ++++++++++++++++++------------------ src/battle_util2.c | 2 +- 5 files changed, 40 insertions(+), 33 deletions(-) diff --git a/src/battle_ai_switch_items.c b/src/battle_ai_switch_items.c index e8e399935..400876cc9 100644 --- a/src/battle_ai_switch_items.c +++ b/src/battle_ai_switch_items.c @@ -82,7 +82,7 @@ static bool8 ShouldSwitchIfWonderGuard(void) opposingPosition = BATTLE_OPPOSITE(GetBattlerPosition(gActiveBattler)); - if (gBattleMons[GetBattlerAtPosition(opposingPosition)].ability != ABILITY_WONDER_GUARD) + if (GetBattlerAbility(GetBattlerAtPosition(opposingPosition)) != ABILITY_WONDER_GUARD) return FALSE; // Check if Pokemon has a super effective move. @@ -176,7 +176,7 @@ static bool8 FindMonThatAbsorbsOpponentsMove(void) else return FALSE; - if (gBattleMons[gActiveBattler].ability == absorbingTypeAbility) + if (AI_GetAbility(gActiveBattler) == absorbingTypeAbility) return FALSE; GetAIPartyIndexes(gActiveBattler, &firstId, &lastId); @@ -228,7 +228,7 @@ static bool8 ShouldSwitchIfNaturalCure(void) { if (!(gBattleMons[gActiveBattler].status1 & STATUS1_SLEEP)) return FALSE; - if (gBattleMons[gActiveBattler].ability != ABILITY_NATURAL_CURE) + if (AI_GetAbility(gActiveBattler) != ABILITY_NATURAL_CURE) return FALSE; if (gBattleMons[gActiveBattler].hp < gBattleMons[gActiveBattler].maxHP / 2) return FALSE; diff --git a/src/battle_ai_util.c b/src/battle_ai_util.c index fef6e6c6f..2223e38d3 100644 --- a/src/battle_ai_util.c +++ b/src/battle_ai_util.c @@ -1068,19 +1068,24 @@ bool32 CanTargetFaintAiWithMod(u8 battlerDef, u8 battlerAtk, s32 hpMod, s32 dmgM // does NOT include ability suppression checks s32 AI_GetAbility(u32 battlerId) { + u32 knownAbility = GetBattlerAbility(battlerId); + // The AI knows its own ability. if (IsBattlerAIControlled(battlerId)) - return gBattleMons[battlerId].ability; + return knownAbility; + + // Check neutralizing gas, gastro acid + if (knownAbility == ABILITY_NONE) + return knownAbility; if (BATTLE_HISTORY->abilities[battlerId] != ABILITY_NONE) return BATTLE_HISTORY->abilities[battlerId]; - // Abilities that prevent fleeing. - if (gBattleMons[battlerId].ability == ABILITY_SHADOW_TAG - || gBattleMons[battlerId].ability == ABILITY_MAGNET_PULL - || gBattleMons[battlerId].ability == ABILITY_ARENA_TRAP) - return gBattleMons[battlerId].ability; + // Abilities that prevent fleeing - treat as always known + if (knownAbility == ABILITY_SHADOW_TAG || knownAbility == ABILITY_MAGNET_PULL || knownAbility == ABILITY_ARENA_TRAP) + return knownAbility; + // Else, guess the ability if (gBaseStats[gBattleMons[battlerId].species].abilities[0] != ABILITY_NONE) { if (gBaseStats[gBattleMons[battlerId].species].abilities[1] != ABILITY_NONE) @@ -1093,6 +1098,7 @@ s32 AI_GetAbility(u32 battlerId) return gBaseStats[gBattleMons[battlerId].species].abilities[0]; // It's definitely ability 1. } } + return ABILITY_NONE; // Unknown. } diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 3f53bd301..79d9672af 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -6161,7 +6161,7 @@ static void Cmd_switchineffects(void) { // There is a hack here to ensure the truant counter will be 0 when the battler's next turn starts. // The truant counter is not updated in the case where a mon switches in after a lost judgement in the battle arena. - if (gBattleMons[gActiveBattler].ability == ABILITY_TRUANT + if (GetBattlerAbility(gActiveBattler) == ABILITY_TRUANT && gCurrentActionFuncId != B_ACTION_USE_MOVE && !gDisableStructs[gActiveBattler].truantSwitchInHack) gDisableStructs[gActiveBattler].truantCounter = 1; @@ -7353,7 +7353,7 @@ u32 IsLeafGuardProtected(u32 battler) bool32 IsShieldsDownProtected(u32 battler) { - return (gBattleMons[battler].ability == ABILITY_SHIELDS_DOWN + return (GetBattlerAbility(battler) == ABILITY_SHIELDS_DOWN && GetFormIdFromFormSpeciesId(gBattleMons[battler].species) < GetFormIdFromFormSpeciesId(SPECIES_MINIOR_CORE_RED)); // Minior is not in core form } @@ -7821,6 +7821,7 @@ static void Cmd_various(void) break; case VARIOUS_SWITCHIN_ABILITIES: gBattlescriptCurrInstr += 3; + AbilityBattleEffects(ABILITYEFFECT_NEUTRALIZINGGAS, gActiveBattler, 0, 0, 0); AbilityBattleEffects(ABILITYEFFECT_ON_SWITCHIN, gActiveBattler, 0, 0, 0); AbilityBattleEffects(ABILITYEFFECT_INTIMIDATE2, gActiveBattler, 0, 0, 0); AbilityBattleEffects(ABILITYEFFECT_TRACE2, gActiveBattler, 0, 0, 0); @@ -11060,11 +11061,11 @@ static void Cmd_healpartystatus(void) u16 ability; if (gBattlerPartyIndexes[gBattlerAttacker] == i) - ability = gBattleMons[gBattlerAttacker].ability; + ability = GetBattlerAbility(gBattlerAttacker); else if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE && gBattlerPartyIndexes[gActiveBattler] == i && !(gAbsentBattlerFlags & gBitTable[gActiveBattler])) - ability = gBattleMons[gActiveBattler].ability; + ability = GetBattlerAbility(gBattlerAttacker); else ability = GetAbilityBySpecies(species, abilityNum); diff --git a/src/battle_util.c b/src/battle_util.c index 9497f7d18..212f4216e 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -317,8 +317,8 @@ void HandleAction_UseMove(void) else if ((gBattleTypeFlags & BATTLE_TYPE_DOUBLE) && gSideTimers[side].followmeTimer == 0 && (gBattleMoves[gCurrentMove].power != 0 || gBattleMoves[gCurrentMove].target != MOVE_TARGET_USER) - && ((gBattleMons[*(gBattleStruct->moveTarget + gBattlerAttacker)].ability != ABILITY_LIGHTNING_ROD && moveType == TYPE_ELECTRIC) - || (gBattleMons[*(gBattleStruct->moveTarget + gBattlerAttacker)].ability != ABILITY_STORM_DRAIN && moveType == TYPE_WATER))) + && ((GetBattlerAbility(*(gBattleStruct->moveTarget + gBattlerAttacker)) != ABILITY_LIGHTNING_ROD && moveType == TYPE_ELECTRIC) + || (GetBattlerAbility(*(gBattleStruct->moveTarget + gBattlerAttacker)) != ABILITY_STORM_DRAIN && moveType == TYPE_WATER))) { side = GetBattlerSide(gBattlerAttacker); for (gActiveBattler = 0; gActiveBattler < gBattlersCount; gActiveBattler++) @@ -387,9 +387,9 @@ void HandleAction_UseMove(void) { gActiveBattler = gBattlerByTurnOrder[var]; RecordAbilityBattle(gActiveBattler, gBattleMons[gActiveBattler].ability); - if (gBattleMons[gActiveBattler].ability == ABILITY_LIGHTNING_ROD) + if (GetBattlerAbility(gActiveBattler) == ABILITY_LIGHTNING_ROD) gSpecialStatuses[gActiveBattler].lightningRodRedirected = 1; - else if (gBattleMons[gActiveBattler].ability == ABILITY_STORM_DRAIN) + else if (GetBattlerAbility(gActiveBattler) == ABILITY_STORM_DRAIN) gSpecialStatuses[gActiveBattler].stormDrainRedirected = 1; gBattlerTarget = gActiveBattler; } @@ -2641,7 +2641,7 @@ u8 DoBattlerEndTurnEffects(void) for (gBattlerAttacker = 0; gBattlerAttacker < gBattlersCount; gBattlerAttacker++) { if ((gBattleMons[gBattlerAttacker].status1 & STATUS1_SLEEP) - && gBattleMons[gBattlerAttacker].ability != ABILITY_SOUNDPROOF) + && GetBattlerAbility(gBattlerAttacker) != ABILITY_SOUNDPROOF) { gBattleMons[gBattlerAttacker].status1 &= ~(STATUS1_SLEEP); gBattleMons[gBattlerAttacker].status2 &= ~(STATUS2_NIGHTMARE); @@ -3225,7 +3225,7 @@ u8 AtkCanceller_UnableToUseMove(void) gBattleStruct->atkCancellerTracker++; break; case CANCELLER_TRUANT: // truant - if (gBattleMons[gBattlerAttacker].ability == ABILITY_TRUANT && gDisableStructs[gBattlerAttacker].truantCounter) + if (GetBattlerAbility(gBattlerAttacker) == ABILITY_TRUANT && gDisableStructs[gBattlerAttacker].truantCounter) { CancelMultiTurnMoves(gBattlerAttacker); gHitMarker |= HITMARKER_UNABLE_TO_USE_MOVE; @@ -3665,7 +3665,7 @@ u8 TryWeatherFormChange(u8 battler) if (gBattleMons[battler].species == SPECIES_CASTFORM) { - if (gBattleMons[battler].ability != ABILITY_FORECAST || gBattleMons[battler].hp == 0) + if (GetBattlerAbility(battler) != ABILITY_FORECAST || gBattleMons[battler].hp == 0) { ret = 0; } @@ -3701,7 +3701,7 @@ u8 TryWeatherFormChange(u8 battler) } else if (gBattleMons[battler].species == SPECIES_CHERRIM) { - if (gBattleMons[battler].ability != ABILITY_FLOWER_GIFT || gBattleMons[battler].hp == 0) + if (GetBattlerAbility(battler) != ABILITY_FLOWER_GIFT || gBattleMons[battler].hp == 0) ret = 0; else if (gBattleMonForms[battler] == 0 && weatherEffect && gBattleWeather & WEATHER_SUN_ANY) ret = 2; @@ -3791,7 +3791,7 @@ static bool32 ShouldChangeFormHpBased(u32 battler) for (i = 0; i < ARRAY_COUNT(forms); i++) { - if (gBattleMons[battler].ability == forms[i][0]) + if (GetBattlerAbility(battler) == forms[i][0]) { if (gBattleMons[battler].species == forms[i][2] && gBattleMons[battler].hp > gBattleMons[battler].maxHP / forms[i][3]) @@ -5220,7 +5220,7 @@ u8 AbilityBattleEffects(u8 caseID, u8 battler, u16 ability, u8 special, u16 move case ABILITYEFFECT_IMMUNITY: // 5 for (battler = 0; battler < gBattlersCount; battler++) { - switch (gBattleMons[battler].ability) + switch (GetBattlerAbility(battler)) { case ABILITY_IMMUNITY: if (gBattleMons[battler].status1 & (STATUS1_POISON | STATUS1_TOXIC_POISON | STATUS1_TOXIC_COUNTER)) @@ -5310,7 +5310,7 @@ u8 AbilityBattleEffects(u8 caseID, u8 battler, u16 ability, u8 special, u16 move case ABILITYEFFECT_FORECAST: // 6 for (battler = 0; battler < gBattlersCount; battler++) { - if (gBattleMons[battler].ability == ABILITY_FORECAST || gBattleMons[battler].ability == ABILITY_FLOWER_GIFT) + if (GetBattlerAbility(battler) == ABILITY_FORECAST || GetBattlerAbility(battler) == ABILITY_FLOWER_GIFT) { effect = TryWeatherFormChange(battler); if (effect) @@ -5369,7 +5369,7 @@ u8 AbilityBattleEffects(u8 caseID, u8 battler, u16 ability, u8 special, u16 move case ABILITYEFFECT_INTIMIDATE2: for (i = 0; i < gBattlersCount; i++) { - if (gBattleMons[i].ability == ABILITY_INTIMIDATE && gBattleResources->flags->flags[i] & RESOURCE_FLAG_INTIMIDATED) + if (GetBattlerAbility(i) == ABILITY_INTIMIDATE && gBattleResources->flags->flags[i] & RESOURCE_FLAG_INTIMIDATED) { gLastUsedAbility = ABILITY_INTIMIDATE; gBattleResources->flags->flags[i] &= ~(RESOURCE_FLAG_INTIMIDATED); @@ -5437,7 +5437,7 @@ u8 AbilityBattleEffects(u8 caseID, u8 battler, u16 ability, u8 special, u16 move } break; case ABILITYEFFECT_NEUTRALIZINGGAS: - // for the start of battle only. The switch-in message plays in Cmd_switchineffects because it comes before the hazards + // Prints message only. separate from ABILITYEFFECT_ON_SWITCHIN bc activates before entry hazards for (i = 0; i < gBattlersCount; i++) { if (gBattleMons[i].ability == ABILITY_NEUTRALIZING_GAS && !(gBattleResources->flags->flags[i] & RESOURCE_FLAG_NEUTRALIZING_GAS)) @@ -5460,7 +5460,7 @@ u8 AbilityBattleEffects(u8 caseID, u8 battler, u16 ability, u8 special, u16 move return effect; } -static bool32 IsNeutralizingGasBannedAbility(u32 ability) +bool32 IsNeutralizingGasBannedAbility(u32 ability) { switch (ability) { @@ -5483,7 +5483,7 @@ static bool32 IsNeutralizingGasBannedAbility(u32 ability) } } -static bool32 IsNeutralizingGasOnField(void) +bool32 IsNeutralizingGasOnField(void) { u32 i; @@ -5567,7 +5567,7 @@ u32 IsAbilityPreventingEscape(u32 battlerId) return 0; #endif #if B_SHADOW_TAG_ESCAPE >= GEN_4 - if ((id = IsAbilityOnOpposingSide(battlerId, ABILITY_SHADOW_TAG)) && gBattleMons[battlerId].ability != ABILITY_SHADOW_TAG) + if ((id = IsAbilityOnOpposingSide(battlerId, ABILITY_SHADOW_TAG)) && GetBattlerAbility(battlerId) != ABILITY_SHADOW_TAG) #else if (id = IsAbilityOnOpposingSide(battlerId, ABILITY_SHADOW_TAG)) #endif @@ -7113,7 +7113,7 @@ u32 GetMoveTarget(u16 move, u8 setTarget) targetBattler = SetRandomTarget(gBattlerAttacker); if (gBattleMoves[move].type == TYPE_ELECTRIC && IsAbilityOnOpposingSide(gBattlerAttacker, ABILITY_LIGHTNING_ROD) - && gBattleMons[targetBattler].ability != ABILITY_LIGHTNING_ROD) + && GetBattlerAbility(targetBattler) != ABILITY_LIGHTNING_ROD) { targetBattler ^= BIT_FLANK; RecordAbilityBattle(targetBattler, gBattleMons[targetBattler].ability); @@ -7121,7 +7121,7 @@ u32 GetMoveTarget(u16 move, u8 setTarget) } else if (gBattleMoves[move].type == TYPE_WATER && IsAbilityOnOpposingSide(gBattlerAttacker, ABILITY_STORM_DRAIN) - && gBattleMons[targetBattler].ability != ABILITY_STORM_DRAIN) + && GetBattlerAbility(targetBattler) != ABILITY_STORM_DRAIN) { targetBattler ^= BIT_FLANK; RecordAbilityBattle(targetBattler, gBattleMons[targetBattler].ability); @@ -7299,7 +7299,7 @@ u32 GetBattlerHoldEffect(u8 battlerId, bool32 checkNegating) return HOLD_EFFECT_NONE; if (gFieldStatuses & STATUS_FIELD_MAGIC_ROOM) return HOLD_EFFECT_NONE; - if (gBattleMons[battlerId].ability == ABILITY_KLUTZ && !(gStatuses3[battlerId] & STATUS3_GASTRO_ACID)) + if (GetBattlerAbility(battlerId) == ABILITY_KLUTZ && !(gStatuses3[battlerId] & STATUS3_GASTRO_ACID)) return HOLD_EFFECT_NONE; } diff --git a/src/battle_util2.c b/src/battle_util2.c index d15700706..0af19de27 100644 --- a/src/battle_util2.c +++ b/src/battle_util2.c @@ -142,7 +142,7 @@ u32 sub_805725C(u8 battlerId) { u32 toSub; - if (gBattleMons[battlerId].ability == ABILITY_EARLY_BIRD) + if (GetBattlerAbility(battlerId) == ABILITY_EARLY_BIRD) toSub = 2; else toSub = 1; From a1e64fce4f52f40a68d06f18e313e76450677009 Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Mon, 1 Nov 2021 13:50:57 -0400 Subject: [PATCH 10/46] handle simple beam, gastro acid --- asm/macros/battle_script.inc | 4 ++++ data/battle_scripts_1.s | 2 ++ include/battle.h | 3 ++- include/constants/battle_script_commands.h | 1 + src/battle_script_commands.c | 15 +++++++++++++++ src/battle_util.c | 2 +- 6 files changed, 25 insertions(+), 2 deletions(-) diff --git a/asm/macros/battle_script.inc b/asm/macros/battle_script.inc index 04f6e04dc..80c78a932 100644 --- a/asm/macros/battle_script.inc +++ b/asm/macros/battle_script.inc @@ -1863,6 +1863,10 @@ .2byte \species .4byte \ptr .endm + + .macro tryendneutralizinggas battler:req + various \battler, VARIOUS_TRY_END_NEUTRALIZING_GAS + .endm @ helpful macros .macro setstatchanger stat:req, stages:req, down:req diff --git a/data/battle_scripts_1.s b/data/battle_scripts_1.s index 9fdd54e71..2b07f49d9 100644 --- a/data/battle_scripts_1.s +++ b/data/battle_scripts_1.s @@ -1993,6 +1993,7 @@ BattleScript_EffectSimpleBeam: printstring STRINGID_PKMNACQUIREDSIMPLE waitmessage B_WAIT_TIME_LONG trytoclearprimalweather + tryendneutralizinggas BS_TARGET printstring STRINGID_EMPTYSTRING3 waitmessage 1 goto BattleScript_MoveEnd @@ -2215,6 +2216,7 @@ BattleScript_EffectGastroAcid: printstring STRINGID_PKMNSABILITYSUPPRESSED waitmessage B_WAIT_TIME_LONG trytoclearprimalweather + tryendneutralizinggas BS_TARGET printstring STRINGID_EMPTYSTRING3 waitmessage 1 goto BattleScript_MoveEnd diff --git a/include/battle.h b/include/battle.h index 68a40c978..d50522083 100644 --- a/include/battle.h +++ b/include/battle.h @@ -182,7 +182,8 @@ struct SpecialStatus u8 damagedMons:4; // Mons that have been damaged directly by using a move, includes substitute. u8 dancerUsedMove:1; u8 dancerOriginalTarget:3; - u8 announceNeutralizingGas:1; + u8 announceNeutralizingGas:1; // See Cmd_switchineffects + u8 neutralizingGasRemoved:1; // See VARIOUS_TRY_END_NEUTRALIZING_GAS s32 dmg; s32 physicalDmg; s32 specialDmg; diff --git a/include/constants/battle_script_commands.h b/include/constants/battle_script_commands.h index f00695276..d05559bf4 100644 --- a/include/constants/battle_script_commands.h +++ b/include/constants/battle_script_commands.h @@ -194,6 +194,7 @@ #define VARIOUS_HANDLE_PRIMAL_REVERSION 121 #define VARIOUS_APPLY_PLASMA_FISTS 122 #define VARIOUS_JUMP_IF_SPECIES 123 +#define VARIOUS_TRY_END_NEUTRALIZING_GAS 124 // Cmd_manipulatedamage #define DMG_CHANGE_SIGN 0 diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 79d9672af..802c151b4 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -8002,6 +8002,9 @@ static void Cmd_various(void) } else { + if (gBattleMons[gBattlerTarget].ability == ABILITY_NEUTRALIZING_GAS) + gSpecialStatuses[gBattlerTarget].neutralizingGasRemoved = TRUE; + gBattleMons[gBattlerTarget].ability = ABILITY_SIMPLE; gBattlescriptCurrInstr += 7; } @@ -8899,6 +8902,15 @@ static void Cmd_various(void) } break; } + case VARIOUS_TRY_END_NEUTRALIZING_GAS: + if (gSpecialStatuses[gActiveBattler].neutralizingGasRemoved) + { + gSpecialStatuses[gActiveBattler].neutralizingGasRemoved = FALSE; + BattleScriptPush(gBattlescriptCurrInstr + 3); + gBattlescriptCurrInstr = BattleScript_NeutralizingGasExits; + return; + } + break; case VARIOUS_GET_ROTOTILLER_TARGETS: // Gets the battlers to be affected by rototiller. If there are none, print 'But it failed!' { @@ -12011,6 +12023,9 @@ static void Cmd_setgastroacid(void) } else { + if (gBattleMons[gBattlerTarget].ability == ABILITY_NEUTRALIZING_GAS) + gSpecialStatuses[gBattlerTarget].neutralizingGasRemoved = TRUE; + gStatuses3[gBattlerTarget] |= STATUS3_GASTRO_ACID; gBattlescriptCurrInstr += 5; } diff --git a/src/battle_util.c b/src/battle_util.c index 212f4216e..3ec6fc3d9 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -5489,7 +5489,7 @@ bool32 IsNeutralizingGasOnField(void) for (i = 0; i < gBattlersCount; i++) { - if (IsBattlerAlive(i) && gBattleMons[i].ability == ABILITY_NEUTRALIZING_GAS) + if (IsBattlerAlive(i) && gBattleMons[i].ability == ABILITY_NEUTRALIZING_GAS && !(gStatuses3[i] & STATUS3_GASTRO_ACID)) return TRUE; } From 48bea554a3ce764bc8546e326f5eb92afbbde88a Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Mon, 1 Nov 2021 14:21:28 -0400 Subject: [PATCH 11/46] fix tryendneutralizinggas placement --- data/battle_scripts_1.s | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data/battle_scripts_1.s b/data/battle_scripts_1.s index aca99afbc..3549a1f7f 100644 --- a/data/battle_scripts_1.s +++ b/data/battle_scripts_1.s @@ -1993,9 +1993,9 @@ BattleScript_EffectSimpleBeam: printstring STRINGID_PKMNACQUIREDSIMPLE waitmessage B_WAIT_TIME_LONG trytoclearprimalweather - tryendneutralizinggas BS_TARGET printstring STRINGID_EMPTYSTRING3 waitmessage 1 + tryendneutralizinggas BS_TARGET goto BattleScript_MoveEnd BattleScript_EffectSuckerPunch: @@ -2216,9 +2216,9 @@ BattleScript_EffectGastroAcid: printstring STRINGID_PKMNSABILITYSUPPRESSED waitmessage B_WAIT_TIME_LONG trytoclearprimalweather - tryendneutralizinggas BS_TARGET printstring STRINGID_EMPTYSTRING3 waitmessage 1 + tryendneutralizinggas BS_TARGET goto BattleScript_MoveEnd BattleScript_EffectToxicSpikes: From e4242d04abf2a1c3e906283302a4fe3aee1ac2a8 Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Mon, 1 Nov 2021 14:28:41 -0400 Subject: [PATCH 12/46] remove battler skipping in neutralizing gas exit bs --- data/battle_scripts_1.s | 2 -- src/battle_script_commands.c | 1 - 2 files changed, 3 deletions(-) diff --git a/data/battle_scripts_1.s b/data/battle_scripts_1.s index 3549a1f7f..59941ec23 100644 --- a/data/battle_scripts_1.s +++ b/data/battle_scripts_1.s @@ -8962,9 +8962,7 @@ BattleScript_NeutralizingGasExits:: waitmessage B_WAIT_TIME_LONG setbyte gBattlerTarget, 0 BattleScript_NeutralizingGasExitsLoop: - jumpifbyteequal gEffectBattler, gBattlerTarget, BattleScript_NeutralizingGasExitsIncrement @ skip over battler switching out switchinabilities BS_TARGET -BattleScript_NeutralizingGasExitsIncrement: addbyte gBattlerTarget, 1 jumpifbytenotequal gBattlerTarget, sByteFour, BattleScript_NeutralizingGasExitsLoop @ SOMEHOW, comparing to gBattlersCount is problematic. restoretarget diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 5c61b9278..b4345bff5 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -12312,7 +12312,6 @@ static void Cmd_switchoutabilities(void) if (gBattleMons[gActiveBattler].ability == ABILITY_NEUTRALIZING_GAS) { gBattleMons[gActiveBattler].ability = ABILITY_NONE; - gEffectBattler = gActiveBattler; BattleScriptPush(gBattlescriptCurrInstr); gBattlescriptCurrInstr = BattleScript_NeutralizingGasExits; } From 802ba88621a65f0be7d1f1849ae42e2dab990a70 Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Mon, 1 Nov 2021 15:10:51 -0400 Subject: [PATCH 13/46] fix ABILITYEFFECT_NEUTRALIZINGGAS --- src/battle_util.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/battle_util.c b/src/battle_util.c index 071f999f3..b39df4992 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -5498,6 +5498,9 @@ u8 AbilityBattleEffects(u8 caseID, u8 battler, u16 ability, u8 special, u16 move BattleScriptPushCursorAndCallback(BattleScript_SwitchInAbilityMsg); effect++; } + + if (effect) + break; } break; } From 71ca9fdb58345542c7de05ce7b25a65b824b8d90 Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Mon, 1 Nov 2021 15:18:41 -0400 Subject: [PATCH 14/46] fix Cmd_healpartystatus GetBattlerAbility check --- src/battle_script_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index b4345bff5..db9794836 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -11080,7 +11080,7 @@ static void Cmd_healpartystatus(void) else if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE && gBattlerPartyIndexes[gActiveBattler] == i && !(gAbsentBattlerFlags & gBitTable[gActiveBattler])) - ability = GetBattlerAbility(gBattlerAttacker); + ability = GetBattlerAbility(gActiveBattler); else ability = GetAbilityBySpecies(species, abilityNum); From 8f320a8d33a3a7222471a5c1bf10108f325430a9 Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Mon, 8 Nov 2021 11:37:41 -0500 Subject: [PATCH 15/46] some ai updates, recycle + ripen logic --- include/battle_ai_util.h | 2 ++ src/battle_ai_main.c | 13 +++++++++- src/battle_ai_util.c | 56 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/include/battle_ai_util.h b/include/battle_ai_util.h index 1b2591899..6da519342 100644 --- a/include/battle_ai_util.h +++ b/include/battle_ai_util.h @@ -52,6 +52,8 @@ 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 IsHpRestoringBerry(u16 item); +bool32 IsStatBoostingBerry(u16 item); bool32 CanKnockOffItem(u8 battler, u16 item); bool32 IsAbilityOfRating(u16 ability, s8 rating); s8 GetAbilityRating(u16 ability); diff --git a/src/battle_ai_main.c b/src/battle_ai_main.c index 7fbb43260..b8dbdebac 100644 --- a/src/battle_ai_main.c +++ b/src/battle_ai_main.c @@ -2969,6 +2969,10 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) { case ABILITY_MOXIE: case ABILITY_BEAST_BOOST: + case ABILITY_CHILLING_NEIGH: + case ABILITY_GRIM_NEIGH: + case ABILITY_AS_ONE_ICE_RIDER: + case ABILITY_AS_ONE_SHADOW_RIDER: if (GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0) // attacker should go first { if (CanIndexMoveFaintTarget(battlerAtk, battlerDef, AI_THINKING_STRUCT->movesetIndex, 0)) @@ -2980,7 +2984,6 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) // move effect checks switch (moveEffect) { - case EFFECT_HIT: break; case EFFECT_SLEEP: @@ -4101,6 +4104,14 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) score++; if (IsRecycleEncouragedItem(GetUsedHeldItem(battlerAtk))) score++; + if (AI_DATA->atkAbility == ABILITY_RIPEN) + { + u16 item = GetUsedHeldItem(battlerAtk); + if (IsStatBoostingBerry(item) && atkHpPercent > 60) + score++; + else if (IsHpRestoringBerry(item) && atkHpPercent < 60) + score++; // TODO check if player can still faint us after we heal + } break; case EFFECT_BRICK_BREAK: if (gSideStatuses[GetBattlerSide(battlerDef)] & SIDE_STATUS_REFLECT) diff --git a/src/battle_ai_util.c b/src/battle_ai_util.c index 71dc78d0a..3ccf065a5 100644 --- a/src/battle_ai_util.c +++ b/src/battle_ai_util.c @@ -3368,6 +3368,44 @@ static const u16 sRecycleEncouragedItems[] = // TODO expand this }; +// Its assumed that the berry is strategically given, so no need to check benefits of the berry +bool32 IsStatBoostingBerry(u16 item) +{ + switch (item) + { + case ITEM_LIECHI_BERRY: + case ITEM_GANLON_BERRY: + case ITEM_SALAC_BERRY: + case ITEM_PETAYA_BERRY: + case ITEM_APICOT_BERRY: + //case ITEM_LANSAT_BERRY: + case ITEM_STARF_BERRY: + #ifdef ITEM_EXPANSION + case ITEM_MICLE_BERRY: + #endif + return TRUE; + default: + return FALSE; + } +} + +bool32 IsHpRestoringBerry(u16 item) +{ + switch (item) + { + case ITEM_ORAN_BERRY: + case ITEM_SITRUS_BERRY: + case ITEM_FIGY_BERRY: + case ITEM_WIKI_BERRY: + case ITEM_MAGO_BERRY: + case ITEM_AGUAV_BERRY: + case ITEM_IAPAPA_BERRY: + return TRUE; + default: + return FALSE; + } +} + bool32 IsRecycleEncouragedItem(u16 item) { u32 i; @@ -3389,6 +3427,9 @@ void IncreaseStatUpScore(u8 battlerAtk, u8 battlerDef, u8 statId, s16 *score) if (GetHealthPercentage(battlerAtk) < 80 && AI_RandLessThan(128)) return; + + if (CanAIFaintTarget(battlerAtk, battlerDef, 0)) + return; // Damaging moves would get a score boost from AI_TryToFaint or PreferStrongestMove so we don't consider them here switch (statId) { @@ -3461,6 +3502,9 @@ void IncreaseStatUpScore(u8 battlerAtk, u8 battlerDef, u8 statId, s16 *score) void IncreasePoisonScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) { + if (CanAIFaintTarget(battlerAtk, battlerDef, 0)) + return; + if (AI_CanPoison(battlerAtk, battlerDef, AI_DATA->defAbility, move, AI_DATA->partnerMove) && GetHealthPercentage(battlerDef) > 20) { if (!HasDamagingMove(battlerDef)) @@ -3481,6 +3525,9 @@ void IncreasePoisonScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) void IncreaseBurnScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) { + if (CanAIFaintTarget(battlerAtk, battlerDef, 0)) + return; + if (AI_CanBurn(battlerAtk, battlerDef, AI_DATA->defAbility, AI_DATA->battlerAtkPartner, move, AI_DATA->partnerMove)) { (*score)++; // burning is good @@ -3497,6 +3544,9 @@ void IncreaseBurnScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) void IncreaseParalyzeScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) { + if (CanAIFaintTarget(battlerAtk, battlerDef, 0)) + return; + if (AI_CanParalyze(battlerAtk, battlerDef, AI_DATA->defAbility, move, AI_DATA->partnerMove)) { u8 atkSpeed = GetBattlerTotalSpeedStat(battlerAtk); @@ -3515,6 +3565,9 @@ void IncreaseParalyzeScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) void IncreaseSleepScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) { + if (CanAIFaintTarget(battlerAtk, battlerDef, 0)) + return; + if (AI_CanPutToSleep(battlerAtk, battlerDef, AI_DATA->defAbility, move, AI_DATA->partnerMove)) *score += 2; else @@ -3530,6 +3583,9 @@ void IncreaseSleepScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) void IncreaseConfusionScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) { + if (CanAIFaintTarget(battlerAtk, battlerDef, 0)) + return; + 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) From e5a0630596a83fe823d5bae01f12aa8dc79d7cfd Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Mon, 8 Nov 2021 11:42:32 -0500 Subject: [PATCH 16/46] add ai flag checks alongside CanFaintTarget --- src/battle_ai_util.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/battle_ai_util.c b/src/battle_ai_util.c index 3ccf065a5..60083bb29 100644 --- a/src/battle_ai_util.c +++ b/src/battle_ai_util.c @@ -1703,7 +1703,7 @@ u32 CountNegativeStatStages(u8 battlerId) bool32 ShouldLowerAttack(u8 battlerAtk, u8 battlerDef, u16 defAbility) { - if (IsAiFaster(AI_CHECK_FASTER) && CanAIFaintTarget(battlerAtk, battlerDef, 0)) + if (IsAiFaster(AI_CHECK_FASTER) && (AI_THINKING_STRUCT->aiFlags & AI_FLAG_TRY_TO_FAINT) && CanAIFaintTarget(battlerAtk, battlerDef, 0)) return FALSE; // Don't bother lowering stats if can kill enemy. if (gBattleMons[battlerDef].statStages[STAT_ATK] > 4 @@ -1719,7 +1719,7 @@ bool32 ShouldLowerAttack(u8 battlerAtk, u8 battlerDef, u16 defAbility) bool32 ShouldLowerDefense(u8 battlerAtk, u8 battlerDef, u16 defAbility) { - if (IsAiFaster(AI_CHECK_FASTER) && CanAIFaintTarget(battlerAtk, battlerDef, 0)) + if (IsAiFaster(AI_CHECK_FASTER) && (AI_THINKING_STRUCT->aiFlags & AI_FLAG_TRY_TO_FAINT) && CanAIFaintTarget(battlerAtk, battlerDef, 0)) return FALSE; // Don't bother lowering stats if can kill enemy. if (gBattleMons[battlerDef].statStages[STAT_DEF] > 4 @@ -1735,7 +1735,7 @@ bool32 ShouldLowerDefense(u8 battlerAtk, u8 battlerDef, u16 defAbility) bool32 ShouldLowerSpeed(u8 battlerAtk, u8 battlerDef, u16 defAbility) { - if (IsAiFaster(AI_CHECK_FASTER) && CanAIFaintTarget(battlerAtk, battlerDef, 0)) + if (IsAiFaster(AI_CHECK_FASTER) && (AI_THINKING_STRUCT->aiFlags & AI_FLAG_TRY_TO_FAINT) && CanAIFaintTarget(battlerAtk, battlerDef, 0)) return FALSE; // Don't bother lowering stats if can kill enemy. if (IsAiFaster(AI_CHECK_SLOWER) @@ -1749,7 +1749,7 @@ bool32 ShouldLowerSpeed(u8 battlerAtk, u8 battlerDef, u16 defAbility) bool32 ShouldLowerSpAtk(u8 battlerAtk, u8 battlerDef, u16 defAbility) { - if (IsAiFaster(AI_CHECK_FASTER) && CanAIFaintTarget(battlerAtk, battlerDef, 0)) + if (IsAiFaster(AI_CHECK_FASTER) && (AI_THINKING_STRUCT->aiFlags & AI_FLAG_TRY_TO_FAINT) && CanAIFaintTarget(battlerAtk, battlerDef, 0)) return FALSE; // Don't bother lowering stats if can kill enemy. if (gBattleMons[battlerDef].statStages[STAT_SPATK] > 4 @@ -1764,7 +1764,7 @@ bool32 ShouldLowerSpAtk(u8 battlerAtk, u8 battlerDef, u16 defAbility) bool32 ShouldLowerSpDef(u8 battlerAtk, u8 battlerDef, u16 defAbility) { - if (IsAiFaster(AI_CHECK_FASTER) && CanAIFaintTarget(battlerAtk, battlerDef, 0)) + if (IsAiFaster(AI_CHECK_FASTER) && (AI_THINKING_STRUCT->aiFlags & AI_FLAG_TRY_TO_FAINT) && CanAIFaintTarget(battlerAtk, battlerDef, 0)) return FALSE; // Don't bother lowering stats if can kill enemy. if (gBattleMons[battlerDef].statStages[STAT_SPDEF] > 4 @@ -1779,7 +1779,7 @@ bool32 ShouldLowerSpDef(u8 battlerAtk, u8 battlerDef, u16 defAbility) bool32 ShouldLowerAccuracy(u8 battlerAtk, u8 battlerDef, u16 defAbility) { - if (IsAiFaster(AI_CHECK_FASTER) && CanAIFaintTarget(battlerAtk, battlerDef, 0)) + if (IsAiFaster(AI_CHECK_FASTER) && (AI_THINKING_STRUCT->aiFlags & AI_FLAG_TRY_TO_FAINT) && CanAIFaintTarget(battlerAtk, battlerDef, 0)) return FALSE; // Don't bother lowering stats if can kill enemy. if (defAbility != ABILITY_CONTRARY @@ -1793,7 +1793,7 @@ bool32 ShouldLowerAccuracy(u8 battlerAtk, u8 battlerDef, u16 defAbility) bool32 ShouldLowerEvasion(u8 battlerAtk, u8 battlerDef, u16 defAbility) { - if (IsAiFaster(AI_CHECK_FASTER) && CanAIFaintTarget(battlerAtk, battlerDef, 0)) + if (IsAiFaster(AI_CHECK_FASTER) && (AI_THINKING_STRUCT->aiFlags & AI_FLAG_TRY_TO_FAINT) && CanAIFaintTarget(battlerAtk, battlerDef, 0)) return FALSE; // Don't bother lowering stats if can kill enemy. if (gBattleMons[battlerDef].statStages[STAT_EVASION] > DEFAULT_STAT_STAGE @@ -3428,7 +3428,7 @@ void IncreaseStatUpScore(u8 battlerAtk, u8 battlerDef, u8 statId, s16 *score) if (GetHealthPercentage(battlerAtk) < 80 && AI_RandLessThan(128)) return; - if (CanAIFaintTarget(battlerAtk, battlerDef, 0)) + if ((AI_THINKING_STRUCT->aiFlags & AI_FLAG_TRY_TO_FAINT) && CanAIFaintTarget(battlerAtk, battlerDef, 0)) return; // Damaging moves would get a score boost from AI_TryToFaint or PreferStrongestMove so we don't consider them here switch (statId) @@ -3502,7 +3502,7 @@ void IncreaseStatUpScore(u8 battlerAtk, u8 battlerDef, u8 statId, s16 *score) void IncreasePoisonScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) { - if (CanAIFaintTarget(battlerAtk, battlerDef, 0)) + if ((AI_THINKING_STRUCT->aiFlags & AI_FLAG_TRY_TO_FAINT) && CanAIFaintTarget(battlerAtk, battlerDef, 0)) return; if (AI_CanPoison(battlerAtk, battlerDef, AI_DATA->defAbility, move, AI_DATA->partnerMove) && GetHealthPercentage(battlerDef) > 20) @@ -3525,7 +3525,7 @@ void IncreasePoisonScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) void IncreaseBurnScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) { - if (CanAIFaintTarget(battlerAtk, battlerDef, 0)) + if ((AI_THINKING_STRUCT->aiFlags & AI_FLAG_TRY_TO_FAINT) && CanAIFaintTarget(battlerAtk, battlerDef, 0)) return; if (AI_CanBurn(battlerAtk, battlerDef, AI_DATA->defAbility, AI_DATA->battlerAtkPartner, move, AI_DATA->partnerMove)) @@ -3544,7 +3544,7 @@ void IncreaseBurnScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) void IncreaseParalyzeScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) { - if (CanAIFaintTarget(battlerAtk, battlerDef, 0)) + if ((AI_THINKING_STRUCT->aiFlags & AI_FLAG_TRY_TO_FAINT) && CanAIFaintTarget(battlerAtk, battlerDef, 0)) return; if (AI_CanParalyze(battlerAtk, battlerDef, AI_DATA->defAbility, move, AI_DATA->partnerMove)) @@ -3565,7 +3565,7 @@ void IncreaseParalyzeScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) void IncreaseSleepScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) { - if (CanAIFaintTarget(battlerAtk, battlerDef, 0)) + if ((AI_THINKING_STRUCT->aiFlags & AI_FLAG_TRY_TO_FAINT) && CanAIFaintTarget(battlerAtk, battlerDef, 0)) return; if (AI_CanPutToSleep(battlerAtk, battlerDef, AI_DATA->defAbility, move, AI_DATA->partnerMove)) @@ -3583,7 +3583,7 @@ void IncreaseSleepScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) void IncreaseConfusionScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) { - if (CanAIFaintTarget(battlerAtk, battlerDef, 0)) + if ((AI_THINKING_STRUCT->aiFlags & AI_FLAG_TRY_TO_FAINT) && CanAIFaintTarget(battlerAtk, battlerDef, 0)) return; if (AI_CanConfuse(battlerAtk, battlerDef, AI_DATA->defAbility, AI_DATA->battlerAtkPartner, move, AI_DATA->partnerMove) From 01cf8e6452d592b5d8010a25fab32dcf0eab9ab7 Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Mon, 8 Nov 2021 11:55:06 -0500 Subject: [PATCH 17/46] add AI_MoveMakesContact, dissuade multi-hit attacks against rocky helmet --- include/battle_ai_util.h | 1 + src/battle_ai_main.c | 3 +++ src/battle_ai_util.c | 9 +++++++++ 3 files changed, 13 insertions(+) diff --git a/include/battle_ai_util.h b/include/battle_ai_util.h index 6da519342..cc626fa5e 100644 --- a/include/battle_ai_util.h +++ b/include/battle_ai_util.h @@ -58,6 +58,7 @@ bool32 CanKnockOffItem(u8 battler, u16 item); bool32 IsAbilityOfRating(u16 ability, s8 rating); s8 GetAbilityRating(u16 ability); bool32 AI_IsAbilityOnSide(u32 battlerId, u32 ability); +bool32 AI_MoveMakesContact(u32 ability, u32 holdEffect, u16 move); // stat stage checks bool32 AnyStatIsRaised(u8 battlerId); diff --git a/src/battle_ai_main.c b/src/battle_ai_main.c index b8dbdebac..cc0f8b2c7 100644 --- a/src/battle_ai_main.c +++ b/src/battle_ai_main.c @@ -3246,6 +3246,9 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) case EFFECT_MULTI_HIT: case EFFECT_DOUBLE_HIT: case EFFECT_TRIPLE_KICK: + if (AI_MoveMakesContact(AI_DATA->atkAbility, AI_DATA->atkHoldEffect, move) + && AI_DATA->defHoldEffect == HOLD_EFFECT_ROCKY_HELMET) + score -= 2; break; case EFFECT_CONVERSION: if (!IS_BATTLER_OF_TYPE(battlerAtk, gBattleMoves[gBattleMons[battlerAtk].moves[0]].type)) diff --git a/src/battle_ai_util.c b/src/battle_ai_util.c index 60083bb29..19dfab857 100644 --- a/src/battle_ai_util.c +++ b/src/battle_ai_util.c @@ -3598,3 +3598,12 @@ void IncreaseConfusionScore(u8 battlerAtk, u8 battlerDef, u16 move, s16 *score) *score += 2; } } + +bool32 AI_MoveMakesContact(u32 ability, u32 holdEffect, u16 move) +{ + if (TestMoveFlags(move, FLAG_MAKES_CONTACT) + && ability != ABILITY_LONG_REACH + && holdEffect != HOLD_EFFECT_PROTECTIVE_PADS) + return TRUE; + return FALSE; +} From 4566b5234fde6772184f191471455d3296ddf547 Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Mon, 8 Nov 2021 11:56:51 -0500 Subject: [PATCH 18/46] magic guard user doesnt care about rocky helmet --- src/battle_ai_main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/battle_ai_main.c b/src/battle_ai_main.c index cc0f8b2c7..317f79d97 100644 --- a/src/battle_ai_main.c +++ b/src/battle_ai_main.c @@ -3247,6 +3247,7 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) case EFFECT_DOUBLE_HIT: case EFFECT_TRIPLE_KICK: if (AI_MoveMakesContact(AI_DATA->atkAbility, AI_DATA->atkHoldEffect, move) + && AI_DATA->atkAbility != ABILITY_MAGIC_GUARD && AI_DATA->defHoldEffect == HOLD_EFFECT_ROCKY_HELMET) score -= 2; break; From 6f6e0d6baf6abe2b58b2ebe37d64ac5579b89873 Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Mon, 8 Nov 2021 21:27:40 -0500 Subject: [PATCH 19/46] add recycle hp berry logic --- src/battle_ai_main.c | 8 ++++++-- src/battle_ai_util.c | 5 ++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/battle_ai_main.c b/src/battle_ai_main.c index 317f79d97..1dcb86805 100644 --- a/src/battle_ai_main.c +++ b/src/battle_ai_main.c @@ -4111,10 +4111,14 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) if (AI_DATA->atkAbility == ABILITY_RIPEN) { u16 item = GetUsedHeldItem(battlerAtk); + u16 toHeal = (ItemId_GetHoldEffectParam(item) == 10) ? 10 : gBattleMons[battlerAtk].maxHP / ItemId_GetHoldEffectParam(item); + if (IsStatBoostingBerry(item) && atkHpPercent > 60) score++; - else if (IsHpRestoringBerry(item) && atkHpPercent < 60) - score++; // TODO check if player can still faint us after we heal + else if (IsHpRestoringBerry(item) && !CanAIFaintTarget(battlerAtk, battlerDef, 0) + && ((GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0 && CanTargetFaintAiWithMod(battlerDef, battlerAtk, 0, 0)) + || !CanTargetFaintAiWithMod(battlerDef, battlerAtk, toheal, 0))) + score++; // Recycle healing berry if we can't otherwise faint the target and the target wont kill us after we activate the berry } break; case EFFECT_BRICK_BREAK: diff --git a/src/battle_ai_util.c b/src/battle_ai_util.c index 19dfab857..dc85901e3 100644 --- a/src/battle_ai_util.c +++ b/src/battle_ai_util.c @@ -3389,11 +3389,14 @@ bool32 IsStatBoostingBerry(u16 item) } } -bool32 IsHpRestoringBerry(u16 item) +bool32 IsHpRestoringBerry(u8 battlerAtk, u16 item) { switch (item) { case ITEM_ORAN_BERRY: + if (gBattleMons[battlerAtk].maxHp <= 50) + return TRUE; // Only worth it in the early game + return FALSE; case ITEM_SITRUS_BERRY: case ITEM_FIGY_BERRY: case ITEM_WIKI_BERRY: From e635930a150918581e2d6266335327592058450b Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Mon, 8 Nov 2021 21:33:00 -0500 Subject: [PATCH 20/46] fixes --- include/battle_ai_util.h | 2 +- src/battle_ai_main.c | 4 ++-- src/battle_ai_util.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/battle_ai_util.h b/include/battle_ai_util.h index cc626fa5e..4fe36c48d 100644 --- a/include/battle_ai_util.h +++ b/include/battle_ai_util.h @@ -52,7 +52,7 @@ 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 IsHpRestoringBerry(u16 item); +bool32 ShouldRestoreHpBerry(u8 battlerAtk, u16 item); bool32 IsStatBoostingBerry(u16 item); bool32 CanKnockOffItem(u8 battler, u16 item); bool32 IsAbilityOfRating(u16 ability, s8 rating); diff --git a/src/battle_ai_main.c b/src/battle_ai_main.c index 1dcb86805..fcd11d1c3 100644 --- a/src/battle_ai_main.c +++ b/src/battle_ai_main.c @@ -4115,9 +4115,9 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) if (IsStatBoostingBerry(item) && atkHpPercent > 60) score++; - else if (IsHpRestoringBerry(item) && !CanAIFaintTarget(battlerAtk, battlerDef, 0) + else if (ShouldRestoreHpBerry(battlerAtk, item) && !CanAIFaintTarget(battlerAtk, battlerDef, 0) && ((GetWhoStrikesFirst(battlerAtk, battlerDef, TRUE) == 0 && CanTargetFaintAiWithMod(battlerDef, battlerAtk, 0, 0)) - || !CanTargetFaintAiWithMod(battlerDef, battlerAtk, toheal, 0))) + || !CanTargetFaintAiWithMod(battlerDef, battlerAtk, toHeal, 0))) score++; // Recycle healing berry if we can't otherwise faint the target and the target wont kill us after we activate the berry } break; diff --git a/src/battle_ai_util.c b/src/battle_ai_util.c index dc85901e3..c90c9cdab 100644 --- a/src/battle_ai_util.c +++ b/src/battle_ai_util.c @@ -3389,12 +3389,12 @@ bool32 IsStatBoostingBerry(u16 item) } } -bool32 IsHpRestoringBerry(u8 battlerAtk, u16 item) +bool32 ShouldRestoreHpBerry(u8 battlerAtk, u16 item) { switch (item) { case ITEM_ORAN_BERRY: - if (gBattleMons[battlerAtk].maxHp <= 50) + if (gBattleMons[battlerAtk].maxHP <= 50) return TRUE; // Only worth it in the early game return FALSE; case ITEM_SITRUS_BERRY: From 65cb371104603a42ad484e90ecf6e2ff60b5d5f6 Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Thu, 11 Nov 2021 16:16:46 -0500 Subject: [PATCH 21/46] fix needing to 'run' twice --- src/battle_main.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/battle_main.c b/src/battle_main.c index 01cb40317..688a3e2be 100644 --- a/src/battle_main.c +++ b/src/battle_main.c @@ -3974,9 +3974,6 @@ static void HandleTurnActionSelectionState(void) return; case B_ACTION_DEBUG: BtlController_EmitDebugMenu(0); - // fallthrough - case B_ACTION_SAFARI_WATCH_CAREFULLY: - case B_ACTION_RUN: MarkBattlerForControllerExec(gActiveBattler); break; } From 3976d982ffef7d7733b206d862a6e3a2a8e06b10 Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Thu, 11 Nov 2021 16:35:10 -0500 Subject: [PATCH 22/46] fix AI_ShouldHeal speed check --- src/battle_ai_switch_items.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/battle_ai_switch_items.c b/src/battle_ai_switch_items.c index 4924cc511..45928df3c 100644 --- a/src/battle_ai_switch_items.c +++ b/src/battle_ai_switch_items.c @@ -1001,7 +1001,7 @@ static bool32 AI_ShouldHeal(u8 healAmount) } // AI_THINKING_STRUCT->movesetIndex is the array index of the AI's chosen move - if (CanIndexMoveFaintTarget(gActiveBattler, i, AI_THINKING_STRUCT->movesetIndex, 0) && WillAIStrikeFirst()) + if (CanIndexMoveFaintTarget(gActiveBattler, i, AI_THINKING_STRUCT->movesetIndex, 0) && AI_WhoStrikesFirst(gActiveBattler, i) == AI_IS_FASTER) { // We can faint the target and move first -> don't heal shouldHeal = FALSE; From bdbdd0564fe9e3aaf3d15139e6260d371c939923 Mon Sep 17 00:00:00 2001 From: BuffelSaft Date: Fri, 12 Nov 2021 21:05:42 +1300 Subject: [PATCH 23/46] Fix Knock Off, add config Added a config for Knock Off's damage boost. Also fixed the conditions it checks to do bonus damage. --- include/constants/battle_config.h | 1 + src/battle_util.c | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/include/constants/battle_config.h b/include/constants/battle_config.h index aaf02a37f..62bd7e447 100644 --- a/include/constants/battle_config.h +++ b/include/constants/battle_config.h @@ -114,6 +114,7 @@ #define B_PAYBACK_SWITCH_BOOST GEN_7 // In Gen5+, if the opponent switches out, Payback's damage will no longer be doubled. #define B_HIDDEN_POWER_DMG GEN_7 // In Gen6+, Hidden Power's base power was set to always be 60. Before, it was determined by the mon's IVs. #define B_ROUGH_SKIN_DMG GEN_7 // In Gen4+, Rough Skin contact damage is 1/8th of max HP instead of 1/16th. This will also affect Iron Barbs. +#define B_KNOCK_OFF_DMG GEN_8 // In Gen6+, Knock Off deals 50% more damage when knocking off an item // Type settings #define B_GHOSTS_ESCAPE GEN_7 // In Gen6+, abilities like Shadow Tag or moves like Mean Look fail on Ghost-type Pokémon. They can also escape any Wild Battle. diff --git a/src/battle_util.c b/src/battle_util.c index 4b3fc11aa..7f01891bd 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -8191,8 +8191,11 @@ static u32 CalcMoveBasePowerAfterModifiers(u16 move, u8 battlerAtk, u8 battlerDe MulModifier(&modifier, UQ_4_12(0.5)); break; case EFFECT_KNOCK_OFF: - if (gBattleMons[battlerDef].item != ITEM_NONE && GetBattlerAbility(battlerDef) != ABILITY_STICKY_HOLD) + #if B_KNOCK_OFF_DMG >= GEN_6 + if (gBattleMons[battlerDef].item != ITEM_NONE + && CanBattlerGetOrLoseItem(battlerDef, gBattleMons[battlerDef].item)) MulModifier(&modifier, UQ_4_12(1.5)); + #endif break; } From 276b454b4088d7ffd4f57e22fdc5982056375f11 Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Fri, 12 Nov 2021 08:13:17 -0500 Subject: [PATCH 24/46] primal weather checks --- src/battle_ai_main.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/battle_ai_main.c b/src/battle_ai_main.c index fcd11d1c3..3e51b0dc4 100644 --- a/src/battle_ai_main.c +++ b/src/battle_ai_main.c @@ -1392,22 +1392,22 @@ static s16 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) } break; case EFFECT_SANDSTORM: - if (gBattleWeather & WEATHER_SANDSTORM_ANY //TODO | WEATHER_PRIMAL_ANY) + if (gBattleWeather & (WEATHER_SANDSTORM_ANY | WEATHER_PRIMAL_ANY) || PartnerMoveEffectIsWeather(AI_DATA->battlerAtkPartner, AI_DATA->partnerMove)) score -= 8; break; case EFFECT_SUNNY_DAY: - if (gBattleWeather & WEATHER_SUN_ANY //TODO | WEATHER_PRIMAL_ANY) + if (gBattleWeather & (WEATHER_SUN_ANY | WEATHER_PRIMAL_ANY) || PartnerMoveEffectIsWeather(AI_DATA->battlerAtkPartner, AI_DATA->partnerMove)) score -= 8; break; case EFFECT_RAIN_DANCE: - if (gBattleWeather & WEATHER_RAIN_ANY //TODO | WEATHER_PRIMAL_ANY) + if (gBattleWeather & (WEATHER_RAIN_ANY | WEATHER_PRIMAL_ANY) || PartnerMoveEffectIsWeather(AI_DATA->battlerAtkPartner, AI_DATA->partnerMove)) score -= 8; break; case EFFECT_HAIL: - if (gBattleWeather & WEATHER_HAIL_ANY //TODO | WEATHER_PRIMAL_ANY) + if (gBattleWeather & (WEATHER_HAIL_ANY | WEATHER_PRIMAL_ANY) || PartnerMoveEffectIsWeather(AI_DATA->battlerAtkPartner, AI_DATA->partnerMove)) score -= 8; break; From bcbdb353f5145c64c1dd0d2d00436e97af4b0ccb Mon Sep 17 00:00:00 2001 From: Eduardo Quezada D'Ottone Date: Sat, 13 Nov 2021 12:10:20 -0300 Subject: [PATCH 25/46] Adjusted elements to simplify the conflicts between BE and IE --- include/constants/hold_effects.h | 4 ++-- src/battle_ai_main.c | 2 +- src/battle_debug.c | 16 ++++++++++++---- src/battle_script_commands.c | 8 ++++++-- src/battle_util.c | 4 ++-- src/data/items.h | 4 ++-- 6 files changed, 25 insertions(+), 13 deletions(-) diff --git a/include/constants/hold_effects.h b/include/constants/hold_effects.h index d7d74d3e1..cba2321ce 100644 --- a/include/constants/hold_effects.h +++ b/include/constants/hold_effects.h @@ -62,12 +62,12 @@ #define HOLD_EFFECT_FIRE_POWER 58 #define HOLD_EFFECT_DRAGON_POWER 59 #define HOLD_EFFECT_NORMAL_POWER 60 -#define HOLD_EFFECT_UP_GRADE 61 +#define HOLD_EFFECT_UPGRADE 61 #define HOLD_EFFECT_SHELL_BELL 62 #define HOLD_EFFECT_LUCKY_PUNCH 63 #define HOLD_EFFECT_METAL_POWDER 64 #define HOLD_EFFECT_THICK_CLUB 65 -#define HOLD_EFFECT_STICK 66 +#define HOLD_EFFECT_LEEK 66 // Gen4 hold effects. #define HOLD_EFFECT_CHOICE_SCARF 67 diff --git a/src/battle_ai_main.c b/src/battle_ai_main.c index 7fbb43260..e3b8e8d5d 100644 --- a/src/battle_ai_main.c +++ b/src/battle_ai_main.c @@ -2320,7 +2320,7 @@ static s16 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) || IsInstructBannedMove(instructedMove) || MoveRequiresRecharging(instructedMove) || MoveCallsOtherMove(instructedMove) - #ifdef ITEM_Z_RING + #ifdef ITEM_Z_POWER_RING //|| (IsZMove(instructedMove)) #endif || (gLockedMoves[battlerDef] != 0 && gLockedMoves[battlerDef] != 0xFFFF) diff --git a/src/battle_debug.c b/src/battle_debug.c index c967cfe00..a10e60412 100644 --- a/src/battle_debug.c +++ b/src/battle_debug.c @@ -1924,12 +1924,20 @@ static const u8 sText_HoldEffectPsychicPower[] = _("Psychic Power"); static const u8 sText_HoldEffectFirePower[] = _("Fire Power"); static const u8 sText_HoldEffectDragonPower[] = _("Dragon Power"); static const u8 sText_HoldEffectNormalPower[] = _("Normal Power"); -static const u8 sText_HoldEffectUpGrade[] = _("Up Grade"); +#ifdef ITEM_EXPANSION +static const u8 sText_HoldEffectUpgrade[] = _("Upgrade"); +#else +static const u8 sText_HoldEffectUpgrade[] = _("Up Grade"); +#endif static const u8 sText_HoldEffectShellBell[] = _("Shell Bell"); static const u8 sText_HoldEffectLuckyPunch[] = _("Lucky Punch"); static const u8 sText_HoldEffectMetalPowder[] = _("Metal Powder"); static const u8 sText_HoldEffectThickClub[] = _("Thick Club"); -static const u8 sText_HoldEffectStick[] = _("Stick"); +#ifdef ITEM_EXPANSION +static const u8 sText_HoldEffectLeek[] = _("Leek"); +#else +static const u8 sText_HoldEffectLeek[] = _("Stick"); +#endif static const u8 sText_HoldEffectChoiceScarf[] = _("Choice Scarf"); static const u8 sText_HoldEffectChoiceSpecs[] = _("Choice Specs"); static const u8 sText_HoldEffectDampRock[] = _("Damp Rock"); @@ -2064,12 +2072,12 @@ static const u8 *const sHoldEffectNames[] = [HOLD_EFFECT_FIRE_POWER] = sText_HoldEffectFirePower, [HOLD_EFFECT_DRAGON_POWER] = sText_HoldEffectDragonPower, [HOLD_EFFECT_NORMAL_POWER] = sText_HoldEffectNormalPower, - [HOLD_EFFECT_UP_GRADE] = sText_HoldEffectUpGrade, + [HOLD_EFFECT_UPGRADE] = sText_HoldEffectUpgrade, [HOLD_EFFECT_SHELL_BELL] = sText_HoldEffectShellBell, [HOLD_EFFECT_LUCKY_PUNCH] = sText_HoldEffectLuckyPunch, [HOLD_EFFECT_METAL_POWDER] = sText_HoldEffectMetalPowder, [HOLD_EFFECT_THICK_CLUB] = sText_HoldEffectThickClub, - [HOLD_EFFECT_STICK] = sText_HoldEffectStick, + [HOLD_EFFECT_LEEK] = sText_HoldEffectLeek, [HOLD_EFFECT_CHOICE_SCARF] = sText_HoldEffectChoiceScarf, [HOLD_EFFECT_CHOICE_SPECS] = sText_HoldEffectChoiceSpecs, [HOLD_EFFECT_DAMP_ROCK] = sText_HoldEffectDampRock, diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index eb9334c4e..a3fb8fe45 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -1893,7 +1893,7 @@ s32 CalcCritChanceStage(u8 battlerAtk, u8 battlerDef, u32 move, bool32 recordAbi + ((gBattleMoves[gCurrentMove].flags & FLAG_HIGH_CRIT) != 0) + (holdEffectAtk == HOLD_EFFECT_SCOPE_LENS) + 2 * (holdEffectAtk == HOLD_EFFECT_LUCKY_PUNCH && gBattleMons[gBattlerAttacker].species == SPECIES_CHANSEY) - + 2 * (holdEffectAtk == HOLD_EFFECT_STICK && gBattleMons[gBattlerAttacker].species == SPECIES_FARFETCHD) + + 2 * (holdEffectAtk == HOLD_EFFECT_LEEK && gBattleMons[gBattlerAttacker].species == SPECIES_FARFETCHD) + (abilityAtk == ABILITY_SUPER_LUCK); if (critChance >= ARRAY_COUNT(sCriticalHitChance)) @@ -3164,7 +3164,11 @@ void SetMoveEffect(bool32 primary, u32 certain) RecordAbilityBattle(gBattlerTarget, gLastUsedAbility); } else if (gBattleMons[gBattlerAttacker].item != 0 + #ifdef ITEM_EXPANSION + || gBattleMons[gBattlerTarget].item == ITEM_ENIGMA_BERRY_E_READER + #else || gBattleMons[gBattlerTarget].item == ITEM_ENIGMA_BERRY + #endif || gBattleMons[gBattlerTarget].item == 0) { gBattlescriptCurrInstr++; @@ -13138,7 +13142,7 @@ static void Cmd_handleballthrow(void) u8 catchRate; gLastThrownBall = gLastUsedItem; - if (gLastUsedItem == ITEM_SAFARI_BALL) + if (gBattleTypeFlags & BATTLE_TYPE_SAFARI) catchRate = gBattleStruct->safariCatchFactor * 1275 / 100; else catchRate = gBaseStats[gBattleMons[gBattlerTarget].species].catchRate; diff --git a/src/battle_util.c b/src/battle_util.c index 6ce984cf4..7f65ec55f 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -9048,9 +9048,9 @@ bool32 CanMegaEvolve(u8 battlerId) struct MegaEvolutionData *mega = &(((struct ChooseMoveStruct*)(&gBattleResources->bufferA[gActiveBattler][4]))->mega); #ifdef ITEM_EXPANSION - // Check if Player has a Mega Bracelet + // Check if Player has a Mega Ring if ((GetBattlerPosition(battlerId) == B_POSITION_PLAYER_LEFT || (!(gBattleTypeFlags & BATTLE_TYPE_MULTI) && GetBattlerPosition(battlerId) == B_POSITION_PLAYER_RIGHT)) - && !CheckBagHasItem(ITEM_MEGA_BRACELET, 1)) + && !CheckBagHasItem(ITEM_MEGA_RING, 1)) return FALSE; #endif diff --git a/src/data/items.h b/src/data/items.h index ef5c8825d..7e62558aa 100644 --- a/src/data/items.h +++ b/src/data/items.h @@ -2680,7 +2680,7 @@ const struct Item gItems[] = .name = _("UP-GRADE"), .itemId = ITEM_UP_GRADE, .price = 2100, - .holdEffect = HOLD_EFFECT_UP_GRADE, + .holdEffect = HOLD_EFFECT_UPGRADE, .description = sUpGradeDesc, .pocket = POCKET_ITEMS, .type = ITEM_USE_BAG_MENU, @@ -2767,7 +2767,7 @@ const struct Item gItems[] = .name = _("STICK"), .itemId = ITEM_STICK, .price = 200, - .holdEffect = HOLD_EFFECT_STICK, + .holdEffect = HOLD_EFFECT_LEEK, .description = sStickDesc, .pocket = POCKET_ITEMS, .type = ITEM_USE_BAG_MENU, From cedc2e07bda0199fda7e8da7465e2ac0cbe6a0a3 Mon Sep 17 00:00:00 2001 From: BuffelSaft Date: Sun, 14 Nov 2021 13:31:06 +1300 Subject: [PATCH 26/46] Fix Cherrim softlock Fix typo that caused infinite loop. --- src/battle_util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/battle_util.c b/src/battle_util.c index 6ce984cf4..2c2918a00 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -3792,7 +3792,7 @@ u8 TryWeatherFormChange(u8 battler) ret = 0; else if (gBattleMonForms[battler] == 0 && weatherEffect && holdEffect != HOLD_EFFECT_UTILITY_UMBRELLA && gBattleWeather & WEATHER_SUN_ANY) ret = 2; - else if (gBattleMonForms[battler] != 0 && (!weatherEffect || holdEffect != HOLD_EFFECT_UTILITY_UMBRELLA || !(gBattleWeather & WEATHER_SUN_ANY))) + else if (gBattleMonForms[battler] != 0 && (!weatherEffect || holdEffect == HOLD_EFFECT_UTILITY_UMBRELLA || !(gBattleWeather & WEATHER_SUN_ANY))) ret = 1; } From d4fa346540f4f955dc45648d6fc20269cac5fa2a Mon Sep 17 00:00:00 2001 From: BuffelSaft Date: Mon, 15 Nov 2021 21:40:20 +1300 Subject: [PATCH 27/46] Fix Dark Aura and Fairy Aura Damage boost should be 33%, not 25%. --- src/battle_util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/battle_util.c b/src/battle_util.c index 7d0f6cebf..d57d25e0e 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -8131,7 +8131,7 @@ static u32 CalcMoveBasePowerAfterModifiers(u16 move, u8 battlerAtk, u8 battlerDe if (IsAbilityOnField(ABILITY_AURA_BREAK)) MulModifier(&modifier, UQ_4_12(0.75)); else - MulModifier(&modifier, UQ_4_12(1.25)); + MulModifier(&modifier, UQ_4_12(1.33)); } // attacker partner's abilities From 9fdd6fffee4d06d56f610ff3265adcb4571dbc3c Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Mon, 15 Nov 2021 10:45:23 -0500 Subject: [PATCH 28/46] AI_GetAbility supports hidden abilities --- src/battle_ai_util.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/battle_ai_util.c b/src/battle_ai_util.c index c90c9cdab..499969ec3 100644 --- a/src/battle_ai_util.c +++ b/src/battle_ai_util.c @@ -1133,18 +1133,17 @@ s32 AI_GetAbility(u32 battlerId) || gBattleMons[battlerId].ability == ABILITY_MAGNET_PULL || gBattleMons[battlerId].ability == ABILITY_ARENA_TRAP) return gBattleMons[battlerId].ability; - + + // AI has no knowledge of opponent, so it guesses which ability. if (gBaseStats[gBattleMons[battlerId].species].abilities[0] != ABILITY_NONE) { - if (gBaseStats[gBattleMons[battlerId].species].abilities[1] != ABILITY_NONE) + u16 abilityGuess = ABILITY_NONE; + while (abilityGuess == ABILITY_NONE) { - // AI has no knowledge of opponent, so it guesses which ability. - return gBaseStats[gBattleMons[battlerId].species].abilities[Random() & 1]; - } - else - { - return gBaseStats[gBattleMons[battlerId].species].abilities[0]; // It's definitely ability 1. + abilityGuess = gBaseStats[gBattleMons[battlerId].species].abilities[Random() % NUM_ABILITY_SLOTS]; } + + return abilityGuess; } return ABILITY_NONE; // Unknown. } From ea34d4694d7c9bf50ac7b1cef8b4ccfdebc15c85 Mon Sep 17 00:00:00 2001 From: kleeenexfeu <94004034+kleeenexfeu@users.noreply.github.com> Date: Mon, 15 Nov 2021 16:48:44 +0100 Subject: [PATCH 29/46] PartyBattlerShouldAvoidHazards fix AI was loading and modifying wrong data in the RAM --- src/battle_ai_util.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/battle_ai_util.c b/src/battle_ai_util.c index 71dc78d0a..d46a43f6b 100644 --- a/src/battle_ai_util.c +++ b/src/battle_ai_util.c @@ -2408,9 +2408,19 @@ static bool32 AnyUsefulStatIsRaised(u8 battler) return FALSE; } +struct Pokemon *GetPartyBattlerPartyData(u8 battlerId, u8 switchBattler) +{ + struct Pokemon *mon; + if (GetBattlerSide(battlerId) == B_SIDE_PLAYER) + mon = &gPlayerParty[switchBattler]; + else + mon = &gEnemyParty[switchBattler]; + return mon; +} + static bool32 PartyBattlerShouldAvoidHazards(u8 currBattler, u8 switchBattler) { - struct Pokemon *mon = GetBattlerPartyData(switchBattler); + struct Pokemon *mon = GetPartyBattlerPartyData(currBattler, 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); From a1bd6bd52fd9021c12d07624882afa483004948d Mon Sep 17 00:00:00 2001 From: Eduardo Quezada D'Ottone Date: Mon, 15 Nov 2021 22:30:20 -0300 Subject: [PATCH 30/46] Updated species that benefit from Leek --- include/constants/battle_config.h | 1 + src/battle_script_commands.c | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/include/constants/battle_config.h b/include/constants/battle_config.h index 6f4e283ca..7f4facf02 100644 --- a/include/constants/battle_config.h +++ b/include/constants/battle_config.h @@ -51,6 +51,7 @@ #define SPECIES_MELOETTA_PIROUETTE 10019 #define SPECIES_MORPEKO 0 #define SPECIES_MORPEKO_HANGRY 10020 + #define SPECIES_SIRFETCHD 10021 #endif // Items with peculiar battle effects. diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 4a5b09df3..fe7d3ca30 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -1893,7 +1893,9 @@ s32 CalcCritChanceStage(u8 battlerAtk, u8 battlerDef, u32 move, bool32 recordAbi + ((gBattleMoves[gCurrentMove].flags & FLAG_HIGH_CRIT) != 0) + (holdEffectAtk == HOLD_EFFECT_SCOPE_LENS) + 2 * (holdEffectAtk == HOLD_EFFECT_LUCKY_PUNCH && gBattleMons[gBattlerAttacker].species == SPECIES_CHANSEY) - + 2 * (holdEffectAtk == HOLD_EFFECT_LEEK && gBattleMons[gBattlerAttacker].species == SPECIES_FARFETCHD) + + 2 * (holdEffectAtk == HOLD_EFFECT_LEEK + && (GET_BASE_SPECIES_ID(gBattleMons[gBattlerAttacker].species) == SPECIES_FARFETCHD + || gBattleMons[gBattlerAttacker].species == SPECIES_SIRFETCHD)) + (abilityAtk == ABILITY_SUPER_LUCK); if (critChance >= ARRAY_COUNT(sCriticalHitChance)) From cc833ebb5979730389ac6897636b63cd9d1ef652 Mon Sep 17 00:00:00 2001 From: Eduardo Quezada D'Ottone Date: Mon, 15 Nov 2021 23:21:46 -0300 Subject: [PATCH 31/46] Created macro + config for always critting when using high-crit moves --- include/constants/battle_config.h | 1 + src/battle_script_commands.c | 15 +++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/include/constants/battle_config.h b/include/constants/battle_config.h index 7f4facf02..5e6c7d729 100644 --- a/include/constants/battle_config.h +++ b/include/constants/battle_config.h @@ -186,6 +186,7 @@ #define B_HEAVY_BALL_MODIFIER GEN_7 // In Gen7+, Heavy Ball's ranges change. See Cmd_handleballthrow. #define B_DREAM_BALL_MODIFIER GEN_8 // In Gen8, Dream Ball's catch multiplier is x4 when the target is asleep or has the ability Comatose. #define B_SERENE_GRACE_BOOST GEN_7 // In Gen5+, Serene Grace boosts the added flinch chance of King's Rock and Razor Fang. +#define B_LEEK_ALWAYS_CRIT GEN_7 // In Gen6+, if a Farfetch'd or Sirfetch'd holding a Leek use a move with increased Critical Hit ratio, it will always result in a Critical Hit. // Flag settings // To use the following features in scripting, replace the 0s with the flag ID you're assigning it to. diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index fe7d3ca30..99441e142 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -1861,11 +1861,13 @@ static void Cmd_ppreduce(void) static const u8 sCriticalHitChance[] = {16, 8, 4, 3, 2}; // Gens 2,3,4,5 #endif // B_CRIT_CHANCE +#define BENEFITS_FROM_LEEK(battler, holdEffect)((holdEffect == HOLD_EFFECT_LEEK) && (GET_BASE_SPECIES_ID(gBattleMons[battler].species) == SPECIES_FARFETCHD || gBattleMons[battler].species == SPECIES_SIRFETCHD)) s32 CalcCritChanceStage(u8 battlerAtk, u8 battlerDef, u32 move, bool32 recordAbility) { s32 critChance = 0; u32 abilityAtk = GetBattlerAbility(gBattlerAttacker); u32 abilityDef = GetBattlerAbility(gBattlerTarget); + u32 holdEffectAtk = GetBattlerHoldEffect(battlerAtk, TRUE); if (gSideStatuses[battlerDef] & SIDE_STATUS_LUCKY_CHANT || gStatuses3[gBattlerAttacker] & STATUS3_CANT_SCORE_A_CRIT) @@ -1881,21 +1883,21 @@ s32 CalcCritChanceStage(u8 battlerAtk, u8 battlerDef, u32 move, bool32 recordAbi else if (gStatuses3[battlerAtk] & STATUS3_LASER_FOCUS || gBattleMoves[move].effect == EFFECT_ALWAYS_CRIT || (abilityAtk == ABILITY_MERCILESS && gBattleMons[battlerDef].status1 & STATUS1_PSN_ANY) - || move == MOVE_SURGING_STRIKES) + || move == MOVE_SURGING_STRIKES + #if B_LEEK_ALWAYS_CRIT >= GEN_6 + || ((gBattleMoves[gCurrentMove].flags & FLAG_HIGH_CRIT) && BENEFITS_FROM_LEEK(battlerAtk, holdEffectAtk)) + #endif + ) { critChance = -2; } else { - u32 holdEffectAtk = GetBattlerHoldEffect(battlerAtk, TRUE); - critChance = 2 * ((gBattleMons[gBattlerAttacker].status2 & STATUS2_FOCUS_ENERGY) != 0) + ((gBattleMoves[gCurrentMove].flags & FLAG_HIGH_CRIT) != 0) + (holdEffectAtk == HOLD_EFFECT_SCOPE_LENS) + 2 * (holdEffectAtk == HOLD_EFFECT_LUCKY_PUNCH && gBattleMons[gBattlerAttacker].species == SPECIES_CHANSEY) - + 2 * (holdEffectAtk == HOLD_EFFECT_LEEK - && (GET_BASE_SPECIES_ID(gBattleMons[gBattlerAttacker].species) == SPECIES_FARFETCHD - || gBattleMons[gBattlerAttacker].species == SPECIES_SIRFETCHD)) + + 2 * BENEFITS_FROM_LEEK(battlerAtk, holdEffectAtk) + (abilityAtk == ABILITY_SUPER_LUCK); if (critChance >= ARRAY_COUNT(sCriticalHitChance)) @@ -1904,6 +1906,7 @@ s32 CalcCritChanceStage(u8 battlerAtk, u8 battlerDef, u32 move, bool32 recordAbi return critChance; } +#undef BenefitsFromLeek s8 GetInverseCritChance(u8 battlerAtk, u8 battlerDef, u32 move) { From 0af63c04ebfac6730cfb9c972621258d70ad8212 Mon Sep 17 00:00:00 2001 From: Eduardo Quezada D'Ottone Date: Mon, 15 Nov 2021 23:29:54 -0300 Subject: [PATCH 32/46] Removed redundant uses of STATUS3_GASTRO_ACID check. --- src/battle_script_commands.c | 10 +++++----- src/battle_util.c | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 99441e142..2f290c3ab 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -8971,11 +8971,11 @@ static void Cmd_various(void) for (i = 0; i < gBattlersCount; i++) { - if (((GetBattlerAbility(i) == ABILITY_DESOLATE_LAND && gBattleWeather & WEATHER_SUN_PRIMAL) - || (GetBattlerAbility(i) == ABILITY_PRIMORDIAL_SEA && gBattleWeather & WEATHER_RAIN_PRIMAL) - || (GetBattlerAbility(i) == ABILITY_DELTA_STREAM && gBattleWeather & WEATHER_STRONG_WINDS)) - && IsBattlerAlive(i) - && !(gStatuses3[i] & STATUS3_GASTRO_ACID)) + u32 ability = GetBattlerAbility(i); + if (((ability == ABILITY_DESOLATE_LAND && gBattleWeather & WEATHER_SUN_PRIMAL) + || (ability == ABILITY_PRIMORDIAL_SEA && gBattleWeather & WEATHER_RAIN_PRIMAL) + || (ability == ABILITY_DELTA_STREAM && gBattleWeather & WEATHER_STRONG_WINDS)) + && IsBattlerAlive(i)) shouldNotClear = TRUE; } if (gBattleWeather & WEATHER_SUN_PRIMAL && !shouldNotClear) diff --git a/src/battle_util.c b/src/battle_util.c index 1e29695e2..6eb95d848 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -7462,7 +7462,7 @@ u32 GetBattlerHoldEffect(u8 battlerId, bool32 checkNegating) return HOLD_EFFECT_NONE; if (gFieldStatuses & STATUS_FIELD_MAGIC_ROOM) return HOLD_EFFECT_NONE; - if (GetBattlerAbility(battlerId) == ABILITY_KLUTZ && !(gStatuses3[battlerId] & STATUS3_GASTRO_ACID)) + if (GetBattlerAbility(battlerId) == ABILITY_KLUTZ) return HOLD_EFFECT_NONE; } From 7dbc94deefc0945597d81f249425876a40d277c3 Mon Sep 17 00:00:00 2001 From: Eduardo Quezada D'Ottone Date: Mon, 15 Nov 2021 23:36:52 -0300 Subject: [PATCH 33/46] Fixed undef --- src/battle_script_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 2f290c3ab..7923a6739 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -1906,7 +1906,7 @@ s32 CalcCritChanceStage(u8 battlerAtk, u8 battlerDef, u32 move, bool32 recordAbi return critChance; } -#undef BenefitsFromLeek +#undef BENEFITS_FROM_LEEK s8 GetInverseCritChance(u8 battlerAtk, u8 battlerDef, u32 move) { From c4f27174c3f138b591f582399193b80c6d1b8d33 Mon Sep 17 00:00:00 2001 From: BuffelSaft Date: Fri, 19 Nov 2021 16:21:33 +1300 Subject: [PATCH 34/46] Implement Unseen Fist Unseen Fist allows contact moves to bypass protect effects, but doesn't lift the effect. --- src/battle_util.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/battle_util.c b/src/battle_util.c index 1e29695e2..0b25f7227 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -7517,8 +7517,13 @@ bool32 IsBattlerProtected(u8 battlerId, u16 move) else if (gProtectStructs[battlerId].protected) return FALSE; } - - if (!(gBattleMoves[move].flags & FLAG_PROTECT_AFFECTED)) + + // Protective Pads doesn't stop Unseen Fist from bypassing Protect effects, so IsMoveMakingContact() isn't used here. + // This means extra logic is needed to handle Shell Side Arm. + if (GetBattlerAbility(gBattlerAttacker == ABILITY_UNSEEN_FIST) + && (gBattleMoves[move].flags & FLAG_MAKES_CONTACT || (gBattleMoves[move].effect == EFFECT_SHELL_SIDE_ARM && gSwapDamageCategory))) + return FALSE; + else if (!(gBattleMoves[move].flags & FLAG_PROTECT_AFFECTED)) return FALSE; else if (gBattleMoves[move].effect == MOVE_EFFECT_FEINT) return FALSE; From 03a9f3f41338eedd9e6555d8aeb4788aba2f5463 Mon Sep 17 00:00:00 2001 From: kleeenexfeu Date: Fri, 19 Nov 2021 22:03:10 +0100 Subject: [PATCH 35/46] Mega Evolution alters turn order --- include/constants/battle_config.h | 5 +++++ src/battle_main.c | 32 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/include/constants/battle_config.h b/include/constants/battle_config.h index 5e6c7d729..cc331c316 100644 --- a/include/constants/battle_config.h +++ b/include/constants/battle_config.h @@ -92,6 +92,11 @@ #define GEN_8 5 #endif + +// Mega Evolution settings +#define B_MEGA_EVO_ALTER_TURN_ORDER GEN_7 // In gen6, mega evolving doesn't change the turn order when mega evolving occurs. This is fixed in gen 7 + + // Calculation settings #define B_CRIT_CHANCE GEN_7 // Chances of a critical hit landing. See CalcCritChanceStage. #define B_CRIT_MULTIPLIER GEN_7 // In Gen6+, critical hits multiply damage by 1.5 instead of 2. diff --git a/src/battle_main.c b/src/battle_main.c index 5277ade1e..f0a2ee045 100644 --- a/src/battle_main.c +++ b/src/battle_main.c @@ -104,6 +104,7 @@ static void RunTurnActionsFunctions(void); static void SetActionsAndBattlersTurnOrder(void); static void sub_803CDF8(void); static bool8 AllAtActionConfirmed(void); +static void TryChangeTurnOrder(void); static void CheckFocusPunch_ClearVarsBeforeTurnStarts(void); static void CheckMegaEvolutionBeforeTurn(void); static void CheckQuickClaw_CustapBerryActivation(void); @@ -4688,10 +4689,41 @@ static void CheckMegaEvolutionBeforeTurn(void) } } + #if B_MEGA_EVO_ALTER_TURN_ORDER <= GEN_6 + gBattleMainFunc = CheckFocusPunch_ClearVarsBeforeTurnStarts; + gBattleStruct->focusPunchBattlerId = 0; + #else + gBattleMainFunc = TryChangeTurnOrder; // This will just do nothing if no mon has mega evolved + #endif +} + +// In gen7, priority and speed are recalculated during the turn in which a pokemon mega evolves +static void TryChangeTurnOrder(void) +{ + s32 i, j; + for (i = 0; i < gBattlersCount - 1; i++) + { + for (j = i + 1; j < gBattlersCount; j++) + { + u8 battler1 = gBattlerByTurnOrder[i]; + u8 battler2 = gBattlerByTurnOrder[j]; + if (gActionsByTurnOrder[i] != B_ACTION_USE_ITEM + && gActionsByTurnOrder[j] != B_ACTION_USE_ITEM + && gActionsByTurnOrder[i] != B_ACTION_SWITCH + && gActionsByTurnOrder[j] != B_ACTION_SWITCH + && gActionsByTurnOrder[i] != B_ACTION_THROW_BALL + && gActionsByTurnOrder[j] != B_ACTION_THROW_BALL) + { + if (GetWhoStrikesFirst(battler1, battler2, FALSE)) + SwapTurnOrder(i, j); + } + } + } gBattleMainFunc = CheckFocusPunch_ClearVarsBeforeTurnStarts; gBattleStruct->focusPunchBattlerId = 0; } + static void CheckFocusPunch_ClearVarsBeforeTurnStarts(void) { u32 i; From fa4d71817b975e1ae6514ab4582114a728d3ce94 Mon Sep 17 00:00:00 2001 From: kleeenexfeu <94004034+kleeenexfeu@users.noreply.github.com> Date: Fri, 19 Nov 2021 22:30:30 +0100 Subject: [PATCH 36/46] Spaces instead of tab --- src/battle_main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/battle_main.c b/src/battle_main.c index f0a2ee045..3c000989b 100644 --- a/src/battle_main.c +++ b/src/battle_main.c @@ -4717,8 +4717,8 @@ static void TryChangeTurnOrder(void) if (GetWhoStrikesFirst(battler1, battler2, FALSE)) SwapTurnOrder(i, j); } - } - } + } + } gBattleMainFunc = CheckFocusPunch_ClearVarsBeforeTurnStarts; gBattleStruct->focusPunchBattlerId = 0; } From fb468b67762fd08f9021152bf7fa70216ab4d771 Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Fri, 19 Nov 2021 16:32:00 -0500 Subject: [PATCH 37/46] fix unseen fist check --- src/battle_util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/battle_util.c b/src/battle_util.c index c37d64adc..cc619decf 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -7520,7 +7520,7 @@ bool32 IsBattlerProtected(u8 battlerId, u16 move) // Protective Pads doesn't stop Unseen Fist from bypassing Protect effects, so IsMoveMakingContact() isn't used here. // This means extra logic is needed to handle Shell Side Arm. - if (GetBattlerAbility(gBattlerAttacker == ABILITY_UNSEEN_FIST) + if (GetBattlerAbility(gBattlerAttacker) == ABILITY_UNSEEN_FIST && (gBattleMoves[move].flags & FLAG_MAKES_CONTACT || (gBattleMoves[move].effect == EFFECT_SHELL_SIDE_ARM && gSwapDamageCategory))) return FALSE; else if (!(gBattleMoves[move].flags & FLAG_PROTECT_AFFECTED)) From acddeeb1f92cd9e211710d28f1eef72885ff49fc Mon Sep 17 00:00:00 2001 From: kleeenexfeu <94004034+kleeenexfeu@users.noreply.github.com> Date: Fri, 19 Nov 2021 23:30:33 +0100 Subject: [PATCH 38/46] follow suggestion Co-authored-by: LOuroboros --- include/constants/battle_config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/constants/battle_config.h b/include/constants/battle_config.h index cc331c316..13a59060a 100644 --- a/include/constants/battle_config.h +++ b/include/constants/battle_config.h @@ -94,7 +94,7 @@ // Mega Evolution settings -#define B_MEGA_EVO_ALTER_TURN_ORDER GEN_7 // In gen6, mega evolving doesn't change the turn order when mega evolving occurs. This is fixed in gen 7 +#define B_MEGA_EVO_TURN_ORDER GEN_7 // In Gen7, a Pokémon's Speed after Mega Evolution is used to determine turn order, not its Speed before. // Calculation settings From c1743db19aa353aab5448ca3f8d4e40060b0db01 Mon Sep 17 00:00:00 2001 From: kleeenexfeu <94004034+kleeenexfeu@users.noreply.github.com> Date: Fri, 19 Nov 2021 23:30:45 +0100 Subject: [PATCH 39/46] follow suggestion Co-authored-by: LOuroboros --- src/battle_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/battle_main.c b/src/battle_main.c index 3c000989b..3737fcc54 100644 --- a/src/battle_main.c +++ b/src/battle_main.c @@ -4689,7 +4689,7 @@ static void CheckMegaEvolutionBeforeTurn(void) } } - #if B_MEGA_EVO_ALTER_TURN_ORDER <= GEN_6 + #if B_MEGA_EVO_TURN_ORDER <= GEN_6 gBattleMainFunc = CheckFocusPunch_ClearVarsBeforeTurnStarts; gBattleStruct->focusPunchBattlerId = 0; #else From 6402a7ab6e0b01e319ba2753563475596310effc Mon Sep 17 00:00:00 2001 From: kleeenexfeu <94004034+kleeenexfeu@users.noreply.github.com> Date: Fri, 19 Nov 2021 23:42:20 +0100 Subject: [PATCH 40/46] follow suggestion Co-authored-by: LOuroboros --- include/constants/battle_config.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/include/constants/battle_config.h b/include/constants/battle_config.h index 13a59060a..4ca3ae4dd 100644 --- a/include/constants/battle_config.h +++ b/include/constants/battle_config.h @@ -92,11 +92,9 @@ #define GEN_8 5 #endif - // Mega Evolution settings #define B_MEGA_EVO_TURN_ORDER GEN_7 // In Gen7, a Pokémon's Speed after Mega Evolution is used to determine turn order, not its Speed before. - // Calculation settings #define B_CRIT_CHANCE GEN_7 // Chances of a critical hit landing. See CalcCritChanceStage. #define B_CRIT_MULTIPLIER GEN_7 // In Gen6+, critical hits multiply damage by 1.5 instead of 2. From da20dae9c35f77616189c6cada31ab7d1e1166b9 Mon Sep 17 00:00:00 2001 From: kleeenexfeu <94004034+kleeenexfeu@users.noreply.github.com> Date: Sat, 20 Nov 2021 02:09:33 +0100 Subject: [PATCH 41/46] Only recalculate speed for battler choosing a move Other actions have priority --- src/battle_main.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/battle_main.c b/src/battle_main.c index 3737fcc54..6caff36f5 100644 --- a/src/battle_main.c +++ b/src/battle_main.c @@ -4707,12 +4707,8 @@ static void TryChangeTurnOrder(void) { u8 battler1 = gBattlerByTurnOrder[i]; u8 battler2 = gBattlerByTurnOrder[j]; - if (gActionsByTurnOrder[i] != B_ACTION_USE_ITEM - && gActionsByTurnOrder[j] != B_ACTION_USE_ITEM - && gActionsByTurnOrder[i] != B_ACTION_SWITCH - && gActionsByTurnOrder[j] != B_ACTION_SWITCH - && gActionsByTurnOrder[i] != B_ACTION_THROW_BALL - && gActionsByTurnOrder[j] != B_ACTION_THROW_BALL) + if (gActionsByTurnOrder[i] = B_ACTION_USE_MOVE + && gActionsByTurnOrder[j] = B_ACTION_USE_MOVE) { if (GetWhoStrikesFirst(battler1, battler2, FALSE)) SwapTurnOrder(i, j); From 62b31aec55cf20f6b972e5e633a3dec7d92efdfe Mon Sep 17 00:00:00 2001 From: kleeenexfeu <94004034+kleeenexfeu@users.noreply.github.com> Date: Sat, 20 Nov 2021 02:12:43 +0100 Subject: [PATCH 42/46] syntax --- src/battle_main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/battle_main.c b/src/battle_main.c index 6caff36f5..0c7f023bb 100644 --- a/src/battle_main.c +++ b/src/battle_main.c @@ -4707,8 +4707,8 @@ static void TryChangeTurnOrder(void) { u8 battler1 = gBattlerByTurnOrder[i]; u8 battler2 = gBattlerByTurnOrder[j]; - if (gActionsByTurnOrder[i] = B_ACTION_USE_MOVE - && gActionsByTurnOrder[j] = B_ACTION_USE_MOVE) + if (gActionsByTurnOrder[i] == B_ACTION_USE_MOVE + && gActionsByTurnOrder[j] == B_ACTION_USE_MOVE) { if (GetWhoStrikesFirst(battler1, battler2, FALSE)) SwapTurnOrder(i, j); From 37ab20e29ca52fc6379bb54aa70da88e444e7445 Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Sat, 20 Nov 2021 16:49:04 -0500 Subject: [PATCH 43/46] fix wish --- include/battle.h | 2 +- src/battle_script_commands.c | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/include/battle.h b/include/battle.h index b587e482d..5658f603c 100644 --- a/include/battle.h +++ b/include/battle.h @@ -239,7 +239,7 @@ struct WishFutureKnock u8 futureSightAttacker[MAX_BATTLERS_COUNT]; u16 futureSightMove[MAX_BATTLERS_COUNT]; u8 wishCounter[MAX_BATTLERS_COUNT]; - u8 wishMonId[MAX_BATTLERS_COUNT]; + u8 wishPartyId[MAX_BATTLERS_COUNT]; u8 weatherDuration; u8 knockedOffMons[2]; // Each battler is represented by a bit. The array entry is dependent on the battler's side. }; diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 7923a6739..b3e1d0714 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -12327,7 +12327,7 @@ static void Cmd_trywish(void) if (gWishFutureKnock.wishCounter[gBattlerAttacker] == 0) { gWishFutureKnock.wishCounter[gBattlerAttacker] = 2; - gWishFutureKnock.wishMonId[gBattlerAttacker] = gBattlerPartyIndexes[gBattlerAttacker]; + gWishFutureKnock.wishPartyId[gBattlerAttacker] = gBattlerPartyIndexes[gBattlerAttacker]; gBattlescriptCurrInstr += 6; } else @@ -12336,9 +12336,12 @@ static void Cmd_trywish(void) } break; case 1: // heal effect - PREPARE_MON_NICK_WITH_PREFIX_BUFFER(gBattleTextBuff1, gBattlerTarget, gWishFutureKnock.wishMonId[gBattlerTarget]) + PREPARE_MON_NICK_WITH_PREFIX_BUFFER(gBattleTextBuff1, gBattlerTarget, gWishFutureKnock.wishPartyId[gBattlerTarget]) #if B_WISH_HP_SOURCE >= GEN_5 - gBattleMoveDamage = max(1, gBattleMons[gWishFutureKnock.wishMonId[gBattlerTarget]].maxHP / 2); + if (GetBattlerSide(gBattlerTarget) == B_SIDE_PLAYER) + gBattleMoveDamage = max(1, GetMonData(&gPlayerParty[gWishFutureKnock.wishPartyId[gBattlerTarget]], MON_DATA_MAX_HP) / 2); + else + gBattleMoveDamage = max(1, GetMonData(&gEnemyParty[gWishFutureKnock.wishPartyId[gBattlerTarget]], MON_DATA_MAX_HP) / 2); #else gBattleMoveDamage = max(1, gBattleMons[gBattlerTarget].maxHP / 2); #endif From 3fb62f74c6f029123f8e9d4a39d0459fd4c42aff Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Sat, 20 Nov 2021 18:35:56 -0500 Subject: [PATCH 44/46] fix rampage cancelling --- include/constants/battle_config.h | 1 + src/battle_script_commands.c | 8 ++++++++ src/battle_util.c | 1 + 3 files changed, 10 insertions(+) diff --git a/include/constants/battle_config.h b/include/constants/battle_config.h index 5e6c7d729..0831b93a3 100644 --- a/include/constants/battle_config.h +++ b/include/constants/battle_config.h @@ -156,6 +156,7 @@ #define B_SKILL_SWAP GEN_7 // In Gen4+, Skill Swap triggers switch-in abilities after use. #define B_BRICK_BREAK GEN_7 // In Gen4+, you can destroy your own side's screens. In Gen 5+, screens are not removed if the target is immune. #define B_WISH_HP_SOURCE GEN_7 // In Gen5+, Wish heals half of the user's max HP instead of the target's. +#define B_RAMPAGE_CANCELLING GEN_7 // In Gen5+, a failed Thrash, etc, will cancel except on its last turn. // Ability settings #define B_ABILITY_WEATHER GEN_7 // In Gen6+, ability-induced weather lasts 5 turns. Before, it lasted until the battle ended or until it was changed by a move or a different weather-affecting ability. diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 7923a6739..0f2fdfe84 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -5466,6 +5466,14 @@ static void Cmd_moveend(void) *(gBattleStruct->moveTarget + gBattlerAttacker) = gSpecialStatuses[gBattlerAttacker].instructedChosenTarget & 0x3; if (gSpecialStatuses[gBattlerAttacker].dancerOriginalTarget) *(gBattleStruct->moveTarget + gBattlerAttacker) = gSpecialStatuses[gBattlerAttacker].dancerOriginalTarget & 0x3; + + #if B_RAMPAGE_CANCELLING >= GEN_5 + if (gBattleMoves[gCurrentMove].effect == EFFECT_RAMPAGE // If we're rampaging + && (gMoveResultFlags & MOVE_RESULT_NO_EFFECT) // And it is unusable + && (gBattleMons[gBattlerAttacker].status2 & STATUS2_LOCK_CONFUSE) != STATUS2_LOCK_CONFUSE_TURN(1)) // And won't end this turn + CancelMultiTurnMoves(gBattlerAttacker); // Cancel it + #endif + gProtectStructs[gBattlerAttacker].usesBouncedMove = FALSE; gProtectStructs[gBattlerAttacker].targetAffected = FALSE; gBattleStruct->ateBoost[gBattlerAttacker] = 0; diff --git a/src/battle_util.c b/src/battle_util.c index cc619decf..7e2320500 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -2795,6 +2795,7 @@ u8 DoBattlerEndTurnEffects(void) case ENDTURN_FLINCH: // reset flinch gBattleMons[gActiveBattler].status2 &= ~(STATUS2_FLINCHED); gBattleStruct->turnEffectsTracker++; + break; case ENDTURN_DISABLE: // disable if (gDisableStructs[gActiveBattler].disableTimer != 0) { From 8118f3bb44e7c21c32c021bf28132042289bddf1 Mon Sep 17 00:00:00 2001 From: BuffelSaft Date: Mon, 22 Nov 2021 16:17:10 +1300 Subject: [PATCH 45/46] Fix Brick Break Should remove the target's screens, not the attacker's. --- src/battle_script_commands.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index a236bf880..4ae42cb48 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -13120,7 +13120,8 @@ static void Cmd_removelightscreenreflect(void) // brick break bool32 failed; #if B_BRICK_BREAK >= GEN_4 - side = GetBattlerSide(gBattlerAttacker); + // From Gen 4 onwards, Brick Break can remove screens on the user's side if used on an ally + side = GetBattlerSide(gBattlerTarget); #else side = GetBattlerSide(gBattlerAttacker) ^ BIT_SIDE; #endif From d108f77aa4f687ca949febc2aab0df4325edc3bd Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Sun, 28 Nov 2021 13:12:56 -0500 Subject: [PATCH 46/46] revert ai item usage code --- include/battle_ai_main.h | 7 +- include/battle_ai_switch_items.h | 2 +- include/battle_main.h | 3 - src/battle_ai_main.c | 46 ++++- src/battle_ai_switch_items.c | 248 +++++++++---------------- src/battle_controller_opponent.c | 136 +++++++------- src/battle_controller_player_partner.c | 52 +++--- src/battle_main.c | 38 ++-- 8 files changed, 243 insertions(+), 289 deletions(-) diff --git a/include/battle_ai_main.h b/include/battle_ai_main.h index c98cedbd9..66c614f78 100644 --- a/include/battle_ai_main.h +++ b/include/battle_ai_main.h @@ -3,10 +3,9 @@ // return values for BattleAI_ChooseMoveOrAction // 0 - 3 are move idx -#define AI_CHOICE_FLEE 4 -#define AI_CHOICE_WATCH 5 -#define AI_CHOICE_SWITCH 7 -#define AI_CHOICE_USE_ITEM 8 +#define AI_CHOICE_FLEE 4 +#define AI_CHOICE_WATCH 5 +#define AI_CHOICE_SWITCH 7 #define RETURN_SCORE_PLUS(val) \ { \ diff --git a/include/battle_ai_switch_items.h b/include/battle_ai_switch_items.h index 6a8042cc9..70dc41b34 100644 --- a/include/battle_ai_switch_items.h +++ b/include/battle_ai_switch_items.h @@ -32,7 +32,7 @@ enum { }; void GetAIPartyIndexes(u32 battlerId, s32 *firstId, s32 *lastId); -u8 AI_TrySwitchOrUseItem(u8 currAction); +void AI_TrySwitchOrUseItem(void); u8 GetMostSuitableMonToSwitchInto(void); bool32 ShouldSwitch(void); diff --git a/include/battle_main.h b/include/battle_main.h index 184786ea5..bf95ca156 100644 --- a/include/battle_main.h +++ b/include/battle_main.h @@ -21,8 +21,6 @@ struct UnknownPokemonStruct4 /*0x1D*/ u8 language; }; -struct ChooseMoveStruct; - #define TYPE_NAME_LENGTH 6 #define ABILITY_NAME_LENGTH 12 @@ -72,7 +70,6 @@ void RunBattleScriptCommands(void); bool8 TryRunFromBattle(u8 battlerId); void SpecialStatusesClear(void); void SetTypeBeforeUsingMove(u16 move, u8 battlerAtk); -void FillChooseMoveStruct(struct ChooseMoveStruct *moveInfo); extern struct UnknownPokemonStruct4 gMultiPartnerParty[MULTI_PARTY_SIZE]; diff --git a/src/battle_ai_main.c b/src/battle_ai_main.c index 86e5f4049..25250c480 100644 --- a/src/battle_ai_main.c +++ b/src/battle_ai_main.c @@ -209,10 +209,6 @@ u8 BattleAI_ChooseMoveOrAction(void) ret = ChooseMoveOrAction_Singles(); else ret = ChooseMoveOrAction_Doubles(); - - AI_THINKING_STRUCT->movesetIndex = ret; - AI_THINKING_STRUCT->moveConsidered = gBattleMons[sBattler_AI].moves[AI_THINKING_STRUCT->movesetIndex]; - ret = AI_TrySwitchOrUseItem(ret); // Clear protect structures, some flags may be set during AI calcs // e.g. pranksterElevated from GetMovePriority @@ -282,6 +278,48 @@ static u8 ChooseMoveOrAction_Singles(void) return AI_CHOICE_WATCH; gActiveBattler = sBattler_AI; + + // If can switch. + if (CountUsablePartyMons(sBattler_AI) > 0 + && !IsAbilityPreventingEscape(sBattler_AI) + && !(gBattleMons[gActiveBattler].status2 & (STATUS2_WRAPPED | STATUS2_ESCAPE_PREVENTION)) + && !(gStatuses3[gActiveBattler] & STATUS3_ROOTED) + && !(gBattleTypeFlags & (BATTLE_TYPE_ARENA | BATTLE_TYPE_PALACE)) + && AI_THINKING_STRUCT->aiFlags & (AI_FLAG_CHECK_VIABILITY | AI_FLAG_CHECK_BAD_MOVE | AI_FLAG_TRY_TO_FAINT | AI_FLAG_PREFER_BATON_PASS)) + { + // Consider switching if all moves are worthless to use. + if (GetTotalBaseStat(gBattleMons[sBattler_AI].species) >= 310 // Mon is not weak. + && gBattleMons[sBattler_AI].hp >= gBattleMons[sBattler_AI].maxHP / 2) + { + s32 cap = AI_THINKING_STRUCT->aiFlags & (AI_FLAG_CHECK_VIABILITY) ? 95 : 93; + for (i = 0; i < MAX_MON_MOVES; i++) + { + if (AI_THINKING_STRUCT->score[i] > cap) + break; + } + + if (i == MAX_MON_MOVES && GetMostSuitableMonToSwitchInto() != PARTY_SIZE) + { + AI_THINKING_STRUCT->switchMon = TRUE; + return AI_CHOICE_SWITCH; + } + } + + // Consider switching if your mon with truant is bodied by Protect spam. + // Or is using a double turn semi invulnerable move(such as Fly) and is faster. + if (GetBattlerAbility(sBattler_AI) == ABILITY_TRUANT + && IsTruantMonVulnerable(sBattler_AI, gBattlerTarget) + && gDisableStructs[sBattler_AI].truantCounter + && gBattleMons[sBattler_AI].hp >= gBattleMons[sBattler_AI].maxHP / 2) + { + if (GetMostSuitableMonToSwitchInto() != PARTY_SIZE) + { + AI_THINKING_STRUCT->switchMon = TRUE; + return AI_CHOICE_SWITCH; + } + } + } + numOfBestMoves = 1; currentMoveArray[0] = AI_THINKING_STRUCT->score[0]; consideredMoveArray[0] = 0; diff --git a/src/battle_ai_switch_items.c b/src/battle_ai_switch_items.c index f40677bd9..a3a1af8da 100644 --- a/src/battle_ai_switch_items.c +++ b/src/battle_ai_switch_items.c @@ -18,77 +18,8 @@ static bool8 HasSuperEffectiveMoveAgainstOpponents(bool8 noRng); static bool8 FindMonWithFlagsAndSuperEffective(u16 flags, u8 moduloPercent); static bool8 ShouldUseItem(void); -static bool32 AI_ShouldHeal(u8 healAmount); // Functions -u8 AI_TrySwitchOrUseItem(u8 currAction) -{ - struct Pokemon *party; - u8 battlerIn1, battlerIn2; - s32 firstId; - s32 lastId; // + 1 - u8 battlerIdentity = GetBattlerPosition(gActiveBattler); - - if (!(gBattleTypeFlags & BATTLE_TYPE_TRAINER)) - return currAction; - - if (GetBattlerSide(gActiveBattler) == B_SIDE_PLAYER) - party = gPlayerParty; // Player's partner - else - party = gEnemyParty; // Enemy trainer - - // Switching logic - if (ShouldSwitch()) - { - if (*(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) == PARTY_SIZE) - { - s32 monToSwitchId = GetMostSuitableMonToSwitchInto(); - if (monToSwitchId == PARTY_SIZE) - { - if (!(gBattleTypeFlags & BATTLE_TYPE_DOUBLE)) - { - battlerIn1 = GetBattlerAtPosition(battlerIdentity); - battlerIn2 = battlerIn1; - } - else - { - battlerIn1 = GetBattlerAtPosition(battlerIdentity); - battlerIn2 = GetBattlerAtPosition(battlerIdentity ^ BIT_FLANK); - } - - GetAIPartyIndexes(gActiveBattler, &firstId, &lastId); - - for (monToSwitchId = firstId; monToSwitchId < lastId; monToSwitchId++) - { - if (GetMonData(&party[monToSwitchId], MON_DATA_HP) == 0) - continue; - if (monToSwitchId == gBattlerPartyIndexes[battlerIn1]) - continue; - if (monToSwitchId == gBattlerPartyIndexes[battlerIn2]) - continue; - if (monToSwitchId == *(gBattleStruct->monToSwitchIntoId + battlerIn1)) - continue; - if (monToSwitchId == *(gBattleStruct->monToSwitchIntoId + battlerIn2)) - continue; - - break; - } - } - - *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) = monToSwitchId; - } - - *(gBattleStruct->monToSwitchIntoId + gActiveBattler) = *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler); - return AI_CHOICE_SWITCH; - } - - // Item Logic - if (ShouldUseItem()) - return AI_CHOICE_USE_ITEM; - - return currAction; -} - void GetAIPartyIndexes(u32 battlerId, s32 *firstId, s32 *lastId) { if (BATTLE_TWO_VS_ONE_OPPONENT && (battlerId & BIT_SIDE) == B_SIDE_OPPONENT) @@ -110,52 +41,11 @@ void GetAIPartyIndexes(u32 battlerId, s32 *firstId, s32 *lastId) static bool8 ShouldSwitchIfAllBadMoves(void) { - u32 i; - - if (AI_THINKING_STRUCT->aiFlags & (AI_FLAG_CHECK_VIABILITY | AI_FLAG_CHECK_BAD_MOVE | AI_FLAG_TRY_TO_FAINT | AI_FLAG_PREFER_BATON_PASS)) + if (gBattleResources->ai->switchMon) { - if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE) - { - // TODO double battle bad move switching logic - } - else - { - // Single battle. gActiveBattler is the enemy's battler id - if (GetTotalBaseStat(gBattleMons[gActiveBattler].species) >= 310 // Mon is not weak. - && gBattleMons[gActiveBattler].hp >= gBattleMons[gActiveBattler].maxHP / 2) - { - s32 cap = AI_THINKING_STRUCT->aiFlags & (AI_FLAG_CHECK_VIABILITY) ? 95 : 93; - for (i = 0; i < MAX_MON_MOVES; i++) - { - if (AI_THINKING_STRUCT->score[i] > cap) - break; - } - - if (i == MAX_MON_MOVES && GetMostSuitableMonToSwitchInto() != PARTY_SIZE) - { - AI_THINKING_STRUCT->switchMon = TRUE; - } - } - - // Consider switching if your mon with truant is bodied by Protect spam. - // Or is using a double turn semi invulnerable move(such as Fly) and is faster. - if (GetBattlerAbility(gActiveBattler) == ABILITY_TRUANT - && IsTruantMonVulnerable(gActiveBattler, gBattlerTarget) - && gDisableStructs[gActiveBattler].truantCounter - && gBattleMons[gActiveBattler].hp >= gBattleMons[gActiveBattler].maxHP / 2) - { - if (GetMostSuitableMonToSwitchInto() != PARTY_SIZE) - { - AI_THINKING_STRUCT->switchMon = TRUE; - } - } - } - } - - if (AI_THINKING_STRUCT->switchMon) - { - AI_THINKING_STRUCT->switchMon = FALSE; + gBattleResources->ai->switchMon = 0; *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) = PARTY_SIZE; + BtlController_EmitTwoReturnValues(1, B_ACTION_SWITCH, 0); return TRUE; } else @@ -170,6 +60,7 @@ static bool8 ShouldSwitchIfPerishSong(void) && gDisableStructs[gActiveBattler].perishSongTimer == 0) { *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) = PARTY_SIZE; + BtlController_EmitTwoReturnValues(1, B_ACTION_SWITCH, 0); return TRUE; } else @@ -236,6 +127,7 @@ static bool8 ShouldSwitchIfWonderGuard(void) { // We found a mon. *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) = i; + BtlController_EmitTwoReturnValues(1, B_ACTION_SWITCH, 0); return TRUE; } } @@ -326,6 +218,7 @@ static bool8 FindMonThatAbsorbsOpponentsMove(void) { // we found a mon. *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) = i; + BtlController_EmitTwoReturnValues(1, B_ACTION_SWITCH, 0); return TRUE; } } @@ -345,11 +238,13 @@ static bool8 ShouldSwitchIfNaturalCure(void) if ((gLastLandedMoves[gActiveBattler] == 0 || gLastLandedMoves[gActiveBattler] == 0xFFFF) && Random() & 1) { *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) = PARTY_SIZE; + BtlController_EmitTwoReturnValues(1, B_ACTION_SWITCH, 0); return TRUE; } else if (gBattleMoves[gLastLandedMoves[gActiveBattler]].power == 0 && Random() & 1) { *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) = PARTY_SIZE; + BtlController_EmitTwoReturnValues(1, B_ACTION_SWITCH, 0); return TRUE; } @@ -361,6 +256,7 @@ static bool8 ShouldSwitchIfNaturalCure(void) if (Random() & 1) { *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) = PARTY_SIZE; + BtlController_EmitTwoReturnValues(1, B_ACTION_SWITCH, 0); return TRUE; } @@ -541,10 +437,6 @@ bool32 ShouldSwitch(void) return FALSE; availableToSwitch = 0; - AI_THINKING_STRUCT->switchMon = FALSE; - - if (CountUsablePartyMons(gActiveBattler) == 0) // No pokemon to switch to - return FALSE; if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE) { @@ -610,6 +502,74 @@ bool32 ShouldSwitch(void) return FALSE; } +void AI_TrySwitchOrUseItem(void) +{ + struct Pokemon *party; + u8 battlerIn1, battlerIn2; + s32 firstId; + s32 lastId; // + 1 + u8 battlerIdentity = GetBattlerPosition(gActiveBattler); + + if (GetBattlerSide(gActiveBattler) == B_SIDE_PLAYER) + party = gPlayerParty; + else + party = gEnemyParty; + + if (gBattleTypeFlags & BATTLE_TYPE_TRAINER) + { + if (ShouldSwitch()) + { + if (*(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) == PARTY_SIZE) + { + s32 monToSwitchId = GetMostSuitableMonToSwitchInto(); + if (monToSwitchId == PARTY_SIZE) + { + if (!(gBattleTypeFlags & BATTLE_TYPE_DOUBLE)) + { + battlerIn1 = GetBattlerAtPosition(battlerIdentity); + battlerIn2 = battlerIn1; + } + else + { + battlerIn1 = GetBattlerAtPosition(battlerIdentity); + battlerIn2 = GetBattlerAtPosition(battlerIdentity ^ BIT_FLANK); + } + + GetAIPartyIndexes(gActiveBattler, &firstId, &lastId); + + for (monToSwitchId = firstId; monToSwitchId < lastId; monToSwitchId++) + { + if (GetMonData(&party[monToSwitchId], MON_DATA_HP) == 0) + continue; + if (monToSwitchId == gBattlerPartyIndexes[battlerIn1]) + continue; + if (monToSwitchId == gBattlerPartyIndexes[battlerIn2]) + continue; + if (monToSwitchId == *(gBattleStruct->monToSwitchIntoId + battlerIn1)) + continue; + if (monToSwitchId == *(gBattleStruct->monToSwitchIntoId + battlerIn2)) + continue; + + break; + } + } + + *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler) = monToSwitchId; + } + + *(gBattleStruct->monToSwitchIntoId + gActiveBattler) = *(gBattleStruct->AI_monToSwitchIntoId + gActiveBattler); + return; + } + else if (ShouldUseItem()) + { + return; + } + } + + // AI has decided it shouldn't switch or use an item, so it will now choose a move to use + BtlController_EmitTwoReturnValues(1, B_ACTION_USE_MOVE, (gActiveBattler ^ BIT_SIDE) << 8); +} + // If there are two(or more) mons to choose from, always choose one that has baton pass // as most often it can't do much on its own. static u32 GetBestMonBatonPass(struct Pokemon *party, int firstId, int lastId, u8 invalidMons, int aliveCount) @@ -882,10 +842,21 @@ static bool8 ShouldUseItem(void) switch (*(gBattleStruct->AI_itemType + gActiveBattler / 2)) { case AI_ITEM_FULL_RESTORE: - shouldUse = AI_ShouldHeal(0); + if (gBattleMons[gActiveBattler].hp >= gBattleMons[gActiveBattler].maxHP / 4) + break; + if (gBattleMons[gActiveBattler].hp == 0) + break; + shouldUse = TRUE; break; case AI_ITEM_HEAL_HP: - shouldUse = AI_ShouldHeal(itemEffects[GetItemEffectParamOffset(item, 4, 4)]); + paramOffset = GetItemEffectParamOffset(item, 4, 4); + if (paramOffset == 0) + break; + if (gBattleMons[gActiveBattler].hp == 0) + break; + if (gBattleMons[gActiveBattler].hp < gBattleMons[gActiveBattler].maxHP / 4 + || gBattleMons[gActiveBattler].maxHP - gBattleMons[gActiveBattler].hp > itemEffects[paramOffset]) + shouldUse = TRUE; break; case AI_ITEM_CURE_CONDITION: *(gBattleStruct->AI_itemFlags + gActiveBattler / 2) = 0; @@ -967,6 +938,7 @@ static bool8 ShouldUseItem(void) if (shouldUse) { + BtlController_EmitTwoReturnValues(1, B_ACTION_USE_ITEM, 0); *(gBattleStruct->chosenItem + (gActiveBattler / 2) * 2) = item; gBattleResources->battleHistory->trainerItems[i] = 0; return shouldUse; @@ -975,41 +947,3 @@ static bool8 ShouldUseItem(void) return FALSE; } - -static bool32 AI_ShouldHeal(u8 healAmount) -{ - bool32 shouldHeal = FALSE; - u32 i; - - if (gBattleMons[gActiveBattler].hp < gBattleMons[gActiveBattler].maxHP / 4 - || gBattleMons[gActiveBattler].hp == 0 - || (healAmount != 0 && gBattleMons[gActiveBattler].maxHP - gBattleMons[gActiveBattler].hp > healAmount)) - { - // We have low enough HP to consider healing - shouldHeal = TRUE; - - // Check special cases to NOT heal - for (i = 0; i < gBattlersCount; i++) - { - if (GetBattlerSide(i) == B_SIDE_PLAYER) - { - if (CanTargetFaintAiWithMod(i, gActiveBattler, healAmount, 0)) - { - // Target is expected to faint us even after we heal. So why bother. - shouldHeal = FALSE; - break; - } - - // AI_THINKING_STRUCT->movesetIndex is the array index of the AI's chosen move - if (CanIndexMoveFaintTarget(gActiveBattler, i, AI_THINKING_STRUCT->movesetIndex, 0) && AI_WhoStrikesFirst(gActiveBattler, i) == AI_IS_FASTER) - { - // We can faint the target and move first -> don't heal - shouldHeal = FALSE; - break; - } - } - } - } - - return shouldHeal; -} diff --git a/src/battle_controller_opponent.c b/src/battle_controller_opponent.c index 5975ab2a1..f4982b804 100644 --- a/src/battle_controller_opponent.c +++ b/src/battle_controller_opponent.c @@ -1545,70 +1545,7 @@ static void OpponentHandlePrintSelectionString(void) static void OpponentHandleChooseAction(void) { - if (gBattleTypeFlags & BATTLE_TYPE_PALACE) - { - BtlController_EmitTwoReturnValues(1, B_ACTION_USE_MOVE, ChooseMoveAndTargetInBattlePalace()); - } - else - { - u8 chosenMoveId; - struct ChooseMoveStruct moveInfo; - - FillChooseMoveStruct(&moveInfo); - if (gBattleTypeFlags & (BATTLE_TYPE_TRAINER | BATTLE_TYPE_FIRST_BATTLE | BATTLE_TYPE_SAFARI | BATTLE_TYPE_ROAMER)) - { - BattleAI_SetupAIData(0xF); - chosenMoveId = BattleAI_ChooseMoveOrAction(); - switch (chosenMoveId) - { - case AI_CHOICE_USE_ITEM: - BtlController_EmitTwoReturnValues(1, B_ACTION_USE_ITEM, 0); - break; - case AI_CHOICE_SWITCH: - BtlController_EmitTwoReturnValues(1, B_ACTION_SWITCH, 0); - break; - case AI_CHOICE_WATCH: - BtlController_EmitTwoReturnValues(1, B_ACTION_SAFARI_WATCH_CAREFULLY, 0); - break; - case AI_CHOICE_FLEE: - BtlController_EmitTwoReturnValues(1, B_ACTION_RUN, 0); - break; - default: - if (gBattleMoves[moveInfo.moves[chosenMoveId]].target & (MOVE_TARGET_USER_OR_SELECTED | MOVE_TARGET_USER)) - gBattlerTarget = gActiveBattler; - if (gBattleMoves[moveInfo.moves[chosenMoveId]].target & MOVE_TARGET_BOTH) - { - gBattlerTarget = GetBattlerAtPosition(B_POSITION_PLAYER_LEFT); - if (gAbsentBattlerFlags & gBitTable[gBattlerTarget]) - gBattlerTarget = GetBattlerAtPosition(B_POSITION_PLAYER_RIGHT); - } - - if (CanMegaEvolve(gActiveBattler)) { // If opponent can mega evolve, do it. - BtlController_EmitTwoReturnValues(1, B_ACTION_USE_MOVE, (chosenMoveId) | (RET_MEGA_EVOLUTION) | (gBattlerTarget << 8)); - } else { - BtlController_EmitTwoReturnValues(1, B_ACTION_USE_MOVE, (chosenMoveId) | (gBattlerTarget << 8)); - } - break; - } - } - else // Wild pokemon - use random move - { - u16 move; - do - { - chosenMoveId = Random() & 3; - move = moveInfo.moves[chosenMoveId]; - } while (move == MOVE_NONE); - - if (gBattleMoves[move].target & (MOVE_TARGET_USER_OR_SELECTED | MOVE_TARGET_USER)) - BtlController_EmitTwoReturnValues(1, B_ACTION_USE_MOVE, (chosenMoveId) | (gActiveBattler << 8)); - else if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE) - BtlController_EmitTwoReturnValues(1, B_ACTION_USE_MOVE, (chosenMoveId) | (GetBattlerAtPosition(Random() & 2) << 8)); - else - BtlController_EmitTwoReturnValues(1, B_ACTION_USE_MOVE, (chosenMoveId) | (GetBattlerAtPosition(B_POSITION_PLAYER_LEFT) << 8)); - } - } - + AI_TrySwitchOrUseItem(); // TODO consider move choice first OpponentBufferExecCompleted(); } @@ -1619,9 +1556,74 @@ static void OpponentHandleYesNoBox(void) static void OpponentHandleChooseMove(void) { - u8 *bufferB = gBattleResources->bufferB[gActiveBattler]; - BtlController_EmitTwoReturnValues(1, 10, bufferB[2] | (bufferB[3] << 8)); - OpponentBufferExecCompleted(); + if (gBattleTypeFlags & BATTLE_TYPE_PALACE) + { + BtlController_EmitTwoReturnValues(1, 10, ChooseMoveAndTargetInBattlePalace()); + OpponentBufferExecCompleted(); + } + else + { + u8 chosenMoveId; + struct ChooseMoveStruct *moveInfo = (struct ChooseMoveStruct*)(&gBattleResources->bufferA[gActiveBattler][4]); + + if (gBattleTypeFlags & (BATTLE_TYPE_TRAINER | BATTLE_TYPE_FIRST_BATTLE | BATTLE_TYPE_SAFARI | BATTLE_TYPE_ROAMER)) + { + BattleAI_SetupAIData(0xF); + chosenMoveId = BattleAI_ChooseMoveOrAction(); + + switch (chosenMoveId) + { + case AI_CHOICE_WATCH: + BtlController_EmitTwoReturnValues(1, B_ACTION_SAFARI_WATCH_CAREFULLY, 0); + break; + case AI_CHOICE_FLEE: + BtlController_EmitTwoReturnValues(1, B_ACTION_RUN, 0); + break; + case AI_CHOICE_SWITCH: + BtlController_EmitTwoReturnValues(1, 10, 0xFFFF); + break; + case 6: + BtlController_EmitTwoReturnValues(1, 15, gBattlerTarget); + break; + default: + if (gBattleMoves[moveInfo->moves[chosenMoveId]].target & (MOVE_TARGET_USER_OR_SELECTED | MOVE_TARGET_USER)) + gBattlerTarget = gActiveBattler; + if (gBattleMoves[moveInfo->moves[chosenMoveId]].target & MOVE_TARGET_BOTH) + { + gBattlerTarget = GetBattlerAtPosition(B_POSITION_PLAYER_LEFT); + if (gAbsentBattlerFlags & gBitTable[gBattlerTarget]) + gBattlerTarget = GetBattlerAtPosition(B_POSITION_PLAYER_RIGHT); + } + + // If opponent can mega evolve, do it. + if (CanMegaEvolve(gActiveBattler)) + BtlController_EmitTwoReturnValues(1, 10, (chosenMoveId) | (RET_MEGA_EVOLUTION) | (gBattlerTarget << 8)); + else + BtlController_EmitTwoReturnValues(1, 10, (chosenMoveId) | (gBattlerTarget << 8)); + break; + } + + OpponentBufferExecCompleted(); + } + else // Wild pokemon - use random move + { + u16 move; + do + { + chosenMoveId = Random() & 3; + move = moveInfo->moves[chosenMoveId]; + } while (move == MOVE_NONE); + + if (gBattleMoves[move].target & (MOVE_TARGET_USER_OR_SELECTED | MOVE_TARGET_USER)) + BtlController_EmitTwoReturnValues(1, 10, (chosenMoveId) | (gActiveBattler << 8)); + else if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE) + BtlController_EmitTwoReturnValues(1, 10, (chosenMoveId) | (GetBattlerAtPosition(Random() & 2) << 8)); + else + BtlController_EmitTwoReturnValues(1, 10, (chosenMoveId) | (GetBattlerAtPosition(B_POSITION_PLAYER_LEFT) << 8)); + + OpponentBufferExecCompleted(); + } + } } static void OpponentHandleChooseItem(void) diff --git a/src/battle_controller_player_partner.c b/src/battle_controller_player_partner.c index b4052402e..2b4187a31 100644 --- a/src/battle_controller_player_partner.c +++ b/src/battle_controller_player_partner.c @@ -1509,37 +1509,7 @@ static void PlayerPartnerHandlePrintSelectionString(void) static void PlayerPartnerHandleChooseAction(void) { - u8 chosenMoveId; - struct ChooseMoveStruct *moveInfo = (struct ChooseMoveStruct*)(&gBattleResources->bufferA[gActiveBattler][4]); - - BattleAI_SetupAIData(0xF); - chosenMoveId = BattleAI_ChooseMoveOrAction(); - - switch (chosenMoveId) - { - case AI_CHOICE_USE_ITEM: - BtlController_EmitTwoReturnValues(1, B_ACTION_USE_ITEM, 0); - break; - case AI_CHOICE_SWITCH: - BtlController_EmitTwoReturnValues(1, B_ACTION_SWITCH, 0); - break; - default: - if (gBattleMoves[moveInfo->moves[chosenMoveId]].target & (MOVE_TARGET_USER | MOVE_TARGET_USER_OR_SELECTED)) - gBattlerTarget = gActiveBattler; - if (gBattleMoves[moveInfo->moves[chosenMoveId]].target & MOVE_TARGET_BOTH) - { - gBattlerTarget = GetBattlerAtPosition(B_POSITION_OPPONENT_LEFT); - if (gAbsentBattlerFlags & gBitTable[gBattlerTarget]) - gBattlerTarget = GetBattlerAtPosition(B_POSITION_OPPONENT_RIGHT); - } - - if (CanMegaEvolve(gActiveBattler)) // If partner can mega evolve, do it. - BtlController_EmitTwoReturnValues(1, B_ACTION_USE_MOVE, (chosenMoveId) | (RET_MEGA_EVOLUTION) | (gBattlerTarget << 8)); - else - BtlController_EmitTwoReturnValues(1, B_ACTION_USE_MOVE, (chosenMoveId) | (gBattlerTarget << 8)); - break; - } - + AI_TrySwitchOrUseItem(); PlayerPartnerBufferExecCompleted(); } @@ -1550,6 +1520,26 @@ static void PlayerPartnerHandleYesNoBox(void) static void PlayerPartnerHandleChooseMove(void) { + u8 chosenMoveId; + struct ChooseMoveStruct *moveInfo = (struct ChooseMoveStruct*)(&gBattleResources->bufferA[gActiveBattler][4]); + + BattleAI_SetupAIData(0xF); + chosenMoveId = BattleAI_ChooseMoveOrAction(); + + if (gBattleMoves[moveInfo->moves[chosenMoveId]].target & (MOVE_TARGET_USER | MOVE_TARGET_USER_OR_SELECTED)) + gBattlerTarget = gActiveBattler; + if (gBattleMoves[moveInfo->moves[chosenMoveId]].target & MOVE_TARGET_BOTH) + { + gBattlerTarget = GetBattlerAtPosition(B_POSITION_OPPONENT_LEFT); + if (gAbsentBattlerFlags & gBitTable[gBattlerTarget]) + gBattlerTarget = GetBattlerAtPosition(B_POSITION_OPPONENT_RIGHT); + } + + if (CanMegaEvolve(gActiveBattler)) // If partner can mega evolve, do it. + BtlController_EmitTwoReturnValues(1, 10, (chosenMoveId) | (RET_MEGA_EVOLUTION) | (gBattlerTarget << 8)); + else + BtlController_EmitTwoReturnValues(1, 10, (chosenMoveId) | (gBattlerTarget << 8)); + PlayerPartnerBufferExecCompleted(); } diff --git a/src/battle_main.c b/src/battle_main.c index 84f66d6de..766736048 100644 --- a/src/battle_main.c +++ b/src/battle_main.c @@ -3877,7 +3877,22 @@ static void HandleTurnActionSelectionState(void) { struct ChooseMoveStruct moveInfo; - FillChooseMoveStruct(&moveInfo); + moveInfo.mega = gBattleStruct->mega; + moveInfo.species = gBattleMons[gActiveBattler].species; + moveInfo.monType1 = gBattleMons[gActiveBattler].type1; + moveInfo.monType2 = gBattleMons[gActiveBattler].type2; + moveInfo.monType3 = gBattleMons[gActiveBattler].type3; + + for (i = 0; i < MAX_MON_MOVES; i++) + { + moveInfo.moves[i] = gBattleMons[gActiveBattler].moves[i]; + moveInfo.currentPp[i] = gBattleMons[gActiveBattler].pp[i]; + moveInfo.maxPp[i] = CalculatePPWithBonus( + gBattleMons[gActiveBattler].moves[i], + gBattleMons[gActiveBattler].ppBonuses, + i); + } + BtlController_EmitChooseMove(0, (gBattleTypeFlags & BATTLE_TYPE_DOUBLE) != 0, FALSE, &moveInfo); MarkBattlerForControllerExec(gActiveBattler); } @@ -5302,24 +5317,3 @@ void SetTotemBoost(void) } } } - -void FillChooseMoveStruct(struct ChooseMoveStruct * moveInfo) -{ - int i; - - moveInfo->mega = gBattleStruct->mega; - moveInfo->species = gBattleMons[gActiveBattler].species; - moveInfo->monType1 = gBattleMons[gActiveBattler].type1; - moveInfo->monType2 = gBattleMons[gActiveBattler].type2; - moveInfo->monType3 = gBattleMons[gActiveBattler].type3; - - for (i = 0; i < MAX_MON_MOVES; i++) - { - moveInfo->moves[i] = gBattleMons[gActiveBattler].moves[i]; - moveInfo->currentPp[i] = gBattleMons[gActiveBattler].pp[i]; - moveInfo->maxPp[i] = CalculatePPWithBonus( - gBattleMons[gActiveBattler].moves[i], - gBattleMons[gActiveBattler].ppBonuses, - i); - } -}