Skip to content

Commit

Permalink
Reverts additional move effect macros (#4277)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexOn1ine authored Mar 14, 2024
1 parent 68e5c9f commit b5af343
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 70 deletions.
2 changes: 1 addition & 1 deletion include/battle_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ bool32 IsHealBlockPreventingMove(u32 battler, u32 move);
bool32 HasEnoughHpToEatBerry(u32 battler, u32 hpFraction, u32 itemId);
bool32 IsPartnerMonFromSameTrainer(u32 battler);
u8 GetCategoryBasedOnStats(u32 battler);
bool32 MoveIsAffectedBySheerForce(u16 move);
bool32 MoveIsAffectedBySheerForce(u32 move);
bool32 TestIfSheerForceAffected(u32 battler, u16 move);
void TryRestoreHeldItems(void);
bool32 CanStealItem(u32 battlerStealing, u32 battlerItem, u16 item);
Expand Down
12 changes: 4 additions & 8 deletions include/pokemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,8 @@ struct MoveInfo
u32 strikeCount:4; // Max 15 hits. Defaults to 1 if not set. May apply its effect on each hit.
u32 criticalHitStage:2;
u32 alwaysCriticalHit:1;
// 14 bits left to complete this word - continues into flags
u32 numAdditionalEffects:2; // limited to 3 - don't want to get too crazy
// 12 bits left to complete this word - continues into flags

// Flags
u32 makesContact:1;
Expand Down Expand Up @@ -516,7 +517,7 @@ struct MoveInfo
u32 argument;

// primary/secondary effects
uintptr_t additionalEffects;
const struct AdditionalEffect *additionalEffects;

// contest parameters
u8 contestEffect;
Expand All @@ -526,12 +527,7 @@ struct MoveInfo
};

#define EFFECTS_ARR(...) (const struct AdditionalEffect[]) {__VA_ARGS__}
#define ADDITIONAL_EFFECTS(...) ((min(ARRAY_COUNT(EFFECTS_ARR( __VA_ARGS__ )), 15)) << 28) + (uintptr_t)(EFFECTS_ARR( __VA_ARGS__ ))

// Retrieve a move's additional effects and the count thereof
#define GET_ADDITIONAL_EFFECTS(move) (void *)(gMovesInfo[move].additionalEffects & 0x8FFFFFF)
#define GET_ADDITIONAL_EFFECT_COUNT(move) (gMovesInfo[move].additionalEffects >> 28)
#define GET_ADDITIONAL_EFFECTS_AND_COUNT(move, _count, _effects) u32 _count = GET_ADDITIONAL_EFFECT_COUNT(move); const struct AdditionalEffect *_effects = GET_ADDITIONAL_EFFECTS(move)
#define ADDITIONAL_EFFECTS(...) EFFECTS_ARR( __VA_ARGS__ ), .numAdditionalEffects = ARRAY_COUNT(EFFECTS_ARR( __VA_ARGS__ ))

// Just a hack to make a move boosted by Sheer Force despite having no secondary effects affected
#define SHEER_FORCE_HACK { .moveEffect = 0, .chance = 100, }
Expand Down
27 changes: 11 additions & 16 deletions src/battle_ai_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3149,9 +3149,6 @@ static u32 AI_CalcMoveScore(u32 battlerAtk, u32 battlerDef, u32 move)
u32 movesetIndex = AI_THINKING_STRUCT->movesetIndex;
u32 effectiveness = aiData->effectiveness[battlerAtk][battlerDef][movesetIndex];

// additional effects
GET_ADDITIONAL_EFFECTS_AND_COUNT(move, additionalEffectsCount, additionalEffects);

s32 score = 0;
u32 predictedMove = aiData->predictedMoves[battlerDef];
u32 predictedMoveSlot = GetMoveSlot(GetMovesArray(battlerDef), predictedMove);
Expand Down Expand Up @@ -4381,19 +4378,19 @@ static u32 AI_CalcMoveScore(u32 battlerAtk, u32 battlerDef, u32 move)
} // move effect checks

