Adds script hooks, bug fix for empty ScriptIterator

This commit is contained in:
2024-07-28 11:46:36 +02:00
parent 74996d96b0
commit cb2b566388
5 changed files with 495 additions and 19 deletions

View File

@@ -1,5 +1,4 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using PkmnLib.Dynamic.Libraries;
using PkmnLib.Dynamic.Models;
@@ -23,7 +22,7 @@ public class Gen7DamageCalculator(bool hasRandomness) : IDamageCalculator
var levelModifier = (2.0f * executingMove.User.Level) / 5.0f + 2.0f;
var basePower = (float)hitData.BasePower;
var statModifier = GetStatModifier(executingMove, target, hitNumber, hitData);
var damageModifier = GetDamageModifier(executingMove, target, hitNumber, hitData);
var damageModifier = GetDamageModifier(executingMove, target, hitNumber);
var floatDamage = MathF.Floor(levelModifier * basePower);
floatDamage = MathF.Floor(floatDamage * statModifier);
@@ -35,7 +34,8 @@ public class Gen7DamageCalculator(bool hasRandomness) : IDamageCalculator
if (hitData.IsCritical)
{
var critModifier = 1.5f;
// TODO: script hook to change the critical modifier
executingMove.RunScriptHook(script =>
script.ChangeCriticalModifier(executingMove, target, hitNumber, ref critModifier));
floatDamage = MathF.Floor(floatDamage * critModifier);
}
@@ -64,8 +64,10 @@ public class Gen7DamageCalculator(bool hasRandomness) : IDamageCalculator
< 1 => 1,
_ => (uint)floatDamage
};
// TODO: script hook to modify the damage
// TODO: script hook to modify incoming damage
executingMove.RunScriptHook(script =>
script.ChangeDamage(executingMove, target, hitNumber, ref damage));
target.RunScriptHook(script =>
script.ChangeIncomingDamage(executingMove, target, hitNumber, ref damage));
return damage;
}
@@ -76,18 +78,19 @@ public class Gen7DamageCalculator(bool hasRandomness) : IDamageCalculator
if (executingMove.UseMove.Category == MoveCategory.Status)
return 0;
var basePower = hitData.BasePower;
// TODO: script hook to modify the base power
executingMove.RunScriptHook(script =>
script.ChangeBasePower(executingMove, target, hitNumber, ref basePower));
return basePower;
}
/// <inheritdoc />
[SuppressMessage("ReSharper", "UnreachableSwitchArmDueToIntegerAnalysis")] // disabled because of the TODO
public bool IsCritical(IBattle battle, IExecutingMove executingMove, IPokemon target, byte hitNumber)
{
if (executingMove.UseMove.Category == MoveCategory.Status)
return false;
byte critStage = 0;
// TODO: script hook to modify the crit stage
executingMove.RunScriptHook(script =>
script.ChangeCriticalStage(executingMove, target, hitNumber, ref critStage));
var random = battle.Random;
return critStage switch
@@ -115,13 +118,15 @@ public class Gen7DamageCalculator(bool hasRandomness) : IDamageCalculator
// move is critical, and the target has a defensive stat boost of > 0, but a script is
// allowed to change this.
var bypassDefense = hitData.IsCritical && target.StatBoost.GetStatistic(defensive) > 0;
// TODO: script hook
executingMove.RunScriptHook(script =>
script.BypassDefensiveStatBoosts(executingMove, target, hitNumber, ref bypassDefense));
// Check if we can bypass the offensive stat boost on the user. We default to this if the
// move is critical, and the user has an offensive stat boost of < 0, but a script is
// allowed to change this.
var bypassOffense = hitData.IsCritical && executingMove.User.StatBoost.GetStatistic(offensive) < 0;
// TODO: script hook
executingMove.RunScriptHook(script =>
script.BypassOffensiveStatBoosts(executingMove, target, hitNumber, ref bypassOffense));
var userStats = executingMove.User.BoostedStats;
if (bypassOffense)
@@ -132,11 +137,15 @@ public class Gen7DamageCalculator(bool hasRandomness) : IDamageCalculator
if (bypassDefense)
targetStats = target.FlatStats;
var defensiveStat = targetStats.GetStatistic(defensive);
// TODO: script hook to modify the stats above
executingMove.RunScriptHook(script =>
script.ChangeOffensiveStatValue(executingMove, target, hitNumber, ref offensiveStat));
executingMove.RunScriptHook(script =>
script.ChangeDefensiveStatValue(executingMove, target, hitNumber, ref defensiveStat));
var modifier = (float)offensiveStat / defensiveStat;
// TODO: script hook to modify the modifier
executingMove.RunScriptHook(script =>
script.ChangeDamageStatModifier(executingMove, target, hitNumber, ref modifier));
return modifier;
}
@@ -145,12 +154,12 @@ public class Gen7DamageCalculator(bool hasRandomness) : IDamageCalculator
/// Gets the damage modifier. This is a value that defaults to 1.0, but can be modified by scripts
/// to apply a raw modifier to the damage.
/// </summary>
private static float GetDamageModifier(IExecutingMove executingMove, IPokemon target, byte hitNumber,
IHitData hitData)
private static float GetDamageModifier(IExecutingMove executingMove, IPokemon target, byte hitNumber)
{
var modifier = 1.0f;
// TODO: script hook to modify the modifier
executingMove.RunScriptHook(script =>
script.ChangeDamageModifier(executingMove, target, hitNumber, ref modifier));
return modifier;
}