Example #1
0
			/// <summary>
			/// Evaluates all possible neighbor cells. The return values of <paramref name="f"/> are used as probability multipliers 
			/// to chose a random a option.
			/// Currently not thread-safe
			/// </summary>
			/// <param name="location">Location to evaluate the neighborhood of</param>
			/// <param name="f">Evaluation function. Must return 0/NotAChoice if not applicable, and >1 otherwise. Higher values indicate higher probability</param>
			/// <returns>Chocen relative cell, or Grid.RelativeCell.Invalid if none was found.</returns>
			public static Grid.RelativeCell EvaluateChoices(Grid.CellID location, Func<Grid.RelativeCell, Grid.CellID, int> f)
			{
				options.Clear();
				int total = 0;
				foreach (var n in location.GetRelativeNeighbors())
				{
					Grid.CellID cellLocation = location + n;
					int q = f(n, cellLocation);
					if (q > 0)
					{
						total += q;
						options.Add(new KeyValuePair<int, Grid.RelativeCell>(q, n));
					}
				}
				if (total == 0)
					return Grid.RelativeCell.Invalid;
				if (options.Count == 1)
					return options[0].Value;
				int c = random.Next(total);
				foreach (var o in options)
				{
					if (c <= o.Key)
						return o.Value;
					c -= o.Key;
				}
				Out.Log(Significance.ProgramFatal, "Logic error");
				return Grid.RelativeCell.Invalid;
			}
Example #2
0
 public static Grid.RelativeCell EvaluateYieldChoices(Grid.CellID location, Func<Grid.RelativeCell, Grid.CellID, int> f)
 {
     options.Clear();
     foreach (var n in location.GetRelativeNeighbors())
     {
         Grid.CellID cellLocation = location + n;
         int q = f(n, cellLocation);
         if (q > 0)
         {
             options.Add(new KeyValuePair<int, Grid.RelativeCell>(q, n));
         }
     }
     if (options.Count == 0)
     {
         //Out.Log(Significance.ProgramFatal, "Logic error");
         return Grid.RelativeCell.Invalid;
     }
     if (options.Count == 1)
         return options[0].Value;
     int maxYield = 0;
     Grid.RelativeCell maxOption = Grid.RelativeCell.Invalid;
     foreach (var o in options)
     {
        if(maxYield <= o.Key)
        {
            maxYield = o.Key;
            maxOption = o.Value;
        }
     }
     return maxOption;
     //else
     //{
     //    Out.Log(Significance.ProgramFatal, "Logic error");
     //    return Grid.RelativeCell.Invalid;
     //}
 }