Various BUGFIXes and UBFIXes

This commit is contained in:
ExpoSeed 2021-05-15 15:56:17 -05:00
parent ab8318cc79
commit 361fa594b3
10 changed files with 48 additions and 9 deletions

View File

@ -1877,9 +1877,14 @@ static void Cmd_if_has_move_with_effect(void)
case AI_TARGET_PARTNER: case AI_TARGET_PARTNER:
for (i = 0; i < MAX_MON_MOVES; i++) 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]) if (gBattleMons[sBattler_AI].moves[i] != 0 && gBattleMoves[BATTLE_HISTORY->usedMoves[gBattlerTarget].moves[i]].effect == gAIScriptPtr[2])
break; 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) if (i == MAX_MON_MOVES)
gAIScriptPtr += 7; gAIScriptPtr += 7;

View File

@ -135,8 +135,10 @@ void SoundTask_PlayCryHighPitch(u8 taskId)
{ {
if (gBattleAnimArgs[0] == ANIM_ATTACKER) if (gBattleAnimArgs[0] == ANIM_ATTACKER)
species = gContestResources->moveAnim->species; species = gContestResources->moveAnim->species;
#ifndef UBFIX
else else
DestroyAnimVisualTask(taskId); // UB: function should return upon destroying task. DestroyAnimVisualTask(taskId); // UB: task gets destroyed twice.
#endif
} }
else else
{ {
@ -179,8 +181,10 @@ void SoundTask_PlayDoubleCry(u8 taskId)
{ {
if (gBattleAnimArgs[0] == ANIM_ATTACKER) if (gBattleAnimArgs[0] == ANIM_ATTACKER)
species = gContestResources->moveAnim->species; species = gContestResources->moveAnim->species;
#ifndef UBFIX
else else
DestroyAnimVisualTask(taskId); // UB: function should return upon destroying task. DestroyAnimVisualTask(taskId); // UB: task gets destroyed twice.
#endif
} }
else else
{ {

View File

@ -2766,13 +2766,22 @@ static int GetTypeEffectivenessPoints(int move, int targetSpecies, int arg2)
} }
if (TYPE_EFFECT_ATK_TYPE(i) == moveType) 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 (TYPE_EFFECT_DEF_TYPE(i) == defType1)
if ((defAbility == ABILITY_WONDER_GUARD && TYPE_EFFECT_MULTIPLIER(i) == TYPE_x2) || defAbility != ABILITY_WONDER_GUARD) if ((defAbility == ABILITY_WONDER_GUARD && TYPE_EFFECT_MULTIPLIER(i) == TYPE_x2) || defAbility != ABILITY_WONDER_GUARD)
typePower = (typePower * TYPE_EFFECT_MULTIPLIER(i)) / 10; typePower = (typePower * TYPE_EFFECT_MULTIPLIER(i)) / 10;
if (TYPE_EFFECT_DEF_TYPE(i) == defType2 && defType1 != defType2) if (TYPE_EFFECT_DEF_TYPE(i) == defType2 && defType1 != defType2)
if ((defAbility == ABILITY_WONDER_GUARD && TYPE_EFFECT_MULTIPLIER(i) == TYPE_x2) || defAbility != ABILITY_WONDER_GUARD) if ((defAbility == ABILITY_WONDER_GUARD && TYPE_EFFECT_MULTIPLIER(i) == TYPE_x2) || defAbility != ABILITY_WONDER_GUARD)
typePower = (typePower * TYPE_EFFECT_MULTIPLIER(i)) / 10; 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; i += 3;
} }

View File

