Refactor minds to be entities with components, make roles components (#19591)

Co-authored-by: ShadowCommander <10494922+ShadowCommander@users.noreply.github.com>
Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com>
This commit is contained in:
DrSmugleaf
2023-08-28 16:53:24 -07:00
committed by GitHub
parent e0ee397af7
commit 15c0211fb2
119 changed files with 1445 additions and 1289 deletions

View File

@@ -1,6 +1,6 @@
using System.Text;
using Content.Server.Administration;
using Content.Server.Players;
using Content.Server.Roles;
using Content.Shared.Administration;
using Robust.Server.Player;
using Robust.Shared.Console;
@@ -10,10 +10,10 @@ namespace Content.Server.Mind.Commands
[AdminCommand(AdminFlags.Admin)]
public sealed class MindInfoCommand : IConsoleCommand
{
[Dependency] private readonly IEntityManager _entities = default!;
public string Command => "mindinfo";
public string Description => "Lists info for the mind of a specific player.";
public string Help => "mindinfo <session ID>";
public void Execute(IConsoleShell shell, string argStr, string[] args)
@@ -25,15 +25,14 @@ namespace Content.Server.Mind.Commands
}
var mgr = IoCManager.Resolve<IPlayerManager>();
if (!mgr.TryGetSessionByUsername(args[0], out var data))
if (!mgr.TryGetSessionByUsername(args[0], out var session))
{
shell.WriteLine("Can't find that mind");
return;
}
var mind = data.ContentData()?.Mind;
if (mind == null)
var minds = _entities.System<MindSystem>();
if (!minds.TryGetMind(session, out var mindId, out var mind))
{
shell.WriteLine("Can't find that mind");
return;
@@ -41,7 +40,9 @@ namespace Content.Server.Mind.Commands
var builder = new StringBuilder();
builder.AppendFormat("player: {0}, mob: {1}\nroles: ", mind.UserId, mind.OwnedEntity);
foreach (var role in mind.AllRoles)
var roles = _entities.System<RoleSystem>();
foreach (var role in roles.MindGetAllRoles(mindId))
{
builder.AppendFormat("{0} ", role.Name);
}

View File

@@ -1,7 +1,6 @@
using Content.Server.Access.Systems;
using Content.Server.Administration;
using Content.Server.Administration.Systems;
using Content.Server.Mind.Components;
using Content.Server.PDA;
using Content.Server.StationRecords.Systems;
using Content.Shared.Access.Components;
@@ -46,16 +45,16 @@ public sealed class RenameCommand : IConsoleCommand
var oldName = metadata.EntityName;
entMan.System<MetaDataSystem>().SetEntityName(entityUid, name, metadata);
var entSysMan = IoCManager.Resolve<IEntitySystemManager>();
var minds = entMan.System<MindSystem>();
if (entMan.TryGetComponent(entityUid, out MindContainerComponent? mind) && mind.Mind != null)
if (minds.TryGetMind(entityUid, out var mindId, out var mind))
{
// Mind
mind.Mind.CharacterName = name;
mind.CharacterName = name;
}
// Id Cards
if (entSysMan.TryGetEntitySystem<IdCardSystem>(out var idCardSystem))
if (entMan.TrySystem<IdCardSystem>(out var idCardSystem))
{
if (idCardSystem.TryFindIdCard(entityUid, out var idCard))
{
@@ -63,7 +62,7 @@ public sealed class RenameCommand : IConsoleCommand
// Records
// This is done here because ID cards are linked to station records
if (entSysMan.TryGetEntitySystem<StationRecordsSystem>(out var recordsSystem)
if (entMan.TrySystem<StationRecordsSystem>(out var recordsSystem)
&& entMan.TryGetComponent(idCard.Owner, out StationRecordKeyStorageComponent? keyStorage)
&& keyStorage.Key != null)
{
@@ -80,7 +79,7 @@ public sealed class RenameCommand : IConsoleCommand
}
// PDAs
if (entSysMan.TryGetEntitySystem<PdaSystem>(out var pdaSystem))
if (entMan.TrySystem<PdaSystem>(out var pdaSystem))
{
var query = entMan.EntityQueryEnumerator<PdaComponent>();
while (query.MoveNext(out var uid, out var pda))
@@ -93,7 +92,7 @@ public sealed class RenameCommand : IConsoleCommand
}
// Admin Overlay
if (entSysMan.TryGetEntitySystem<AdminSystem>(out var adminSystem)
if (entMan.TrySystem<AdminSystem>(out var adminSystem)
&& entMan.TryGetComponent<ActorComponent>(entityUid, out var actorComp))
{
adminSystem.UpdatePlayerList(actorComp.PlayerSession);

View File

@@ -3,7 +3,7 @@ using System.Diagnostics.CodeAnalysis;
namespace Content.Server.Mind.Components
{
/// <summary>
/// Stores a <see cref="Server.Mind.Mind"/> on a mob.
/// Stores a <see cref="MindComponent"/> on a mob.
/// </summary>
[RegisterComponent, Access(typeof(MindSystem))]
public sealed partial class MindContainerComponent : Component
@@ -13,7 +13,7 @@ namespace Content.Server.Mind.Components
/// </summary>
[ViewVariables]
[Access(typeof(MindSystem), Other = AccessPermissions.ReadWriteExecute)] // FIXME Friends
public Mind? Mind { get; set; }
public EntityUid? Mind { get; set; }
/// <summary>
/// True if we have a mind, false otherwise.
@@ -40,10 +40,12 @@ namespace Content.Server.Mind.Components
public sealed class MindRemovedMessage : EntityEventArgs
{
public Mind OldMind;
public EntityUid OldMindId;
public MindComponent OldMind;
public MindRemovedMessage(Mind oldMind)
public MindRemovedMessage(EntityUid oldMindId, MindComponent oldMind)
{
OldMindId = oldMindId;
OldMind = oldMind;
}
}

View File

@@ -4,7 +4,10 @@ namespace Content.Server.Mind.Components
public sealed partial class VisitingMindComponent : Component
{
[ViewVariables]
public Mind? Mind;
public EntityUid? MindId;
[ViewVariables]
public MindComponent? Mind;
}
public sealed class MindUnvisitedMessage : EntityEventArgs

View File

@@ -1,15 +1,14 @@
using System.Linq;
using Content.Server.GameTicking;
using Content.Server.GameTicking;
using Content.Server.Mind.Components;
using Content.Server.Objectives;
using Content.Server.Roles;
using Robust.Server.Player;
using Robust.Shared.Network;
namespace Content.Server.Mind
{
/// <summary>
/// A mind represents the IC "mind" of a player. Stores roles currently.
/// A mind represents the IC "mind" of a player.
/// Roles are attached as components to its owning entity.
/// </summary>
/// <remarks>
/// Think of it like this: if a player is supposed to have their memories,
@@ -18,23 +17,11 @@ namespace Content.Server.Mind
/// Things such as respawning do not follow, because you're a new character.
/// Getting borged, cloned, turned into a catbeast, etc... will keep it following you.
/// </remarks>
public sealed class Mind
[RegisterComponent]
public sealed partial class MindComponent : Component
{
internal readonly ISet<Role> Roles = new HashSet<Role>();
internal readonly List<Objective> Objectives = new();
public string Briefing = String.Empty;
/// <summary>
/// Creates the new mind.
/// Note: the Mind is NOT initially attached!
/// The provided UserId is solely for tracking of intended owner.
/// </summary>
public Mind()
{
}
/// <summary>
/// The session ID of the player owning this mind.
/// </summary>
@@ -87,12 +74,7 @@ namespace Content.Server.Mind
[ViewVariables, Access(typeof(MindSystem))]
public EntityUid? OwnedEntity { get; set; }
/// <summary>
/// An enumerable over all the roles this mind has.
/// </summary>
[ViewVariables]
public IEnumerable<Role> AllRoles => Roles;
// TODO move objectives out of mind component
/// <summary>
/// An enumerable over all the objectives this mind has.
/// </summary>
@@ -119,10 +101,5 @@ namespace Content.Server.Mind
/// </summary>
[ViewVariables, Access(typeof(MindSystem), typeof(GameTicker))]
public IPlayerSession? Session { get; internal set; }
/// <summary>
/// Gets the current job
/// </summary>
public Job? CurrentJob => Roles.OfType<Job>().SingleOrDefault();
}
}

View File

@@ -1,19 +1,17 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Server.Administration.Logs;
using Content.Server.GameTicking;
using Content.Server.Ghost;
using Content.Server.Mind.Components;
using Content.Server.Objectives;
using Content.Server.Players;
using Content.Server.Roles;
using Content.Shared.Database;
using Content.Shared.Examine;
using Content.Shared.GameTicking;
using Content.Shared.Ghost;
using Content.Shared.Mobs.Systems;
using Content.Shared.Interaction.Events;
using Content.Shared.Mobs.Components;
using Content.Shared.Mobs.Systems;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.Map;
@@ -36,7 +34,7 @@ public sealed class MindSystem : EntitySystem
[Dependency] private readonly MetaDataSystem _metaData = default!;
// This is dictionary is required to track the minds of disconnected players that may have had their entity deleted.
private readonly Dictionary<NetUserId, Mind> _userMinds = new();
private readonly Dictionary<NetUserId, EntityUid> _userMinds = new();
public override void Initialize()
{
@@ -55,14 +53,6 @@ public sealed class MindSystem : EntitySystem
WipeAllMinds();
}
public void SetGhostOnShutdown(EntityUid uid, bool value, MindContainerComponent? mind = null)
{
if (!Resolve(uid, ref mind))
return;
mind.GhostOnShutdown = value;
}
private void OnReset(RoundRestartCleanupEvent ev)
{
WipeAllMinds();
@@ -86,59 +76,35 @@ public sealed class MindSystem : EntitySystem
}
}
public Mind? GetMind(NetUserId user)
public EntityUid? GetMind(NetUserId user)
{
TryGetMind(user, out var mind);
TryGetMind(user, out var mind, out _);
return mind;
}
public bool TryGetMind(NetUserId user, [NotNullWhen(true)] out Mind? mind)
public bool TryGetMind(NetUserId user, [NotNullWhen(true)] out EntityUid? mindId, [NotNullWhen(true)] out MindComponent? mind)
{
if (_userMinds.TryGetValue(user, out mind))
if (_userMinds.TryGetValue(user, out var mindIdValue) &&
TryComp(mindIdValue, out mind))
{
DebugTools.Assert(mind.UserId == user);
DebugTools.Assert(_playerManager.GetPlayerData(user).ContentData() is not {} data
|| data.Mind == mind);
|| data.Mind == mindIdValue);
mindId = mindIdValue;
return true;
}
DebugTools.Assert(_playerManager.GetPlayerData(user).ContentData()?.Mind == null);
mindId = null;
mind = null;
return false;
}
/// <summary>
/// Don't call this unless you know what the hell you're doing.
/// Use <see cref="MindSystem.TransferTo(Mind,System.Nullable{Robust.Shared.GameObjects.EntityUid},bool)"/> instead.
/// If that doesn't cover it, make something to cover it.
/// </summary>
private void InternalAssignMind(EntityUid uid, Mind value, MindContainerComponent? mind = null)
{
if (!Resolve(uid, ref mind))
return;
mind.Mind = value;
RaiseLocalEvent(uid, new MindAddedMessage(), true);
}
/// <summary>
/// Don't call this unless you know what the hell you're doing.
/// Use <see cref="MindSystem.TransferTo(Mind,System.Nullable{Robust.Shared.GameObjects.EntityUid},bool)"/> instead.
/// If that doesn't cover it, make something to cover it.
/// </summary>
private void InternalEjectMind(EntityUid uid, MindContainerComponent? mind = null)
{
if (!Resolve(uid, ref mind, false) || mind.Mind == null)
return;
var oldMind = mind.Mind;
mind.Mind = null;
RaiseLocalEvent(uid, new MindRemovedMessage(oldMind), true);
}
private void OnVisitingTerminating(EntityUid uid, VisitingMindComponent component, ref EntityTerminatingEvent args)
{
if (component.Mind != null)
UnVisit(component.Mind);
if (component.MindId != null)
UnVisit(component.MindId.Value, component.Mind);
}
private void OnMindContainerTerminating(EntityUid uid, MindContainerComponent component, ref EntityTerminatingEvent args)
@@ -147,7 +113,7 @@ public sealed class MindSystem : EntitySystem
if (_gameTicker.RunLevel == GameRunLevel.PreRoundLobby)
return;
if (component.Mind is not { } mind)
if (!TryGetMind(uid, out var mindId, out var mind, component))
return;
// If the player is currently visiting some other entity, simply attach to that entity.
@@ -156,13 +122,13 @@ public sealed class MindSystem : EntitySystem
&& !Deleted(visiting)
&& !Terminating(visiting))
{
TransferTo(mind, visiting);
TransferTo(mindId, visiting, mind: mind);
if (TryComp(visiting, out GhostComponent? ghost))
_ghostSystem.SetCanReturnToBody(ghost, false);
return;
}
TransferTo(mind, null, createGhost: false);
TransferTo(mindId, null, createGhost: false, mind: mind);
if (component.GhostOnShutdown && mind.Session != null)
{
@@ -187,7 +153,7 @@ public sealed class MindSystem : EntitySystem
{
// This should be an error, if it didn't cause tests to start erroring when they delete a player.
Log.Warning($"Entity \"{ToPrettyString(uid)}\" for {mind.CharacterName} was deleted, and no applicable spawn location is available.");
TransferTo(mind, null, createGhost: false);
TransferTo(mindId, null, createGhost: false, mind: mind);
return;
}
@@ -200,7 +166,7 @@ public sealed class MindSystem : EntitySystem
var val = mind.CharacterName ?? string.Empty;
_metaData.SetEntityName(ghost, val);
TransferTo(mind, ghost);
TransferTo(mindId, ghost, mind: mind);
});
}
}
@@ -211,7 +177,7 @@ public sealed class MindSystem : EntitySystem
return;
var dead = _mobStateSystem.IsDead(uid);
var hasSession = mindContainer.Mind?.Session;
var hasSession = CompOrNull<MindComponent>(mindContainer.Mind)?.Session;
if (dead && !mindContainer.HasMind)
args.PushMarkup($"[color=mediumpurple]{Loc.GetString("comp-mind-examined-dead-and-irrecoverable", ("ent", uid))}[/color]");
@@ -230,36 +196,40 @@ public sealed class MindSystem : EntitySystem
if (args.Handled)
return;
if (component.HasMind && component.Mind.PreventSuicide)
if (TryComp(component.Mind, out MindComponent? mind) && mind.PreventSuicide)
{
args.BlockSuicideAttempt(true);
}
}
public Mind? GetMind(EntityUid uid, MindContainerComponent? mind = null)
public EntityUid? GetMind(EntityUid uid, MindContainerComponent? mind = null)
{
if (!Resolve(uid, ref mind))
return null;
if (mind.HasMind)
return mind.Mind;
return null;
}
public Mind CreateMind(NetUserId? userId, string? name = null)
public EntityUid CreateMind(NetUserId? userId, string? name = null)
{
var mind = new Mind();
var mindId = Spawn(null, MapCoordinates.Nullspace);
var mind = EnsureComp<MindComponent>(mindId);
mind.CharacterName = name;
SetUserId(mind, userId);
SetUserId(mindId, userId, mind);
return mind;
Dirty(mindId, MetaData(mindId));
return mindId;
}
/// <summary>
/// True if the OwnedEntity of this mind is physically dead.
/// This specific definition, as opposed to CharacterDeadIC, is used to determine if ghosting should allow return.
/// </summary>
public bool IsCharacterDeadPhysically(Mind mind)
public bool IsCharacterDeadPhysically(MindComponent mind)
{
// This is written explicitly so that the logic can be understood.
// But it's also weird and potentially situational.
@@ -285,8 +255,11 @@ public sealed class MindSystem : EntitySystem
return _mobStateSystem.IsDead(mind.OwnedEntity.Value, targetMobState);
}
public void Visit(Mind mind, EntityUid entity)
public void Visit(EntityUid mindId, EntityUid entity, MindComponent? mind = null)
{
if (!Resolve(mindId, ref mind))
return;
if (mind.VisitingEntity != null)
{
Log.Error($"Attempted to visit an entity ({ToPrettyString(entity)}) while already visiting another ({ToPrettyString(mind.VisitingEntity.Value)}).");
@@ -304,6 +277,7 @@ public sealed class MindSystem : EntitySystem
// EnsureComp instead of AddComp to deal with deferred deletions.
var comp = EnsureComp<VisitingMindComponent>(entity);
comp.MindId = mindId;
comp.Mind = mind;
Log.Info($"Session {mind.Session?.Name} visiting entity {entity}.");
}
@@ -311,9 +285,12 @@ public sealed class MindSystem : EntitySystem
/// <summary>
/// Returns the mind to its original entity.
/// </summary>
public void UnVisit(Mind? mind)
public void UnVisit(EntityUid mindId, MindComponent? mind = null)
{
if (mind == null || mind.VisitingEntity == null)
if (!Resolve(mindId, ref mind))
return;
if (mind.VisitingEntity == null)
return;
RemoveVisitingEntity(mind);
@@ -331,11 +308,22 @@ public sealed class MindSystem : EntitySystem
}
}
/// <summary>
/// Returns the mind to its original entity.
/// </summary>
public void UnVisit(IPlayerSession? player)
{
if (player == null || !TryGetMind(player, out var mindId, out var mind))
return;
UnVisit(mindId, mind);
}
/// <summary>
/// Cleans up the VisitingEntity.
/// </summary>
/// <param name="mind"></param>
private void RemoveVisitingEntity(Mind mind)
private void RemoveVisitingEntity(MindComponent mind)
{
if (mind.VisitingEntity == null)
return;
@@ -363,19 +351,19 @@ public sealed class MindSystem : EntitySystem
/// <summary>
/// Detaches a mind from all entities and clears the user ID.
/// </summary>
public void WipeMind(Mind? mind)
public void WipeMind(EntityUid? mindId, MindComponent? mind = null)
{
if (mind == null)
if (mindId == null || !Resolve(mindId.Value, ref mind, false))
return;
TransferTo(mind, null);
SetUserId(mind, null);
TransferTo(mindId.Value, null, mind: mind);
SetUserId(mindId.Value, null, mind: mind);
}
/// <summary>
/// Transfer this mind's control over to a new entity.
/// </summary>
/// <param name="mind">The mind to transfer</param>
/// <param name="mindId">The mind to transfer</param>
/// <param name="entity">
/// The entity to control.
/// Can be null, in which case it will simply detach the mind from any entity.
@@ -384,10 +372,13 @@ public sealed class MindSystem : EntitySystem
/// If true, skips ghost check for Visiting Entity
/// </param>
/// <exception cref="ArgumentException">
/// Thrown if <paramref name="entity"/> is already owned by another mind.
/// Thrown if <paramref name="entity"/> is already controlled by another player.
/// </exception>
public void TransferTo(Mind mind, EntityUid? entity, bool ghostCheckOverride = false, bool createGhost = true)
public void TransferTo(EntityUid mindId, EntityUid? entity, bool ghostCheckOverride = false, bool createGhost = true, MindComponent? mind = null)
{
if (!Resolve(mindId, ref mind))
return;
if (entity == mind.OwnedEntity)
return;
@@ -399,7 +390,7 @@ public sealed class MindSystem : EntitySystem
component = EnsureComp<MindContainerComponent>(entity.Value);
if (component.HasMind)
_gameTicker.OnGhostAttempt(component.Mind, false);
_gameTicker.OnGhostAttempt(component.Mind.Value, false);
if (TryComp<ActorComponent>(entity.Value, out var actor))
{
@@ -425,8 +416,11 @@ public sealed class MindSystem : EntitySystem
var oldComp = mind.OwnedComponent;
var oldEntity = mind.OwnedEntity;
if(oldComp != null && oldEntity != null)
InternalEjectMind(oldEntity.Value, oldComp);
if (oldComp != null && oldEntity != null)
{
oldComp.Mind = null;
RaiseLocalEvent(oldEntity.Value, new MindRemovedMessage(oldEntity.Value, mind), true);
}
SetOwnedEntity(mind, entity, component);
@@ -455,7 +449,8 @@ public sealed class MindSystem : EntitySystem
if (mind.OwnedComponent != null)
{
InternalAssignMind(mind.OwnedEntity!.Value, mind, mind.OwnedComponent);
mind.OwnedComponent.Mind = mindId;
RaiseLocalEvent(mind.OwnedEntity!.Value, new MindAddedMessage(), true);
mind.OriginalOwnedEntity ??= mind.OwnedEntity;
}
}
@@ -463,11 +458,11 @@ public sealed class MindSystem : EntitySystem
/// <summary>
/// Adds an objective to this mind.
/// </summary>
public bool TryAddObjective(Mind mind, ObjectivePrototype objectivePrototype)
public bool TryAddObjective(EntityUid mindId, MindComponent mind, ObjectivePrototype objectivePrototype)
{
if (!objectivePrototype.CanBeAssigned(mind))
if (!objectivePrototype.CanBeAssigned(mindId, mind))
return false;
var objective = objectivePrototype.GetObjective(mind);
var objective = objectivePrototype.GetObjective(mindId, mind);
if (mind.Objectives.Contains(objective))
return false;
@@ -476,7 +471,6 @@ public sealed class MindSystem : EntitySystem
_adminLogger.Add(LogType.Mind, LogImpact.Low, $"'{condition.Title}' added to mind of {MindOwnerLoggingString(mind)}");
}
mind.Objectives.Add(objective);
return true;
}
@@ -485,9 +479,10 @@ public sealed class MindSystem : EntitySystem
/// Removes an objective to this mind.
/// </summary>
/// <returns>Returns true if the removal succeeded.</returns>
public bool TryRemoveObjective(Mind mind, int index)
public bool TryRemoveObjective(MindComponent mind, int index)
{
if (index < 0 || index >= mind.Objectives.Count) return false;
if (index < 0 || index >= mind.Objectives.Count)
return false;
var objective = mind.Objectives[index];
@@ -500,90 +495,56 @@ public sealed class MindSystem : EntitySystem
return true;
}
/// <summary>
/// Gives this mind a new role.
/// </summary>
/// <param name="mind">The mind to add the role to.</param>
/// <param name="role">The type of the role to give.</param>
/// <returns>The instance of the role.</returns>
/// <exception cref="ArgumentException">
/// Thrown if we already have a role with this type.
/// </exception>
public void AddRole(Mind mind, Role role)
public bool TryGetSession(EntityUid? mindId, [NotNullWhen(true)] out IPlayerSession? session)
{
if (mind.Roles.Contains(role))
{
throw new ArgumentException($"We already have this role: {role}");
}
mind.Roles.Add(role);
role.Greet();
var message = new RoleAddedEvent(mind, role);
if (mind.OwnedEntity != null)
{
RaiseLocalEvent(mind.OwnedEntity.Value, message, true);
}
_adminLogger.Add(LogType.Mind, LogImpact.Low,
$"'{role.Name}' added to mind of {MindOwnerLoggingString(mind)}");
}
/// <summary>
/// Removes a role from this mind.
/// </summary>
/// <param name="mind">The mind to remove the role from.</param>
/// <param name="role">The type of the role to remove.</param>
/// <exception cref="ArgumentException">
/// Thrown if we do not have this role.
/// </exception>
public void RemoveRole(Mind mind, Role role)
{
if (!mind.Roles.Contains(role))
{
throw new ArgumentException($"We do not have this role: {role}");
}
mind.Roles.Remove(role);
var message = new RoleRemovedEvent(mind, role);
if (mind.OwnedEntity != null)
{
RaiseLocalEvent(mind.OwnedEntity.Value, message, true);
}
_adminLogger.Add(LogType.Mind, LogImpact.Low,
$"'{role.Name}' removed from mind of {MindOwnerLoggingString(mind)}");
}
public bool HasRole<T>(Mind mind) where T : Role
{
return mind.Roles.Any(role => role is T);
}
public bool TryGetSession(Mind mind, [NotNullWhen(true)] out IPlayerSession? session)
{
return (session = mind.Session) != null;
session = null;
return TryComp(mindId, out MindComponent? mind) && (session = mind.Session) != null;
}
/// <summary>
/// Gets a mind from uid and/or MindContainerComponent. Used for null checks.
/// </summary>
/// <param name="uid">Entity UID that owns the mind.</param>
/// <param name="mindId">The mind id.</param>
/// <param name="mind">The returned mind.</param>
/// <param name="mindContainerComponent">Mind component on <paramref name="uid"/> to get the mind from.</param>
/// <param name="container">Mind component on <paramref name="uid"/> to get the mind from.</param>
/// <returns>True if mind found. False if not.</returns>
public bool TryGetMind(EntityUid uid, [NotNullWhen(true)] out Mind? mind, MindContainerComponent? mindContainerComponent = null)
public bool TryGetMind(
EntityUid uid,
out EntityUid mindId,
[NotNullWhen(true)] out MindComponent? mind,
MindContainerComponent? container = null)
{
mindId = default;
mind = null;
if (!Resolve(uid, ref mindContainerComponent, false))
if (!Resolve(uid, ref container, false))
return false;
if (!mindContainerComponent.HasMind)
if (!container.HasMind)
return false;
mind = mindContainerComponent.Mind;
return true;
mindId = container.Mind ?? default;
return TryComp(mindId, out mind);
}
public bool TryGetMind(
PlayerData player,
out EntityUid mindId,
[NotNullWhen(true)] out MindComponent? mind)
{
mindId = player.Mind ?? default;
return TryComp(mindId, out mind);
}
public bool TryGetMind(
IPlayerSession? player,
out EntityUid mindId,
[NotNullWhen(true)] out MindComponent? mind)
{
mindId = default;
mind = null;
return player?.ContentData() is { } data && TryGetMind(data, out mindId, out mind);
}
/// <summary>
@@ -592,7 +553,7 @@ public sealed class MindSystem : EntitySystem
/// <param name="mind">Mind to set OwnedComponent and OwnedEntity on</param>
/// <param name="uid">Entity owned by <paramref name="mind"/></param>
/// <param name="mindContainerComponent">MindContainerComponent owned by <paramref name="mind"/></param>
private void SetOwnedEntity(Mind mind, EntityUid? uid, MindContainerComponent? mindContainerComponent)
private void SetOwnedEntity(MindComponent mind, EntityUid? uid, MindContainerComponent? mindContainerComponent)
{
if (uid != null)
Resolve(uid.Value, ref mindContainerComponent);
@@ -606,8 +567,11 @@ public sealed class MindSystem : EntitySystem
/// entity that any mind is connected to, except as a side effect of the fact that it may change a player's
/// attached entity. E.g., ghosts get deleted.
/// </summary>
public void SetUserId(Mind mind, NetUserId? userId)
public void SetUserId(EntityUid mindId, NetUserId? userId, MindComponent? mind = null)
{
if (!Resolve(mindId, ref mind))
return;
if (mind.UserId == userId)
return;
@@ -637,12 +601,15 @@ public sealed class MindSystem : EntitySystem
return;
}
if (_userMinds.TryGetValue(userId.Value, out var oldMind))
SetUserId(oldMind, null);
if (_userMinds.TryGetValue(userId.Value, out var oldMindId) &&
TryComp(oldMindId, out MindComponent? oldMind))
{
SetUserId(oldMindId, null, oldMind);
}
DebugTools.AssertNull(_playerManager.GetPlayerData(userId.Value).ContentData()?.Mind);
_userMinds[userId.Value] = mind;
_userMinds[userId.Value] = mindId;
mind.UserId = userId;
mind.OriginalOwnerUserId ??= userId;
@@ -654,7 +621,7 @@ public sealed class MindSystem : EntitySystem
// session may be null, but user data may still exist for disconnected players.
if (_playerManager.GetPlayerData(userId.Value).ContentData() is { } data)
data.Mind = mind;
data.Mind = mindId;
}
/// <summary>
@@ -663,7 +630,7 @@ public sealed class MindSystem : EntitySystem
/// "If administrators decide that zombies are dead, this returns true for zombies."
/// (Maybe you were looking for the action blocker system?)
/// </summary>
public bool IsCharacterDeadIc(Mind mind)
public bool IsCharacterDeadIc(MindComponent mind)
{
if (mind.OwnedEntity is { } owned)
{
@@ -680,7 +647,7 @@ public sealed class MindSystem : EntitySystem
/// <summary>
/// A string to represent the mind for logging
/// </summary>
private string MindOwnerLoggingString(Mind mind)
public string MindOwnerLoggingString(MindComponent mind)
{
if (mind.OwnedEntity != null)
return ToPrettyString(mind.OwnedEntity.Value);
@@ -688,6 +655,11 @@ public sealed class MindSystem : EntitySystem
return mind.UserId.Value.ToString();
return "(originally " + mind.OriginalOwnerUserId + ")";
}
public string? GetCharacterName(NetUserId userId)
{
return TryGetMind(userId, out _, out var mind) ? mind.CharacterName : null;
}
}
/// <summary>

View File

@@ -1,39 +0,0 @@
using Content.Server.Mind.Components;
using Content.Shared.GameTicking;
namespace Content.Server.Mind
{
/// <summary>
/// This is absolutely evil.
/// It tracks all mind changes and logs all the Mind objects.
/// This is so that when round end comes around, there's a coherent list of all Minds that were in play during the round.
/// The Minds themselves contain metadata about their owners.
/// Anyway, this is because disconnected people and ghost roles have been breaking round end statistics for way too long.
/// </summary>
public sealed class MindTrackerSystem : EntitySystem
{
[ViewVariables]
public readonly HashSet<Mind> AllMinds = new();
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<RoundRestartCleanupEvent>(Reset);
SubscribeLocalEvent<MindContainerComponent, MindAddedMessage>(OnMindAdded);
}
void Reset(RoundRestartCleanupEvent ev)
{
AllMinds.Clear();
}
void OnMindAdded(EntityUid uid, MindContainerComponent mc, MindAddedMessage args)
{
var mind = mc.Mind;
if (mind != null)
AllMinds.Add(mind);
}
}
}

View File

@@ -1,7 +1,4 @@
using Content.Server.Mind.Components;
using Content.Server.Players;
using Robust.Server.Player;
using Robust.Shared.Players;
using Robust.Server.Player;
using Robust.Shared.Toolshed;
using Robust.Shared.Toolshed.Errors;
using Robust.Shared.Toolshed.Syntax;
@@ -17,20 +14,17 @@ public sealed class MindCommand : ToolshedCommand
private MindSystem? _mind;
[CommandImplementation("get")]
public Mind? Get([PipedArgument] IPlayerSession session)
public MindComponent? Get([PipedArgument] IPlayerSession session)
{
return session.ContentData()?.Mind;
_mind ??= GetSys<MindSystem>();
return _mind.TryGetMind(session, out _, out var mind) ? mind : null;
}
[CommandImplementation("get")]
public Mind? Get([PipedArgument] EntityUid ent)
public MindComponent? Get([PipedArgument] EntityUid ent)
{
if (!TryComp<MindContainerComponent>(ent, out var container))
{
return null;
}
return container.Mind;
_mind ??= GetSys<MindSystem>();
return _mind.TryGetMind(ent, out _, out var mind) ? mind : null;
}
[CommandImplementation("control")]
@@ -48,15 +42,13 @@ public sealed class MindCommand : ToolshedCommand
return target;
}
var mind = player.ContentData()?.Mind;
if (mind is null)
if (!_mind.TryGetMind(player, out var mindId, out var mind))
{
ctx.ReportError(new SessionHasNoEntityError(player));
return target;
}
_mind.TransferTo(mind, target);
_mind.TransferTo(mindId, target, mind: mind);
return target;
}
}

View File

@@ -25,7 +25,7 @@ public sealed class TransferMindOnGibSystem : EntitySystem
private void OnGib(EntityUid uid, TransferMindOnGibComponent component, BeingGibbedEvent args)
{
if (!TryComp<MindContainerComponent>(uid, out var mindcomp) || mindcomp.Mind == null)
if (!_mindSystem.TryGetMind(uid, out var mindId, out var mind))
return;
var validParts = args.GibbedParts.Where(p => _tag.HasTag(p, component.TargetTag)).ToHashSet();
@@ -33,6 +33,6 @@ public sealed class TransferMindOnGibSystem : EntitySystem
return;
var ent = _random.Pick(validParts);
_mindSystem.TransferTo(mindcomp.Mind, ent);
_mindSystem.TransferTo(mindId, ent, mind: mind);
}
}