More abilities
All checks were successful
Build / Build (push) Successful in 48s

This commit is contained in:
2025-06-09 13:44:26 +02:00
parent 00005aa4bf
commit 97868ab4c6
94 changed files with 829 additions and 150 deletions

View File

@@ -1,5 +1,6 @@
using PkmnLib.Dynamic.Models;
using PkmnLib.Static.Species;
using PkmnLib.Static.Utils;
namespace PkmnLib.Dynamic.Events;
@@ -18,6 +19,8 @@ public record AbilityTriggerEvent : IEventData
/// </summary>
public IAbility? Ability { get; }
public Dictionary<StringKey, object?>? Metadata { get; init; } = null;
/// <inheritdoc cref="AbilityTriggerEvent"/>
public AbilityTriggerEvent(IPokemon pokemon)
{

View File

@@ -274,8 +274,9 @@ public interface IPokemon : IScriptSource, IDeepCloneable
/// <param name="stat">The stat to be changed</param>
/// <param name="change">The amount to change the stat by</param>
/// <param name="selfInflicted">Whether the change was self-inflicted. This can be relevant in scripts.</param>
/// <param name="force"></param>
/// <param name="batchId">The event batch ID this change is a part of. This is relevant for visual handling</param>
bool ChangeStatBoost(Statistic stat, sbyte change, bool selfInflicted, EventBatchId batchId = default);
bool ChangeStatBoost(Statistic stat, sbyte change, bool selfInflicted, bool force, EventBatchId batchId = default);
/// <summary>
/// Suppresses the ability of the Pokémon.
@@ -354,7 +355,7 @@ public interface IPokemon : IScriptSource, IDeepCloneable
/// <summary>
/// Adds a non-volatile status to the Pokemon.
/// </summary>
bool SetStatus(StringKey status, EventBatchId batchId = default);
bool SetStatus(StringKey status, bool selfInflicted, EventBatchId batchId = default);
/// <summary>
/// Removes the current non-volatile status from the Pokemon.
@@ -841,15 +842,20 @@ public class PokemonImpl : ScriptSource, IPokemon
}
/// <inheritdoc />
public bool ChangeStatBoost(Statistic stat, sbyte change, bool selfInflicted, EventBatchId batchId = default)
public bool ChangeStatBoost(Statistic stat, sbyte change, bool selfInflicted, bool force,
EventBatchId batchId = default)
{
var prevented = false;
this.RunScriptHook(script => script.PreventStatBoostChange(this, stat, change, selfInflicted, ref prevented));
if (prevented)
return false;
this.RunScriptHook(script => script.ChangeStatBoostChange(this, stat, selfInflicted, ref change));
if (change == 0)
return false;
if (!force)
{
var prevented = false;
this.RunScriptHook(script =>
script.PreventStatBoostChange(this, stat, change, selfInflicted, ref prevented));
if (prevented)
return false;
this.RunScriptHook(script => script.ChangeStatBoostChange(this, stat, selfInflicted, ref change));
if (change == 0)
return false;
}
var changed = false;
var oldBoost = StatBoost.GetStatistic(stat);
changed = change switch
@@ -1135,7 +1141,7 @@ public class PokemonImpl : ScriptSource, IPokemon
public bool HasStatus(StringKey status) => StatusScript.Script?.Name == status;
/// <inheritdoc />
public bool SetStatus(StringKey status, EventBatchId batchId = default)
public bool SetStatus(StringKey status, bool selfInflicted, EventBatchId batchId = default)
{
if (!Library.ScriptResolver.TryResolve(ScriptCategory.Status, status, null, out var statusScript))
throw new KeyNotFoundException($"Status script {status} not found");
@@ -1145,7 +1151,7 @@ public class PokemonImpl : ScriptSource, IPokemon
var oldStatus = StatusScript.Script?.Name;
var preventStatus = false;
this.RunScriptHook(script => script.PreventStatusChange(this, status, ref preventStatus));
this.RunScriptHook(script => script.PreventStatusChange(this, status, selfInflicted, ref preventStatus));
if (preventStatus)
return false;

View File

@@ -0,0 +1,36 @@
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, ref uint value)
{
foreach (var (script, handler) in _changeOffensiveStatValueEvents)
{
if (!script.IsSuppressed)
{
handler(move, target, hit, defensiveStat, targetStats, ref value);
}
}
}
}

View File

@@ -728,7 +728,8 @@ public abstract class Script : IDeepCloneable
/// <summary>
/// This function allows a script to prevent a Pokemon from being affected by a status condition.
/// </summary>
public virtual void PreventStatusChange(IPokemon pokemonImpl, StringKey status, ref bool preventStatus)
public virtual void PreventStatusChange(IPokemon pokemon, StringKey status, bool selfInflicted,
ref bool preventStatus)
{
}

View File

@@ -65,10 +65,7 @@ public class ScriptContainer : IReadOnlyScriptContainer
/// </summary>
public Script? Clear()
{
if (Script is not null)
{
Script.OnRemove();
}
Script?.OnRemove();
var script = Script;
Script = null;
return script;

View File

@@ -1,4 +1,5 @@
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using PkmnLib.Dynamic.ScriptHandling.Registry;
using PkmnLib.Static.Utils;
@@ -31,6 +32,11 @@ public interface IScriptSet : IEnumerable<ScriptContainer>
/// </summary>
ScriptContainer? Get(StringKey scriptKey);
/// <summary>
/// Tries to get a script from the set using its type.
/// </summary>
bool TryGet<T>([NotNullWhen(true)] out T? script) where T : Script;
/// <summary>
/// Gets a script from the set using its type.
/// </summary>
@@ -144,6 +150,20 @@ public class ScriptSet : IScriptSet
/// <inheritdoc />
public ScriptContainer? Get(StringKey scriptKey) => _scripts.FirstOrDefault(s => s.Script?.Name == scriptKey);
/// <inheritdoc />
public bool TryGet<T>([NotNullWhen(true)] out T? script) where T : Script
{
var scriptName = ScriptUtils.ResolveName<T>();
var container = _scripts.FirstOrDefault(sc => sc.Script?.Name == scriptName);
if (container?.Script is not T s)
{
script = null;
return false;
}
script = s;
return true;
}
/// <inheritdoc />
public T? Get<T>() where T : Script => Get(ScriptUtils.ResolveName<T>())?.Script as T;