Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up array access by using index instead of dereferencing the value #6057

Merged
merged 3 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/battle_ai_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1707,7 +1707,7 @@ static s32 AI_CheckBadMove(u32 battlerAtk, u32 battlerDef, u32 move, s32 score)
|| !IsBattlerAlive(BATTLE_PARTNER(battlerAtk))
|| PartnerHasSameMoveEffectWithoutTarget(BATTLE_PARTNER(battlerAtk), move, aiData->partnerMove)
|| (aiData->partnerMove != MOVE_NONE && IsBattleMoveStatus(aiData->partnerMove))
|| *(gBattleStruct->monToSwitchIntoId + BATTLE_PARTNER(battlerAtk)) != PARTY_SIZE) //Partner is switching out.
|| gBattleStruct->monToSwitchIntoId[BATTLE_PARTNER(battlerAtk)] != PARTY_SIZE) //Partner is switching out.
ADJUST_SCORE(-10);
break;
case EFFECT_TRICK:
Expand Down
6 changes: 3 additions & 3 deletions src/battle_ai_switch_items.c
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ void AI_TrySwitchOrUseItem(u32 battler)
gBattleStruct->AI_monToSwitchIntoId[battler] = monToSwitchId;
}

*(gBattleStruct->monToSwitchIntoId + battler) = gBattleStruct->AI_monToSwitchIntoId[battler];
gBattleStruct->monToSwitchIntoId[battler] = gBattleStruct->AI_monToSwitchIntoId[battler];
AI_DATA->monToSwitchInId[battler] = gBattleStruct->AI_monToSwitchIntoId[battler];
return;
}
Expand Down Expand Up @@ -1984,8 +1984,8 @@ u32 GetMostSuitableMonToSwitchInto(u32 battler, bool32 switchAfterMonKOd)
s32 lastId = 0; // + 1
struct Pokemon *party;

if (*(gBattleStruct->monToSwitchIntoId + battler) != PARTY_SIZE)
return *(gBattleStruct->monToSwitchIntoId + battler);
if (gBattleStruct->monToSwitchIntoId[battler] != PARTY_SIZE)
return gBattleStruct->monToSwitchIntoId[battler];
if (gBattleTypeFlags & BATTLE_TYPE_ARENA)
return gBattlerPartyIndexes[battler] + 1;

