Reworks PokemonBuilder.

This commit is contained in:
Deukhoofd 2020-08-08 17:07:05 +02:00
parent 7f4ccbfe2a
commit 69ab7b197c
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
14 changed files with 112 additions and 76 deletions

View File

@ -1,5 +1,3 @@
using System;
namespace PkmnLibSharp.Battling.ChoiceTurn
{
public class FleeTurnChoice : BaseTurnChoice

View File

@ -1,5 +1,3 @@
using System;
namespace PkmnLibSharp.Battling.ChoiceTurn
{
public class SwitchTurnChoice : BaseTurnChoice

View File

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using PkmnLibSharp.Library;
using PkmnLibSharp.Utilities;

View File

@ -6,9 +6,9 @@ using Random = PkmnLibSharp.Utilities.Random;
namespace PkmnLibSharp.Battling
{
public class PokemonBuilder
public abstract class BasePokemonBuilder<T> where T : Pokemon
{
private BattleLibrary _library;
protected readonly BattleLibrary Library;
public string Species { get; }
public string Forme { get; private set; } = "default";
public string Nickname { get; private set; } = "";
@ -27,46 +27,46 @@ namespace PkmnLibSharp.Battling
public List<(string moveName, MoveLearnMethod learnMethod)> LearnedMoves { get; } =
new List<(string moveName, MoveLearnMethod learnMethod)>();
public PokemonBuilder(BattleLibrary library, string species, byte level)
public BasePokemonBuilder(BattleLibrary library, string species, byte level)
{
_library = library;
Library = library;
Species = species;
Level = level;
}
public PokemonBuilder WithForme(string forme)
public BasePokemonBuilder<T> WithForme(string forme)
{
Forme = forme;
return this;
}
public PokemonBuilder WithGender(Gender gender)
public BasePokemonBuilder<T> WithGender(Gender gender)
{
Gender = gender;
return this;
}
public PokemonBuilder ForceShiny(bool value)
public BasePokemonBuilder<T> ForceShiny(bool value)
{
IsForceShiny = value;
return this;
}
public PokemonBuilder WithHeldItem(string heldItem)
public BasePokemonBuilder<T> WithHeldItem(string heldItem)
{
HeldItem = heldItem;
return this;
}
public PokemonBuilder WithNickname(string nickname)
public BasePokemonBuilder<T> WithNickname(string nickname)
{
Nickname = nickname;
return this;
}
public PokemonBuilder LearnMove(string moveName, MoveLearnMethod moveLearnMethod)
public BasePokemonBuilder<T> LearnMove(string moveName, MoveLearnMethod moveLearnMethod)
{
if (LearnedMoves.Count > _library.StaticLibrary.Settings.MaximalMoves)
if (LearnedMoves.Count > Library.StaticLibrary.Settings.MaximalMoves)
{
throw new Exception("Too many moves for Pokemon.");
}
@ -74,9 +74,60 @@ namespace PkmnLibSharp.Battling
return this;
}
public Pokemon Build()
protected byte Coloring;
protected uint Experience;
protected uint Uid;
protected int AbilityIndex;
protected bool HiddenAbility;
protected virtual void PopulateUninitialized(Species species, Forme forme, Random random)
{
if (!_library.StaticLibrary.SpeciesLibrary.TryGet(Species, out var species))
Experience = Library.StaticLibrary.GrowthRateLibrary.CalculateExperience(species.GrowthRate, Level);
Uid = random.GetUnsigned(0, uint.MaxValue);
if (Gender == (Gender) (-1))
{
Gender = species.GetRandomGender(random);
}
Coloring = 0;
if (IsForceShiny.HasValue)
{
Coloring = (byte) (IsForceShiny.Value ? 1 : 0);
}
else if (random.Get(Library.StaticLibrary.Settings.ShinyRate) == 0)
{
Coloring = 1;
}
AbilityIndex = 0;
HiddenAbility = false;
if (string.IsNullOrEmpty(Ability))
{
AbilityIndex = forme!.GetRandomAbility(random);
}
else
{
AbilityIndex = forme!.Abilities.IndexOf(Ability);
if (AbilityIndex == -1)
{
AbilityIndex = forme.HiddenAbilities.IndexOf(Ability);
if (AbilityIndex == -1)
{
throw new Exception(
$"Invalid ability '{Ability}' for Pokemon '{species.Name}' and forme '{forme.Name}'.");
}
HiddenAbility = true;
}
}
}
protected abstract T Finalize(Species species, Forme forme, Item? heldItem,
IReadOnlyCollection<LearnedMove> moves, Nature nature);
public T Build()
{
if (!Library.StaticLibrary.SpeciesLibrary.TryGet(Species, out var species))
{
throw new Exception($"Species '{Species}' was not found.");
}
@ -86,59 +137,21 @@ namespace PkmnLibSharp.Battling
throw new Exception($"Forme '{Forme}' was not found on species '{Species}'");
}
var experience = _library.StaticLibrary.GrowthRateLibrary.CalculateExperience(species.GrowthRate, Level);
var random = new Random();
var uid = random.GetUnsigned(0, uint.MaxValue);
if (Gender == (Gender) (-1))
{
Gender = species.GetRandomGender(random);
}
byte coloring = 0;
if (IsForceShiny.HasValue)
{
coloring = (byte) (IsForceShiny.Value ? 1 : 0);
}
else if (random.Get(_library.StaticLibrary.Settings.ShinyRate) == 0)
{
coloring = 1;
}
Item? heldItem = null;
if (HeldItem != null)
{
if (!_library.StaticLibrary.ItemLibrary.TryGet(HeldItem, out heldItem))
if (!Library.StaticLibrary.ItemLibrary.TryGet(HeldItem, out heldItem))
{
throw new Exception($"Item '{HeldItem}' was not found.");
}
}
var abilityIndex = 0;
var isHiddenAbility = false;
if (string.IsNullOrEmpty(Ability))
{
abilityIndex = forme!.GetRandomAbility(random);
}
else
{
abilityIndex = forme!.Abilities.IndexOf(Ability);
if (abilityIndex == -1)
{
abilityIndex = forme.HiddenAbilities.IndexOf(Ability);
if (abilityIndex == -1)
{
throw new Exception(
$"Invalid ability '{Ability}' for Pokemon '{species.Name}' and forme '{forme.Name}'.");
}
isHiddenAbility = true;
}
}
var moves = new LearnedMove[_library.StaticLibrary.Settings.MaximalMoves];
var moves = new LearnedMove[Library.StaticLibrary.Settings.MaximalMoves];
for (var i = 0; i < LearnedMoves.Count; i++)
{
if (!_library.StaticLibrary.MoveLibrary.TryGet(LearnedMoves[i].moveName, out var move))
if (!Library.StaticLibrary.MoveLibrary.TryGet(LearnedMoves[i].moveName, out var move))
{
throw new Exception($"Move '{LearnedMoves[i].moveName}' was not found.");
}
@ -148,13 +161,11 @@ namespace PkmnLibSharp.Battling
if (string.IsNullOrEmpty(Nature))
{
Nature = _library.StaticLibrary.NatureLibrary.GetRandomNatureName(random);
Nature = Library.StaticLibrary.NatureLibrary.GetRandomNatureName(random);
}
var nature = _library.StaticLibrary.NatureLibrary.GetNature(Nature);
return new Pokemon(_library, species, forme, Level, experience, uid, Gender, coloring, heldItem, Nickname,
isHiddenAbility, (byte) abilityIndex, moves, IVs, EVs, nature);
var nature = Library.StaticLibrary.NatureLibrary.GetNature(Nature);
return Finalize(species, forme!, heldItem, moves, nature);
}
}
}

View File

@ -80,5 +80,22 @@ namespace Pkmnlib.Generated
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_Pokemon_GetStatusName")]
internal static extern IntPtr GetStatusName(IntPtr p);
/// <param name="p">const Pokemon *</param>
/// <returns>unsigned char</returns>
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_Pokemon_GetFriendship")]
internal static extern byte GetFriendship(IntPtr p);
/// <param name="p">Pokemon *</param>
/// <param name="value">unsigned char</param>
/// <returns>void</returns>
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_Pokemon_SetFriendship")]
internal static extern void SetFriendship(IntPtr p, byte value);
/// <param name="p">Pokemon *</param>
/// <param name="amount">signed char</param>
/// <returns>void</returns>
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_Pokemon_ChangeFriendship")]
internal static extern void ChangeFriendship(IntPtr p, sbyte amount);
}
}

