コード例 #1
0
        public void SelectTest([Values(-0.1f, 0.0f, 0.1f, 0.5f, 0.9f, 1.0f, 2.0f)] float proportion)
        {
            var cP     = proportion.Clamp01();
            var rn     = new MockRandom();
            var rnList = new List <double>();

            for (int i = 0; i < 100; i++)
            {
                rnList.Add(_rnd.NextDouble());
            }

            rn.Values = rnList;
            var s = new WeightedRandomSelector(rn);

            s.Proportion = proportion;

            var uList  = SelectorTestsHelper.CreateRandomUtilityList(20);
            var sorted = uList.OrderByDescending(x => x.Combined).ToList();

            for (int curI = 0; curI < 100; curI++)
            {
                Utility cUtil = CorrectUtility(curI, cP, uList, sorted, rnList);

                var aIdx  = s.Select(uList);
                var aUtil = new Utility(0.0f, 0.0f);
                if (aIdx >= 0)
                {
                    aUtil = uList[aIdx];
                }

                Assert.That(aUtil.Value, Is.EqualTo(cUtil.Value).Within(Tolerance));
                Assert.That(aUtil.Weight, Is.EqualTo(cUtil.Weight).Within(Tolerance));
            }
        }
コード例 #2
0
        public void ZeroSizeUtilityListSelectTest()
        {
            var uList = new List <Utility>();
            var s     = new WeightedRandomSelector();

            Assert.That(s.Select(uList), Is.EqualTo(-1));
        }
コード例 #3
0
 private void AssignRandomAnomalies_Terrain()
 {
     foreach (HexPos hexPos in this.randomAnomalyPositions)
     {
         Terrain terrain = base.Context.GetTerrain(hexPos);
         WeightedRandomSelector <string> weightedRandomSelector = new WeightedRandomSelector <string>();
         weightedRandomSelector.Randomizer = base.Context.Randomizer;
         Dictionary <string, int> dictionary = new Dictionary <string, int>(base.Context.Settings.AnomalyWeightsPerTerrain[terrain.Name]);
         foreach (string key in base.Context.Settings.UniqueAnomaliesQuantities.Keys)
         {
             dictionary.Remove(key);
         }
         if (dictionary.Count != 0)
         {
             weightedRandomSelector.UseDictionary(dictionary);
             string randomSelected = weightedRandomSelector.RandomSelected;
             if (!base.Context.Settings.UniqueAnomaliesQuantities.ContainsKey(randomSelected))
             {
                 if (!base.Context.Anomalies.ContainsKey(randomSelected))
                 {
                     base.Context.Anomalies.Add(randomSelected, new List <HexPos>());
                 }
                 base.Context.Anomalies[randomSelected].Add(hexPos);
                 base.Context.AnomalyMap[hexPos.Row, hexPos.Column] = randomSelected;
             }
         }
     }
     this.randomAnomalyPositions.Clear();
 }
コード例 #4
0
        public void ProportionSetterClampedTo01Test([Values(0.0f, 0.1f, 0.5f, 0.9f, 1.0f, 2.0f)] float proportion)
        {
            var s  = new WeightedRandomSelector();
            var cP = proportion.Clamp01();

            s.Proportion = proportion;
            Assert.That(s.Proportion, Is.EqualTo(cP).Within(Tolerance));
        }
コード例 #5
0
        public void Should_Return_String(string expected)
        {
            _stringSelector = new WeightedRandomSelector <string>();

            _stringSelector.Add(expected, 1.0);

            Assert.Equal(expected, _stringSelector.Select());
        }
コード例 #6
0
        public void Should_Return_Integer(int expected)
        {
            _intSelector = new WeightedRandomSelector <int>();

            _intSelector.Add(expected, 1.0);

            Assert.Equal(expected, _intSelector.Select());
        }
