2023-07-08 14:08:32 +10:00
|
|
|
using System.Numerics;
|
2023-03-31 07:49:25 +03:00
|
|
|
using Content.Server.Forensics;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Stack;
|
|
|
|
|
using Content.Shared.Prototypes;
|
2022-12-24 23:28:21 -05:00
|
|
|
using Content.Shared.Stacks;
|
2022-03-19 18:34:56 +13:00
|
|
|
using Robust.Shared.Prototypes;
|
2023-03-31 07:49:25 +03:00
|
|
|
using Robust.Shared.Random;
|
2022-03-19 18:34:56 +13:00
|
|
|
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Dictionary;
|
2020-12-23 13:34:57 +01:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Destructible.Thresholds.Behaviors
|
2020-12-23 13:34:57 +01:00
|
|
|
{
|
2021-02-05 13:41:05 +01:00
|
|
|
[Serializable]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataDefinition]
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class SpawnEntitiesBehavior : IThresholdBehavior
|
2020-12-23 13:34:57 +01:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Entities spawned on reaching this threshold, from a min to a max.
|
|
|
|
|
/// </summary>
|
2022-03-19 18:34:56 +13:00
|
|
|
[DataField("spawn", customTypeSerializer:typeof(PrototypeIdDictionarySerializer<MinMax, EntityPrototype>))]
|
2020-12-23 13:34:57 +01:00
|
|
|
public Dictionary<string, MinMax> Spawn { get; set; } = new();
|
|
|
|
|
|
2021-12-29 05:24:51 -04:00
|
|
|
[DataField("offset")]
|
|
|
|
|
public float Offset { get; set; } = 0.5f;
|
|
|
|
|
|
2023-03-31 07:49:25 +03:00
|
|
|
[DataField("transferForensics")]
|
|
|
|
|
public bool DoTransferForensics = false;
|
|
|
|
|
|
2023-02-10 17:45:38 -06:00
|
|
|
public void Execute(EntityUid owner, DestructibleSystem system, EntityUid? cause = null)
|
2020-12-23 13:34:57 +01:00
|
|
|
{
|
2021-11-09 21:24:35 +01:00
|
|
|
var position = system.EntityManager.GetComponent<TransformComponent>(owner).MapPosition;
|
2021-11-09 12:06:00 +01:00
|
|
|
|
2022-02-17 15:39:56 +13:00
|
|
|
var getRandomVector = () => new Vector2(system.Random.NextFloat(-Offset, Offset), system.Random.NextFloat(-Offset, Offset));
|
|
|
|
|
|
2020-12-23 13:34:57 +01:00
|
|
|
foreach (var (entityId, minMax) in Spawn)
|
|
|
|
|
{
|
|
|
|
|
var count = minMax.Min >= minMax.Max
|
|
|
|
|
? minMax.Min
|
|
|
|
|
: system.Random.Next(minMax.Min, minMax.Max + 1);
|
|
|
|
|
|
|
|
|
|
if (count == 0) continue;
|
|
|
|
|
|
2022-02-17 15:39:56 +13:00
|
|
|
if (EntityPrototypeHelpers.HasComponent<StackComponent>(entityId, system.PrototypeManager, system.ComponentFactory))
|
2020-12-23 13:34:57 +01:00
|
|
|
{
|
2022-02-17 15:39:56 +13:00
|
|
|
var spawned = system.EntityManager.SpawnEntity(entityId, position.Offset(getRandomVector()));
|
|
|
|
|
system.StackSystem.SetCount(spawned, count);
|
2023-03-31 07:49:25 +03:00
|
|
|
|
|
|
|
|
TransferForensics(spawned, system, owner);
|
2020-12-23 13:34:57 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < count; i++)
|
|
|
|
|
{
|
2023-03-31 07:49:25 +03:00
|
|
|
var spawned = system.EntityManager.SpawnEntity(entityId, position.Offset(getRandomVector()));
|
|
|
|
|
|
|
|
|
|
TransferForensics(spawned, system, owner);
|
2020-12-23 13:34:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-31 07:49:25 +03:00
|
|
|
|
|
|
|
|
public void TransferForensics(EntityUid spawned, DestructibleSystem system, EntityUid owner)
|
|
|
|
|
{
|
|
|
|
|
if (!DoTransferForensics ||
|
|
|
|
|
!system.EntityManager.TryGetComponent<ForensicsComponent>(owner, out var forensicsComponent))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var comp = system.EntityManager.EnsureComponent<ForensicsComponent>(spawned);
|
|
|
|
|
comp.DNAs = forensicsComponent.DNAs;
|
|
|
|
|
|
|
|
|
|
if (!system.Random.Prob(0.4f))
|
|
|
|
|
return;
|
|
|
|
|
comp.Fingerprints = forensicsComponent.Fingerprints;
|
|
|
|
|
comp.Fibers = forensicsComponent.Fibers;
|
|
|
|
|
}
|
2020-12-23 13:34:57 +01:00
|
|
|
}
|
|
|
|
|
}
|