PKHeX/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_Pokepuff.cs
Kurt 9166d0eb64
Refactoring: Move Source (Legality) (#3560)
Rewrites a good amount of legality APIs pertaining to:
* Legal moves that can be learned
* Evolution chains & cross-generation paths
* Memory validation with forgotten moves

In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data.

The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space.

The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation.

* `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game.
* `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`).
* Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 16:15:27 -07:00

123 lines
3.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using PKHeX.Core;
namespace PKHeX.WinForms;
public partial class SAV_Pokepuff : Form
{
private readonly ISaveBlock6Main SAV;
public SAV_Pokepuff(SaveFile sav)
{
InitializeComponent();
WinFormsUtil.TranslateInterface(this, Main.CurrentLanguage);
SAV = (ISaveBlock6Main)sav;
var puffs = SAV.Puff.GetPuffs();
Setup(puffs.Length);
LoadPuffs(puffs);
toolTip1.SetToolTip(B_Sort, "Hold CTRL to reverse sort.");
toolTip2.SetToolTip(B_All, "Hold CTRL to best instead of varied.");
}
private readonly string[] pfa = GameInfo.Strings.puffs;
private void Setup(int rowCount)
{
dgv.Rows.Clear();
dgv.Columns.Clear();
DataGridViewColumn dgvIndex = new DataGridViewTextBoxColumn();
{
dgvIndex.HeaderText = "Slot";
dgvIndex.DisplayIndex = 0;
dgvIndex.Width = 45;
dgvIndex.ReadOnly = true;
dgvIndex.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
DataGridViewComboBoxColumn dgvPuff = new()
{
DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing,
};
{
foreach (string t in pfa)
dgvPuff.Items.Add(t);
dgvPuff.DisplayIndex = 1;
dgvPuff.Width = 135;
dgvPuff.FlatStyle = FlatStyle.Flat;
}
dgv.Columns.Add(dgvIndex);
dgv.Columns.Add(dgvPuff);
dgv.Rows.Add(rowCount);
}
private void LoadPuffs(ReadOnlySpan<byte> Puffs)
{
for (int i = 0; i < Puffs.Length; i++)
{
dgv.Rows[i].Cells[0].Value = (i + 1).ToString();
int puffval = Puffs[i];
if (puffval >= pfa.Length)
{
WinFormsUtil.Error($"Invalid Puff Index: {i}", $"Expected < ${pfa.Length}");
puffval = 0;
}
dgv.Rows[i].Cells[1].Value = pfa[puffval];
}
}
private void DropClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex != 1)
return;
((ComboBox)((DataGridView) sender).EditingControl).DroppedDown = true;
}
private void B_Cancel_Click(object sender, EventArgs e)
{
Close();
}
private void B_All_Click(object sender, EventArgs e)
{
SAV.Puff.MaxCheat(ModifierKeys == Keys.Control);
LoadPuffs(SAV.Puff.GetPuffs());
}
private void B_None_Click(object sender, EventArgs e)
{
SAV.Puff.Reset();
LoadPuffs(SAV.Puff.GetPuffs());
}
private void B_Sort_Click(object sender, EventArgs e)
{
SAV.Puff.Sort(ModifierKeys == Keys.Control);
LoadPuffs(SAV.Puff.GetPuffs());
}
private byte[] GetPuffs()
{
var puffs = new List<byte>();
for (int i = 0; i < dgv.Rows.Count; i++)
{
var puff = dgv.Rows[i].Cells[1].Value?.ToString();
int index = (byte)Array.IndexOf(pfa, puff);
puffs.Add((byte)index);
}
return puffs.ToArray();
}
private void B_Save_Click(object sender, EventArgs e)
{
var puffs = GetPuffs();
SAV.Puff.SetPuffs(puffs);
SAV.Puff.PuffCount = puffs.Length;
Close();
}
}