Further work on the script interface rework

This commit is contained in:
2025-07-05 11:30:18 +02:00
parent 1feb27e826
commit 4499927551
100 changed files with 418 additions and 352 deletions

View File

@@ -220,8 +220,10 @@ public static class MoveTurnExecutor
}
var blockIncomingHit = false;
target.RunScriptHook(x => x.BlockIncomingHit(executingMove, target, hitIndex, ref blockIncomingHit));
executingMove.RunScriptHook(x => x.BlockOutgoingHit(executingMove, target, hitIndex, ref blockIncomingHit));
target.RunScriptHookInterface<IScriptBlockIncomingHit>(x =>
x.BlockIncomingHit(executingMove, target, hitIndex, ref blockIncomingHit));
executingMove.RunScriptHookInterface<IScriptBlockOutgoingHit>(x =>
x.BlockOutgoingHit(executingMove, target, hitIndex, ref blockIncomingHit));
if (blockIncomingHit)
break;
if (executingMove.GetHitData(target, hitIndex).HasFailed)

View File

@@ -111,7 +111,8 @@ public static class TurnRunner
if (battleData == null)
return;
var preventSwitch = false;
fleeChoice.RunScriptHook(script => script.PreventSelfSwitch(fleeChoice, ref preventSwitch));
fleeChoice.RunScriptHookInterface<IScriptPreventSelfSwitch>(script =>
script.PreventSelfSwitch(fleeChoice, ref preventSwitch));
if (preventSwitch)
return;
foreach (var side in battle.Sides)
@@ -120,7 +121,8 @@ public static class TurnRunner
continue;
foreach (var pokemon in side.Pokemon.WhereNotNull())
{
pokemon.RunScriptHook(script => script.PreventOpponentSwitch(fleeChoice, ref preventSwitch));
pokemon.RunScriptHookInterface<IScriptPreventOpponentSwitch>(script =>
script.PreventOpponentSwitch(fleeChoice, ref preventSwitch));
if (preventSwitch)
return;
}
@@ -140,7 +142,8 @@ public static class TurnRunner
return;
var preventFlee = false;
fleeChoice.RunScriptHook(script => script.PreventSelfRunAway(fleeChoice, ref preventFlee));
fleeChoice.RunScriptHookInterface<IScriptPreventSelfRunAway>(script =>
script.PreventSelfRunAway(fleeChoice, ref preventFlee));
if (preventFlee)
return;
@@ -150,7 +153,8 @@ public static class TurnRunner
continue;
foreach (var pokemon in side.Pokemon.WhereNotNull())
{
pokemon.RunScriptHook(script => script.PreventOpponentRunAway(fleeChoice, ref preventFlee));
pokemon.RunScriptHookInterface<IScriptPreventOpponentRunAway>(script =>
script.PreventOpponentRunAway(fleeChoice, ref preventFlee));
if (preventFlee)
return;
}

View File

@@ -261,7 +261,7 @@ public class BattleSideImpl : ScriptSource, IBattleSide
var oldPokemon = _pokemon[position];
if (oldPokemon is not null)
{
oldPokemon.RunScriptHook(script => script.OnSwitchOut(oldPokemon, position));
oldPokemon.RunScriptHookInterface<IScriptOnSwitchOut>(script => script.OnSwitchOut(oldPokemon, position));
oldPokemon.RunScriptHook(script => script.OnRemove());
oldPokemon.SetOnBattlefield(false);
}
@@ -272,7 +272,7 @@ public class BattleSideImpl : ScriptSource, IBattleSide
pokemon.SetOnBattlefield(true);
pokemon.SetBattleSidePosition(position);
Battle.EventHook.Invoke(new SwitchEvent(Index, position, pokemon));
pokemon.RunScriptHook(script => script.OnSwitchIn(pokemon, position));
pokemon.RunScriptHookInterface<IScriptOnSwitchIn>(script => script.OnSwitchIn(pokemon, position));
foreach (var side in Battle.Sides)
{
@@ -286,9 +286,11 @@ public class BattleSideImpl : ScriptSource, IBattleSide
scripts.Clear();
opponent.GetOwnScripts(scripts);
opponent.RunScriptHook(script => script.OnOpponentSwitchIn(pokemon, position));
opponent.RunScriptHookInterface<IScriptOnOpponentSwitchIn>(script =>
script.OnOpponentSwitchIn(pokemon, position));
}
side.RunScriptHook(script => script.OnOpponentSwitchIn(pokemon, position));
side.RunScriptHookInterface<IScriptOnOpponentSwitchIn>(script =>
script.OnOpponentSwitchIn(pokemon, position));
}
}
else

