Exemplo n.º 1
0
        public FormHorseViewEdit(FormGrangerMain mainForm, Horse horse, GrangerContext context, HorseViewEditOpType optype, string herdID)
        {
            this.MainForm = mainForm;
            this.horse = horse;
            this.Context = context;
            this.HerdID = herdID;
            InitializeComponent();

            disableAllFields();

            List<string> list = new List<string>();
            list.AddRange(Context.Horses.Select(x => x.Name));
            list.AddRange(Context.Horses.Select(x => x.MotherName));
            list.AddRange(Context.Horses.Select(x => x.FatherName));
            string[] allHorseNamesInDatabase = list.Distinct().Where(x => x != null).ToArray();

            comboBoxFather.Items.AddRange(allHorseNamesInDatabase);
            comboBoxMother.Items.AddRange(allHorseNamesInDatabase);

            comboBoxColor.Items.AddRange(HorseColor.GetColorsEnumStrArray());
            comboBoxColor.Text = HorseColor.GetDefaultColorStr();
            comboBoxAge.Items.AddRange(HorseAge.GetColorsEnumStrArray());
            comboBoxAge.Text = HorseAge.GetDefaultAgeStr();

            this.OpMode = optype;
        }
Exemplo n.º 2
0
 internal BreedingEvalResults? GetBreedingValue(Horse horse)
 {
     if (IsDisabled) return null;
     if (MainForm.SelectedSingleHorse != null) //this is cached value
     {
         // this is the currently user-selected horse, while parameter horses are iterated by display process
         Horse evaluatedHorse = MainForm.SelectedSingleHorse;
         return BreedEvalutator.Evaluate(evaluatedHorse, horse, MainForm.CurrentValuator);
     }
     else return null;
 }
        public override BreedingEvalResults? Evaluate(Horse horse1, Horse horse2, TraitValuator valuator)
        {
            if (horse1 == horse2) return null;

            BreedingEvalResults results = new BreedingEvalResults();

            var allPossibleTraits = HorseTrait.GetAllPossibleTraits();
            var traits1 = horse1.Traits;
            var traits2 = horse2.Traits;
            var concatTraits = traits1.Concat<HorseTrait>(traits2).ToArray(); //horse1 + horse2
            var presentTraits = concatTraits.Distinct().ToArray(); //horse1 + horse2 but without duplicates
            // not using these for now:
            //var uniqueTraits = GetUniqueTraits(presentTraits, traits1, traits2);  //traits which only one horse have
            //var missingTraits = HorseTrait.GetAllTraits().Where(x => !presentTraits.Contains(x)).ToArray(); //all traits that horses dont have
            double value2 = horse2.Value;

            if (horse1.IsMale == horse2.IsMale) results.Ignored = true;

            if (_options.IgnoreNotInMood)
                if (horse1.NotInMood || horse2.NotInMood) results.Ignored = true;

            if (_options.IgnorePregnant)
                if (horse1.Pregnant || horse2.Pregnant) results.Ignored = true;

            if (_options.IgnoreRecentlyPregnant)
                if (horse1.PregnantInLast24h || horse2.PregnantInLast24h) results.Ignored = true;

            if (_options.IgnoreOtherHerds)
                if (horse1.Herd != horse2.Herd) results.Ignored = true;

            if (_options.IgnorePairedHorses)
                if (horse1.HasMate() || horse2.HasMate())
                    results.Ignored = true;

            if (_options.IgnoreSold)
                if (horse1.CheckTag("sold") || horse2.CheckTag("sold"))
                    results.Ignored = true;

            if (_options.IgnoreDead)
                if (horse1.CheckTag("dead") || horse2.CheckTag("dead"))
                    results.Ignored = true;

            if (_options.IgnoreFoals)
                if ((horse1.IsFoal() && !_options.AgeIgnoreOnlyOtherHorses) ||
                    horse2.IsFoal()) results.Ignored = true;

            if (_options.IgnoreYoung)
                if (((horse1.Age.EnumVal == HorseAge.Age.Young) && !_options.AgeIgnoreOnlyOtherHorses) ||
                    horse2.Age.EnumVal == HorseAge.Age.Young)
                    results.Ignored = true;

            if (_options.IgnoreAdolescent)
                if (((horse1.Age.EnumVal == HorseAge.Age.Adolescent) && !_options.AgeIgnoreOnlyOtherHorses) ||
                    horse2.Age.EnumVal == HorseAge.Age.Adolescent)
                    results.Ignored = true;

            if (_options.ExcludeExactAgeEnabled)
            {

                if (DateTime.Now - horse1.BirthDate < _options.ExcludeExactAgeValue ||
                    DateTime.Now - horse2.BirthDate < _options.ExcludeExactAgeValue)
                {
                    results.Ignored = true;
                }
            }

            if (horse1.IsInbreedWith(horse2))
            {
                if (_options.DiscardOnInbreeding) results.Discarded = true;
                else
                {
                    // get all potential inbreeding-specific bad traits this horse doesnt yet have,
                    // average a value out of these traits,
                    // multiply by 2 (because this is like both horses having one bad trait)
                    // multiply by inbreed weight (NOT bad trait weight)
                    // we add this to results
                    var potentialBadTraits = HorseTrait.GetInbreedBadTraits().Where(x => !presentTraits.Contains(x)).ToArray();
                    double sum = 0;
                    foreach (var trait in potentialBadTraits)
                    {
                        sum += trait.GetTraitValue(valuator);
                    }
                    sum /= potentialBadTraits.Length;
                    sum *= _options.InbreedingPenaltyWeight * 2;
                    results.Value += sum;
                }
            }

            if (_options.DiscardOnAnyNegativeTraits)
            {
                if (horse2.Traits.Where(x => x.GetTraitValue(valuator) < 0).Count() > 0)
                    results.Discarded = true;
            }

            // continue only if horse is still evaluated
            if (results.Discarded != true && results.Ignored != true)
            {
                // calculate value for each trait:
                // use 1.0, bad trait or unique trait weights if appropriate
                // use dict to check, which traits were already handled, 
                // the value of keys is meaningless, only key presence check is needed
                Dictionary<HorseTrait, int> uniqueTraitCounter = new Dictionary<HorseTrait, int>();
                foreach (var trait in concatTraits)
                {
                    //add this trait to counter for PreferUniqueTraits
                    if (uniqueTraitCounter.ContainsKey(trait))
                    {
                        uniqueTraitCounter[trait] += 1;
                    }
                    else
                    {
                        uniqueTraitCounter[trait] = 1;
                    }
                    var traitval = trait.GetTraitValue(valuator);
                    double result = 0;
                    if (traitval < 0) result += traitval * _options.BadTraitWeight;
                    else if (traitval > 0) result += traitval;

                    results.Value += result;
                }

                //apply bonus for unique traits
                if (_options.PreferUniqueTraits)
                {
                    foreach (var keyval in uniqueTraitCounter)
                    {
                        if (keyval.Value == 1) //this trait was unique in this evaluation
                        {
                            var traitval = keyval.Key.GetTraitValue(valuator);
                            if (traitval > 0) //apply bonus if the trait is positive value
                            {
                                results.Value += (traitval * _options.UniqueTraitWeight) - traitval;
                                //subtracting initial traitval because it was already applied
                                //this works for any weight, a 0.5 weight causes unique trait to have half value for result
                                //0.0 weight causes trait to have 0 value for result (effectively nullifying this trait value)

                                // NOTE: if in future good trait value has any other weights applied,
                                // this WILL break. This class is not expected to be improved,
                                // please write your own, new evaluator by subclassing BreedingEvaluator class
                                // and writing your own complete logic!
                            }
                        }
                    }

                }

                if (_options.IncludePotentialValue)
                {
                    // here we need to take care of potential trait values
                    // this is hard to figure, because horse can contain many different hidden traits,
                    // that all can participate in breeding

                    // we handle this naively, asume horses have all of their potential traits
                    // we regulate how much this affects result with the weight

                    // we need to loop over all possible traits twice, for each horse
                    // pick traits that req AH above their inspect skill and do eval for these
                    // we do this explicitly rather than in methods to improve readability
                    foreach (var trait in allPossibleTraits)
                    {
                        double result = 0;
                        result += EvaluatePotentialValue(horse1, valuator, trait);
                        result += EvaluatePotentialValue(horse2, valuator, trait);
                        results.Value += result;
                    }
                }

                // boost or lower value based on potential color of offspring
                if (results.Value > 0)
                {
                    var h1colVal = _options.GetValueForColor(horse1.Color);
                    var h2colVal = _options.GetValueForColor(horse2.Color);
                    var colValAdj = (h1colVal + h2colVal)*0.5f;
                    results.Value *= colValAdj;
                }
            }
            return results;
        }
