From 361fa594b30005edaa8aef5f6b02ac8b15149cba Mon Sep 17 00:00:00 2001 From: ExpoSeed <> Date: Sat, 15 May 2021 15:56:17 -0500 Subject: [PATCH 1/2] Various BUGFIXes and UBFIXes --- src/battle_ai_script_commands.c | 7 ++++++- src/battle_anim_sound_tasks.c | 8 ++++++-- src/battle_dome.c | 11 ++++++++++- src/battle_factory_screen.c | 5 +++++ src/battle_pyramid.c | 6 +++++- src/egg_hatch.c | 3 +++ src/item.c | 1 - src/roulette.c | 4 +++- src/siirtc.c | 5 ++++- src/union_room.c | 7 ++++++- 10 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/battle_ai_script_commands.c b/src/battle_ai_script_commands.c index 9fdd4d0c38..81e7c15cc7 100644 --- a/src/battle_ai_script_commands.c +++ b/src/battle_ai_script_commands.c @@ -1877,9 +1877,14 @@ static void Cmd_if_has_move_with_effect(void) case AI_TARGET_PARTNER: for (i = 0; i < MAX_MON_MOVES; i++) { - // UB: checks sBattler_AI instead of gBattlerTarget. + // BUG: checks sBattler_AI instead of gBattlerTarget. + #ifndef BUGFIX if (gBattleMons[sBattler_AI].moves[i] != 0 && gBattleMoves[BATTLE_HISTORY->usedMoves[gBattlerTarget].moves[i]].effect == gAIScriptPtr[2]) break; + #else + if (gBattleMons[gBattlerTarget].moves[i] != 0 && gBattleMoves[BATTLE_HISTORY->usedMoves[gBattlerTarget].moves[i]].effect == gAIScriptPtr[2]) + break; + #endif } if (i == MAX_MON_MOVES) gAIScriptPtr += 7; diff --git a/src/battle_anim_sound_tasks.c b/src/battle_anim_sound_tasks.c index 8fc93c3aa9..39d6729e8d 100644 --- a/src/battle_anim_sound_tasks.c +++ b/src/battle_anim_sound_tasks.c @@ -135,8 +135,10 @@ void SoundTask_PlayCryHighPitch(u8 taskId) { if (gBattleAnimArgs[0] == ANIM_ATTACKER) species = gContestResources->moveAnim->species; + #ifndef UBFIX else - DestroyAnimVisualTask(taskId); // UB: function should return upon destroying task. + DestroyAnimVisualTask(taskId); // UB: task gets destroyed twice. + #endif } else { @@ -179,8 +181,10 @@ void SoundTask_PlayDoubleCry(u8 taskId) { if (gBattleAnimArgs[0] == ANIM_ATTACKER) species = gContestResources->moveAnim->species; + #ifndef UBFIX else - DestroyAnimVisualTask(taskId); // UB: function should return upon destroying task. + DestroyAnimVisualTask(taskId); // UB: task gets destroyed twice. + #endif } else { diff --git a/src/battle_dome.c b/src/battle_dome.c index a3bf3dadfa..59e418a065 100644 --- a/src/battle_dome.c +++ b/src/battle_dome.c @@ -2766,13 +2766,22 @@ static int GetTypeEffectivenessPoints(int move, int targetSpecies, int arg2) } if (TYPE_EFFECT_ATK_TYPE(i) == moveType) { - // BUG: TYPE_x2 is not necessary and makes the condition always false if the ability is wonder guard. + // BUG: the value of TYPE_x2 does not exist in gTypeEffectiveness, so if defAbility is ABILITY_WONDER_GUARD, the conditional always fails + #ifndef BUGFIX if (TYPE_EFFECT_DEF_TYPE(i) == defType1) if ((defAbility == ABILITY_WONDER_GUARD && TYPE_EFFECT_MULTIPLIER(i) == TYPE_x2) || defAbility != ABILITY_WONDER_GUARD) typePower = (typePower * TYPE_EFFECT_MULTIPLIER(i)) / 10; if (TYPE_EFFECT_DEF_TYPE(i) == defType2 && defType1 != defType2) if ((defAbility == ABILITY_WONDER_GUARD && TYPE_EFFECT_MULTIPLIER(i) == TYPE_x2) || defAbility != ABILITY_WONDER_GUARD) typePower = (typePower * TYPE_EFFECT_MULTIPLIER(i)) / 10; + #else + if (TYPE_EFFECT_DEF_TYPE(i) == defType1) + if ((defAbility == ABILITY_WONDER_GUARD && TYPE_EFFECT_MULTIPLIER(i) == TYPE_MUL_SUPER_EFFECTIVE) || defAbility != ABILITY_WONDER_GUARD) + typePower = (typePower * TYPE_EFFECT_MULTIPLIER(i)) / 10; + if (TYPE_EFFECT_DEF_TYPE(i) == defType2 && defType1 != defType2) + if ((defAbility == ABILITY_WONDER_GUARD && TYPE_EFFECT_MULTIPLIER(i) == TYPE_MUL_SUPER_EFFECTIVE) || defAbility != ABILITY_WONDER_GUARD) + typePower = (typePower * TYPE_EFFECT_MULTIPLIER(i)) / 10; + #endif } i += 3; } diff --git a/src/battle_factory_screen.c b/src/battle_factory_screen.c index 680c6e81c0..db810e965b 100644 --- a/src/battle_factory_screen.c +++ b/src/battle_factory_screen.c @@ -4221,12 +4221,17 @@ static void Task_OpenMonPic(u8 taskId) return; break; default: + #ifndef UBFIX DestroyTask(taskId); + #endif // UB: Should not use the task after it has been deleted. if (gTasks[taskId].tIsSwapScreen == TRUE) Swap_CreateMonSprite(); else Select_CreateMonSprite(); + #ifdef UBFIX + DestroyTask(taskId); + #endif return; } task->tState++; diff --git a/src/battle_pyramid.c b/src/battle_pyramid.c index a41a80bbb3..8eb09bb8da 100644 --- a/src/battle_pyramid.c +++ b/src/battle_pyramid.c @@ -1399,8 +1399,12 @@ void GenerateBattlePyramidWildMon(void) for (i = 0; i < MAX_MON_MOVES; i++) SetMonMoveSlot(&gEnemyParty[0], wildMons[id].moves[i], i); - // BUG: Reading outside the array as lvl was used for mon level instead of frontier lvl mode. + // UB: Reading outside the array as lvl was used for mon level instead of frontier lvl mode. + #ifndef UBFIX if (gSaveBlock2Ptr->frontier.pyramidWinStreaks[lvl] >= 140) + #else + if (gSaveBlock2Ptr->frontier.pyramidWinStreas[gSaveBlock2Ptr->frontier.lvlMode] >= 140) + #endif { id = (Random() % 17) + 15; for (i = 0; i < NUM_STATS; i++) diff --git a/src/egg_hatch.c b/src/egg_hatch.c index 7a3019f56c..576e5c0d9f 100644 --- a/src/egg_hatch.c +++ b/src/egg_hatch.c @@ -584,6 +584,9 @@ static void Task_EggHatchPlayBGM(u8 taskID) PlayBGM(MUS_EVOLUTION); DestroyTask(taskID); // UB: task is destroyed, yet the value is incremented + #ifdef UBFIX + return; + #endif } gTasks[taskID].data[0]++; } diff --git a/src/item.c b/src/item.c index 68907b02c0..1560342629 100644 --- a/src/item.c +++ b/src/item.c @@ -548,7 +548,6 @@ bool8 AddPCItem(u16 itemId, u16 count) void RemovePCItem(u8 index, u16 count) { - // UB: should use GetPCItemQuantity and SetPCItemQuantity functions gSaveBlock1Ptr->pcItems[index].quantity -= count; if (gSaveBlock1Ptr->pcItems[index].quantity == 0) { diff --git a/src/roulette.c b/src/roulette.c index 1ea69dc2e9..adde161991 100644 --- a/src/roulette.c +++ b/src/roulette.c @@ -2896,7 +2896,9 @@ static const union AnimCmd sAnim_CreditDigit[] = ANIMCMD_FRAME(18, 0), // 9 // BUG: Animation not terminated properly // Doesn't matter in practice, the frames are set directly and not looped - //ANIMCMD_END +#ifdef BUGFIX + ANIMCMD_END +#endif }; static const union AnimCmd *const sAnims_CreditDigit[] = diff --git a/src/siirtc.c b/src/siirtc.c index 5c153a8413..01d2e0e723 100644 --- a/src/siirtc.c +++ b/src/siirtc.c @@ -417,6 +417,9 @@ static u8 ReadData() u8 i; u8 temp; u8 value; + #ifdef UBFIX + value = 0; + #endif for (i = 0; i < 8; i++) { @@ -428,7 +431,7 @@ static u8 ReadData() GPIO_PORT_DATA = SCK_HI | CS_HI; temp = ((GPIO_PORT_DATA & SIO_HI) >> 1); - value = (value >> 1) | (temp << 7); // UB: accessing uninitialized var + value = (value >> 1) | (temp << 7); // UB: value is uninitialized on first iteration } return value; diff --git a/src/union_room.c b/src/union_room.c index f41cfd45f3..bd6b303b92 100644 --- a/src/union_room.c +++ b/src/union_room.c @@ -319,8 +319,13 @@ static void StringExpandPlaceholders_AwaitingCommFromAnother(u8 *dst, u8 caseId) case ACTIVITY_CONTEST_CUTE: case ACTIVITY_CONTEST_SMART: case ACTIVITY_CONTEST_TOUGH: - // UB: argument *dst isn't used, instead it always prints to gStringVar4 + // BUG: argument *dst isn't used, instead it always prints to gStringVar4 + // not an issue in practice since Gamefreak never used any other arguments here besides gStringVar4 + #ifndef BUGFIX StringExpandPlaceholders(gStringVar4, sText_AwaitingCommunication); + #else + StringExpandPlaceholders(dst, sText_AwaitingCommunication); + #endif break; } } From a7e3da2301aa7913f2437fe2152efcec6e96faef Mon Sep 17 00:00:00 2001 From: ExpoSeed <> Date: Sat, 15 May 2021 15:59:54 -0500 Subject: [PATCH 2/2] pyramidWinStreas --- src/battle_pyramid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/battle_pyramid.c b/src/battle_pyramid.c index 8eb09bb8da..50efeecb4d 100644 --- a/src/battle_pyramid.c +++ b/src/battle_pyramid.c @@ -1403,7 +1403,7 @@ void GenerateBattlePyramidWildMon(void) #ifndef UBFIX if (gSaveBlock2Ptr->frontier.pyramidWinStreaks[lvl] >= 140) #else - if (gSaveBlock2Ptr->frontier.pyramidWinStreas[gSaveBlock2Ptr->frontier.lvlMode] >= 140) + if (gSaveBlock2Ptr->frontier.pyramidWinStreaks[gSaveBlock2Ptr->frontier.lvlMode] >= 140) #endif { id = (Random() % 17) + 15;