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;
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;
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;
}
}
- 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)