Exemplo n.º 4
0
 internal System.Drawing.Color? GetHintColor(Horse horse, double minBreedValue, double maxBreedValue)
 {
     if (BreedEvalutator == null) return null;
     else
     {
         return BreedEvalutator.GetHintColor(horse, minBreedValue, maxBreedValue);
     }
 }
Exemplo n.º 5
0
 internal int GetPotentialNegativeValueForHorse(Horse horse)
 {
     return GetPotentialValueForHorse(horse, false);
 }
Exemplo n.º 6
0
 private void objectListView1_SelectionChanged(object sender, EventArgs e)
 {
     var newSelectedHorses = SelectedHorses;
     bool changed = SelectionChangedCheck(lastSelectedHorses, newSelectedHorses);
     if (!_updatingListView && changed)
     {
         var selhorses = newSelectedHorses;
         Logger.LogDebug("Selected horses changed, array count: " + selhorses.Length);
         if (selhorses.Length == 1)
         {
             Logger.LogDebug("Selected single horse, array count: " + selhorses.Length);
             var horse = selhorses[0];
             if (!horse.Equals(SelectedSingleHorse)) //change only if new selected horse is different
             {
                 Logger.LogDebug("Selected single horse changing");
                 SelectedSingleHorse = selhorses[0];
             }
         }
         else SelectedSingleHorse = null;
     }
     lastSelectedHorses = newSelectedHorses;
 }
