Skip to content

Commit

Permalink
bugfix: rounding-errors
Browse files Browse the repository at this point in the history
ui-improvements
  • Loading branch information
cadon committed Nov 22, 2015
1 parent 4bd81f0 commit 149912f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 42 deletions.
54 changes: 31 additions & 23 deletions ARKBreedingStats/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public partial class Form1 : Form
private List<List<double[]>> results = new List<List<double[]>>();
private int c = 0; // current creature
private bool postTamed = false;
private int activeStat = 0;
private int activeStat = -1;
private List<int> statWithEff = new List<int>();
private List<int> chosenResults = new List<int>();
private int[] precisions = new int[] { 1, 1, 1, 1, 1, 3, 3, 1 }; // damage and speed are percentagevalues, need more precision
Expand Down Expand Up @@ -69,14 +69,16 @@ private void clearAll()
this.numericUpDownLowerTEffL.BackColor = SystemColors.Window;
this.numericUpDownLowerTEffU.BackColor = SystemColors.Window;
buttonCopyClipboard.Enabled = false;
activeStat = -1;
}

private void buttonCalculate_Click(object sender, EventArgs e)
{
int activeStatKeeper = activeStat;
clearAll();
bool resultsValid = true;
// torpor is directly proportional to wild level (after taming it's a too high estimate, making the upper bound worse)
postTamed = (stats[c][7][0] + stats[c][7][0] * stats[c][7][1] * Math.Round((statIOs[7].Input - stats[c][7][0]) / (stats[c][7][0] * stats[c][7][1]), 0) != statIOs[7].Input);
postTamed = (stats[c][7][0] + stats[c][7][0] * stats[c][7][1] * Math.Round((statIOs[7].Input - stats[c][7][0]) / (stats[c][7][0] * stats[c][7][1])) != statIOs[7].Input);
for (int s = 0; s < statIOs.Count; s++)
{
results.Add(new List<double[]>());
Expand All @@ -90,10 +92,10 @@ private void buttonCalculate_Click(object sender, EventArgs e)
double maxLD = 0;
if (stats[c][s][0] > 0 && stats[c][s][2] > 0 && postTamed)
{
maxLD = Math.Floor((inputValue - stats[c][s][0]) / (stats[c][s][0] * stats[c][s][2]));
maxLD = Math.Round((inputValue - stats[c][s][0]) / (stats[c][s][0] * stats[c][s][2])); //floor is sometimes too unprecise
}
double vWildL = 0; // value with only wild levels
double tamingEfficiency = 0;
double tamingEfficiency = -1;
bool withTEff = (postTamed && stats[c][s][4] > 0);
if (withTEff) { statWithEff.Add(s); }
for (int w = 0; w < maxLW + 1; w++)
Expand All @@ -105,7 +107,7 @@ private void buttonCalculate_Click(object sender, EventArgs e)
{
// taming bonus is percentual, this means the taming-efficiency plays a role
// get tamingEfficiency-possibility
tamingEfficiency = Math.Round((inputValue / (1 + stats[c][s][2] * d) - vWildL) / (vWildL * stats[c][s][4]), 3);
tamingEfficiency = Math.Round((inputValue / (1 + stats[c][s][2] * d) - vWildL) / (vWildL * stats[c][s][4]), 3, MidpointRounding.AwayFromZero);
if (tamingEfficiency * 100 >= (double)this.numericUpDownLowerTEffL.Value)
{
if (tamingEfficiency * 100 <= (double)this.numericUpDownLowerTEffU.Value)
Expand All @@ -120,9 +122,10 @@ private void buttonCalculate_Click(object sender, EventArgs e)
break;
}
}
else if (Math.Round(vWildL + vWildL * stats[c][s][2] * d, precisions[s]) == inputValue)
else if (Math.Round(vWildL + vWildL * stats[c][s][2] * d, precisions[s], MidpointRounding.AwayFromZero) == inputValue)
{
results[s].Add(new double[] { w, d, tamingEfficiency });
break; // no other solution possible
}
}
}
Expand Down Expand Up @@ -224,7 +227,7 @@ private void buttonCalculate_Click(object sender, EventArgs e)
{
// display result with most levels in wild (most probable for the stats getting not unique results here)
int r = 0;
if (results[s][0][2] == 0) { r = results[s].Count - 1; }
if (results[s][0][2] == -1) { r = results[s].Count - 1; }
setPossibility(s, r);
if (results[s].Count > 1)
{
Expand Down Expand Up @@ -269,6 +272,7 @@ private void buttonCalculate_Click(object sender, EventArgs e)
if (resultsValid)
{
buttonCopyClipboard.Enabled = true;
setActiveStat(activeStatKeeper);
}
if (!postTamed)
{
Expand All @@ -281,19 +285,24 @@ private void statIO_Click(object sender, EventArgs e)
StatIO se = (StatIO)sender;
if (se != null)
{
int ss = se.Id;
for (int s = 0; s < 8; s++)
setActiveStat(se.Id);
}
}

private void setActiveStat(int stat)
{
this.listBoxPossibilities.Items.Clear();
for (int s = 0; s < 8; s++)
{
if (s == stat && statIOs[s].Warning == 1)
{
if (s == ss)
{
statIOs[s].Selected = true;
activeStat = s;
setCombobox(s);
}
else
{
statIOs[s].Selected = false;
}
statIOs[s].Selected = true;
activeStat = s;
setCombobox(s);
}
else
{
statIOs[s].Selected = false;
}
}
}
Expand All @@ -302,10 +311,9 @@ private void setCombobox(int s)
{
if (s < results.Count)
{
this.listBoxPossibilities.Items.Clear();
for (int r = 0; r < results[s].Count; r++)
{
this.listBoxPossibilities.Items.Add(results[s][r][0].ToString() + "\t" + results[s][r][1].ToString() + (results[s][r][2] > 0 ? "\t" + (results[s][r][2] * 100).ToString() + "%" : ""));
this.listBoxPossibilities.Items.Add(results[s][r][0].ToString() + "\t" + results[s][r][1].ToString() + (results[s][r][2] >= 0 ? "\t" + (results[s][r][2] * 100).ToString() + "%" : ""));
}
}
}
Expand Down Expand Up @@ -379,7 +387,7 @@ private void comboBoxCreatures_SelectedIndexChanged(object sender, EventArgs e)
private void listBoxPossibilities_MouseClick(object sender, MouseEventArgs e)
{
int index = this.listBoxPossibilities.IndexFromPoint(e.Location);
if (index != System.Windows.Forms.ListBox.NoMatches)
if (index != System.Windows.Forms.ListBox.NoMatches && activeStat >= 0)
{
setPossibility(activeStat, index);
}
Expand Down Expand Up @@ -426,7 +434,7 @@ private double breedingValue(int s, int r)
{
if (r >= 0 && r < results[s].Count)
{
return Math.Round((stats[c][s][0] + stats[c][s][0] * stats[c][s][1] * results[s][r][0] + stats[c][s][3]) * (1 + stats[c][s][4] * results[s][r][2]), precisions[s]);
return Math.Round((stats[c][s][0] + stats[c][s][0] * stats[c][s][1] * results[s][r][0] + stats[c][s][3]) * (results[s][r][2] >= 0 ? (1 + stats[c][s][4] * results[s][r][2]) : 1), precisions[s], MidpointRounding.AwayFromZero);
}
}
return -1;
Expand Down
26 changes: 7 additions & 19 deletions ARKBreedingStats/StatIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,21 @@ public StatIO()

private void groupBox1_Click(object sender, System.EventArgs e)
{
if (warning == 1)
{
this.OnClick(e);
}
this.OnClick(e);
}
private void labelLvW_Click(object sender, EventArgs e)
{
if (warning == 1)
{
this.OnClick(e);
}
this.OnClick(e);
}

private void labelLvD_Click(object sender, EventArgs e)
{
if (warning == 1)
{
this.OnClick(e);
}
this.OnClick(e);
}

private void labelBValue_Click(object sender, EventArgs e)
{
if (warning == 1)
{
this.OnClick(e);
}
this.OnClick(e);
}

public double Input
Expand Down Expand Up @@ -86,7 +74,7 @@ public double BreedingValue
{
set
{
if (value > 0) { this.labelBValue.Text = Math.Round((percent ? 100 : 1) * value, 1).ToString() + (percent ? " %" : "") + (postTame ? "" : "+*"); }
if (value >= 0) { this.labelBValue.Text = Math.Round((percent ? 100 : 1) * value, 1).ToString() + (percent ? " %" : "") + (postTame ? "" : "+*"); }
else { this.labelBValue.Text = "error"; }
}
}
Expand Down Expand Up @@ -131,14 +119,13 @@ public int Warning
{
warning = value;
this.ForeColor = SystemColors.ControlText;
this.numericUpDownInput.BackColor = System.Drawing.SystemColors.Window;
switch (warning)
{
case 0:
this.numericUpDownInput.BackColor = System.Drawing.SystemColors.Window;
this.BackColor = SystemColors.Control;
break;
case 1:
this.numericUpDownInput.BackColor = Color.LightYellow;
this.BackColor = SystemColors.ControlDark;
break;
case 2:
Expand All @@ -147,6 +134,7 @@ public int Warning
break;
}
}
get { return warning; }
}

public int BarLength
Expand Down

0 comments on commit 149912f

Please sign in to comment.