Expand Down
6 changes: 3 additions & 3 deletions src/battle_controller_player.c
Original file line number Diff line number Diff line change
Expand Up @@ -2043,7 +2043,7 @@ static void PlayerHandleChooseAction(u32 battler)
if (B_SHOW_PARTNER_TARGET && gBattleTypeFlags & BATTLE_TYPE_INGAME_PARTNER && IsBattlerAlive(B_POSITION_PLAYER_RIGHT))
{
StringCopy(gStringVar1, COMPOUND_STRING("Partner will use:\n"));
u32 move = gBattleMons[B_POSITION_PLAYER_RIGHT].moves[*(gBattleStruct->chosenMovePositions + B_POSITION_PLAYER_RIGHT)];
u32 move = gBattleMons[B_POSITION_PLAYER_RIGHT].moves[gBattleStruct->chosenMovePositions[B_POSITION_PLAYER_RIGHT]];
StringAppend(gStringVar1, GetMoveName(move));
u32 moveTarget = GetBattlerMoveTargetType(B_POSITION_PLAYER_RIGHT, move);
if (moveTarget == MOVE_TARGET_SELECTED)
Expand Down Expand Up @@ -2107,7 +2107,7 @@ void HandleChooseMoveAfterDma3(u32 battler)

static void PlayerChooseMoveInBattlePalace(u32 battler)
{
if (--*(gBattleStruct->arenaMindPoints + battler) == 0)
if (--gBattleStruct->arenaMindPoints[battler] == 0)
{
gBattlePalaceMoveSelectionRngValue = gRngValue;
BtlController_EmitTwoReturnValues(battler, BUFFER_B, 10, ChooseMoveAndTargetInBattlePalace(battler));
Expand All @@ -2119,7 +2119,7 @@ void PlayerHandleChooseMove(u32 battler)
{
if (gBattleTypeFlags & BATTLE_TYPE_PALACE)
{
*(gBattleStruct->arenaMindPoints + battler) = 8;
gBattleStruct->arenaMindPoints[battler] = 8;
gBattlerControllerFuncs[battler] = PlayerChooseMoveInBattlePalace;
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/battle_controller_player_partner.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ static void PlayerPartnerHandleChoosePokemon(u32 battler)
}
}
}
*(gBattleStruct->monToSwitchIntoId + battler) = chosenMonId;
gBattleStruct->monToSwitchIntoId[battler] = chosenMonId;
}
else // Mon to switch out has been already chosen.
{
Expand Down
4 changes: 2 additions & 2 deletions src/battle_controller_recorded_opponent.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,9 @@ static void RecordedOpponentHandleChooseItem(u32 battler)

static void RecordedOpponentHandleChoosePokemon(u32 battler)
{
*(gBattleStruct->monToSwitchIntoId + battler) = RecordedBattle_GetBattlerAction(RECORDED_PARTY_INDEX, battler);
gBattleStruct->monToSwitchIntoId[battler] = RecordedBattle_GetBattlerAction(RECORDED_PARTY_INDEX, battler);
gSelectedMonPartyId = gBattleStruct->monToSwitchIntoId[battler]; // Revival Blessing
BtlController_EmitChosenMonReturnValue(battler, BUFFER_B, *(gBattleStruct->monToSwitchIntoId + battler), NULL);
BtlController_EmitChosenMonReturnValue(battler, BUFFER_B, gBattleStruct->monToSwitchIntoId[battler], NULL);
RecordedOpponentBufferExecCompleted(battler);
}

Expand Down
4 changes: 2 additions & 2 deletions src/battle_controller_recorded_player.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,9 @@ static void RecordedPlayerHandleChooseItem(u32 battler)

static void RecordedPlayerHandleChoosePokemon(u32 battler)
{
*(gBattleStruct->monToSwitchIntoId + battler) = RecordedBattle_GetBattlerAction(RECORDED_PARTY_INDEX, battler);
gBattleStruct->monToSwitchIntoId[battler] = RecordedBattle_GetBattlerAction(RECORDED_PARTY_INDEX, battler);
gSelectedMonPartyId = gBattleStruct->monToSwitchIntoId[battler]; // Revival Blessing
BtlController_EmitChosenMonReturnValue(battler, BUFFER_B, *(gBattleStruct->monToSwitchIntoId + battler), NULL);
BtlController_EmitChosenMonReturnValue(battler, BUFFER_B, gBattleStruct->monToSwitchIntoId[battler], NULL);
RecordedPlayerBufferExecCompleted(battler);
}

Expand Down
82 changes: 41 additions & 41 deletions src/battle_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3198,9 +3198,9 @@ void SwitchInClearSetData(u32 battler)
{
if (gBattleMons[i].status2 & STATUS2_INFATUATED_WITH(battler))
gBattleMons[i].status2 &= ~STATUS2_INFATUATED_WITH(battler);
if ((gBattleMons[i].status2 & STATUS2_WRAPPED) && *(gBattleStruct->wrappedBy + i) == battler)
if ((gBattleMons[i].status2 & STATUS2_WRAPPED) && gBattleStruct->wrappedBy[i] == battler)
gBattleMons[i].status2 &= ~STATUS2_WRAPPED;
if ((gStatuses4[i] & STATUS4_SYRUP_BOMB) && *(gBattleStruct->stickySyrupdBy + i) == battler)
if ((gStatuses4[i] & STATUS4_SYRUP_BOMB) && gBattleStruct->stickySyrupdBy[i] == battler)
gStatuses4[i] &= ~STATUS4_SYRUP_BOMB;
}

Expand Down Expand Up @@ -3313,9 +3313,9 @@ const u8* FaintClearSetData(u32 battler)
gBattleMons[i].status2 &= ~STATUS2_ESCAPE_PREVENTION;
if (gBattleMons[i].status2 & STATUS2_INFATUATED_WITH(battler))
gBattleMons[i].status2 &= ~STATUS2_INFATUATED_WITH(battler);
if ((gBattleMons[i].status2 & STATUS2_WRAPPED) && *(gBattleStruct->wrappedBy + i) == battler)
if ((gBattleMons[i].status2 & STATUS2_WRAPPED) && gBattleStruct->wrappedBy[i] == battler)
gBattleMons[i].status2 &= ~STATUS2_WRAPPED;
if ((gStatuses4[i] & STATUS4_SYRUP_BOMB) && *(gBattleStruct->stickySyrupdBy + i) == battler)
if ((gStatuses4[i] & STATUS4_SYRUP_BOMB) && gBattleStruct->stickySyrupdBy[i] == battler)
gStatuses4[i] &= ~STATUS4_SYRUP_BOMB;
}

Expand Down Expand Up @@ -3931,7 +3931,7 @@ static void TryDoEventsBeforeFirstTurn(void)
case FIRST_TURN_EVENTS_END:
for (i = 0; i < MAX_BATTLERS_COUNT; i++)
{
*(gBattleStruct->monToSwitchIntoId + i) = PARTY_SIZE;
gBattleStruct->monToSwitchIntoId[i] = PARTY_SIZE;
gChosenActionByBattler[i] = B_ACTION_NONE;
gChosenMoveByBattler[i] = MOVE_NONE;
gBattleStruct->battlerState[i].absentBattlerFlags = gAbsentBattlerFlags & (1u << i);
Expand Down Expand Up @@ -4052,22 +4052,22 @@ void BattleTurnPassed(void)
gChosenActionByBattler[i] = B_ACTION_NONE;
gChosenMoveByBattler[i] = MOVE_NONE;
gBattleStruct->battlerState[i].absentBattlerFlags = gAbsentBattlerFlags & (1u << i);
gBattleStruct->monToSwitchIntoId[i] = PARTY_SIZE;
}

for (i = 0; i < MAX_BATTLERS_COUNT; i++)
*(gBattleStruct->monToSwitchIntoId + i) = PARTY_SIZE;
for (i = 0; i < NUM_BATTLE_SIDES; i++)
{
if (gSideTimers[i].retaliateTimer > 0)
gSideTimers[i].retaliateTimer--;

}

BattlePutTextOnWindow(gText_EmptyString3, B_WIN_MSG);
AssignUsableGimmicks();
SetShellSideArmCategory();
SetAiLogicDataForTurn(AI_DATA); // get assumed abilities, hold effects, etc of all battlers
gBattleMainFunc = HandleTurnActionSelectionState;

if (gSideTimers[B_SIDE_PLAYER].retaliateTimer > 0)
gSideTimers[B_SIDE_PLAYER].retaliateTimer--;
if (gSideTimers[B_SIDE_OPPONENT].retaliateTimer > 0)
gSideTimers[B_SIDE_OPPONENT].retaliateTimer--;

if (gBattleTypeFlags & BATTLE_TYPE_PALACE)
BattleScriptExecute(BattleScript_PalacePrintFlavorText);
else if (gBattleTypeFlags & BATTLE_TYPE_ARENA && gBattleStruct->arenaTurnCounter == 0)
Expand Down Expand Up @@ -4168,7 +4168,7 @@ void SwitchPartyOrder(u32 battler)
gBattlePartyCurrentOrder[i] = *(battler * 3 + i + (u8 *)(gBattleStruct->battlerPartyOrders));

partyId1 = GetPartyIdFromBattlePartyId(gBattlerPartyIndexes[battler]);
partyId2 = GetPartyIdFromBattlePartyId(*(gBattleStruct->monToSwitchIntoId + battler));
partyId2 = GetPartyIdFromBattlePartyId(gBattleStruct->monToSwitchIntoId[battler]);
SwitchPartyMonSlots(partyId1, partyId2);

if (IsDoubleBattle())
Expand Down Expand Up @@ -4204,7 +4204,7 @@ enum
void SetupAISwitchingData(u32 battler, bool32 isAiRisky)
{
s32 opposingBattler = GetBattlerAtPosition(BATTLE_OPPOSITE(GetBattlerPosition(battler)));

// AI's data
AI_DATA->mostSuitableMonId[battler] = GetMostSuitableMonToSwitchInto(battler, isAiRisky);
if (ShouldSwitch(battler))
Expand All @@ -4219,7 +4219,7 @@ void SetupAISwitchingData(u32 battler, bool32 isAiRisky)
if (ShouldSwitch(opposingBattler))
AI_DATA->shouldSwitch |= (1u << opposingBattler);
AI_DATA->aiSwitchPredictionInProgress = FALSE;

// Determine whether AI will use predictions this turn
AI_DATA->predictingSwitch = RandomPercentage(RNG_AI_PREDICT_SWITCH, 50);
}
Expand Down Expand Up @@ -4257,7 +4257,7 @@ static void HandleTurnActionSelectionState(void)
}
// fallthrough
case STATE_BEFORE_ACTION_CHOSEN: // Choose an action.
*(gBattleStruct->monToSwitchIntoId + battler) = PARTY_SIZE;
gBattleStruct->monToSwitchIntoId[battler] = PARTY_SIZE;
if (gBattleTypeFlags & BATTLE_TYPE_MULTI
|| (position & BIT_FLANK) == B_FLANK_LEFT
|| gBattleStruct->battlerState[GetBattlerAtPosition(BATTLE_PARTNER(position))].absentBattlerFlags
Expand Down Expand Up @@ -4310,15 +4310,15 @@ static void HandleTurnActionSelectionState(void)
if (AreAllMovesUnusable(battler))
{
gBattleCommunication[battler] = STATE_SELECTION_SCRIPT;
*(gBattleStruct->selectionScriptFinished + battler) = FALSE;
*(gBattleStruct->stateIdAfterSelScript + battler) = STATE_WAIT_ACTION_CONFIRMED_STANDBY;
*(gBattleStruct->moveTarget + battler) = gBattleResources->bufferB[battler][3];
gBattleStruct->selectionScriptFinished[battler] = FALSE;
gBattleStruct->stateIdAfterSelScript[battler] = STATE_WAIT_ACTION_CONFIRMED_STANDBY;
gBattleStruct->moveTarget[battler] = gBattleResources->bufferB[battler][3];
return;
}
else if (gDisableStructs[battler].encoredMove != 0)
{
gChosenMoveByBattler[battler] = gDisableStructs[battler].encoredMove;
*(gBattleStruct->chosenMovePositions + battler) = gDisableStructs[battler].encoredMovePos;
gBattleStruct->chosenMovePositions[battler] = gDisableStructs[battler].encoredMovePos;
gBattleCommunication[battler] = STATE_WAIT_ACTION_CONFIRMED_STANDBY;
return;
}
Expand Down Expand Up @@ -4352,8 +4352,8 @@ static void HandleTurnActionSelectionState(void)
RecordedBattle_ClearBattlerAction(battler, 1);
gSelectionBattleScripts[battler] = BattleScript_ActionSelectionItemsCantBeUsed;
gBattleCommunication[battler] = STATE_SELECTION_SCRIPT;
*(gBattleStruct->selectionScriptFinished + battler) = FALSE;
*(gBattleStruct->stateIdAfterSelScript + battler) = STATE_BEFORE_ACTION_CHOSEN;
gBattleStruct->selectionScriptFinished[battler] = FALSE;
gBattleStruct->stateIdAfterSelScript[battler] = STATE_BEFORE_ACTION_CHOSEN;
return;
}

