From 0ee13418dc5c8867673fecab6e61c40fc6b78243 Mon Sep 17 00:00:00 2001 From: sbird Date: Wed, 22 Sep 2021 13:15:43 +0200 Subject: [PATCH] [ai] use expected value move dmg calculation --- include/battle_script_commands.h | 1 + src/battle_ai_util.c | 16 +++++++++++++--- src/battle_script_commands.c | 9 +++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/include/battle_script_commands.h b/include/battle_script_commands.h index 2e6a0aa4eb..dec1596f21 100644 --- a/include/battle_script_commands.h +++ b/include/battle_script_commands.h @@ -13,6 +13,7 @@ struct StatFractions }; s32 CalcCritChanceStage(u8 battlerAtk, u8 battlerDef, u32 move, bool32 recordAbility); +s8 GetInverseCritChance(u8 battlerAtk, u8 battlerDef, u32 move); u32 GetTotalAccuracy(u32 battlerAtk, u32 battlerDef, u32 move); u8 GetBattlerTurnOrderNum(u8 battlerId); bool32 NoAliveMonsForEitherParty(void); diff --git a/src/battle_ai_util.c b/src/battle_ai_util.c index 48d3817c8e..b68c68e1bf 100644 --- a/src/battle_ai_util.c +++ b/src/battle_ai_util.c @@ -711,7 +711,8 @@ static bool32 AI_GetIfCrit(u32 move, u8 battlerAtk, u8 battlerDef) s32 AI_CalcDamage(u16 move, u8 battlerAtk, u8 battlerDef) { - s32 dmg, moveType; + s32 dmg, moveType, critDmg, normalDmg; + s8 critChance; SaveBattlerData(battlerAtk); SaveBattlerData(battlerDef); @@ -722,7 +723,15 @@ s32 AI_CalcDamage(u16 move, u8 battlerAtk, u8 battlerDef) gBattleStruct->dynamicMoveType = 0; SetTypeBeforeUsingMove(move, battlerAtk); GET_MOVE_TYPE(move, moveType); - dmg = CalculateMoveDamage(move, battlerAtk, battlerDef, moveType, 0, AI_GetIfCrit(move, battlerAtk, battlerDef), FALSE, FALSE); + + critChance = GetInverseCritChance(battlerAtk, battlerDef, move); + normalDmg = CalculateMoveDamage(move, battlerAtk, battlerDef, moveType, 0, FALSE, FALSE, FALSE); + critDmg = CalculateMoveDamage(move, battlerAtk, battlerDef, moveType, 0, TRUE, FALSE, FALSE); + + if(critChance == -1) + dmg = normalDmg; + else + dmg = (critDmg + normalDmg * (critChance - 1)) / critChance; // handle dynamic move damage switch (gBattleMoves[move].effect) @@ -749,7 +758,8 @@ s32 AI_CalcDamage(u16 move, u8 battlerAtk, u8 battlerDef) //case EFFECT_METAL_BURST: //case EFFECT_COUNTER: default: - dmg *= (100 - (Random() % 10)) / 100; // add random factor + //do not add the random factor, it's an average case analysis + //dmg *= (100 - (Random() % 10)) / 100; // add random factor break; } diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index e1d51bd05e..a10635e1a8 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -1822,6 +1822,15 @@ s32 CalcCritChanceStage(u8 battlerAtk, u8 battlerDef, u32 move, bool32 recordAbi return critChance; } +s8 GetInverseCritChance(u8 battlerAtk, u8 battlerDef, u32 move) +{ + s32 critChanceIndex = CalcCritChanceStage(battlerAtk, battlerDef, move, FALSE); + if(critChanceIndex < 0) + return -1; + else + return sCriticalHitChance[critChanceIndex]; +} + static void Cmd_critcalc(void) { s32 critChance = CalcCritChanceStage(gBattlerAttacker, gBattlerTarget, gCurrentMove, TRUE);