Move minds, roles, jobs and objectives to shared (#19679)

This commit is contained in:
DrSmugleaf
2023-08-30 21:46:11 -07:00
committed by GitHub
parent b702963d5e
commit 3f3ba6ac62
139 changed files with 1209 additions and 1041 deletions

View File

@@ -0,0 +1,9 @@
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Shared.Roles;
public abstract partial class AntagonistRoleComponent : Component
{
[DataField("prototype", required: true, customTypeSerializer: typeof(PrototypeIdSerializer<AntagPrototype>))]
public string? PrototypeId;
}

View File

@@ -0,0 +1,13 @@
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Shared.Roles.Jobs;
/// <summary>
/// Added to mind entities to hold the data for the player's current job.
/// </summary>
[RegisterComponent]
public sealed partial class JobComponent : Component
{
[DataField("prototype", required: true, customTypeSerializer: typeof(PrototypeIdSerializer<JobPrototype>))]
public string? PrototypeId;
}

View File

@@ -0,0 +1,70 @@
using System.Diagnostics.CodeAnalysis;
using Content.Shared.Players;
using Robust.Shared.Players;
using Robust.Shared.Prototypes;
namespace Content.Shared.Roles.Jobs;
/// <summary>
/// Handles the job data on mind entities.
/// </summary>
public abstract class SharedJobSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypes = default!;
[Dependency] private readonly SharedPlayerSystem _playerSystem = default!;
public bool MindHasJobWithId(EntityUid? mindId, string prototypeId)
{
return CompOrNull<JobComponent>(mindId)?.PrototypeId == prototypeId;
}
public bool MindTryGetJob(
[NotNullWhen(true)] EntityUid? mindId,
[NotNullWhen(true)] out JobComponent? comp,
[NotNullWhen(true)] out JobPrototype? prototype)
{
comp = null;
prototype = null;
return TryComp(mindId, out comp) &&
comp.PrototypeId != null &&
_prototypes.TryIndex(comp.PrototypeId, out prototype);
}
/// <summary>
/// Tries to get the job name for this mind.
/// Returns unknown if not found.
/// </summary>
public bool MindTryGetJobName([NotNullWhen(true)] EntityUid? mindId, out string name)
{
if (MindTryGetJob(mindId, out _, out var prototype))
{
name = prototype.LocalizedName;
return true;
}
name = Loc.GetString("generic-unknown-title");
return false;
}
/// <summary>
/// Tries to get the job name for this mind.
/// Returns unknown if not found.
/// </summary>
public string MindTryGetJobName([NotNullWhen(true)] EntityUid? mindId)
{
MindTryGetJobName(mindId, out var name);
return name;
}
public bool CanBeAntag(ICommonSession player)
{
if (_playerSystem.ContentData(player) is not { Mind: { } mindId })
return false;
if (!MindTryGetJob(mindId, out _, out var prototype))
return true;
return prototype.CanBeAntag;
}
}

View File

@@ -0,0 +1,19 @@
using Content.Shared.Players.PlayTimeTracking;
namespace Content.Shared.Roles;
/// <summary>
/// Event raised on a mind entity to get all roles that a player has.
/// </summary>
/// <param name="Roles">The list of roles on the player.</param>
[ByRefEvent]
public readonly record struct MindGetAllRolesEvent(List<RoleInfo> Roles);
/// <summary>
/// Returned by <see cref="MindGetAllRolesEvent"/> to give some information about a player's role.
/// </summary>
/// <param name="Component">Role component associated with the mind entity id.</param>
/// <param name="Name">Name of the role.</param>
/// <param name="Antagonist">Whether or not this role makes this player an antagonist.</param>
/// <param name="PlayTimeTrackerId">The <see cref="PlayTimeTrackerPrototype"/> id associated with the role.</param>
public readonly record struct RoleInfo(Component Component, string Name, bool Antagonist, string? PlayTimeTrackerId);

View File

@@ -0,0 +1,9 @@
namespace Content.Shared.Roles;
/// <summary>
/// Event raised on a mind entity id to get whether or not the player is considered an antagonist,
/// depending on their roles.
/// </summary>
/// <param name="IsAntagonist">Whether or not the player is an antagonist.</param>
[ByRefEvent]
public record struct MindIsAntagonistEvent(bool IsAntagonist);

View File

@@ -0,0 +1,8 @@
namespace Content.Shared.Roles;
/// <summary>
/// Raised on mind entities when a role is added to them.
/// <see cref="RoleAddedEvent"/> for the one raised on player entities.
/// </summary>
[ByRefEvent]
public readonly record struct MindRoleAddedEvent(bool Silent);

View File

@@ -0,0 +1,12 @@
using Content.Shared.Mind;
namespace Content.Shared.Roles;
/// <summary>
/// Raised on player entities when a role is added to them.
/// <see cref="RoleAddedEvent"/> for the one raised on mind entities.
/// </summary>
/// <param name="MindId">The mind id associated with the player.</param>
/// <param name="Mind">The mind component associated with the mind id.</param>
/// <param name="Antagonist">Whether or not the role makes the player an antagonist.</param>
public sealed record RoleAddedEvent(EntityUid MindId, MindComponent Mind, bool Antagonist, bool Silent = false) : RoleEvent(MindId, Mind, Antagonist);

View File

@@ -0,0 +1,11 @@
using Content.Shared.Mind;
namespace Content.Shared.Roles;
/// <summary>
/// Base event raised on player entities to indicate that something changed about one of their roles.
/// </summary>
/// <param name="MindId">The mind id associated with the player.</param>
/// <param name="Mind">The mind component associated with the mind id.</param>
/// <param name="Antagonist">Whether or not the role makes the player an antagonist.</param>
public abstract record RoleEvent(EntityUid MindId, MindComponent Mind, bool Antagonist);