View File

@ -1,5 +1,4 @@
using System;
using Pkmnlib;
using PkmnLibSharp.Utilities;
namespace PkmnLibSharp.Library

View File

@ -1,4 +1,3 @@
using System;
using PkmnLibSharp.Utilities;
namespace PkmnLibSharp.Library.GrowthRates

View File

@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using Creaturelib.Generated;
using Pkmnlib;
using Pkmnlib.Generated;
using PkmnLibSharp.Utilities;
using EvolutionData = PkmnLibSharp.Library.Evolution.EvolutionData;
using Gender = PkmnLibSharp.Library.Gender;
using Random = PkmnLibSharp.Utilities.Random;
namespace PkmnLibSharp.Library

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

Binary file not shown.

View File

@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Runtime.InteropServices;
namespace PkmnLibSharp.Utilities

View File

@ -1,5 +1,3 @@
using System;
namespace PkmnLibSharp.Utilities
{
public class Random : PointerWrapper

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,4 @@
using System.Linq;
using NUnit.Framework;
using PkmnLibSharp.Battling;
using PkmnLibSharp.Library;
using PkmnLibSharp.Library.GrowthRates;

View File

@ -0,0 +1,21 @@
using System.Collections.Generic;
using PkmnLibSharp.Battling;
using PkmnLibSharp.Library;
using PkmnLibSharp.Library.Items;
namespace PkmnLibSharpTests.Battling
{
public class PokemonBuilder : BasePokemonBuilder<Pokemon>
{
public PokemonBuilder(BattleLibrary library, string species, byte level) : base(library, species, level)
{
}
protected override Pokemon Finalize(Species species, Forme forme, Item? heldItem, IReadOnlyCollection<LearnedMove> moves, Nature nature)
{
return new Pokemon(Library, species, forme!, Level, Experience, Uid, Gender, Coloring,
heldItem, Nickname, HiddenAbility, (byte) AbilityIndex, moves, IVs, EVs, nature);
}
}
}