Refactor move classes to implement respective interfaces for better structure
All checks were successful
Build / Build (push) Successful in 52s

This commit is contained in:
2025-06-28 12:02:24 +02:00
parent 2319160b52
commit b7bdf2b744
115 changed files with 407 additions and 348 deletions

View File

@@ -19,7 +19,7 @@ public static class MoveTurnExecutor
var useMove = chosenMove.MoveData;
var moveDataName = useMove.Name;
moveChoice.RunScriptHook(x => x.ChangeMove(moveChoice, ref moveDataName));
moveChoice.RunScriptHookInterface<IScriptChangeMove>(x => x.ChangeMove(moveChoice, ref moveDataName));
if (useMove.Name != moveDataName)
{
if (!battle.Library.StaticLibrary.Moves.TryGet(moveDataName, out useMove))
@@ -51,17 +51,22 @@ public static class MoveTurnExecutor
var targetType = useMove.Target;
var targets =
TargetResolver.ResolveTargets(battle, moveChoice.TargetSide, moveChoice.TargetPosition, targetType);
moveChoice.RunScriptHook(x => x.ChangeTargets(moveChoice, ref targets));
moveChoice.RunScriptHookInterface<IScriptChangeTargets>(x => x.ChangeTargets(moveChoice, ref targets));
if (targets.Count == 0)
{
moveChoice.Fail();
return;
}
targets.WhereNotNull().RunScriptHook(x => x.ChangeIncomingTargets(moveChoice, ref targets));
foreach (var target in targets.WhereNotNull())
{
target.RunScriptHookInterface<IScriptChangeIncomingTargets>(x =>
x.ChangeIncomingTargets(moveChoice, ref targets));
}
byte numberOfHits = 1;
moveChoice.RunScriptHook(x => x.ChangeNumberOfHits(moveChoice, ref numberOfHits));
moveChoice.RunScriptHookInterface<IScriptChangeNumberOfHits>(x =>
x.ChangeNumberOfHits(moveChoice, ref numberOfHits));
if (numberOfHits == 0)
{
return;
@@ -72,7 +77,7 @@ public static class MoveTurnExecutor
battle.EventHook.Invoke(new MoveUseEvent(executingMove));
var prevented = false;
executingMove.RunScriptHook(x => x.PreventMove(executingMove, ref prevented));
executingMove.RunScriptHookInterface<IScriptPreventMove>(x => x.PreventMove(executingMove, ref prevented));
if (prevented)
return;
@@ -83,7 +88,7 @@ public static class MoveTurnExecutor
return;
var failed = false;
executingMove.RunScriptHook(x => x.FailMove(executingMove, ref failed));
executingMove.RunScriptHookInterface<IScriptFailMove>(x => x.FailMove(executingMove, ref failed));
if (failed)
{
// TODO: fail handling
@@ -106,16 +111,16 @@ public static class MoveTurnExecutor
public static void ExecuteMove(IExecutingMove executingMove)
{
var stopped = false;
executingMove.RunScriptHook(x => x.StopBeforeMove(executingMove, ref stopped));
executingMove.RunScriptHookInterface<IScriptStopBeforeMove>(x => x.StopBeforeMove(executingMove, ref stopped));
if (stopped)
return;
executingMove.RunScriptHook(x => x.OnBeforeMove(executingMove));
executingMove.RunScriptHookInterface<IScriptOnBeforeMove>(x => x.OnBeforeMove(executingMove));
foreach (var target in executingMove.Targets.WhereNotNull())
{
ExecuteMoveChoiceForTarget(executingMove.Battle, executingMove, target);
}
executingMove.RunScriptHook(x => x.OnAfterMove(executingMove));
executingMove.RunScriptHookInterface<IScriptOnAfterMove>(x => x.OnAfterMove(executingMove));
}
private static void ExecuteMoveChoiceForTarget(IBattle battle, IExecutingMove executingMove, IPokemon target)

View File

@@ -31,7 +31,7 @@ public static class TurnRunner
// they can then know this later on.)
foreach (var choice in queue.GetChoices().WhereNotNull())
{
choice.RunScriptHook(script => script.OnBeforeTurnStart(choice));
choice.RunScriptHookInterface<IScriptOnBeforeTurnStart>(script => script.OnBeforeTurnStart(choice));
}
// Now we can properly begin executing choices.

View File

@@ -309,7 +309,7 @@ public class BattleImpl : ScriptSource, IBattle
}
ITurnChoice? forcedChoice = null;
pokemon.RunScriptHook(script =>
pokemon.RunScriptHookInterface<IScriptForceTurnSelection>(script =>
script.ForceTurnSelection(this, battleData.SideIndex, battleData.Position, ref forcedChoice));
choice = forcedChoice;
return choice != null;
@@ -375,12 +375,13 @@ public class BattleImpl : ScriptSource, IBattle
if (choice is IMoveChoice moveChoice)
{
var priority = moveChoice.ChosenMove.MoveData.Priority;
choice.RunScriptHook(script => script.ChangePriority(moveChoice, ref priority));
choice.RunScriptHookInterface<IScriptChangePriority>(script =>
script.ChangePriority(moveChoice, ref priority));
moveChoice.Priority = priority;
}
var speed = choice.User.BoostedStats.Speed;
choice.RunScriptHook(script => script.ChangeSpeed(choice, ref speed));
choice.RunScriptHookInterface<IScriptChangeSpeed>(script => script.ChangeSpeed(choice, ref speed));
choice.Speed = speed;
choice.RandomValue = (uint)Random.GetInt();

View File

@@ -72,7 +72,7 @@ public class BattleChoiceQueue : IDeepCloneable
continue;
// Ensure that the speed is up to date
var speed = choice.User.BoostedStats.Speed;
choice.User.RunScriptHook(script => script.ChangeSpeed(choice, ref speed));
choice.User.RunScriptHookInterface<IScriptChangeSpeed>(script => script.ChangeSpeed(choice, ref speed));
choice.Speed = speed;
}

View File

@@ -61,109 +61,6 @@ public abstract class Script : IDeepCloneable
{
}
/// <summary>
/// Force a certain move choice to be selected. If the choice is set, the Pokemon will be forced
/// to use it, and will not be able to select any other choice.
/// </summary>
public virtual void ForceTurnSelection(IBattle battle, byte sideIndex, byte position, ref ITurnChoice? choice)
{
}
/// <summary>
/// This function is ran just before the start of the turn. Everyone has made its choices here,
/// and the turn is about to start. This is a great place to initialize data if you need to know
/// something has happened during a turn.
/// </summary>
public virtual void OnBeforeTurnStart(ITurnChoice choice)
{
}
/// <summary>
/// This function allows you to modify the effective speed of the Pokemon. This is run before
/// turn ordering, so overriding here will allow you to put certain Pokemon before others.
/// </summary>
public virtual void ChangeSpeed(ITurnChoice choice, ref uint speed)
{
}
/// <summary>
/// This function allows you to modify the effective priority of the Pokemon. This is run before
/// turn ordering, so overriding here will allow you to put certain Pokemon before others. Note
/// that this is only relevant on move choices, as other turn choice types do not have a priority.
/// </summary>
public virtual void ChangePriority(IMoveChoice choice, ref sbyte priority)
{
}
/// <summary>
/// This function allows you to change the move that is used during execution. This is useful for
/// moves such as metronome, where the move chosen actually differs from the move used.
/// </summary>
public virtual void ChangeMove(IMoveChoice choice, ref StringKey moveName)
{
}
/// <summary>
/// Changes the targets of a move choice. This allows for changing the targets of a move before the move starts.
/// </summary>
public virtual void ChangeTargets(IMoveChoice moveChoice, ref IReadOnlyList<IPokemon?> targets)
{
}
/// <summary>
/// This function allows you to change the targets of a move choice before the move starts.
/// </summary>
public virtual void ChangeIncomingTargets(IMoveChoice moveChoice, ref IReadOnlyList<IPokemon?> targets)
{
}
/// <summary>
/// This function allows you to change a move into a multi-hit move. The number of hits set here
/// gets used as the number of hits. If set to 0, this will behave as if the move missed on its
/// first hit.
/// </summary>
public virtual void ChangeNumberOfHits(IMoveChoice choice, ref byte numberOfHits)
{
}
/// <summary>
/// This function allows you to prevent a move from running. If this gets set to true, the move
/// ends execution here. No PP will be decreased in this case.
/// </summary>
public virtual void PreventMove(IExecutingMove move, ref bool prevent)
{
}
/// <summary>
/// This function makes the move fail. If the fail field gets set to true, the move ends execution,
/// and fail events get triggered.
/// </summary>
public virtual void FailMove(IExecutingMove move, ref bool fail)
{
}
/// <summary>
/// Similar to <see cref="PreventMove"/>. This function will also stop execution of the move, but
/// PP will still be decreased.
/// </summary>
public virtual void StopBeforeMove(IExecutingMove move, ref bool stop)
{
}
/// <summary>
/// This function runs just before the move starts its execution.
/// </summary>
public virtual void OnBeforeMove(IExecutingMove move)
{
}
/// <summary>
/// This function runs immediately after all targets have had their hits executed.
/// </summary>
public virtual void OnAfterMove(IExecutingMove move)
{
}
/// <summary>
/// This function allows a script to prevent a move that is targeted at its owner. If set to true
/// the move fails, and fail events get triggered.
@@ -858,4 +755,159 @@ public interface IScriptPreventMoveSelection
/// Override to customize whether the move can be selected at all.
/// </summary>
void PreventMoveSelection(IMoveChoice choice, ref bool prevent);
}
/// <summary>
/// This interface is used to allow scripts to force a certain turn choice to be selected.
/// </summary>
public interface IScriptForceTurnSelection
{
/// <summary>
/// Force a certain move choice to be selected. If the choice is set, the Pokemon will be forced
/// to use it, and will not be able to select any other choice.
/// </summary>
void ForceTurnSelection(IBattle battle, byte sideIndex, byte position, ref ITurnChoice? choice);
}
/// <summary>
/// This interface is used to allow scripts to run before the start of a turn.
/// </summary>
public interface IScriptOnBeforeTurnStart
{
/// <summary>
/// This function is ran just before the start of the turn. Everyone has made its choices here,
/// and the turn is about to start. This is a great place to initialize data if you need to know
/// something has happened during a turn.
/// </summary>
void OnBeforeTurnStart(ITurnChoice choice);
}
/// <summary>
/// This interface allows scripts to modify the effective speed of the Pokemon.
/// </summary>
public interface IScriptChangeSpeed
{
/// <summary>
/// This function allows you to modify the effective speed of the Pokemon. This is run before
/// turn ordering, so overriding here will allow you to put certain Pokemon before others.
/// </summary>
void ChangeSpeed(ITurnChoice choice, ref uint speed);
}
/// <summary>
/// This interface allows scripts to modify the effective priority of the Pokemon.
/// </summary>
public interface IScriptChangePriority
{
/// <summary>
/// This function allows you to modify the effective priority of the Pokemon. This is run before
/// turn ordering, so overriding here will allow you to put certain Pokemon before others. Note
/// that this is only relevant on move choices, as other turn choice types do not have a priority.
/// </summary>
void ChangePriority(IMoveChoice choice, ref sbyte priority);
}
/// <summary>
/// This interface allows scripts to change the move that is used during execution.
/// </summary>
public interface IScriptChangeMove
{
/// <summary>
/// This function allows you to change the move that is used during execution. This is useful for
/// moves such as metronome, where the move chosen actually differs from the move used.
/// </summary>
void ChangeMove(IMoveChoice choice, ref StringKey moveName);
}
/// <summary>
/// This interface allows scripts to change the targets of a move choice.
/// </summary>
public interface IScriptChangeTargets
{
/// <summary>
/// Changes the targets of a move choice. This allows for changing the targets of a move before the move starts.
/// </summary>
void ChangeTargets(IMoveChoice moveChoice, ref IReadOnlyList<IPokemon?> targets);
}
/// <summary>
/// This interface allows scripts to change the incoming targets of a move choice.
/// </summary>
public interface IScriptChangeIncomingTargets
{
/// <summary>
/// This function allows you to change the targets of a move choice before the move starts.
/// </summary>
void ChangeIncomingTargets(IMoveChoice moveChoice, ref IReadOnlyList<IPokemon?> targets);
}
/// <summary>
/// This interface allows scripts to change a move into a multi-hit move.
/// </summary>
public interface IScriptChangeNumberOfHits
{
/// <summary>
/// This function allows you to change a move into a multi-hit move. The number of hits set here
/// gets used as the number of hits. If set to 0, this will behave as if the move missed on its
/// first hit.
/// </summary>
void ChangeNumberOfHits(IMoveChoice choice, ref byte numberOfHits);
}
/// <summary>
/// This interface allows scripts to prevent a move from running.
/// </summary>
public interface IScriptPreventMove
{
/// <summary>
/// This function allows you to prevent a move from running. If this gets set to true, the move
/// ends execution here. No PP will be decreased in this case.
/// </summary>
void PreventMove(IExecutingMove move, ref bool prevent);
}
/// <summary>
/// This interface allows scripts to make the move fail.
/// </summary>
public interface IScriptFailMove
{
/// <summary>
/// This function makes the move fail. If the fail field gets set to true, the move ends execution,
/// and fail events get triggered.
/// </summary>
void FailMove(IExecutingMove move, ref bool fail);
}
/// <summary>
/// This interface allows scripts to stop execution of the move before it starts.
/// </summary>
public interface IScriptStopBeforeMove
{
/// <summary>
/// Similar to <see cref="IScriptPreventMove"/>. This function will also stop execution of the move, but
/// PP will still be decreased.
/// </summary>
void StopBeforeMove(IExecutingMove move, ref bool stop);
}
/// <summary>
/// This interface allows scripts to run just before the move starts its execution.
/// </summary>
public interface IScriptOnBeforeMove
{
/// <summary>
/// This function runs just before the move starts its execution.
/// </summary>
void OnBeforeMove(IExecutingMove move);
}
/// <summary>
/// This interface allows scripts to run immediately after all targets have had their hits executed.
/// </summary>
public interface IScriptOnAfterMove
{
/// <summary>
/// This function runs immediately after all targets have had their hits executed.
/// </summary>
void OnAfterMove(IExecutingMove move);
}