From 23870a40a8cf8ecc47acf1092a108a641c18e1c9 Mon Sep 17 00:00:00 2001 From: sneed Date: Mon, 2 May 2022 06:04:16 +0300 Subject: [PATCH] Initial commit --- include/battle_util.h | 1 + src/battle_ai_util.c | 34 +++++++++++++++++++++++++++++----- src/battle_util.c | 14 ++++++++++---- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/include/battle_util.h b/include/battle_util.h index d2d7ac29f..bf6d4280c 100644 --- a/include/battle_util.h +++ b/include/battle_util.h @@ -133,6 +133,7 @@ u16 CalcTypeEffectivenessMultiplier(u16 move, u8 moveType, u8 battlerAtk, u8 bat u16 CalcPartyMonTypeEffectivenessMultiplier(u16 move, u16 speciesDef, u16 abilityDef); u16 GetTypeModifier(u8 atkType, u8 defType); s32 GetStealthHazardDamage(u8 hazardType, u8 battlerId); +s32 GetStealthHazardDamageByTypesAndHP(u8 hazardType, u8 type1, u8 type2, u32 maxHp); u16 GetMegaEvolutionSpecies(u16 preEvoSpecies, u16 heldItemId); u16 GetWishMegaEvolutionSpecies(u16 preEvoSpecies, u16 moveId1, u16 moveId2, u16 moveId3, u16 moveId4); bool32 CanMegaEvolve(u8 battlerId); diff --git a/src/battle_ai_util.c b/src/battle_ai_util.c index de4271bba..79c2f7ed3 100644 --- a/src/battle_ai_util.c +++ b/src/battle_ai_util.c @@ -2437,19 +2437,43 @@ static bool32 PartyBattlerShouldAvoidHazards(u8 currBattler, u8 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); + u16 holdEffect; + u16 species = GetMonData(mon, MON_DATA_SPECIES); u32 flags = gSideStatuses[GetBattlerSide(currBattler)] & (SIDE_STATUS_SPIKES | SIDE_STATUS_STEALTH_ROCK | SIDE_STATUS_STICKY_WEB | SIDE_STATUS_TOXIC_SPIKES); + s32 hazardDamage = 0; + u8 type1 = gBaseStats[species].type1; + u8 type2 = gBaseStats[species].type2; + u32 maxHp = GetMonData(mon, MON_DATA_MAX_HP); if (flags == 0) return FALSE; - if (ability == ABILITY_MAGIC_GUARD || ability == ABILITY_LEVITATE - || holdEffect == HOLD_EFFECT_HEAVY_DUTY_BOOTS) + if (ability == ABILITY_MAGIC_GUARD) + return FALSE; + if (!(gFieldStatuses & STATUS_FIELD_MAGIC_ROOM || ability == ABILITY_KLUTZ)) + holdEffect = gItems[GetMonData(mon, MON_DATA_HELD_ITEM)].holdEffect; + else + holdEffect = HOLD_EFFECT_NONE; + if (holdEffect == HOLD_EFFECT_HEAVY_DUTY_BOOTS) return FALSE; - if (flags & (SIDE_STATUS_SPIKES | SIDE_STATUS_STEALTH_ROCK) && GetMonData(mon, MON_DATA_HP) < (GetMonData(mon, MON_DATA_MAX_HP) / 8)) - return TRUE; + if (flags & SIDE_STATUS_STEALTH_ROCK) + { + hazardDamage += GetStealthHazardDamageByTypesAndHP(gBattleMoves[MOVE_STEALTH_ROCK].type, type1, type2, maxHp); + } + if (flags & SIDE_STATUS_SPIKES && ((type1 != TYPE_FLYING && type2 != TYPE_FLYING + && ability != ABILITY_LEVITATE && holdEffect != HOLD_EFFECT_AIR_BALLOON) + || holdEffect == HOLD_EFFECT_IRON_BALL || gFieldStatuses & STATUS_FIELD_GRAVITY)) + { + u8 spikesDmg = maxHp / ((5 - gSideTimers[GetBattlerSide(currBattler)].spikesAmount) * 2); + if (spikesDmg == 0) + spikesDmg = 1; + hazardDamage += spikesDmg; + } + + if (hazardDamage >= GetMonData(mon, MON_DATA_HP)) + return TRUE; return FALSE; } diff --git a/src/battle_util.c b/src/battle_util.c index c0a7f2150..06b7c03d2 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -9216,11 +9216,8 @@ u16 GetTypeModifier(u8 atkType, u8 defType) return sTypeEffectivenessTable[atkType][defType]; } -s32 GetStealthHazardDamage(u8 hazardType, u8 battlerId) +s32 GetStealthHazardDamageByTypesAndHP(u8 hazardType, u8 type1, u8 type2, u32 maxHp) { - u8 type1 = gBattleMons[battlerId].type1; - u8 type2 = gBattleMons[battlerId].type2; - u32 maxHp = gBattleMons[battlerId].maxHP; s32 dmg = 0; u16 modifier = UQ_4_12(1.0); @@ -9263,6 +9260,15 @@ s32 GetStealthHazardDamage(u8 hazardType, u8 battlerId) return dmg; } +s32 GetStealthHazardDamage(u8 hazardType, u8 battlerId) +{ + u8 type1 = gBattleMons[battlerId].type1; + u8 type2 = gBattleMons[battlerId].type2; + u32 maxHp = gBattleMons[battlerId].maxHP; + + return GetStealthHazardDamageByTypesAndHP(hazardType, type1, type2, maxHp); +} + bool32 IsPartnerMonFromSameTrainer(u8 battlerId) { if (GetBattlerSide(battlerId) == B_SIDE_OPPONENT && gBattleTypeFlags & BATTLE_TYPE_TWO_OPPONENTS)