using System.Diagnostics.CodeAnalysis;
using Content.Shared.EntityTable.EntitySelectors;
using JetBrains.Annotations;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Shared.EntityTable;
public sealed partial class EntityTableSystem : EntitySystem
{
[Dependency] private IRobustRandom _random = default!;
///
/// 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);
}
}
///
/// Context used by selectors and conditions to evaluate in generic gamestate information.
///
public sealed class EntityTableContext
{
private readonly Dictionary _data = new();
public EntityTableContext()
{
}
public EntityTableContext(Dictionary data)
{
_data = data;
}
///
/// Retrieves an arbitrary piece of data from the context based on a provided key.
///
/// A string key that corresponds to the value we are searching for.
/// The value we are trying to extract from the context object
/// The type of that we are trying to retrieve
/// If has a corresponding value of type
[PublicAPI]
public bool TryGetData([ForbidLiteral] string key, [NotNullWhen(true)] out T? value)
{
value = default;
if (!_data.TryGetValue(key, out var valueData) || valueData is not T castValueData)
return false;
value = castValueData;
return true;
}
///
/// Sets data into context using provided key.
///
[PublicAPI]
public void SetData([ForbidLiteral] string key, T data) where T : notnull
{
_data[key] = data;
}
///
/// Removes data from the context, if the key exists.
///
[PublicAPI]
public void RemoveData([ForbidLiteral] string key)
{
_data.Remove(key);
}
}