CatStation/Content.Shared/EntityTable/EntitySelectors/GroupSelector.cs
Ewanderer d7f7a91183
Documentation of e api (#44395)
* documentation pass on content.server -> E!

* APi pass on content.server for the E folder.

* documentation pass on content.shared for e.

* Apply suggestions from code review

Co-authored-by: SlamBamActionman <83650252+SlamBamActionman@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: SlamBamActionman <83650252+SlamBamActionman@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: SlamBamActionman <83650252+SlamBamActionman@users.noreply.github.com>

* fixing documentation according to review part 1.

* typo

* Very minor fixes

* And a short explanation about EUI

---------

Co-authored-by: SlamBamActionman <83650252+SlamBamActionman@users.noreply.github.com>
Co-authored-by: SlamBamActionman <slambamactionman@gmail.com>
2026-07-01 08:38:04 +00:00

69 lines
2.2 KiB
C#

using System.Linq;
using Content.Shared.Random.Helpers;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
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
{
/// <summary>
/// The child entries of this selector.
/// </summary>
[DataField(required: true)]
public List<EntityTableSelector> Children = new();
protected override IEnumerable<EntProtoId> GetSpawnsImplementation(IRobustRandom 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);
}
}
}
}