private void BtOk_Click(object sender, EventArgs e) { if (NudAmount.Value > 0) { DialogResult = DialogResult.OK; Settings = new DummyCreatureCreationSettings { CreatureCount = (int)NudAmount.Value, OnlySelectedSpecies = RbOnlySelectedSpecies.Checked, SpeciesCount = (int)NudSpeciesAmount.Value, Generations = (int)NudBreedForGenerations.Value, PairsPerGeneration = (int)NudUsePairsPerGeneration.Value, ProbabilityHigherStat = (double)NudProbabilityInheritingHigherStat.Value / 100, RandomMutationChance = (double)NudMutationChance.Value / 100, MaxWildLevel = (int)nudMaxWildLevel.Value }; } Close(); }
/// <summary> /// Creates a list of random creatures. /// </summary> /// <param name="count">Number of creatures</param> /// <param name="species">If null, species will be selected randomly (domesticable preferred)</param> /// <param name="numberSpecies">If species are randomly selected, this is the number of different species</param> /// <param name="breedGenerations">If > 0, the creatures will be bred according to the breeding planner, the offspring will also be returned.</param> /// <param name="usePairsPerGeneration">If bred, this indicates how many of the top breeding pairs will be used to breed</param> /// <param name="probabilityHigherStat"></param> /// <param name="randomMutationChance"></param> /// <param name="maxWildLevel"></param> /// <returns></returns> public static List <Creature> CreateCreatures(int count, Species species = null, int numberSpecies = 1, int breedGenerations = 0, int usePairsPerGeneration = 2, double probabilityHigherStat = 0.55, double randomMutationChance = 0.025, int maxWildLevel = 150) { if (count < 1) { return(null); } LastSettings = new DummyCreatureCreationSettings { CreatureCount = count, OnlySelectedSpecies = species != null, SpeciesCount = numberSpecies, Generations = breedGenerations, PairsPerGeneration = usePairsPerGeneration, ProbabilityHigherStat = probabilityHigherStat, RandomMutationChance = randomMutationChance, MaxWildLevel = maxWildLevel }; if (_levelInverseCumulativeFunction == null) { InitializeLevelFunction(); } var creatures = new List <Creature>(count); var rand = new Random(); var randomSpecies = species == null; Species[] speciesSelection = null; var speciesCount = 0; if (randomSpecies) { if (numberSpecies < 1) { numberSpecies = 1; } speciesSelection = Values.V.species.Where(s => s.IsDomesticable && !s.name.Contains("Tek") && !s.name.Contains("Alpha") && (s.variants?.Length ?? 0) < 2).ToArray(); speciesCount = speciesSelection.Length; if (speciesCount > numberSpecies) { var speciesLeft = numberSpecies; var speciesIndices = new List <int>(numberSpecies); while (speciesLeft > 0) { var i = rand.Next(speciesCount); if (speciesIndices.Contains(i)) { continue; } speciesIndices.Add(i); speciesLeft--; } speciesSelection = speciesIndices.Select(i => speciesSelection[i]).ToArray(); speciesCount = speciesSelection.Length; } } if (maxWildLevel < 1) { maxWildLevel = CreatureCollection.CurrentCreatureCollection?.maxWildLevel ?? 150; } var difficulty = maxWildLevel / 30d; var levelStep = (int)difficulty; var nameCounter = new Dictionary <string, int>(); for (int i = 0; i < count; i++) { if (randomSpecies) { species = speciesSelection[rand.Next(speciesCount)]; } // rather "tame" higher creatures var creatureLevel = (rand.Next(5) == 0 ? rand.Next(21) + 1 : 21 + rand.Next(10)) * difficulty; var tamingEffectiveness = 0.5 + rand.NextDouble() / 2; // assume at least 50 % te creatureLevel *= 1 + 0.5 * tamingEffectiveness; var levelFactor = creatureLevel / _totalLevels; var levelsWild = new int[Values.STATS_COUNT]; var levelsDom = new int[Values.STATS_COUNT]; var torpidityLevel = 0; for (int si = 0; si < Values.STATS_COUNT; si++) { if (!species.UsesStat(si) || si == (int)StatNames.Torpidity) { continue; } var level = (int)(levelFactor * GetBinomialLevel(rand)); torpidityLevel += level; levelsWild[si] = level; } levelsWild[(int)StatNames.Torpidity] = torpidityLevel; var sex = species.noGender ? Sex.Unknown : rand.Next(2) == 0 ? Sex.Female : Sex.Male; var names = sex == Sex.Female ? _namesFemale : _namesMale; var name = names[rand.Next(names.Length)]; if (nameCounter.TryGetValue(name, out var nameCount)) { nameCounter[name]++; name += $" {nameCount + 1}"; } else { nameCounter.Add(name, 1); } var creature = new Creature(species, name, sex: sex, levelsWild: levelsWild, levelsDom: levelsDom, tamingEff: tamingEffectiveness) { guid = Guid.NewGuid() }; creature.RecalculateCreatureValues(levelStep); creature.colors = species.RandomSpeciesColors(rand); creatures.Add(creature); } if (breedGenerations > 0) { var creaturesBySpecies = creatures.GroupBy(c => c.Species).ToDictionary(g => g.Key, g => g.ToArray()); foreach (var s in creaturesBySpecies) { var newCreatures = BreedCreatures(s.Value, s.Key, breedGenerations, usePairsPerGeneration, probabilityHigherStat, randomMutationChance); if (newCreatures != null) { creatures.AddRange(newCreatures); } } } return(creatures); }