Advertisement
klassekatze

plotter prototype

Oct 18th, 2023
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.57 KB | None | 0 0
  1. using Sandbox.Game.EntityComponents;
  2. using Sandbox.ModAPI.Ingame;
  3. using Sandbox.ModAPI.Interfaces;
  4. using SpaceEngineers.Game.ModAPI.Ingame;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Collections.Immutable;
  9. using System.Diagnostics;
  10. using System.Linq;
  11. using System.Security.Policy;
  12. using System.Text;
  13. using VRage;
  14. using VRage.Collections;
  15. using VRage.Game;
  16. using VRage.Game.Components;
  17. using VRage.Game.GUI.TextPanel;
  18. using VRage.Game.ModAPI.Ingame;
  19. using VRage.Game.ModAPI.Ingame.Utilities;
  20. using VRage.Game.ObjectBuilders.Definitions;
  21. using VRageMath;
  22. using static IngameScript.Program;
  23.  
  24. namespace IngameScript
  25. {
  26.     partial class Program : MyGridProgram
  27.     {
  28.  
  29.         static string extract(string s, string prefix, string suffix="")
  30.         {
  31.             var i = s.IndexOf(prefix);
  32.             if (i >= 0)
  33.             {
  34.                 s = s.Substring(i + prefix.Length);
  35.             }
  36.             else
  37.             {
  38.                 return "";
  39.             }
  40.             if (suffix != "")
  41.             {
  42.                 i = s.IndexOf(suffix);
  43.                 if (i >= 0)
  44.                 {
  45.                     s = s.Substring(0, i);
  46.                 }
  47.                 else
  48.                 {
  49.                     return "";
  50.                 }
  51.             }
  52.             return s;
  53.         }
  54.  
  55.  
  56.         public class SpeedSector
  57.         {
  58.             public Vector3D pos;
  59.             public double radius;
  60.             public string name;
  61.             public string color;
  62.         }
  63.         public class GPS
  64.         {
  65.             public string name;
  66.             public Vector3D pos;
  67.             public string color;
  68.             public GPS()
  69.             {
  70.  
  71.             }
  72.             public GPS(string str)
  73.             {
  74.                 gps(str);
  75.             }
  76.             public GPS(GPS c)
  77.             {
  78.                 name = c.name;
  79.                 pos = c.pos;
  80.                 color = c.color;
  81.             }
  82.  
  83.             public string gps()
  84.             {
  85.                 return "GPS:" + name + ":" + pos.X.ToString("0.0") + ":" + pos.Y.ToString("0.0") + ":" + pos.Z.ToString("0.0") + ":"+color;//..""GPS:Mars Station:1200000:120000:500000:#FF90FF00:"
  86.             }
  87.             public void gps(string s)
  88.             {
  89.                 var t = s.Split(':');
  90.                 if (t.Length > 3)
  91.                 {
  92.                     pos = new Vector3D(float.Parse(t[2]), float.Parse(t[3]), float.Parse(t[4]));
  93.                     name = t[1];
  94.                     color = t[5];
  95.                 }
  96.             }
  97.         }
  98.  
  99.  
  100.         public class Plotter
  101.         {
  102.             public Plotter()
  103.             {
  104.                 parsesectors();
  105.                 mkspheres();
  106.             }
  107.  
  108.             string nexus_sectors = "GPS:Far Gate Zone - (R:80km):-9999999:-9999999:9999999:#FFFFFF00:\r\nGPS:Sol Gate - (R:80km):-3900000:3600000:-200000:#FFFFFF00:\r\nGPS:Uranus - (R:500km):-4200000:3000000:-250000:#FFFFFF00:\r\nGPS:M2 - (R:300km):1550000:-800000:0:#FFFFFF00:\r\nGPS:Vesta - (R:300km):-1400000:1000000:-50000:#FFFFFF00:\r\nGPS:Tycho - (R:300km):1400000:1000000:-50000:#FFFFFF00:\r\nGPS:T1 - (R:300km):1725000:0:0:#FFFFFF00:\r\nGPS:Pallas - (R:300km):0:-1725000:100000:#FFFFFF00:\r\nGPS:P1 - (R:300km):-800000:-1550000:0:#FFFFFF00:\r\nGPS:M1 - (R:300km):800000:-1550000:0:#FFFFFF00:\r\nGPS:E1 - (R:300km):-1725000:0:0:#FFFFFF00:\r\nGPS:E2 - (R:300km):-1500000:-900000:0:#FFFFFF00:\r\nGPS:C1 - (R:300km):600000:1600000:0:#FFFFFF00:\r\nGPS:C2 - (R:300km):-600000:1600000:0:#FFFFFF00:\r\nGPS:Earth - (R:400km):-800000:100000:80000:#FFFFFF00:\r\nGPS:Mars - (R:400km):750000:-750000:-80000:#FFFFFF00:\r\nGPS:Trojan Cluster - (R:400km):1600000:-2700000:445350:#FFFFFF00:\r\nGPS:Greek Cluster - (R:400km):-1600000:-2700000:-334490:#FFFFFF00:\r\nGPS:Ceres - (R:400km):0:2350000:100000:#FFFFFF00:\r\nGPS:Saturn - (R:750km):2800000:2000000:-100000:#FFFFFF00:\r\nGPS:Jupiter - (R:750km):0:-3200000:0:#FFFFFF00:\r\nGPS:Inner High Speed Zone - (R:4400km):0:0:0:#FFFFFF00:";
  109.             public List<SpeedSector> sectors = new List<SpeedSector>();
  110.             void parsesectors()
  111.             {
  112.                 sectors.Clear();
  113.                 string[] lines = nexus_sectors.Replace("\r", "").Split('\n');
  114.                 foreach (var l in lines)
  115.                 {
  116.                     if (l.ToLower().Contains("high speed")) continue;
  117.                     string n = l.Replace("R:", "R;");
  118.  
  119.                     SpeedSector s = new SpeedSector();
  120.                     s.pos = new GPS(n).pos;
  121.                     s.radius = 0;
  122.                     double.TryParse(extract(n, "R;", "km"), out s.radius);
  123.                     if (s.radius != 0)
  124.                     {
  125.                         s.radius *= 1000;
  126.                         s.name = n.Split(':')[1].Split('-')[0].Trim();
  127.                         sectors.Add(s);
  128.                     }
  129.                 }
  130.             }
  131.             List<BoundingSphereD> sectorSpheres = new List<BoundingSphereD>();
  132.             void mkspheres()
  133.             {
  134.                 foreach (var z in sectors)
  135.                 {
  136.                     sectorSpheres.Add(new BoundingSphereD(z.pos, z.radius));
  137.                 }
  138.             }
  139.  
  140.             public SpeedSector getSector(Vector3D p)
  141.             {
  142.                 foreach (var sector in sectors)
  143.                 {
  144.                     if (Vector3.DistanceSquared(p, sector.pos) < sector.radius * sector.radius) return sector;
  145.                 }
  146.                 return null;
  147.             }
  148.  
  149.             public List<GPS> calcroute(GPS start, GPS end, double pad)
  150.             {
  151.                 List<GPS> route = new List<GPS>();
  152.                 var sa = getSector(start.pos);
  153.                 var sb = getSector(end.pos);
  154.                 if (sa != null && sb == sa) return new List<GPS>(new GPS[] { start, end });
  155.  
  156.                 if (sa != null)
  157.                 {
  158.                     route.Add(closest_highspeed(start, pad, sa));
  159.                 }
  160.                 else route.Add(start);
  161.  
  162.                 if (sb != null)
  163.                 {
  164.                     route.Add(closest_highspeed(end, pad, sb));
  165.                 }
  166.                 else route.Add(end);
  167.  
  168.                 for (int i = 0; i < route.Count - 1;)
  169.                 {
  170.                     GPS a = route[i];
  171.                     GPS b = route[i + 1];
  172.  
  173.                     Vector3D hit = Vector3D.Zero;
  174.                     var hitsector = checkObjectIntersect(a.pos, b.pos, out hit);
  175.  
  176.                     if(hitsector != null)
  177.                     {
  178.                         GPS hitgps = new GPS(a);
  179.                         hitgps.pos = hit;
  180.                         GPS shoved = closest_highspeed(hitgps, pad);
  181.                         clog("intersected " + hitsector.name + " zone, adding divert at closest approach, " + (a.pos - shoved.pos).Length() + "m out at " + (shoved.pos).Length());
  182.                         route.Insert(i + 1, shoved);
  183.                         continue;
  184.                     }
  185.                     i++;
  186.                 }
  187.  
  188.                 if(route[0] != start)
  189.                 {
  190.                     route.Insert(0, start);
  191.                 }
  192.                 if (route[route.Count-1] != end)
  193.                 {
  194.                     route.Add(end);
  195.                 }
  196.  
  197.                 return route;
  198.             }
  199.  
  200.             Vector3D rayHitPos(Vector3D start, Vector3D end, BoundingSphereD sphere)
  201.             {
  202.                 var testLine = new RayD(start, Vector3D.Normalize(end - start));
  203.                 double? res = testLine.Intersects(sphere);
  204.                 if (res.HasValue)
  205.                 {
  206.                     Vector3D hp1 = start + (Vector3D.Normalize(end - start) * res.Value);
  207.                     return hp1;
  208.                 }
  209.                 return Vector3D.Zero;
  210.             }
  211.  
  212.  
  213.  
  214.             SpeedSector checkObjectIntersect(Vector3D start, Vector3D end, out Vector3D hitpos)
  215.             {
  216.                 SpeedSector r = null;
  217.                 int i = 0;
  218.                 foreach (var z in sectors)
  219.                 {
  220.  
  221.                     var sphere = sectorSpheres[i];
  222.  
  223.                     Vector3D hp1 = rayHitPos(start, end, sphere);
  224.                     if (hp1 != Vector3D.Zero)
  225.                     {
  226.                         Vector3D hp2 = rayHitPos(end, start, sphere);
  227.                         if (hp2 != Vector3D.Zero)
  228.                         {
  229.                             //not sure what happening here exactly, but it's possible to have a ray hit that isn't reversible, so check hp2
  230.                             //this occurred on the tycho-vesta route impacting "jupiter" despite making no sense
  231.                             hitpos = (hp1 + hp2) * 0.5;
  232.                             clog("hitpos is in " + sectors[i].name);// _getZoneName(AllSpeedZones[i]));// ;
  233.                             clog("hitpos is reporting " + getSector(hitpos)?.name);
  234.  
  235.                             clog("hp1 m " + hp1.Length());
  236.                             clog("hp2 m " + hp1.Length());
  237.                             clog("hp m " + hitpos.Length());
  238.  
  239.                             return z;
  240.                         }
  241.                         else
  242.                         {
  243.                             //clog("no hit on " + z.name);
  244.                         }
  245.                     }
  246.                     else
  247.                     {
  248.                         //clog("no hit on " + z.name);
  249.                     }
  250.                     i += 1;
  251.                 }
  252.                 hitpos = Vector3D.Zero;
  253.                 return r;
  254.             }
  255.  
  256.             static int cnt = 0;
  257.             GPS closest_highspeed(GPS gps, double padding, SpeedSector zone = null)
  258.             {
  259.                 SpeedSector s_z = zone;
  260.                 if (s_z == null) s_z = getSector(gps.pos);
  261.                 if (s_z == null) return null;
  262.  
  263.                 Vector3D pushed_out = s_z.pos + (Vector3D.Normalize(gps.pos - s_z.pos) * (s_z.radius + padding));
  264.  
  265.                 GPS ngps = new GPS(gps);
  266.                 ngps.name = cnt + "";
  267.                 cnt++;
  268.                 ngps.pos = pushed_out;
  269.                 return ngps;
  270.             }
  271.         }
  272.  
  273.         public Plotter plotter = new Plotter();
  274.     }
  275. }
  276.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement