Add condition support to entity tables (#36819)

This commit is contained in:
Nemanja
2025-05-02 04:37:14 -04:00
committed by GitHub
parent 25108234ea
commit 26ebf06b81
5 changed files with 109 additions and 0 deletions

View File

@@ -1,3 +1,4 @@
using Content.Shared.EntityTable.Conditions;
using Content.Shared.EntityTable.ValueSelector;
using JetBrains.Annotations;
using Robust.Shared.Prototypes;
@@ -26,10 +27,26 @@ public abstract partial class EntityTableSelector
[DataField]
public double Prob = 1;
/// <summary>
/// A list of conditions that must evaluate to 'true' for the selector to apply.
/// </summary>
[DataField]
public List<EntityTableCondition> Conditions = new();
/// <summary>
/// If true, all the conditions must be successful in order for the selector to process.
/// Otherwise, only one of them must be.
/// </summary>
[DataField]
public bool RequireAll = true;
public IEnumerable<EntProtoId> GetSpawns(System.Random rand,
IEntityManager entMan,
IPrototypeManager proto)
{
if (!CheckConditions(entMan, proto))
yield break;
var rolls = Rolls.Get(rand);
for (var i = 0; i < rolls; i++)
{
@@ -43,6 +60,28 @@ public abstract partial class EntityTableSelector
}
}
public bool CheckConditions(IEntityManager entMan, IPrototypeManager proto)
{
if (Conditions.Count == 0)
return true;
var success = false;
foreach (var condition in Conditions)
{
var res = condition.Evaluate(entMan, proto);
if (RequireAll && !res)
return false; // intentional break out of loop and function
success |= res;
}
if (RequireAll)
return true;
return success;
}
protected abstract IEnumerable<EntProtoId> GetSpawnsImplementation(System.Random rand,
IEntityManager entMan,
IPrototypeManager proto);