make objectives use yml defined mind filters (#36030)
* add MindHasRole whitelist overload * add mind filters framework * add different mind filters and pools * update traitor stuff to use mind filters * line * don't duplicate kill objectives * g * gs --------- Co-authored-by: deltanedas <@deltanedas:kde.org> Co-authored-by: ScarKy0 <106310278+ScarKy0@users.noreply.github.com> Co-authored-by: ScarKy0 <scarky0@onet.eu> Co-authored-by: SlamBamActionman <slambamactionman@gmail.com>
This commit is contained in:
@@ -9,6 +9,7 @@ using Content.Shared.Humanoid;
|
||||
using Content.Shared.Interaction.Events;
|
||||
using Content.Shared.Movement.Components;
|
||||
using Content.Shared.Mind.Components;
|
||||
using Content.Shared.Mind.Filters;
|
||||
using Content.Shared.Mobs.Components;
|
||||
using Content.Shared.Mobs.Systems;
|
||||
using Content.Shared.Objectives.Systems;
|
||||
@@ -19,6 +20,7 @@ using Content.Shared.Whitelist;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Network;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Shared.Mind;
|
||||
@@ -27,6 +29,7 @@ public abstract partial class SharedMindSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
|
||||
[Dependency] private readonly INetManager _net = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly MobStateSystem _mobState = default!;
|
||||
[Dependency] private readonly SharedObjectivesSystem _objectives = default!;
|
||||
[Dependency] private readonly SharedPlayerSystem _player = default!;
|
||||
@@ -37,6 +40,8 @@ public abstract partial class SharedMindSystem : EntitySystem
|
||||
[ViewVariables]
|
||||
protected readonly Dictionary<NetUserId, EntityUid> UserMinds = new();
|
||||
|
||||
private HashSet<Entity<MindComponent>> _pickingMinds = new();
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
@@ -618,23 +623,70 @@ public abstract partial class SharedMindSystem : EntitySystem
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of every living humanoid player's minds, except for a single one which is exluded.
|
||||
/// A new hashset is allocated for every call, consider using <see cref="AddAliveHumans"/> instead.
|
||||
/// </summary>
|
||||
public HashSet<Entity<MindComponent>> GetAliveHumans(EntityUid? exclude = null)
|
||||
{
|
||||
var allHumans = new HashSet<Entity<MindComponent>>();
|
||||
AddAliveHumans(allHumans, exclude);
|
||||
return allHumans;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds to a hashset every living humanoid player's minds, except for a single one which is exluded.
|
||||
/// </summary>
|
||||
public void AddAliveHumans(HashSet<Entity<MindComponent>> allHumans, EntityUid? exclude = null)
|
||||
{
|
||||
// HumanoidAppearanceComponent is used to prevent mice, pAIs, etc from being chosen
|
||||
var query = EntityQueryEnumerator<MobStateComponent, HumanoidAppearanceComponent>();
|
||||
while (query.MoveNext(out var uid, out var mobState, out _))
|
||||
var query = EntityQueryEnumerator<HumanoidAppearanceComponent, MobStateComponent>();
|
||||
while (query.MoveNext(out var uid, out _, out var mobState))
|
||||
{
|
||||
// the player needs to have a mind and not be the excluded one +
|
||||
// the player has to be alive
|
||||
if (!TryGetMind(uid, out var mind, out var mindComp) || mind == exclude || !_mobState.IsAlive(uid, mobState))
|
||||
continue;
|
||||
|
||||
allHumans.Add(new Entity<MindComponent>(mind, mindComp));
|
||||
allHumans.Add((mind, mindComp));
|
||||
}
|
||||
}
|
||||
|
||||
return allHumans;
|
||||
/// <summary>
|
||||
/// Picks a random mind from a pool after applying a list of filters.
|
||||
/// Returns null if no valid mind could be found.
|
||||
/// </summary>
|
||||
public Entity<MindComponent>? PickFromPool(IMindPool pool, List<MindFilter> filters, EntityUid? exclude = null)
|
||||
{
|
||||
_pickingMinds.Clear();
|
||||
pool.FindMinds(_pickingMinds, exclude, EntityManager, this);
|
||||
FilterMinds(_pickingMinds, filters, exclude);
|
||||
|
||||
if (_pickingMinds.Count == 0)
|
||||
return null;
|
||||
|
||||
return _random.Pick(_pickingMinds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Filters minds from a hashset using a single <see cref="MindFilter"/>.
|
||||
/// </summary>
|
||||
public void FilterMinds(HashSet<Entity<MindComponent>> minds, MindFilter filter, EntityUid? exclude = null)
|
||||
{
|
||||
minds.RemoveWhere(mind => filter.Filter(mind, exclude, EntityManager, this));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Filters minds from a hashset using a list of <see cref="MindFilter"/>s to apply sequentially.
|
||||
/// </summary>
|
||||
public void FilterMinds(HashSet<Entity<MindComponent>> minds, List<MindFilter> filters, EntityUid? exclude = null)
|
||||
{
|
||||
foreach (var filter in filters)
|
||||
{
|
||||
// no point calling it if there are none left
|
||||
if (minds.Count == 0)
|
||||
break;
|
||||
|
||||
FilterMinds(minds, filter, exclude);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user