From 59c16ff1cf1efda65873dbf425bb5073a39a8691 Mon Sep 17 00:00:00 2001 From: ghoulslash Date: Sat, 10 Jul 2021 16:41:41 -0600 Subject: [PATCH] AI_IsTerrainAffected checks --- include/battle_ai_util.h | 1 + src/battle_ai_main.c | 15 +++++++-------- src/battle_ai_util.c | 9 +++++++++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/include/battle_ai_util.h b/include/battle_ai_util.h index db33a7ff1..d4df682c6 100644 --- a/include/battle_ai_util.h +++ b/include/battle_ai_util.h @@ -36,6 +36,7 @@ u32 AI_GetMoveAccuracy(u8 battlerAtk, u8 battlerDef, u16 atkAbility, u16 defAbil bool32 DoesBattlerIgnoreAbilityChecks(u16 atkAbility, u16 move); bool32 AI_WeatherHasEffect(void); bool32 CanAttackerFaintTarget(u8 battlerAtk, u8 battlerDef, u8 index, u8 numHits); +bool32 AI_IsTerrainAffected(u8 battlerId, u32 flags); bool32 AI_IsBattlerGrounded(u8 battlerId); bool32 HasDamagingMove(u8 battlerId); bool32 HasDamagingMoveOfType(u8 battlerId, u8 type); diff --git a/src/battle_ai_main.c b/src/battle_ai_main.c index 5dc460f6a..81d97fcab 100644 --- a/src/battle_ai_main.c +++ b/src/battle_ai_main.c @@ -721,22 +721,21 @@ static s16 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) #endif // terrain & effect checks - if (gFieldStatuses & STATUS_FIELD_MISTY_TERRAIN) + if (AI_IsTerrainAffected(battlerDef, STATUS_FIELD_ELECTRIC_TERRAIN)) { if (moveEffect == EFFECT_SLEEP || moveEffect == EFFECT_YAWN) RETURN_SCORE_MINUS(20); } - if (gFieldStatuses & STATUS_FIELD_ELECTRIC_TERRAIN) + if (AI_IsTerrainAffected(battlerDef, STATUS_FIELD_MISTY_TERRAIN)) { if (IsNonVolatileStatusMoveEffect(moveEffect) || IsConfusionMoveEffect(moveEffect)) RETURN_SCORE_MINUS(20); } - if (gFieldStatuses & STATUS_FIELD_PSYCHIC_TERRAIN) + if (AI_IsTerrainAffected(battlerAtk, STATUS_FIELD_PSYCHIC_TERRAIN) && atkPriority > 0) { - if (atkPriority > 0) - RETURN_SCORE_MINUS(20); + RETURN_SCORE_MINUS(20); } } // end check MOVE_TARGET_USER @@ -1641,11 +1640,11 @@ static s16 AI_CheckBadMove(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) case EFFECT_TEETER_DANCE: if (((gBattleMons[battlerDef].status2 & STATUS2_CONFUSION) || (!DoesBattlerIgnoreAbilityChecks(AI_DATA->atkAbility, move) && AI_DATA->defAbility == ABILITY_OWN_TEMPO) - || (IsBattlerGrounded(battlerDef) && (gFieldStatuses & STATUS_FIELD_MISTY_TERRAIN)) + || (IsBattlerGrounded(battlerDef) && AI_IsTerrainAffected(battlerDef, STATUS_FIELD_MISTY_TERRAIN)) || (DoesSubstituteBlockMove(battlerAtk, battlerDef, move))) && ((gBattleMons[AI_DATA->battlerDefPartner].status2 & STATUS2_CONFUSION) || (!DoesBattlerIgnoreAbilityChecks(AI_DATA->atkAbility, move) && AI_DATA->defPartnerAbility == ABILITY_OWN_TEMPO) - || (IsBattlerGrounded(AI_DATA->battlerDefPartner) && (gFieldStatuses & STATUS_FIELD_MISTY_TERRAIN)) + || (IsBattlerGrounded(AI_DATA->battlerDefPartner) && AI_IsTerrainAffected(AI_DATA->battlerDefPartner, STATUS_FIELD_MISTY_TERRAIN)) || (DoesSubstituteBlockMove(battlerAtk, AI_DATA->battlerDefPartner, move)))) { score -= 10; @@ -3836,7 +3835,7 @@ static s16 AI_CheckViability(u8 battlerAtk, u8 battlerDef, u16 move, s16 score) score++; break; case EFFECT_SAFEGUARD: - if (!(gFieldStatuses & STATUS_FIELD_MISTY_TERRAIN) || !IsBattlerGrounded(battlerAtk)) + if (!AI_IsTerrainAffected(battlerAtk, STATUS_FIELD_MISTY_TERRAIN) || !IsBattlerGrounded(battlerAtk)) score++; //if (CountUsablePartyMons(battlerDef) != 0) //score += 8; diff --git a/src/battle_ai_util.c b/src/battle_ai_util.c index 5695677bb..600921d97 100644 --- a/src/battle_ai_util.c +++ b/src/battle_ai_util.c @@ -1110,6 +1110,15 @@ u16 AI_GetHoldEffect(u32 battlerId) return holdEffect; } +bool32 AI_IsTerrainAffected(u8 battlerId, u32 flags) +{ + if (gStatuses3[battlerId] & STATUS3_SEMI_INVULNERABLE) + return FALSE; + else if (!(gFieldStatuses & flags)) + return FALSE; + return AI_IsBattlerGrounded(battlerId); +} + // different from IsBattlerGrounded in that we don't always know battler's hold effect or ability bool32 AI_IsBattlerGrounded(u8 battlerId) {