Expand All @@ -4368,8 +4368,8 @@ static void HandleTurnActionSelectionState(void)
RecordedBattle_ClearBattlerAction(battler, 1);
gSelectionBattleScripts[battler] = BattleScript_ActionSelectionItemsCantBeUsed;
gBattleCommunication[battler] = STATE_SELECTION_SCRIPT;
*(gBattleStruct->selectionScriptFinished + battler) = FALSE;
*(gBattleStruct->stateIdAfterSelScript + battler) = STATE_BEFORE_ACTION_CHOSEN;
gBattleStruct->selectionScriptFinished[battler] = FALSE;
gBattleStruct->stateIdAfterSelScript[battler] = STATE_BEFORE_ACTION_CHOSEN;
return;
}
else
Expand All @@ -4379,7 +4379,7 @@ static void HandleTurnActionSelectionState(void)
}
break;
case B_ACTION_SWITCH:
*(gBattleStruct->battlerPartyIndexes + battler) = gBattlerPartyIndexes[battler];
gBattleStruct->battlerPartyIndexes[battler] = gBattlerPartyIndexes[battler];
if (gBattleTypeFlags & BATTLE_TYPE_ARENA
|| !CanBattlerEscape(battler))
{
Expand All @@ -4393,9 +4393,9 @@ static void HandleTurnActionSelectionState(void)
else
{
if (battler == 2 && gChosenActionByBattler[0] == B_ACTION_SWITCH)
BtlController_EmitChoosePokemon(battler, BUFFER_A, PARTY_ACTION_CHOOSE_MON, *(gBattleStruct->monToSwitchIntoId + 0), ABILITY_NONE, gBattleStruct->battlerPartyOrders[battler]);
BtlController_EmitChoosePokemon(battler, BUFFER_A, PARTY_ACTION_CHOOSE_MON, gBattleStruct->monToSwitchIntoId[0], ABILITY_NONE, gBattleStruct->battlerPartyOrders[battler]);
else if (battler == 3 && gChosenActionByBattler[1] == B_ACTION_SWITCH)
BtlController_EmitChoosePokemon(battler, BUFFER_A, PARTY_ACTION_CHOOSE_MON, *(gBattleStruct->monToSwitchIntoId + 1), ABILITY_NONE, gBattleStruct->battlerPartyOrders[battler]);
BtlController_EmitChoosePokemon(battler, BUFFER_A, PARTY_ACTION_CHOOSE_MON, gBattleStruct->monToSwitchIntoId[1], ABILITY_NONE, gBattleStruct->battlerPartyOrders[battler]);
else
BtlController_EmitChoosePokemon(battler, BUFFER_A, PARTY_ACTION_CHOOSE_MON, PARTY_SIZE, ABILITY_NONE, gBattleStruct->battlerPartyOrders[battler]);
}
Expand All @@ -4406,8 +4406,8 @@ static void HandleTurnActionSelectionState(void)
{
gSelectionBattleScripts[battler] = BattleScript_PrintFullBox;
gBattleCommunication[battler] = STATE_SELECTION_SCRIPT;
*(gBattleStruct->selectionScriptFinished + battler) = FALSE;
*(gBattleStruct->stateIdAfterSelScript + battler) = STATE_BEFORE_ACTION_CHOSEN;
gBattleStruct->selectionScriptFinished[battler] = FALSE;
gBattleStruct->stateIdAfterSelScript[battler] = STATE_BEFORE_ACTION_CHOSEN;
return;
}
break;
Expand Down Expand Up @@ -4467,8 +4467,8 @@ static void HandleTurnActionSelectionState(void)
{
gSelectionBattleScripts[battler] = BattleScript_AskIfWantsToForfeitMatch;
gBattleCommunication[battler] = STATE_SELECTION_SCRIPT_MAY_RUN;
*(gBattleStruct->selectionScriptFinished + battler) = FALSE;
*(gBattleStruct->stateIdAfterSelScript + battler) = STATE_BEFORE_ACTION_CHOSEN;
gBattleStruct->selectionScriptFinished[battler] = FALSE;
gBattleStruct->stateIdAfterSelScript[battler] = STATE_BEFORE_ACTION_CHOSEN;
return;
}
else if (gBattleTypeFlags & BATTLE_TYPE_TRAINER
Expand All @@ -4484,8 +4484,8 @@ static void HandleTurnActionSelectionState(void)
{
gSelectionBattleScripts[battler] = BattleScript_PrintCantEscapeFromBattle;
gBattleCommunication[battler] = STATE_SELECTION_SCRIPT;
*(gBattleStruct->selectionScriptFinished + battler) = FALSE;
*(gBattleStruct->stateIdAfterSelScript + battler) = STATE_BEFORE_ACTION_CHOSEN;
gBattleStruct->selectionScriptFinished[battler] = FALSE;
gBattleStruct->stateIdAfterSelScript[battler] = STATE_BEFORE_ACTION_CHOSEN;
return;
}
else
Expand Down Expand Up @@ -4526,9 +4526,9 @@ static void HandleTurnActionSelectionState(void)
{
RecordedBattle_ClearBattlerAction(battler, 1);
gBattleCommunication[battler] = STATE_SELECTION_SCRIPT;
*(gBattleStruct->selectionScriptFinished + battler) = FALSE;
gBattleStruct->selectionScriptFinished[battler] = FALSE;
gBattleResources->bufferB[battler][1] = B_ACTION_USE_MOVE;
*(gBattleStruct->stateIdAfterSelScript + battler) = STATE_WAIT_ACTION_CHOSEN;
gBattleStruct->stateIdAfterSelScript[battler] = STATE_WAIT_ACTION_CHOSEN;
return;
}
else
Expand Down Expand Up @@ -4657,9 +4657,9 @@ static void HandleTurnActionSelectionState(void)
}
break;
case STATE_SELECTION_SCRIPT:
if (*(gBattleStruct->selectionScriptFinished + battler))
if (gBattleStruct->selectionScriptFinished[battler])
{
gBattleCommunication[battler] = *(gBattleStruct->stateIdAfterSelScript + battler);
gBattleCommunication[battler] = gBattleStruct->stateIdAfterSelScript[battler];
}
else
{
Expand All @@ -4679,7 +4679,7 @@ static void HandleTurnActionSelectionState(void)
}
break;
case STATE_SELECTION_SCRIPT_MAY_RUN:
if (*(gBattleStruct->selectionScriptFinished + battler))
if (gBattleStruct->selectionScriptFinished[battler])
{
if (gBattleResources->bufferB[battler][1] == B_ACTION_NOTHING_FAINTED)
{
Expand All @@ -4690,7 +4690,7 @@ static void HandleTurnActionSelectionState(void)
else
{
RecordedBattle_ClearBattlerAction(battler, 1);
gBattleCommunication[battler] = *(gBattleStruct->stateIdAfterSelScript + battler);
gBattleCommunication[battler] = gBattleStruct->stateIdAfterSelScript[battler];
}
}
else
Expand Down Expand Up @@ -4732,7 +4732,7 @@ static void HandleTurnActionSelectionState(void)
for (i = 0; i < gBattlersCount; i++)
{
if (gChosenActionByBattler[i] == B_ACTION_SWITCH)
SwitchPartyOrderInGameMulti(i, *(gBattleStruct->monToSwitchIntoId + i));
SwitchPartyOrderInGameMulti(i, gBattleStruct->monToSwitchIntoId[battler]);
}
}
}
Expand Down Expand Up @@ -4862,7 +4862,7 @@ s8 GetChosenMovePriority(u32 battler)
if (gProtectStructs[battler].noValidMoves)
move = MOVE_STRUGGLE;
else
move = gBattleMons[battler].moves[*(gBattleStruct->chosenMovePositions + battler)];
move = gBattleMons[battler].moves[gBattleStruct->chosenMovePositions[battler]];

return GetBattleMovePriority(battler, move);
}
Expand Down
Loading
Loading