Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make kill objectives only target players with jobs #33135

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Content.Server/Objectives/Components/PickRandomPersonComponent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Content.Server.Objectives.Systems;
using Content.Shared.Whitelist;

namespace Content.Server.Objectives.Components;

Expand All @@ -8,4 +9,21 @@ namespace Content.Server.Objectives.Components;
[RegisterComponent, Access(typeof(KillPersonConditionSystem))]
public sealed partial class PickRandomPersonComponent : Component
{
/// <summary>
/// If non-null, a target must have a role matching this whitelist to be chosen.
/// </summary>
[DataField]
public EntityWhitelist? RoleWhitelist;

/// <summary>
/// If non-null, a target cannot have a role matching this blacklist to be chosen.
/// </summary>
[DataField]
public EntityWhitelist? RoleBlacklist;

/// <summary>
/// If non-null a target must have a job with SetPreference set to true.
/// </summary>
[DataField]
public bool OnlyChoosableJobs;
}
21 changes: 20 additions & 1 deletion Content.Server/Objectives/Systems/KillPersonConditionSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
using Content.Shared.CCVar;
using Content.Shared.Mind;
using Content.Shared.Objectives.Components;
using Content.Shared.Roles;
using Content.Shared.Roles.Jobs;
using Robust.Shared.Configuration;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;

namespace Content.Server.Objectives.Systems;
Expand All @@ -16,8 +19,10 @@ public sealed class KillPersonConditionSystem : EntitySystem
{
[Dependency] private readonly EmergencyShuttleSystem _emergencyShuttle = default!;
[Dependency] private readonly IConfigurationManager _config = default!;
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly SharedMindSystem _mind = default!;
[Dependency] private readonly SharedRoleSystem _role = default!;
[Dependency] private readonly TargetObjectiveSystem _target = default!;

public override void Initialize()
Expand Down Expand Up @@ -52,8 +57,22 @@ private void OnPersonAssigned(EntityUid uid, PickRandomPersonComponent comp, ref
if (target.Target != null)
return;

// no other humans to kill
// TODO: make a reusable filter system to avoid duplication with PickRandomHead or any future objectives
var allHumans = _mind.GetAliveHumans(args.MindId);
if (comp.RoleWhitelist is {} whitelist)
allHumans.RemoveWhere(mindId => !_role.MindHasMatchingRole((mindId.Owner, mindId.Comp), whitelist));
if (comp.RoleBlacklist is {} blacklist)
allHumans.RemoveWhere(mindId => _role.MindHasMatchingRole((mindId.Owner, mindId.Comp), blacklist));

if (comp.OnlyChoosableJobs)
{
allHumans.RemoveWhere(mindId => !(
_role.MindHasRole<JobRoleComponent>((mindId.Owner, mindId.Comp), out var role) &&
role?.Comp1.JobPrototype is {} jobId &&
_proto.Index(jobId).SetPreference));
}

// no other humans to kill
if (allHumans.Count == 0)
{
args.Cancelled = true;
Expand Down
19 changes: 19 additions & 0 deletions Content.Shared/Roles/SharedRoleSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Content.Shared.Database;
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;
Expand All @@ -19,6 +20,7 @@ public abstract class SharedRoleSystem : EntitySystem
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly EntityWhitelistSystem _whitelist = default!;
[Dependency] private readonly IPrototypeManager _prototypes = default!;

private JobRequirementOverridePrototype? _requirementOverride;
Expand Down Expand Up @@ -297,6 +299,23 @@ public bool MindHasRole<T>(Entity<MindComponent?> mind,
return false;
}

/// <summary>
/// Returns true if the mind has any role that matches a whitelist.
/// </summary>
public bool MindHasMatchingRole(Entity<MindComponent?> mind, EntityWhitelist whitelist)
{
if (!Resolve(mind.Owner, ref mind.Comp))
return false;

foreach (var role in mind.Comp.MindRoles)
{
if (_whitelist.IsValid(whitelist, role))
return true;
}

return false;
}

/// <summary>
/// Finds the first mind role of a specific type on a mind entity.
/// Outputs an entity component for the mind role's MindRoleComponent
Expand Down
1 change: 1 addition & 0 deletions Resources/Prototypes/Objectives/traitor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
- type: TargetObjective
title: objective-condition-kill-person-title
- type: PickRandomPerson
onlyChoosableJobs: true

- type: entity
parent: [BaseTraitorObjective, BaseKillObjective]
Expand Down
Loading