From 4526bfa21851e2ce35750e37c41128ecb1dcc075 Mon Sep 17 00:00:00 2001 From: harliq <30803300+harliq@users.noreply.github.com> Date: Fri, 24 Dec 2021 21:23:55 -0500 Subject: [PATCH 1/7] Add Spellbook Spell Chances --- WeenieFab/WeenieFab/EventsButton.cs | 150 +++++++++- WeenieFab/WeenieFab/MainWindow.xaml | 7 +- WeenieFab/WeenieFab/MainWindow.xaml.cs | 2 + WeenieFab/WeenieFab/SpellProbability.xaml | 284 +++++++++++++++++++ WeenieFab/WeenieFab/SpellProbability.xaml.cs | 32 +++ 5 files changed, 473 insertions(+), 2 deletions(-) create mode 100644 WeenieFab/WeenieFab/SpellProbability.xaml create mode 100644 WeenieFab/WeenieFab/SpellProbability.xaml.cs diff --git a/WeenieFab/WeenieFab/EventsButton.cs b/WeenieFab/WeenieFab/EventsButton.cs index ce7b26d..d35d46d 100644 --- a/WeenieFab/WeenieFab/EventsButton.cs +++ b/WeenieFab/WeenieFab/EventsButton.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Data; using System.Diagnostics; +using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Windows; @@ -663,6 +664,39 @@ private void btnRemoveSpell_Click(object sender, RoutedEventArgs e) } FileChanged(); } + private void btnGenerateProbability_Click(object sender, RoutedEventArgs e) + { + txtBoxSpellProbabilityDetails.Text = ""; + + int counter = 1; + int rowcount = spellDataTable.Rows.Count; + float tempProb = 0; + + List spellProbList = new List(); + + List SpellProb = new List(); + + if (rowcount > 0) + { + + foreach (DataRow row in spellDataTable.Rows) + { + tempProb = ConvertToFloat(row[1].ToString()); + SpellProb.Add(ConvertToFloat(row[1].ToString()) - 2); + + spellProbList.Add(new SpellProbabilityChances(row[2].ToString(), ConvertToFloat(row[1].ToString()) - 2)); + } + } + txtBoxSpellProbabilityDetails.Text = SpellbookToIndependent(SpellProb, spellProbList); + + } + private void ButtonActualSpellChance_Click(object sender, RoutedEventArgs e) + { + SpellProbability winSpellChances = new SpellProbability(); + winSpellChances.Owner = this; + winSpellChances.Show(); + + } // Attributes and Skills Tab // Attributes private void btnAttribDefaults_Click(object sender, RoutedEventArgs e) @@ -1406,5 +1440,119 @@ private DataRow ConvertToIntRow(string property, string propertyValue, string de return tdr; } + + + private string SpellbookToIndependent(List SpellProb, List spellProbabilities) + { + string spellProbablityInfo = ""; + //string individualOverallSpellCastingPercentage = ""; + //float totalPercentChance = 0; + //float totalIndependentChance = 0; + + + spellProbablityInfo += $"Spellbook probabilities: {string.Join(", ", SpellProb.Select(i => PercentFormat(i)))}\n\n"; + + var castChance = GetProbabilityAny(SpellProb); + + spellProbablityInfo += $"Chance for each spell:\n\n"; + + for (var i = 0; i < SpellProb.Count; i++) + { + var prevChanceNone = i > 0 ? GetProbabilityNone(SpellProb.GetRange(0, i)) : 1.0f; + + //spellProbablityInfo += $"Chance of casting spell #{i + 1}: {PercentFormat(SpellProb[i] * prevChanceNone)}\n"; + spellProbablityInfo += $"{spellProbabilities.ElementAt(i).spell}: {PercentFormat(SpellProb[i] * prevChanceNone)}\n"; + + } + spellProbablityInfo += $"\nTotal casting chance: {PercentFormat(castChance)}\n"; + + return spellProbablityInfo; + } + + + + private string IndependentToSpellbook(List SpellProb, List spellProbabilities) + { + + string spellProbablityInfo = ""; + string individualOverallSpellCastingPercentage = ""; + float totalPercentChance = 0; + float totalIndependentChance = 0; + + + spellProbablityInfo = $"Independent probabilities (cannot go above 100% additive): {string.Join(", ", SpellProb.Select(i => PercentFormat(i)))}\n"; + foreach (var independentChance in SpellProb) + totalIndependentChance += independentChance; + if(totalIndependentChance > 1) + spellProbablityInfo += $"\n*WARNING* Independent Additive Probability is over 100% \nTotal Independent Additive Probability is {PercentFormat(totalIndependentChance)}\n\n"; + else + spellProbablityInfo += $"\nTotal Independent Additive Probability is {PercentFormat(totalIndependentChance)}\n\n"; + + var spellbook = new List(); + + //for (var i = 0; i < SpellProb.Count; i++) + //{ + // var prevChanceNone = i > 0 ? GetProbabilityNone(spellbook) : 1.0f; + + // //spellbook.Add(SpellProb[i] / prevChanceNone); + // spellbook.Add(SpellProb[i] * prevChanceNone); + //} + + for (var i = 0; i < SpellProb.Count; i++) + { + var prevChanceNone = i > 0 ? GetProbabilityNone(spellbook) : 1.0f; + + //spellbook.Add(SpellProb[i] / prevChanceNone); + spellbook.Add(SpellProb[i] * prevChanceNone); + //individualOverallSpellCastingPercentage += $"{spellProbabilities.GetType}"; + } + + + spellProbablityInfo += $"Spellbook format: {string.Join(", ", spellbook.Select(i => PercentFormat(i)))}\n"; + + foreach (var percent in spellbook) + totalPercentChance += percent; + spellProbablityInfo += $"\nTotal chance of casting a spell = {PercentFormat(totalPercentChance)}"; + + return spellProbablityInfo; + } + + /// + /// Returns the probability of none of the events occurring + /// from a list of chances + /// + public static float GetProbabilityNone(List chances) + { + var probability = 1.0f; + + foreach (var chance in chances) + probability *= 1.0f - chance; + + return probability; + } + + /// + /// Returns the probability of any of the events occurring + /// from a list of chances + /// + public static float GetProbabilityAny(List chances) + { + return 1.0f - GetProbabilityNone(chances); + } + + public static string PercentFormat(float percent) + { + return $"{Math.Round(percent * 100, 2)}%"; + } + class SpellProbabilityChances + { + public SpellProbabilityChances(string spellName, float spellProbability) + { + this.spell = spellName; + this.probabiliy = spellProbability; + } + public string spell { get; set; } + public float probabiliy { get; set; } + } } -} +} \ No newline at end of file diff --git a/WeenieFab/WeenieFab/MainWindow.xaml b/WeenieFab/WeenieFab/MainWindow.xaml index 263cd5f..fbe8f70 100644 --- a/WeenieFab/WeenieFab/MainWindow.xaml +++ b/WeenieFab/WeenieFab/MainWindow.xaml @@ -6,7 +6,7 @@ xmlns:local="clr-namespace:WeenieFab" mc:Ignorable="d" Title="Weenie Fab" Height="775" Width="1200" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" WindowStartupLocation="CenterScreen" Icon="/weeniefactoryicon.ico" Closing="Window_Closing"> - + @@ -1998,6 +1998,7 @@