using System.Text.Json;
using XFEEDToolBox.Core.Models;
namespace XFEEDToolBox.Core.Navigation;
public static class ScoopReference
{
private sealed record Module(string Symbol,double TonnesPerSecond);
private sealed record Document(Module[] Modules);
private static readonly Dictionary<string,double> Rates=Read();
private static Dictionary<string,double> Read()
{
using var source=typeof(ScoopReference).Assembly.GetManifestResourceStream("XFEEDToolBox.Core.Navigation.scoop-reference.json")!;
return JsonSerializer.Deserialize<Document>(source,ToolboxSettings.JsonOptions)!.Modules.ToDictionary(m=>m.Symbol,m=>m.TonnesPerSecond,StringComparer.OrdinalIgnoreCase);
}
public static double? MaximumRate(ShipRecord ship)
{
var module=ship.Modules.FirstOrDefault(m=>m.Enabled&&m.Item.Contains("fuelscoop",StringComparison.OrdinalIgnoreCase));
if(module is null||!Rates.TryGetValue(module.Item.Trim('$',';'),out var rate))return null;
var modifier=module.Engineering?.Modifiers.FirstOrDefault(m=>m.Label.Equals("FuelScoopRate",StringComparison.OrdinalIgnoreCase));
if(modifier is null)return rate;
if(modifier.Value is not >0||modifier.OriginalValue is not >0)return null;
var scale=Math.Abs(modifier.OriginalValue.Value-rate)<rate*.02?1:Math.Abs(modifier.OriginalValue.Value-rate*1000)<rate*20?.001:double.NaN;
var actual=modifier.Value.Value*scale;
return double.IsFinite(actual)&&actual>0?actual:null;
}
}
/// <summary>Selects the native full-rate orbit immediately; thermal protection can only enlarge it.</summary>
public sealed class ScoopBandTracker
{
private double? previousFuel,previousHeat;
private DateTimeOffset? previousFuelAt,lastAdjustment,lastRateAt;
private double? rate;
private double? preferred;
private double? thermalFloor;
private double heatTrend;
public double? Rate=>rate;
public double? Radius=>preferred;
public double Update(double radius,double exclusion,double heat,bool active,double? fuel,DateTimeOffset? fuelAt,double? maximumRate,bool refuelling,DateTimeOffset now,double? fullRateRadius=null)
{
if(!double.IsFinite(radius)||!double.IsFinite(exclusion)||exclusion<=0||!double.IsFinite(heat))throw new ArgumentException("Invalid scoop envelope.");
var full=fullRateRadius??exclusion*1.10;
var safe=exclusion*1.05;
if(!double.IsFinite(full)||full<=safe)throw new ArgumentException("Full-rate scoop band overlaps the collision margin.");
var nominal=safe+(full-safe)*.6;
thermalFloor??=nominal;
if(fuel is {} amount&&fuelAt is {} at&&(previousFuelAt is null||at>previousFuelAt))
{
if(previousFuel is {} before&&previousFuelAt is {} beforeAt&&at-beforeAt is var elapsed&&elapsed.TotalSeconds is >=.1 and <=5)
{
var observed=(amount-before)/elapsed.TotalSeconds;
if(observed>0&&(!maximumRate.HasValue||observed<=maximumRate*1.15))
{rate=rate is {} old?old*.3+observed*.7:observed;lastRateAt=now;}
}
previousFuel=amount;previousFuelAt=at;
}
if(lastRateAt is {} measured&&now-measured>TimeSpan.FromSeconds(2.5))rate=null;
var dt=lastAdjustment is {} adjusted?Math.Clamp((now-adjusted).TotalSeconds,0,.5):.25;
if(previousHeat is {} oldHeat&&dt>0)heatTrend+=(1-Math.Exp(-dt/.8))*((heat-oldHeat)/dt-heatTrend);
previousHeat=heat;lastAdjustment=now;
if(!refuelling){preferred=Math.Max(nominal,radius);return preferred.Value;}
var projected=heat+Math.Max(0,heatTrend)*4;
// No inward search or rate ramp. Keep the precomputed full-rate circle.
// Keep the temporary thermal clearance through the escape/turn, then
// restore the precomputed circle after cooling. Arrival heat must not
// permanently turn a full-power refill into a low-rate outer orbit.
if(heat<87&&heatTrend<=0)thermalFloor=nominal;
if(active&&projected>97)thermalFloor=Math.Max(thermalFloor.Value,radius*Math.Min(1.2,projected/94));
preferred=Math.Max(nominal,thermalFloor.Value);
return preferred.Value;
}
}
using System.Text.Json;
using XFEEDToolBox.Core.Models;
namespace XFEEDToolBox.Core.Navigation;
public static class ScoopReference
{
private sealed record Module(string Symbol,double TonnesPerSecond);
private sealed record Document(Module[] Modules);
private static readonly Dictionary<string,double> Rates=Read();
private static Dictionary<string,double> Read()
{
using var source=typeof(ScoopReference).Assembly.GetManifestResourceStream("XFEEDToolBox.Core.Navigation.scoop-reference.json")!;
return JsonSerializer.Deserialize<Document>(source,ToolboxSettings.JsonOptions)!.Modules.ToDictionary(m=>m.Symbol,m=>m.TonnesPerSecond,StringComparer.OrdinalIgnoreCase);
}
public static double? MaximumRate(ShipRecord ship)
{
var module=ship.Modules.FirstOrDefault(m=>m.Enabled&&m.Item.Contains("fuelscoop",StringComparison.OrdinalIgnoreCase));
if(module is null||!Rates.TryGetValue(module.Item.Trim('$',';'),out var rate))return null;
var modifier=module.Engineering?.Modifiers.FirstOrDefault(m=>m.Label.Equals("FuelScoopRate",StringComparison.OrdinalIgnoreCase));
if(modifier is null)return rate;
if(modifier.Value is not >0||modifier.OriginalValue is not >0)return null;
var scale=Math.Abs(modifier.OriginalValue.Value-rate)<rate*.02?1:Math.Abs(modifier.OriginalValue.Value-rate*1000)<rate*20?.001:double.NaN;
var actual=modifier.Value.Value*scale;
return double.IsFinite(actual)&&actual>0?actual:null;
}
}
/// <summary>Selects the native full-rate orbit immediately; thermal protection can only enlarge it.</summary>
public sealed class ScoopBandTracker
{
private double? previousFuel,previousHeat;
private DateTimeOffset? previousFuelAt,lastAdjustment,lastRateAt;
private double? rate;
private double? preferred;
private double? thermalFloor;
private double heatTrend;
public double? Rate=>rate;
public double? Radius=>preferred;
public double Update(double radius,double exclusion,double heat,bool active,double? fuel,DateTimeOffset? fuelAt,double? maximumRate,bool refuelling,DateTimeOffset now,double? fullRateRadius=null)
{
if(!double.IsFinite(radius)||!double.IsFinite(exclusion)||exclusion<=0||!double.IsFinite(heat))throw new ArgumentException("Invalid scoop envelope.");
var full=fullRateRadius??exclusion*1.10;
var safe=exclusion*1.05;
if(!double.IsFinite(full)||full<=safe)throw new ArgumentException("Full-rate scoop band overlaps the collision margin.");
var nominal=safe+(full-safe)*.6;
thermalFloor??=nominal;
if(fuel is {} amount&&fuelAt is {} at&&(previousFuelAt is null||at>previousFuelAt))
{
if(previousFuel is {} before&&previousFuelAt is {} beforeAt&&at-beforeAt is var elapsed&&elapsed.TotalSeconds is >=.1 and <=5)
{
var observed=(amount-before)/elapsed.TotalSeconds;
if(observed>0&&(!maximumRate.HasValue||observed<=maximumRate*1.15))
{rate=rate is {} old?old*.3+observed*.7:observed;lastRateAt=now;}
}
previousFuel=amount;previousFuelAt=at;
}
if(lastRateAt is {} measured&&now-measured>TimeSpan.FromSeconds(2.5))rate=null;
var dt=lastAdjustment is {} adjusted?Math.Clamp((now-adjusted).TotalSeconds,0,.5):.25;
if(previousHeat is {} oldHeat&&dt>0)heatTrend+=(1-Math.Exp(-dt/.8))*((heat-oldHeat)/dt-heatTrend);
previousHeat=heat;lastAdjustment=now;
if(!refuelling){preferred=Math.Max(nominal,radius);return preferred.Value;}
var projected=heat+Math.Max(0,heatTrend)*4;
// No inward search or rate ramp. Keep the precomputed full-rate circle.
// Keep the temporary thermal clearance through the escape/turn, then
// restore the precomputed circle after cooling. Arrival heat must not
// permanently turn a full-power refill into a low-rate outer orbit.
if(heat<87&&heatTrend<=0)thermalFloor=nominal;
if(active&&projected>97)thermalFloor=Math.Max(thermalFloor.Value,radius*Math.Min(1.2,projected/94));
preferred=Math.Max(nominal,thermalFloor.Value);
return preferred.Value;
}
}