Cleanup, move OnBeforeAnyHookInvoked function to an interface
This commit is contained in:
parent
43813c1c1c
commit
d719ce03ea
@ -1,36 +0,0 @@
|
|||||||
using PkmnLib.Dynamic.Models;
|
|
||||||
using PkmnLib.Static;
|
|
||||||
|
|
||||||
namespace PkmnLib.Dynamic.ScriptHandling;
|
|
||||||
|
|
||||||
public class ProxyScript : Script
|
|
||||||
{
|
|
||||||
public delegate void ChangeOffensiveStatValueEventHandler(IExecutingMove move, IPokemon target, byte hit,
|
|
||||||
uint defensiveStat, ImmutableStatisticSet<uint> targetStats, ref uint value);
|
|
||||||
|
|
||||||
private readonly List<(Script, ChangeOffensiveStatValueEventHandler)> _changeOffensiveStatValueEvents = new();
|
|
||||||
|
|
||||||
public void AddChangeOffensiveStatValueEvent(Script script, ChangeOffensiveStatValueEventHandler handler)
|
|
||||||
{
|
|
||||||
_changeOffensiveStatValueEvents.Add((script, handler));
|
|
||||||
script.OnRemoveEvent += OnRemoveScriptEvent;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnRemoveScriptEvent(Script script)
|
|
||||||
{
|
|
||||||
_changeOffensiveStatValueEvents.RemoveAll(x => x.Item1 == script);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public override void ChangeOffensiveStatValue(IExecutingMove move, IPokemon target, byte hit, uint defensiveStat,
|
|
||||||
ImmutableStatisticSet<uint> targetStats, Statistic stat, ref uint value)
|
|
||||||
{
|
|
||||||
foreach (var (script, handler) in _changeOffensiveStatValueEvents)
|
|
||||||
{
|
|
||||||
if (!script.IsSuppressed)
|
|
||||||
{
|
|
||||||
handler(move, target, hit, defensiveStat, targetStats, ref value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -18,8 +18,6 @@ public abstract class Script : IDeepCloneable
|
|||||||
{
|
{
|
||||||
internal event Action<Script>? OnRemoveEvent;
|
internal event Action<Script>? OnRemoveEvent;
|
||||||
|
|
||||||
private int _suppressCount;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Remove the script from its owner.
|
/// Remove the script from its owner.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -41,40 +39,6 @@ public abstract class Script : IDeepCloneable
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual ScriptCategory Category => this.ResolveCategory();
|
public virtual ScriptCategory Category => this.ResolveCategory();
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// A script can be suppressed by other scripts. If a script is suppressed by at least one script
|
|
||||||
/// we will not execute its methods. This should return the number of suppressions on the script.
|
|
||||||
/// </summary>
|
|
||||||
public int SuppressCount => _suppressCount;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Helper function to check if there is at least one suppression on the script
|
|
||||||
/// </summary>
|
|
||||||
public bool IsSuppressed => _suppressCount > 0;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a suppression. This makes the script not run anymore. Note that adding this should also
|
|
||||||
/// remove the suppression later.
|
|
||||||
///
|
|
||||||
/// A common pattern for this is to run this in the <see cref="OnInitialize"/> and remove it in the
|
|
||||||
/// <see cref="OnRemove"/> function.
|
|
||||||
/// </summary>
|
|
||||||
public void Suppress() => _suppressCount++;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Removes a suppression. This allows the script to run again (provided other scripts are not
|
|
||||||
/// suppressing it). Note that running this should only occur if <see cref="Suppress"/> was called earlier
|
|
||||||
/// </summary>
|
|
||||||
public void Unsuppress() => _suppressCount--;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// This function is ran before any hook is invoked. This allows for suppressing certain categories
|
|
||||||
/// of scripts. This is useful for example to prevent certain effects from running.
|
|
||||||
/// </summary>
|
|
||||||
public virtual void OnBeforeAnyHookInvoked(ref List<ScriptCategory>? suppressedCategories)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This function is ran when a volatile effect is added while that volatile effect already is
|
/// This function is ran when a volatile effect is added while that volatile effect already is
|
||||||
/// in place. Instead of adding the volatile effect twice, it will execute this function instead.
|
/// in place. Instead of adding the volatile effect twice, it will execute this function instead.
|
||||||
@ -767,6 +731,9 @@ public abstract class Script : IDeepCloneable
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This function triggers after a status condition has been applied to a Pokemon.
|
||||||
|
/// </summary>
|
||||||
public virtual void OnAfterStatusChange(IPokemon pokemon, StringKey status, IPokemon? originPokemon)
|
public virtual void OnAfterStatusChange(IPokemon pokemon, StringKey status, IPokemon? originPokemon)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -796,6 +763,9 @@ public abstract class Script : IDeepCloneable
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This function triggers when the weather changes.
|
||||||
|
/// </summary>
|
||||||
public virtual void OnWeatherChange(IBattle battle, StringKey? weatherName, StringKey? oldWeatherName)
|
public virtual void OnWeatherChange(IBattle battle, StringKey? weatherName, StringKey? oldWeatherName)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -844,15 +814,38 @@ public abstract class Script : IDeepCloneable
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This function triggers just before a move choice is executed.
|
||||||
|
/// </summary>
|
||||||
public virtual void OnBeforeMoveChoice(IMoveChoice moveChoice)
|
public virtual void OnBeforeMoveChoice(IMoveChoice moveChoice)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This function triggers after a move choice has been executed in its entirety.
|
||||||
|
/// </summary>
|
||||||
public virtual void OnAfterMoveChoice(IMoveChoice moveChoice)
|
public virtual void OnAfterMoveChoice(IMoveChoice moveChoice)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This function allows a script to change the turn choice that is made by a Pokemon.
|
||||||
|
/// </summary>
|
||||||
public virtual void ChangeTurnChoice(ref ITurnChoice choice)
|
public virtual void ChangeTurnChoice(ref ITurnChoice choice)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This interface is used to allow scripts to run before any hook is invoked. This allows for
|
||||||
|
/// suppressing certain categories of scripts, which can be useful for preventing certain effects
|
||||||
|
/// from running.
|
||||||
|
/// </summary>
|
||||||
|
public interface IScriptOnBeforeAnyHookInvoked
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// This function is ran before any hook is invoked. This allows for suppressing certain categories
|
||||||
|
/// of scripts. This is useful for example to prevent certain effects from running.
|
||||||
|
/// </summary>
|
||||||
|
void OnBeforeAnyHookInvoked(ref List<ScriptCategory>? suppressedCategories);
|
||||||
|
}
|
@ -23,15 +23,14 @@ public static class ScriptExecution
|
|||||||
if (container.IsEmpty)
|
if (container.IsEmpty)
|
||||||
continue;
|
continue;
|
||||||
var script = container.Script;
|
var script = container.Script;
|
||||||
script.OnBeforeAnyHookInvoked(ref suppressedCategories);
|
if (script is IScriptOnBeforeAnyHookInvoked onBeforeAnyHookInvoked)
|
||||||
|
onBeforeAnyHookInvoked.OnBeforeAnyHookInvoked(ref suppressedCategories);
|
||||||
}
|
}
|
||||||
foreach (var container in iterator)
|
foreach (var container in iterator)
|
||||||
{
|
{
|
||||||
if (container.IsEmpty)
|
if (container.IsEmpty)
|
||||||
continue;
|
continue;
|
||||||
var script = container.Script;
|
var script = container.Script;
|
||||||
if (script.IsSuppressed)
|
|
||||||
continue;
|
|
||||||
if (suppressedCategories != null && suppressedCategories.Contains(script.Category))
|
if (suppressedCategories != null && suppressedCategories.Contains(script.Category))
|
||||||
continue;
|
continue;
|
||||||
hook(script);
|
hook(script);
|
||||||
@ -51,15 +50,14 @@ public static class ScriptExecution
|
|||||||
if (container.IsEmpty)
|
if (container.IsEmpty)
|
||||||
continue;
|
continue;
|
||||||
var script = container.Script;
|
var script = container.Script;
|
||||||
script.OnBeforeAnyHookInvoked(ref suppressedCategories);
|
if (script is IScriptOnBeforeAnyHookInvoked onBeforeAnyHookInvoked)
|
||||||
|
onBeforeAnyHookInvoked.OnBeforeAnyHookInvoked(ref suppressedCategories);
|
||||||
}
|
}
|
||||||
foreach (var container in iterator)
|
foreach (var container in iterator)
|
||||||
{
|
{
|
||||||
if (container.IsEmpty)
|
if (container.IsEmpty)
|
||||||
continue;
|
continue;
|
||||||
var script = container.Script;
|
var script = container.Script;
|
||||||
if (script.IsSuppressed)
|
|
||||||
continue;
|
|
||||||
if (suppressedCategories != null && suppressedCategories.Contains(script.Category))
|
if (suppressedCategories != null && suppressedCategories.Contains(script.Category))
|
||||||
continue;
|
continue;
|
||||||
hook(script);
|
hook(script);
|
||||||
@ -78,15 +76,14 @@ public static class ScriptExecution
|
|||||||
if (container.IsEmpty)
|
if (container.IsEmpty)
|
||||||
continue;
|
continue;
|
||||||
var script = container.Script;
|
var script = container.Script;
|
||||||
script.OnBeforeAnyHookInvoked(ref suppressedCategories);
|
if (script is IScriptOnBeforeAnyHookInvoked onBeforeAnyHookInvoked)
|
||||||
|
onBeforeAnyHookInvoked.OnBeforeAnyHookInvoked(ref suppressedCategories);
|
||||||
}
|
}
|
||||||
foreach (var container in source.SelectMany(x => x))
|
foreach (var container in source.SelectMany(x => x))
|
||||||
{
|
{
|
||||||
if (container.IsEmpty)
|
if (container.IsEmpty)
|
||||||
continue;
|
continue;
|
||||||
var script = container.Script;
|
var script = container.Script;
|
||||||
if (script.IsSuppressed)
|
|
||||||
continue;
|
|
||||||
if (suppressedCategories != null && suppressedCategories.Contains(script.Category))
|
if (suppressedCategories != null && suppressedCategories.Contains(script.Category))
|
||||||
continue;
|
continue;
|
||||||
hook(script);
|
hook(script);
|
||||||
|
@ -6,10 +6,10 @@ namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;
|
|||||||
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Klutz_(Ability)">Bulbapedia - Klutz</see>
|
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Klutz_(Ability)">Bulbapedia - Klutz</see>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Script(ScriptCategory.Ability, "klutz")]
|
[Script(ScriptCategory.Ability, "klutz")]
|
||||||
public class Klutz : Script
|
public class Klutz : Script, IScriptOnBeforeAnyHookInvoked
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void OnBeforeAnyHookInvoked(ref List<ScriptCategory>? suppressedCategories)
|
public void OnBeforeAnyHookInvoked(ref List<ScriptCategory>? suppressedCategories)
|
||||||
{
|
{
|
||||||
suppressedCategories ??= new List<ScriptCategory>();
|
suppressedCategories ??= new List<ScriptCategory>();
|
||||||
suppressedCategories.Add(ScriptCategory.ItemBattleTrigger);
|
suppressedCategories.Add(ScriptCategory.ItemBattleTrigger);
|
||||||
|
@ -6,10 +6,10 @@ namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;
|
|||||||
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Mold_Breaker_(Ability)">Bulbapedia - Mold Breaker</see>
|
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Mold_Breaker_(Ability)">Bulbapedia - Mold Breaker</see>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Script(ScriptCategory.Ability, "mold_breaker")]
|
[Script(ScriptCategory.Ability, "mold_breaker")]
|
||||||
public class MoldBreaker : Script
|
public class MoldBreaker : Script, IScriptOnBeforeAnyHookInvoked
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void OnBeforeAnyHookInvoked(ref List<ScriptCategory>? suppressedCategories)
|
public void OnBeforeAnyHookInvoked(ref List<ScriptCategory>? suppressedCategories)
|
||||||
{
|
{
|
||||||
suppressedCategories ??= [];
|
suppressedCategories ??= [];
|
||||||
suppressedCategories.Add(ScriptCategory.Ability);
|
suppressedCategories.Add(ScriptCategory.Ability);
|
||||||
|
@ -6,7 +6,7 @@ namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;
|
|||||||
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Shadow_Shield_(Ability)">Bulbapedia - Shadow Shield</see>
|
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Shadow_Shield_(Ability)">Bulbapedia - Shadow Shield</see>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Script(ScriptCategory.Ability, "shadow_shield")]
|
[Script(ScriptCategory.Ability, "shadow_shield")]
|
||||||
public class ShadowShield : Script
|
public class ShadowShield : Script, IScriptOnBeforeAnyHookInvoked
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void ChangeIncomingMoveDamage(IExecutingMove move, IPokemon target, byte hit, ref uint damage)
|
public override void ChangeIncomingMoveDamage(IExecutingMove move, IPokemon target, byte hit, ref uint damage)
|
||||||
@ -18,7 +18,7 @@ public class ShadowShield : Script
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void OnBeforeAnyHookInvoked(ref List<ScriptCategory>? suppressedCategories)
|
public void OnBeforeAnyHookInvoked(ref List<ScriptCategory>? suppressedCategories)
|
||||||
{
|
{
|
||||||
// Shadow Shield can not be suppressed by any other script, so we remove it from the list of suppressed categories
|
// Shadow Shield can not be suppressed by any other script, so we remove it from the list of suppressed categories
|
||||||
// if it was added.
|
// if it was added.
|
||||||
|
@ -9,10 +9,10 @@ namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;
|
|||||||
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Air_Lock_(Ability)">Bulbapedia - Air Lock</see>
|
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Air_Lock_(Ability)">Bulbapedia - Air Lock</see>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Script(ScriptCategory.Ability, "suppress_weather")]
|
[Script(ScriptCategory.Ability, "suppress_weather")]
|
||||||
public class SuppressWeatherAbility : Script
|
public class SuppressWeatherAbility : Script, IScriptOnBeforeAnyHookInvoked
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void OnBeforeAnyHookInvoked(ref List<ScriptCategory>? suppressedCategories)
|
public void OnBeforeAnyHookInvoked(ref List<ScriptCategory>? suppressedCategories)
|
||||||
{
|
{
|
||||||
suppressedCategories ??= [];
|
suppressedCategories ??= [];
|
||||||
suppressedCategories.Add(ScriptCategory.Weather);
|
suppressedCategories.Add(ScriptCategory.Weather);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
namespace PkmnLib.Plugin.Gen7.Scripts.Battle;
|
namespace PkmnLib.Plugin.Gen7.Scripts.Battle;
|
||||||
|
|
||||||
[Script(ScriptCategory.Battle, "magic_room")]
|
[Script(ScriptCategory.Battle, "magic_room")]
|
||||||
public class MagicRoomEffect : Script
|
public class MagicRoomEffect : Script, IScriptOnBeforeAnyHookInvoked
|
||||||
{
|
{
|
||||||
private int _turnsLeft = 5;
|
private int _turnsLeft = 5;
|
||||||
|
|
||||||
@ -12,7 +12,7 @@ public class MagicRoomEffect : Script
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void OnBeforeAnyHookInvoked(ref List<ScriptCategory>? suppressedCategories)
|
public void OnBeforeAnyHookInvoked(ref List<ScriptCategory>? suppressedCategories)
|
||||||
{
|
{
|
||||||
suppressedCategories ??= [];
|
suppressedCategories ??= [];
|
||||||
suppressedCategories.Add(ScriptCategory.ItemBattleTrigger);
|
suppressedCategories.Add(ScriptCategory.ItemBattleTrigger);
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
namespace PkmnLib.Plugin.Gen7.Scripts.Moves;
|
namespace PkmnLib.Plugin.Gen7.Scripts.Moves;
|
||||||
|
|
||||||
[Script(ScriptCategory.Move, "moongeist_beam")]
|
[Script(ScriptCategory.Move, "moongeist_beam")]
|
||||||
public class MoongeistBeam : Script
|
public class MoongeistBeam : Script, IScriptOnBeforeAnyHookInvoked
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void OnBeforeAnyHookInvoked(ref List<ScriptCategory>? suppressedCategories)
|
public void OnBeforeAnyHookInvoked(ref List<ScriptCategory>? suppressedCategories)
|
||||||
{
|
{
|
||||||
suppressedCategories ??= [];
|
suppressedCategories ??= [];
|
||||||
suppressedCategories.Add(ScriptCategory.Ability);
|
suppressedCategories.Add(ScriptCategory.Ability);
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
namespace PkmnLib.Plugin.Gen7.Scripts.Side;
|
namespace PkmnLib.Plugin.Gen7.Scripts.Side;
|
||||||
|
|
||||||
[Script(ScriptCategory.Side, "teravolt")]
|
[Script(ScriptCategory.Side, "teravolt")]
|
||||||
public class TeravoltEffect : Script
|
public class TeravoltEffect : Script, IScriptOnBeforeAnyHookInvoked
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void OnBeforeAnyHookInvoked(ref List<ScriptCategory>? suppressedCategories)
|
public void OnBeforeAnyHookInvoked(ref List<ScriptCategory>? suppressedCategories)
|
||||||
{
|
{
|
||||||
suppressedCategories ??= [];
|
suppressedCategories ??= [];
|
||||||
suppressedCategories.Add(ScriptCategory.Ability);
|
suppressedCategories.Add(ScriptCategory.Ability);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user