/// <summary> /// Imports a <see cref="set"/> to create a new <see cref="PKM"/> with a context of <see cref="tr"/>. /// </summary> /// <param name="tr">Source/Destination trainer</param> /// <param name="set">Set data to import</param> /// <param name="msg">Result code indicating success or failure</param> /// <returns>Legalized PKM (hopefully legal)</returns> public static PKM GetLegalFromSet(this ITrainerInfo tr, IBattleTemplate set, out LegalizationResult msg) { var template = PKMConverter.GetBlank(tr.Generation, (GameVersion)tr.Game); template.ApplySetDetails(set); return(tr.GetLegalFromSet(set, template, out msg)); }
/// <summary> /// Imports a <see cref="set"/> to create a new <see cref="PKM"/> with a context of <see cref="tr"/>. /// </summary> /// <param name="tr">Source/Destination trainer</param> /// <param name="set">Set data to import</param> /// <param name="msg">Result code indicating success or failure</param> /// <param name="allowAPI">Use <see cref="Core"/> to find and generate a new pkm</param> /// <returns>Legalized PKM (hopefully legal)</returns> public static PKM GetLegalFromSet(this ITrainerInfo tr, ShowdownSet set, out LegalizationResult msg, bool allowAPI = true) { var template = PKMConverter.GetBlank(tr.Generation, (GameVersion)tr.Game); template.ApplySetDetails(set); return(tr.GetLegalFromSet(set, template, out msg, allowAPI)); }
/// <summary> /// Imports a <see cref="set"/> to create a new <see cref="PKM"/> with a context of <see cref="tr"/>. /// </summary> /// <param name="tr">Source/Destination trainer</param> /// <param name="set">Set data to import</param> /// <param name="msg">Result code indicating success or failure</param> /// <returns>Legalized PKM (hopefully legal)</returns> public static PKM GetLegalFromSet(this ITrainerInfo tr, IBattleTemplate set, out LegalizationResult msg) { var template = EntityBlank.GetBlank(tr); if (template.Version == 0) { template.Version = tr.Game; } template.ApplySetDetails(set); return(tr.GetLegalFromSet(set, template, out msg)); }
/// <summary> /// Regenerates the set by searching for an encounter that can generate the template. /// </summary> /// <param name="tr">Trainer Data that was passed in</param> /// <param name="set">Showdown set being used</param> /// <param name="template">template PKM to legalize</param> /// <param name="msg">Legalization result</param> /// <returns>Legalized pkm</returns> private static PKM GetLegalFromSet(this ITrainerInfo tr, IBattleTemplate set, PKM template, out LegalizationResult msg) { if (set is ShowdownSet s) { set = new RegenTemplate(s, tr.Generation); } msg = tr.TryAPIConvert(set, template, out PKM pk); if (msg == LegalizationResult.Regenerated) { return(pk); } if (EnableEasterEggs) { return(tr.GetEasterEggFromSet(set, template)); } return(template); }
/// <summary> /// Regenerates the set by searching for an encounter that can generate the template. /// </summary> /// <param name="tr">Trainer Data that was passed in</param> /// <param name="set">Showdown set being used</param> /// <param name="template">template PKM to legalize</param> /// <param name="msg">Legalization result</param> /// <returns>Legalized pkm</returns> private static PKM GetLegalFromSet(this ITrainerInfo tr, IBattleTemplate set, PKM template, out LegalizationResult msg) { if (set is ShowdownSet s) { set = new RegenTemplate(s); } bool success = tr.TryAPIConvert(set, template, out PKM pk); if (success) { msg = LegalizationResult.Regenerated; return(pk); } msg = LegalizationResult.Failed; if (EnableEasterEggs) { var gen = EasterEggs.GetGeneration(template.Species); var species = (int)EasterEggs.GetMemeSpecies(gen); template.Species = species; var legalencs = tr.GetRandomEncounter(species, out var legal); if (legalencs && legal != null) { template = legal; } template.SetNickname(EasterEggs.GetMemeNickname(gen)); } return(template); }
/// <summary> /// Main function that auto legalizes based on the legality /// </summary> /// <remarks>Leverages <see cref="Core"/>'s <see cref="EncounterMovesetGenerator"/> to create a <see cref="PKM"/> from a <see cref="IBattleTemplate"/>.</remarks> /// <param name="dest">Destination for the generated pkm</param> /// <param name="template">rough pkm that has all the <see cref="set"/> values entered</param> /// <param name="set">Showdown set object</param> /// <param name="satisfied">If the final result is legal or not</param> public static PKM GetLegalFromTemplate(this ITrainerInfo dest, PKM template, IBattleTemplate set, out LegalizationResult satisfied) { RegenSet regen; if (set is RegenTemplate t) { t.FixGender(template.PersonalInfo); regen = t.Regen; } else { regen = RegenSet.Default; } template.ApplySetDetails(set); template.SetRecordFlags(); // Validate TR moves for the encounter var isHidden = template.AbilityNumber == 4; if (template.PersonalInfo.Abilities.Count > 2) // Hidden ability exists for the template { isHidden = isHidden || template.PersonalInfo.Abilities[2] == template.Ability; } var destType = template.GetType(); var destVer = (GameVersion)dest.Game; if (destVer <= 0 && dest is SaveFile s) { destVer = s.Version; } var timer = Stopwatch.StartNew(); var gamelist = FilteredGameList(template, destVer); var encounters = EncounterMovesetGenerator.GenerateEncounters(pk: template, moves: set.Moves, gamelist); var criteria = EncounterCriteria.GetCriteria(set); foreach (var enc in encounters) { // Return out if set times out if (timer.Elapsed.TotalSeconds >= Timeout) { timer.Stop(); satisfied = LegalizationResult.Timeout; return(template); } // Look before we leap -- don't waste time generating invalid / incompatible junk. if (!IsEncounterValid(set, enc, isHidden, destVer)) { continue; } // Create the PKM from the template. var tr = GetTrainer(regen, enc.Version, enc.Generation); var raw = enc.ConvertToPKM(tr, criteria); raw = raw.SanityCheckLocation(enc); if (raw.IsEgg) // PGF events are sometimes eggs. Force hatch them before proceeding { raw.HandleEggEncounters(enc, tr); } raw.PreSetPIDIV(enc, set); // Transfer any VC1 via VC2, as there may be GSC exclusive moves requested. if (dest.Generation >= 7 && raw is PK1 basepk1) { raw = basepk1.ConvertToPK2(); } // Bring to the target generation, then apply final details. var pk = PKMConverter.ConvertToType(raw, destType, out _); if (pk == null) { continue; } ApplySetDetails(pk, set, raw, dest, enc, regen); // Apply final tweaks to the data. if (pk is IGigantamax gmax && gmax.CanGigantamax != set.CanGigantamax) { if (!gmax.CanToggleGigantamax(pk.Species, pk.Form, enc.Species, enc.Form)) { continue; } gmax.CanGigantamax = set.CanGigantamax; // soup hax } // Try applying batch editor values. if (AllowBatchCommands && regen.HasBatchSettings) { pk.RefreshChecksum(); var b = regen.Batch; if (!BatchEditing.TryModify(pk, b.Filters, b.Instructions)) { continue; } } if (pk is PK1 pk1 && ParseSettings.AllowGen1Tradeback) { pk1.Catch_Rate = pk1.Gen2Item; // Simulate a gen 2 trade/tradeback to allow tradeback moves } // Verify the Legality of what we generated, and exit if it is valid. var la = new LegalityAnalysis(pk); if (la.Valid) { satisfied = LegalizationResult.Regenerated; return(pk); } Debug.WriteLine($"{la.Report()}\n"); } satisfied = LegalizationResult.Failed; return(template); }
private static PKM GetLegalFromSet(this ITrainerInfo tr, ShowdownSet set, PKM template, out LegalizationResult msg, bool allowAPI = true) { if (allowAPI && tr.TryAPIConvert(set, template, out PKM pk)) { msg = LegalizationResult.Regenerated; return(pk); } msg = LegalizationResult.BruteForce; return(tr.GetBruteForcedLegalMon(set, template)); }
private static PKM GetLegalFromSet(this ITrainerInfo tr, ShowdownSet set, PKM template, out LegalizationResult msg) { if (AllowAPI) { bool success = tr.TryAPIConvert(set, template, out PKM pk); if (success) { msg = LegalizationResult.Regenerated; return(pk); } if (!AllowBruteForce) { msg = LegalizationResult.Failed; return(pk); } } if (AllowBruteForce) { msg = LegalizationResult.BruteForce; return(tr.GetBruteForcedLegalMon(set, template)); } msg = LegalizationResult.Failed; return(template); }