From 149912f8c24ccaf036b2e3ff6a05bb89a77bf5d3 Mon Sep 17 00:00:00 2001 From: cadon Date: Sun, 22 Nov 2015 01:40:44 +0100 Subject: [PATCH] bugfix: rounding-errors ui-improvements --- ARKBreedingStats/Form1.cs | 54 ++++++++++++++++++++++---------------- ARKBreedingStats/StatIO.cs | 26 +++++------------- 2 files changed, 38 insertions(+), 42 deletions(-) diff --git a/ARKBreedingStats/Form1.cs b/ARKBreedingStats/Form1.cs index af05378c..6ddc0327 100644 --- a/ARKBreedingStats/Form1.cs +++ b/ARKBreedingStats/Form1.cs @@ -19,7 +19,7 @@ public partial class Form1 : Form private List> results = new List>(); private int c = 0; // current creature private bool postTamed = false; - private int activeStat = 0; + private int activeStat = -1; private List statWithEff = new List(); private List chosenResults = new List(); private int[] precisions = new int[] { 1, 1, 1, 1, 1, 3, 3, 1 }; // damage and speed are percentagevalues, need more precision @@ -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()); @@ -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++) @@ -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) @@ -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 } } } @@ -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) { @@ -269,6 +272,7 @@ private void buttonCalculate_Click(object sender, EventArgs e) if (resultsValid) { buttonCopyClipboard.Enabled = true; + setActiveStat(activeStatKeeper); } if (!postTamed) { @@ -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; } } } @@ -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() + "%" : "")); } } } @@ -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); } @@ -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; diff --git a/ARKBreedingStats/StatIO.cs b/ARKBreedingStats/StatIO.cs index 5f9c6e28..7fcc1c61 100644 --- a/ARKBreedingStats/StatIO.cs +++ b/ARKBreedingStats/StatIO.cs @@ -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 @@ -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"; } } } @@ -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: @@ -147,6 +134,7 @@ public int Warning break; } } + get { return warning; } } public int BarLength