diff --git a/src/battle_ai_script_commands.c b/src/battle_ai_script_commands.c index 81e7c15cc..b72875036 100644 --- a/src/battle_ai_script_commands.c +++ b/src/battle_ai_script_commands.c @@ -1761,7 +1761,11 @@ static void Cmd_if_cant_faint(void) gBattleMoveDamage = gBattleMoveDamage * AI_THINKING_STRUCT->simulatedRNG[AI_THINKING_STRUCT->movesetIndex] / 100; - // This macro is missing the damage 0 = 1 assumption. +#ifdef BUGFIX + // Moves always do at least 1 damage. + if (gBattleMoveDamage == 0) + gBattleMoveDamage = 1; +#endif if (gBattleMons[gBattlerTarget].hp > gBattleMoveDamage) gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 1); @@ -2019,18 +2023,24 @@ static void Cmd_if_holds_item(void) { u8 battlerId = BattleAI_GetWantedBattler(gAIScriptPtr[1]); u16 item; - u8 var1, var2; + u8 itemLo, itemHi; if ((battlerId & BIT_SIDE) == (sBattler_AI & BIT_SIDE)) item = gBattleMons[battlerId].item; else item = BATTLE_HISTORY->itemEffects[battlerId]; - // UB: doesn't properly read an unaligned u16 - var2 = gAIScriptPtr[2]; - var1 = gAIScriptPtr[3]; + itemHi = gAIScriptPtr[2]; + itemLo = gAIScriptPtr[3]; - if ((var1 | var2) == item) +#ifdef BUGFIX + // This bug doesn't affect the vanilla game because this script command + // is only used to check ITEM_PERSIM_BERRY, whose high byte happens to + // be 0. + if (((itemHi << 8) | itemLo) == item) +#else + if ((itemLo | itemHi) == item) +#endif gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 4); else gAIScriptPtr += 8; diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 0492caeb5..15e44dd75 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -3357,7 +3357,7 @@ static void Cmd_getexp(void) // get exp getter battlerId if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE) { - if (!(gBattlerPartyIndexes[2] != gBattleStruct->expGetterMonId) && !(gAbsentBattlerFlags & gBitTable[2])) + if (gBattlerPartyIndexes[2] == gBattleStruct->expGetterMonId && !(gAbsentBattlerFlags & gBitTable[2])) gBattleStruct->expGetterBattlerId = 2; else { @@ -3431,14 +3431,13 @@ static void Cmd_getexp(void) gBattleMons[0].maxHP = GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_MAX_HP); gBattleMons[0].attack = GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_ATK); gBattleMons[0].defense = GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_DEF); - // Why is this duplicated? + // Speed is duplicated, likely due to a copy-paste error. gBattleMons[0].speed = GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_SPEED); gBattleMons[0].speed = GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_SPEED); - gBattleMons[0].spAttack = GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_SPATK); gBattleMons[0].spDefense = GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_SPDEF); } - // What is else if? + if (gBattlerPartyIndexes[2] == gBattleStruct->expGetterMonId && gBattleMons[2].hp && (gBattleTypeFlags & BATTLE_TYPE_DOUBLE)) { gBattleMons[2].level = GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_LEVEL); @@ -3446,10 +3445,13 @@ static void Cmd_getexp(void) gBattleMons[2].maxHP = GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_MAX_HP); gBattleMons[2].attack = GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_ATK); gBattleMons[2].defense = GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_DEF); - // Duplicated again, but this time there's no Sp Defense gBattleMons[2].speed = GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_SPEED); + // Speed is duplicated again, but Special Defense is missing. +#ifdef BUGFIX + gBattleMons[2].spDefense = GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_SPDEF); +#else gBattleMons[2].speed = GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_SPEED); - +#endif gBattleMons[2].spAttack = GetMonData(&gPlayerParty[gBattleStruct->expGetterMonId], MON_DATA_SPATK); } gBattleScripting.getexpState = 5; @@ -6168,7 +6170,13 @@ static void Cmd_recordlastability(void) { gActiveBattler = GetBattlerForBattleScript(gBattlescriptCurrInstr[1]); RecordAbilityBattle(gActiveBattler, gLastUsedAbility); - gBattlescriptCurrInstr += 1; // UB: Should be + 2, one byte for command and one byte for battlerId argument. + +#ifdef BUGFIX + // This command occupies two bytes (one for the command id, and one for the battler id parameter). + gBattlescriptCurrInstr += 2; +#else + gBattlescriptCurrInstr += 1; +#endif } void BufferMoveToLearnIntoBattleTextBuff2(void)