Files
tbd-station-14/Content.Server/Destructible/Thresholds/Behaviors/SpawnEntitiesBehavior.cs
āda 8d8af1bab7 Stack System Cleanup (#38872)
* eye on the prize

* OnStackInteractUsing, TryMergeStacks, TryMergeToHands, TryMergeToContacts

* namespace

* Use, get count, getMaxCount

* component access

* add regions, mark TODO

* obsolete TryAdd, public TryMergeStacks

* GetMaxCount

* event handlers

* event handlers

* SetCount

* client server event handlers

* move to shared

* Revert "move to shared"

This reverts commit 45540a2d6b8e1e6d2a8f83a584267776c7edcd73.

* misc changes to shared

* split

* spawn and SpawnNextToOrDrop

* SpawnMultipleAtPosition, SpawnMultipleNextToOrDrop, CalculateSpawns, general server cleanup

* Rename Use to TryUse.

* Small misc changes

* Remove obsolete functions

* Remove some SetCount calls

* Partialize

* small misc change

* don't nuke the git dif with the namespace block

* Comments and reordering

* touchup to UpdateLingering

* Summary comment for StackStatusControl

* Last pass

* Actual last pass (for now)

* I know myself too well

* fixup

* goodbye lingering

* fixes

* review

* fix test

* second look

* fix test

* forgot

* remove early comp getting

---------

Co-authored-by: iaada <iaada@users.noreply.github.com>
Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
2025-10-25 14:40:48 +00:00

96 lines
3.6 KiB
C#

using System.Numerics;
using Content.Server.Forensics;
using Content.Server.Stack;
using Content.Shared.Destructible.Thresholds;
using Content.Shared.Prototypes;
using Content.Shared.Stacks;
using Robust.Server.GameObjects;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Server.Destructible.Thresholds.Behaviors
{
[Serializable]
[DataDefinition]
public sealed partial class SpawnEntitiesBehavior : IThresholdBehavior
{
/// <summary>
/// Entities spawned on reaching this threshold, from a min to a max.
/// </summary>
[DataField]
public Dictionary<EntProtoId, MinMax> Spawn = new();
[DataField("offset")]
public float Offset { get; set; } = 0.5f;
[DataField("transferForensics")]
public bool DoTransferForensics;
[DataField]
public bool SpawnInContainer;
public void Execute(EntityUid owner, DestructibleSystem system, EntityUid? cause = null)
{
var tSys = system.EntityManager.System<TransformSystem>();
var position = tSys.GetMapCoordinates(owner);
var getRandomVector = () => new Vector2(system.Random.NextFloat(-Offset, Offset), system.Random.NextFloat(-Offset, Offset));
var executions = 1;
if (system.EntityManager.TryGetComponent<StackComponent>(owner, out var stack))
{
executions = stack.Count;
}
foreach (var (entityId, minMax) in Spawn)
{
for (var execution = 0; execution < executions; execution++)
{
var count = minMax.Min >= minMax.Max
? minMax.Min
: system.Random.Next(minMax.Min, minMax.Max + 1);
if (count == 0)
continue;
if (EntityPrototypeHelpers.HasComponent<StackComponent>(entityId, system.PrototypeManager, system.EntityManager.ComponentFactory))
{
var spawned = SpawnInContainer
? system.EntityManager.SpawnNextToOrDrop(entityId, owner)
: system.EntityManager.SpawnEntity(entityId, position.Offset(getRandomVector()));
system.StackSystem.SetCount((spawned, null), count);
TransferForensics(spawned, system, owner);
}
else
{
for (var i = 0; i < count; i++)
{
var spawned = SpawnInContainer
? system.EntityManager.SpawnNextToOrDrop(entityId, owner)
: system.EntityManager.SpawnEntity(entityId, position.Offset(getRandomVector()));
TransferForensics(spawned, system, owner);
}
}
}
}
}
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;
}
}
}