From 3ab91927c3fbef9a58d8bfbb9915ede45e094a9e Mon Sep 17 00:00:00 2001 From: Isaac Rivera Date: Tue, 22 Oct 2024 16:22:28 -0400 Subject: [PATCH 1/3] initial pass at sleep clause cleanup --- include/battle_util.h | 2 + src/battle_dynamax.c | 8 +--- src/battle_script_commands.c | 45 ++++++------------- src/battle_util.c | 87 +++++++++++++++--------------------- src/pokemon.c | 1 + 5 files changed, 55 insertions(+), 88 deletions(-) diff --git a/include/battle_util.h b/include/battle_util.h index a1572c2db8c4..1b32b578f08b 100644 --- a/include/battle_util.h +++ b/include/battle_util.h @@ -300,5 +300,7 @@ bool8 CanMonParticipateInSkyBattle(struct Pokemon *mon); bool8 IsMonBannedFromSkyBattles(u16 species); void RemoveBattlerType(u32 battler, u8 type); u32 GetMoveType(u32 move); +void TryActivateSleepClause(u32 battler); +void TryDeactivateSleepClause(u32 battler); #endif // GUARD_BATTLE_UTIL_H diff --git a/src/battle_dynamax.c b/src/battle_dynamax.c index f46b3a918afd..b9999db2a597 100644 --- a/src/battle_dynamax.c +++ b/src/battle_dynamax.c @@ -890,12 +890,8 @@ void BS_TrySetStatus1(void) gBattleMons[gBattlerTarget].status1 |= STATUS1_SLEEP_TURN((Random() % 3) + 2); else gBattleMons[gBattlerTarget].status1 |= STATUS1_SLEEP_TURN((Random() % 4) + 3); - if (FlagGet(B_FLAG_SLEEP_CLAUSE)) - { - gBattleStruct->sleepClause.isActive[GetBattlerSide(gBattlerTarget)] = TRUE; - gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(gBattlerTarget)][gBattlerPartyIndexes[gBattlerTarget]] = TRUE; - } - + + TryActivateSleepClause(gBattlerTarget); gBattleCommunication[MULTISTRING_CHOOSER] = 4; effect++; } diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index d6df8fd782e6..966c3c98c727 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -3238,6 +3238,8 @@ void SetMoveEffect(bool32 primary, bool32 certain) gBattleMons[gEffectBattler].status1 |= STATUS1_SLEEP_TURN(1 + RandomUniform(RNG_SLEEP_TURNS, 1, 3)); else gBattleMons[gEffectBattler].status1 |= STATUS1_SLEEP_TURN(1 + RandomUniform(RNG_SLEEP_TURNS, 2, 5)); + + // TODO sleep clause: revisit this and determine if effectExempt is needed if (FlagGet(B_FLAG_SLEEP_CLAUSE) && !gBattleStruct->sleepClause.effectExempt) { gBattleStruct->sleepClause.isActive[GetBattlerSide(gEffectBattler)] = TRUE; @@ -4200,11 +4202,8 @@ static void Cmd_tryfaintmon(void) PREPARE_MOVE_BUFFER(gBattleTextBuff1, gBattleMons[gBattlerAttacker].moves[moveIndex]) } - if (FlagGet(B_FLAG_SLEEP_CLAUSE) && gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)] && gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(battler)][gBattlerPartyIndexes[battler]]) - { - gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)] = FALSE; - gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(battler)][gBattlerPartyIndexes[battler]] = FALSE; - } + + TryDeactivateSleepClause(battler); } else { @@ -5917,11 +5916,7 @@ static void Cmd_moveend(void) gBattlescriptCurrInstr = BattleScript_TargetPRLZHeal; break; case STATUS1_SLEEP: - if (FlagGet(B_FLAG_SLEEP_CLAUSE) && gBattleStruct->sleepClause.isActive[GetBattlerSide(gBattlerTarget)] && gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(gBattlerTarget)][gBattlerPartyIndexes[gBattlerTarget]]) - { - gBattleStruct->sleepClause.isActive[GetBattlerSide(gBattlerTarget)] = FALSE; - gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(gBattlerTarget)][gBattlerPartyIndexes[gBattlerTarget]] = FALSE; - } + TryDeactivateSleepClause(gBattlerTarget); gBattlescriptCurrInstr = BattleScript_TargetWokeUp; break; case STATUS1_BURN: @@ -10202,25 +10197,15 @@ static void Cmd_various(void) BtlController_EmitSetMonData(battler, BUFFER_A, REQUEST_STATUS_BATTLE, 0, sizeof(gBattleMons[battler].status1), &gBattleMons[battler].status1); MarkBattlerForControllerExec(battler); gBattlescriptCurrInstr = cmd->nextInstr; - if (FlagGet(B_FLAG_SLEEP_CLAUSE)) - { - gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)] = TRUE; - gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(battler)][gBattlerPartyIndexes[battler]] = TRUE; - } + TryActivateSleepClause(battler); return; } case VARIOUS_CURE_STATUS: { VARIOUS_ARGS(); - if (FlagGet(B_FLAG_SLEEP_CLAUSE) - && (gBattleMons[battler].status1 & STATUS1_SLEEP) - && gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)] - && gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(battler)][gBattlerPartyIndexes[battler]]) - { - gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)] = FALSE; - gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(battler)][gBattlerPartyIndexes[battler]] = FALSE; - } + if (gBattleMons[battler].status1 & STATUS1_SLEEP) + TryDeactivateSleepClause(battler); gBattleMons[battler].status1 = 0; BtlController_EmitSetMonData(battler, BUFFER_A, REQUEST_STATUS_BATTLE, 0, sizeof(gBattleMons[battler].status1), &gBattleMons[battler].status1); @@ -13411,12 +13396,14 @@ static void Cmd_healpartystatus(void) { toHeal |= (1 << i); + // TODO sleep clause: revisit this and determine if iriv is a bot if (FlagGet(B_FLAG_SLEEP_CLAUSE) && gBattleStruct->sleepClause.isCausingSleepClause[gBattlerAttacker][i]) { gBattleStruct->sleepClause.isActive[gBattlerAttacker] = FALSE; gBattleStruct->sleepClause.isCausingSleepClause[gBattlerAttacker][i] = FALSE; } + // TODO sleep clause: revisit this and determine if iriv is a bot if (IsDoubleBattle() && !(gAbsentBattlerFlags & (1u << gBattlerTarget))) { if (FlagGet(B_FLAG_SLEEP_CLAUSE) && gBattleStruct->sleepClause.isCausingSleepClause[partner][i]) @@ -13434,6 +13421,7 @@ static void Cmd_healpartystatus(void) gBattleCommunication[MULTISTRING_CHOOSER] = B_MSG_SOOTHING_AROMA; toHeal = (1 << PARTY_SIZE) - 1; + // TODO sleep clause: revisit this and determine if iriv is a bot if (FlagGet(B_FLAG_SLEEP_CLAUSE)) { gBattleStruct->sleepClause.isActive[gBattlerAttacker] = FALSE; @@ -13449,6 +13437,7 @@ static void Cmd_healpartystatus(void) if (IsDoubleBattle() && !(gAbsentBattlerFlags & (1u <sleepClause.isActive[partner] = FALSE; @@ -14828,14 +14817,8 @@ static void Cmd_switchoutabilities(void) switch (GetBattlerAbility(battler)) { case ABILITY_NATURAL_CURE: - if (FlagGet(B_FLAG_SLEEP_CLAUSE) - && (gBattleMons[battler].status1 & STATUS1_SLEEP ) - && gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)] - && gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(battler)][gBattlerPartyIndexes[battler]]) - { - gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)] = FALSE; - gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(battler)][gBattlerPartyIndexes[battler]] = FALSE; - } + if (gBattleMons[battler].status1 & STATUS1_SLEEP) + TryDeactivateSleepClause(battler); gBattleMons[battler].status1 = 0; BtlController_EmitSetMonData(battler, BUFFER_A, REQUEST_STATUS_BATTLE, 1u << *(gBattleStruct->battlerPartyIndexes + battler), diff --git a/src/battle_util.c b/src/battle_util.c index cd72a3b613e2..946def6c85a0 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -2780,12 +2780,7 @@ u8 DoBattlerEndTurnEffects(void) else gBattleMons[battler].status1 |= ((Random() % 4) + 3); - if (FlagGet(B_FLAG_SLEEP_CLAUSE)) - { - gBattleStruct->sleepClause.isActive[GetBattlerSide(gBattlerTarget)] = TRUE; - gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(gBattlerTarget)][gBattlerPartyIndexes[gBattlerTarget]] = TRUE; - } - + TryActivateSleepClause(gBattlerTarget); BtlController_EmitSetMonData(battler, BUFFER_A, REQUEST_STATUS_BATTLE, 0, 4, &gBattleMons[battler].status1); MarkBattlerForControllerExec(battler); BattleScriptExecute(BattleScript_YawnMakesAsleep); @@ -3230,11 +3225,7 @@ u8 AtkCanceller_UnableToUseMove(u32 moveType) { if (UproarWakeUpCheck(gBattlerAttacker)) { - if (FlagGet(B_FLAG_SLEEP_CLAUSE) && gBattleStruct->sleepClause.isActive[GetBattlerSide(gBattlerAttacker)] && gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(gBattlerAttacker)][gBattlerPartyIndexes[gBattlerAttacker]]) - { - gBattleStruct->sleepClause.isActive[GetBattlerSide(gBattlerAttacker)] = FALSE; - gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(gBattlerAttacker)][gBattlerPartyIndexes[gBattlerAttacker]] = FALSE; - } + TryDeactivateSleepClause(gBattlerAttacker); gBattleMons[gBattlerAttacker].status1 &= ~STATUS1_SLEEP; gBattleMons[gBattlerAttacker].status2 &= ~STATUS2_NIGHTMARE; @@ -3265,12 +3256,7 @@ u8 AtkCanceller_UnableToUseMove(u32 moveType) } else { - if (FlagGet(B_FLAG_SLEEP_CLAUSE) && gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(gBattlerAttacker)][gBattlerPartyIndexes[gBattlerAttacker]]) - { - gBattleStruct->sleepClause.isActive[GetBattlerSide(gBattlerAttacker)] = FALSE; - gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(gBattlerAttacker)][gBattlerPartyIndexes[gBattlerAttacker]] = FALSE; - } - + TryDeactivateSleepClause(gBattlerAttacker); gBattleMons[gBattlerAttacker].status2 &= ~STATUS2_NIGHTMARE; BattleScriptPushCursor(); gBattleCommunication[MULTISTRING_CHOOSER] = B_MSG_WOKE_UP; @@ -5158,11 +5144,7 @@ u32 AbilityBattleEffects(u32 caseID, u32 battler, u32 ability, u32 special, u32 if (gBattleMons[battler].status1 & STATUS1_SLEEP) { StringCopy(gBattleTextBuff1, gStatusConditionString_SleepJpn); - if (FlagGet(B_FLAG_SLEEP_CLAUSE) && gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)] && gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(battler)][gBattlerPartyIndexes[battler]]) - { - gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)] = FALSE; - gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(battler)][gBattlerPartyIndexes[battler]] = FALSE; - } + TryDeactivateSleepClause(battler); } if (gBattleMons[battler].status1 & STATUS1_PARALYSIS) @@ -5253,14 +5235,9 @@ u32 AbilityBattleEffects(u32 caseID, u32 battler, u32 ability, u32 special, u32 && gBattleMons[gBattleScripting.battler].status1 & STATUS1_ANY && RandomPercentage(RNG_HEALER, 30)) { - if (FlagGet(B_FLAG_SLEEP_CLAUSE) - && (gBattleMons[gBattleScripting.battler].status1 & STATUS1_SLEEP ) - && gBattleStruct->sleepClause.isActive[GetBattlerSide(gBattleScripting.battler)] - && gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(battler)][gBattlerPartyIndexes[gBattleScripting.battler]]) - { - gBattleStruct->sleepClause.isActive[GetBattlerSide(gBattleScripting.battler)] = FALSE; - gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(gBattleScripting.battler)][gBattlerPartyIndexes[gBattleScripting.battler]] = FALSE; - } + if (gBattleMons[gBattleScripting.battler].status1 & STATUS1_SLEEP) + TryDeactivateSleepClause(gBattleScripting.battler); + BattleScriptPushCursorAndCallback(BattleScript_HealerActivates); effect++; } @@ -6214,11 +6191,7 @@ u32 AbilityBattleEffects(u32 caseID, u32 battler, u32 ability, u32 special, u32 case ABILITY_VITAL_SPIRIT: if (gBattleMons[battler].status1 & STATUS1_SLEEP) { - if (FlagGet(B_FLAG_SLEEP_CLAUSE) && gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)] && gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(battler)][gBattlerPartyIndexes[battler]]) - { - gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)] = FALSE; - gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(battler)][gBattlerPartyIndexes[battler]] = FALSE; - } + TryDeactivateSleepClause(battler); gBattleMons[battler].status2 &= ~STATUS2_NIGHTMARE; StringCopy(gBattleTextBuff1, gStatusConditionString_SleepJpn); effect = 1; @@ -6661,7 +6634,7 @@ bool32 CanBeSlept(u32 battler, u32 ability, u32 isBlockedBySleepClause) || IsAbilityOnSide(battler, ABILITY_SWEET_VEIL) || IsAbilityStatusProtected(battler) || IsBattlerTerrainAffected(battler, STATUS_FIELD_ELECTRIC_TERRAIN | STATUS_FIELD_MISTY_TERRAIN) - || (isBlockedBySleepClause && gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)] == TRUE)) + || (FlagGet(B_FLAG_SLEEP_CLAUSE) && isBlockedBySleepClause && gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)])) return FALSE; return TRUE; } @@ -7304,11 +7277,7 @@ static u8 ItemEffectMoveEnd(u32 battler, u16 holdEffect) BattleScriptPushCursor(); gBattlescriptCurrInstr = BattleScript_BerryCureSlpRet; effect = ITEM_STATUS_CHANGE; - if (FlagGet(B_FLAG_SLEEP_CLAUSE) && gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)] && gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(battler)][gBattlerPartyIndexes[battler]]) - { - gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)] = FALSE; - gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(battler)][gBattlerPartyIndexes[battler]] = FALSE; - } + TryDeactivateSleepClause(battler); } break; case HOLD_EFFECT_CURE_CONFUSION: @@ -7550,11 +7519,7 @@ u8 ItemBattleEffects(u8 caseID, u32 battler, bool32 moveTurn) gBattleMons[battler].status2 &= ~STATUS2_NIGHTMARE; BattleScriptExecute(BattleScript_BerryCureSlpEnd2); effect = ITEM_STATUS_CHANGE; - if (FlagGet(B_FLAG_SLEEP_CLAUSE) && gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)] && gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(battler)][gBattlerPartyIndexes[battler]]) - { - gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)] = FALSE; - gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(battler)][gBattlerPartyIndexes[battler]] = FALSE; - } + TryDeactivateSleepClause(battler); } break; case HOLD_EFFECT_CURE_STATUS: @@ -7862,11 +7827,7 @@ u8 ItemBattleEffects(u8 caseID, u32 battler, bool32 moveTurn) gBattleMons[battler].status2 &= ~STATUS2_NIGHTMARE; BattleScriptExecute(BattleScript_BerryCureSlpEnd2); effect = ITEM_STATUS_CHANGE; - if (FlagGet(B_FLAG_SLEEP_CLAUSE) && gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)] && gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(battler)][gBattlerPartyIndexes[battler]]) - { - gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)] = FALSE; - gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(battler)][gBattlerPartyIndexes[battler]] = FALSE; - } + TryDeactivateSleepClause(battler); } break; case HOLD_EFFECT_CURE_CONFUSION: @@ -11921,3 +11882,27 @@ u32 GetMoveType(u32 move) return TYPE_MYSTERY; return gMovesInfo[move].type; } + +void TryActivateSleepClause(u32 battler) +{ + if (!FlagGet(B_FLAG_SLEEP_CLAUSE)) + return; + + u32 side = GetBattlerSide(battler); + gBattleStruct->sleepClause.isActive[side] = TRUE; + gBattleStruct->sleepClause.isCausingSleepClause[side][gBattlerPartyIndexes[battler]] = TRUE; +} + +void TryDeactivateSleepClause(u32 battler) +{ + if (!FlagGet(B_FLAG_SLEEP_CLAUSE)) + return; + + u32 side = GetBattlerSide(battler); + + if (gBattleStruct->sleepClause.isActive[side] && gBattleStruct->sleepClause.isCausingSleepClause[side][gBattlerPartyIndexes[battler]]) + { + gBattleStruct->sleepClause.isActive[side] = FALSE; + gBattleStruct->sleepClause.isCausingSleepClause[side][gBattlerPartyIndexes[battler]] = FALSE; + } +} diff --git a/src/pokemon.c b/src/pokemon.c index 1369eb178969..27021c276e97 100644 --- a/src/pokemon.c +++ b/src/pokemon.c @@ -4183,6 +4183,7 @@ bool8 HealStatusConditions(struct Pokemon *mon, u32 healMask, u8 battlerId) SetMonData(mon, MON_DATA_STATUS, &status); if (gMain.inBattle && battlerId != MAX_BATTLERS_COUNT) { + // TODO sleep clause: revisit this and see if it can be cleaned up gBattleMons[battlerId].status1 &= ~healMask; if((healMask & STATUS1_SLEEP) && FlagGet(B_FLAG_SLEEP_CLAUSE) && gBattleStruct->sleepClause.isActive[battlerSide]) { From d062a974ffd295d9369c12dd051b43fb8e520dde Mon Sep 17 00:00:00 2001 From: Isaac Rivera Date: Tue, 22 Oct 2024 20:01:52 -0400 Subject: [PATCH 2/3] more clean up, add code comments for documentation --- include/battle_util.h | 2 +- src/battle_script_commands.c | 51 ++++++++++-------------------------- src/battle_util.c | 41 +++++++++++++++++------------ src/pokemon.c | 14 +++++----- 4 files changed, 46 insertions(+), 62 deletions(-) diff --git a/include/battle_util.h b/include/battle_util.h index 1b32b578f08b..9c411b7a90b0 100644 --- a/include/battle_util.h +++ b/include/battle_util.h @@ -301,6 +301,6 @@ bool8 IsMonBannedFromSkyBattles(u16 species); void RemoveBattlerType(u32 battler, u8 type); u32 GetMoveType(u32 move); void TryActivateSleepClause(u32 battler); -void TryDeactivateSleepClause(u32 battler); +void TryDeactivateSleepClause(u32 battlerSide, u32 indexInParty); #endif // GUARD_BATTLE_UTIL_H diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 966c3c98c727..5415b6121468 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -4203,7 +4203,8 @@ static void Cmd_tryfaintmon(void) PREPARE_MOVE_BUFFER(gBattleTextBuff1, gBattleMons[gBattlerAttacker].moves[moveIndex]) } - TryDeactivateSleepClause(battler); + // Try to deactivate Sleep Clause when a mon faints + TryDeactivateSleepClause(GetBattlerSide(battler), gBattlerPartyIndexes[battler]); } else { @@ -5916,7 +5917,8 @@ static void Cmd_moveend(void) gBattlescriptCurrInstr = BattleScript_TargetPRLZHeal; break; case STATUS1_SLEEP: - TryDeactivateSleepClause(gBattlerTarget); + // Try to deactivate Sleep Clause when a mon gets woken up by Wake-Up Slap + TryDeactivateSleepClause(GetBattlerSide(gBattlerTarget), gBattlerPartyIndexes[gBattlerTarget]); gBattlescriptCurrInstr = BattleScript_TargetWokeUp; break; case STATUS1_BURN: @@ -10204,8 +10206,9 @@ static void Cmd_various(void) { VARIOUS_ARGS(); + // Try to deactivate Sleep Clause when a mon gets woken up by curing sleep via Jungle Healing, Purify, Psycho Shift, Healer, G-Max Sweetness if (gBattleMons[battler].status1 & STATUS1_SLEEP) - TryDeactivateSleepClause(battler); + TryDeactivateSleepClause(GetBattlerSide(battler), gBattlerPartyIndexes[battler]); gBattleMons[battler].status1 = 0; BtlController_EmitSetMonData(battler, BUFFER_A, REQUEST_STATUS_BATTLE, 0, sizeof(gBattleMons[battler].status1), &gBattleMons[battler].status1); @@ -13395,23 +13398,8 @@ static void Cmd_healpartystatus(void) if (ability != ABILITY_SOUNDPROOF) { toHeal |= (1 << i); - - // TODO sleep clause: revisit this and determine if iriv is a bot - if (FlagGet(B_FLAG_SLEEP_CLAUSE) && gBattleStruct->sleepClause.isCausingSleepClause[gBattlerAttacker][i]) - { - gBattleStruct->sleepClause.isActive[gBattlerAttacker] = FALSE; - gBattleStruct->sleepClause.isCausingSleepClause[gBattlerAttacker][i] = FALSE; - } - - // TODO sleep clause: revisit this and determine if iriv is a bot - if (IsDoubleBattle() && !(gAbsentBattlerFlags & (1u << gBattlerTarget))) - { - if (FlagGet(B_FLAG_SLEEP_CLAUSE) && gBattleStruct->sleepClause.isCausingSleepClause[partner][i]) - { - gBattleStruct->sleepClause.isActive[partner] = FALSE; - gBattleStruct->sleepClause.isCausingSleepClause[partner][i] = FALSE; - } - } + // Try to deactivate Sleep Clause when a mon gets woken up by curing sleep via Heal Bell + TryDeactivateSleepClause(GetBattlerSide(gBattlerAttacker), i); } } } @@ -13421,14 +13409,10 @@ static void Cmd_healpartystatus(void) gBattleCommunication[MULTISTRING_CHOOSER] = B_MSG_SOOTHING_AROMA; toHeal = (1 << PARTY_SIZE) - 1; - // TODO sleep clause: revisit this and determine if iriv is a bot - if (FlagGet(B_FLAG_SLEEP_CLAUSE)) + for (i = 0; i < PARTY_SIZE; i++) { - gBattleStruct->sleepClause.isActive[gBattlerAttacker] = FALSE; - for (i = 0; i < PARTY_SIZE; i++) - { - gBattleStruct->sleepClause.isCausingSleepClause[gBattlerAttacker][i] = FALSE; - } + // Try to deactivate Sleep Clause when a mon gets woken up by curing sleep via Aromatherapy, Sparkly Swirl? + TryDeactivateSleepClause(GetBattlerSide(gBattlerAttacker), i); } gBattleMons[gBattlerAttacker].status1 = 0; @@ -13437,15 +13421,6 @@ static void Cmd_healpartystatus(void) if (IsDoubleBattle() && !(gAbsentBattlerFlags & (1u <sleepClause.isActive[partner] = FALSE; - for (i = 0; i < PARTY_SIZE; i++) - { - gBattleStruct->sleepClause.isCausingSleepClause[partner][i] = FALSE; - } - } gBattleMons[partner].status1 = 0; gBattleMons[partner].status2 &= ~STATUS2_NIGHTMARE; } @@ -14817,8 +14792,10 @@ static void Cmd_switchoutabilities(void) switch (GetBattlerAbility(battler)) { case ABILITY_NATURAL_CURE: + // Try to deactivate Sleep Clause when a mon gets woken up by curing sleep via Natural Cure if (gBattleMons[battler].status1 & STATUS1_SLEEP) - TryDeactivateSleepClause(battler); + TryDeactivateSleepClause(GetBattlerSide(battler), gBattlerPartyIndexes[battler]); + gBattleMons[battler].status1 = 0; BtlController_EmitSetMonData(battler, BUFFER_A, REQUEST_STATUS_BATTLE, 1u << *(gBattleStruct->battlerPartyIndexes + battler), diff --git a/src/battle_util.c b/src/battle_util.c index 946def6c85a0..a154a4fff238 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -3225,8 +3225,8 @@ u8 AtkCanceller_UnableToUseMove(u32 moveType) { if (UproarWakeUpCheck(gBattlerAttacker)) { - TryDeactivateSleepClause(gBattlerAttacker); - + // Try to deactivate Sleep Clause when a mon gets woken up by curing sleep via Uproar + TryDeactivateSleepClause(GetBattlerSide(gBattlerAttacker), gBattlerPartyIndexes[gBattlerAttacker]); gBattleMons[gBattlerAttacker].status1 &= ~STATUS1_SLEEP; gBattleMons[gBattlerAttacker].status2 &= ~STATUS2_NIGHTMARE; BattleScriptPushCursor(); @@ -3256,7 +3256,8 @@ u8 AtkCanceller_UnableToUseMove(u32 moveType) } else { - TryDeactivateSleepClause(gBattlerAttacker); + // Try to deactivate Sleep Clause when a mon wakes up + TryDeactivateSleepClause(GetBattlerSide(gBattlerAttacker), gBattlerPartyIndexes[gBattlerAttacker]); gBattleMons[gBattlerAttacker].status2 &= ~STATUS2_NIGHTMARE; BattleScriptPushCursor(); gBattleCommunication[MULTISTRING_CHOOSER] = B_MSG_WOKE_UP; @@ -5144,7 +5145,8 @@ u32 AbilityBattleEffects(u32 caseID, u32 battler, u32 ability, u32 special, u32 if (gBattleMons[battler].status1 & STATUS1_SLEEP) { StringCopy(gBattleTextBuff1, gStatusConditionString_SleepJpn); - TryDeactivateSleepClause(battler); + // Try to deactivate Sleep Clause when a mon gets woken up by curing sleep via Shed Skin, Hydration + TryDeactivateSleepClause(GetBattlerSide(battler), gBattlerPartyIndexes[battler]); } if (gBattleMons[battler].status1 & STATUS1_PARALYSIS) @@ -5235,9 +5237,6 @@ u32 AbilityBattleEffects(u32 caseID, u32 battler, u32 ability, u32 special, u32 && gBattleMons[gBattleScripting.battler].status1 & STATUS1_ANY && RandomPercentage(RNG_HEALER, 30)) { - if (gBattleMons[gBattleScripting.battler].status1 & STATUS1_SLEEP) - TryDeactivateSleepClause(gBattleScripting.battler); - BattleScriptPushCursorAndCallback(BattleScript_HealerActivates); effect++; } @@ -6191,7 +6190,8 @@ u32 AbilityBattleEffects(u32 caseID, u32 battler, u32 ability, u32 special, u32 case ABILITY_VITAL_SPIRIT: if (gBattleMons[battler].status1 & STATUS1_SLEEP) { - TryDeactivateSleepClause(battler); + // Try to deactivate Sleep Clause when a mon gets woken up by curing sleep via gaining the ability Insomnia or Vital Spirit + TryDeactivateSleepClause(GetBattlerSide(battler), gBattlerPartyIndexes[battler]); gBattleMons[battler].status2 &= ~STATUS2_NIGHTMARE; StringCopy(gBattleTextBuff1, gStatusConditionString_SleepJpn); effect = 1; @@ -7277,7 +7277,8 @@ static u8 ItemEffectMoveEnd(u32 battler, u16 holdEffect) BattleScriptPushCursor(); gBattlescriptCurrInstr = BattleScript_BerryCureSlpRet; effect = ITEM_STATUS_CHANGE; - TryDeactivateSleepClause(battler); + // Try to deactivate Sleep Clause when a mon gets woken up by curing sleep via a Chesto Berry + TryDeactivateSleepClause(GetBattlerSide(battler), gBattlerPartyIndexes[battler]); } break; case HOLD_EFFECT_CURE_CONFUSION: @@ -7309,6 +7310,8 @@ static u8 ItemEffectMoveEnd(u32 battler, u16 holdEffect) { gBattleMons[battler].status2 &= ~STATUS2_NIGHTMARE; StringCopy(gBattleTextBuff1, gStatusConditionString_SleepJpn); + // Try to deactivate Sleep Clause when a mon gets woken up by curing sleep via a Lum Berry + TryDeactivateSleepClause(GetBattlerSide(battler), gBattlerPartyIndexes[battler]); } if (gBattleMons[battler].status1 & STATUS1_PARALYSIS) @@ -7519,7 +7522,8 @@ u8 ItemBattleEffects(u8 caseID, u32 battler, bool32 moveTurn) gBattleMons[battler].status2 &= ~STATUS2_NIGHTMARE; BattleScriptExecute(BattleScript_BerryCureSlpEnd2); effect = ITEM_STATUS_CHANGE; - TryDeactivateSleepClause(battler); + // Try to deactivate Sleep Clause when a mon gets woken up by curing sleep via a Chesto Berry + TryDeactivateSleepClause(GetBattlerSide(battler), gBattlerPartyIndexes[battler]); } break; case HOLD_EFFECT_CURE_STATUS: @@ -7538,6 +7542,8 @@ u8 ItemBattleEffects(u8 caseID, u32 battler, bool32 moveTurn) gBattleMons[battler].status2 &= ~STATUS2_NIGHTMARE; StringCopy(gBattleTextBuff1, gStatusConditionString_SleepJpn); i++; + // Try to deactivate Sleep Clause when a mon gets woken up by curing sleep via a Lum Berry + TryDeactivateSleepClause(GetBattlerSide(battler), gBattlerPartyIndexes[battler]); } if (gBattleMons[battler].status1 & STATUS1_PARALYSIS) { @@ -7827,7 +7833,8 @@ u8 ItemBattleEffects(u8 caseID, u32 battler, bool32 moveTurn) gBattleMons[battler].status2 &= ~STATUS2_NIGHTMARE; BattleScriptExecute(BattleScript_BerryCureSlpEnd2); effect = ITEM_STATUS_CHANGE; - TryDeactivateSleepClause(battler); + // Try to deactivate Sleep Clause when a mon gets woken up by curing sleep via a Chesto Berry + TryDeactivateSleepClause(GetBattlerSide(battler), gBattlerPartyIndexes[battler]); } break; case HOLD_EFFECT_CURE_CONFUSION: @@ -7852,6 +7859,8 @@ u8 ItemBattleEffects(u8 caseID, u32 battler, bool32 moveTurn) gBattleMons[battler].status2 &= ~STATUS2_NIGHTMARE; StringCopy(gBattleTextBuff1, gStatusConditionString_SleepJpn); i++; + // Try to deactivate Sleep Clause when a mon gets woken up by curing sleep via a Lum Berry + TryDeactivateSleepClause(GetBattlerSide(battler), gBattlerPartyIndexes[battler]); } if (gBattleMons[battler].status1 & STATUS1_PARALYSIS) { @@ -11893,16 +11902,14 @@ void TryActivateSleepClause(u32 battler) gBattleStruct->sleepClause.isCausingSleepClause[side][gBattlerPartyIndexes[battler]] = TRUE; } -void TryDeactivateSleepClause(u32 battler) +void TryDeactivateSleepClause(u32 battlerSide, u32 indexInParty) { if (!FlagGet(B_FLAG_SLEEP_CLAUSE)) return; - u32 side = GetBattlerSide(battler); - - if (gBattleStruct->sleepClause.isActive[side] && gBattleStruct->sleepClause.isCausingSleepClause[side][gBattlerPartyIndexes[battler]]) + if (gBattleStruct->sleepClause.isActive[battlerSide] && gBattleStruct->sleepClause.isCausingSleepClause[battlerSide][indexInParty]) { - gBattleStruct->sleepClause.isActive[side] = FALSE; - gBattleStruct->sleepClause.isCausingSleepClause[side][gBattlerPartyIndexes[battler]] = FALSE; + gBattleStruct->sleepClause.isActive[battlerSide] = FALSE; + gBattleStruct->sleepClause.isCausingSleepClause[battlerSide][indexInParty] = FALSE; } } diff --git a/src/pokemon.c b/src/pokemon.c index 27021c276e97..37d2a093dc8a 100644 --- a/src/pokemon.c +++ b/src/pokemon.c @@ -4174,8 +4174,6 @@ bool8 PokemonUseItemEffects(struct Pokemon *mon, u16 item, u8 partyIndex, u8 mov bool8 HealStatusConditions(struct Pokemon *mon, u32 healMask, u8 battlerId) { u32 status = GetMonData(mon, MON_DATA_STATUS, 0); - u32 i = 0; - u32 battlerSide = GetBattlerSide(battlerId); if (status & healMask) { @@ -4183,11 +4181,13 @@ bool8 HealStatusConditions(struct Pokemon *mon, u32 healMask, u8 battlerId) SetMonData(mon, MON_DATA_STATUS, &status); if (gMain.inBattle && battlerId != MAX_BATTLERS_COUNT) { - // TODO sleep clause: revisit this and see if it can be cleaned up gBattleMons[battlerId].status1 &= ~healMask; - if((healMask & STATUS1_SLEEP) && FlagGet(B_FLAG_SLEEP_CLAUSE) && gBattleStruct->sleepClause.isActive[battlerSide]) + if((healMask & STATUS1_SLEEP)) { + u32 i = 0; + u32 battlerSide = GetBattlerSide(battlerId); struct Pokemon *party; + if (battlerSide == B_SIDE_PLAYER) party = gPlayerParty; else @@ -4195,10 +4195,10 @@ bool8 HealStatusConditions(struct Pokemon *mon, u32 healMask, u8 battlerId) for (i = 0; i < PARTY_SIZE; i++) { - if (&party[i] == mon && gBattleStruct->sleepClause.isCausingSleepClause[battlerSide][i]) + if (&party[i] == mon) { - gBattleStruct->sleepClause.isActive[battlerSide] = FALSE; - gBattleStruct->sleepClause.isCausingSleepClause[battlerSide][i] = FALSE; + // Try to deactivate Sleep Clause when a mon gets woken up by curing sleep via a Used Item, such as Awakening or Full Heal + TryDeactivateSleepClause(battlerSide, i); break; } } From c7db7192a1dd74dda314cc2ad566e822f13324d0 Mon Sep 17 00:00:00 2001 From: Isaac Rivera Date: Tue, 22 Oct 2024 20:55:05 -0400 Subject: [PATCH 3/3] switch to using bitfield for isCausingSleepClause which also allows isActive to be removed --- include/battle.h | 5 ++--- include/battle_util.h | 2 +- src/battle_dynamax.c | 3 ++- src/battle_script_commands.c | 10 +++++----- src/battle_util.c | 17 ++++++----------- 5 files changed, 16 insertions(+), 21 deletions(-) diff --git a/include/battle.h b/include/battle.h index 17e9d3ab9a50..ff3811d97d57 100644 --- a/include/battle.h +++ b/include/battle.h @@ -603,9 +603,8 @@ struct BattleGimmickData struct SleepClauseData { - u8 isActive[NUM_BATTLE_SIDES]; // Stores sleep clause state for each battle side - bool8 effectExempt; // Stores whether effect should be exempt from triggering sleep clause (Effect Spore) - bool8 isCausingSleepClause[NUM_BATTLE_SIDES][PARTY_SIZE]; // When a Pokemon falls asleep, need to know if it should deactivate sleep clause upon waking + bool8 effectExempt; // Stores whether effect should be exempt from triggering Sleep Clause (Effect Spore) + u8 isCausingSleepClause[NUM_BATTLE_SIDES]; // stores which pokemon on a given side is causing Sleep Clause to be active as a bitfield }; struct LostItem diff --git a/include/battle_util.h b/include/battle_util.h index 9c411b7a90b0..4fb9970f386e 100644 --- a/include/battle_util.h +++ b/include/battle_util.h @@ -300,7 +300,7 @@ bool8 CanMonParticipateInSkyBattle(struct Pokemon *mon); bool8 IsMonBannedFromSkyBattles(u16 species); void RemoveBattlerType(u32 battler, u8 type); u32 GetMoveType(u32 move); -void TryActivateSleepClause(u32 battler); +void TryActivateSleepClause(u32 battlerSide, u32 indexInParty); void TryDeactivateSleepClause(u32 battlerSide, u32 indexInParty); #endif // GUARD_BATTLE_UTIL_H diff --git a/src/battle_dynamax.c b/src/battle_dynamax.c index b9999db2a597..7adfc3154029 100644 --- a/src/battle_dynamax.c +++ b/src/battle_dynamax.c @@ -891,7 +891,8 @@ void BS_TrySetStatus1(void) else gBattleMons[gBattlerTarget].status1 |= STATUS1_SLEEP_TURN((Random() % 4) + 3); - TryActivateSleepClause(gBattlerTarget); + // Try to activate Sleep Clause when a mon is put to Sleep + TryActivateSleepClause(GetBattlerSide(gBattlerTarget), gBattlerPartyIndexes[gBattlerTarget]); gBattleCommunication[MULTISTRING_CHOOSER] = 4; effect++; } diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 5415b6121468..ed19c5ea5edb 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -3242,8 +3242,7 @@ void SetMoveEffect(bool32 primary, bool32 certain) // TODO sleep clause: revisit this and determine if effectExempt is needed if (FlagGet(B_FLAG_SLEEP_CLAUSE) && !gBattleStruct->sleepClause.effectExempt) { - gBattleStruct->sleepClause.isActive[GetBattlerSide(gEffectBattler)] = TRUE; - gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(gEffectBattler)][gBattlerPartyIndexes[gEffectBattler]] = TRUE; + gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(gEffectBattler)] |= 1u << gBattlerPartyIndexes[gEffectBattler]; gBattleStruct->sleepClause.effectExempt = FALSE; } } @@ -10199,7 +10198,8 @@ static void Cmd_various(void) BtlController_EmitSetMonData(battler, BUFFER_A, REQUEST_STATUS_BATTLE, 0, sizeof(gBattleMons[battler].status1), &gBattleMons[battler].status1); MarkBattlerForControllerExec(battler); gBattlescriptCurrInstr = cmd->nextInstr; - TryActivateSleepClause(battler); + // Try to activate Sleep Clause when a mon is put to Sleep by Psycho Shift + TryActivateSleepClause(GetBattlerSide(battler), gBattlerPartyIndexes[battler]); return; } case VARIOUS_CURE_STATUS: @@ -13411,7 +13411,7 @@ static void Cmd_healpartystatus(void) for (i = 0; i < PARTY_SIZE; i++) { - // Try to deactivate Sleep Clause when a mon gets woken up by curing sleep via Aromatherapy, Sparkly Swirl? + // Try to deactivate Sleep Clause when a mon gets woken up by curing sleep via Aromatherapy, Sparkly Swirl TryDeactivateSleepClause(GetBattlerSide(gBattlerAttacker), i); } @@ -17320,7 +17320,7 @@ void BS_JumpIfSleepClause(void) NATIVE_ARGS(u8 battler, const u8 *jumpInstr); u8 battler = GetBattlerForBattleScript(cmd->battler); - if (gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)]) + if (gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(battler)]) gBattlescriptCurrInstr = cmd->jumpInstr; else gBattlescriptCurrInstr = cmd->nextInstr; diff --git a/src/battle_util.c b/src/battle_util.c index a154a4fff238..cb42cd0ac580 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -2780,7 +2780,8 @@ u8 DoBattlerEndTurnEffects(void) else gBattleMons[battler].status1 |= ((Random() % 4) + 3); - TryActivateSleepClause(gBattlerTarget); + // Try to activate Sleep Clause when a mon is put to Sleep by Yawn + TryActivateSleepClause(GetBattlerSide(gBattlerTarget), gBattlerPartyIndexes[gBattlerTarget]); BtlController_EmitSetMonData(battler, BUFFER_A, REQUEST_STATUS_BATTLE, 0, 4, &gBattleMons[battler].status1); MarkBattlerForControllerExec(battler); BattleScriptExecute(BattleScript_YawnMakesAsleep); @@ -6634,7 +6635,7 @@ bool32 CanBeSlept(u32 battler, u32 ability, u32 isBlockedBySleepClause) || IsAbilityOnSide(battler, ABILITY_SWEET_VEIL) || IsAbilityStatusProtected(battler) || IsBattlerTerrainAffected(battler, STATUS_FIELD_ELECTRIC_TERRAIN | STATUS_FIELD_MISTY_TERRAIN) - || (FlagGet(B_FLAG_SLEEP_CLAUSE) && isBlockedBySleepClause && gBattleStruct->sleepClause.isActive[GetBattlerSide(battler)])) + || (FlagGet(B_FLAG_SLEEP_CLAUSE) && isBlockedBySleepClause && gBattleStruct->sleepClause.isCausingSleepClause[GetBattlerSide(battler)])) return FALSE; return TRUE; } @@ -11892,14 +11893,12 @@ u32 GetMoveType(u32 move) return gMovesInfo[move].type; } -void TryActivateSleepClause(u32 battler) +void TryActivateSleepClause(u32 battlerSide, u32 indexInParty) { if (!FlagGet(B_FLAG_SLEEP_CLAUSE)) return; - u32 side = GetBattlerSide(battler); - gBattleStruct->sleepClause.isActive[side] = TRUE; - gBattleStruct->sleepClause.isCausingSleepClause[side][gBattlerPartyIndexes[battler]] = TRUE; + gBattleStruct->sleepClause.isCausingSleepClause[battlerSide] |= 1u << indexInParty; } void TryDeactivateSleepClause(u32 battlerSide, u32 indexInParty) @@ -11907,9 +11906,5 @@ void TryDeactivateSleepClause(u32 battlerSide, u32 indexInParty) if (!FlagGet(B_FLAG_SLEEP_CLAUSE)) return; - if (gBattleStruct->sleepClause.isActive[battlerSide] && gBattleStruct->sleepClause.isCausingSleepClause[battlerSide][indexInParty]) - { - gBattleStruct->sleepClause.isActive[battlerSide] = FALSE; - gBattleStruct->sleepClause.isCausingSleepClause[battlerSide][indexInParty] = FALSE; - } + gBattleStruct->sleepClause.isCausingSleepClause[battlerSide] &= ~(1u << indexInParty); }