コード例 #7
0
        private void AssignRandomAnomalies_Unique()
        {
            Dictionary <HexPos, int> weights = new Dictionary <HexPos, int>();

            this.randomAnomalyPositions.ForEach(delegate(HexPos h)
            {
                weights.Add(h, 0);
            });
            WeightedRandomSelector <HexPos> weightedRandomSelector = new WeightedRandomSelector <HexPos>
            {
                Randomizer = base.Context.Randomizer
            };

            foreach (string text in base.Context.Settings.UniqueAnomaliesQuantities.Keys)
            {
                int num = 0;
                if (base.Context.Anomalies.ContainsKey(text))
                {
                    num = base.Context.Anomalies[text].Count;
                }
                if (base.Context.Settings.UniqueAnomaliesQuantities[text] > num)
                {
                    foreach (HexPos hexPos in this.randomAnomalyPositions)
                    {
                        string name = base.Context.GetTerrain(hexPos).Name;
                        weights[hexPos] = 0;
                        if (base.Context.Settings.AnomalyWeightsPerTerrain.ContainsKey(name) && base.Context.Settings.AnomalyWeightsPerTerrain[name].ContainsKey(text))
                        {
                            weights[hexPos] = base.Context.Settings.AnomalyWeightsPerTerrain[name][text];
                        }
                    }
                    weightedRandomSelector.UseDictionary(weights);
                    int num2 = base.Context.Settings.UniqueAnomaliesQuantities[text] - num;
                    if (weightedRandomSelector.IsValid && num2 > 0)
                    {
                        for (int i = 0; i < num2; i++)
                        {
                            if (weightedRandomSelector.IsValid)
                            {
                                HexPos randomSelected = weightedRandomSelector.RandomSelected;
                                weights[randomSelected] = 0;
                                weightedRandomSelector.UseDictionary(weights);
                                if (!base.Context.Anomalies.ContainsKey(text))
                                {
                                    base.Context.Anomalies.Add(text, new List <HexPos>());
                                }
                                base.Context.Anomalies[text].Add(randomSelected);
                                base.Context.AnomalyMap[randomSelected.Row, randomSelected.Column] = text;
                                this.randomAnomalyPositions.Remove(randomSelected);
                            }
                        }
                    }
                }
            }
        }
コード例 #8
0
        public void Should_Return_OneOf_Integers(int expected1, int expected2, int expected3, int expected4)
        {
            _intSelector = new WeightedRandomSelector <int>();

            int[] testPool = { expected1, expected2, expected3, expected4 };

            _intSelector.Add(expected1, 0.5);
            _intSelector.Add(expected2, 0.5);
            _intSelector.Add(expected3, 0.5);
            _intSelector.Add(expected4, 0.5);

            Assert.Contains(_intSelector.Select(), testPool);
        }
コード例 #9
0
        public void ConstructorTest()
        {
            var s = new WeightedRandomSelector();

            Assert.IsNotNull(s);
        }
コード例 #10
0
        protected void GenerateUniqueAnomalies()
        {
            List <HexPos> list = new List <HexPos>(from d in base.Context.Districts.Values
                                                   where d.Content == District.Contents.Land
                                                   where !base.Context.SpawnRegions.Contains(d.MotherRegion.Id)
                                                   from h in d
                                                   where !base.Context.HasRiver[h.Row, h.Column]
                                                   where base.Context.POIValidityMap[h.Row, h.Column] == WorldGeneratorContext.POIValidity.Free
                                                   select h);

            if (list.Count < 1)
            {
                list = new List <HexPos>(from d in base.Context.Districts.Values
                                         where d.Content == District.Contents.Land
                                         from h in d
                                         where !base.Context.HasRiver[h.Row, h.Column]
                                         where base.Context.POIValidityMap[h.Row, h.Column] == WorldGeneratorContext.POIValidity.Free
                                         select h);
            }
            Dictionary <HexPos, int> weights = new Dictionary <HexPos, int>();

            list.ForEach(delegate(HexPos h)
            {
                weights.Add(h, 0);
            });
            WeightedRandomSelector <HexPos> weightedRandomSelector = new WeightedRandomSelector <HexPos>
            {
                Randomizer = base.Context.Randomizer
            };

            foreach (string text in base.Context.Settings.UniqueAnomaliesQuantities.Keys)
            {
                foreach (HexPos hexPos in list)
                {
                    string name = base.Context.GetTerrain(hexPos).Name;
                    weights[hexPos] = 0;
                    if (base.Context.Settings.AnomalyWeightsPerTerrain.ContainsKey(name) && base.Context.POIValidityMap[hexPos.Row, hexPos.Column] == WorldGeneratorContext.POIValidity.Free && base.Context.Settings.AnomalyWeightsPerTerrain[name].ContainsKey(text))
                    {
                        weights[hexPos] = base.Context.Settings.AnomalyWeightsPerTerrain[name][text];
                    }
                }
                weightedRandomSelector.UseDictionary(weights);
                int num = base.Context.Settings.UniqueAnomaliesQuantities[text];
                if (weightedRandomSelector.IsValid && num > 0)
                {
                    for (int i = 0; i < num; i++)
                    {
                        if (weightedRandomSelector.IsValid)
                        {
                            HexPos randomSelected = weightedRandomSelector.RandomSelected;
                            base.Context.POIValidityMap[randomSelected.Row, randomSelected.Column] = WorldGeneratorContext.POIValidity.Impossible;
                            weights[randomSelected] = 0;
                            weightedRandomSelector.UseDictionary(weights);
                            if (!base.Context.Anomalies.ContainsKey(text))
                            {
                                base.Context.Anomalies.Add(text, new List <HexPos>());
                            }
                            base.Context.Anomalies[text].Add(randomSelected);
                            base.Context.AnomalyMap[randomSelected.Row, randomSelected.Column] = text;
                        }
                    }
                }
            }
        }
