]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Fix `TryFindRandomTile` grid weighting (#27724)
authorKara <lunarautomaton6@gmail.com>
Mon, 6 May 2024 11:25:11 +0000 (04:25 -0700)
committerGitHub <noreply@github.com>
Mon, 6 May 2024 11:25:11 +0000 (04:25 -0700)
Content.Server/GameTicking/Rules/GameRuleSystem.Utility.cs
Content.Shared/Random/Helpers/SharedRandomExtensions.cs

index 27a9edbad71fcd1a7a30553a78a24da9e2980992..cbd981e99e69f92f43dbf6b92968d953390275d4 100644 (file)
@@ -1,7 +1,10 @@
 using System.Diagnostics.CodeAnalysis;
+using System.Linq;
 using Content.Server.GameTicking.Components;
 using Content.Server.GameTicking.Rules.Components;
 using Content.Server.Station.Components;
+using Content.Shared.Random.Helpers;
+using Robust.Server.GameObjects;
 using Robust.Shared.Collections;
 using Robust.Shared.Map;
 using Robust.Shared.Map.Components;
@@ -82,17 +85,23 @@ public abstract partial class GameRuleSystem<T> where T: IComponent
         targetCoords = EntityCoordinates.Invalid;
         targetGrid = EntityUid.Invalid;
 
-        var possibleTargets = station.Comp.Grids;
-        if (possibleTargets.Count == 0)
+        // Weight grid choice by tilecount
+        var weights = new Dictionary<Entity<MapGridComponent>, float>();
+        foreach (var possibleTarget in station.Comp.Grids)
+        {
+            if (!TryComp<MapGridComponent>(possibleTarget, out var comp))
+                continue;
+
+            weights.Add((possibleTarget, comp), _map.GetAllTiles(possibleTarget, comp).Count());
+        }
+
+        if (weights.Count == 0)
         {
             targetGrid = EntityUid.Invalid;
             return false;
         }
 
-        targetGrid = RobustRandom.Pick(possibleTargets);
-
-        if (!TryComp<MapGridComponent>(targetGrid, out var gridComp))
-            return false;
+        (targetGrid, var gridComp) = RobustRandom.Pick(weights);
 
         var found = false;
         var aabb = gridComp.LocalAABB;
index dcdc9d61d98027f6a40271e954b6565b3e052d43..20e57e94212803603b7a74d019f947dcd38e49fe 100644 (file)
@@ -57,7 +57,8 @@ namespace Content.Shared.Random.Helpers
             throw new InvalidOperationException($"Invalid weighted pick for {prototype.ID}!");
         }
 
-        public static string Pick(this IRobustRandom random, Dictionary<string, float> weights)
+        public static T Pick<T>(this IRobustRandom random, Dictionary<T, float> weights)
+            where T: notnull
         {
             var sum = weights.Values.Sum();
             var accumulated = 0f;
@@ -74,7 +75,7 @@ namespace Content.Shared.Random.Helpers
                 }
             }
 
-            throw new InvalidOperationException($"Invalid weighted pick");
+            throw new InvalidOperationException("Invalid weighted pick");
         }
 
         public static (string reagent, FixedPoint2 quantity) Pick(this WeightedRandomFillSolutionPrototype prototype, IRobustRandom? random = null)