MindSystem.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. using Content.Server.Administration.Logs;
  2. using Content.Server.GameTicking;
  3. using Content.Server.Ghost;
  4. using Content.Server.Mind.Commands;
  5. using Content.Shared.Database;
  6. using Content.Shared.Ghost;
  7. using Content.Shared.Mind;
  8. using Content.Shared.Mind.Components;
  9. using Content.Shared.Players;
  10. using Robust.Server.GameStates;
  11. using Robust.Server.Player;
  12. using Robust.Shared.Network;
  13. using Robust.Shared.Player;
  14. using Robust.Shared.Utility;
  15. using System.Diagnostics.CodeAnalysis;
  16. namespace Content.Server.Mind;
  17. public sealed class MindSystem : SharedMindSystem
  18. {
  19. [Dependency] private readonly GameTicker _gameTicker = default!;
  20. [Dependency] private readonly IAdminLogManager _adminLogger = default!;
  21. [Dependency] private readonly IPlayerManager _players = default!;
  22. [Dependency] private readonly GhostSystem _ghosts = default!;
  23. [Dependency] private readonly SharedTransformSystem _transform = default!;
  24. [Dependency] private readonly PvsOverrideSystem _pvsOverride = default!;
  25. public override void Initialize()
  26. {
  27. base.Initialize();
  28. SubscribeLocalEvent<MindContainerComponent, EntityTerminatingEvent>(OnMindContainerTerminating);
  29. SubscribeLocalEvent<MindComponent, ComponentShutdown>(OnMindShutdown);
  30. }
  31. private void OnMindShutdown(EntityUid uid, MindComponent mind, ComponentShutdown args)
  32. {
  33. if (mind.UserId is {} user)
  34. {
  35. UserMinds.Remove(user);
  36. if (_players.TryGetPlayerData(user, out var data) && data.ContentData() is { } oldData)
  37. oldData.Mind = null;
  38. mind.UserId = null;
  39. }
  40. if (mind.OwnedEntity != null && !TerminatingOrDeleted(mind.OwnedEntity.Value))
  41. TransferTo(uid, null, mind: mind, createGhost: false);
  42. mind.OwnedEntity = null;
  43. }
  44. private void OnMindContainerTerminating(EntityUid uid, MindContainerComponent component, ref EntityTerminatingEvent args)
  45. {
  46. if (!TryGetMind(uid, out var mindId, out var mind, component))
  47. return;
  48. // If the player is currently visiting some other entity, simply attach to that entity.
  49. if (mind.VisitingEntity is {Valid: true} visiting
  50. && visiting != uid
  51. && !Deleted(visiting)
  52. && !Terminating(visiting))
  53. {
  54. TransferTo(mindId, visiting, mind: mind);
  55. if (TryComp(visiting, out GhostComponent? ghostComp))
  56. _ghosts.SetCanReturnToBody(ghostComp, false);
  57. return;
  58. }
  59. TransferTo(mindId, null, createGhost: false, mind: mind);
  60. DebugTools.AssertNull(mind.OwnedEntity);
  61. if (!component.GhostOnShutdown || mind.Session == null || _gameTicker.RunLevel == GameRunLevel.PreRoundLobby)
  62. return;
  63. var ghost = _ghosts.SpawnGhost((mindId, mind), uid);
  64. if (ghost != null)
  65. // Log these to make sure they're not causing the GameTicker round restart bugs...
  66. Log.Debug($"Entity \"{ToPrettyString(uid)}\" for {mind.CharacterName} was deleted, spawned \"{ToPrettyString(ghost)}\".");
  67. else
  68. // This should be an error, if it didn't cause tests to start erroring when they delete a player.
  69. Log.Warning($"Entity \"{ToPrettyString(uid)}\" for {mind.CharacterName} was deleted, and no applicable spawn location is available.");
  70. }
  71. public override bool TryGetMind(NetUserId user, [NotNullWhen(true)] out EntityUid? mindId, [NotNullWhen(true)] out MindComponent? mind)
  72. {
  73. if (base.TryGetMind(user, out mindId, out mind))
  74. {
  75. DebugTools.Assert(_players.GetPlayerData(user).ContentData() is not { } data || data.Mind == mindId);
  76. return true;
  77. }
  78. DebugTools.Assert(_players.GetPlayerData(user).ContentData()?.Mind == null);
  79. return false;
  80. }
  81. public ICommonSession? GetSession(MindComponent mind)
  82. {
  83. return mind.Session;
  84. }
  85. public bool TryGetSession(MindComponent mind, [NotNullWhen(true)] out ICommonSession? session)
  86. {
  87. return (session = GetSession(mind)) != null;
  88. }
  89. public override void WipeAllMinds()
  90. {
  91. base.WipeAllMinds();
  92. foreach (var unCastData in _players.GetAllPlayerData())
  93. {
  94. if (unCastData.ContentData()?.Mind is not { } mind)
  95. continue;
  96. Log.Error("Player mind was missing from MindSystem dictionary.");
  97. WipeMind(mind);
  98. }
  99. }
  100. public override void Visit(EntityUid mindId, EntityUid entity, MindComponent? mind = null)
  101. {
  102. base.Visit(mindId, entity, mind);
  103. if (!Resolve(mindId, ref mind))
  104. return;
  105. if (mind.VisitingEntity != null)
  106. {
  107. Log.Error($"Attempted to visit an entity ({ToPrettyString(entity)}) while already visiting another ({ToPrettyString(mind.VisitingEntity.Value)}).");
  108. return;
  109. }
  110. if (HasComp<VisitingMindComponent>(entity))
  111. {
  112. Log.Error($"Attempted to visit an entity that already has a visiting mind. Entity: {ToPrettyString(entity)}");
  113. return;
  114. }
  115. mind.VisitingEntity = entity;
  116. // EnsureComp instead of AddComp to deal with deferred deletions.
  117. var comp = EnsureComp<VisitingMindComponent>(entity);
  118. comp.MindId = mindId;
  119. // Do this AFTER the entity changes above as this will fire off a player-detached event
  120. // which will run ghosting twice.
  121. if (GetSession(mind) is { } session)
  122. _players.SetAttachedEntity(session, entity);
  123. Log.Info($"Session {mind.Session?.Name} visiting entity {entity}.");
  124. }
  125. public override void UnVisit(EntityUid mindId, MindComponent? mind = null)
  126. {
  127. base.UnVisit(mindId, mind);
  128. if (!Resolve(mindId, ref mind))
  129. return;
  130. if (mind.VisitingEntity == null)
  131. return;
  132. RemoveVisitingEntity(mindId, mind);
  133. if (mind.Session == null || mind.Session.AttachedEntity == mind.VisitingEntity)
  134. return;
  135. var owned = mind.OwnedEntity;
  136. if (GetSession(mind) is { } session)
  137. _players.SetAttachedEntity(session, owned);
  138. if (owned.HasValue)
  139. {
  140. _adminLogger.Add(LogType.Mind, LogImpact.Low,
  141. $"{mind.Session.Name} returned to {ToPrettyString(owned.Value)}");
  142. }
  143. }
  144. public override void TransferTo(EntityUid mindId, EntityUid? entity, bool ghostCheckOverride = false, bool createGhost = true,
  145. MindComponent? mind = null)
  146. {
  147. if (mind == null && !Resolve(mindId, ref mind))
  148. return;
  149. if (entity == mind.OwnedEntity)
  150. return;
  151. Dirty(mindId, mind);
  152. MindContainerComponent? component = null;
  153. var alreadyAttached = false;
  154. if (entity != null)
  155. {
  156. component = EnsureComp<MindContainerComponent>(entity.Value);
  157. if (component.HasMind)
  158. _ghosts.OnGhostAttempt(component.Mind.Value, false);
  159. if (TryComp<ActorComponent>(entity.Value, out var actor))
  160. {
  161. // Happens when transferring to your currently visited entity.
  162. if (actor.PlayerSession != mind.Session)
  163. {
  164. throw new ArgumentException("Visit target already has a session.", nameof(entity));
  165. }
  166. alreadyAttached = true;
  167. }
  168. }
  169. else if (createGhost)
  170. {
  171. // TODO remove this option.
  172. // Transfer-to-null should just detach a mind.
  173. // If people want to create a ghost, that should be done explicitly via some TransferToGhost() method, not
  174. // not implicitly via optional arguments.
  175. var position = Deleted(mind.OwnedEntity)
  176. ? _gameTicker.GetObserverSpawnPoint().ToMap(EntityManager, _transform)
  177. : _transform.GetMapCoordinates(mind.OwnedEntity.Value);
  178. entity = Spawn(GameTicker.ObserverPrototypeName, position);
  179. component = EnsureComp<MindContainerComponent>(entity.Value);
  180. var ghostComponent = Comp<GhostComponent>(entity.Value);
  181. _ghosts.SetCanReturnToBody(ghostComponent, false);
  182. }
  183. var oldEntity = mind.OwnedEntity;
  184. if (TryComp(oldEntity, out MindContainerComponent? oldContainer))
  185. {
  186. oldContainer.Mind = null;
  187. mind.OwnedEntity = null;
  188. Entity<MindComponent> mindEnt = (mindId, mind);
  189. Entity<MindContainerComponent> containerEnt = (oldEntity.Value, oldContainer);
  190. RaiseLocalEvent(oldEntity.Value, new MindRemovedMessage(mindEnt, containerEnt));
  191. RaiseLocalEvent(mindId, new MindGotRemovedEvent(mindEnt, containerEnt));
  192. Dirty(oldEntity.Value, oldContainer);
  193. }
  194. // Don't do the full deletion cleanup if we're transferring to our VisitingEntity
  195. if (alreadyAttached)
  196. {
  197. // Set VisitingEntity null first so the removal of VisitingMind doesn't get through Unvisit() and delete what we're visiting.
  198. // Yes this control flow sucks.
  199. mind.VisitingEntity = null;
  200. RemComp<VisitingMindComponent>(entity!.Value);
  201. }
  202. else if (mind.VisitingEntity != null
  203. && (ghostCheckOverride // to force mind transfer, for example from ControlMobVerb
  204. || !TryComp(mind.VisitingEntity!, out GhostComponent? ghostComponent) // visiting entity is not a Ghost
  205. || !ghostComponent.CanReturnToBody)) // it is a ghost, but cannot return to body anyway, so it's okay
  206. {
  207. RemoveVisitingEntity(mindId, mind);
  208. }
  209. // Player is CURRENTLY connected.
  210. var session = GetSession(mind);
  211. if (session != null && !alreadyAttached && mind.VisitingEntity == null)
  212. {
  213. _players.SetAttachedEntity(session, entity, true);
  214. DebugTools.Assert(session.AttachedEntity == entity, $"Failed to attach entity.");
  215. Log.Info($"Session {session.Name} transferred to entity {entity}.");
  216. }
  217. if (entity != null)
  218. {
  219. component!.Mind = mindId;
  220. mind.OwnedEntity = entity;
  221. mind.OriginalOwnedEntity ??= GetNetEntity(mind.OwnedEntity);
  222. Entity<MindComponent> mindEnt = (mindId, mind);
  223. Entity<MindContainerComponent> containerEnt = (entity.Value, component);
  224. RaiseLocalEvent(entity.Value, new MindAddedMessage(mindEnt, containerEnt));
  225. RaiseLocalEvent(mindId, new MindGotAddedEvent(mindEnt, containerEnt));
  226. Dirty(entity.Value, component);
  227. }
  228. }
  229. /// <summary>
  230. /// Sets the Mind's UserId, Session, and updates the player's PlayerData. This should have no direct effect on the
  231. /// entity that any mind is connected to, except as a side effect of the fact that it may change a player's
  232. /// attached entity. E.g., ghosts get deleted.
  233. /// </summary>
  234. public override void SetUserId(EntityUid mindId, NetUserId? userId, MindComponent? mind = null)
  235. {
  236. if (!Resolve(mindId, ref mind))
  237. return;
  238. if (mind.UserId == userId)
  239. return;
  240. Dirty(mindId, mind);
  241. var netMind = GetNetEntity(mindId);
  242. _pvsOverride.ClearOverride(netMind);
  243. if (userId != null && !_players.TryGetPlayerData(userId.Value, out _))
  244. {
  245. Log.Error($"Attempted to set mind user to invalid value {userId}");
  246. return;
  247. }
  248. if (mind.Session != null)
  249. {
  250. _players.SetAttachedEntity(GetSession(mind), null);
  251. mind.Session = null;
  252. }
  253. if (mind.UserId != null)
  254. {
  255. UserMinds.Remove(mind.UserId.Value);
  256. if (_players.GetPlayerData(mind.UserId.Value).ContentData() is { } oldData)
  257. oldData.Mind = null;
  258. mind.UserId = null;
  259. }
  260. if (userId == null)
  261. {
  262. DebugTools.AssertNull(mind.Session);
  263. return;
  264. }
  265. if (UserMinds.TryGetValue(userId.Value, out var oldMindId) &&
  266. TryComp(oldMindId, out MindComponent? oldMind))
  267. {
  268. SetUserId(oldMindId, null, oldMind);
  269. }
  270. DebugTools.AssertNull(_players.GetPlayerData(userId.Value).ContentData()?.Mind);
  271. UserMinds[userId.Value] = mindId;
  272. mind.UserId = userId;
  273. mind.OriginalOwnerUserId ??= userId;
  274. // The UserId may not have a current session, but user data may still exist for disconnected players.
  275. // So we cannot combine this with the TryGetSessionById() check below.
  276. if (_players.GetPlayerData(userId.Value).ContentData() is { } data)
  277. data.Mind = mindId;
  278. if (_players.TryGetSessionById(userId.Value, out var ret))
  279. {
  280. mind.Session = ret;
  281. _pvsOverride.AddSessionOverride(netMind, ret);
  282. _players.SetAttachedEntity(ret, mind.CurrentEntity);
  283. }
  284. }
  285. public override void ControlMob(EntityUid user, EntityUid target)
  286. {
  287. if (TryComp(user, out ActorComponent? actor))
  288. ControlMob(actor.PlayerSession.UserId, target);
  289. }
  290. public override void ControlMob(NetUserId user, EntityUid target)
  291. {
  292. var (mindId, mind) = GetOrCreateMind(user);
  293. if (mind.CurrentEntity == target)
  294. return;
  295. if (mind.OwnedEntity == target)
  296. {
  297. UnVisit(mindId, mind);
  298. return;
  299. }
  300. MakeSentientCommand.MakeSentient(target, EntityManager);
  301. TransferTo(mindId, target, ghostCheckOverride: true, mind: mind);
  302. }
  303. }