using Content.Server.Objectives.Components;
using Content.Server.Shuttles.Systems;
using Content.Shared.Cuffs.Components;
using Content.Shared.Mind;
using Content.Shared.Objectives.Components;
namespace Content.Server.Objectives.Systems;
///
/// Handles escaping on the shuttle while being another person detection.
///
public sealed class ImpersonateConditionSystem : EntitySystem
{
[Dependency] private readonly TargetObjectiveSystem _target = default!;
[Dependency] private readonly EmergencyShuttleSystem _emergencyShuttle = default!;
[Dependency] private readonly SharedMindSystem _mind = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent(OnAfterAssign);
SubscribeLocalEvent(OnGetProgress);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator();
while (query.MoveNext(out var _, out var comp))
{
if (comp.Name == null || comp.MindId == null
|| !TryComp(comp.MindId, out var mind) || mind.OwnedEntity == null
|| !TryComp(mind.CurrentEntity, out var metaData))
continue;
comp.Completed = metaData.EntityName == comp.Name;
}
}
private void OnAfterAssign(EntityUid uid, ImpersonateConditionComponent comp, ref ObjectiveAfterAssignEvent args)
{
if (!_target.GetTarget(uid, out var target)
|| !TryComp(target, out var targetMind) || targetMind.CharacterName == null)
return;
comp.Name = targetMind.CharacterName;
comp.MindId = args.MindId;
}
// copypasta from escape shittle objective. eh.
private void OnGetProgress(EntityUid uid, ImpersonateConditionComponent comp, ref ObjectiveGetProgressEvent args)
{
args.Progress = GetProgress(args.Mind, comp);
}
public float GetProgress(MindComponent mind, ImpersonateConditionComponent comp)
{
// not escaping alive if you're deleted/dead
if (mind.OwnedEntity == null || _mind.IsCharacterDeadIc(mind))
return 0f;
// You're not escaping if you're restrained!
if (TryComp(mind.OwnedEntity, out var cuffed) && cuffed.CuffedHandCount > 0)
return 0f;
return (_emergencyShuttle.IsTargetEscaping(mind.OwnedEntity.Value) ? .5f : 0f) + (comp.Completed ? .5f : 0f);
}
}