Skip to content

Commit

Permalink
Show correct HatchCounter value for BDSP traded eggs
Browse files Browse the repository at this point in the history
Yay abstractions to hide away the quirks.

BDSP traded eggs set HT_Friendship and the CurrentHandler flag, but the HatchCounter is always the OT_Friendship value.

Trades always receive BaseFriendship, instead of a hardcoded 50. Necessary to not hardcode because it's immutable for eggs.

Clicking the hatch counter label now sets it to the legal minimum hatch counter (best), and control clicking sets it to the max (worst). Check the encounter template for the true maximum.
  • Loading branch information
kwsch committed Nov 22, 2021
1 parent 1ca1b83 commit cd5c220
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 18 deletions.
31 changes: 29 additions & 2 deletions PKHeX.Core/Legality/Verifiers/Egg/EggStateLegality.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,40 @@
{
public static class EggStateLegality
{
public static int GetMinimumEggHatchCycles(PKM pkm) => pkm switch
public static bool GetIsEggHatchCyclesValid(PKM pk, IEncounterTemplate enc)
{
var hatchCounter = pk.OT_Friendship;
var max = GetMaximumEggHatchCycles(pk, enc);
if (hatchCounter > max)
return false;
var min = GetMinimumEggHatchCycles(pk);
if (hatchCounter < min)
return false;

return true;
}

public static int GetMinimumEggHatchCycles(PKM pk) => pk switch
{
PK7 => 0, // pelago can decrement to 0
_ => 1, // whenever it hits 0, it hatches, so anything above that is fine.
};

public static bool IsValidHTEgg(PKM pkm) => pkm switch
public static int GetMaximumEggHatchCycles(PKM pk)
{
var la = new LegalityAnalysis(pk);
var enc = la.EncounterMatch;
return GetMaximumEggHatchCycles(pk, enc);
}

public static int GetMaximumEggHatchCycles(PKM pk, IEncounterTemplate enc)
{
if (enc is EncounterStatic { EggCycles: not 0 } s)
return s.EggCycles;
return pk.PersonalInfo.HatchCycles;
}

public static bool IsValidHTEgg(PKM pk) => pk switch
{
PB8 { Met_Location: Locations.LinkTrade6NPC } pb8 when pb8.HT_Friendship == PersonalTable.BDSP[pb8.Species].BaseFriendship => true,
_ => false,
Expand Down
5 changes: 1 addition & 4 deletions PKHeX.Core/Legality/Verifiers/MiscVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,7 @@ private static void VerifyMiscEggCommon(LegalityAnalysis data)
data.AddLine(GetInvalid(LEggPP, Egg));

var enc = data.EncounterMatch;
var HatchCycles = enc is EncounterStatic s ? s.EggCycles : 0;
if (HatchCycles == 0) // no value set
HatchCycles = pkm.PersonalInfo.HatchCycles;
if (pkm.OT_Friendship > HatchCycles || pkm.OT_Friendship < EggStateLegality.GetMinimumEggHatchCycles(pkm))
if (!EggStateLegality.GetIsEggHatchCyclesValid(pkm, enc))
data.AddLine(GetInvalid(LEggHatchCycles, Egg));

if (pkm.Format >= 6 && enc is EncounterEgg && !MovesMatchRelearn(pkm))
Expand Down
2 changes: 1 addition & 1 deletion PKHeX.Core/PKM/PB8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void TradeHT(ITrainerInfo tr)
{
if (HT_Name != tr.OT)
{
HT_Friendship = 50;
HT_Friendship = PersonalInfo.BaseFriendship;
HT_Name = tr.OT;
}
CurrentHandler = 1;
Expand Down
21 changes: 19 additions & 2 deletions PKHeX.WinForms/Controls/PKM Editor/LoadSave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,19 @@ private void LoadMisc2(PKM pk)
if (pk is IFormArgument f)
FA_Form.LoadArgument(f, pk.Species, pk.Form, pk.Format);

TB_Friendship.Text = pk.CurrentFriendship.ToString();
ReloadToFriendshipTextBox(pk);

Label_HatchCounter.Visible = CHK_IsEgg.Checked;
Label_Friendship.Visible = !CHK_IsEgg.Checked;
}

private void ReloadToFriendshipTextBox(PKM pk)
{
// Show OT friendship always if it is an egg.
var fs = (pk.IsEgg ? pk.OT_Friendship : pk.CurrentFriendship);
TB_Friendship.Text = fs.ToString();
}

private void SaveMisc2(PKM pk)
{
SavePKRS(pk);
Expand All @@ -191,7 +198,17 @@ private void SaveMisc2(PKM pk)
pk.Form = CB_Form.Enabled ? CB_Form.SelectedIndex & 0x1F : 0;
if (Entity is IFormArgument f)
FA_Form.SaveArgument(f);
pk.CurrentFriendship = Util.ToInt32(TB_Friendship.Text);

var friendship = Util.ToInt32(TB_Friendship.Text);
UpdateFromFriendshipTextBox(pk, friendship);
}

private static void UpdateFromFriendshipTextBox(PKM pk, int friendship)
{
if (pk.IsEgg)
pk.OT_Friendship = friendship;
else
pk.CurrentFriendship = friendship;
}

private void LoadMisc3(PKM pk)
Expand Down
1 change: 1 addition & 0 deletions PKHeX.WinForms/Controls/PKM Editor/PKMEditor.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions PKHeX.WinForms/Controls/PKM Editor/PKMEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,10 @@ private static void SetCountrySubRegion(ComboBox cb, string type)
// Prompted Updates of PKM //
private void ClickFriendship(object sender, EventArgs e)
{
if (ModifierKeys == Keys.Control) // clear
TB_Friendship.Text = "0";
if (ModifierKeys == Keys.Control ^ CHK_IsEgg.Checked) // clear
TB_Friendship.Text = CHK_IsEgg.Checked ? EggStateLegality.GetMinimumEggHatchCycles(Entity).ToString() : "0";
else
TB_Friendship.Text = TB_Friendship.Text == "255" ? Entity.PersonalInfo.BaseFriendship.ToString() : "255";
TB_Friendship.Text = CHK_IsEgg.Checked ? EggStateLegality.GetMaximumEggHatchCycles(Entity).ToString() : TB_Friendship.Text == "255" ? Entity.PersonalInfo.BaseFriendship.ToString() : "255";
}

private void ClickLevel(object sender, EventArgs e)
Expand Down Expand Up @@ -706,7 +706,7 @@ private void ClickGT(object? sender, EventArgs e)
Entity.CurrentHandler = 1;
UpadteHandlingTrainerBackground(Entity.CurrentHandler);

TB_Friendship.Text = Entity.CurrentFriendship.ToString();
ReloadToFriendshipTextBox(Entity);
}

private void ClickNature(object sender, EventArgs e)
Expand Down Expand Up @@ -972,7 +972,7 @@ private void Update255_MTB(object sender, EventArgs e)
tb.Text = "255";
if (sender == TB_Friendship && int.TryParse(TB_Friendship.Text, out var val))
{
Entity.CurrentFriendship = val;
UpdateFromFriendshipTextBox(Entity, val);
UpdateStats();
}
}
Expand Down Expand Up @@ -1383,7 +1383,7 @@ private void UpdateNotOT(object sender, EventArgs e)
{
ClickGT(GB_OT, EventArgs.Empty); // Switch CT over to OT.
Label_CTGender.Text = string.Empty;
TB_Friendship.Text = Entity.CurrentFriendship.ToString();
ReloadToFriendshipTextBox(Entity);
}
else if (string.IsNullOrWhiteSpace(Label_CTGender.Text))
{
Expand Down Expand Up @@ -1766,10 +1766,10 @@ private void OpenHistory(object sender, EventArgs e)
Entity.HT_Name = TB_OTt2.Text;
Entity.OT_Name = TB_OT.Text;
Entity.IsEgg = CHK_IsEgg.Checked;
Entity.CurrentFriendship = Util.ToInt32(TB_Friendship.Text);
UpdateFromFriendshipTextBox(Entity, Util.ToInt32(TB_Friendship.Text));
using var form = new MemoryAmie(Entity);
form.ShowDialog();
TB_Friendship.Text = Entity.CurrentFriendship.ToString();
ReloadToFriendshipTextBox(Entity);
}

private void B_Records_Click(object sender, EventArgs e)
Expand Down
2 changes: 1 addition & 1 deletion PKHeX.WinForms/Subforms/PKM Editors/MemoryAmie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private void LoadFields()
GB_M_OT.Enabled = GB_M_CT.Enabled = GB_Residence.Enabled =
BTN_Save.Enabled = M_Fullness.Enabled = M_Enjoyment.Enabled =
L_Sociability.Enabled = MT_Sociability.Enabled =
L_Fullness.Enabled = L_Enjoyment.Enabled = !pkm.IsEgg;
L_Fullness.Enabled = L_Enjoyment.Enabled = !(pkm.IsEgg && pkm.IsUntraded && pkm.HT_Friendship == 0);

if (!pkm.IsEgg)
{
Expand Down

0 comments on commit cd5c220

Please sign in to comment.