Skip to content

Commit

Permalink
Merge pull request #49 from harliq/SpellProb
Browse files Browse the repository at this point in the history
Spell Probability Percentage
  • Loading branch information
harliq authored Dec 27, 2021
2 parents 4d90114 + 11ce21f commit a87d3bf
Show file tree
Hide file tree
Showing 9 changed files with 800 additions and 14 deletions.
176 changes: 170 additions & 6 deletions WeenieFab/WeenieFab/EventsButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,7 +62,7 @@ private void btnSaveSqlFile_Click(object sender, RoutedEventArgs e)
Globals.FileChanged = false;
}
private void btnSaveAsSqlFile_Click(object sender, RoutedEventArgs e)
{
{
SaveFileAs();
ProgressBarClearAnimation();
Globals.FileChanged = false;
Expand Down Expand Up @@ -116,7 +117,7 @@ private void btnAddInt32_Click(object sender, RoutedEventArgs e)
dgInt32.Items.Refresh();
}
FileChanged();
}
}
private void btnInt32Remove_Click(object sender, RoutedEventArgs e)
{
var index = dgInt32.SelectedIndex;
Expand Down Expand Up @@ -156,7 +157,7 @@ private void btnUpdateInt32_Click(object sender, RoutedEventArgs e)

integerDataTable.AcceptChanges();
dgInt32.Items.Refresh();

}
catch (Exception ex)
{
Expand Down Expand Up @@ -184,7 +185,7 @@ private void btnAddInt64_Click(object sender, RoutedEventArgs e)
integer64DataTable.Rows.Add(dr);
integer64DataTable = ResortDataTable(integer64DataTable, "Property", "ASC");
dgInt64.DataContext = integer64DataTable.DefaultView;
dgInt64.Items.Refresh();
dgInt64.Items.Refresh();
}
FileChanged();
}
Expand Down Expand Up @@ -464,7 +465,7 @@ private void btnAddDiD_Click(object sender, RoutedEventArgs e)
didDataTable = ResortDataTable(didDataTable, "Property", "ASC");
dgDiD.DataContext = didDataTable.DefaultView;
dgDiD.Items.Refresh();

}
FileChanged();
}
Expand Down Expand Up @@ -663,6 +664,55 @@ 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<SpellProbabilityChances> spellProbList = new List<SpellProbabilityChances>();

List<float> SpellProb = new List<float>();

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(spellDataTable);
winSpellChances.Owner = this;

if (winSpellChances.ShowDialog() == true)
{
if (winSpellChances.SpellBookProbability is null)
{

}
else
{
spellDataTable = winSpellChances.SpellBookProbability;
spellDataTable.AcceptChanges();
dgSpell.DataContext = spellDataTable;
dgSpell.Items.Refresh();
}
}
else
{
}
}
// Attributes and Skills Tab
// Attributes
private void btnAttribDefaults_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -1406,5 +1456,119 @@ private DataRow ConvertToIntRow(string property, string propertyValue, string de

return tdr;
}


private string SpellbookToIndependent(List<float> SpellProb, List<SpellProbabilityChances> 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<float> SpellProb, List<SpellProbability> 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<float>();

//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;
}

/// <summary>
/// Returns the probability of none of the events occurring
/// from a list of chances
/// </summary>
public static float GetProbabilityNone(List<float> chances)
{
var probability = 1.0f;

foreach (var chance in chances)
probability *= 1.0f - chance;

return probability;
}

/// <summary>
/// Returns the probability of any of the events occurring
/// from a list of chances
/// </summary>
public static float GetProbabilityAny(List<float> chances)
{
return 1.0f - GetProbabilityNone(chances);
}