// check move additional effects that are likely to happen
for (i = 0; i < additionalEffectsCount; i++)
for (i = 0; i < gMovesInfo[move].numAdditionalEffects; i++)
{
// Only consider effects with a guaranteed chance to happen
if (!MoveEffectIsGuaranteed(battlerAtk, aiData->abilities[battlerAtk], &additionalEffects[i]))
if (!MoveEffectIsGuaranteed(battlerAtk, aiData->abilities[battlerAtk], &gMovesInfo[move].additionalEffects[i]))
continue;

// Consider move effects that target self
if (additionalEffects[i].self)
if (gMovesInfo[move].additionalEffects[i].self)
{
u32 oneStageStatId = STAT_CHANGE_ATK + additionalEffects[i].moveEffect - MOVE_EFFECT_ATK_PLUS_1;
u32 twoStageStatId = STAT_CHANGE_ATK_2 + additionalEffects[i].moveEffect - MOVE_EFFECT_ATK_PLUS_1;
u32 oneStageStatId = STAT_CHANGE_ATK + gMovesInfo[move].additionalEffects[i].moveEffect - MOVE_EFFECT_ATK_PLUS_1;
u32 twoStageStatId = STAT_CHANGE_ATK_2 + gMovesInfo[move].additionalEffects[i].moveEffect - MOVE_EFFECT_ATK_PLUS_1;

switch (additionalEffects[i].moveEffect)
switch (gMovesInfo[move].additionalEffects[i].moveEffect)
{
case MOVE_EFFECT_SPD_PLUS_2:
case MOVE_EFFECT_SPD_PLUS_1:
Expand Down Expand Up @@ -4444,7 +4441,7 @@ static u32 AI_CalcMoveScore(u32 battlerAtk, u32 battlerDef, u32 move)
}
else // consider move effects that hinder the target
{
switch (additionalEffects[i].moveEffect)
switch (gMovesInfo[move].additionalEffects[i].moveEffect)
{
case MOVE_EFFECT_FLINCH:
score += ShouldTryToFlinch(battlerAtk, battlerDef, aiData->abilities[battlerAtk], aiData->abilities[battlerDef], move);
Expand Down Expand Up @@ -4736,10 +4733,9 @@ static s32 AI_SetupFirstTurn(u32 battlerAtk, u32 battlerDef, u32 move, s32 score
{
// TEMPORARY - should applied to all moves regardless of EFFECT
// Consider move effects
GET_ADDITIONAL_EFFECTS_AND_COUNT(move, additionalEffectsCount, additionalEffects);
for (i = 0; i < additionalEffectsCount; i++)
for (i = 0; i < gMovesInfo[move].numAdditionalEffects; i++)
{
switch (additionalEffects[i].moveEffect)
switch (gMovesInfo[move].additionalEffects[i].moveEffect)
{
case MOVE_EFFECT_STEALTH_ROCK:
case MOVE_EFFECT_SPIKES:
Expand Down Expand Up @@ -4793,10 +4789,9 @@ static s32 AI_Risky(u32 battlerAtk, u32 battlerDef, u32 move, s32 score)
{
// TEMPORARY - should applied to all moves regardless of EFFECT
// Consider move effects
GET_ADDITIONAL_EFFECTS_AND_COUNT(move, additionalEffectsCount, additionalEffects);
for (i = 0; i < additionalEffectsCount; i++)
for (i = 0; i < gMovesInfo[move].numAdditionalEffects; i++)
{
switch (additionalEffects[i].moveEffect)
switch (gMovesInfo[move].additionalEffects[i].moveEffect)
{
case MOVE_EFFECT_ALL_STATS_UP:
if (Random() & 1)
Expand Down
23 changes: 10 additions & 13 deletions src/battle_ai_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,6 @@ static bool32 AI_IsMoveEffectInPlus(u32 battlerAtk, u32 battlerDef, u32 move, s3
u32 i;
u32 abilityDef = AI_DATA->abilities[battlerDef];
u32 abilityAtk = AI_DATA->abilities[battlerAtk];
GET_ADDITIONAL_EFFECTS_AND_COUNT(move, additionalEffectsCount, additionalEffects);

switch (gMovesInfo[move].effect)
{
Expand All @@ -611,12 +610,12 @@ static bool32 AI_IsMoveEffectInPlus(u32 battlerAtk, u32 battlerDef, u32 move, s3
}

// check ADDITIONAL_EFFECTS
for (i = 0; i < additionalEffectsCount; i++)
for (i = 0; i < gMovesInfo[move].numAdditionalEffects; i++)
{
// Consider move effects that target self
if (additionalEffects[i].self)
if (gMovesInfo[move].additionalEffects[i].self)
{
switch (additionalEffects[i].moveEffect)
switch (gMovesInfo[move].additionalEffects[i].moveEffect)
{
case MOVE_EFFECT_ATK_PLUS_1:
if (BattlerStatCanRise(battlerAtk, abilityAtk, STAT_ATK))
Expand Down Expand Up @@ -646,7 +645,7 @@ static bool32 AI_IsMoveEffectInPlus(u32 battlerAtk, u32 battlerDef, u32 move, s3
}
else // consider move effects that hinder the target
{
switch (additionalEffects[i].moveEffect)
switch (gMovesInfo[move].additionalEffects[i].moveEffect)
{
case MOVE_EFFECT_POISON:
case MOVE_EFFECT_TOXIC:
Expand Down Expand Up @@ -680,7 +679,7 @@ static bool32 AI_IsMoveEffectInPlus(u32 battlerAtk, u32 battlerDef, u32 move, s3
case MOVE_EFFECT_SP_DEF_MINUS_1:
case MOVE_EFFECT_ACC_MINUS_1:
case MOVE_EFFECT_EVS_MINUS_1:
if (ShouldLowerStat(battlerDef, abilityDef, STAT_ATK + (additionalEffects[i].moveEffect - MOVE_EFFECT_ATK_MINUS_1)) && noOfHitsToKo != 1)
if (ShouldLowerStat(battlerDef, abilityDef, STAT_ATK + (gMovesInfo[move].additionalEffects[i].moveEffect - MOVE_EFFECT_ATK_MINUS_1)) && noOfHitsToKo != 1)
return TRUE;
break;
case MOVE_EFFECT_ATK_MINUS_2:
Expand All @@ -690,7 +689,7 @@ static bool32 AI_IsMoveEffectInPlus(u32 battlerAtk, u32 battlerDef, u32 move, s3
case MOVE_EFFECT_SP_DEF_MINUS_2:
case MOVE_EFFECT_ACC_MINUS_2:
case MOVE_EFFECT_EVS_MINUS_2:
if (ShouldLowerStat(battlerDef, abilityDef, STAT_ATK + (additionalEffects[i].moveEffect - MOVE_EFFECT_ATK_MINUS_2)) && noOfHitsToKo != 1)
if (ShouldLowerStat(battlerDef, abilityDef, STAT_ATK + (gMovesInfo[move].additionalEffects[i].moveEffect - MOVE_EFFECT_ATK_MINUS_1)) && noOfHitsToKo != 1)
return TRUE;
break;
}
Expand Down Expand Up @@ -721,11 +720,9 @@ static bool32 AI_IsMoveEffectInMinus(u32 battlerAtk, u32 battlerDef, u32 move, s
break;
default:
{
GET_ADDITIONAL_EFFECTS_AND_COUNT(move, additionalEffectsCount, additionalEffects);

for (i = 0; i < additionalEffectsCount; i++)
for (i = 0; i < gMovesInfo[move].numAdditionalEffects; i++)
{
switch (additionalEffects[i].moveEffect)
switch (gMovesInfo[move].additionalEffects[i].moveEffect)
{
case MOVE_EFFECT_ATK_MINUS_1:
case MOVE_EFFECT_DEF_MINUS_1:
Expand All @@ -737,12 +734,12 @@ static bool32 AI_IsMoveEffectInMinus(u32 battlerAtk, u32 battlerDef, u32 move, s
case MOVE_EFFECT_DEF_SPDEF_DOWN:
case MOVE_EFFECT_SP_DEF_MINUS_1:
case MOVE_EFFECT_SP_DEF_MINUS_2:
if ((additionalEffects[i].self && GetBattlerAbility(battlerAtk) != ABILITY_CONTRARY)
if ((gMovesInfo[move].additionalEffects[i].self && GetBattlerAbility(battlerAtk) != ABILITY_CONTRARY)
|| (noOfHitsToKo != 1 && abilityDef == ABILITY_CONTRARY && !IsMoldBreakerTypeAbility(abilityAtk)))
return TRUE;
break;
case MOVE_EFFECT_RECHARGE:
return additionalEffects[i].self;
return gMovesInfo[move].additionalEffects[i].self;
}
}
break;
Expand Down
21 changes: 10 additions & 11 deletions src/battle_script_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -3858,21 +3858,21 @@ static void Cmd_setadditionaleffects(void)

if (!(gMoveResultFlags & MOVE_RESULT_NO_EFFECT))
{
GET_ADDITIONAL_EFFECTS_AND_COUNT(gCurrentMove, additionalEffectsCount, additionalEffects);
if (additionalEffectsCount > gBattleStruct->additionalEffectsCounter)
if (gMovesInfo[gCurrentMove].numAdditionalEffects > gBattleStruct->additionalEffectsCounter)
{
u32 percentChance, i = gBattleStruct->additionalEffectsCounter;
u32 percentChance;
const struct AdditionalEffect *additionalEffect = &gMovesInfo[gCurrentMove].additionalEffects[gBattleStruct->additionalEffectsCounter];
const u8 *currentPtr = gBattlescriptCurrInstr;

// Various checks for if this move effect can be applied this turn
if (CanApplyAdditionalEffect(&additionalEffects[i]))
if (CanApplyAdditionalEffect(additionalEffect))
{
percentChance = CalcSecondaryEffectChance(gBattlerAttacker, GetBattlerAbility(gBattlerAttacker), &additionalEffects[i]);
percentChance = CalcSecondaryEffectChance(gBattlerAttacker, GetBattlerAbility(gBattlerAttacker), additionalEffect);

// Activate effect if it's primary (chance == 0) or if RNGesus says so
if ((percentChance == 0) || RandomPercentage(RNG_SECONDARY_EFFECT + (i % 3), percentChance))
if ((percentChance == 0) || RandomPercentage(RNG_SECONDARY_EFFECT + gBattleStruct->additionalEffectsCounter, percentChance))
{
gBattleScripting.moveEffect = additionalEffects[i].moveEffect | (MOVE_EFFECT_AFFECTS_USER * (additionalEffects[i].self));
gBattleScripting.moveEffect = additionalEffect->moveEffect | (MOVE_EFFECT_AFFECTS_USER * (additionalEffect->self));

SetMoveEffect(
percentChance == 0, // a primary effect
Expand All @@ -3887,7 +3887,7 @@ static void Cmd_setadditionaleffects(void)

// Call setadditionaleffects again in the case of a move with multiple effects
gBattleStruct->additionalEffectsCounter++;
if (additionalEffectsCount > gBattleStruct->additionalEffectsCounter)
if (gMovesInfo[gCurrentMove].numAdditionalEffects > gBattleStruct->additionalEffectsCounter)
gBattleScripting.moveEffect = MOVE_EFFECT_CONTINUE;
else
gBattleScripting.moveEffect = gBattleStruct->additionalEffectsCounter = 0;
Expand Down Expand Up @@ -15756,13 +15756,12 @@ static bool8 CanAbilityPreventStatLoss(u16 abilityDef, bool8 byIntimidate)
static bool8 CanBurnHitThaw(u16 move)
{
u8 i;
GET_ADDITIONAL_EFFECTS_AND_COUNT(move, additionalEffectsCount, additionalEffects);

if (B_BURN_HIT_THAW >= GEN_6)
{
for (i = 0; i < additionalEffectsCount; i++)
for (i = 0; i < gMovesInfo[move].numAdditionalEffects; i++)
{
if (additionalEffects[i].moveEffect == MOVE_EFFECT_BURN)
if (gMovesInfo[move].additionalEffects[i].moveEffect == MOVE_EFFECT_BURN)
return TRUE;
}
}
Expand Down
60 changes: 39 additions & 21 deletions src/battle_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -11078,35 +11078,41 @@ bool32 IsGen6ExpShareEnabled(void)
return FlagGet(I_EXP_SHARE_FLAG);
}

#define R_CHECK_MOVE_EFFECT(...) RECURSIVELY(R_FOR_EACH(CHECK_MOVE_EFFECT, __VA_ARGS__))
#define CHECK_MOVE_EFFECT(_condition) && effects[i]._condition

/* Quick way of checking if a move has move effects with match a few
comma-separated conditions. Each condition has to check a field of AdditionalEffect. */
#define RETURN_HAS_MOVE_ADDITIONAL_EFFECT(condition1, ...) \
u32 i; \
GET_ADDITIONAL_EFFECTS_AND_COUNT(move, count, effects); \
for (i = 0; i < count; i++) \
{ \
if (effects[i].condition1 R_CHECK_MOVE_EFFECT(__VA_ARGS__)) \
return TRUE; \
} \
return FALSE;


bool32 MoveHasAdditionalEffect(u32 move, u32 moveEffect)
{
RETURN_HAS_MOVE_ADDITIONAL_EFFECT(moveEffect == moveEffect, self == FALSE)
u32 i;
for (i = 0; i < gMovesInfo[move].numAdditionalEffects; i++)
{
if (gMovesInfo[move].additionalEffects[i].moveEffect == moveEffect
&& gMovesInfo[move].additionalEffects[i].self == FALSE)
return TRUE;
}
return FALSE;
}

bool32 MoveHasAdditionalEffectWithChance(u32 move, u32 moveEffect, u32 chance)
{
RETURN_HAS_MOVE_ADDITIONAL_EFFECT(moveEffect == moveEffect, chance == chance)
u32 i;
for (i = 0; i < gMovesInfo[move].numAdditionalEffects; i++)
{
if (gMovesInfo[move].additionalEffects[i].moveEffect == moveEffect
&& gMovesInfo[move].additionalEffects[i].chance == chance)
return TRUE;
}
return FALSE;
}

bool32 MoveHasAdditionalEffectSelf(u32 move, u32 moveEffect)
{
RETURN_HAS_MOVE_ADDITIONAL_EFFECT(moveEffect == moveEffect, self == TRUE)
u32 i;
for (i = 0; i < gMovesInfo[move].numAdditionalEffects; i++)
{
if (gMovesInfo[move].additionalEffects[i].moveEffect == moveEffect
&& gMovesInfo[move].additionalEffects[i].self == TRUE)
return TRUE;
}
return FALSE;
}

bool32 MoveHasAdditionalEffectSelfArg(u32 move, u32 moveEffect, u32 argument)
Expand All @@ -11116,12 +11122,24 @@ bool32 MoveHasAdditionalEffectSelfArg(u32 move, u32 moveEffect, u32 argument)

bool32 MoveHasChargeTurnAdditionalEffect(u32 move)
{
RETURN_HAS_MOVE_ADDITIONAL_EFFECT(onChargeTurnOnly == TRUE)
u32 i;
for (i = 0; i < gMovesInfo[move].numAdditionalEffects; i++)
{
if (gMovesInfo[move].additionalEffects[i].onChargeTurnOnly)
return TRUE;
}
return FALSE;
}

bool32 MoveIsAffectedBySheerForce(u16 move)
bool32 MoveIsAffectedBySheerForce(u32 move)
{
RETURN_HAS_MOVE_ADDITIONAL_EFFECT(chance > 0)
u32 i;
for (i = 0; i < gMovesInfo[move].numAdditionalEffects; i++)
{
if (gMovesInfo[move].additionalEffects[i].chance > 0)
return TRUE;
}
return FALSE;
}

bool8 CanMonParticipateInSkyBattle(struct Pokemon *mon)
Expand Down

0 comments on commit b5af343

Please sign in to comment.