View File

@@ -0,0 +1,14 @@
using Content.Shared.Mind;
namespace Content.Shared.Roles;
/// <summary>
/// Event raised on player entities to indicate that a role was removed from their mind.
/// </summary>
/// <param name="MindId">The mind id associated with the player.</param>
/// <param name="Mind">The mind component associated with the mind id.</param>
/// <param name="Antagonist">
/// Whether or not the role made the player an antagonist.
/// They may still be one due to one of their other roles.
/// </param>
public sealed record RoleRemovedEvent(EntityUid MindId, MindComponent Mind, bool Antagonist) : RoleEvent(MindId, Mind, Antagonist);

View File

@@ -0,0 +1,156 @@
using Content.Shared.Administration.Logs;
using Content.Shared.Database;
using Content.Shared.Mind;
using Content.Shared.Roles.Jobs;
using Robust.Shared.Prototypes;
namespace Content.Shared.Roles;
public abstract class SharedRoleSystem : EntitySystem
{
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
[Dependency] private readonly IPrototypeManager _prototypes = default!;
[Dependency] private readonly SharedMindSystem _minds = default!;
// TODO please lord make role entities
private readonly HashSet<Type> _antagTypes = new();
public override void Initialize()
{
// TODO make roles entities
SubscribeLocalEvent<JobComponent, MindGetAllRolesEvent>(OnJobGetAllRoles);
}
private void OnJobGetAllRoles(EntityUid uid, JobComponent component, ref MindGetAllRolesEvent args)
{
var name = "game-ticker-unknown-role";
string? playTimeTracker = null;
if (component.PrototypeId != null && _prototypes.TryIndex(component.PrototypeId, out JobPrototype? job))
{
name = job.Name;
playTimeTracker = job.PlayTimeTracker;
}
name = Loc.GetString(name);
args.Roles.Add(new RoleInfo(component, name, false, playTimeTracker));
}
protected void SubscribeAntagEvents<T>() where T : AntagonistRoleComponent
{
SubscribeLocalEvent((EntityUid _, T component, ref MindGetAllRolesEvent args) =>
{
var name = "game-ticker-unknown-role";
if (component.PrototypeId != null && _prototypes.TryIndex(component.PrototypeId, out AntagPrototype? antag))
{
name = antag.Name;
}
name = Loc.GetString(name);
args.Roles.Add(new RoleInfo(component, name, true, null));
});
SubscribeLocalEvent((EntityUid _, T _, ref MindIsAntagonistEvent args) => args.IsAntagonist = true);
_antagTypes.Add(typeof(T));
}
/// <summary>
/// Gives this mind a new role.
/// </summary>
/// <param name="mindId">The mind to add the role to.</param>
/// <param name="component">The role instance to add.</param>
/// <typeparam name="T">The role type to add.</typeparam>
/// <param name="silent">Whether or not the role should be added silently</param>
/// <returns>The instance of the role.</returns>
/// <exception cref="ArgumentException">
/// Thrown if we already have a role with this type.
/// </exception>
public void MindAddRole<T>(EntityUid mindId, T component, MindComponent? mind = null, bool silent = false) where T : Component, new()
{
if (!Resolve(mindId, ref mind))
return;
if (HasComp<T>(mindId))
{
throw new ArgumentException($"We already have this role: {typeof(T)}");
}
AddComp(mindId, component);
var antagonist = IsAntagonistRole<T>();
var mindEv = new MindRoleAddedEvent();
RaiseLocalEvent(mindId, ref mindEv);
var message = new RoleAddedEvent(mindId, mind, antagonist, silent);
if (mind.OwnedEntity != null)
{
RaiseLocalEvent(mind.OwnedEntity.Value, message, true);
}
_adminLogger.Add(LogType.Mind, LogImpact.Low,
$"'Role {typeof(T).Name}' added to mind of {_minds.MindOwnerLoggingString(mind)}");
}
/// <summary>
/// Removes a role from this mind.
/// </summary>
/// <param name="mindId">The mind to remove the role from.</param>
/// <typeparam name="T">The type of the role to remove.</typeparam>
/// <exception cref="ArgumentException">
/// Thrown if we do not have this role.
/// </exception>
public void MindRemoveRole<T>(EntityUid mindId) where T : Component
{
if (!RemComp<T>(mindId))
{
throw new ArgumentException($"We do not have this role: {typeof(T)}");
}
var mind = Comp<MindComponent>(mindId);
var antagonist = IsAntagonistRole<T>();
var message = new RoleRemovedEvent(mindId, mind, antagonist);
if (mind.OwnedEntity != null)
{
RaiseLocalEvent(mind.OwnedEntity.Value, message, true);
}
_adminLogger.Add(LogType.Mind, LogImpact.Low,
$"'Role {typeof(T).Name}' removed from mind of {_minds.MindOwnerLoggingString(mind)}");
}
public bool MindTryRemoveRole<T>(EntityUid mindId) where T : Component
{
if (!MindHasRole<T>(mindId))
return false;
MindRemoveRole<T>(mindId);
return true;
}
public bool MindHasRole<T>(EntityUid mindId) where T : Component
{
return HasComp<T>(mindId);
}
public List<RoleInfo> MindGetAllRoles(EntityUid mindId)
{
var ev = new MindGetAllRolesEvent(new List<RoleInfo>());
RaiseLocalEvent(mindId, ref ev);
return ev.Roles;
}
public bool MindIsAntagonist(EntityUid? mindId)
{
if (mindId == null)
return false;
var ev = new MindIsAntagonistEvent();
RaiseLocalEvent(mindId.Value, ref ev);
return ev.IsAntagonist;
}
public bool IsAntagonistRole<T>()
{
return _antagTypes.Contains(typeof(T));
}
}