PkmnLibRSharp/PkmnLibRSharp/StaticData/Species.cs

118 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.Species;
namespace PkmnLibSharp.StaticData
{
/// <summary>
/// The data belonging to a Pokemon with certain characteristics.
/// </summary>
public class Species : HandleType
{
/// <inheritdoc cref="Species"/>
protected Species(FFIHandle handle) : base(handle)
{
}
/// <summary>
/// Creates a new species.
/// </summary>
public static Species Create(ushort id, string name, float genderRate, string growthRate, byte captureRate,
Form defaultForm, IReadOnlyCollection<string> flags)
{
var flagsPtrArray = flags.Select(x => x.ToPtr()).ToArray();
var handle = Interface.species_new(id, name.ToPtr(), genderRate, growthRate.ToPtr(), captureRate,
defaultForm.Handle, flagsPtrArray.ArrayPtr(), (ulong)flags.Count);
var species = Resolver.Instance.ResolveSpecies(handle.Result().Resolve());
species._forms.Add("default", defaultForm);
return species;
}
private ushort? _id;
/// <summary>
/// The national dex identifier of the Pokemon.
/// </summary>
public ushort Id => _id ??= Interface.species_id(Handle);
private string? _name;
/// <summary>
/// The name of the Pokemon.
/// </summary>
public string Name => _name ??= Interface.species_name(Handle).PtrString()!;
private float? _genderRate;
/// <summary>
/// The chance between 0.0 and 1.0 that a Pokemon is female.
/// </summary>
public float GenderRate => _genderRate ??= Interface.species_gender_rate(Handle);
private string? _growthRate;
/// <summary>
/// How much experience is required for a level.
/// </summary>
public string GrowthRate => _growthRate ??= Interface.species_growth_rate(Handle).PtrString()!;
private byte? _captureRate;
/// <summary>
/// How hard it is to capture a Pokemon. 255 means this will be always caught, 0 means this is
/// uncatchable.
/// </summary>
public byte CaptureRate => _captureRate ??= Interface.species_capture_rate(Handle);
/// <summary>
/// Gets the form the Pokemon will have by default, if no other form is specified.
/// </summary>
public Form DefaultForm
{
get
{
TryGetForm("default", out var form);
return form!;
}
}
private readonly Dictionary<string, Form> _forms = new(StringComparer.InvariantCultureIgnoreCase);
/// <summary>
/// Gets a form by name.
/// </summary>
public bool TryGetForm(string formName, [NotNullWhen(true)] out Form? form)
{
if (_forms.TryGetValue(formName, out form))
return true;
var formPtr = Interface.species_get_form(Handle, formName.ToPtr());
if (formPtr.Handle == 0)
{
form = null;
return false;
}
form = Resolver.Instance.ResolveForm(formPtr.Resolve());
_forms.Add(formName, form);
return true;
}
/// <summary>
/// Adds a new form to the species.
/// </summary>
/// <param name="form"></param>
public void AddForm(Form form)
{
Interface.species_add_form(Handle, form.Name.ToPtr(), form.Handle).Result();
_forms.Add(form.Name, form);
}
/// <summary>
/// Gets a random gender, returning a value based on the species gender ratio.
/// </summary>
public Gender GetRandomGender(ulong seed) => Interface.species_get_random_gender(Handle, seed);
}
}