Fix TryFindRandomTile grid weighting (#27724)

This commit is contained in:
Kara
2024-05-06 04:25:11 -07:00
committed by GitHub
parent 8fa7ea7cf9
commit aaabd5e914
2 changed files with 18 additions and 8 deletions

View 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;

View 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)