コード例 #11
0
 public override void Execute(object context)
 {
     base.Execute(context);
     if (base.Context.Settings.GlobalAnomalyMultiplier <= 0)
     {
         return;
     }
     for (int i = 0; i < base.Context.Grid.Rows; i++)
     {
         for (int j = 0; j < base.Context.Grid.Columns; j++)
         {
             int     index   = (int)base.Context.TerrainData[i, j];
             Terrain terrain = base.Context.Settings.Terrains[index];
             HexPos  item    = new HexPos(j, i);
             int     num     = 0;
             if (base.Context.POIValidityMap[item.Row, item.Column] == WorldGeneratorContext.POIValidity.Free || base.Context.POIValidityMap[item.Row, item.Column] == WorldGeneratorContext.POIValidity.Excluded)
             {
                 item.Neighbours.ForEach(delegate(HexPos hex)
                 {
                     int num2 = hex.Row;
                     if (num2 < 0)
                     {
                         num2 = base.Context.AnomalyMap.GetLength(0) - 1;
                     }
                     else if (num2 >= base.Context.AnomalyMap.GetLength(0))
                     {
                         num2 -= num2;
                     }
                     int num3 = hex.Column;
                     if (num3 < 0)
                     {
                         num3 = base.Context.AnomalyMap.GetLength(1) - 1;
                     }
                     else if (num3 >= base.Context.AnomalyMap.GetLength(1))
                     {
                         num3 -= num3;
                     }
                     if (base.Context.AnomalyMap[num2, num3] != null)
                     {
                         this.XephiAnyNeighborAnomalies = true;
                     }
                 });
                 if (base.Context.Settings.AnomalyOdds.ContainsKey(terrain.Name))
                 {
                     num = base.Context.Settings.AnomalyOdds[terrain.Name];
                 }
                 num *= 10000;
                 num /= base.Context.Settings.GlobalAnomalyMultiplier;
                 bool flag = base.Context.Randomizer.Next(num) <= 100;
                 if ((num > 0 && base.Context.Settings.AnomalyWeightsPerTerrain.ContainsKey(terrain.Name) && flag) || (this.XephiConfirmNextHexAnomaly > 0 && base.Context.Settings.AnomalyWeightsPerTerrain.ContainsKey(terrain.Name)))
                 {
                     if (flag)
                     {
                         this.XephiConfirmNextHexAnomaly++;
                     }
                     if (this.XephiAnyNeighborAnomalies && base.Context.Settings.XephiWorldGeneratorBalance)
                     {
                         this.XephiAnyNeighborAnomalies = false;
                     }
                     else
                     {
                         WeightedRandomSelector <string> weightedRandomSelector = new WeightedRandomSelector <string>();
                         weightedRandomSelector.Randomizer = base.Context.Randomizer;
                         weightedRandomSelector.UseDictionary(base.Context.Settings.AnomalyWeightsPerTerrain[terrain.Name]);
                         string randomSelected = weightedRandomSelector.RandomSelected;
                         if (!base.Context.Settings.UniqueAnomaliesQuantities.ContainsKey(randomSelected) && (!base.Context.HasRiver[item.Row, item.Column] || !base.Context.Settings.NoRiverHexAnomalies.Contains(randomSelected)))
                         {
                             if (!base.Context.Anomalies.ContainsKey(randomSelected))
                             {
                                 base.Context.Anomalies.Add(randomSelected, new List <HexPos>());
                             }
                             base.Context.Anomalies[randomSelected].Add(item);
                             base.Silent = false;
                             base.Trace(string.Format("{0} at {1}", randomSelected, item.ToString()));
                             base.Silent = true;
                             base.Context.AnomalyMap[item.Row, item.Column]     = randomSelected;
                             base.Context.POIValidityMap[item.Row, item.Column] = WorldGeneratorContext.POIValidity.Impossible;
                             this.XephiConfirmNextHexAnomaly--;
                             this.XephiAnomalyCountTracker++;
                         }
                     }
                 }
             }
         }
     }
     string[,] array = new string[base.Context.Grid.Rows, base.Context.Grid.Columns];
     foreach (PointOfInterestDefinition pointOfInterestDefinition in base.Context.POIDefinitions)
     {
         array[pointOfInterestDefinition.Position.Row, pointOfInterestDefinition.Position.Column] = pointOfInterestDefinition.TemplateName;
         if (base.Context.AnomalyMap[pointOfInterestDefinition.Position.Row, pointOfInterestDefinition.Position.Column] != null)
         {
             base.Trace(string.Format("Dual setup POI/Anomaly at {0} : {1} & {2}", pointOfInterestDefinition.Position.ToString(), pointOfInterestDefinition.TemplateName, base.Context.AnomalyMap[pointOfInterestDefinition.Position.Row, pointOfInterestDefinition.Position.Column]));
         }
     }
 }