sightEndlessDraw function

SightStep sightEndlessDraw(
  1. List<SightStep> pool,
  2. Random rng
)

Sorteia uma figura de pool: primeiro a fase, pelo peso de sightEndlessPhaseWeights (só entre as que têm figura em pool), depois uma figura da fase, sem preferência.

Implementation

SightStep sightEndlessDraw(List<SightStep> pool, Random rng) {
  final byPhase = <int, List<SightStep>>{};
  for (final d in pool) {
    byPhase.putIfAbsent(sightPhaseOf(d.level).number, () => []).add(d);
  }
  final phases = byPhase.keys.toList()..sort();
  final weights = [for (final p in phases) sightEndlessPhaseWeights[p] ?? 1];
  var x = rng.nextInt(weights.fold(0, (a, b) => a + b));
  var i = 0;
  while (x >= weights[i]) {
    x -= weights[i];
    i++;
  }
  final chosen = byPhase[phases[i]]!;
  return chosen[rng.nextInt(chosen.length)];
}