using Newtonsoft.Json.Linq; namespace XFEWarframe.Core.Services; /// The client's 64-bit LCG. Overflow is intentional; do not replace with System.Random. public sealed class SeededRng(ulong seed) { public ulong State { get; private set; } = seed; public SeededRng(long seed) : this(unchecked((ulong)seed)) { } private void Advance() => State = unchecked(0x5851f42d4c957f2dUL * State + 0x14057b7ef767814fUL); public int Int(int min, int max) { if (max < min) throw new ArgumentOutOfRangeException(nameof(max)); if (min == max) return min; Advance(); return checked(min + (int)(((State >> 32) & 0x3fffffffUL) % ((ulong)((long)max - min) + 1))); } public double Float() { Advance(); return ((State >> 38) & 0xffffffUL) * 0.000000059604645; } public long Long(long min, long max) { if (max < min) throw new ArgumentOutOfRangeException(nameof(max)); if (min == max) return min; Advance(); return checked(min + (long)(((State >> 32) & 0x3fffffffUL) % ((ulong)(max - min) + 1))); } public T Element(IReadOnlyList items) => items.Count == 0 ? throw new ArgumentException("Empty random pool") : items[Int(0, items.Count - 1)]; public T Pop(IList items) { var i = Int(0, items.Count - 1); var value = items[i]; items.RemoveAt(i); return value; } public void Churn(int count) { for (var i = 0; i < count; i++) Advance(); } public T[] Shuffle(IEnumerable items) { var result = items.ToArray(); for (var last = result.Length - 1; last >= 1; last--) { var i = Int(0, last); (result[i], result[last]) = (result[last], result[i]); } return result; } public JObject? Reward(JArray pool) => RewardAt(pool, Float()); public static JObject? RewardAt(JArray pool, double fraction) { var entries = pool.OfType().ToArray(); if (entries.Length == 0) return null; var value = fraction * entries.Sum(x => x.Value("probability")); var total = 0d; foreach (var entry in entries) { total += entry.Value("probability"); if (value <= total) return entry; } return entries[^1]; } public static uint Mix(int first, int second) { var value = unchecked((uint)(first ^ second)); value ^= value >> 21; value ^= value << 3; value ^= value >> 4; return value; } public static T Sequential(IReadOnlyList deck, int index, int lookbehind, int seed = 0) { if (index < 0 || lookbehind < 0 || lookbehind + 1 >= deck.Count - lookbehind) throw new ArgumentOutOfRangeException(nameof(index)); var iteration = index / deck.Count; var card = index % deck.Count; var current = new SeededRng((ulong)Mix(new SeededRng((long)iteration).Int(0, 100_000), seed)).Shuffle(deck); if (card < current.Length - lookbehind && lookbehind > 0) { var previous = new SeededRng((ulong)Mix(new SeededRng((long)iteration - 1).Int(0, 100_000), seed)).Shuffle(deck); var window = previous.TakeLast(lookbehind).ToList(); for (var i = 0; i < lookbehind; i++) { if (window.Contains(current[i])) { var next = i; while (window.Contains(current[next])) next++; (current[i], current[next]) = (current[next], current[i]); } window.RemoveAt(0); window.Add(current[i]); } } return current[card]; } }