diff --git a/PkmnLibSharp/Battling/Battle/Battle.cs b/PkmnLibSharp/Battling/Battle/Battle.cs index b1b5e41..f943494 100644 --- a/PkmnLibSharp/Battling/Battle/Battle.cs +++ b/PkmnLibSharp/Battling/Battle/Battle.cs @@ -8,13 +8,14 @@ namespace PkmnLibSharp.Battling { public class Battle : PointerWrapper { - public Battle(BattleLibrary library, BattleParty[] parties, bool canFlee, byte numberOfSides, byte creaturesPerSide, + public Battle(BattleLibrary library, BattleParty[] parties, bool canFlee, byte numberOfSides, byte pokemonPerSide, ulong randomSeed) { var ptr = IntPtr.Zero; var arr = parties.Select(x => x.Ptr).ToArray(); Pkmnlib.Generated.Battle.Construct(ref ptr, library.Ptr, arr.ArrayPtr(), (ulong) arr.Length, - canFlee.ToNative(), numberOfSides, creaturesPerSide, randomSeed); + canFlee.ToNative(), numberOfSides, pokemonPerSide, randomSeed); + Initialize(ptr); } public BattleLibrary Library diff --git a/PkmnLibSharp/Battling/Battle/BattleBuilder.cs b/PkmnLibSharp/Battling/Battle/BattleBuilder.cs new file mode 100644 index 0000000..e314886 --- /dev/null +++ b/PkmnLibSharp/Battling/Battle/BattleBuilder.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace PkmnLibSharp.Battling +{ + public class BattleBuilder + { + private readonly BattleLibrary _library; + private readonly bool _canFlee; + private readonly byte _numberOfSides; + private readonly byte _pokemonPerSide; + private ulong? _seed; + private readonly List _parties = new List(); + + public BattleBuilder(BattleLibrary library, bool canFlee, byte numberOfSides = 2, byte pokemonPerSide = 1) + { + _library = library; + _canFlee = canFlee; + _numberOfSides = numberOfSides; + _pokemonPerSide = pokemonPerSide; + } + + public BattleBuilder WithRandomSeed(ulong seed) + { + _seed = seed; + return this; + } + + public BattleBuilder WithPartyOnPositions(PokemonParty party, params BattlePosition[] positions) + { + var battleParty = new BattleParty(party, positions.SelectMany(x => new[] {x.Side, x.Index}).ToArray()); + _parties.Add(battleParty); + return this; + } + + public Battle Build() + { + // Use the milliseconds since epoch time as random seed if none is specified. + _seed ??= (ulong) (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds; + return new Battle(_library, _parties.ToArray(), _canFlee, _numberOfSides, _pokemonPerSide, _seed.Value); + } + } +} \ No newline at end of file diff --git a/PkmnLibSharp/Battling/Battle/BattleParty.cs b/PkmnLibSharp/Battling/Battle/BattleParty.cs index 11ade4a..b9db01c 100644 --- a/PkmnLibSharp/Battling/Battle/BattleParty.cs +++ b/PkmnLibSharp/Battling/Battle/BattleParty.cs @@ -11,7 +11,7 @@ namespace PkmnLibSharp.Battling { var ptr = IntPtr.Zero; Creaturelibbattling.Generated.BattleParty.Construct(ref ptr, party.Ptr, responsibleIndices.ArrayPtr(), - (ulong) responsibleIndices.Length).Assert(); + (ulong) responsibleIndices.Length / 2).Assert(); Initialize(ptr); } diff --git a/PkmnLibSharp/Battling/Battle/BattlePosition.cs b/PkmnLibSharp/Battling/Battle/BattlePosition.cs new file mode 100644 index 0000000..0d6d50a --- /dev/null +++ b/PkmnLibSharp/Battling/Battle/BattlePosition.cs @@ -0,0 +1,14 @@ +namespace PkmnLibSharp.Battling +{ + public readonly struct BattlePosition + { + public readonly byte Side; + public readonly byte Index; + + public BattlePosition(byte side, byte index) + { + Side = side; + Index = index; + } + } +} \ No newline at end of file diff --git a/PkmnLibSharp/Battling/Pokemon.cs b/PkmnLibSharp/Battling/Pokemon.cs index 5be555b..8435709 100644 --- a/PkmnLibSharp/Battling/Pokemon.cs +++ b/PkmnLibSharp/Battling/Pokemon.cs @@ -319,11 +319,11 @@ namespace PkmnLibSharp.Battling protected internal override void MarkAsDeleted() { - base.MarkAsDeleted(); foreach (var move in Moves) { - move.MarkAsDeleted(); + move?.MarkAsDeleted(); } + base.MarkAsDeleted(); } } } \ No newline at end of file diff --git a/PkmnLibSharp/Battling/PokemonParty.cs b/PkmnLibSharp/Battling/PokemonParty.cs index e33365a..0f8b67d 100644 --- a/PkmnLibSharp/Battling/PokemonParty.cs +++ b/PkmnLibSharp/Battling/PokemonParty.cs @@ -7,7 +7,7 @@ namespace PkmnLibSharp.Battling { private ReadOnlyNativePtrArray _party; - public PokemonParty(ulong size) : base(Creaturelibbattling.Generated.CreatureParty.ConstructWithSize(size)) + public PokemonParty(ulong size = 6) : base(Creaturelibbattling.Generated.CreatureParty.ConstructWithSize(size)) {} public PokemonParty(Pokemon[] pokemon) : base( diff --git a/PkmnLibSharp/Native/libArbutils.so b/PkmnLibSharp/Native/libArbutils.so index 434ea18..bb50916 100755 --- a/PkmnLibSharp/Native/libArbutils.so +++ b/PkmnLibSharp/Native/libArbutils.so @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:76380cc667fa450d14995406a82309890ebcfd729a33dc1c2628b3979be945a3 +oid sha256:e2c84c858831b6d6e0309c4da7d3e7d4c4e0b13355864a4497e0792b02d2b864 size 35432 diff --git a/PkmnLibSharp/Native/libCreatureLibBattling.so b/PkmnLibSharp/Native/libCreatureLibBattling.so index 322d7aa..ae63c6e 100755 --- a/PkmnLibSharp/Native/libCreatureLibBattling.so +++ b/PkmnLibSharp/Native/libCreatureLibBattling.so @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:4e51719e4d4e9dabf2f56d8fafedd1cd2db245fc509b5415a80e488fd43816b5 -size 345192 +oid sha256:0ef3fe98dae421cbcfa0404e98daddcc8c54fc88b4ec9a1cd29e9099bf82d650 +size 345072 diff --git a/PkmnLibSharp/Native/libCreatureLibLibrary.so b/PkmnLibSharp/Native/libCreatureLibLibrary.so index 1065dc1..c116486 100755 --- a/PkmnLibSharp/Native/libCreatureLibLibrary.so +++ b/PkmnLibSharp/Native/libCreatureLibLibrary.so @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:bf06dd2d61ad59871742777851b7e524d7088d824174164fff3f6fac649fe017 +oid sha256:f35b7716912a51ba8bd606b36b76b550068299dc52e94d5f7fa0070536db30c5 size 203504 diff --git a/PkmnLibSharp/Native/libpkmnLib.so b/PkmnLibSharp/Native/libpkmnLib.so index abc7358..f265927 100755 --- a/PkmnLibSharp/Native/libpkmnLib.so +++ b/PkmnLibSharp/Native/libpkmnLib.so @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:316e6ff5d59ecc2dee3070684f3911ceb00eefad725caf7c1cfdcc6dc2b47eb9 +oid sha256:a98bf1f55cde87718b80ff300c9dd1e83816c547135abb28bf61556d157e315b size 1035736 diff --git a/PkmnLibSharpTests/Battling/BattleTests/BasicBattleTests.cs b/PkmnLibSharpTests/Battling/BattleTests/BasicBattleTests.cs new file mode 100644 index 0000000..6f2c278 --- /dev/null +++ b/PkmnLibSharpTests/Battling/BattleTests/BasicBattleTests.cs @@ -0,0 +1,45 @@ +using NUnit.Framework; +using PkmnLibSharp.Battling; + +namespace PkmnLibSharpTests.Battling.BattleTests +{ + public class BasicBattleTests + { + [Test] + public void InitializeBattle() + { + var lib = BattleLibraryHelper.GetLibrary(); + var battle = new BattleBuilder(lib, true, 2, 1).Build(); + Assert.AreEqual(lib, battle.Library); + Assert.AreEqual(true, battle.CanFlee); + Assert.AreEqual(2, battle.SidesCount); + Assert.AreEqual(false, battle.HasEnded); + Assert.AreEqual(0, battle.PartiesCount); + battle.Dispose(); + } + + private static PokemonParty BuildTestParty(BattleLibrary lib) + { + var party = new PokemonParty(); + party.SwapInto(0, new PokemonBuilder(lib, "testSpecies", 50).Build()); + return party; + } + + [Test] + public void InitializeBattleWithParties() + { + var lib = BattleLibraryHelper.GetLibrary(); + var battle = + new BattleBuilder(lib, true, 2, 1) + .WithPartyOnPositions(BuildTestParty(lib), new BattlePosition(0, 0)) + .WithPartyOnPositions(BuildTestParty(lib), new BattlePosition(1, 0)) + .Build(); + Assert.AreEqual(lib, battle.Library); + Assert.AreEqual(true, battle.CanFlee); + Assert.AreEqual(2, battle.SidesCount); + Assert.AreEqual(false, battle.HasEnded); + Assert.AreEqual(2, battle.PartiesCount); + battle.Dispose(); + } + } +} \ No newline at end of file diff --git a/PkmnLibSharpTests/Battling/PokemonBuilderTests.cs b/PkmnLibSharpTests/Battling/PokemonBuilderTests.cs index 38d9420..3dd804f 100644 --- a/PkmnLibSharpTests/Battling/PokemonBuilderTests.cs +++ b/PkmnLibSharpTests/Battling/PokemonBuilderTests.cs @@ -14,6 +14,7 @@ namespace PkmnLibSharpTests.Battling Assert.AreEqual("testSpecies", pokemon.Species.Name); Assert.AreEqual(50, pokemon.Level); Assert.AreEqual("default", pokemon.Forme.Name); + pokemon.Dispose(); } [Test] @@ -24,6 +25,7 @@ namespace PkmnLibSharpTests.Battling .WithNickname("cuteNickname") .Build(); Assert.AreEqual("cuteNickname", pokemon.Nickname); + pokemon.Dispose(); } [Test] @@ -34,6 +36,7 @@ namespace PkmnLibSharpTests.Battling .WithGender(Gender.Female) .Build(); Assert.AreEqual(Gender.Female, pokemon.Gender); + pokemon.Dispose(); } [Test] @@ -46,6 +49,7 @@ namespace PkmnLibSharpTests.Battling .Build(); Assert.AreEqual("testMove", pokemon.Moves[0].Move.Name); Assert.AreEqual("testMove2", pokemon.Moves[1].Move.Name); + pokemon.Dispose(); } }