Adds several convenience features

This commit is contained in:
Deukhoofd 2024-09-30 14:20:45 +02:00
parent 257c04c98b
commit a39c77745d
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
9 changed files with 72 additions and 13 deletions

View File

@ -107,6 +107,11 @@ public interface IPokemon : IScriptSource
/// The stats of the Pokemon including the stat boosts /// The stats of the Pokemon including the stat boosts
/// </summary> /// </summary>
StatisticSet<uint> BoostedStats { get; } StatisticSet<uint> BoostedStats { get; }
/// <summary>
/// The maximum health of the Pokemon.
/// </summary>
uint MaxHealth { get; }
/// <summary> /// <summary>
/// The individual values of the Pokemon. /// The individual values of the Pokemon.
@ -268,6 +273,11 @@ public interface IPokemon : IScriptSource
/// heal if the Pokemon has 0 health. If the amount healed is 0, this will return false. /// heal if the Pokemon has 0 health. If the amount healed is 0, this will return false.
/// </summary> /// </summary>
bool Heal(uint heal, bool allowRevive); bool Heal(uint heal, bool allowRevive);
/// <summary>
/// Restores all PP of the Pokemon.
/// </summary>
void RestoreAllPP();
/// <summary> /// <summary>
/// Learn a move by name. /// Learn a move by name.
@ -489,6 +499,9 @@ public class PokemonImpl : ScriptSource, IPokemon
/// <inheritdoc /> /// <inheritdoc />
public StatisticSet<uint> BoostedStats { get; } = new(); public StatisticSet<uint> BoostedStats { get; } = new();
/// <inheritdoc />
public uint MaxHealth => BoostedStats.Hp;
/// <inheritdoc /> /// <inheritdoc />
public IndividualValueStatisticSet IndividualValues { get; } = new(); public IndividualValueStatisticSet IndividualValues { get; } = new();
@ -780,6 +793,15 @@ public class PokemonImpl : ScriptSource, IPokemon
return true; return true;
} }
/// <inheritdoc />
public void RestoreAllPP()
{
foreach (var move in Moves)
{
move?.RestoreAllUses();
}
}
/// <inheritdoc /> /// <inheritdoc />
/// <remarks> /// <remarks>
/// If the index is 255, it will try to find the first empty move slot. /// If the index is 255, it will try to find the first empty move slot.

View File

@ -40,7 +40,7 @@ public class PokemonParty : IPokemonParty
/// <summary> /// <summary>
/// Sets the Pokemon at an index to a Pokemon, returning the old Pokemon. /// Sets the Pokemon at an index to a Pokemon, returning the old Pokemon.
/// </summary> /// </summary>
public IPokemon? SwapInto(IPokemon pokemon, int index) public IPokemon? SwapInto(IPokemon? pokemon, int index)
{ {
var old = _pokemon[index]; var old = _pokemon[index];
_pokemon[index] = pokemon; _pokemon[index] = pokemon;
@ -68,4 +68,21 @@ public class PokemonParty : IPokemonParty
/// <inheritdoc /> /// <inheritdoc />
public IPokemon? this[int index] => _pokemon[index]; public IPokemon? this[int index] => _pokemon[index];
public void Pack()
{
// Pack the party so that all Pokémon are at the front.
for (var i = 0; i < _pokemon.Length; i++)
{
if (_pokemon[i] != null)
continue;
for (var j = i + 1; j < _pokemon.Length; j++)
{
if (_pokemon[j] == null)
continue;
Swap(i, j);
break;
}
}
}
} }

View File

@ -7,7 +7,7 @@ namespace PkmnLib.Static.Libraries;
/// <summary> /// <summary>
/// The library for all abilities in the game. /// The library for all abilities in the game.
/// </summary> /// </summary>
public interface IReadOnlyAbilityLibrary public interface IReadOnlyAbilityLibrary : IEnumerable<IAbility>
{ {
/// <summary> /// <summary>
/// Tries to get an ability from the library. Returns false if the ability is not found. /// Tries to get an ability from the library. Returns false if the ability is not found.

View File

@ -1,3 +1,4 @@
using System.Collections;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using PkmnLib.Static.Utils; using PkmnLib.Static.Utils;
@ -6,10 +7,13 @@ namespace PkmnLib.Static.Libraries;
/// <summary> /// <summary>
/// A basic library for data types. Stores data both by name and by index. /// A basic library for data types. Stores data both by name and by index.
/// </summary> /// </summary>
public abstract class DataLibrary<T> public abstract class DataLibrary<T> : IEnumerable<T>
where T : INamedValue where T : INamedValue
{ {
private readonly Dictionary<StringKey, T> _data = new(); /// <summary>
/// The underlying data storage.
/// </summary>
protected readonly Dictionary<StringKey, T> Data = new();
private readonly List<T> _values = []; private readonly List<T> _values = [];
/// <summary> /// <summary>
@ -17,7 +21,7 @@ public abstract class DataLibrary<T>
/// </summary> /// </summary>
public void Add(T value) public void Add(T value)
{ {
_data.Add(value.Name, value); Data.Add(value.Name, value);
_values.Add(value); _values.Add(value);
} }
@ -26,14 +30,14 @@ public abstract class DataLibrary<T>
/// </summary> /// </summary>
public void Remove(T value) public void Remove(T value)
{ {
_data.Remove(value.Name); Data.Remove(value.Name);
_values.Remove(value); _values.Remove(value);
} }
/// <summary> /// <summary>
/// Try to get a value from the library. Returns false if the value is not found. /// Try to get a value from the library. Returns false if the value is not found.
/// </summary> /// </summary>
public bool TryGet(StringKey key, [MaybeNullWhen(false)] out T value) => _data.TryGetValue(key, out value); public bool TryGet(StringKey key, [MaybeNullWhen(false)] out T value) => Data.TryGetValue(key, out value);
/// <summary> /// <summary>
/// Get a random value from the library. /// Get a random value from the library.
@ -49,4 +53,10 @@ public abstract class DataLibrary<T>
/// Whether the library is empty. /// Whether the library is empty.
/// </summary> /// </summary>
public bool IsEmpty => _values.Count == 0; public bool IsEmpty => _values.Count == 0;
/// <inheritdoc />
public IEnumerator<T> GetEnumerator() => _values.GetEnumerator();
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
} }

View File

@ -6,7 +6,7 @@ namespace PkmnLib.Static.Libraries;
/// <summary> /// <summary>
/// The library for all growth rates in the game. /// The library for all growth rates in the game.
/// </summary> /// </summary>
public interface IReadOnlyGrowthRateLibrary public interface IReadOnlyGrowthRateLibrary : IEnumerable<IGrowthRate>
{ {
/// <summary> /// <summary>
/// Tries to get a growth rate from the library. Returns false if the growth rate is not found. /// Tries to get a growth rate from the library. Returns false if the growth rate is not found.

View File

@ -6,7 +6,7 @@ namespace PkmnLib.Static.Libraries;
/// <summary> /// <summary>
/// The library for all items in the game. /// The library for all items in the game.
/// </summary> /// </summary>
public interface IReadOnlyItemLibrary public interface IReadOnlyItemLibrary : IEnumerable<IItem>
{ {
/// <summary> /// <summary>
/// Tries to get an item from the library. Returns false if the item is not found. /// Tries to get an item from the library. Returns false if the item is not found.

View File

@ -7,7 +7,7 @@ namespace PkmnLib.Static.Libraries;
/// <summary> /// <summary>
/// The library for all moves in the game. /// The library for all moves in the game.
/// </summary> /// </summary>
public interface IReadOnlyMoveLibrary public interface IReadOnlyMoveLibrary : IEnumerable<IMoveData>
{ {
/// <summary> /// <summary>
/// Tries to get a move from the library. Returns false if the move is not found. /// Tries to get a move from the library. Returns false if the move is not found.

View File

@ -6,7 +6,7 @@ namespace PkmnLib.Static.Libraries;
/// <summary> /// <summary>
/// The library for all natures in the game. /// The library for all natures in the game.
/// </summary> /// </summary>
public interface IReadOnlyNatureLibrary public interface IReadOnlyNatureLibrary : IEnumerable<INature>
{ {
/// <summary> /// <summary>
/// Tries to get a nature from the library. Returns false if the nature is not found. /// Tries to get a nature from the library. Returns false if the nature is not found.

View File

@ -7,12 +7,13 @@ namespace PkmnLib.Static.Libraries;
/// <summary> /// <summary>
/// The library for all species in the game. /// The library for all species in the game.
/// </summary> /// </summary>
public interface IReadOnlySpeciesLibrary public interface IReadOnlySpeciesLibrary : IEnumerable<ISpecies>
{ {
/// <summary> /// <summary>
/// Tries to get a species from the library. Returns false if the species is not found. /// Tries to get a species from the library. Returns false if the species is not found.
/// </summary> /// </summary>
bool TryGet(StringKey key, [MaybeNullWhen(false)] out ISpecies value); bool TryGet(StringKey key, [MaybeNullWhen(false)] out ISpecies value);
bool TryGetById(int id, [MaybeNullWhen(false)] out ISpecies value);
/// <summary> /// <summary>
/// Gets a random species from the library. /// Gets a random species from the library.
@ -31,4 +32,13 @@ public interface IReadOnlySpeciesLibrary
} }
/// <inheritdoc cref="IReadOnlySpeciesLibrary"/> /// <inheritdoc cref="IReadOnlySpeciesLibrary"/>
public class SpeciesLibrary : DataLibrary<ISpecies>, IReadOnlySpeciesLibrary; public class SpeciesLibrary : DataLibrary<ISpecies>, IReadOnlySpeciesLibrary
{
/// <inheritdoc />
public bool TryGetById(int id, [MaybeNullWhen(false)] out ISpecies value)
{
return this.FirstOrDefault(s => s.Id == id) is { } species
? (value = species) != null
: (value = default) != null;
}
}