Advertisement
NickNDS

Command Ship 3

Aug 2nd, 2019
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.37 KB | None | 0 0
  1. public string receiveTag = "spycoordinate", sendTag = "controlcoordinate", scriptName = "NDS Command Ship", settingBackup = "";
  2.  
  3. public IMyBroadcastListener coordinateListener;
  4.  
  5. public List<Vector3D> receivedCoordinates = new List<Vector3D>(), sentCoordinates = new List<Vector3D>();
  6.  
  7. public int currentCoordinate = 0;
  8.  
  9. public double settingsVersion = 1.0;
  10.  
  11. public bool resave = false, foundName = false, foundVersion = false;
  12.  
  13. public Program()
  14. {
  15.     Runtime.UpdateFrequency = UpdateFrequency.Update10;
  16.     if (Me.CustomData != "") LoadData();
  17.     else {
  18.         SaveData();
  19.         SetListeners();
  20.     }
  21.     Save();
  22. }
  23.  
  24. public void Save()
  25. {
  26.  
  27. }
  28.  
  29. public void Main(string argument, UpdateType updateSource)
  30. {
  31.     if (Me.CustomData != settingBackup) LoadData();
  32.     if (argument != "") Commands(argument.ToLower());
  33.     if (coordinateListener.HasPendingMessage)
  34.     {
  35.         MyIGCMessage message = coordinateListener.AcceptMessage();
  36.         Vector3D coordinate = (Vector3D)(message.Data);
  37.         Echo("Coordinate received at: " + coordinate.ToString("N0"));
  38.         if (!receivedCoordinates.Contains(coordinate)) {
  39.             receivedCoordinates.Add(coordinate);
  40.             SaveData();
  41.         }
  42.     }
  43.     while (currentCoordinate > 0 && currentCoordinate > receivedCoordinates.Count - 1) currentCoordinate--;
  44.     StringBuilder builder = new StringBuilder(), gpsBuilder = new StringBuilder();
  45.     builder.AppendLine("Received Coordinates");
  46.     gpsBuilder.AppendLine("Received Coordinates GPS");
  47.     if (receivedCoordinates.Count > 0)
  48.     {
  49.         Echo("Commands: 'down', 'up', 'remove', 'send'");
  50.         Echo("Current Coordinates");
  51.         for (int i = 0; i < receivedCoordinates.Count; i++)
  52.         {
  53.             gpsBuilder.AppendLine(Convert(receivedCoordinates[i]));
  54.             if (currentCoordinate == i) {
  55.                 Echo("> " + receivedCoordinates[i].ToString("N0"));
  56.                 builder.AppendLine("> " + receivedCoordinates[i].ToString("N0"));
  57.             }
  58.             else {
  59.                 Echo(receivedCoordinates[i].ToString("N0"));
  60.                 builder.AppendLine(receivedCoordinates[i].ToString("N0"));
  61.             }
  62.         }
  63.     } else Echo("No coordinates");
  64.     OutputBuilder(builder, "received");
  65.     OutputBuilder(gpsBuilder, "gps");
  66.     builder.Clear();
  67.     builder.AppendLine("Sent Coordinates");
  68.     if (sentCoordinates.Count > 0)
  69.     {
  70.         for (int i = 0; i < sentCoordinates.Count; i++)
  71.         {
  72.             builder.AppendLine("Sent: " + sentCoordinates[i].ToString("N0"));
  73.         }
  74.     }
  75.     OutputBuilder(builder, "sent");
  76. }
  77.  
  78. public bool ConvertCommand(string argument, ref Vector3D targetVector)
  79. {
  80.     string arg = argument.Substring(argument.IndexOf(":") + 1);
  81.     arg = arg.Substring(arg.IndexOf(":") + 1);
  82.     try
  83.     {
  84.         targetVector.X = double.Parse(arg.Substring(0, arg.IndexOf(":")));
  85.     } catch { return false; }
  86.     arg = arg.Substring(arg.IndexOf(":") + 1);
  87.     try
  88.     {
  89.         targetVector.Y = double.Parse(arg.Substring(0, arg.IndexOf(":")));
  90.     } catch { return false; }
  91.     arg = arg.Substring(arg.IndexOf(":") + 1);
  92.     try
  93.     {
  94.         targetVector.Z = double.Parse(arg.Substring(0, arg.IndexOf(":")));
  95.     } catch { return false; }
  96.     return true;
  97. }
  98.  
  99. public string Convert(Vector3D coordinate)
  100. {
  101.     return "GPS:Target:" + coordinate.X + ":" + coordinate.Y + ":" + coordinate.Z + ":";
  102. }
  103.  
  104. public void OutputBuilder(StringBuilder builder, string panelKeyword)
  105. {
  106.     List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
  107.     GridTerminalSystem.GetBlocksOfType<IMyTextPanel>(blocks, b => b.CustomName.ToLower().Contains(panelKeyword));
  108.     for (int i = 0; i < blocks.Count; i++)
  109.     {
  110.         IMyTextSurface surface = (IMyTextSurface)blocks[i];
  111.         surface.ContentType = VRage.Game.GUI.TextPanel.ContentType.TEXT_AND_IMAGE;
  112.         surface.WriteText(builder);
  113.     }
  114. }
  115.  
  116. public void Commands(string argument)
  117. {
  118.     if (receivedCoordinates.Count > 0)
  119.     {
  120.         switch (argument)
  121.         {
  122.             case "down":
  123.                 if (currentCoordinate < receivedCoordinates.Count - 1) currentCoordinate++;
  124.             break;
  125.             case "up":
  126.                 if (currentCoordinate > 0) currentCoordinate--;
  127.             break;
  128.             case "remove":
  129.                 if (currentCoordinate <= receivedCoordinates.Count - 1) {
  130.                     receivedCoordinates.RemoveAt(currentCoordinate);
  131.                     SaveData();
  132.                 }
  133.             break;
  134.             case "send":
  135.                 IGC.SendBroadcastMessage<Vector3D>(sendTag, receivedCoordinates[currentCoordinate], TransmissionDistance.AntennaRelay);
  136.                 if (!sentCoordinates.Contains(receivedCoordinates[currentCoordinate])) sentCoordinates.Add(receivedCoordinates[currentCoordinate]);
  137.             break;
  138.         }
  139.     }
  140.     Vector3D targetVector = new Vector3D(0, 0, 0);
  141.     if (ConvertCommand(argument, ref targetVector) && !receivedCoordinates.Contains(targetVector)) {
  142.         receivedCoordinates.Add(targetVector);
  143.         SaveData();
  144.     }
  145. }
  146.  
  147. public void SetListeners()
  148. {
  149.     List<IMyBroadcastListener> listeners = new List<IMyBroadcastListener>();
  150.     IGC.GetBroadcastListeners(listeners);
  151.     for (int i = 0; i < listeners.Count; i++)
  152.         IGC.DisableBroadcastListener(listeners[i]);
  153.     coordinateListener = IGC.RegisterBroadcastListener(receiveTag);
  154.     Echo("Listening on tag: " + receiveTag);
  155. }
  156.  
  157. public void SaveData()
  158. {
  159.     StringBuilder builder = new StringBuilder();
  160.    
  161.     builder.AppendLine("receiveTag=" + receiveTag + ";");
  162.     builder.AppendLine("sendTag=" + sendTag + ";");
  163.     for (int i = 0; i < receivedCoordinates.Count; i++)
  164.     {
  165.         builder.AppendLine("coordinate=" + receivedCoordinates[i].ToString() + ";");
  166.     }
  167.     builder.AppendLine("scriptName=" + scriptName + ";");
  168.     builder.AppendLine("settingsVersion=" + settingsVersion.ToString("N2") + ";");
  169.    
  170.     Me.CustomData = builder.ToString().TrimEnd();
  171.    
  172.     foundVersion = false;
  173.     foundName = false;
  174.     resave = false;
  175.     Storage = Me.CustomData;
  176.     settingBackup = Me.CustomData;
  177. }
  178.  
  179. public void LoadData()
  180. {
  181.     if (Me.CustomData == "" && Storage != "") Me.CustomData = Storage;
  182.    
  183.     string temp = Me.CustomData.Replace("\r\n", String.Empty).Replace("\n", String.Empty).Replace("\r", String.Empty).Replace("\t", String.Empty);
  184.    
  185.     string[] settingArray = temp.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  186.    
  187.     for (int i = 0; i < settingArray.Length; i++)
  188.         ProcessSetting(settingArray[i]);
  189.  
  190.     SetListeners();
  191.     if (resave || !foundVersion || !foundName) SaveData();
  192.     else settingBackup = Me.CustomData;
  193. }
  194.  
  195. public bool Convert(string settingValue, ref Vector3D vector)
  196. {
  197.     string temp = settingValue.Substring(settingValue.IndexOf(":") + 1);
  198.     try
  199.     {
  200.         vector.X = double.Parse(temp.Substring(0, temp.IndexOf(" ")));
  201.     } catch { return false; }
  202.     temp = temp.Substring(temp.IndexOf(":") + 1);
  203.     try
  204.     {
  205.         vector.Y = double.Parse(temp.Substring(0, temp.IndexOf(" ")));
  206.     } catch { return false; }
  207.     temp = temp.Substring(temp.IndexOf(":") + 1);
  208.     try
  209.     {
  210.         vector.Z = double.Parse(temp.Substring(0, temp.IndexOf("}")));
  211.     } catch { return false; }
  212.     return true;
  213. }
  214.  
  215. public void ProcessSetting(string settingString)
  216. {
  217.     string settingKey = settingString.Substring(0, settingString.IndexOf("=")), settingValue = settingString.Substring(settingString.IndexOf("=") + 1);
  218.    
  219.     bool settingBool = settingValue.ToLower() == "true";
  220.    
  221.     double settingDouble = 0.0;
  222.     try { settingDouble = double.Parse(settingValue); } catch { }
  223.    
  224.     switch (settingKey)
  225.     {
  226.         case "receiveTag":
  227.             receiveTag = settingValue;
  228.         break;
  229.         case "sendTag":
  230.             sendTag = settingValue;
  231.         break;
  232.         case "coordinate":
  233.             Vector3D vector = new Vector3D(0, 0, 0);
  234.             if (Convert(settingValue, ref vector) && !receivedCoordinates.Contains(vector))
  235.                 receivedCoordinates.Add(vector);
  236.         break;
  237.         case "scriptName":
  238.             foundName = true;
  239.             if (settingValue != scriptName) resave = true;
  240.         break;
  241.         case "settingsVersion":
  242.             foundVersion = true;
  243.             if (settingDouble != settingsVersion) resave = true;
  244.         break;
  245.     }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement