From 9abddb17e28bda5a47ea6eb59f99d136881109ff Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 16 Dec 2018 20:45:16 -0800 Subject: [PATCH] Add force fully evolved implementation use current evo data instead of hardcoded max-evolved table for each game --- .../Randomizers/TrainerRandomizer.cs | 51 +++++++++++++++---- pkNX.Structures/Evolution/EvolutionMethod.cs | 2 + pkNX.WinForms/Subforms/BTTE.cs | 3 +- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/pkNX.Randomization/Randomizers/TrainerRandomizer.cs b/pkNX.Randomization/Randomizers/TrainerRandomizer.cs index 5ec488d3..8427a61e 100644 --- a/pkNX.Randomization/Randomizers/TrainerRandomizer.cs +++ b/pkNX.Randomization/Randomizers/TrainerRandomizer.cs @@ -22,15 +22,16 @@ public class TrainerRandomizer : Randomizer public MoveRandomizer RandMove { get; set; } public int ClassCount { get; set; } public Func GetBlank { get; set; } - public IList FinalEvo { get; set; } = Array.Empty(); + public EvolutionSet[] Evos { get; set; } private TrainerRandSettings Settings; - public TrainerRandomizer(GameInfo info, PersonalTable t, VsTrainer[] trainers) + public TrainerRandomizer(GameInfo info, PersonalTable t, VsTrainer[] trainers, EvolutionSet[] evos) { Trainers = trainers; Info = info; Personal = t; + Evos = evos; PossibleHeldItems = Legal.GetRandomItemList(Info.Game); MegaDictionary = Legal.GetMegaDictionary(Info.Game); @@ -127,14 +128,6 @@ private void DetermineSpecies(IPokeData pk) pk.Gender = 0; // random pk.Nature = Util.Random.Next(25); // random } - - if (Settings.ForceFullyEvolved && pk.Level >= Settings.ForceFullyEvolvedAtLevel && !FinalEvo.Contains(pk.Species)) - { - int randFinalEvo() => Util.Random.Next(FinalEvo.Count); - if (FinalEvo.Count != 0) - pk.Species = FinalEvo[randFinalEvo()]; - pk.Form = Legal.GetRandomForme(pk.Species, Settings.AllowRandomMegaForms, true, Personal); - } } private void RandomizeSpecFormItem(IPokeData pk, int Type) @@ -157,6 +150,7 @@ private void RandomizeSpecFormItem(IPokeData pk, int Type) else // every other pkm { pk.Species = RandSpec.GetRandomSpeciesType(pk.Species, Type); + TryForceEvolve(pk); pk.HeldItem = PossibleHeldItems[Util.Random.Next(PossibleHeldItems.Length)]; pk.Form = Legal.GetRandomForme(pk.Species, Settings.AllowRandomMegaForms, true, Personal); } @@ -177,9 +171,46 @@ private void RandomizeSpecForm(TrainerPoke7b pk, int type) pk.MegaFormChoice = 0; pk.Species = RandSpec.GetRandomSpeciesType(pk.Species, type); + TryForceEvolve(pk); pk.Form = Legal.GetRandomForme(pk.Species, Settings.AllowRandomMegaForms, true, Personal); } + private void TryForceEvolve(IPokeData pk) + { + if (!Settings.ForceFullyEvolved || pk.Level < Settings.ForceFullyEvolvedAtLevel) + return; + + var evos = Evos; + int species = pk.Species; + int form = pk.Form; + + int timesEvolved = TryForceEvolve(evos, ref species, ref form); + if (timesEvolved == 0) + return; + pk.Species = species; + pk.Form = form; + } + + private int TryForceEvolve(IReadOnlyList evos, ref int species, ref int form) + { + int timesEvolved = 0; + do + { + var index = Personal.GetFormeIndex(species, form); + var eSet = evos[index].PossibleEvolutions; + int evoCount = eSet.Count(z => z.HasData); + if (evoCount == 0) + break; + ++timesEvolved; + var next = Util.Random.Next(evoCount); + var nextEvo = eSet[next]; + species = nextEvo.Species; + form = nextEvo.Form >= 0 ? nextEvo.Form : form; + } + while (timesEvolved < 3); // prevent randomized evos from looping excessively + return timesEvolved; + } + private void UpdatePKMFromSettings(TrainerPoke pk) { if (Settings.BoostLevel) diff --git a/pkNX.Structures/Evolution/EvolutionMethod.cs b/pkNX.Structures/Evolution/EvolutionMethod.cs index 1139406c..64fc6863 100644 --- a/pkNX.Structures/Evolution/EvolutionMethod.cs +++ b/pkNX.Structures/Evolution/EvolutionMethod.cs @@ -24,5 +24,7 @@ public EvolutionMethod Copy(int species = -1) Level = Level }; } + + public bool HasData => Species != 0; } } \ No newline at end of file diff --git a/pkNX.WinForms/Subforms/BTTE.cs b/pkNX.WinForms/Subforms/BTTE.cs index 5ecd4fa9..850cfc7e 100644 --- a/pkNX.WinForms/Subforms/BTTE.cs +++ b/pkNX.WinForms/Subforms/BTTE.cs @@ -540,7 +540,8 @@ private void B_Randomize_Click(object sender, EventArgs e) var rspec = new SpeciesRandomizer(Game.Info, Personal); rspec.Initialize((SpeciesSettings)PG_Species.SelectedObject); learn.Moves = moves; - var trand = new TrainerRandomizer(Game.Info, Personal, Trainers.LoadAll()) + var evos = Game.Data.EvolutionData; + var trand = new TrainerRandomizer(Game.Info, Personal, Trainers.LoadAll(), evos.LoadAll()) { ClassCount = CB_Trainer_Class.Items.Count, Learn = learn,