PkmnLibSharp/PkmnLibSharp/Battling/PokemonBuilder.cs

160 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
using PkmnLibSharp.Library;
using PkmnLibSharp.Library.Items;
using Random = PkmnLibSharp.Utilities.Random;
namespace PkmnLibSharp.Battling
{
public class PokemonBuilder
{
private BattleLibrary _library;
public string Species { get; }
public string Forme { get; private set; } = "default";
public string Nickname { get; private set; } = "";
public string Ability { get; private set; } = "";
public string Nature { get; private set; } = "";
public Gender Gender { get; private set; } = (Gender) (-1);
public uint Identifier { get; private set; } = 0;
public byte Level { get; }
public StatisticSet<byte> IVs;
public StatisticSet<byte> EVs;
public bool? IsForceShiny { get; private set; }
public string HeldItem { get; private set; }
public List<(string moveName, MoveLearnMethod learnMethod)> LearnedMoves { get; } =
new List<(string moveName, MoveLearnMethod learnMethod)>();
public PokemonBuilder(BattleLibrary library, string species, byte level)
{
_library = library;
Species = species;
Level = level;
}
public PokemonBuilder WithForme(string forme)
{
Forme = forme;
return this;
}
public PokemonBuilder WithGender(Gender gender)
{
Gender = gender;
return this;
}
public PokemonBuilder ForceShiny(bool value)
{
IsForceShiny = value;
return this;
}
public PokemonBuilder WithHeldItem(string heldItem)
{
HeldItem = heldItem;
return this;
}
public PokemonBuilder WithNickname(string nickname)
{
Nickname = nickname;
return this;
}
public PokemonBuilder LearnMove(string moveName, MoveLearnMethod moveLearnMethod)
{
if (LearnedMoves.Count > _library.StaticLibrary.Settings.MaximalMoves)
{
throw new Exception("Too many moves for Pokemon.");
}
LearnedMoves.Add((moveName, moveLearnMethod));
return this;
}
public Pokemon Build()
{
if (!_library.StaticLibrary.SpeciesLibrary.TryGet(Species, out var species))
{
throw new Exception($"Species '{Species}' was not found.");
}
if (!species.TryGetForme(Forme, out var forme))
{
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))
{
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];
for (var i = 0; i < LearnedMoves.Count; i++)
{
if (!_library.StaticLibrary.MoveLibrary.TryGet(LearnedMoves[i].moveName, out var move))
{
throw new Exception($"Move '{LearnedMoves[i].moveName}' was not found.");
}
moves[i] = LearnedMove.Create(move, move.BaseUsages, LearnedMoves[i].learnMethod);
}
if (string.IsNullOrEmpty(Nature))
{
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);
}
}
}