Unstable 1.1.14.0

This commit is contained in:
Markus Isberg
2023-10-02 16:43:54 +03:00
parent 94f5a93a0c
commit cf8f0de659
606 changed files with 21906 additions and 11456 deletions
@@ -101,27 +101,9 @@ namespace Barotrauma.Extensions
return source.Where(predicate).OrderBy(p => p.UintIdentifier).ToArray().GetRandom(randSync);
}
public static T RandomElementByWeight<T>(this IList<T> source, Func<T, float> weightSelector, Rand.RandSync randSync = Rand.RandSync.Unsynced)
public static T GetRandomByWeight<T>(this IEnumerable<T> source, Func<T, float> weightSelector, Rand.RandSync randSync)
{
float totalWeight = source.Sum(weightSelector);
float itemWeightIndex = Rand.Range(0f, 1f, randSync) * totalWeight;
float currentWeightIndex = 0;
for (int i = 0; i < source.Count; i++)
{
T weightedItem = source[i];
float weight = weightSelector(weightedItem);
currentWeightIndex += weight;
if (currentWeightIndex >= itemWeightIndex)
{
return weightedItem;
}
}
return default;
return ToolBox.SelectWeightedRandom(source, weightSelector, randSync);
}
/// <summary>