From 48da191dfbae6e793f52024b27710d9838d7ee6c Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 12 Feb 2022 17:00:37 +0100 Subject: [PATCH] Adds a bunch of script hooks to the damage library calculations --- src/Battling/Library/DamageLibrary.cpp | 34 +++++++------------ src/Battling/PkmnScript.hpp | 7 +++- .../AngelScript/AngelScriptScript.cpp | 21 ++++++++++++ .../AngelScript/AngelScriptScript.hpp | 5 +++ .../AngelScript/AngelScriptTypeInfo.hpp | 7 ++++ .../TypeRegistry/BasicScriptClass.cpp | 3 ++ 6 files changed, 55 insertions(+), 22 deletions(-) diff --git a/src/Battling/Library/DamageLibrary.cpp b/src/Battling/Library/DamageLibrary.cpp index 70877e9..8ca2aba 100644 --- a/src/Battling/Library/DamageLibrary.cpp +++ b/src/Battling/Library/DamageLibrary.cpp @@ -1,11 +1,8 @@ #include "DamageLibrary.hpp" -#include #include "../PkmnScriptHook.hpp" -#include "../Pokemon/Pokemon.hpp" using HitData = const CreatureLib::Battling::ExecutingAttack::HitData; -inline constexpr float to_f(i32 a) { return static_cast(a); } inline constexpr float fl(float a) { return std::floor(a); } uint32_t PkmnLib::Battling::DamageLibrary::GetDamage(CreatureLib::Battling::ExecutingAttack* attack, @@ -14,8 +11,8 @@ uint32_t PkmnLib::Battling::DamageLibrary::GetDamage(CreatureLib::Battling::Exec auto levelMod = fl((2 * attack->GetUser()->GetLevel()) / 5.0f) + 2; auto bp = hitData.GetBasePower(); auto statMod = GetStatModifier(attack, target, hitIndex, hitData); + PKMN_HOOK(ModifyStatModifier, attack, attack, target, hitIndex, &statMod); auto damageMod = GetDamageModifier(attack, target, hitIndex, hitData); - // HOOK: Modify stat modifier auto floatDamage = fl(levelMod * bp); floatDamage = fl(floatDamage * statMod); @@ -70,10 +67,10 @@ uint8_t PkmnLib::Battling::DamageLibrary::GetBasePower(CreatureLib::Battling::Ex return bp; } float PkmnLib::Battling::DamageLibrary::GetStatModifier(CreatureLib::Battling::ExecutingAttack* attack, - CreatureLib::Battling::Creature* target, uint8_t, + CreatureLib::Battling::Creature* target, uint8_t hit, const HitData& hitData) const { - auto user = attack->GetUser(); - // HOOK: allow overriding for which users stat we use. + auto* user = attack->GetUser().GetRaw(); + PKMN_HOOK(ChangeDamageStatsUser, attack, attack, target, hit, &user); CreatureLib::Library::Statistic offensiveStat; CreatureLib::Library::Statistic defensiveStat; auto moveData = attack->GetUseAttack().ForceAs(); @@ -86,23 +83,18 @@ float PkmnLib::Battling::DamageLibrary::GetStatModifier(CreatureLib::Battling::E } auto bypassDefensive = hitData.IsCritical() && target->GetStatBoost(defensiveStat) > 0; - // HOOK: allow bypassing defensive stat modifiers. + PKMN_HOOK(BypassDefensiveStat, attack, attack, target, hit, &bypassDefensive); auto bypassOffensive = hitData.IsCritical() && user->GetStatBoost(offensiveStat) < 0; - // HOOK: Allow bypassing offensive stat modifiers. + PKMN_HOOK(BypassOffensiveStat, attack, attack, target, hit, &bypassOffensive); - float offensiveValue; - float defensiveValue; + float offensiveValue = + bypassOffensive ? (float)user->GetFlatStat(offensiveStat) : (float)user->GetBoostedStat(offensiveStat); + float defensiveValue = + bypassDefensive ? (float)target->GetFlatStat(defensiveStat) : (float)target->GetBoostedStat(defensiveStat); + + PKMN_HOOK(ModifyOffensiveStatValue, attack, attack, target, hit, &offensiveValue); + PKMN_HOOK(ModifyDefensiveStatValue, attack, attack, target, hit, &defensiveValue); - if (bypassOffensive) { - offensiveValue = user->GetFlatStat(offensiveStat); - } else { - offensiveValue = user->GetBoostedStat(offensiveStat); - } - if (bypassDefensive) { - defensiveValue = target->GetFlatStat(defensiveStat); - } else { - defensiveValue = target->GetBoostedStat(defensiveStat); - } return offensiveValue / defensiveValue; } diff --git a/src/Battling/PkmnScript.hpp b/src/Battling/PkmnScript.hpp index 6805c90..6af72d6 100644 --- a/src/Battling/PkmnScript.hpp +++ b/src/Battling/PkmnScript.hpp @@ -17,7 +17,7 @@ namespace PkmnLib::Battling { PkmnScript(const ArbUt::OptionalBorrowedPtr& owner) : CreatureLib::Battling::BattleScript(owner) {} virtual void PreventIncomingCritical(CreatureLib::Battling::ExecutingAttack* attack, - CreatureLib::Battling::Creature* target, uint8_t hit, bool* preventCrit){}; + CreatureLib::Battling::Creature* target, uint8_t hit, bool* preventCrit){}; virtual void ModifyCriticalStage(CreatureLib::Battling::ExecutingAttack* attack, CreatureLib::Battling::Creature* target, uint8_t hit, uint8_t* critStage){}; virtual void OverrideCriticalModifier(CreatureLib::Battling::ExecutingAttack* attack, @@ -31,6 +31,11 @@ namespace PkmnLib::Battling { virtual void DoesShareExperience(CreatureLib::Battling::Creature* faintedMon, CreatureLib::Battling::Creature* winningMon, bool* shareExperience){}; virtual void BlockWeather(CreatureLib::Battling::Battle* battle, bool* blockWeather){}; + + virtual void ModifyOffensiveStatValue(CreatureLib::Battling::ExecutingAttack* attack, + CreatureLib::Battling::Creature* target, u8 hitIndex, float* modifier){}; + virtual void ModifyDefensiveStatValue(CreatureLib::Battling::ExecutingAttack* attack, + CreatureLib::Battling::Creature* target, u8 hitIndex, float* modifier){}; }; } diff --git a/src/ScriptResolving/AngelScript/AngelScriptScript.cpp b/src/ScriptResolving/AngelScript/AngelScriptScript.cpp index e2b7404..9feacc8 100644 --- a/src/ScriptResolving/AngelScript/AngelScriptScript.cpp +++ b/src/ScriptResolving/AngelScript/AngelScriptScript.cpp @@ -411,3 +411,24 @@ void AngelScriptScript::OnFaintingOpponent(const CreatureLib::Battling::Executin ctx->SetArgByte(2, hitNumber); }) } +void AngelScriptScript::ModifyOffensiveStatValue(CreatureLib::Battling::ExecutingAttack* attack, + CreatureLib::Battling::Creature* target, u8 hitIndex, + float* modifier) { + CALL_HOOK(ModifyOffensiveStatValue, { + ctx->SetArgObject(0, (void*)attack); + ctx->SetArgObject(1, (void*)target); + ctx->SetArgByte(2, hitIndex); + ctx->SetArgAddress(3, modifier); + }) + +} +void AngelScriptScript::ModifyDefensiveStatValue(CreatureLib::Battling::ExecutingAttack* attack, + CreatureLib::Battling::Creature* target, u8 hitIndex, + float* modifier) { + CALL_HOOK(ModifyDefensiveStatValue, { + ctx->SetArgObject(0, (void*)attack); + ctx->SetArgObject(1, (void*)target); + ctx->SetArgByte(2, hitIndex); + ctx->SetArgAddress(3, modifier); + }) +} diff --git a/src/ScriptResolving/AngelScript/AngelScriptScript.hpp b/src/ScriptResolving/AngelScript/AngelScriptScript.hpp index 24e64a4..af3d4cf 100644 --- a/src/ScriptResolving/AngelScript/AngelScriptScript.hpp +++ b/src/ScriptResolving/AngelScript/AngelScriptScript.hpp @@ -152,6 +152,11 @@ public: bool* shareExperience) override; void BlockWeather(CreatureLib::Battling::Battle* battle, bool* blockWeather) override; + + void ModifyOffensiveStatValue(CreatureLib::Battling::ExecutingAttack* attack, + CreatureLib::Battling::Creature* target, u8 hitIndex, float* modifier) override; + void ModifyDefensiveStatValue(CreatureLib::Battling::ExecutingAttack* attack, + CreatureLib::Battling::Creature* target, u8 hitIndex, float* modifier) override; }; #undef CALL_HOOK diff --git a/src/ScriptResolving/AngelScript/AngelScriptTypeInfo.hpp b/src/ScriptResolving/AngelScript/AngelScriptTypeInfo.hpp index 5f30073..96614bb 100644 --- a/src/ScriptResolving/AngelScript/AngelScriptTypeInfo.hpp +++ b/src/ScriptResolving/AngelScript/AngelScriptTypeInfo.hpp @@ -182,6 +182,13 @@ public: SCRIPT_HOOK_FUNCTION(OnFaint, "void OnFaint(Pokemon@ pokemon, DamageSource damageSource)"); SCRIPT_HOOK_FUNCTION(BlockWeather, "void BlockWeather(Battle@ battle, bool& result)"); SCRIPT_HOOK_FUNCTION(OnSwitchIn, "void OnSwitchIn(Pokemon@ pokemon)"); + + SCRIPT_HOOK_FUNCTION( + ModifyOffensiveStatValue, + "void ModifyOffensiveStatValue(ExecutingMove@ attack, Pokemon@ target, uint8 hit, float& offensiveStatValue)"); + SCRIPT_HOOK_FUNCTION( + ModifyDefensiveStatValue, + "void ModifyDefensiveStatValue(ExecutingMove@ attack, Pokemon@ target, uint8 hit, float& defensiveStatValue)"); }; #undef SCRIPT_HOOK_FUNCTION diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/BasicScriptClass.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/BasicScriptClass.cpp index 07de88c..5ed07b9 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/BasicScriptClass.cpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/BasicScriptClass.cpp @@ -69,6 +69,9 @@ shared abstract class PkmnScript { void DoesShareExperience(Pokemon@ faintedMon, Pokemon@ winningMon, bool& shareExperience){}; void BlockWeather(Battle@ battle, bool& blockWeather){}; void OnSwitchIn(Pokemon@ pokemon){}; + + void ModifyOffensiveStatValue(ExecutingMove@ attack, Pokemon@ target, uint8 hit, float& offensiveStatValue){}; + void ModifyDefensiveStatValue(ExecutingMove@ attack, Pokemon@ target, uint8 hit, float& defensiveStatValue){}; } )"); Ensure(r >= 0);