From 40803f02693efb3ce50abfc4d31a31ad95f97fda Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 29 Dec 2024 13:51:59 +0100 Subject: [PATCH] Support for deep cloning battles and Pokemon --- PkmnLib.Dynamic/Models/Battle.cs | 2 +- PkmnLib.Dynamic/Models/BattleChoiceQueue.cs | 3 +- PkmnLib.Dynamic/Models/BattleParty.cs | 4 +- PkmnLib.Dynamic/Models/BattleSide.cs | 2 +- PkmnLib.Dynamic/Models/Choices/TurnChoice.cs | 3 +- PkmnLib.Dynamic/Models/LearnedMove.cs | 3 +- PkmnLib.Dynamic/Models/Pokemon.cs | 4 +- PkmnLib.Dynamic/Models/PokemonParty.cs | 3 +- PkmnLib.Dynamic/ScriptHandling/Script.cs | 2 +- .../ScriptHandling/ScriptContainer.cs | 3 +- PkmnLib.Static/StatisticSet.cs | 3 +- PkmnLib.Static/Utils/DeepClone.cs | 196 ++++++++++++++++++ PkmnLib.Tests/Static/DeepCloneTests.cs | 140 +++++++++++++ 13 files changed, 356 insertions(+), 12 deletions(-) create mode 100644 PkmnLib.Static/Utils/DeepClone.cs create mode 100644 PkmnLib.Tests/Static/DeepCloneTests.cs diff --git a/PkmnLib.Dynamic/Models/Battle.cs b/PkmnLib.Dynamic/Models/Battle.cs index da563a9..a807983 100644 --- a/PkmnLib.Dynamic/Models/Battle.cs +++ b/PkmnLib.Dynamic/Models/Battle.cs @@ -11,7 +11,7 @@ namespace PkmnLib.Dynamic.Models; /// A battle is a representation of a battle in the Pokemon games. It contains all the information needed /// to simulate a battle, and can be used to simulate a battle between two parties. /// -public interface IBattle : IScriptSource +public interface IBattle : IScriptSource, IDeepCloneable { /// /// The library the battle uses for handling. diff --git a/PkmnLib.Dynamic/Models/BattleChoiceQueue.cs b/PkmnLib.Dynamic/Models/BattleChoiceQueue.cs index 9087efa..2e47495 100644 --- a/PkmnLib.Dynamic/Models/BattleChoiceQueue.cs +++ b/PkmnLib.Dynamic/Models/BattleChoiceQueue.cs @@ -1,5 +1,6 @@ using PkmnLib.Dynamic.Models.Choices; using PkmnLib.Dynamic.ScriptHandling; +using PkmnLib.Static.Utils; namespace PkmnLib.Dynamic.Models; @@ -11,7 +12,7 @@ namespace PkmnLib.Dynamic.Models; /// It holds several helper functions to change the turn order while doing the execution. This is needed, as several /// moves in Pokémon actively mess with this order. /// -public class BattleChoiceQueue +public class BattleChoiceQueue : IDeepCloneable { private readonly ITurnChoice?[] _choices; private int _currentIndex; diff --git a/PkmnLib.Dynamic/Models/BattleParty.cs b/PkmnLib.Dynamic/Models/BattleParty.cs index 1ae02d2..93f2701 100644 --- a/PkmnLib.Dynamic/Models/BattleParty.cs +++ b/PkmnLib.Dynamic/Models/BattleParty.cs @@ -1,10 +1,12 @@ +using PkmnLib.Static.Utils; + namespace PkmnLib.Dynamic.Models; /// /// A battle party is a wrapper around a Pokemon party that provides additional functionality for battles. /// It indicates for which side and position the party is responsible. /// -public interface IBattleParty +public interface IBattleParty : IDeepCloneable { /// /// The backing Pokemon party. diff --git a/PkmnLib.Dynamic/Models/BattleSide.cs b/PkmnLib.Dynamic/Models/BattleSide.cs index 796028e..a0da422 100644 --- a/PkmnLib.Dynamic/Models/BattleSide.cs +++ b/PkmnLib.Dynamic/Models/BattleSide.cs @@ -8,7 +8,7 @@ namespace PkmnLib.Dynamic.Models; /// /// A side in a battle. /// -public interface IBattleSide : IScriptSource +public interface IBattleSide : IScriptSource, IDeepCloneable { /// /// The index of the side on the battle. diff --git a/PkmnLib.Dynamic/Models/Choices/TurnChoice.cs b/PkmnLib.Dynamic/Models/Choices/TurnChoice.cs index 2df1487..6be3be6 100644 --- a/PkmnLib.Dynamic/Models/Choices/TurnChoice.cs +++ b/PkmnLib.Dynamic/Models/Choices/TurnChoice.cs @@ -1,11 +1,12 @@ using PkmnLib.Dynamic.ScriptHandling; +using PkmnLib.Static.Utils; namespace PkmnLib.Dynamic.Models.Choices; /// /// A choice that is made at the beginning of a turn. This can be a switch, flee, item, or pass choice. /// -public interface ITurnChoice : IScriptSource +public interface ITurnChoice : IScriptSource, IDeepCloneable { /// /// The user of the turn choice diff --git a/PkmnLib.Dynamic/Models/LearnedMove.cs b/PkmnLib.Dynamic/Models/LearnedMove.cs index 4e60a1d..771eba1 100644 --- a/PkmnLib.Dynamic/Models/LearnedMove.cs +++ b/PkmnLib.Dynamic/Models/LearnedMove.cs @@ -1,4 +1,5 @@ using PkmnLib.Static.Moves; +using PkmnLib.Static.Utils; namespace PkmnLib.Dynamic.Models; @@ -42,7 +43,7 @@ public enum MoveLearnMethod /// A learned move is the data attached to a Pokemon for a move it has learned. It has information /// such as the remaining amount of users, how it has been learned, etc. /// -public interface ILearnedMove +public interface ILearnedMove : IDeepCloneable { /// /// The immutable move information of the move. diff --git a/PkmnLib.Dynamic/Models/Pokemon.cs b/PkmnLib.Dynamic/Models/Pokemon.cs index 2111bb7..4aa0bd6 100644 --- a/PkmnLib.Dynamic/Models/Pokemon.cs +++ b/PkmnLib.Dynamic/Models/Pokemon.cs @@ -12,7 +12,7 @@ namespace PkmnLib.Dynamic.Models; /// /// The data of a Pokemon. /// -public interface IPokemon : IScriptSource +public interface IPokemon : IScriptSource, IDeepCloneable { /// /// The library data of the Pokemon. @@ -352,7 +352,7 @@ public interface IPokemon : IScriptSource /// The data of the Pokémon related to being in a battle. /// This is only set when the Pokémon is on the field in a battle. /// -public interface IPokemonBattleData +public interface IPokemonBattleData : IDeepCloneable { /// /// The battle the Pokémon is in. diff --git a/PkmnLib.Dynamic/Models/PokemonParty.cs b/PkmnLib.Dynamic/Models/PokemonParty.cs index 09d3cd7..23e3871 100644 --- a/PkmnLib.Dynamic/Models/PokemonParty.cs +++ b/PkmnLib.Dynamic/Models/PokemonParty.cs @@ -1,11 +1,12 @@ using System.Collections; +using PkmnLib.Static.Utils; namespace PkmnLib.Dynamic.Models; /// /// A collection of Pokemon. /// -public interface IPokemonParty : IReadOnlyList +public interface IPokemonParty : IReadOnlyList, IDeepCloneable { event EventHandler<(IPokemon?, int index)>? OnSwapInto; event EventHandler<(int index1, int index2)>? OnSwap; diff --git a/PkmnLib.Dynamic/ScriptHandling/Script.cs b/PkmnLib.Dynamic/ScriptHandling/Script.cs index bf51a89..c54569e 100644 --- a/PkmnLib.Dynamic/ScriptHandling/Script.cs +++ b/PkmnLib.Dynamic/ScriptHandling/Script.cs @@ -12,7 +12,7 @@ namespace PkmnLib.Dynamic.ScriptHandling; /// changes. This allows for easily defining generational differences, and add effects that the /// developer might require. /// -public abstract class Script +public abstract class Script : IDeepCloneable { internal event Action