Fix ghosts getting spawned in nullspace (#27617)

* Add tests for ghost spawn position

* Make ghosts spawn immediately

* Format mind system

* Move ghost spawning to GhostSystem

* Spawn ghost on grid or map

This fixes the ghosts being attached the parent entity instead of the grid.

* Move logging out of the ghost system

* Make round start observer spawn using GhostSystem

* Move GameTicker ghost spawning to GhostSystem

Moved the more robust character name selection code over.
Moved the TimeOfDeath code over.
Added canReturn logic.

* Add overrides and default for ghost spawn coordinates

* Add warning log to ghost spawn fail

* Clean up test

* Dont spawn ghost on map delete

* Minor changes to the role test

* Fix role test failing to spawn ghost

It was failing the map check due to using Nullspace

* Fix ghost tests when running in parallel

Not sure what happened, but it seems to be because they were running simultaneously and overwriting values.

* Clean up ghost tests

* Test that map deletion does not spawn ghosts

* Spawn ghost on the next available map

* Disallow spawning on deleted maps

* Fix map deletion ghost test

* Cleanup
This commit is contained in:
ShadowCommander
2024-05-11 08:03:40 -07:00
committed by GitHub
parent 742a1a5fbd
commit a985c5e83e
7 changed files with 261 additions and 99 deletions

View File

@@ -19,6 +19,7 @@ using Content.Shared.Movement.Systems;
using Content.Shared.Storage.Components;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.Map;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Player;
@@ -42,6 +43,8 @@ namespace Content.Server.Ghost
[Dependency] private readonly GameTicker _ticker = default!;
[Dependency] private readonly TransformSystem _transformSystem = default!;
[Dependency] private readonly VisibilitySystem _visibilitySystem = default!;
[Dependency] private readonly MetaDataSystem _metaData = default!;
[Dependency] private readonly IMapManager _mapManager = default!;
private EntityQuery<GhostComponent> _ghostQuery;
private EntityQuery<PhysicsComponent> _physicsQuery;
@@ -389,5 +392,59 @@ namespace Content.Server.Ghost
return ghostBoo.Handled;
}
public EntityUid? SpawnGhost(Entity<MindComponent?> mind, EntityUid targetEntity,
bool canReturn = false)
{
_transformSystem.TryGetMapOrGridCoordinates(targetEntity, out var spawnPosition);
return SpawnGhost(mind, spawnPosition, canReturn);
}
public EntityUid? SpawnGhost(Entity<MindComponent?> mind, EntityCoordinates? spawnPosition = null,
bool canReturn = false)
{
if (!Resolve(mind, ref mind.Comp))
return null;
// Test if the map is being deleted
var mapUid = spawnPosition?.GetMapUid(EntityManager);
if (mapUid == null || TerminatingOrDeleted(mapUid.Value))
spawnPosition = null;
spawnPosition ??= _ticker.GetObserverSpawnPoint();
if (!spawnPosition.Value.IsValid(EntityManager))
{
Log.Warning($"No spawn valid ghost spawn position found for {mind.Comp.CharacterName}"
+ " \"{ToPrettyString(mind)}\"");
_minds.TransferTo(mind.Owner, null, createGhost: false, mind: mind.Comp);
return null;
}
var ghost = SpawnAtPosition(GameTicker.ObserverPrototypeName, spawnPosition.Value);
var ghostComponent = Comp<GhostComponent>(ghost);
// Try setting the ghost entity name to either the character name or the player name.
// If all else fails, it'll default to the default entity prototype name, "observer".
// However, that should rarely happen.
if (!string.IsNullOrWhiteSpace(mind.Comp.CharacterName))
_metaData.SetEntityName(ghost, mind.Comp.CharacterName);
else if (!string.IsNullOrWhiteSpace(mind.Comp.Session?.Name))
_metaData.SetEntityName(ghost, mind.Comp.Session.Name);
if (mind.Comp.TimeOfDeath.HasValue)
{
SetTimeOfDeath(ghost, mind.Comp.TimeOfDeath!.Value, ghostComponent);
}
SetCanReturnToBody(ghostComponent, canReturn);
if (canReturn)
_minds.Visit(mind.Owner, ghost, mind.Comp);
else
_minds.TransferTo(mind.Owner, ghost, mind: mind.Comp);
Log.Debug($"Spawned ghost \"{ToPrettyString(ghost)}\" for {mind.Comp.CharacterName}.");
return ghost;
}
}
}