View File

@@ -861,7 +861,8 @@ public class PokemonImpl : ScriptSource, IPokemon
if (BattleData != null)
{
var prevented = false;
this.RunScriptHook(script => script.PreventHeldItemConsume(this, HeldItem, ref prevented));
this.RunScriptHookInterface<IScriptPreventHeldItemConsume>(script =>
script.PreventHeldItemConsume(this, HeldItem, ref prevented));
if (prevented)
return false;
BattleData.MarkItemAsConsumed(HeldItem);
@@ -879,7 +880,7 @@ public class PokemonImpl : ScriptSource, IPokemon
{
// TODO: actually consume the item
this.RunScriptHook(x => x.OnAfterItemConsume(this, item));
this.RunScriptHookInterface<IScriptOnAfterItemConsume>(x => x.OnAfterItemConsume(this, item));
}
/// <inheritdoc />
@@ -1055,7 +1056,8 @@ public class PokemonImpl : ScriptSource, IPokemon
if (BattleData is not null && !forceDamage)
{
var dmg = damage;
this.RunScriptHook(script => script.ChangeIncomingDamage(this, source, ref dmg));
this.RunScriptHookInterface<IScriptChangeIncomingDamage>(script =>
script.ChangeIncomingDamage(this, source, ref dmg));
damage = dmg;
}
if (damage == 0)
@@ -1075,7 +1077,8 @@ public class PokemonImpl : ScriptSource, IPokemon
BatchId = batchId,
});
// And allow scripts to execute.
this.RunScriptHook(script => script.OnDamage(this, source, CurrentHealth, newHealth));
this.RunScriptHookInterface<IScriptOnDamage>(script =>
script.OnDamage(this, source, CurrentHealth, newHealth));
}
CurrentHealth = newHealth;
@@ -1104,10 +1107,10 @@ public class PokemonImpl : ScriptSource, IPokemon
BattleData.Battle.EventHook.Invoke(new FaintEvent(this));
// Allow scripts to trigger based on the faint.
this.RunScriptHook(script => script.OnFaint(this, source));
this.RunScriptHookInterface<IScriptOnFaint>(script => script.OnFaint(this, source));
foreach (var ally in BattleData.BattleSide.Pokemon.WhereNotNull().Where(x => x != this))
{
ally.RunScriptHook(script => script.OnAllyFaint(ally, this));
ally.RunScriptHookInterface<IScriptOnAllyFaint>(script => script.OnAllyFaint(ally, this));
}
// Make sure the OnRemove script is run.

View File