@ -4221,12 +4221,17 @@ static void Task_OpenMonPic(u8 taskId)
return; return;
break; break;
default: default:
#ifndef UBFIX
DestroyTask(taskId); DestroyTask(taskId);
#endif
// UB: Should not use the task after it has been deleted. // UB: Should not use the task after it has been deleted.
if (gTasks[taskId].tIsSwapScreen == TRUE) if (gTasks[taskId].tIsSwapScreen == TRUE)
Swap_CreateMonSprite(); Swap_CreateMonSprite();
else else
Select_CreateMonSprite(); Select_CreateMonSprite();
#ifdef UBFIX
DestroyTask(taskId);
#endif
return; return;
} }
task->tState++; task->tState++;

View File

@ -1399,8 +1399,12 @@ void GenerateBattlePyramidWildMon(void)
for (i = 0; i < MAX_MON_MOVES; i++) for (i = 0; i < MAX_MON_MOVES; i++)
SetMonMoveSlot(&gEnemyParty[0], wildMons[id].moves[i], 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) if (gSaveBlock2Ptr->frontier.pyramidWinStreaks[lvl] >= 140)
#else
if (gSaveBlock2Ptr->frontier.pyramidWinStreas[gSaveBlock2Ptr->frontier.lvlMode] >= 140)
#endif
{ {
id = (Random() % 17) + 15; id = (Random() % 17) + 15;
for (i = 0; i < NUM_STATS; i++) for (i = 0; i < NUM_STATS; i++)

View File

@ -584,6 +584,9 @@ static void Task_EggHatchPlayBGM(u8 taskID)
PlayBGM(MUS_EVOLUTION); PlayBGM(MUS_EVOLUTION);
DestroyTask(taskID); DestroyTask(taskID);
// UB: task is destroyed, yet the value is incremented // UB: task is destroyed, yet the value is incremented
#ifdef UBFIX
return;
#endif
} }
gTasks[taskID].data[0]++; gTasks[taskID].data[0]++;
} }

View File

@ -548,7 +548,6 @@ bool8 AddPCItem(u16 itemId, u16 count)
void RemovePCItem(u8 index, u16 count) void RemovePCItem(u8 index, u16 count)
{ {
// UB: should use GetPCItemQuantity and SetPCItemQuantity functions
gSaveBlock1Ptr->pcItems[index].quantity -= count; gSaveBlock1Ptr->pcItems[index].quantity -= count;
if (gSaveBlock1Ptr->pcItems[index].quantity == 0) if (gSaveBlock1Ptr->pcItems[index].quantity == 0)
{ {

View File

@ -2896,7 +2896,9 @@ static const union AnimCmd sAnim_CreditDigit[] =
ANIMCMD_FRAME(18, 0), // 9 ANIMCMD_FRAME(18, 0), // 9
// BUG: Animation not terminated properly // BUG: Animation not terminated properly
// Doesn't matter in practice, the frames are set directly and not looped // 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[] = static const union AnimCmd *const sAnims_CreditDigit[] =

View File

@ -417,6 +417,9 @@ static u8 ReadData()
u8 i; u8 i;
u8 temp; u8 temp;
u8 value; u8 value;
#ifdef UBFIX
value = 0;
#endif
for (i = 0; i < 8; i++) for (i = 0; i < 8; i++)
{ {
@ -428,7 +431,7 @@ static u8 ReadData()
GPIO_PORT_DATA = SCK_HI | CS_HI; GPIO_PORT_DATA = SCK_HI | CS_HI;
temp = ((GPIO_PORT_DATA & SIO_HI) >> 1); 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; return value;

View File

@ -319,8 +319,13 @@ static void StringExpandPlaceholders_AwaitingCommFromAnother(u8 *dst, u8 caseId)
case ACTIVITY_CONTEST_CUTE: case ACTIVITY_CONTEST_CUTE:
case ACTIVITY_CONTEST_SMART: case ACTIVITY_CONTEST_SMART:
case ACTIVITY_CONTEST_TOUGH: 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); StringExpandPlaceholders(gStringVar4, sText_AwaitingCommunication);
#else
StringExpandPlaceholders(dst, sText_AwaitingCommunication);
#endif
break; break;
} }
} }