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

Cherry Pick "Fix Latejoin Antag Preferences Not Being Respected" #1038

Merged
merged 2 commits into from
Oct 13, 2024
Merged
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
31 changes: 31 additions & 0 deletions Content.Server/Antag/AntagSelectionSystem.API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Content.Server.Objectives;
using Content.Shared.Chat;
using Content.Shared.Mind;
using Content.Shared.Preferences;
using JetBrains.Annotations;
using Robust.Shared.Audio;
using Robust.Shared.Enums;
Expand Down Expand Up @@ -156,6 +157,36 @@ public List<EntityUid> GetAntagMindEntityUids(Entity<AntagSelectionComponent?> e
return ent.Comp.SelectedMinds.Select(p => p.Item1).ToList();
}

/// <summary>
/// Checks if a given session has the primary antag preferences for a given definition
/// </summary>
public bool HasPrimaryAntagPreference(ICommonSession? session, AntagSelectionDefinition def)
{
if (session == null)
return true;

if (def.PrefRoles.Count == 0)
return false;

var pref = (HumanoidCharacterProfile) _pref.GetPreferences(session.UserId).SelectedCharacter;
return pref.AntagPreferences.Any(p => def.PrefRoles.Contains(p));
}

/// <summary>
/// Checks if a given session has the fallback antag preferences for a given definition
/// </summary>
public bool HasFallbackAntagPreference(ICommonSession? session, AntagSelectionDefinition def)
{
if (session == null)
return true;

if (def.FallbackRoles.Count == 0)
return false;

var pref = (HumanoidCharacterProfile) _pref.GetPreferences(session.UserId).SelectedCharacter;
return pref.AntagPreferences.Any(p => def.FallbackRoles.Contains(p));
}

/// <summary>
/// Returns all the antagonists for this rule who are currently alive
/// </summary>
Expand Down
34 changes: 17 additions & 17 deletions Content.Server/Antag/AntagSelectionSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using Content.Shared.Ghost;
using Content.Shared.Humanoid;
using Content.Shared.Players;
using Content.Shared.Preferences;
using Robust.Server.Audio;
using Robust.Server.GameObjects;
using Robust.Server.Player;
Expand Down Expand Up @@ -118,12 +117,15 @@ private void OnSpawnComplete(PlayerSpawnCompleteEvent args)
// something to figure out later.

var query = QueryActiveRules();
var rules = new List<(EntityUid, AntagSelectionComponent)>();
while (query.MoveNext(out var uid, out _, out var antag, out _))
{
// TODO ANTAG
// what why aasdiuhasdopiuasdfhksad
// stop this insanity please
// probability of antag assignment shouldn't depend on the order in which rules are returned by the query.
rules.Add((uid, antag));
}
RobustRandom.Shuffle(rules);

foreach (var (uid, antag) in rules)
{
if (!RobustRandom.Prob(LateJoinRandomChance))
continue;

Expand Down Expand Up @@ -221,13 +223,13 @@ public void ChooseAntags(Entity<AntagSelectionComponent> ent, IList<ICommonSessi
/// <summary>
/// Tries to makes a given player into the specified antagonist.
/// </summary>
public bool TryMakeAntag(Entity<AntagSelectionComponent> ent, ICommonSession? session, AntagSelectionDefinition def, bool ignoreSpawner = false)
public bool TryMakeAntag(Entity<AntagSelectionComponent> ent, ICommonSession? session, AntagSelectionDefinition def, bool ignoreSpawner = false, bool checkPref = true)
{
if (!IsSessionValid(ent, session, def) ||
!IsEntityValid(session?.AttachedEntity, def))
{
if (checkPref && !HasPrimaryAntagPreference(session, def))
return false;

if (!IsSessionValid(ent, session, def) || !IsEntityValid(session?.AttachedEntity, def))
return false;
}

MakeAntag(ent, session, def, ignoreSpawner);
return true;
Expand Down Expand Up @@ -324,16 +326,14 @@ public AntagSelectionPlayerPool GetPlayerPool(Entity<AntagSelectionComponent> en
var fallbackList = new List<ICommonSession>();
foreach (var session in sessions)
{
if (!IsSessionValid(ent, session, def) ||
!IsEntityValid(session.AttachedEntity, def))
if (!IsSessionValid(ent, session, def) || !IsEntityValid(session.AttachedEntity, def))
continue;

var pref = (HumanoidCharacterProfile) _pref.GetPreferences(session.UserId).SelectedCharacter;
if (def.PrefRoles.Count != 0 && pref.AntagPreferences.Any(p => def.PrefRoles.Contains(p)))
if (HasPrimaryAntagPreference(session, def))
{
preferredList.Add(session);
}
else if (def.FallbackRoles.Count != 0 && pref.AntagPreferences.Any(p => def.FallbackRoles.Contains(p)))
else if (HasFallbackAntagPreference(session, def))
{
fallbackList.Add(session);
}
Expand Down Expand Up @@ -404,13 +404,13 @@ public bool IsEntityValid(EntityUid? entity, AntagSelectionDefinition def)

if (def.Whitelist != null)
{
if (!def.Whitelist.IsValid(entity.Value, EntityManager))
if (!def.Whitelist.IsValid(entity.Value))
return false;
}

if (def.Blacklist != null)
{
if (def.Blacklist.IsValid(entity.Value, EntityManager))
if (def.Blacklist.IsValid(entity.Value))
return false;
}

Expand Down
Loading