Cloning Rework (#8972)

Co-authored-by: fishfish458 <fishfish458>
This commit is contained in:
Rane
2022-08-04 00:05:17 -04:00
committed by GitHub
parent 37f9e825ea
commit 2f4849eae1
41 changed files with 928 additions and 1227 deletions

View File

@@ -1,36 +1,63 @@
using System.Linq;
using Content.Server.Cloning.Components;
using Content.Server.Mind.Components;
using Content.Server.Power.Components;
using Content.Shared.Audio;
using Content.Server.Power.EntitySystems;
using Content.Shared.GameTicking;
using Content.Shared.Preferences;
using Robust.Shared.Timing;
using static Content.Shared.Cloning.SharedCloningPodComponent;
using Content.Shared.CharacterAppearance.Systems;
using Content.Shared.CharacterAppearance.Components;
using Content.Shared.Species;
using Robust.Server.Player;
using Robust.Shared.Prototypes;
using Content.Server.EUI;
using Robust.Shared.Containers;
using Robust.Server.Containers;
using Content.Shared.Cloning;
using Content.Server.MachineLinking.System;
using Content.Server.MachineLinking.Events;
using Content.Server.MobState;
namespace Content.Server.Cloning
namespace Content.Server.Cloning.Systems
{
internal sealed class CloningSystem : EntitySystem
public sealed class CloningSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _timing = default!;
public readonly Dictionary<Mind.Mind, int> MindToId = new();
public readonly Dictionary<int, ClonerDNAEntry> IdToDNA = new();
private int _nextAllocatedMindId = 0;
[Dependency] private readonly SignalLinkerSystem _signalSystem = default!;
[Dependency] private readonly IPlayerManager _playerManager = null!;
[Dependency] private readonly IPrototypeManager _prototype = default!;
[Dependency] private readonly EuiManager _euiManager = null!;
[Dependency] private readonly CloningConsoleSystem _cloningConsoleSystem = default!;
[Dependency] private readonly SharedHumanoidAppearanceSystem _appearanceSystem = default!;
[Dependency] private readonly ContainerSystem _containerSystem = default!;
[Dependency] private readonly MobStateSystem _mobStateSystem = default!;
[Dependency] private readonly PowerReceiverSystem _powerReceiverSystem = default!;
public readonly Dictionary<Mind.Mind, EntityUid> ClonesWaitingForMind = new();
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<CloningPodComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<RoundRestartCleanupEvent>(Reset);
SubscribeLocalEvent<BeingClonedComponent, MindAddedMessage>(HandleMindAdded);
SubscribeLocalEvent<CloningPodComponent, PortDisconnectedEvent>(OnPortDisconnected);
SubscribeLocalEvent<CloningPodComponent, AnchorStateChangedEvent>(OnAnchor);
}
private void OnComponentInit(EntityUid uid, CloningPodComponent clonePod, ComponentInit args)
{
clonePod.BodyContainer = _containerSystem.EnsureContainer<ContainerSlot>(clonePod.Owner, $"{Name}-bodyContainer");
_signalSystem.EnsureReceiverPorts(uid, CloningPodComponent.PodPort);
}
private void UpdateAppearance(CloningPodComponent clonePod)
{
if (TryComp<AppearanceComponent>(clonePod.Owner, out var appearance))
appearance.SetData(CloningPodVisuals.Status, clonePod.Status);
}
internal void TransferMindToClone(Mind.Mind mind)
{
if (!ClonesWaitingForMind.TryGetValue(mind, out var entity) ||
!EntityManager.EntityExists(entity) ||
!EntityManager.TryGetComponent(entity, out MindComponent? mindComp) ||
!TryComp<MindComponent>(entity, out var mindComp) ||
mindComp.Mind != null)
return;
@@ -39,33 +66,94 @@ namespace Content.Server.Cloning
ClonesWaitingForMind.Remove(mind);
}
private void HandleMindAdded(EntityUid uid, BeingClonedComponent component, MindAddedMessage message)
private void HandleMindAdded(EntityUid uid, BeingClonedComponent clonedComponent, MindAddedMessage message)
{
if (component.Parent == EntityUid.Invalid ||
!EntityManager.EntityExists(component.Parent) ||
!EntityManager.TryGetComponent<CloningPodComponent?>(component.Parent, out var cloningPodComponent) ||
component.Owner != cloningPodComponent.BodyContainer?.ContainedEntity)
if (clonedComponent.Parent == EntityUid.Invalid ||
!EntityManager.EntityExists(clonedComponent.Parent) ||
!TryComp<CloningPodComponent>(clonedComponent.Parent, out var cloningPodComponent) ||
clonedComponent.Owner != cloningPodComponent.BodyContainer?.ContainedEntity)
{
EntityManager.RemoveComponent<BeingClonedComponent>(component.Owner);
EntityManager.RemoveComponent<BeingClonedComponent>(clonedComponent.Owner);
return;
}
UpdateStatus(CloningPodStatus.Cloning, cloningPodComponent);
}
cloningPodComponent.UpdateStatus(CloningPodStatus.Cloning);
private void OnPortDisconnected(EntityUid uid, CloningPodComponent pod, PortDisconnectedEvent args)
{
pod.ConnectedConsole = null;
}
private void OnAnchor(EntityUid uid, CloningPodComponent component, ref AnchorStateChangedEvent args)
{
if (component.ConnectedConsole == null || !TryComp<CloningConsoleComponent>(component.ConnectedConsole, out var console))
return;
if (args.Anchored)
{
_cloningConsoleSystem.RecheckConnections(component.ConnectedConsole.Value, uid, console.GeneticScanner, console);
return;
}
_cloningConsoleSystem.UpdateUserInterface(console);
}
public bool TryCloning(EntityUid uid, EntityUid bodyToClone, Mind.Mind mind, CloningPodComponent? clonePod)
{
if (!Resolve(uid, ref clonePod) || bodyToClone == null)
return false;
if (ClonesWaitingForMind.TryGetValue(mind, out var clone))
{
if (EntityManager.EntityExists(clone) &&
!_mobStateSystem.IsDead(clone) &&
TryComp<MindComponent>(clone, out var cloneMindComp) &&
(cloneMindComp.Mind == null || cloneMindComp.Mind == mind))
return false; // Mind already has clone
ClonesWaitingForMind.Remove(mind);
}
if (mind.OwnedEntity != null && !_mobStateSystem.IsDead(mind.OwnedEntity.Value))
return false; // Body controlled by mind is not dead
// Yes, we still need to track down the client because we need to open the Eui
if (mind.UserId == null || !_playerManager.TryGetSessionById(mind.UserId.Value, out var client))
return false; // If we can't track down the client, we can't offer transfer. That'd be quite bad.
if (!TryComp<HumanoidAppearanceComponent>(bodyToClone, out var humanoid))
return false; // whatever body was to be cloned, was not a humanoid
var speciesProto = _prototype.Index<SpeciesPrototype>(humanoid.Species).Prototype;
var mob = Spawn(speciesProto, Transform(clonePod.Owner).MapPosition);
_appearanceSystem.UpdateAppearance(mob, humanoid.Appearance);
_appearanceSystem.UpdateSexGender(mob, humanoid.Sex, humanoid.Gender);
MetaData(mob).EntityName = MetaData(bodyToClone).EntityName;
var cloneMindReturn = EntityManager.AddComponent<BeingClonedComponent>(mob);
cloneMindReturn.Mind = mind;
cloneMindReturn.Parent = clonePod.Owner;
clonePod.BodyContainer.Insert(mob);
clonePod.CapturedMind = mind;
ClonesWaitingForMind.Add(mind, mob);
UpdateStatus(CloningPodStatus.NoMind, clonePod);
_euiManager.OpenEui(new AcceptCloningEui(mind, this), client);
AddComp<ActiveCloningPodComponent>(uid);
return true;
}
public void UpdateStatus(CloningPodStatus status, CloningPodComponent cloningPod)
{
cloningPod.Status = status;
UpdateAppearance(cloningPod);
}
public override void Update(float frameTime)
{
// TODO: Make this stateful
foreach (var (cloning, power) in EntityManager.EntityQuery<CloningPodComponent, ApcPowerReceiverComponent>())
foreach (var (_, cloning) in EntityManager.EntityQuery<ActiveCloningPodComponent, CloningPodComponent>())
{
if (cloning.UiKnownPowerState != power.Powered)
{
// Must be *before* update
cloning.UiKnownPowerState = power.Powered;
UpdateUserInterface(cloning);
}
if (!power.Powered)
if (!_powerReceiverSystem.IsPowered(cloning.Owner))
continue;
if (cloning.BodyContainer.ContainedEntity != null)
@@ -76,77 +164,30 @@ namespace Content.Server.Cloning
if (cloning.CapturedMind?.Session?.AttachedEntity == cloning.BodyContainer.ContainedEntity)
{
cloning.Eject();
Eject(cloning.Owner, cloning);
}
}
}
public void UpdateUserInterface(CloningPodComponent comp)
public void Eject(EntityUid uid, CloningPodComponent? clonePod)
{
var idToUser = GetIdToUser();
comp.UserInterface?.SetState(
new CloningPodBoundUserInterfaceState(
idToUser,
// now
_timing.CurTime,
// progress, time, progressing
comp.CloningProgress,
comp.CloningTime,
// this is duplicate w/ the above check that actually updates progress
// better here than on client though
comp.UiKnownPowerState && (comp.BodyContainer.ContainedEntity != null),
comp.Status == CloningPodStatus.Cloning));
}
if (!Resolve(uid, ref clonePod))
return;
public void AddToDnaScans(ClonerDNAEntry dna)
{
if (!MindToId.ContainsKey(dna.Mind))
{
int id = _nextAllocatedMindId++;
MindToId.Add(dna.Mind, id);
IdToDNA.Add(id, dna);
}
OnChangeMadeToDnaScans();
}
if (clonePod.BodyContainer.ContainedEntity is not {Valid: true} entity || clonePod.CloningProgress < clonePod.CloningTime)
return;
public void OnChangeMadeToDnaScans()
{
foreach (var cloning in EntityManager.EntityQuery<CloningPodComponent>())
UpdateUserInterface(cloning);
}
public bool HasDnaScan(Mind.Mind mind)
{
return MindToId.ContainsKey(mind);
}
public Dictionary<int, string?> GetIdToUser()
{
return IdToDNA.ToDictionary(m => m.Key, m => m.Value.Mind.CharacterName);
EntityManager.RemoveComponent<BeingClonedComponent>(entity);
clonePod.BodyContainer.Remove(entity);
clonePod.CapturedMind = null;
clonePod.CloningProgress = 0f;
UpdateStatus(CloningPodStatus.Idle, clonePod);
RemCompDeferred<ActiveCloningPodComponent>(uid);
}
public void Reset(RoundRestartCleanupEvent ev)
{
MindToId.Clear();
IdToDNA.Clear();
ClonesWaitingForMind.Clear();
_nextAllocatedMindId = 0;
// We PROBABLY don't need to send out UI interface updates for the dna scan changes during a reset
}
}
// TODO: This needs to be moved to Content.Server.Mobs and made a global point of reference.
// For example, GameTicker should be using this, and this should be using ICharacterProfile rather than HumanoidCharacterProfile.
// It should carry a reference or copy of itself with the mobs that it affects.
// See TODO in MedicalScannerComponent.
public struct ClonerDNAEntry {
public Mind.Mind Mind;
public HumanoidCharacterProfile Profile;
public ClonerDNAEntry(Mind.Mind m, HumanoidCharacterProfile hcp)
{
Mind = m;
Profile = hcp;
}
}
}