Exemplo n.º 7
0
 /// <summary>
 /// get evaluation results for these horse,
 /// null if evaluation does not return any valid results
 /// </summary>
 /// <param name="horse1">first evaluated horse</param>
 /// <param name="horse2">other evaluated horse</param>
 /// <param name="valuator">current trait valuator used by granger</param>
 /// <returns></returns>
 public abstract BreedingEvalResults? Evaluate(Horse horse1, Horse horse2, TraitValuator valuator);
Exemplo n.º 8
0
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (!ValidateHorseIdentity())
            {
                this.DialogResult = System.Windows.Forms.DialogResult.None;
            }
            else
            {
                try
                {
                    if (OpMode == HorseViewEditOpType.New)
                    {
                        var newEntity = new HorseEntity() { ID = HorseEntity.GenerateNewHorseID(Context) };
                        horse = new Horse(MainForm, newEntity, Context);
                    }
                    horse.Name = textBoxName.Text;
                    horse.Father = comboBoxFather.Text;
                    horse.Mother = comboBoxMother.Text;
                    horse.TakenCareOfBy = textBoxCaredForBy.Text;
                    horse.BrandedFor = textBoxBrandedFor.Text;

                    List<HorseTrait> traitlist = new List<HorseTrait>();
                    foreach (var item in checkedListBoxTraits.CheckedItems)
                    {
                        try
                        {
                            traitlist.Add(HorseTrait.FromWurmTextRepr(item.ToString()));
                        }
                        catch (Exception _e)
                        {
                            Logger.LogError("failed to create horse trait from text:" + item.ToString(), this, _e);
                        }
                    }
                    var traitlistArray = traitlist.ToArray();
                    horse.Traits = traitlistArray;

                    horse.TraitsInspectSkill = (float)numericUpDownAHskill.Value;
                    float traitSkill = HorseTrait.GetMinSkillForTraits(traitlistArray, checkBoxEpic.Checked);
                    if (horse.TraitsInspectSkill < traitSkill)
                        horse.TraitsInspectSkill = traitSkill;
                    horse.EpicCurve = checkBoxEpic.Checked;

                    horse.Comments = textBoxComment.Text;
                    horse.IsMale = radioButtonMale.Checked;
                    horse.Color = HorseColor.CreateColorFromEnumString(comboBoxColor.Text);
                    horse.Age = HorseAge.CreateAgeFromEnumString(comboBoxAge.Text);

                    horse.NotInMoodUntil = dateTimePickerBred.Value;
                    horse.GroomedOn = dateTimePickerGroomed.Value;
                    horse.PregnantUntil = dateTimePickerPregnant.Value;
                    horse.BirthDate = dateTimePickerBirthDate.Value;

                    horse.SetTag("diseased", checkBoxDiseased.Checked);
                    horse.SetTag("dead", checkBoxDead.Checked);
                    horse.SetTag("sold", checkBoxSold.Checked);

                    if (OpMode == HorseViewEditOpType.New)
                    {
                        horse.Herd = HerdID;
                        Context.InsertHorse(horse.Entity);
                    }
                    else
                    {
                        Context.SubmitChangesToHorses();
                    }
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                catch (Exception _e)
                {
                    Logger.LogError("problem while updating database, op: " + OpMode, this, _e);
                    MessageBox.Show("There was a problem on submitting to database.\r\n" + _e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.DialogResult = System.Windows.Forms.DialogResult.None;
                }
            }
        }
Exemplo n.º 9
0
 private void BuildTraitView(Horse horse)
 {
     HorseTrait[] currentHorseTraits = horse.Traits;
     Items.Clear();
     foreach (var trait in AllTraits)
     {
         Items.Add(new TraitItem()
         {
             DisplayMode = MainForm.TraitViewDisplayMode,
             Trait = trait,
             Exists = currentHorseTraits.Contains(trait),
             Unknown = trait.IsUnknownForThisHorse(horse),
             Value = trait.GetTraitValue(MainForm.CurrentValuator)
         });
     }
 }
Exemplo n.º 10
0
 internal bool IsInbreedWith(Horse otherHorse)
 {
     if (   Name == otherHorse.Mother
         || Name == otherHorse.Father
         || Mother == otherHorse.Name
         || Father == otherHorse.Name
         || (!string.IsNullOrEmpty(Mother) && Mother == otherHorse.Mother)
         || (!string.IsNullOrEmpty(Father) && Father == otherHorse.Father))
         return true;
     else return false;
 }
Exemplo n.º 11
0
        public bool Equals(Horse p)
        {
            // If parameter is null return false:
            if ((object)p == null)
            {
                return false;
            }

            // Return true if the fields match:
            return this.Entity.ID == p.Entity.ID;
        }
Exemplo n.º 12
0
 /// <summary>
 /// Updated: this now only compares horse names due to issues with name+gender setup
 /// </summary>
 /// <param name="other"></param>
 /// <returns></returns>
 public bool IsIdenticalIdentity(Horse other)
 {
     return !this.Entity.IsDifferentIdentityThan(other.Entity);
 }
Exemplo n.º 13
0
 public void SetMate(Horse value)
 {
     if (value == null)
     {
         var mate = GetMate();
         if (mate != null) mate.Entity.PairedWith = null;
         this.Entity.PairedWith = null;
     }
     else
     {
         this.Entity.PairedWith = value.Entity.ID;
         value.Entity.PairedWith = this.Entity.ID;
     }
 }
Exemplo n.º 14
0
        private bool SelectionChangedCheck(Horse[] lastSelectedHorses, Horse[] newSelectedHorses)
        {
            if (lastSelectedHorses == null && newSelectedHorses == null)
                return false;

            if (lastSelectedHorses == null && newSelectedHorses != null ||
                lastSelectedHorses != null && newSelectedHorses == null)
                return true;

            if (lastSelectedHorses.Length != newSelectedHorses.Length) return true;
            else
            {
                foreach (var horse in lastSelectedHorses)
                {
                    if (!newSelectedHorses.Contains(horse)) return true;
                }
            }

            return false;
        }
 private double EvaluatePotentialValue(Horse horse, TraitValuator valuator, HorseTrait trait)
 {
     double result = 0;
     if (trait.IsUnknownForThisHorse(horse))
     {
         var traitval = trait.GetTraitValue(valuator);
         if (traitval > 0)
         {
             result += traitval * _options.PotentialValuePositiveWeight;
         }
         else if (traitval < 0)
         {
             result += traitval * _options.PotentialValueNegativeWeight;
         }
         // we dont care when its 0
     }
     return result;
 }
        public override System.Drawing.Color? GetHintColor(Horse horse, double minBreedValue, double maxBreedValue)
        {
            if (horse.CachedBreedValue.HasValue == false) { return DefaultIgnoredHintColor; }
            if (horse.CachedBreedValue == double.PositiveInfinity) { return DefaultExcludedHintColor; }

            CustomColors.HSLColor color = new CustomColors.HSLColor();
            color.Luminosity = 210D;
            color.Saturation = 240D;
            color.Hue = 35D; //0 is red, 35 is yellow, 70 is green
            //for best candidate (maxBreedValue) name-only highlights: HSLColor(120D, 240D, 180D)

            double spectrum = Math.Max(Math.Abs(minBreedValue), Math.Abs(maxBreedValue));
            //normalize breed value to the spectrum
            double normalizedBValue = horse.CachedBreedValue.Value / spectrum;
            color.Hue += normalizedBValue * 35;
            return color;
        }
Exemplo n.º 17
0
 internal int GetValueForHorse(Horse horse)
 {
     return Evaluate(horse.Traits);
 }
Exemplo n.º 18
0
 int GetPotentialValueForHorse(Horse horse, bool positive)
 {
     var missingTraits = HorseTrait.GetMissingTraits(horse.Traits, horse.TraitsInspectSkill, horse.EpicCurve);
     return Evaluate(missingTraits, positive);
 }
Exemplo n.º 19
0
 internal int GetPotentialPositiveValueForHorse(Horse horse)
 {
     return GetPotentialValueForHorse(horse, true);
 }
Exemplo n.º 20
0
 /// <summary>
 /// Determines color for color-cued highlights
 /// </summary>
 /// <param name="horse">evaluated horse</param>
 /// <param name="minBreedValue">minimum breed value for the batch</param>
 /// <param name="maxBreedValue">maximum breed value for the batch</param>
 /// <returns></returns>
 public abstract System.Drawing.Color? GetHintColor(Horse horse, double minBreedValue, double maxBreedValue);
Exemplo n.º 21
0
 internal bool IsUnknownForThisHorse(Horse horse)
 {
     if (HorseTrait.GetSkillForTrait(this, horse.EpicCurve) > horse.TraitsInspectSkill)
         return true;
     else return false;
 }