1
0

GhostSystem.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. using System.Linq;
  2. using System.Numerics;
  3. using Content.Server.Administration.Logs;
  4. using Content.Server.Chat.Managers;
  5. using Content.Server.GameTicking;
  6. using Content.Server.Ghost.Components;
  7. using Content.Server.Mind;
  8. using Content.Server.Roles.Jobs;
  9. using Content.Server.Warps;
  10. using Content.Shared.Actions;
  11. using Content.Shared.CCVar;
  12. using Content.Shared.Damage;
  13. using Content.Shared.Damage.Prototypes;
  14. using Content.Shared.Database;
  15. using Content.Shared.Examine;
  16. using Content.Shared.Eye;
  17. using Content.Shared.FixedPoint;
  18. using Content.Shared.Follower;
  19. using Content.Shared.Ghost;
  20. using Content.Shared.Mind;
  21. using Content.Shared.Mind.Components;
  22. using Content.Shared.Mobs;
  23. using Content.Shared.Mobs.Components;
  24. using Content.Shared.Mobs.Systems;
  25. using Content.Shared.Movement.Events;
  26. using Content.Shared.Movement.Systems;
  27. using Content.Shared.Popups;
  28. using Content.Shared.Storage.Components;
  29. using Content.Shared.Tag;
  30. using Robust.Server.GameObjects;
  31. using Robust.Server.Player;
  32. using Robust.Shared.Configuration;
  33. using Robust.Shared.Map;
  34. using Robust.Shared.Physics.Components;
  35. using Robust.Shared.Physics.Systems;
  36. using Robust.Shared.Player;
  37. using Robust.Shared.Prototypes;
  38. using Robust.Shared.Random;
  39. using Robust.Shared.Timing;
  40. namespace Content.Server.Ghost
  41. {
  42. public sealed class GhostSystem : SharedGhostSystem
  43. {
  44. [Dependency] private readonly SharedActionsSystem _actions = default!;
  45. [Dependency] private readonly IAdminLogManager _adminLog = default!;
  46. [Dependency] private readonly SharedEyeSystem _eye = default!;
  47. [Dependency] private readonly FollowerSystem _followerSystem = default!;
  48. [Dependency] private readonly IGameTiming _gameTiming = default!;
  49. [Dependency] private readonly JobSystem _jobs = default!;
  50. [Dependency] private readonly EntityLookupSystem _lookup = default!;
  51. [Dependency] private readonly MindSystem _minds = default!;
  52. [Dependency] private readonly MobStateSystem _mobState = default!;
  53. [Dependency] private readonly SharedPhysicsSystem _physics = default!;
  54. [Dependency] private readonly IPlayerManager _playerManager = default!;
  55. [Dependency] private readonly TransformSystem _transformSystem = default!;
  56. [Dependency] private readonly VisibilitySystem _visibilitySystem = default!;
  57. [Dependency] private readonly MetaDataSystem _metaData = default!;
  58. [Dependency] private readonly MobThresholdSystem _mobThresholdSystem = default!;
  59. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  60. [Dependency] private readonly IConfigurationManager _configurationManager = default!;
  61. [Dependency] private readonly IChatManager _chatManager = default!;
  62. [Dependency] private readonly SharedMindSystem _mind = default!;
  63. [Dependency] private readonly GameTicker _gameTicker = default!;
  64. [Dependency] private readonly DamageableSystem _damageable = default!;
  65. [Dependency] private readonly SharedPopupSystem _popup = default!;
  66. [Dependency] private readonly IRobustRandom _random = default!;
  67. [Dependency] private readonly TagSystem _tag = default!;
  68. private EntityQuery<GhostComponent> _ghostQuery;
  69. private EntityQuery<PhysicsComponent> _physicsQuery;
  70. public override void Initialize()
  71. {
  72. base.Initialize();
  73. _ghostQuery = GetEntityQuery<GhostComponent>();
  74. _physicsQuery = GetEntityQuery<PhysicsComponent>();
  75. SubscribeLocalEvent<GhostComponent, ComponentStartup>(OnGhostStartup);
  76. SubscribeLocalEvent<GhostComponent, MapInitEvent>(OnMapInit);
  77. SubscribeLocalEvent<GhostComponent, ComponentShutdown>(OnGhostShutdown);
  78. SubscribeLocalEvent<GhostComponent, ExaminedEvent>(OnGhostExamine);
  79. SubscribeLocalEvent<GhostComponent, MindRemovedMessage>(OnMindRemovedMessage);
  80. SubscribeLocalEvent<GhostComponent, MindUnvisitedMessage>(OnMindUnvisitedMessage);
  81. SubscribeLocalEvent<GhostComponent, PlayerDetachedEvent>(OnPlayerDetached);
  82. SubscribeLocalEvent<GhostOnMoveComponent, MoveInputEvent>(OnRelayMoveInput);
  83. SubscribeNetworkEvent<GhostWarpsRequestEvent>(OnGhostWarpsRequest);
  84. SubscribeNetworkEvent<GhostReturnToBodyRequest>(OnGhostReturnToBodyRequest);
  85. SubscribeNetworkEvent<GhostReturnToLobbyRequest>(OnGhostReturnToLobbyRequest);
  86. SubscribeNetworkEvent<GhostWarpToTargetRequestEvent>(OnGhostWarpToTargetRequest);
  87. SubscribeNetworkEvent<GhostnadoRequestEvent>(OnGhostnadoRequest);
  88. SubscribeLocalEvent<GhostComponent, BooActionEvent>(OnActionPerform);
  89. SubscribeLocalEvent<GhostComponent, ToggleGhostHearingActionEvent>(OnGhostHearingAction);
  90. SubscribeLocalEvent<GhostComponent, InsertIntoEntityStorageAttemptEvent>(OnEntityStorageInsertAttempt);
  91. SubscribeLocalEvent<RoundEndTextAppendEvent>(_ => MakeVisible(true));
  92. SubscribeLocalEvent<ToggleGhostVisibilityToAllEvent>(OnToggleGhostVisibilityToAll);
  93. SubscribeLocalEvent<GhostComponent, GetVisMaskEvent>(OnGhostVis);
  94. }
  95. private void OnGhostVis(Entity<GhostComponent> ent, ref GetVisMaskEvent args)
  96. {
  97. // If component not deleting they can see ghosts.
  98. if (ent.Comp.LifeStage <= ComponentLifeStage.Running)
  99. {
  100. args.VisibilityMask |= (int)VisibilityFlags.Ghost;
  101. }
  102. }
  103. private void OnGhostHearingAction(EntityUid uid, GhostComponent component, ToggleGhostHearingActionEvent args)
  104. {
  105. args.Handled = true;
  106. if (HasComp<GhostHearingComponent>(uid))
  107. {
  108. RemComp<GhostHearingComponent>(uid);
  109. _actions.SetToggled(component.ToggleGhostHearingActionEntity, true);
  110. }
  111. else
  112. {
  113. AddComp<GhostHearingComponent>(uid);
  114. _actions.SetToggled(component.ToggleGhostHearingActionEntity, false);
  115. }
  116. var str = HasComp<GhostHearingComponent>(uid)
  117. ? Loc.GetString("ghost-gui-toggle-hearing-popup-on")
  118. : Loc.GetString("ghost-gui-toggle-hearing-popup-off");
  119. Popup.PopupEntity(str, uid, uid);
  120. Dirty(uid, component);
  121. }
  122. private void OnActionPerform(EntityUid uid, GhostComponent component, BooActionEvent args)
  123. {
  124. if (args.Handled)
  125. return;
  126. var entities = _lookup.GetEntitiesInRange(args.Performer, component.BooRadius).ToList();
  127. // Shuffle the possible targets so we don't favor any particular entities
  128. _random.Shuffle(entities);
  129. var booCounter = 0;
  130. foreach (var ent in entities)
  131. {
  132. var handled = DoGhostBooEvent(ent);
  133. if (handled)
  134. booCounter++;
  135. if (booCounter >= component.BooMaxTargets)
  136. break;
  137. }
  138. if (booCounter == 0)
  139. _popup.PopupEntity(Loc.GetString("ghost-component-boo-action-failed"), uid, uid);
  140. args.Handled = true;
  141. }
  142. private void OnRelayMoveInput(EntityUid uid, GhostOnMoveComponent component, ref MoveInputEvent args)
  143. {
  144. // If they haven't actually moved then ignore it.
  145. if ((args.Entity.Comp.HeldMoveButtons &
  146. (MoveButtons.Down | MoveButtons.Left | MoveButtons.Up | MoveButtons.Right)) == 0x0)
  147. {
  148. return;
  149. }
  150. // Let's not ghost if our mind is visiting...
  151. if (HasComp<VisitingMindComponent>(uid))
  152. return;
  153. if (!_minds.TryGetMind(uid, out var mindId, out var mind) || mind.IsVisitingEntity)
  154. return;
  155. if (component.MustBeDead && (_mobState.IsAlive(uid) || _mobState.IsCritical(uid)))
  156. return;
  157. OnGhostAttempt(mindId, component.CanReturn, mind: mind);
  158. }
  159. private void OnGhostStartup(EntityUid uid, GhostComponent component, ComponentStartup args)
  160. {
  161. // Allow this entity to be seen by other ghosts.
  162. var visibility = EnsureComp<VisibilityComponent>(uid);
  163. if (_gameTicker.RunLevel != GameRunLevel.PostRound)
  164. {
  165. _visibilitySystem.AddLayer((uid, visibility), (int)VisibilityFlags.Ghost, false);
  166. _visibilitySystem.RemoveLayer((uid, visibility), (int)VisibilityFlags.Normal, false);
  167. _visibilitySystem.RefreshVisibility(uid, visibilityComponent: visibility);
  168. }
  169. _eye.RefreshVisibilityMask(uid);
  170. var time = _gameTiming.CurTime;
  171. component.TimeOfDeath = time;
  172. }
  173. private void OnGhostShutdown(EntityUid uid, GhostComponent component, ComponentShutdown args)
  174. {
  175. // Perf: If the entity is deleting itself, no reason to change these back.
  176. if (Terminating(uid))
  177. return;
  178. // Entity can't be seen by ghosts anymore.
  179. if (TryComp(uid, out VisibilityComponent? visibility))
  180. {
  181. _visibilitySystem.RemoveLayer((uid, visibility), (int)VisibilityFlags.Ghost, false);
  182. _visibilitySystem.AddLayer((uid, visibility), (int)VisibilityFlags.Normal, false);
  183. _visibilitySystem.RefreshVisibility(uid, visibilityComponent: visibility);
  184. }
  185. // Entity can't see ghosts anymore.
  186. _eye.RefreshVisibilityMask(uid);
  187. _actions.RemoveAction(uid, component.BooActionEntity);
  188. }
  189. private void OnMapInit(EntityUid uid, GhostComponent component, MapInitEvent args)
  190. {
  191. _actions.AddAction(uid, ref component.BooActionEntity, component.BooAction);
  192. _actions.AddAction(uid, ref component.ToggleGhostHearingActionEntity, component.ToggleGhostHearingAction);
  193. _actions.AddAction(uid, ref component.ToggleLightingActionEntity, component.ToggleLightingAction);
  194. _actions.AddAction(uid, ref component.ToggleFoVActionEntity, component.ToggleFoVAction);
  195. _actions.AddAction(uid, ref component.ToggleGhostsActionEntity, component.ToggleGhostsAction);
  196. }
  197. private void OnGhostExamine(EntityUid uid, GhostComponent component, ExaminedEvent args)
  198. {
  199. var timeSinceDeath = _gameTiming.RealTime.Subtract(component.TimeOfDeath);
  200. var deathTimeInfo = timeSinceDeath.Minutes > 0
  201. ? Loc.GetString("comp-ghost-examine-time-minutes", ("minutes", timeSinceDeath.Minutes))
  202. : Loc.GetString("comp-ghost-examine-time-seconds", ("seconds", timeSinceDeath.Seconds));
  203. args.PushMarkup(deathTimeInfo);
  204. }
  205. #region Ghost Deletion
  206. private void OnMindRemovedMessage(EntityUid uid, GhostComponent component, MindRemovedMessage args)
  207. {
  208. DeleteEntity(uid);
  209. }
  210. private void OnMindUnvisitedMessage(EntityUid uid, GhostComponent component, MindUnvisitedMessage args)
  211. {
  212. DeleteEntity(uid);
  213. }
  214. private void OnPlayerDetached(EntityUid uid, GhostComponent component, PlayerDetachedEvent args)
  215. {
  216. DeleteEntity(uid);
  217. }
  218. private void DeleteEntity(EntityUid uid)
  219. {
  220. if (Deleted(uid) || Terminating(uid))
  221. return;
  222. QueueDel(uid);
  223. }
  224. #endregion
  225. private void OnGhostReturnToBodyRequest(GhostReturnToBodyRequest msg, EntitySessionEventArgs args)
  226. {
  227. if (args.SenderSession.AttachedEntity is not { Valid: true } attached
  228. || !_ghostQuery.TryComp(attached, out var ghost)
  229. || !ghost.CanReturnToBody
  230. || !TryComp(attached, out ActorComponent? actor))
  231. {
  232. Log.Warning($"User {args.SenderSession.Name} sent an invalid {nameof(GhostReturnToBodyRequest)}");
  233. return;
  234. }
  235. _mind.UnVisit(actor.PlayerSession);
  236. }
  237. private void OnGhostReturnToLobbyRequest(GhostReturnToLobbyRequest msg, EntitySessionEventArgs args)
  238. {
  239. _gameTicker.PlayerJoinLobby(args.SenderSession);
  240. }
  241. #region Warp
  242. private void OnGhostWarpsRequest(GhostWarpsRequestEvent msg, EntitySessionEventArgs args)
  243. {
  244. if (args.SenderSession.AttachedEntity is not { Valid: true } entity
  245. || !_ghostQuery.HasComp(entity))
  246. {
  247. Log.Warning($"User {args.SenderSession.Name} sent a {nameof(GhostWarpsRequestEvent)} without being a ghost.");
  248. return;
  249. }
  250. var response = new GhostWarpsResponseEvent(GetPlayerWarps(entity).Concat(GetLocationWarps()).ToList());
  251. RaiseNetworkEvent(response, args.SenderSession.Channel);
  252. }
  253. private void OnGhostWarpToTargetRequest(GhostWarpToTargetRequestEvent msg, EntitySessionEventArgs args)
  254. {
  255. if (args.SenderSession.AttachedEntity is not { Valid: true } attached
  256. || !_ghostQuery.HasComp(attached))
  257. {
  258. Log.Warning($"User {args.SenderSession.Name} tried to warp to {msg.Target} without being a ghost.");
  259. return;
  260. }
  261. var target = GetEntity(msg.Target);
  262. if (!Exists(target))
  263. {
  264. Log.Warning($"User {args.SenderSession.Name} tried to warp to an invalid entity id: {msg.Target}");
  265. return;
  266. }
  267. WarpTo(attached, target);
  268. }
  269. private void OnGhostnadoRequest(GhostnadoRequestEvent msg, EntitySessionEventArgs args)
  270. {
  271. if (args.SenderSession.AttachedEntity is not { } uid
  272. || !_ghostQuery.HasComp(uid))
  273. {
  274. Log.Warning($"User {args.SenderSession.Name} tried to ghostnado without being a ghost.");
  275. return;
  276. }
  277. if (_followerSystem.GetMostGhostFollowed() is not { } target)
  278. return;
  279. WarpTo(uid, target);
  280. }
  281. private void WarpTo(EntityUid uid, EntityUid target)
  282. {
  283. _adminLog.Add(LogType.GhostWarp, $"{ToPrettyString(uid)} ghost warped to {ToPrettyString(target)}");
  284. if ((TryComp(target, out WarpPointComponent? warp) && warp.Follow) || HasComp<MobStateComponent>(target))
  285. {
  286. _followerSystem.StartFollowingEntity(uid, target);
  287. return;
  288. }
  289. var xform = Transform(uid);
  290. _transformSystem.SetCoordinates(uid, xform, Transform(target).Coordinates);
  291. _transformSystem.AttachToGridOrMap(uid, xform);
  292. if (_physicsQuery.TryComp(uid, out var physics))
  293. _physics.SetLinearVelocity(uid, Vector2.Zero, body: physics);
  294. }
  295. private IEnumerable<GhostWarp> GetLocationWarps()
  296. {
  297. var allQuery = AllEntityQuery<WarpPointComponent>();
  298. while (allQuery.MoveNext(out var uid, out var warp))
  299. {
  300. yield return new GhostWarp(GetNetEntity(uid), warp.Location ?? Name(uid), true);
  301. }
  302. }
  303. private IEnumerable<GhostWarp> GetPlayerWarps(EntityUid except)
  304. {
  305. foreach (var player in _playerManager.Sessions)
  306. {
  307. if (player.AttachedEntity is not { Valid: true } attached)
  308. continue;
  309. if (attached == except) continue;
  310. TryComp<MindContainerComponent>(attached, out var mind);
  311. var jobName = _jobs.MindTryGetJobName(mind?.Mind);
  312. var playerInfo = $"{Comp<MetaDataComponent>(attached).EntityName} ({jobName})";
  313. if (_mobState.IsAlive(attached) || _mobState.IsCritical(attached))
  314. yield return new GhostWarp(GetNetEntity(attached), playerInfo, false);
  315. }
  316. }
  317. #endregion
  318. private void OnEntityStorageInsertAttempt(EntityUid uid, GhostComponent comp, ref InsertIntoEntityStorageAttemptEvent args)
  319. {
  320. args.Cancelled = true;
  321. }
  322. private void OnToggleGhostVisibilityToAll(ToggleGhostVisibilityToAllEvent ev)
  323. {
  324. if (ev.Handled)
  325. return;
  326. ev.Handled = true;
  327. MakeVisible(true);
  328. }
  329. /// <summary>
  330. /// When the round ends, make all players able to see ghosts.
  331. /// </summary>
  332. public void MakeVisible(bool visible)
  333. {
  334. var entityQuery = EntityQueryEnumerator<GhostComponent, VisibilityComponent>();
  335. while (entityQuery.MoveNext(out var uid, out var _, out var vis))
  336. {
  337. if (!_tag.HasTag(uid, "AllowGhostShownByEvent"))
  338. continue;
  339. if (visible)
  340. {
  341. _visibilitySystem.AddLayer((uid, vis), (int)VisibilityFlags.Normal, false);
  342. _visibilitySystem.RemoveLayer((uid, vis), (int)VisibilityFlags.Ghost, false);
  343. }
  344. else
  345. {
  346. _visibilitySystem.AddLayer((uid, vis), (int)VisibilityFlags.Ghost, false);
  347. _visibilitySystem.RemoveLayer((uid, vis), (int)VisibilityFlags.Normal, false);
  348. }
  349. _visibilitySystem.RefreshVisibility(uid, visibilityComponent: vis);
  350. }
  351. }
  352. public bool DoGhostBooEvent(EntityUid target)
  353. {
  354. var ghostBoo = new GhostBooEvent();
  355. RaiseLocalEvent(target, ghostBoo, true);
  356. return ghostBoo.Handled;
  357. }
  358. public EntityUid? SpawnGhost(Entity<MindComponent?> mind, EntityUid targetEntity,
  359. bool canReturn = false)
  360. {
  361. _transformSystem.TryGetMapOrGridCoordinates(targetEntity, out var spawnPosition);
  362. return SpawnGhost(mind, spawnPosition, canReturn);
  363. }
  364. private bool IsValidSpawnPosition(EntityCoordinates? spawnPosition)
  365. {
  366. if (spawnPosition?.IsValid(EntityManager) != true)
  367. return false;
  368. var mapUid = spawnPosition?.GetMapUid(EntityManager);
  369. var gridUid = spawnPosition?.EntityId;
  370. // Test if the map is being deleted
  371. if (mapUid == null || TerminatingOrDeleted(mapUid.Value))
  372. return false;
  373. // Test if the grid is being deleted
  374. if (gridUid != null && TerminatingOrDeleted(gridUid.Value))
  375. return false;
  376. return true;
  377. }
  378. public EntityUid? SpawnGhost(Entity<MindComponent?> mind, EntityCoordinates? spawnPosition = null,
  379. bool canReturn = false)
  380. {
  381. if (!Resolve(mind, ref mind.Comp))
  382. return null;
  383. // Test if the map or grid is being deleted
  384. if (!IsValidSpawnPosition(spawnPosition))
  385. spawnPosition = null;
  386. // If it's bad, look for a valid point to spawn
  387. spawnPosition ??= _gameTicker.GetObserverSpawnPoint();
  388. // Make sure the new point is valid too
  389. if (!IsValidSpawnPosition(spawnPosition))
  390. {
  391. Log.Warning($"No spawn valid ghost spawn position found for {mind.Comp.CharacterName}"
  392. + $" \"{ToPrettyString(mind)}\"");
  393. _minds.TransferTo(mind.Owner, null, createGhost: false, mind: mind.Comp);
  394. return null;
  395. }
  396. var ghost = SpawnAtPosition(GameTicker.ObserverPrototypeName, spawnPosition.Value);
  397. var ghostComponent = Comp<GhostComponent>(ghost);
  398. // Try setting the ghost entity name to either the character name or the player name.
  399. // If all else fails, it'll default to the default entity prototype name, "observer".
  400. // However, that should rarely happen.
  401. if (!string.IsNullOrWhiteSpace(mind.Comp.CharacterName))
  402. _metaData.SetEntityName(ghost, mind.Comp.CharacterName);
  403. else if (!string.IsNullOrWhiteSpace(mind.Comp.Session?.Name))
  404. _metaData.SetEntityName(ghost, mind.Comp.Session.Name);
  405. if (mind.Comp.TimeOfDeath.HasValue)
  406. {
  407. SetTimeOfDeath(ghost, mind.Comp.TimeOfDeath!.Value, ghostComponent);
  408. }
  409. SetCanReturnToBody(ghostComponent, canReturn);
  410. if (canReturn)
  411. _minds.Visit(mind.Owner, ghost, mind.Comp);
  412. else
  413. _minds.TransferTo(mind.Owner, ghost, mind: mind.Comp);
  414. Log.Debug($"Spawned ghost \"{ToPrettyString(ghost)}\" for {mind.Comp.CharacterName}.");
  415. return ghost;
  416. }
  417. public bool OnGhostAttempt(EntityUid mindId, bool canReturnGlobal, bool viaCommand = false, bool forced = false, MindComponent? mind = null)
  418. {
  419. if (!Resolve(mindId, ref mind))
  420. return false;
  421. var playerEntity = mind.CurrentEntity;
  422. if (playerEntity != null && viaCommand)
  423. {
  424. if (forced)
  425. _adminLog.Add(LogType.Mind, $"{EntityManager.ToPrettyString(playerEntity.Value):player} was forced to ghost via command");
  426. else
  427. _adminLog.Add(LogType.Mind, $"{EntityManager.ToPrettyString(playerEntity.Value):player} is attempting to ghost via command");
  428. }
  429. var handleEv = new GhostAttemptHandleEvent(mind, canReturnGlobal);
  430. RaiseLocalEvent(handleEv);
  431. // Something else has handled the ghost attempt for us! We return its result.
  432. if (handleEv.Handled)
  433. return handleEv.Result;
  434. if (mind.PreventGhosting && !forced)
  435. {
  436. if (mind.Session != null) // Logging is suppressed to prevent spam from ghost attempts caused by movement attempts
  437. {
  438. _chatManager.DispatchServerMessage(mind.Session, Loc.GetString("comp-mind-ghosting-prevented"),
  439. true);
  440. }
  441. return false;
  442. }
  443. if (TryComp<GhostComponent>(playerEntity, out var comp) && !comp.CanGhostInteract)
  444. return false;
  445. if (mind.VisitingEntity != default)
  446. {
  447. _mind.UnVisit(mindId, mind: mind);
  448. }
  449. var position = Exists(playerEntity)
  450. ? Transform(playerEntity.Value).Coordinates
  451. : _gameTicker.GetObserverSpawnPoint();
  452. if (position == default)
  453. return false;
  454. // Ok, so, this is the master place for the logic for if ghosting is "too cheaty" to allow returning.
  455. // There's no reason at this time to move it to any other place, especially given that the 'side effects required' situations would also have to be moved.
  456. // + If CharacterDeadPhysically applies, we're physically dead. Therefore, ghosting OK, and we can return (this is critical for gibbing)
  457. // Note that we could theoretically be ICly dead and still physically alive and vice versa.
  458. // (For example, a zombie could be dead ICly, but may retain memories and is definitely physically active)
  459. // + If we're in a mob that is critical, and we're supposed to be able to return if possible,
  460. // we're succumbing - the mob is killed. Therefore, character is dead. Ghosting OK.
  461. // (If the mob survives, that's a bug. Ghosting is kept regardless.)
  462. var canReturn = canReturnGlobal && _mind.IsCharacterDeadPhysically(mind);
  463. if (_configurationManager.GetCVar(CCVars.GhostKillCrit) &&
  464. canReturnGlobal &&
  465. TryComp(playerEntity, out MobStateComponent? mobState))
  466. {
  467. if (_mobState.IsCritical(playerEntity.Value, mobState))
  468. {
  469. canReturn = true;
  470. //todo: what if they dont breathe lol
  471. //cry deeply
  472. FixedPoint2 dealtDamage = 200;
  473. if (TryComp<DamageableComponent>(playerEntity, out var damageable)
  474. && TryComp<MobThresholdsComponent>(playerEntity, out var thresholds))
  475. {
  476. var playerDeadThreshold = _mobThresholdSystem.GetThresholdForState(playerEntity.Value, MobState.Dead, thresholds);
  477. dealtDamage = playerDeadThreshold - damageable.TotalDamage;
  478. }
  479. DamageSpecifier damage = new(_prototypeManager.Index<DamageTypePrototype>("Asphyxiation"), dealtDamage);
  480. _damageable.TryChangeDamage(playerEntity, damage, true);
  481. }
  482. }
  483. if (playerEntity != null)
  484. _adminLog.Add(LogType.Mind, $"{EntityManager.ToPrettyString(playerEntity.Value):player} ghosted{(!canReturn ? " (non-returnable)" : "")}");
  485. var ghost = SpawnGhost((mindId, mind), position, canReturn);
  486. if (ghost == null)
  487. return false;
  488. return true;
  489. }
  490. }
  491. public sealed class GhostAttemptHandleEvent(MindComponent mind, bool canReturnGlobal) : HandledEntityEventArgs
  492. {
  493. public MindComponent Mind { get; } = mind;
  494. public bool CanReturnGlobal { get; } = canReturnGlobal;
  495. public bool Result { get; set; }
  496. }
  497. }