From 53270be66c9aca763b4bfc68e119d0692cd03fd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20M=C4=99drek?= Date: Fri, 16 Feb 2024 18:42:43 +0000 Subject: [PATCH] Fix spawning glass shard for each glass sheet in stack (#25308) * fix: SpawnEntitiesBehavior now works with stacks Fixed the issue of SpawnEntitiesBehavior not executing multiple times on entities with stack conponent. Fixes #25287 * fix: reduced dictionary iterations --- .../Behaviors/SpawnEntitiesBehavior.cs | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/Content.Server/Destructible/Thresholds/Behaviors/SpawnEntitiesBehavior.cs b/Content.Server/Destructible/Thresholds/Behaviors/SpawnEntitiesBehavior.cs index 5b9b7596ef..ed5777c42a 100644 --- a/Content.Server/Destructible/Thresholds/Behaviors/SpawnEntitiesBehavior.cs +++ b/Content.Server/Destructible/Thresholds/Behaviors/SpawnEntitiesBehavior.cs @@ -31,29 +31,38 @@ namespace Content.Server.Destructible.Thresholds.Behaviors var getRandomVector = () => new Vector2(system.Random.NextFloat(-Offset, Offset), system.Random.NextFloat(-Offset, Offset)); + var executions = 1; + if (system.EntityManager.TryGetComponent(owner, out var stack)) + { + executions = stack.Count; + } + 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; - - if (EntityPrototypeHelpers.HasComponent(entityId, system.PrototypeManager, system.ComponentFactory)) + for (var execution = 0; execution < executions; execution++) { - var spawned = system.EntityManager.SpawnEntity(entityId, position.Offset(getRandomVector())); - system.StackSystem.SetCount(spawned, count); + var count = minMax.Min >= minMax.Max + ? minMax.Min + : system.Random.Next(minMax.Min, minMax.Max + 1); - TransferForensics(spawned, system, owner); - } - else - { - for (var i = 0; i < count; i++) + if (count == 0) continue; + + if (EntityPrototypeHelpers.HasComponent(entityId, system.PrototypeManager, system.ComponentFactory)) { var spawned = system.EntityManager.SpawnEntity(entityId, position.Offset(getRandomVector())); + system.StackSystem.SetCount(spawned, count); TransferForensics(spawned, system, owner); } + else + { + for (var i = 0; i < count; i++) + { + var spawned = system.EntityManager.SpawnEntity(entityId, position.Offset(getRandomVector())); + + TransferForensics(spawned, system, owner); + } + } } } }