Initial battle class tests.

This commit is contained in:
Deukhoofd 2020-07-26 15:40:08 +02:00
parent d29aa60010
commit 283825b355
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
12 changed files with 119 additions and 11 deletions

View File

@ -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

View File

@ -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<BattleParty> _parties = new List<BattleParty>();
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);
}
}
}

View File

@ -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);
}

View File

@ -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;
}
}
}

View File

@ -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();
}
}
}

View File

@ -7,7 +7,7 @@ namespace PkmnLibSharp.Battling
{
private ReadOnlyNativePtrArray<Pokemon> _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(

BIN
PkmnLibSharp/Native/libArbutils.so (Stored with Git LFS)

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
PkmnLibSharp/Native/libpkmnLib.so (Stored with Git LFS)

Binary file not shown.

View File

@ -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();
}
}
}

View File

@ -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();
}
}