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>
This commit is contained in:
Nemanja
2025-05-31 09:40:25 -04:00
committed by GitHub
parent ab776f2493
commit e92b6b6a7e
10 changed files with 71 additions and 24 deletions

View File

@@ -12,11 +12,12 @@ public sealed partial class AllSelector : EntityTableSelector
protected override IEnumerable<EntProtoId> GetSpawnsImplementation(System.Random rand,
IEntityManager entMan,
IPrototypeManager proto)
IPrototypeManager proto,
EntityTableContext ctx)
{
foreach (var child in Children)
{
foreach (var spawn in child.GetSpawns(rand, entMan, proto))
foreach (var spawn in child.GetSpawns(rand, entMan, proto, ctx))
{
yield return spawn;
}

View File

@@ -18,7 +18,8 @@ public sealed partial class EntSelector : EntityTableSelector
protected override IEnumerable<EntProtoId> GetSpawnsImplementation(System.Random rand,
IEntityManager entMan,
IPrototypeManager proto)
IPrototypeManager proto,
EntityTableContext ctx)
{
var num = Amount.Get(rand);
for (var i = 0; i < num; i++)

View File

@@ -42,9 +42,10 @@ public abstract partial class EntityTableSelector
public IEnumerable<EntProtoId> GetSpawns(System.Random rand,
IEntityManager entMan,
IPrototypeManager proto)
IPrototypeManager proto,
EntityTableContext ctx)
{
if (!CheckConditions(entMan, proto))
if (!CheckConditions(entMan, proto, ctx))
yield break;
var rolls = Rolls.Get(rand);
@@ -53,14 +54,14 @@ public abstract partial class EntityTableSelector
if (!rand.Prob(Prob))
continue;
foreach (var spawn in GetSpawnsImplementation(rand, entMan, proto))
foreach (var spawn in GetSpawnsImplementation(rand, entMan, proto, ctx))
{
yield return spawn;
}
}
}
public bool CheckConditions(IEntityManager entMan, IPrototypeManager proto)
public bool CheckConditions(IEntityManager entMan, IPrototypeManager proto, EntityTableContext ctx)
{
if (Conditions.Count == 0)
return true;
@@ -68,7 +69,7 @@ public abstract partial class EntityTableSelector
var success = false;
foreach (var condition in Conditions)
{
var res = condition.Evaluate(entMan, proto);
var res = condition.Evaluate(this, entMan, proto, ctx);
if (RequireAll && !res)
return false; // intentional break out of loop and function
@@ -84,5 +85,6 @@ public abstract partial class EntityTableSelector
protected abstract IEnumerable<EntProtoId> GetSpawnsImplementation(System.Random rand,
IEntityManager entMan,
IPrototypeManager proto);
IPrototypeManager proto,
EntityTableContext ctx);
}

View File

@@ -13,13 +13,14 @@ public sealed partial class GroupSelector : EntityTableSelector
protected override IEnumerable<EntProtoId> GetSpawnsImplementation(System.Random rand,
IEntityManager entMan,
IPrototypeManager proto)
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))
if (!child.CheckConditions(entMan, proto, ctx))
continue;
children.Add(child, child.Weight);
@@ -27,6 +28,6 @@ public sealed partial class GroupSelector : EntityTableSelector
var pick = SharedRandomExtensions.Pick(children, rand);
return pick.GetSpawns(rand, entMan, proto);
return pick.GetSpawns(rand, entMan, proto, ctx);
}
}

View File

@@ -13,8 +13,9 @@ public sealed partial class NestedSelector : EntityTableSelector
protected override IEnumerable<EntProtoId> GetSpawnsImplementation(System.Random rand,
IEntityManager entMan,
IPrototypeManager proto)
IPrototypeManager proto,
EntityTableContext ctx)
{
return proto.Index(TableId).Table.GetSpawns(rand, entMan, proto);
return proto.Index(TableId).Table.GetSpawns(rand, entMan, proto, ctx);
}
}

View File

@@ -9,7 +9,8 @@ public sealed partial class NoneSelector : EntityTableSelector
{
protected override IEnumerable<EntProtoId> GetSpawnsImplementation(System.Random rand,
IEntityManager entMan,
IPrototypeManager proto)
IPrototypeManager proto,
EntityTableContext ctx)
{
yield break;
}