diff --git a/src/PKForge.Engine/SoulGold/SoulGoldData.cs b/src/PKForge.Engine/SoulGold/SoulGoldData.cs index 16af4c9c..7b761dcd 100644 --- a/src/PKForge.Engine/SoulGold/SoulGoldData.cs +++ b/src/PKForge.Engine/SoulGold/SoulGoldData.cs @@ -201,15 +201,20 @@ public int[] ComputeStats(int species, int level, int nature, IReadOnlyList return stats; } - /// Nature multiplier for stat 1..5 (Atk/Def/SpA/SpD/Spe): 110 on the - /// nature's raised stat (nature/5), 90 on its lowered one (nature%5). + /// Nature multiplier for app stat 1..5 (Atk/Def/SpA/SpD/Spe): 110 on the + /// nature's raised stat (nature/5), 90 on its lowered one (nature%5). Natures number + /// their stats Atk, Def, Spe, SpA, SpD, so app index 3 (SpA) is nature stat 3, Spe is 2. + /// (Until the next revision the app index was used as is, so a Speed nature raised + /// Sp. Atk; stats recomputed after an edit then differed from the game's.) private static int NatureStatMultiplier(int nature, int stat) { + ReadOnlySpan natureIndex = [-1, 0, 1, 3, 4, 2]; var raised = nature / 5; var lowered = nature % 5; - if (raised == lowered || (raised != stat - 1 && lowered != stat - 1)) + var index = natureIndex[stat]; + if (raised == lowered || (raised != index && lowered != index)) return 100; - return raised == stat - 1 ? 110 : 90; + return raised == index ? 110 : 90; } public static int GenderOf(uint pid, int ratio) => ratio switch diff --git a/src/PKForge.Engine/SoulGold/SoulGoldMon.cs b/src/PKForge.Engine/SoulGold/SoulGoldMon.cs index 2d2ec270..7aa90d21 100644 --- a/src/PKForge.Engine/SoulGold/SoulGoldMon.cs +++ b/src/PKForge.Engine/SoulGold/SoulGoldMon.cs @@ -79,12 +79,24 @@ private void WriteName(int at, int length, string value) StringConverter3.SetString(span, value, length - 1, jp: false); } - /// language:3 + hiddenNatureModifier:5; the effective nature is - /// the modifier (the PID no longer decides it). + /// language:3 + hiddenNatureModifier:5. As in pokeemerald-expansion, the + /// effective nature is the PID's nature XOR the modifier: 0 keeps the PID's nature, and a + /// mint stores the difference, so the PID (shininess, identity) never changes. A real + /// Soul Gold save shows it: every modifier 0, and the game's stored stats follow PID % 25. + /// (Until the next revision the modifier was read as the nature itself, so every Pokémon + /// without a mint showed as Hardy and stats recomputed after an edit lost its nature.) public int Nature { - get => _buffer[Offset + 0x14] >> 3; - set => _buffer[Offset + 0x14] = (byte)((_buffer[Offset + 0x14] & 0x07) | ((value % 25) << 3)); + get + { + var nature = (int)(Pid % 25) ^ (_buffer[Offset + 0x14] >> 3); + return nature < 25 ? nature : (int)(Pid % 25); + } + set + { + var modifier = (value % 25) ^ (int)(Pid % 25); + _buffer[Offset + 0x14] = (byte)((_buffer[Offset + 0x14] & 0x07) | (modifier << 3)); + } } public int Language @@ -313,8 +325,10 @@ public int[] Evs { for (var i = 0; i < 6; i++) raw[i] = _buffer[Secure + 0x18 + i]; } - // Both stores keep the GBA order HP, Atk, Def, Spe, SpA, SpD. - return [raw[0], raw[1], raw[2], raw[5], raw[3], raw[4]]; + // Both stores keep the GBA order HP, Atk, Def, Spe, SpA, SpD; app order takes SpA, + // SpD, then Spe. (Until the next revision this used the setter's permutation, which + // showed Spe's EVs as SpD and so on; stats recomputed after an edit were wrong.) + return [raw[0], raw[1], raw[2], raw[4], raw[5], raw[3]]; } set { diff --git a/tests/PKForge.Engine.Tests/SoulGoldStatTests.cs b/tests/PKForge.Engine.Tests/SoulGoldStatTests.cs new file mode 100644 index 00000000..0cbca2c5 --- /dev/null +++ b/tests/PKForge.Engine.Tests/SoulGoldStatTests.cs @@ -0,0 +1,94 @@ +using PKForge.Domain; +using PKForge.Engine.SoulGold; +using Xunit; +using static PKForge.Engine.SoulGold.SoulGoldFormat; + +namespace PKForge.Engine.Tests; + +/// +/// Soul Gold natures, EVs and stats (upstream issue #80: "every time I hit save it adds more +/// EVs"). Three errors made a party edit write the wrong stats: the hidden-nature modifier was +/// read as the nature itself (every Pokémon without a mint showed as Hardy), natures raised +/// Sp. Atk where they meant Speed, and EVs were read with the write order (Spe's showed as +/// SpD). The first three tests need no save; the last runs on any real Soul Gold save in +/// .local-testdata/romhacks (gitignored) and skips without one. +/// +public sealed class SoulGoldStatTests +{ + private static SoulGoldMon Mon(StorageEra era = StorageEra.LegacyV19) => + new(new byte[PartyMonSize], 0, true, era, SoulGoldData.Instance); + + [Fact] + public void The_nature_is_the_pids_unless_a_mint_changed_it() + { + var mon = Mon(); + mon.Pid = 0x1234_5678 - 0x1234_5678 % 25 + 17; // PID nature 17 (Quiet) + mon.Language = 2; + Assert.Equal(17, mon.Nature); // modifier 0: the PID's nature + + var pid = mon.Pid; + mon.Nature = 10; // a Timid mint + Assert.Equal(10, mon.Nature); + Assert.Equal(pid, mon.Pid); // shininess and identity untouched + Assert.Equal(2, mon.Language); + Assert.Equal((10 ^ 17) << 3 | 2, mon.Buffer[0x14]); + + mon.Nature = 17; // back to the PID's own nature + Assert.Equal(2, mon.Buffer[0x14]); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void Evs_read_back_in_the_order_they_were_written(bool bx21) + { + var era = bx21 ? StorageEra.Bx21 : StorageEra.LegacyV19; + var mon = Mon(era); + int[] app = [1, 2, 3, 4, 5, 6]; // HP, Atk, Def, SpA, SpD, Spe + mon.Evs = app; + Assert.Equal(app, mon.Evs); + if (era == StorageEra.LegacyV19) + // The game's order: HP, Atk, Def, Spe, SpA, SpD. + Assert.Equal(new byte[] { 1, 2, 3, 6, 4, 5 }, mon.Buffer.AsSpan(0x20 + 0x18, 6).ToArray()); + } + + [Fact] + public void A_speed_nature_raises_speed_and_lowers_its_own_stat() + { + var data = SoulGoldData.Instance; + var species = data.SpeciesFromNational(1); // Bulbasaur + int[] ivs = [31, 31, 31, 31, 31, 31], evs = [0, 0, 0, 0, 0, 0]; + var neutral = data.ComputeStats(species, 50, 0, ivs, evs); + var timid = data.ComputeStats(species, 50, 10, ivs, evs); // + Spe, - Atk + var modest = data.ComputeStats(species, 50, 15, ivs, evs); // + SpA, - Atk + Assert.Equal(neutral[5] * 110 / 100, timid[5]); + Assert.Equal(neutral[1] * 90 / 100, timid[1]); + Assert.Equal(neutral[3], timid[3]); + Assert.Equal(neutral[3] * 110 / 100, modest[3]); + Assert.Equal(neutral[5], modest[5]); + } + + [Fact] + public void Stats_recomputed_after_an_edit_are_the_games_own() + { + var directory = new DirectoryInfo(AppContext.BaseDirectory); + while (directory is not null && !File.Exists(Path.Combine(directory.FullName, "PKForge.sln"))) + directory = directory.Parent; + var folder = directory is null ? null : Path.Combine(directory.FullName, ".local-testdata", "romhacks"); + var saves = folder is not null && Directory.Exists(folder) ? Directory.GetFiles(folder, "soulgold*.sav") : []; + foreach (var path in saves) + { + var sample = File.ReadAllBytes(path); + if (!LooksLike(sample)) continue; + using var session = new SoulGoldEngineSession(sample); + for (var slot = 0; slot < 6; slot++) + { + var before = session.ReadEntity(-1, slot); + if (before.IsEmpty) continue; + // A change that leaves stats alone still makes the engine recompute them. + session.ApplyEdit(-1, slot, new EntityEdit(Friendship: before.Friendship == 255 ? 254 : before.Friendship + 1)); + Assert.Equal(before.Stats, session.ReadEntity(-1, slot).Stats); + } + } + } +}