using Content.Shared.EntityTable.EntitySelectors;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Utility;
namespace Content.Shared.EntityTable;
public sealed partial class EntityTableSystem : EntitySystem
{
[Dependency] private IRobustRandom _random = default!;
///
/// Iterates once through Entities that EntitySelector in EntityTableProto provides and picks first one.
///
public EntProtoId? GetFirstOrNull(
EntityTablePrototype entTableProto,
IRobustRandom? rand = null,
EntityTableContext? ctx = null
)
{
return GetSpawns(entTableProto, rand, ctx).FirstOrNull();
}
///
/// Iterates once through Entities that EntitySelector provides and picks first one.
///
public EntProtoId? GetFirstOrNull(
EntityTableSelector? entTableProto,
IRobustRandom? rand = null,
EntityTableContext? ctx = null
)
{
return GetSpawns(entTableProto, rand, ctx).FirstOrNull();
}
///
/// Compiles a random list of entity prototypes using constraints.
///
public IEnumerable GetSpawns(EntityTablePrototype entTableProto, IRobustRandom? rand = null, EntityTableContext? ctx = null)
{
// convenient
return GetSpawns(entTableProto.Table, rand, ctx);
}
///
/// Compiles a random list of entity prototypes using constraints.
///
public IEnumerable GetSpawns(EntityTableSelector? table, IRobustRandom? rand = null, EntityTableContext? ctx = null)
{
if (table == null)
return new List();
rand ??= _random;
ctx ??= new EntityTableContext();
return table.GetSpawns(rand, EntityManager, ProtoMan, ctx);
}
///
/// Builds a list of all the spawns in an EntityTable as keys, and their modified weights as values.
///
public IEnumerable<(EntProtoId spawn, double)> ListSpawns(EntityTablePrototype entTableProto, EntityTableContext? ctx = null)
{
return ListSpawns(entTableProto.Table, ctx);
}
///
/// Builds a list of all the spawns in an EntityTable as keys, and their modified weights as values.
///
/// Table we're examining
/// Optional extra context
public IEnumerable<(EntProtoId spawn, double)> ListSpawns(EntityTableSelector? table, EntityTableContext? ctx = null)
{
if (table == null)
return new List<(EntProtoId spawn, double)>();
ctx ??= new EntityTableContext();
return table.ListSpawns(EntityManager, ProtoMan, ctx);
}
///
public IEnumerable<(EntProtoId spawn, double)> AverageSpawns(EntityTablePrototype entTableProto, EntityTableContext? ctx = null)
{
return AverageSpawns(entTableProto.Table, ctx);
}
///
/// Returns the average expected spawns of a specific entity table.
///
/// The entity table we want the spawns of
/// Optional EntityTableContext
///
public IEnumerable<(EntProtoId spawn, double)> AverageSpawns(EntityTableSelector? table, EntityTableContext? ctx = null)
{
if (table == null)
return new List<(EntProtoId spawn, double)>();
ctx ??= new EntityTableContext();
return table.AverageSpawns(EntityManager, ProtoMan, ctx);
}
}