From af7f10c7beccebcc90ac188339856a6839b9bff3 Mon Sep 17 00:00:00 2001 From: Dmitriy Date: Thu, 2 May 2024 10:45:10 +0300 Subject: [PATCH] Autodelete items (and grids too) if near entity have HumanoidAppearance component. --- .../AutoDeleteItems/AutoDeleteComponent.cs | 31 +++++++++ .../AutoDeleteItems/AutoDeleteSystem.cs | 66 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 Content.Server/Corvax/AutoDeleteItems/AutoDeleteComponent.cs create mode 100644 Content.Server/Corvax/AutoDeleteItems/AutoDeleteSystem.cs diff --git a/Content.Server/Corvax/AutoDeleteItems/AutoDeleteComponent.cs b/Content.Server/Corvax/AutoDeleteItems/AutoDeleteComponent.cs new file mode 100644 index 00000000000..6df78ac1d96 --- /dev/null +++ b/Content.Server/Corvax/AutoDeleteItems/AutoDeleteComponent.cs @@ -0,0 +1,31 @@ +using Content.Shared.Timing; +using Robust.Shared.Audio; +using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom; + +[RegisterComponent] +[AutoGenerateComponentState, AutoGenerateComponentPause] +public sealed partial class AutoDeleteComponent : Component +{ + [DataField, ViewVariables(VVAccess.ReadWrite)] + public bool IsHumanoidNear = false; + + [DataField, ViewVariables(VVAccess.ReadWrite)] + public bool ReadyToDelete = false; + + [DataField, ViewVariables(VVAccess.ReadWrite)] + [AutoPausedField] + public TimeSpan NextTimeToDelete; + + [DataField, ViewVariables(VVAccess.ReadWrite)] + [AutoPausedField] + public TimeSpan NextTimeToCheck; + + [DataField, ViewVariables(VVAccess.ReadWrite)] + public TimeSpan DelayToCheck = TimeSpan.FromSeconds(10f); + + [DataField, ViewVariables(VVAccess.ReadWrite)] + public TimeSpan DelayToDelete = TimeSpan.FromSeconds(20f); + + [DataField, ViewVariables(VVAccess.ReadWrite)] + public int DistanceToCheck = 5; +} diff --git a/Content.Server/Corvax/AutoDeleteItems/AutoDeleteSystem.cs b/Content.Server/Corvax/AutoDeleteItems/AutoDeleteSystem.cs new file mode 100644 index 00000000000..252a361cc5f --- /dev/null +++ b/Content.Server/Corvax/AutoDeleteItems/AutoDeleteSystem.cs @@ -0,0 +1,66 @@ +using Content.Server.Construction.Completions; +using Content.Shared.Humanoid; +using Content.Shared.Mobs.Systems; +using Robust.Shared.Timing; + +namespace Content.Server.Corvax.AutoDeleteItems; + +public sealed class AutoDeleteSystem : EntitySystem +{ + [Dependency] private readonly EntityLookupSystem _lookup = default!; + [Dependency] private readonly IGameTiming _gameTiming = default!; + + public override void Initialize() + { + base.Initialize(); + } + + public override void Update(float frameTime) + { + base.Update(frameTime); + var query = EntityQueryEnumerator(); + while (query.MoveNext(out var uid, out var autoDeleteComponent)) + { + var xformQuery = GetEntityQuery(); + + if (!xformQuery.TryGetComponent(uid, out var xform) || + xform.MapUid == null) + { + return; + } + var humanoids = new HashSet>(); + + if (autoDeleteComponent.NextTimeToCheck > _gameTiming.CurTime) + return; + + if (_lookup.GetEntitiesInRangeInt(xform.Coordinates, autoDeleteComponent.DistanceToCheck, humanoids) > 0) + autoDeleteComponent.IsHumanoidNear = true; + else + autoDeleteComponent.IsHumanoidNear = false; + + if (autoDeleteComponent.IsHumanoidNear == false && autoDeleteComponent.ReadyToDelete == true && autoDeleteComponent.NextTimeToDelete < _gameTiming.CurTime) + { + EntityManager.DeleteEntity(uid); + } + + if (autoDeleteComponent.IsHumanoidNear == true) + { + if (autoDeleteComponent.ReadyToDelete == true) + autoDeleteComponent.ReadyToDelete = false; + + autoDeleteComponent.NextTimeToDelete = _gameTiming.CurTime + autoDeleteComponent.DelayToDelete; + autoDeleteComponent.NextTimeToCheck = _gameTiming.CurTime + autoDeleteComponent.DelayToCheck; + } + + if (autoDeleteComponent.IsHumanoidNear == false && autoDeleteComponent.ReadyToDelete == false) + { + autoDeleteComponent.NextTimeToDelete = _gameTiming.CurTime + autoDeleteComponent.DelayToDelete; + autoDeleteComponent.NextTimeToCheck = _gameTiming.CurTime + autoDeleteComponent.DelayToCheck; + autoDeleteComponent.ReadyToDelete = true; + } + + //autoDeleteComponent.NextTimeToDelete = _gameTiming.CurTime + autoDeleteComponent.DelayToDelete; + //autoDeleteComponent.NextTimeToCheck = _gameTiming.CurTime + autoDeleteComponent.DelayToCheck; + } + } +}