@@ -39,14 +39,6 @@ public abstract class Script : IDeepCloneable
/// </summary>
public virtual ScriptCategory Category => this.ResolveCategory();
/// <summary>
/// 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.
/// </summary>
public virtual void Stack()
{
}
/// <summary>
/// This function is ran when this script stops being in effect, and is removed from its owner.
/// </summary>
@@ -61,101 +53,6 @@ public abstract class Script : IDeepCloneable
{
}
/// <summary>
/// This function prevents the Pokemon it is attached to from being able to switch out.
/// </summary>
public virtual void PreventSelfSwitch(ISwitchChoice choice, ref bool prevent)
{
}
/// <summary>
/// This function allows the prevention of switching for any opponent.
/// </summary>
public virtual void PreventOpponentSwitch(ISwitchChoice choice, ref bool prevent)
{
}
/// <summary>
/// This function is called on a move and its parents when the move fails.
/// </summary>
public virtual void OnFail(IPokemon pokemon)
{
}
/// <summary>
/// This function is called on a script when an opponent fails.
/// </summary>
/// <param name="pokemon"></param>
public virtual void OnOpponentFail(IPokemon pokemon)
{
}
/// <summary>
/// This function allows preventing the running away of the Pokemon its attached to
/// </summary>
public virtual void PreventSelfRunAway(IFleeChoice choice, ref bool prevent)
{
}
/// <summary>
/// This function prevents a Pokemon on another side than where its attached to from running away.
/// </summary>
public virtual void PreventOpponentRunAway(IFleeChoice choice, ref bool prevent)
{
}
/// <summary>
/// This function is triggered on a Pokemon and its parents when the given Pokemon takes damage.
/// </summary>
public virtual void OnDamage(IPokemon pokemon, DamageSource source, uint oldHealth, uint newHealth)
{
}
/// <summary>
/// This function is triggered on a Pokemon and its parents when the given Pokemon faints.
/// </summary>
public virtual void OnFaint(IPokemon pokemon, DamageSource source)
{
}
/// <summary>
/// This function is triggered on a Pokemon when an ally Pokemon faints.
/// </summary>
public virtual void OnAllyFaint(IPokemon ally, IPokemon faintedPokemon)
{
}
/// <summary>
/// This function is triggered on a Pokemon and its parents when the given Pokemon switches out
/// of the battlefield.
/// </summary>
public virtual void OnSwitchOut(IPokemon oldPokemon, byte position)
{
}
/// <summary>
/// This function is triggered on a Pokemon and its parents when the given Pokemon is switched into
/// the battlefield.
/// </summary>
public virtual void OnSwitchIn(IPokemon pokemon, byte position)
{
}
/// <summary>
/// This function is triggered on a Pokemon and its parents when an opponent switches in.
/// </summary>
public virtual void OnOpponentSwitchIn(IPokemon pokemon, byte position)
{
}
/// <summary>
/// This function is triggered on a Pokemon and its parents when the given Pokemon consumes the
/// held item it had.
/// </summary>
public virtual void OnAfterItemConsume(IPokemon pokemon, IItem item)
{
}
/// <summary>
/// This function is triggered on a Pokemon and its parents when the given Pokemon gains experience,
/// and allows for changing this amount of experience.
@@ -191,20 +88,6 @@ public abstract class Script : IDeepCloneable
{
}
/// <summary>
/// This function allows a script to block an incoming hit.
/// </summary>
public virtual void BlockIncomingHit(IExecutingMove executingMove, IPokemon target, byte hitIndex, ref bool block)
{
}
/// <summary>
/// This function allows a script to block an outgoing hit.
/// </summary>
public virtual void BlockOutgoingHit(IExecutingMove executingMove, IPokemon target, byte hitIndex, ref bool block)
{
}
/// <summary>
/// Custom triggers for scripts. This allows scripts to run custom events that are not part of the
/// standard battle flow.
@@ -220,20 +103,6 @@ public abstract class Script : IDeepCloneable
{
}
/// <summary>
/// This function allows a script to prevent a held item from being consumed.
/// </summary>
public virtual void PreventHeldItemConsume(IPokemon pokemon, IItem heldItem, ref bool prevented)
{
}
/// <summary>
/// This function allows a script to change any kind of damage that is incoming.
/// </summary>
public virtual void ChangeIncomingDamage(IPokemon pokemon, DamageSource source, ref uint damage)
{
}
/// <summary>
/// This function allows a script to change the accuracy of a move used. The value for accuracy is in percentage.
/// A custom case goes when 255 is returned, in which case the entire accuracy check is skipped, and the move
@@ -1053,4 +922,184 @@ public interface IScriptOnEndTurn
/// function.
/// </summary>
void OnEndTurn(IScriptSource owner, IBattle battle);
}
/// <summary>
/// This interface allows scripts to prevent the Pokemon it is attached to from being able to switch out.
/// </summary>
public interface IScriptPreventSelfSwitch
{
/// <summary>
/// This function prevents the Pokemon it is attached to from being able to switch out.
/// </summary>
void PreventSelfSwitch(ISwitchChoice choice, ref bool prevent);
}
/// <summary>
/// This interface allows scripts to prevent switching for any opponent.
/// </summary>
public interface IScriptPreventOpponentSwitch
{
/// <summary>
/// This function allows the prevention of switching for any opponent.
/// </summary>
void PreventOpponentSwitch(ISwitchChoice choice, ref bool prevent);
}
/// <summary>
/// This interface allows scripts to prevent the Pokemon its attached to from running away.
/// </summary>
public interface IScriptPreventSelfRunAway
{
/// <summary>
/// This function allows preventing the running away of the Pokemon its attached to
/// </summary>
void PreventSelfRunAway(IFleeChoice choice, ref bool prevent);
}
/// <summary>
/// This interface allows scripts to prevent a Pokemon on another side from running away.
/// </summary>
public interface IScriptPreventOpponentRunAway
{
/// <summary>
/// This function prevents a Pokemon on another side than where its attached to from running away.
/// </summary>
void PreventOpponentRunAway(IFleeChoice choice, ref bool prevent);
}
/// <summary>
/// This interface allows scripts to trigger when a Pokemon takes damage.
/// </summary>
public interface IScriptOnDamage
{
/// <summary>
/// This function is triggered on a Pokemon and its parents when the given Pokemon takes damage.
/// </summary>
void OnDamage(IPokemon pokemon, DamageSource source, uint oldHealth, uint newHealth);
}
/// <summary>
/// This interface allows scripts to trigger when a Pokemon faints.
/// </summary>
public interface IScriptOnFaint
{
/// <summary>
/// This function is triggered on a Pokemon and its parents when the given Pokemon faints.
/// </summary>
void OnFaint(IPokemon pokemon, DamageSource source);
}
/// <summary>
/// This interface allows scripts to trigger when an ally Pokemon faints.
/// </summary>
public interface IScriptOnAllyFaint
{
/// <summary>
/// This function is triggered on a Pokemon when an ally Pokemon faints.
/// </summary>
void OnAllyFaint(IPokemon ally, IPokemon faintedPokemon);
}
/// <summary>
/// This interface allows scripts to trigger when a Pokemon switches out of the battlefield.
/// </summary>
public interface IScriptOnSwitchOut
{
/// <summary>
/// This function is triggered on a Pokemon and its parents when the given Pokemon switches out
/// of the battlefield.
/// </summary>
void OnSwitchOut(IPokemon oldPokemon, byte position);
}
/// <summary>
/// This interface allows scripts to trigger when a Pokemon is switched into the battlefield.
/// </summary>
public interface IScriptOnSwitchIn
{
/// <summary>
/// This function is triggered on a Pokemon and its parents when the given Pokemon is switched into
/// the battlefield.
/// </summary>
void OnSwitchIn(IPokemon pokemon, byte position);
}
/// <summary>
/// This interface allows scripts to trigger when an opponent Pokemon switches in.
/// </summary>
public interface IScriptOnOpponentSwitchIn
{
/// <summary>
/// This function is triggered on a Pokemon and its parents when an opponent switches in.
/// </summary>
void OnOpponentSwitchIn(IPokemon pokemon, byte position);
}
/// <summary>
/// This interface allows scripts to stack when a volatile effect is added while already in place.
/// </summary>
public interface IScriptStack
{
/// <summary>
/// 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.
/// </summary>
void Stack();
}
/// <summary>
/// This interface allows scripts to trigger after a Pokemon consumes an item.
/// </summary>
public interface IScriptOnAfterItemConsume
{
/// <summary>
/// This function is triggered on a Pokemon and its parents when the given Pokemon consumes the
/// held item it had.
/// </summary>
void OnAfterItemConsume(IPokemon pokemon, IItem item);
}
/// <summary>
/// This interface allows scripts to block incoming hits on a target.
/// </summary>
public interface IScriptBlockIncomingHit
{
/// <summary>
/// This function allows a script to block an incoming hit.
/// </summary>
void BlockIncomingHit(IExecutingMove executingMove, IPokemon target, byte hitIndex, ref bool block);
}
/// <summary>
/// This interface allows scripts to block outgoing hits from a move.
/// </summary>
public interface IScriptBlockOutgoingHit
{
/// <summary>
/// This function allows a script to block an outgoing hit.
/// </summary>
void BlockOutgoingHit(IExecutingMove executingMove, IPokemon target, byte hitIndex, ref bool block);
}
/// <summary>
/// This interface allows scripts to prevent held item consumption.
/// </summary>
public interface IScriptPreventHeldItemConsume
{
/// <summary>
/// This function allows a script to prevent a held item from being consumed.
/// </summary>
void PreventHeldItemConsume(IPokemon pokemon, IItem heldItem, ref bool prevented);
}
/// <summary>
/// This interface allows scripts to change incoming damage to a Pokemon.
/// </summary>
public interface IScriptChangeIncomingDamage
{
/// <summary>
/// This function allows a script to change any kind of damage that is incoming.
/// </summary>
void ChangeIncomingDamage(IPokemon pokemon, DamageSource source, ref uint damage);
}

View File

@@ -116,7 +116,8 @@ public class ScriptSet : IScriptSet
var existing = _scripts.FirstOrDefault(s => s.Script?.Name == script.Name);
if (existing != null)
{
existing.Script!.Stack();
if (existing.Script is IScriptStack stackable)
stackable.Stack();
return existing;
}
@@ -133,7 +134,8 @@ public class ScriptSet : IScriptSet
var existing = _scripts.FirstOrDefault(s => s.Script?.Name == scriptKey);
if (existing != null)
{
existing.Script!.Stack();
if (existing.Script is IScriptStack stackable)
stackable.Stack();
return existing;
}