public static string PercentFormat(float percent)
{
return $"{Math.Round(percent * 100, 2)}%";
}
private class SpellProbabilityChances
{
public SpellProbabilityChances(string spellName, float spellProbability)
{
this.spell = spellName;
this.probabiliy = spellProbability;
}
public string spell { get; set; }
public float probabiliy { get; set; }
}
}
}
}
17 changes: 13 additions & 4 deletions WeenieFab/WeenieFab/Help.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,21 @@ public void CreateResourceList()
}
private void Hyperlink_OnRequestNavigate(object sender, RequestNavigateEventArgs e)
{
var urlPart = ((Hyperlink)sender).NavigateUri;
var fullUrl = urlPart.ToString();
Process.Start(new ProcessStartInfo("cmd", $"/c start {fullUrl}"));
// for .NET Core you need to add UseShellExecute = true
// see https://docs.microsoft.com/dotnet/api/system.diagnostics.processstartinfo.useshellexecute#property-value
Process browser = new Process();
browser.StartInfo.UseShellExecute = true;
browser.StartInfo.FileName = e.Uri.AbsoluteUri;
browser.Start();
e.Handled = true;

//var urlPart = ((Hyperlink)sender).NavigateUri;
//var fullUrl = urlPart.ToString();
//Process.Start(new ProcessStartInfo("cmd", $"/c start {fullUrl}"));
try
{
Process.Start(new ProcessStartInfo("cmd", $"/c start {fullUrl}"));
browser.Start();
//Process.Start(new ProcessStartInfo("cmd", $"/c start {fullUrl}"));
}
catch (Exception ex)
{
Expand Down
18 changes: 17 additions & 1 deletion WeenieFab/WeenieFab/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">


<Window.Resources>
<!--Control colors.-->
Expand Down Expand Up @@ -1979,6 +1979,12 @@
</TabItem>
<TabItem Header="SpellBook">
<Grid Background="#FFE1E8F5">
<TextBlock HorizontalAlignment="Left" Margin="300,0,20,0" VerticalAlignment="Top">
To see how to use this feature, please see the WeenieFab Wiki:
<Hyperlink NavigateUri="https://github.com/harliq/Weenie-Fab/wiki/Spellbook" RequestNavigate="Hyperlink_RequestNavigate">
WeenieFab Wiki - Spellbook Tab
</Hyperlink>
</TextBlock>
<Label Content="Probabilitly (Float)" HorizontalAlignment="Left" Margin="625,85,0,0" VerticalAlignment="Top"/>
<Label Content="Spell ID" HorizontalAlignment="Left" Margin="466,85,0,0" VerticalAlignment="Top"/>
<Label Content="Spell Description" HorizontalAlignment="Left" Margin="466,135,0,0" VerticalAlignment="Top"/>
Expand All @@ -1998,13 +2004,23 @@
<Button x:Name="btnAddSpell1" Content="Add " HorizontalAlignment="Left" Margin="466,191,0,0" VerticalAlignment="Top" Width="100" Click="btnAddSpell_Click" ToolTip="Add spell"/>
<Button x:Name="btnUpdateSpell1" Content="Update" HorizontalAlignment="Left" Margin="591,191,0,0" VerticalAlignment="Top" Click="btnUpdateSpell_Click" Width="100" RenderTransformOrigin="1.465,0.555" ToolTip="Update selected spell" IsDefault="True"/>
<Button x:Name="btnRemoveSpell1" Content="Remove" HorizontalAlignment="Left" Margin="716,191,0,0" VerticalAlignment="Top" Width="100" Click="btnRemoveSpell_Click" ToolTip="Remove selected spell"/>
<Button x:Name="btnGenerateProbability" Content="Generate Probability Details" HorizontalAlignment="Left" Margin="466,242,0,0" VerticalAlignment="Top" Width="163" Click="btnGenerateProbability_Click"/>
<DataGrid x:Name="dgSpell" ItemsSource="{Binding}" Margin="85,290,0,0" AutoGenerateColumns="False" DataGridCell.Selected="dgSpell_RowSelected" HorizontalGridLinesBrush="#FF767575" HorizontalAlignment="Left" MinHeight="275" MaxWidth="730" GridLinesVisibility="Horizontal" Background="#FFEDFCFF" MaxHeight="400" VerticalAlignment="Top" ToolTip="Select a spell to edit/change" CanUserSortColumns="False" CanUserReorderColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="SpellId" IsReadOnly="True" Binding="{Binding Property}" Width="1.5*"/>
<DataGridTextColumn Header="Probability" IsReadOnly="True" Binding="{Binding Value}" Width="1.7*"/>
<DataGridTextColumn Header="Description" IsReadOnly="True" Binding="{Binding Description}" Width="6.8*"/>
</DataGrid.Columns>
</DataGrid>
<TextBox x:Name="txtBoxSpellProbabilityDetails" HorizontalAlignment="Right" Margin="0,85,10,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="340" Height="480"/>
<Label Content="Spell Probability Details" HorizontalAlignment="Left" Margin="832,57,0,0" VerticalAlignment="Top" Width="225"/>
<Button x:Name="ButtonActualSpellChance" Content="Change Actual Spell Chance" HorizontalAlignment="Left" Margin="651,242,0,0" VerticalAlignment="Top" Width="164" Click="ButtonActualSpellChance_Click"/>
<TextBlock HorizontalAlignment="Left" Margin="85,0,0,2" VerticalAlignment="Bottom">
For more Info on how Spellbooks work, please see the ACE Wiki:
<Hyperlink NavigateUri="https://github.com/ACEmulator/ACE/wiki/Spell-Books" RequestNavigate="Hyperlink_RequestNavigate">
Ace Wiki - Spellbook Probability
</Hyperlink>
</TextBlock>
</Grid>
</TabItem>
<TabItem Header="CreateItems" Background="#FFE1E8F5">
Expand Down
11 changes: 11 additions & 0 deletions WeenieFab/WeenieFab/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,17 @@ private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs
FileChangedCheck();

}
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
// for .NET Core you need to add UseShellExecute = true
// see https://docs.microsoft.com/dotnet/api/system.diagnostics.processstartinfo.useshellexecute#property-value
Process browser = new Process();
browser.StartInfo.UseShellExecute = true;
browser.StartInfo.FileName = e.Uri.AbsoluteUri;
browser.Start();
e.Handled = true;
}

}

}
Loading

0 comments on commit a87d3bf

Please sign in to comment.