From 2c987e32ee327d3df2bf3e4a05bdabb9e3d0887f Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 7 Mar 2025 12:57:06 +0100 Subject: [PATCH] Bug fixes, add type name to TypeIdentifier --- PkmnLib.Dataloader/TypeDataLoader.cs | 3 +- PkmnLib.Static/Libraries/TypeLibrary.cs | 32 +++++++------------ PkmnLib.Static/TypeIdentifier.cs | 14 ++++---- .../DamageCalculatorTests.cs | 4 +-- .../Libraries/Gen7MiscLibrary.cs | 2 +- .../{Pokemon => Battle}/FutureSightEffect.cs | 4 +-- .../Scripts/Moves/FutureSight.cs | 1 + .../Scripts/Moves/Geomancy.cs | 1 + .../Scripts/Weather/Hail.cs | 2 +- 9 files changed, 29 insertions(+), 34 deletions(-) rename Plugins/PkmnLib.Plugin.Gen7/Scripts/{Pokemon => Battle}/FutureSightEffect.cs (91%) diff --git a/PkmnLib.Dataloader/TypeDataLoader.cs b/PkmnLib.Dataloader/TypeDataLoader.cs index 20bf632..3c6297b 100644 --- a/PkmnLib.Dataloader/TypeDataLoader.cs +++ b/PkmnLib.Dataloader/TypeDataLoader.cs @@ -46,7 +46,8 @@ public static class TypeDataLoader throw new InvalidDataException( $"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); } } diff --git a/PkmnLib.Static/Libraries/TypeLibrary.cs b/PkmnLib.Static/Libraries/TypeLibrary.cs index 61b1e11..c0b0d78 100644 --- a/PkmnLib.Static/Libraries/TypeLibrary.cs +++ b/PkmnLib.Static/Libraries/TypeLibrary.cs @@ -13,11 +13,6 @@ public interface IReadOnlyTypeLibrary /// bool TryGetTypeIdentifier(StringKey key, out TypeIdentifier typeIdentifier); - /// - /// Gets the type name from the type identifier. - /// - bool TryGetTypeName(TypeIdentifier t, [NotNullWhen(true)] out StringKey? stringKey); - /// /// Gets the effectiveness for a single attacking type against a single defending type. /// @@ -35,25 +30,19 @@ public interface IReadOnlyTypeLibrary /// public class TypeLibrary : IReadOnlyTypeLibrary { - private readonly Dictionary _types = new(); + private readonly List _types = new(); private readonly List> _effectiveness = new(); /// - public bool TryGetTypeIdentifier(StringKey key, out TypeIdentifier type) => _types.TryGetValue(key, out type); - - /// - public bool TryGetTypeName(TypeIdentifier t, [NotNullWhen(true)] out StringKey? stringKey) + public bool TryGetTypeIdentifier(StringKey key, out TypeIdentifier type) { - foreach (var (key, value) in _types) + var found = _types.FirstOrDefault(t => t.Name.Equals(key)); + if (found.Value is not 0) { - if (value == t) - { - stringKey = key; - return true; - } + type = found; + return true; } - - stringKey = default; + type = default; return false; } @@ -79,7 +68,8 @@ public class TypeLibrary : IReadOnlyTypeLibrary throw new ArgumentOutOfRangeException(nameof(attacking)); 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 /// public TypeIdentifier RegisterType(StringKey name) { - var id = new TypeIdentifier((byte)(_types.Count + 1)); - _types.Add(name, id); + var id = new TypeIdentifier((byte)(_types.Count + 1), name); + _types.Add(id); _effectiveness.Add(Enumerable.Repeat(1.0f, _effectiveness.Count).ToList()); foreach (var list in _effectiveness) { diff --git a/PkmnLib.Static/TypeIdentifier.cs b/PkmnLib.Static/TypeIdentifier.cs index 413f1f4..6984cab 100644 --- a/PkmnLib.Static/TypeIdentifier.cs +++ b/PkmnLib.Static/TypeIdentifier.cs @@ -1,4 +1,5 @@ using PkmnLib.Static.Libraries; +using PkmnLib.Static.Utils; namespace PkmnLib.Static; @@ -7,22 +8,23 @@ namespace PkmnLib.Static; /// public readonly record struct TypeIdentifier { + /// + /// The name of the type identifier. + /// + public StringKey Name { get; } + /// /// The underlying value of the type identifier. /// public byte Value { get; } /// - public TypeIdentifier(byte value) + public TypeIdentifier(byte value, StringKey name) { Value = value; + Name = name; } - /// - /// Converts a byte to a type identifier. - /// - public static implicit operator TypeIdentifier(byte value) => new(value); - /// public override int GetHashCode() => Value.GetHashCode(); } \ No newline at end of file diff --git a/Plugins/PkmnLib.Plugin.Gen7.Tests/DamageCalculatorTests.cs b/Plugins/PkmnLib.Plugin.Gen7.Tests/DamageCalculatorTests.cs index 7fea691..cd3ae46 100644 --- a/Plugins/PkmnLib.Plugin.Gen7.Tests/DamageCalculatorTests.cs +++ b/Plugins/PkmnLib.Plugin.Gen7.Tests/DamageCalculatorTests.cs @@ -29,7 +29,7 @@ public class DamageCalculatorTests // with an effective Attack stat of 123 attacker.Setup(x => x.BoostedStats).Returns(new StatisticSet(1, 123, 1, 1, 1, 1)); // 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(); // a Garchomp with an effective Defense stat of 163 @@ -49,7 +49,7 @@ public class DamageCalculatorTests var hit = new Mock(); // Ice Fang (an Ice-type physical move with a power of 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 hit.Setup(x => x.Effectiveness).Returns(4.0f); diff --git a/Plugins/PkmnLib.Plugin.Gen7/Libraries/Gen7MiscLibrary.cs b/Plugins/PkmnLib.Plugin.Gen7/Libraries/Gen7MiscLibrary.cs index 668aaca..45e7628 100644 --- a/Plugins/PkmnLib.Plugin.Gen7/Libraries/Gen7MiscLibrary.cs +++ b/Plugins/PkmnLib.Plugin.Gen7/Libraries/Gen7MiscLibrary.cs @@ -10,7 +10,7 @@ namespace PkmnLib.Plugin.Gen7.Libraries; 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, new SecondaryEffectImpl(-1, "struggle", new Dictionary()), []); diff --git a/Plugins/PkmnLib.Plugin.Gen7/Scripts/Pokemon/FutureSightEffect.cs b/Plugins/PkmnLib.Plugin.Gen7/Scripts/Battle/FutureSightEffect.cs similarity index 91% rename from Plugins/PkmnLib.Plugin.Gen7/Scripts/Pokemon/FutureSightEffect.cs rename to Plugins/PkmnLib.Plugin.Gen7/Scripts/Battle/FutureSightEffect.cs index 4655557..14ad2fc 100644 --- a/Plugins/PkmnLib.Plugin.Gen7/Scripts/Pokemon/FutureSightEffect.cs +++ b/Plugins/PkmnLib.Plugin.Gen7/Scripts/Battle/FutureSightEffect.cs @@ -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 { private int _turnsLeft = 3; diff --git a/Plugins/PkmnLib.Plugin.Gen7/Scripts/Moves/FutureSight.cs b/Plugins/PkmnLib.Plugin.Gen7/Scripts/Moves/FutureSight.cs index 90a2325..232ad8a 100644 --- a/Plugins/PkmnLib.Plugin.Gen7/Scripts/Moves/FutureSight.cs +++ b/Plugins/PkmnLib.Plugin.Gen7/Scripts/Moves/FutureSight.cs @@ -1,4 +1,5 @@ using System; +using PkmnLib.Plugin.Gen7.Scripts.Battle; using PkmnLib.Plugin.Gen7.Scripts.Pokemon; using PkmnLib.Static.Utils; diff --git a/Plugins/PkmnLib.Plugin.Gen7/Scripts/Moves/Geomancy.cs b/Plugins/PkmnLib.Plugin.Gen7/Scripts/Moves/Geomancy.cs index 627d3f6..3813fef 100644 --- a/Plugins/PkmnLib.Plugin.Gen7/Scripts/Moves/Geomancy.cs +++ b/Plugins/PkmnLib.Plugin.Gen7/Scripts/Moves/Geomancy.cs @@ -3,6 +3,7 @@ using PkmnLib.Static; namespace PkmnLib.Plugin.Gen7.Scripts.Moves; +[Script(ScriptCategory.Move, "geomancy")] public class Geomancy : BaseChargeMove { /// diff --git a/Plugins/PkmnLib.Plugin.Gen7/Scripts/Weather/Hail.cs b/Plugins/PkmnLib.Plugin.Gen7/Scripts/Weather/Hail.cs index fced40f..83b383e 100644 --- a/Plugins/PkmnLib.Plugin.Gen7/Scripts/Weather/Hail.cs +++ b/Plugins/PkmnLib.Plugin.Gen7/Scripts/Weather/Hail.cs @@ -26,7 +26,7 @@ public class Hail : Script { 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) {