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:
12
Content.Shared/Mind/Filters/AliveHumansPool.cs
Normal file
12
Content.Shared/Mind/Filters/AliveHumansPool.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace Content.Shared.Mind.Filters;
|
||||
|
||||
/// <summary>
|
||||
/// A mind pool that uses <see cref="SharedMindSystem.AddAliveHumans"/>.
|
||||
/// </summary>
|
||||
public sealed partial class AliveHumansPool : IMindPool
|
||||
{
|
||||
void IMindPool.FindMinds(HashSet<Entity<MindComponent>> minds, EntityUid? exclude, IEntityManager entMan, SharedMindSystem mindSys)
|
||||
{
|
||||
mindSys.AddAliveHumans(minds, exclude);
|
||||
}
|
||||
}
|
||||
15
Content.Shared/Mind/Filters/AntagonistMindFilter.cs
Normal file
15
Content.Shared/Mind/Filters/AntagonistMindFilter.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Content.Shared.Roles;
|
||||
|
||||
namespace Content.Shared.Mind.Filters;
|
||||
|
||||
/// <summary>
|
||||
/// A mind filter that requires minds to have an antagonist role.
|
||||
/// </summary>
|
||||
public sealed partial class AntagonistMindFilter : MindFilter
|
||||
{
|
||||
protected override bool ShouldRemove(Entity<MindComponent> mind, EntityUid? exclude, IEntityManager entMan, SharedMindSystem mindSys)
|
||||
{
|
||||
var roleSys = entMan.System<SharedRoleSystem>();
|
||||
return !roleSys.MindIsAntagonist(mind);
|
||||
}
|
||||
}
|
||||
21
Content.Shared/Mind/Filters/BodyMindFilter.cs
Normal file
21
Content.Shared/Mind/Filters/BodyMindFilter.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using Content.Shared.Whitelist;
|
||||
|
||||
namespace Content.Shared.Mind.Filters;
|
||||
|
||||
/// <summary>
|
||||
/// A mind filter that checks the mind's owned entity against a whitelist.
|
||||
/// </summary>
|
||||
public sealed partial class BodyMindFilter : MindFilter
|
||||
{
|
||||
[DataField(required: true)]
|
||||
public EntityWhitelist Whitelist = new();
|
||||
|
||||
protected override bool ShouldRemove(Entity<MindComponent> ent, EntityUid? exclude, IEntityManager entMan, SharedMindSystem mindSys)
|
||||
{
|
||||
if (ent.Comp.OwnedEntity is not {} mob)
|
||||
return true;
|
||||
|
||||
var sys = entMan.System<EntityWhitelistSystem>();
|
||||
return sys.IsWhitelistFail(Whitelist, mob);
|
||||
}
|
||||
}
|
||||
22
Content.Shared/Mind/Filters/HasRoleMindFilter.cs
Normal file
22
Content.Shared/Mind/Filters/HasRoleMindFilter.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using Content.Shared.Roles;
|
||||
using Content.Shared.Whitelist;
|
||||
|
||||
namespace Content.Shared.Mind.Filters;
|
||||
|
||||
/// <summary>
|
||||
/// A mind filter that requires minds to have a role matching a whitelist.
|
||||
/// </summary>
|
||||
public sealed partial class HasRoleMindFilter : MindFilter
|
||||
{
|
||||
/// <summary>
|
||||
/// The whitelist a role must match for the mind to pass the filter.
|
||||
/// </summary>
|
||||
[DataField(required: true)]
|
||||
public EntityWhitelist Whitelist;
|
||||
|
||||
protected override bool ShouldRemove(Entity<MindComponent> mind, EntityUid? exclude, IEntityManager entMan, SharedMindSystem mindSys)
|
||||
{
|
||||
var roleSys = entMan.System<SharedRoleSystem>();
|
||||
return roleSys.MindHasRole(mind, Whitelist);
|
||||
}
|
||||
}
|
||||
19
Content.Shared/Mind/Filters/IMindPool.cs
Normal file
19
Content.Shared/Mind/Filters/IMindPool.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Shared.Mind.Filters;
|
||||
|
||||
/// <summary>
|
||||
/// A mind pool that can find minds to use for objectives etc.
|
||||
/// Further filtered by <see cref="IMindFilter"/>.
|
||||
/// </summary>
|
||||
[ImplicitDataDefinitionForInheritors]
|
||||
public partial interface IMindPool
|
||||
{
|
||||
/// <summary>
|
||||
/// Add minds for this pool to a hashset.
|
||||
/// The hashset gets reused and is cleared before this is called.
|
||||
/// </summary>
|
||||
/// <param name="minds">The hashset to add to</param>
|
||||
/// <param name="exclude">A mind entity that must not be returned</param>
|
||||
void FindMinds(HashSet<Entity<MindComponent>> minds, EntityUid? exclude, IEntityManager entMan, SharedMindSystem mindSys);
|
||||
}
|
||||
21
Content.Shared/Mind/Filters/JobMindFilter.cs
Normal file
21
Content.Shared/Mind/Filters/JobMindFilter.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using Content.Shared.Roles;
|
||||
using Content.Shared.Roles.Jobs;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Shared.Mind.Filters;
|
||||
|
||||
/// <summary>
|
||||
/// A mind filter that requires minds to have a specific job.
|
||||
/// This uses mind roles, not ID cards.
|
||||
/// </summary>
|
||||
public sealed partial class JobMindFilter : MindFilter
|
||||
{
|
||||
[DataField(required: true)]
|
||||
public ProtoId<JobPrototype> Job;
|
||||
|
||||
protected override bool ShouldRemove(Entity<MindComponent> mind, EntityUid? exclude, IEntityManager entMan, SharedMindSystem mindSys)
|
||||
{
|
||||
var jobSys = entMan.System<SharedJobSystem>();
|
||||
return jobSys.MindHasJobWithId(mind, Job);
|
||||
}
|
||||
}
|
||||
32
Content.Shared/Mind/Filters/MindFilter.cs
Normal file
32
Content.Shared/Mind/Filters/MindFilter.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Shared.Mind.Filters;
|
||||
|
||||
/// <summary>
|
||||
/// A mind filter that can be used to filter out minds from a <see cref="IMindPool"/>.
|
||||
/// </summary>
|
||||
[ImplicitDataDefinitionForInheritors]
|
||||
public abstract partial class MindFilter
|
||||
{
|
||||
/// <summary>
|
||||
/// The actual filter function, this has to return false for minds that get removed from the pool.
|
||||
/// An excluded mind will be the same one passed to <see cref="IMindPool.FindMinds"/>.
|
||||
/// </summary>
|
||||
/// <param name="mind">The mind to check</param>
|
||||
/// <param name="exclude">The same mind passed to FindMinds</param>
|
||||
protected abstract bool ShouldRemove(Entity<MindComponent> mind, EntityUid? exclude, IEntityManager entMan, SharedMindSystem mindSys);
|
||||
|
||||
/// <summary>
|
||||
/// The high-level filter function to be used by the mind system.
|
||||
/// </summary>
|
||||
public bool Filter(Entity<MindComponent> mind, EntityUid? exclude, EntityManager entMan, SharedMindSystem mindSys)
|
||||
{
|
||||
return ShouldRemove(mind, exclude, entMan, mindSys) ^ Inverted;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether to invert functionality, only keeping minds that would otherwise be removed.
|
||||
/// </summary>
|
||||
[DataField]
|
||||
public bool Inverted;
|
||||
}
|
||||
25
Content.Shared/Mind/Filters/ObjectiveMindFilter.cs
Normal file
25
Content.Shared/Mind/Filters/ObjectiveMindFilter.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using Content.Shared.Whitelist;
|
||||
|
||||
namespace Content.Shared.Mind.Filters;
|
||||
|
||||
/// <summary>
|
||||
/// A mind filter that removes minds with a blacklist objective.
|
||||
/// </summary>
|
||||
public sealed partial class ObjectiveMindFilter : MindFilter
|
||||
{
|
||||
[DataField(required: true)]
|
||||
public EntityWhitelist Blacklist = new();
|
||||
|
||||
protected override bool ShouldRemove(Entity<MindComponent> mind, EntityUid? exclude, IEntityManager entMan, SharedMindSystem mindSys)
|
||||
{
|
||||
var whitelistSys = entMan.System<EntityWhitelistSystem>();
|
||||
foreach (var obj in mind.Comp.Objectives)
|
||||
{
|
||||
// mind has a blacklisted objective, remove it from the pool
|
||||
if (whitelistSys.IsBlacklistPass(Blacklist, obj))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
@@ -6,6 +6,7 @@ using Content.Shared.Database;
|
||||
using Content.Shared.GameTicking;
|
||||
using Content.Shared.Mind;
|
||||
using Content.Shared.Roles.Jobs;
|
||||
using Content.Shared.Whitelist;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Audio.Systems;
|
||||
using Robust.Shared.Configuration;
|
||||
@@ -19,13 +20,14 @@ namespace Content.Shared.Roles;
|
||||
|
||||
public abstract class SharedRoleSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
||||
[Dependency] private readonly IEntityManager _entityManager = default!;
|
||||
[Dependency] private readonly IPrototypeManager _prototypes = default!;
|
||||
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
|
||||
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
|
||||
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
||||
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
||||
[Dependency] protected readonly ISharedPlayerManager Player = default!;
|
||||
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
||||
[Dependency] private readonly SharedMindSystem _minds = default!;
|
||||
[Dependency] private readonly IEntityManager _entityManager = default!;
|
||||
[Dependency] private readonly EntityWhitelistSystem _whitelist = default!;
|
||||
[Dependency] private readonly SharedMindSystem _minds = default!;
|
||||
[Dependency] private readonly IPrototypeManager _prototypes = default!;
|
||||
|
||||
private JobRequirementOverridePrototype? _requirementOverride;
|
||||
|
||||
@@ -504,6 +506,20 @@ public abstract class SharedRoleSystem : EntitySystem
|
||||
return found;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if a mind has a role that matches a whitelist.
|
||||
/// </summary>
|
||||
public bool MindHasRole(Entity<MindComponent> mind, EntityWhitelist whitelist)
|
||||
{
|
||||
foreach (var roleEnt in mind.Comp.MindRoles)
|
||||
{
|
||||
if (_whitelist.IsWhitelistPass(whitelist, roleEnt))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds the first mind role of a specific type on a mind entity.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user