klassekatze

fnb2

Aug 23rd, 2026
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.09 KB | None | 0 0
  1. public class FnBPosVelTrackerF
  2. {
  3.     public Vector3D Position = Vector3D.Zero;
  4.     public Vector3D Velocity = Vector3D.Zero;
  5.     public bool Ready = false;
  6.     public bool inFnB = false;
  7.  
  8.  
  9.     const int _cadence = 2;//cadence from mod
  10.     const double safeLimit = 250;//empirical under cap even under aggressive spin
  11.  
  12.     // ---- teleport-phase histogram (running, argmax-decided, NO latch) ----
  13.     int[] _tpCounts = new int[_cadence];   // counts of excess events per tick%cadence
  14.     int _tpPhase = -1;                      // argmax; recomputed each tick
  15.  
  16.     // ---- teleport-mode state ----
  17.     bool _teleportObserved;
  18.     int _misses = 0;                        // consecutive tp-phase ticks w/ no excess
  19.     Vector3D _excess = Vector3D.Zero;
  20.  
  21.     // ---- position/vel carry-forward ----
  22.     bool _lastPosValid;
  23.     Vector3D _lastPos = Vector3D.Zero;
  24.     Vector3D _predPos = Vector3D.Zero;
  25.  
  26.     const double DT = 1.0 / 60.0;
  27.     double jitterChk = 0.1;
  28.     int ticks = 0;
  29.     const int MISS_LIMIT = 3;
  30.  
  31.     public FnBPosVelTrackerF()
  32.     {
  33.         _lastPosValid = false;
  34.         _teleportObserved = false;
  35.     }
  36.  
  37.     public void update(IMyShipController ctrl)//called every tick
  38.     {
  39.         Vector3D linVel = ctrl.GetShipVelocities().LinearVelocity; // clamped ~300
  40.         Vector3D truePos = ctrl.GetPosition();                      // always accurate
  41.  
  42.         Vector3D fnbVel = linVel;
  43.         ticks++;
  44.  
  45.         if (_lastPosValid)
  46.         {
  47.             Vector3D deltaPos = truePos - _lastPos;
  48.             Vector3D expected = linVel * DT;
  49.             Vector3D excessTick = deltaPos - expected;
  50.             bool hasExcess = excessTick.LengthSquared() > jitterChk * jitterChk;
  51.  
  52.             // ---- running histogram: count excess events by tick phase ----
  53.             if (hasExcess)
  54.                 _tpCounts[ticks % _cadence]++;
  55.  
  56.             // decide the teleport phase as the argmax (continuous, self-correcting)
  57.             _tpPhase = 0;
  58.             for (int p = 1; p < _cadence; p++)
  59.                 if (_tpCounts[p] > _tpCounts[_tpPhase]) _tpPhase = p;
  60.  
  61.             bool tpParity = (ticks % _cadence) == _tpPhase;
  62.  
  63.             if (tpParity)
  64.             {
  65.                 // THIS is a teleport tick (by argmax): always anchor to truth;
  66.                 // integrate the excess (eat rotation) only when actually present.
  67.                 _predPos = truePos;
  68.                 if (hasExcess)
  69.                 {
  70.                     _excess = excessTick * (60.0 / _cadence);  // bounded |ω×r|/2
  71.                     _teleportObserved = true;
  72.                     _misses = 0;
  73.                 }
  74.                 else
  75.                 {
  76.                     _misses++;   // teleport phase arrived empty -> heading out of FnB
  77.                 }
  78.             }
  79.             else
  80.             {
  81.                 // non-teleport tick: never integrate; dead-reckon fitted position
  82.                 _predPos += fnbVel * DT;
  83.             }
  84.  
  85.             // structural dropout: teleport phase keeps arriving with no excess
  86.             if (tpParity && _misses > MISS_LIMIT)
  87.             {
  88.                 _teleportObserved = false;
  89.                 _excess = Vector3D.Zero;
  90.                 _predPos = truePos;   // hard snap on exit
  91.             }
  92.         }
  93.         else
  94.         {
  95.             _predPos = truePos;
  96.         }
  97.  
  98.         _lastPos = truePos;
  99.         _lastPosValid = true;
  100.         inFnB = _teleportObserved;
  101.  
  102.         bool underSafeLimit = linVel.LengthSquared() < safeLimit * safeLimit;
  103.  
  104.         Position = underSafeLimit ? _lastPos : _predPos;
  105.         Velocity = underSafeLimit ? linVel : (_teleportObserved ? linVel + _excess : linVel);
  106.         Ready = Ready || ticks > 10 || _teleportObserved;
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment