Bug fixes, add type name to TypeIdentifier

This commit is contained in:
Deukhoofd 2025-03-07 12:57:06 +01:00
parent 3571b2130e
commit 2c987e32ee
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
9 changed files with 29 additions and 34 deletions

View File

@ -46,7 +46,8 @@ public static class TypeDataLoader
throw new InvalidDataException( throw new InvalidDataException(
$"Effectiveness for {type} against {types[i]} is invalid: {effectiveness}. Must be greater than or equal to 0.0."); $"Effectiveness for {type} against {types[i]} is invalid: {effectiveness}. Must be greater than or equal to 0.0.");
} }
library.SetEffectiveness(typeId, (TypeIdentifier)i, effectiveness); library.TryGetTypeIdentifier(types[i], out var defendingTypeId);
library.SetEffectiveness(typeId, defendingTypeId, effectiveness);
} }
} }

View File

@ -13,11 +13,6 @@ public interface IReadOnlyTypeLibrary
/// </summary> /// </summary>
bool TryGetTypeIdentifier(StringKey key, out TypeIdentifier typeIdentifier); bool TryGetTypeIdentifier(StringKey key, out TypeIdentifier typeIdentifier);
/// <summary>
/// Gets the type name from the type identifier.
/// </summary>
bool TryGetTypeName(TypeIdentifier t, [NotNullWhen(true)] out StringKey? stringKey);
/// <summary> /// <summary>
/// Gets the effectiveness for a single attacking type against a single defending type. /// Gets the effectiveness for a single attacking type against a single defending type.
/// </summary> /// </summary>
@ -35,25 +30,19 @@ public interface IReadOnlyTypeLibrary
/// <inheritdoc /> /// <inheritdoc />
public class TypeLibrary : IReadOnlyTypeLibrary public class TypeLibrary : IReadOnlyTypeLibrary
{ {
private readonly Dictionary<StringKey, TypeIdentifier> _types = new(); private readonly List<TypeIdentifier> _types = new();
private readonly List<List<float>> _effectiveness = new(); private readonly List<List<float>> _effectiveness = new();
/// <inheritdoc /> /// <inheritdoc />
public bool TryGetTypeIdentifier(StringKey key, out TypeIdentifier type) => _types.TryGetValue(key, out type); public bool TryGetTypeIdentifier(StringKey key, out TypeIdentifier type)
/// <inheritdoc />
public bool TryGetTypeName(TypeIdentifier t, [NotNullWhen(true)] out StringKey? stringKey)
{ {
foreach (var (key, value) in _types) var found = _types.FirstOrDefault(t => t.Name.Equals(key));
if (found.Value is not 0)
{ {
if (value == t) type = found;
{
stringKey = key;
return true; return true;
} }
} type = default;
stringKey = default;
return false; return false;
} }
@ -79,7 +68,8 @@ public class TypeLibrary : IReadOnlyTypeLibrary
throw new ArgumentOutOfRangeException(nameof(attacking)); throw new ArgumentOutOfRangeException(nameof(attacking));
for (var i = 0; i < _effectiveness.Count; i++) for (var i = 0; i < _effectiveness.Count; i++)
{ {
yield return (new TypeIdentifier((byte)(i + 1)), _effectiveness[attacking.Value - 1][i]); var type = _types[i];
yield return (type, _effectiveness[attacking.Value - 1][i]);
} }
} }
@ -88,8 +78,8 @@ public class TypeLibrary : IReadOnlyTypeLibrary
/// </summary> /// </summary>
public TypeIdentifier RegisterType(StringKey name) public TypeIdentifier RegisterType(StringKey name)
{ {
var id = new TypeIdentifier((byte)(_types.Count + 1)); var id = new TypeIdentifier((byte)(_types.Count + 1), name);
_types.Add(name, id); _types.Add(id);
_effectiveness.Add(Enumerable.Repeat(1.0f, _effectiveness.Count).ToList()); _effectiveness.Add(Enumerable.Repeat(1.0f, _effectiveness.Count).ToList());
foreach (var list in _effectiveness) foreach (var list in _effectiveness)
{ {

View File

@ -1,4 +1,5 @@
using PkmnLib.Static.Libraries; using PkmnLib.Static.Libraries;
using PkmnLib.Static.Utils;
namespace PkmnLib.Static; namespace PkmnLib.Static;
@ -7,22 +8,23 @@ namespace PkmnLib.Static;
/// </summary> /// </summary>
public readonly record struct TypeIdentifier public readonly record struct TypeIdentifier
{ {
/// <summary>
/// The name of the type identifier.
/// </summary>
public StringKey Name { get; }
/// <summary> /// <summary>
/// The underlying value of the type identifier. /// The underlying value of the type identifier.
/// </summary> /// </summary>
public byte Value { get; } public byte Value { get; }
/// <inheritdoc cref="TypeIdentifier"/> /// <inheritdoc cref="TypeIdentifier"/>
public TypeIdentifier(byte value) public TypeIdentifier(byte value, StringKey name)
{ {
Value = value; Value = value;
Name = name;
} }
/// <summary>
/// Converts a byte to a type identifier.
/// </summary>
public static implicit operator TypeIdentifier(byte value) => new(value);
/// <inheritdoc /> /// <inheritdoc />
public override int GetHashCode() => Value.GetHashCode(); public override int GetHashCode() => Value.GetHashCode();
} }

View File

@ -29,7 +29,7 @@ public class DamageCalculatorTests
// with an effective Attack stat of 123 // with an effective Attack stat of 123
attacker.Setup(x => x.BoostedStats).Returns(new StatisticSet<uint>(1, 123, 1, 1, 1, 1)); attacker.Setup(x => x.BoostedStats).Returns(new StatisticSet<uint>(1, 123, 1, 1, 1, 1));
// We use 10 as the Ice type // We use 10 as the Ice type
attacker.Setup(x => x.Types).Returns([new TypeIdentifier(10)]); attacker.Setup(x => x.Types).Returns([new TypeIdentifier(10, "ice")]);
var defender = new Mock<IPokemon>(); var defender = new Mock<IPokemon>();
// a Garchomp with an effective Defense stat of 163 // a Garchomp with an effective Defense stat of 163
@ -49,7 +49,7 @@ public class DamageCalculatorTests
var hit = new Mock<IHitData>(); var hit = new Mock<IHitData>();
// Ice Fang (an Ice-type physical move with a power of 65) // Ice Fang (an Ice-type physical move with a power of 65)
hit.Setup(x => x.BasePower).Returns(65); hit.Setup(x => x.BasePower).Returns(65);
hit.Setup(x => x.Type).Returns(new TypeIdentifier(10)); hit.Setup(x => x.Type).Returns(new TypeIdentifier(10, "ice"));
// has a double weakness to the move's Ice type // has a double weakness to the move's Ice type
hit.Setup(x => x.Effectiveness).Returns(4.0f); hit.Setup(x => x.Effectiveness).Returns(4.0f);

View File

@ -10,7 +10,7 @@ namespace PkmnLib.Plugin.Gen7.Libraries;
public class Gen7MiscLibrary : IMiscLibrary public class Gen7MiscLibrary : IMiscLibrary
{ {
private readonly IMoveData _struggleData = new MoveDataImpl("struggle", new TypeIdentifier(0), private readonly IMoveData _struggleData = new MoveDataImpl("struggle", new TypeIdentifier(0, "none"),
MoveCategory.Physical, 50, 255, 255, MoveTarget.Any, 0, MoveCategory.Physical, 50, 255, 255, MoveTarget.Any, 0,
new SecondaryEffectImpl(-1, "struggle", new Dictionary<StringKey, object?>()), []); new SecondaryEffectImpl(-1, "struggle", new Dictionary<StringKey, object?>()), []);

View File

@ -1,6 +1,6 @@
namespace PkmnLib.Plugin.Gen7.Scripts.Pokemon; namespace PkmnLib.Plugin.Gen7.Scripts.Battle;
[Script(ScriptCategory.Pokemon, "future_sight")] [Script(ScriptCategory.Battle, "future_sight")]
public class FutureSightEffect : Script public class FutureSightEffect : Script
{ {
private int _turnsLeft = 3; private int _turnsLeft = 3;

View File

@ -1,4 +1,5 @@
using System; using System;
using PkmnLib.Plugin.Gen7.Scripts.Battle;
using PkmnLib.Plugin.Gen7.Scripts.Pokemon; using PkmnLib.Plugin.Gen7.Scripts.Pokemon;
using PkmnLib.Static.Utils; using PkmnLib.Static.Utils;

View File

@ -3,6 +3,7 @@ using PkmnLib.Static;
namespace PkmnLib.Plugin.Gen7.Scripts.Moves; namespace PkmnLib.Plugin.Gen7.Scripts.Moves;
[Script(ScriptCategory.Move, "geomancy")]
public class Geomancy : BaseChargeMove<RequireChargeEffect> public class Geomancy : BaseChargeMove<RequireChargeEffect>
{ {
/// <inheritdoc /> /// <inheritdoc />

View File

@ -26,7 +26,7 @@ public class Hail : Script
{ {
if (!battle.Library.StaticLibrary.Types.TryGetTypeIdentifier("ice", out var iceType)) if (!battle.Library.StaticLibrary.Types.TryGetTypeIdentifier("ice", out var iceType))
{ {
iceType = new TypeIdentifier(255); iceType = new TypeIdentifier(255, "non_existent");
} }
foreach (var side in battle.Sides) foreach (var side in battle.Sides)
{ {