Files
wwdpublic/Content.Shared/EntityTable/EntityTableSystem.cs
Kai5 e097ac1750 Dynamic real (#825)
* Add support for contextual information in EntityTables (#37737)

* Add context support for entityTables

* fix build fail

* comments

* Update Content.Shared/EntityTable/EntityTableSystem.cs

---------

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>

(cherry picked from commit e92b6b6a7e2ed2e5f7473321e391394f28b9ee4a)

* Multiantag Gamemode (#37783)

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
Co-authored-by: Southbridge <7013162+southbridge-fur@users.noreply.github.com>
Co-authored-by: ScarKy0 <106310278+ScarKy0@users.noreply.github.com>

(cherry picked from commit f23e8c286153b5742de258a4e722803a1a4fba78)

* Гейммоды

* Ребаланс цен

* Фикс линтера

* Баланс бюджета + фикс повторения

---------

Co-authored-by: Nemanja <98561806+emogarbage404@users.noreply.github.com>
2025-09-03 08:49:15 +03:00

66 lines
2.2 KiB
C#

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 class EntityTableSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
public IEnumerable<EntProtoId> GetSpawns(EntityTablePrototype entTableProto, System.Random? rand = null, EntityTableContext? ctx = null)
{
// convenient
return GetSpawns(entTableProto.Table, rand, ctx);
}
public IEnumerable<EntProtoId> GetSpawns(EntityTableSelector? table, System.Random? rand = null, EntityTableContext? ctx = null)
{
if (table == null)
return new List<EntProtoId>();
rand ??= _random.GetRandom();
ctx ??= new EntityTableContext();
return table.GetSpawns(rand, EntityManager, _prototypeManager, ctx);
}
}
/// <summary>
/// Context used by selectors and conditions to evaluate in generic gamestate information.
/// </summary>
public sealed class EntityTableContext
{
private readonly Dictionary<string, object> _data = new();
public EntityTableContext()
{
}
public EntityTableContext(Dictionary<string, object> data)
{
_data = data;
}
/// <summary>
/// Retrieves an arbitrary piece of data from the context based on a provided key.
/// </summary>
/// <param name="key">A string key that corresponds to the value we are searching for. </param>
/// <param name="value">The value we are trying to extract from the context object</param>
/// <typeparam name="T">The type of <see cref="value"/> that we are trying to retrieve</typeparam>
/// <returns>If <see cref="key"/> has a corresponding value of type <see cref="T"/></returns>
[PublicAPI]
public bool TryGetData<T>([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;
}
}