Advertisement
NickNDS

Preset Planetary GPS Generator

Mar 1st, 2022
1,318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.74 KB | None | 0 0
  1. public string
  2.     panelKey = "[pgps]", //This controls what panels get the points written to
  3.     prefix = "OM"; //This is the prefix for the generated points
  4.  
  5. //These control the absolute north, south, and so on
  6. public List<Vector3D> points = new List<Vector3D>
  7.     {
  8.         new Vector3D(0, 1, 0), //Up (north)
  9.         new Vector3D(1, 0, 0), //Right
  10.         new Vector3D(0, 0, 1) //Forward
  11.     };
  12.  
  13. public List<IMyTextPanel> panels = new List<IMyTextPanel>();
  14.  
  15. public Vector3D planetPosition = new Vector3D(0, 0, 0);
  16.  
  17. public double radius = 50000;
  18.  
  19. Program()
  20. {
  21.     Runtime.UpdateFrequency = UpdateFrequency.Update100;
  22.  
  23.     points.Insert(1, points[0] * -1.0);
  24.     points.Insert(3, points[2] * -1.0);
  25.     points.Add(points[4] * -1.0);
  26.  
  27.     Scan();
  28. }
  29.  
  30. void Main(string argument)
  31. {
  32.     Echo($"-Panel Tag: {panelKey}");
  33.     Echo("-Enter the command 'auto' to generate points for the current planet");
  34.     Echo("-Enter the planet's coordinates using the copy to clipboard function to set the planet's position manually");
  35.     Echo("-Enter a number to set the point's distance from the center of the planet manually");
  36.     Echo("-Enter the command 'scan' to look for panels");
  37.     try { Commands(argument); } catch { Echo($"Error caught running command: {argument}"); }
  38.  
  39.     Echo($"Panels: {panels.Count}");
  40.     Echo($"Planet Position: {planetPosition}");
  41.     Echo($"Point Radius: {radius:N0}");
  42.  
  43.     StringBuilder builder = new StringBuilder();
  44.     for (int i = 0; i < points.Count; i++)
  45.         builder.AppendLine(GPSConvert((points[i] * radius) + planetPosition, $"{prefix}{i + 1}"));
  46.     foreach (IMyTextPanel panel in panels)
  47.         panel.WriteText(builder);
  48. }
  49.  
  50. public void Commands(string argument)
  51. {
  52.     string arg = argument.ToLower();
  53.  
  54.     Vector3D tempPosition;
  55.  
  56.     double tempDouble;
  57.  
  58.     if (TryGetGPS(arg, out tempPosition))
  59.         planetPosition = tempPosition;
  60.     else
  61.         if (double.TryParse(arg, out tempDouble))
  62.         radius = tempDouble;
  63.     else
  64.         switch (arg)
  65.         {
  66.             case "auto":
  67.                 List<IMyShipController> controllers = new List<IMyShipController>();
  68.                 GridTerminalSystem.GetBlocksOfType<IMyShipController>(controllers);
  69.                 if (controllers.Count > 0 && controllers[0].TryGetPlanetPosition(out tempPosition))
  70.                 {
  71.                     planetPosition = tempPosition;
  72.                     controllers[0].TryGetPlanetElevation(MyPlanetElevation.Sealevel, out tempDouble);
  73.                     tempDouble = Vector3D.Distance(controllers[0].GetPosition(), planetPosition) - tempDouble;
  74.                     radius = tempDouble * 1.7182;
  75.                 }
  76.                 break;
  77.             case "scan":
  78.                 Scan();
  79.                 break;
  80.         }
  81. }
  82.  
  83. public void Scan()
  84. {
  85.     panels.Clear();
  86.  
  87.     GridTerminalSystem.GetBlocksOfType<IMyTextPanel>(panels, b => b.CustomName.ToLower().Contains(panelKey.ToLower()));
  88.  
  89.     foreach (IMyTextPanel panel in panels)
  90.         panel.ContentType = ContentType.TEXT_AND_IMAGE;
  91. }
  92.  
  93. public string GPSConvert(Vector3D vector, string name)
  94. {
  95.     return $"GPS:{name}:{Math.Floor(vector.X)}:{Math.Floor(vector.Y)}:{Math.Floor(vector.Z)}:";
  96. }
  97.  
  98. public bool TryGetGPS(string gpsString, out Vector3D vector)
  99. {
  100.     vector = new Vector3D(0, 0, 0);
  101.     try
  102.     {
  103.         string[] gpsArgs = gpsString.Split(':');
  104.         if (gpsArgs.Length >= 5)
  105.         {
  106.             double x, y, z;
  107.             if (double.TryParse(gpsArgs[2], out x) &&
  108.             double.TryParse(gpsArgs[3], out y) &&
  109.             double.TryParse(gpsArgs[4], out z))
  110.             {
  111.                 vector = new Vector3D(x, y, z);
  112.                 return true;
  113.             }
  114.         }
  115.     }
  116.     catch { }
  117.     return false;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement