GhostSystem.cs 25 KB

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