Files
Delta-v/Content.Shared/EntityTable/EntitySelectors/GroupSelector.cs
ce7f99fbfb Entity Table Probabilities API (#41920)
* push

* fix

* push

* abstract and more accurate

* new abstract method I guess

* that table does not explode

* webedit major

* Update Content.Shared/EntityTable/ValueSelector/BinomialNumberSelector.cs

Co-authored-by: SnappingOpossum <snappingopossum@outlook.com>

* big ass merge conflicts

* sadas

* review

---------

Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
Co-authored-by: Tayrtahn <tayrtahn@gmail.com>
Co-authored-by: SnappingOpossum <snappingopossum@outlook.com>
2026-06-29 12:43:45 +02:00

66 lines
2.1 KiB
C#

using System.Linq;
using Content.Shared.Random.Helpers;
using Robust.Shared.Prototypes;
namespace Content.Shared.EntityTable.EntitySelectors;
/// <summary>
/// Gets the spawns from one of the child selectors, based on the weight of the children
/// </summary>
public sealed partial class GroupSelector : EntityTableSelector
{
[DataField(required: true)]
public List<EntityTableSelector> Children = new();
protected override IEnumerable<EntProtoId> GetSpawnsImplementation(System.Random rand,
IEntityManager entMan,
IPrototypeManager proto,
EntityTableContext ctx)
{
var children = new Dictionary<EntityTableSelector, float>(Children.Count);
foreach (var child in Children)
{
// Don't include invalid groups
if (!child.CheckConditions(entMan, proto, ctx))
continue;
children.Add(child, child.Weight);
}
if (children.Count == 0)
return Array.Empty<EntProtoId>();
var pick = SharedRandomExtensions.Pick(children, rand);
return pick.GetSpawns(rand, entMan, proto, ctx);
}
protected override IEnumerable<(EntProtoId spawn, double)> ListSpawnsImplementation(IEntityManager entMan, IPrototypeManager proto, EntityTableContext ctx)
{
var totalWeight = Children.Sum(x => x.Weight);
foreach (var child in Children)
{
var weightMod = child.Weight / totalWeight;
foreach (var (ent, prob) in child.ListSpawns(entMan, proto, ctx, weightMod))
{
yield return (ent, prob);
}
}
}
protected override IEnumerable<(EntProtoId spawn, double)> AverageSpawnsImplementation(IEntityManager entMan, IPrototypeManager proto, EntityTableContext ctx)
{
var totalWeight = Children.Sum(x => x.Weight);
foreach (var child in Children)
{
var weightMod = child.Weight / totalWeight;
foreach (var (ent, prob) in child.AverageSpawns(entMan, proto, ctx, weightMod))
{
yield return (ent, prob);
}
}
}
}