XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEWarframe

【WinUI】基于Warframe游戏本体的公开服务器以及安装器

公开
关注 0 Fork 0 Star 0
UTF-8
using Newtonsoft.Json.Linq;

namespace XFEWarframe.Core.Services;

/// <summary>The client's 64-bit LCG. Overflow is intentional; do not replace with System.Random.</summary>
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<T>(IReadOnlyList<T> items) => items.Count == 0 ? throw new ArgumentException("Empty random pool") : items[Int(0, items.Count - 1)];
    public T Pop<T>(IList<T> 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<T>(IEnumerable<T> 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<JObject>().ToArray();
        if (entries.Length == 0) return null;
        var value = fraction * entries.Sum(x => x.Value<double>("probability")); var total = 0d;
        foreach (var entry in entries) { total += entry.Value<double>("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<T>(IReadOnlyList<T> 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];
    }
}