]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Fix spawning glass shard for each glass sheet in stack (#25308)
authorŁukasz Mędrek <lukasz@lukaszm.xyz>
Fri, 16 Feb 2024 18:42:43 +0000 (18:42 +0000)
committerGitHub <noreply@github.com>
Fri, 16 Feb 2024 18:42:43 +0000 (13:42 -0500)
* 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

Content.Server/Destructible/Thresholds/Behaviors/SpawnEntitiesBehavior.cs

index 5b9b7596efb28806a9dad00ee78db352ac0a3f1d..ed5777c42aa99632ec216f8d58550bf4bae11b69 100644 (file)
@@ -31,29 +31,38 @@ namespace Content.Server.Destructible.Thresholds.Behaviors
 
             var getRandomVector = () => new Vector2(system.Random.NextFloat(-Offset, Offset), system.Random.NextFloat(-Offset, Offset));
 
-            foreach (var (entityId, minMax) in Spawn)
+            var executions = 1;
+            if (system.EntityManager.TryGetComponent<StackComponent>(owner, out var stack))
             {
-                var count = minMax.Min >= minMax.Max
-                    ? minMax.Min
-                    : system.Random.Next(minMax.Min, minMax.Max + 1);
-
-                if (count == 0) continue;
+                executions = stack.Count;
+            }
 
-                if (EntityPrototypeHelpers.HasComponent<StackComponent>(entityId, system.PrototypeManager, system.ComponentFactory))
+            foreach (var (entityId, minMax) in Spawn)
+            {
+                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<StackComponent>(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);
+                        }
+                    }
                 }
             }
         }