From bdc0ea1037ddeb5b9909078c11e9964aeadf4a55 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Wed, 16 Aug 2023 16:44:45 -0400 Subject: [PATCH] Add MOD to match mod with powers of 2 --- include/global.h | 6 ++++++ src/battle_controller_opponent.c | 2 +- src/battle_pyramid.c | 4 ++-- src/battle_script_commands.c | 4 ++-- src/battle_util.c | 11 ++++++----- src/contest_ai.c | 2 +- src/intro.c | 2 +- src/naming_screen.c | 4 ++-- 8 files changed, 21 insertions(+), 14 deletions(-) diff --git a/include/global.h b/include/global.h index e0cecafd07..e6d58f36e0 100644 --- a/include/global.h +++ b/include/global.h @@ -85,6 +85,12 @@ #define SAFE_DIV(a, b) ((a) / (b)) #endif +// The below macro does a%n, but (to match) will switch to a&(n-1) if n is a power of 2. +// There are cases where GF does a&(n-1) where we would really like to have a%n, because +// if n is changed to a value that isn't a power of 2 then a&(n-1) is unlikely to work as +// intended, and a%n for powers of 2 isn't always optimized to use &. +#define MOD(a, n)(((n) & ((n)-1)) ? ((a) % (n)) : ((a) & ((n)-1))) + // Extracts the upper 16 bits of a 32-bit number #define HIHALF(n) (((n) & 0xFFFF0000) >> 16) diff --git a/src/battle_controller_opponent.c b/src/battle_controller_opponent.c index 41afa9505f..e434e3ee62 100644 --- a/src/battle_controller_opponent.c +++ b/src/battle_controller_opponent.c @@ -1588,7 +1588,7 @@ static void OpponentHandleChooseMove(void) u16 move; do { - chosenMoveId = Random() & 3; + chosenMoveId = MOD(Random(), MAX_MON_MOVES); move = moveInfo->moves[chosenMoveId]; } while (move == MOVE_NONE); diff --git a/src/battle_pyramid.c b/src/battle_pyramid.c index a1f1ee542c..e51f804981 100644 --- a/src/battle_pyramid.c +++ b/src/battle_pyramid.c @@ -38,7 +38,7 @@ #include "constants/moves.h" #include "constants/trainers.h" -#define NUM_LAYOUT_OFFSETS 8 // Assumed to be a power of 2 +#define NUM_LAYOUT_OFFSETS 8 extern const struct MapLayout *const gMapLayouts[]; @@ -1904,7 +1904,7 @@ static void GetPyramidFloorLayoutOffsets(u8 *layoutOffsets) for (i = 0; i < NUM_PYRAMID_FLOOR_SQUARES; i++) { - layoutOffsets[i] = sPyramidFloorTemplates[id].layoutOffsets[rand & (NUM_LAYOUT_OFFSETS - 1)]; + layoutOffsets[i] = sPyramidFloorTemplates[id].layoutOffsets[MOD(rand, NUM_LAYOUT_OFFSETS)]; rand >>= 3; if (i == 7) { diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 63569d60c4..60a91e2b48 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -7350,7 +7350,7 @@ static void Cmd_tryconversiontypechange(void) { do { - while ((moveChecked = Random() & (MAX_MON_MOVES - 1)) >= validMoves); + while ((moveChecked = MOD(Random(), MAX_MON_MOVES)) >= validMoves); moveType = gBattleMoves[gBattleMons[gBattlerAttacker].moves[moveChecked]].type; @@ -8189,7 +8189,7 @@ static void Cmd_trychoosesleeptalkmove(void) do { - movePosition = Random() & (MAX_MON_MOVES - 1); + movePosition = MOD(Random(), MAX_MON_MOVES); } while ((gBitTable[movePosition] & unusableMovesBits)); gCalledMove = gBattleMons[gBattlerAttacker].moves[movePosition]; diff --git a/src/battle_util.c b/src/battle_util.c index 6a0137a938..0856b6d856 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -2773,11 +2773,12 @@ u8 AbilityBattleEffects(u8 caseID, u8 battler, u8 ability, u8 special, u16 moveA { do { + // Pick either MOVE_EFFECT_SLEEP, MOVE_EFFECT_POISON or MOVE_EFFECT_BURN gBattleCommunication[MOVE_EFFECT_BYTE] = Random() & 3; } while (gBattleCommunication[MOVE_EFFECT_BYTE] == 0); - + // Replace MOVE_EFFECT_BURN with MOVE_EFFECT_PARALYSIS if (gBattleCommunication[MOVE_EFFECT_BYTE] == MOVE_EFFECT_BURN) - gBattleCommunication[MOVE_EFFECT_BYTE] += 2; // 5 MOVE_EFFECT_PARALYSIS + gBattleCommunication[MOVE_EFFECT_BYTE] += (MOVE_EFFECT_PARALYSIS - MOVE_EFFECT_BURN); gBattleCommunication[MOVE_EFFECT_BYTE] += MOVE_EFFECT_AFFECTS_USER; BattleScriptPushCursor(); @@ -3956,7 +3957,7 @@ u8 IsMonDisobedient(void) { // Randomly select, then print a disobedient string // B_MSG_LOAFING, B_MSG_WONT_OBEY, B_MSG_TURNED_AWAY, or B_MSG_PRETEND_NOT_NOTICE - gBattleCommunication[MULTISTRING_CHOOSER] = Random() & (NUM_LOAF_STRINGS - 1); + gBattleCommunication[MULTISTRING_CHOOSER] = MOD(Random(), NUM_LOAF_STRINGS); gBattlescriptCurrInstr = BattleScript_MoveUsedLoafingAround; return 1; } @@ -3964,7 +3965,7 @@ u8 IsMonDisobedient(void) { do { - gCurrMovePos = gChosenMovePos = Random() & (MAX_MON_MOVES - 1); + gCurrMovePos = gChosenMovePos = MOD(Random(), MAX_MON_MOVES); } while (gBitTable[gCurrMovePos] & calc); gCalledMove = gBattleMons[gBattlerAttacker].moves[gCurrMovePos]; @@ -4007,7 +4008,7 @@ u8 IsMonDisobedient(void) { // Randomly select, then print a disobedient string // B_MSG_LOAFING, B_MSG_WONT_OBEY, B_MSG_TURNED_AWAY, or B_MSG_PRETEND_NOT_NOTICE - gBattleCommunication[MULTISTRING_CHOOSER] = Random() & (NUM_LOAF_STRINGS - 1); + gBattleCommunication[MULTISTRING_CHOOSER] = MOD(Random(), NUM_LOAF_STRINGS); gBattlescriptCurrInstr = BattleScript_MoveUsedLoafingAround; return 1; } diff --git a/src/contest_ai.c b/src/contest_ai.c index 8753ed170d..a4bb5b4549 100644 --- a/src/contest_ai.c +++ b/src/contest_ai.c @@ -326,7 +326,7 @@ u8 ContestAI_GetActionToUse(void) { // Randomly choose a move index. If it's the move // with the highest (or tied highest) score, return - u8 moveIdx = Random() & (MAX_MON_MOVES - 1); // % MAX_MON_MOVES doesn't match + u8 moveIdx = MOD(Random(), MAX_MON_MOVES); u8 score = eContestAI.moveScores[moveIdx]; int i; for (i = 0; i < MAX_MON_MOVES; i++) diff --git a/src/intro.c b/src/intro.c index e5e6a23dd2..6a6a58ddd5 100644 --- a/src/intro.c +++ b/src/intro.c @@ -1163,7 +1163,7 @@ void CB2_InitCopyrightScreenAfterTitleScreen(void) static void Task_Scene1_Load(u8 taskId) { SetVBlankCallback(NULL); - sIntroCharacterGender = Random() & 1; + sIntroCharacterGender = MOD(Random(), GENDER_COUNT); IntroResetGpuRegs(); SetGpuReg(REG_OFFSET_BG3VOFS, 0); SetGpuReg(REG_OFFSET_BG2VOFS, 80); diff --git a/src/naming_screen.c b/src/naming_screen.c index 6dd45aa7e1..6acb29ffbf 100644 --- a/src/naming_screen.c +++ b/src/naming_screen.c @@ -1067,7 +1067,7 @@ static void SpriteCB_InputArrow(struct Sprite *sprite) if (sprite->sDelay == 0 || --sprite->sDelay == 0) { sprite->sDelay = 8; - sprite->sXPosId = (sprite->sXPosId + 1) & (ARRAY_COUNT(x) - 1); + sprite->sXPosId = MOD(sprite->sXPosId + 1, ARRAY_COUNT(x)); } sprite->x2 = x[sprite->sXPosId]; } @@ -1097,7 +1097,7 @@ static void SpriteCB_Underscore(struct Sprite *sprite) sprite->sDelay++; if (sprite->sDelay > 8) { - sprite->sYPosId = (sprite->sYPosId + 1) & (ARRAY_COUNT(y) - 1); + sprite->sYPosId = MOD(sprite->sYPosId + 1, ARRAY_COUNT(y)); sprite->sDelay = 0; } }