klassekatze

Untitled

Aug 30th, 2026
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.60 KB | None | 0 0
  1.  // cpa_time(): compute the time of CPA for two tracks
  2.  //    Input:  two tracks Tr1 and Tr2
  3.  //    Return: the time at which the two tracks are closest
  4.  public static double cpa_time(Vector3D Tr1_p, Vector3D Tr1_v, Vector3D Tr2_p, Vector3D Tr2_v)
  5.  {
  6.      Vector3D dv = Tr1_v - Tr2_v;
  7.  
  8.      double dv2 = Vector3D.Dot(dv, dv);
  9.      if (dv2 < 0.00000001)      // the  tracks are almost parallel
  10.          return 0.0;             // any time is ok.  Use time 0.
  11.  
  12.      Vector3D w0 = Tr1_p - Tr2_p;
  13.      double cpatime = -Vector3D.Dot(w0, dv) / dv2;
  14.  
  15.      return cpatime;             // time of CPA
  16.  }
  17.  
  18.  
  19. public class Target
  20. {
  21.     static public int staleInterval = 5;//the tick limit before we consider a target entry expired. must be larger than our update interval
  22.     public int lastUpd = -1;
  23.     public bool isStale() { return tick - lastUpd > staleInterval; }
  24.  
  25.     public bool isProjectile = false;
  26.     public List<WeaponFirmware> turretsTargeting = new List<WeaponFirmware>();
  27.  
  28.     public List<WeaponFirmware> turretsAbleToTarget = new List<WeaponFirmware>();
  29.  
  30.     public string name;
  31.     public long eid=-1;//torpedoes get a dummy number
  32.     static long torpidx = 1000;
  33.     public Vector3D vel = Vector3D.Zero;
  34.     public Vector3D pos0 = Vector3D.Zero;
  35.     public Vector3D pos1 = Vector3D.Zero;
  36.     public Vector3D pos2 = Vector3D.Zero;
  37.     public BoundingBoxD bbox = BoundingBoxD.CreateInvalid();
  38.     public Target(TorpedoData d) { isProjectile = true; update(d); }
  39.     public Target(MyDetectedEntityInfo i) { isProjectile = false; update(i); }
  40.     public void update(MyDetectedEntityInfo i, WeaponFirmware t =null)
  41.     {
  42.         name = i.Name;
  43.         if (i.Type != MyDetectedEntityType.Missile)
  44.         {
  45.             isProjectile = false;
  46.             eid = i.EntityId;
  47.             bbox = i.BoundingBox;
  48.             pos2 = pos1;
  49.             pos1 = pos0;
  50.             pos0 = i.Position;
  51.  
  52.             vel = i.Velocity;
  53.             lastUpd = tick;
  54.             if (!i.Name.Equals(name)) name = i.Name;
  55.             //if(t != null && !turretsTargeting.Contains(t)) turretsTargeting.Add(t);
  56.         }
  57.     }
  58.     public void update(TorpedoData d)
  59.     {
  60.         if (eid == -1) eid = torpidx++;
  61.         name = "Torp";
  62.         pos2 = pos1;
  63.         pos1 = pos0;
  64.         pos0 = d.positionApx;
  65.         /*if (pos0 != Vector3D.Zero && pos1 != Vector3D.Zero && pos2 != Vector3D.Zero)
  66.         {
  67.             vel.X = CalculateVelocity(pos2.X, pos1.X, pos0.X);
  68.             vel.Y = CalculateVelocity(pos2.X, pos1.Y, pos0.X);
  69.             vel.Z = CalculateVelocity(pos2.X, pos1.Z, pos0.X);
  70.             vel *= 60;
  71.         }*/
  72.         //dunno if velocity matter. I suppose it could be used to identify torpedo type, but who the fuck uses ramshackles anyway eh
  73.         turretsTargeting = d.turretsTargeting;
  74.         lastUpd = tick;
  75.     }
  76.  
  77.     public void delock()
  78.     {
  79.         foreach(var tt in turretsTargeting)
  80.         {
  81.             //WeaponFirmware v = null;
  82.             //managedWeapons.TryGetValue(tt, out v);
  83.             if (tt.assignedTarget == this) tt.assignTarget(null);// tt.assignedTarget = null;
  84.         }
  85.         turretsTargeting.Clear();
  86.         //we can also do any postmortem here, like announcing we killed some shit and it was *this* close to fucking our shit up
  87.     }
  88.  
  89.     public double td0;
  90.     public double td1;
  91. }
  92.  
  93. public class TorpedoData
  94. {
  95.     public List<WeaponFirmware> turretsTargeting = new List<WeaponFirmware>();
  96.     public Vector3D positionApx;
  97.     public bool updated = false;
  98.  
  99.     public static double limitsqr = 6*6;
  100.     public bool closeEnough(Vector3D p)
  101.     {
  102.         return (positionApx - p).LengthSquared() < limitsqr;
  103.     }
  104. }
  105. List<TorpedoData> calculateTorpedoes(List<WeaponFirmware> missileLockedGuns)
  106. {
  107.     List<TorpedoData> torpedoes = new List<TorpedoData>();
  108.     List<MyTuple<Vector3D, Vector3D>> scopes = new List<MyTuple<Vector3D, Vector3D>>();
  109.     foreach (var b in missileLockedGuns) scopes.Add(modAPIWeaponCore.GetWeaponScope(b.b, 0));
  110.     for (int x = 0; x < scopes.Count; x++)
  111.     {
  112.         var xg = missileLockedGuns[x];
  113.         var xs = scopes[x];
  114.         for (int y = 0; y < scopes.Count; y++)
  115.         {
  116.             if (y == x) continue;
  117.             var yg = missileLockedGuns[y];
  118.             var ys = scopes[y];
  119.             double cpa_t = cpa_time(xs.Item1, xs.Item2 * xg.settings.AmmoVelocity, ys.Item1, ys.Item2 * yg.settings.AmmoVelocity);
  120.             if(cpa_t > 0 && cpa_t < 1.5)//all PDCs should hit maxtraj in 1s or less
  121.             {
  122.                 Vector3D xp = xs.Item1 + (xs.Item2 * xg.settings.AmmoVelocity * cpa_t);
  123.                 Vector3D yp = ys.Item1 + (ys.Item2 * yg.settings.AmmoVelocity * cpa_t);
  124.                 double dist = (xp - yp).LengthSquared();
  125.                 if(dist < TorpedoData.limitsqr)//torp 1500m/s so 25m per tick, oof... lets say <30m from another trajectory. update: live test had it 6m at worst, it seems.
  126.                 {
  127.                     Vector3D p = (xp + yp) * 0.5;
  128.                     TorpedoData t = null;
  129.                     foreach(var trp in torpedoes)
  130.                     {
  131.                         if (trp.closeEnough(p))
  132.                         {
  133.                             t = trp;
  134.                             break;
  135.                         }
  136.                     }
  137.                     if (t == null) t = new TorpedoData();
  138.                     t.positionApx = p;
  139.                     if (!t.turretsTargeting.Contains(xg)) t.turretsTargeting.Add(xg);
  140.                     if (!t.turretsTargeting.Contains(yg)) t.turretsTargeting.Add(yg);
  141.                 }
  142.             }
  143.         }
  144.     }
  145.     return torpedoes;
  146. }
  147.  
  148.  
  149. public void updateTargetingData()
  150. {
  151.     torpedolocked.Clear();
  152.     foreach (var kvp in targetList) kvp.Value.turretsTargeting.Clear();
  153.  
  154.     foreach (var kvp in managedWeapons)
  155.     {
  156.         var m = kvp.Value;
  157.         m.update();
  158.         if (m.lockTicks > 0)
  159.         {
  160.             if (m.lastInfo.Type == MyDetectedEntityType.Missile) torpedolocked.Add(m);
  161.             else
  162.             {
  163.                 Target v = null;
  164.                 targetList.TryGetValue(m.lastInfo.EntityId, out v);
  165.                 if (v != null) v.update(m.lastInfo);
  166.                 else
  167.                 {
  168.                     //we aren't gonna add for now - we only want the rapid updates for things that would already be in the global threat list.
  169.                     //v = new Target(m.lastInfo);
  170.                     //targetList[m.lastInfo.EntityId] = new Target(m.lastInfo);
  171.                 }
  172.                 //v.turretsTargeting.Add(m);
  173.             }
  174.         }
  175.         /*else if (m.assignedTarget != null)
  176.         {
  177.             m.assignedTarget.turretsTargeting.Add(m);
  178.         }*/
  179.     }
  180.     List<TorpedoData> td = calculateTorpedoes(torpedolocked);
  181.     torpCount = td.Count;
  182.     dbg += "td:"+td.Count + "\n";
  183.     foreach (var kvp in targetList)
  184.     {
  185.         var l = kvp.Value;
  186.         if (l.isProjectile)
  187.         {
  188.             foreach (var t in td)
  189.             {
  190.                 if (t.closeEnough(l.pos0))
  191.                 {
  192.                     t.updated = true;
  193.                     l.update(t);
  194.                     foreach(var w in t.turretsTargeting)
  195.                     {
  196.                         w.assignTarget(l);
  197.                     }
  198.                     break;
  199.                 }
  200.             }
  201.         }
  202.     }
  203.     foreach (var t in td)
  204.     {
  205.         if (!t.updated)
  206.         {
  207.             var v = new Target(t);
  208.             targetList[v.eid] = v;
  209.             foreach (var w in t.turretsTargeting)
  210.             {
  211.                 w.assignTarget(v);
  212.             }
  213.         }
  214.     }
  215.     List<long> del = new List<long>();
  216.     foreach (var kvp in targetList)
  217.     {
  218.         if (kvp.Value.isStale())
  219.         {
  220.             kvp.Value.delock();
  221.             del.Add(kvp.Key);
  222.         }
  223.     }
  224.     foreach (var d in del)targetList.Remove(d);
  225.  
  226.     int ntrvl = 4;
  227.     if (lockCount > 0 || torpCount > 0) ntrvl = 1;
  228.     dbg += "ntrvl=" + ntrvl+"\n";
  229.     if (tickcheck(ref threatChk, ntrvl))
  230.     {
  231.         threats.Clear();
  232.         modAPIWeaponCore.GetSortedThreats(gProgram.Me, threats);
  233.         dbg += "tl:" + threats.Count + "\n";
  234.         foreach (var kvp in threats)
  235.         {
  236.             if (kvp.Key.Relationship == MyRelationsBetweenPlayerAndBlock.Enemies)
  237.             {
  238.                 Target v = null;
  239.                 targetList.TryGetValue(kvp.Key.EntityId, out v);
  240.                 if (v != null) v.update(kvp.Key);
  241.                 else targetList[kvp.Key.EntityId] = new Target(kvp.Key);
  242.             }
  243.         }
  244.         threatCount = threats.Count;
  245.     }
  246.     //dbg += targetList.Count + "\n";
  247. }
Add Comment
Please, Sign In to add comment