Skip to content

Commit

Permalink
Add global constants for damage roll related code and make AverageRol…
Browse files Browse the repository at this point in the history
…lDmg faster (#4663)

* faster AverageRollDmg, global dmg roll constants

* restore MAX_ROLL_PERCENTAGE & MIN_ROLL_PERCENTAGE

* rename AverageRollDmg
  • Loading branch information
Sneed69 authored May 31, 2024
1 parent 810caa7 commit e869aaf
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions include/battle_ai_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#define FOE(battler) ((BATTLE_OPPOSITE(battler)) & BIT_SIDE)

// Roll boundaries used by AI when scoring. Doesn't affect actual damage dealt.
#define MAX_ROLL_PERCENTAGE 100
#define MIN_ROLL_PERCENTAGE 85
#define MAX_ROLL_PERCENTAGE DMG_ROLL_PERCENT_HI
#define MIN_ROLL_PERCENTAGE DMG_ROLL_PERCENT_LO

enum
{
Expand Down
4 changes: 4 additions & 0 deletions include/battle_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ enum {
#define IS_WHOLE_SIDE_ALIVE(battler) ((IsBattlerAlive(battler) && IsBattlerAlive(BATTLE_PARTNER(battler))))
#define IS_ALIVE_AND_PRESENT(battler) (IsBattlerAlive(battler) && IsBattlerSpritePresent(battler))

// Lowest and highest percentages used for damage roll calculations
#define DMG_ROLL_PERCENT_LO 85
#define DMG_ROLL_PERCENT_HI 100

// for Natural Gift and Fling
struct TypePower
{
Expand Down
8 changes: 4 additions & 4 deletions src/battle_ai_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,9 @@ static inline s32 HighestRollDmg(s32 dmg)
return dmg;
}

static inline s32 AverageRollDmg(s32 dmg)
static inline s32 AverageDmg(s32 dmg)
{
dmg = ((HighestRollDmg(dmg) + LowestRollDmg(dmg)) * 100) / 2;
dmg = (dmg * (MIN_ROLL_PERCENTAGE + MAX_ROLL_PERCENTAGE)) / 2;
dmg /= 100;
return dmg;
}
Expand Down Expand Up @@ -559,7 +559,7 @@ s32 AI_CalcDamage(u32 move, u32 battlerAtk, u32 battlerDef, u8 *typeEffectivenes
u32 critChance = GetCritHitChance(critChanceIndex);
// With critChance getting closer to 1, dmg gets closer to critDmg.
if (dmgRoll == DMG_ROLL_AVERAGE)
dmg = AverageRollDmg((critDmg + normalDmg * (critChance - 1)) / (critChance));
dmg = AverageDmg((critDmg + normalDmg * (critChance - 1)) / (critChance));
else if (dmgRoll == DMG_ROLL_HIGHEST)
dmg = HighestRollDmg((critDmg + normalDmg * (critChance - 1)) / (critChance));
else
Expand All @@ -568,7 +568,7 @@ s32 AI_CalcDamage(u32 move, u32 battlerAtk, u32 battlerDef, u8 *typeEffectivenes
else
{
if (dmgRoll == DMG_ROLL_AVERAGE)
dmg = AverageRollDmg(normalDmg);
dmg = AverageDmg(normalDmg);
else if (dmgRoll == DMG_ROLL_HIGHEST)
dmg = HighestRollDmg(normalDmg);
else
Expand Down
4 changes: 2 additions & 2 deletions src/battle_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -9994,7 +9994,7 @@ static inline s32 DoMoveDamageCalcVars(u32 move, u32 battlerAtk, u32 battlerDef,

if (randomFactor)
{
dmg *= 100 - RandomUniform(RNG_DAMAGE_MODIFIER, 0, 15);
dmg *= DMG_ROLL_PERCENT_HI - RandomUniform(RNG_DAMAGE_MODIFIER, 0, DMG_ROLL_PERCENT_HI - DMG_ROLL_PERCENT_LO);
dmg /= 100;
}

Expand Down Expand Up @@ -10055,7 +10055,7 @@ static inline s32 DoFutureSightAttackDamageCalcVars(u32 move, u32 battlerAtk, u3

if (randomFactor)
{
dmg *= 100 - RandomUniform(RNG_DAMAGE_MODIFIER, 0, 15);
dmg *= DMG_ROLL_PERCENT_HI - RandomUniform(RNG_DAMAGE_MODIFIER, 0, DMG_ROLL_PERCENT_HI - DMG_ROLL_PERCENT_LO);
dmg /= 100;
}

Expand Down

0 comments on commit e869aaf

Please sign in to comment.