Skip to content

Commit

Permalink
fix unown forms in gen 3/4
Browse files Browse the repository at this point in the history
handle signature changes from PKHeX API
  • Loading branch information
santacrab2 committed Oct 1, 2024
1 parent ad10f49 commit d895e50
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
71 changes: 43 additions & 28 deletions PKHeX.Core.AutoMod/AutoMod/APILegality.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static PKM GetLegalFromTemplate(this ITrainerInfo dest, PKM template, IBa
continue;

// Apply final details
ApplySetDetails(pk, set, dest, enc, regen);
ApplySetDetails(pk, set, dest, enc, regen,criteria);

// Apply final tweaks to the data.
if (pk is IGigantamax gmax && gmax.CanGigantamax != set.CanGigantamax)
Expand Down Expand Up @@ -413,6 +413,8 @@ private static GameVersion[] PrioritizeVersion(GameVersion[] gamelist, GameVersi
/// <returns>if the encounter is valid or not</returns>
private static bool IsEncounterValid(IBattleTemplate set, IEncounterable enc, AbilityRequest abilityreq, GameVersion destVer)
{
if (enc is EncounterSlot3 && enc.Species == (ushort)Species.Unown && enc.Form != set.Form)
return false;
// Don't process if encounter min level is higher than requested level
if (!IsRequestedLevelValid(set, enc))
return false;
Expand Down Expand Up @@ -548,8 +550,8 @@ public static bool IsPIDIVSet(PKM pk, IEncounterable enc)

if (enc is IStaticCorrelation8b s && s.GetRequirement(pk) == StaticCorrelation8bRequirement.MustHave)
return true;
if (enc is EncounterSlot4 && pk.Species == (ushort)Species.Unown)
return true;
// if (enc is EncounterSlot4 && pk.Species == (ushort)Species.Unown)
// return true;
if (enc is EncounterSlot3 && pk.Species == (ushort)Species.Unown)
return true;
return enc is EncounterEgg && GameVersion.BDSP.Contains(enc.Version);
Expand Down Expand Up @@ -582,13 +584,13 @@ private static PKM SanityCheckLocation(this PKM pk, IEncounterable enc)
/// <param name="handler">Trainer to handle the Pokémon</param>
/// <param name="enc">Encounter details matched to the Pokémon</param>
/// <param name="regen">Regeneration information</param>
private static void ApplySetDetails(PKM pk, IBattleTemplate set, ITrainerInfo handler, IEncounterable enc, RegenSet regen)
private static void ApplySetDetails(PKM pk, IBattleTemplate set, ITrainerInfo handler, IEncounterable enc, RegenSet regen, EncounterCriteria criteria)
{
byte Form = set.Form;
var language = regen.Extra.Language;
var pidiv = MethodFinder.Analyze(pk);

pk.SetPINGA(set, pidiv.Type, set.HiddenPowerType, enc);
pk.SetPINGA(set, pidiv.Type, set.HiddenPowerType, enc, criteria);
pk.SetSpeciesLevel(set, Form, enc, language);
pk.SetDateLocks(enc);
pk.SetHeldItem(set);
Expand Down Expand Up @@ -819,7 +821,7 @@ private static void HandleEggEncounters(this PKM pk, IEncounterable enc, ITraine
/// <param name="method"></param>
/// <param name="hpType"></param>
/// <param name="enc"></param>
private static void SetPINGA(this PKM pk, IBattleTemplate set, PIDType method, int hpType, IEncounterable enc)
private static void SetPINGA(this PKM pk, IBattleTemplate set, PIDType method, int hpType, IEncounterable enc, EncounterCriteria criteria)
{
var ivprop = enc.GetType().GetProperty("IVs");
if (enc is not EncounterStatic4Pokewalker && enc.Generation > 2)
Expand Down Expand Up @@ -879,7 +881,7 @@ private static void SetPINGA(this PKM pk, IBattleTemplate set, PIDType method, i
pk.SetEncounterTradeIVs();
return; // Fixed PID, no need to mutate
default:
FindPIDIV(pk, method, hpType, set.Shiny, enc, set);
FindPIDIV(pk, method, hpType, set.Shiny, enc, set,criteria);
ValidateGender(pk);
break;
}
Expand Down Expand Up @@ -1018,14 +1020,6 @@ private static void PreSetPIDIV(this PKM pk, IEncounterable enc, IBattleTemplate

FindEggPIDIV8b(pk, shiny, set.Gender, criteria);
}
else if (enc is EncounterSlot4 enc4 && pk.Species == (ushort)Species.Unown)
{
var pi = PersonalTable.HGSS[pk.Species];
if (pk.HGSS)
enc4.SetFromIVsK((PK4)pk, pi, criteria, out var _);
else
enc4.SetFromIVsJ((PK4)pk, pi, criteria, out var _);
}
else if (enc is EncounterSlot3 enc3 && pk.Species == (ushort)Species.Unown)
{
enc3.SetFromIVsUnown((PK3)pk, criteria);
Expand Down Expand Up @@ -1126,14 +1120,14 @@ private static void FindTeraPIDIV<T>(PK9 pk, T enc, IBattleTemplate set, Encount
pk.RefreshAbility(abil >> 1);

var gender_ratio = pi.Gender;
int gender = gender_ratio switch
Gender gender = gender_ratio switch
{
PersonalInfo.RatioMagicGenderless => 2,
PersonalInfo.RatioMagicFemale => 1,
PersonalInfo.RatioMagicMale => 0,
_ => Encounter9RNG.GetGender(gender_ratio, rand.NextInt(100)),
PersonalInfo.RatioMagicGenderless => Gender.Genderless,
PersonalInfo.RatioMagicFemale => Gender.Female,
PersonalInfo.RatioMagicMale => Gender.Male,
_ => (Gender)Encounter9RNG.GetGender(gender_ratio, rand.NextInt(100)),
};
if (criteria.Gender is not null && gender != criteria.Gender)
if (gender != criteria.Gender)
continue;
pk.Gender = (byte)gender;

Expand Down Expand Up @@ -1390,8 +1384,10 @@ private static bool IsMatchCriteria9(PK9 pk, IBattleTemplate template, Encounter
/// <param name="HPType">HPType INT for preserving Hidden powers</param>
/// <param name="shiny">Only used for CHANNEL RNG type</param>
/// <param name="enc"></param>
private static void FindPIDIV(PKM pk, PIDType Method, int HPType, bool shiny, IEncounterable enc, IBattleTemplate set)
private static void FindPIDIV(PKM pk, PIDType Method, int HPType, bool shiny, IEncounterable enc, IBattleTemplate set, EncounterCriteria criteria)
{
if (enc.Generation == 4 && pk.Species == (ushort)Species.Unown)
pk.Form = set.Form;
if (Method == PIDType.None)
{
if (enc is EncounterGift3 wc3)
Expand Down Expand Up @@ -1431,7 +1427,7 @@ private static void FindPIDIV(PKM pk, PIDType Method, int HPType, bool shiny, IE
return;
do
{
if (count >= 2_500_000)
if (count >= 2_500_000 && pk.Species != (ushort)Species.Unown)
compromise = true;

seed = Util.Rand32();
Expand All @@ -1443,6 +1439,7 @@ private static void FindPIDIV(PKM pk, PIDType Method, int HPType, bool shiny, IE
if (PokeWalkerSeedFail(seed, Method, pk, iterPKM))
continue;
PIDGenerator.SetValuesFromSeed(pk, Method, seed);

if (pk.AbilityNumber != iterPKM.AbilityNumber )
continue;
if (!compromise && pk.Nature != iterPKM.Nature)
Expand Down Expand Up @@ -1479,9 +1476,15 @@ private static void FindPIDIV(PKM pk, PIDType Method, int HPType, bool shiny, IE
}
if (pk.Species == (ushort)Species.Unown)
{
if (pk.Form != iterPKM.Form)
continue;
if (enc.Generation == 3 && pk.Form != EntityPID.GetUnownForm3(pk.PID))
if (enc is EncounterSlot4 enc4)
{
var pi = PersonalTable.HGSS[pk.Species];
if (pk.HGSS)
enc4.SetFromIVsK((PK4)pk, pi, criteria, out var _);
else
enc4.SetFromIVsJ((PK4)pk, pi, criteria, out var _);
}
if (enc is EncounterSlot3 && pk.Form != EntityPID.GetUnownForm3(pk.PID))
continue;
}
var pidxor = ((pk.TID16 ^ pk.SID16 ^ (int)(pk.PID & 0xFFFF) ^ (int)(pk.PID >> 16)) & ~0x7) == 8;
Expand Down Expand Up @@ -1785,14 +1788,26 @@ public static EncounterCriteria SetSpecialCriteria(EncounterCriteria criteria, I
IV_SPE = criteria.IV_SPE
};
}
if(enc is EncounterSlot4 && enc.Species == (ushort)Species.Unown)
{
return criteria with
{
IV_ATK = criteria.IV_ATK,
IV_HP = criteria.IV_HP,
IV_DEF = criteria.IV_DEF,
IV_SPA = criteria.IV_SPA,
IV_SPD = criteria.IV_SPD,
IV_SPE = criteria.IV_SPE
};
}
return criteria with
{
IV_ATK = criteria.IV_ATK == 0 ? 0 : -1,
IV_ATK = criteria.IV_ATK == 0 ? (sbyte)0 : (sbyte)-1,
IV_DEF = -1,
IV_HP = -1,
IV_SPA = -1,
IV_SPD = -1,
IV_SPE = criteria.IV_SPE == 0 ? 0 : -1
IV_SPE = criteria.IV_SPE == 0 ? (sbyte)0 : (sbyte)-1
};
}

Expand Down
2 changes: 1 addition & 1 deletion PKHeX.Core.AutoMod/AutoMod/Regeneration/RegenTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public sealed class RegenTemplate : IBattleTemplate
public Nature Nature { get; set; }
public string FormName { get; set; }
public byte Form { get; set; }
public int HiddenPowerType { get; set; }
public sbyte HiddenPowerType { get; set; }
public bool CanGigantamax { get; set; }
public byte DynamaxLevel { get; set; }
public MoveType TeraType { get; set; }
Expand Down

0 comments on commit d895e50

Please sign in to comment.