Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public string receiveTag = "spycoordinate", sendTag = "controlcoordinate", scriptName = "NDS Command Ship", settingBackup = "";
- public IMyBroadcastListener coordinateListener;
- public List<Vector3D> receivedCoordinates = new List<Vector3D>(), sentCoordinates = new List<Vector3D>();
- public int currentCoordinate = 0;
- public double settingsVersion = 1.0;
- public bool resave = false, foundName = false, foundVersion = false;
- public Program()
- {
- Runtime.UpdateFrequency = UpdateFrequency.Update10;
- if (Me.CustomData != "") LoadData();
- else {
- SaveData();
- SetListeners();
- }
- Save();
- }
- public void Save()
- {
- }
- public void Main(string argument, UpdateType updateSource)
- {
- if (Me.CustomData != settingBackup) LoadData();
- if (argument != "") Commands(argument.ToLower());
- if (coordinateListener.HasPendingMessage)
- {
- MyIGCMessage message = coordinateListener.AcceptMessage();
- Vector3D coordinate = (Vector3D)(message.Data);
- Echo("Coordinate received at: " + coordinate.ToString("N0"));
- if (!receivedCoordinates.Contains(coordinate)) {
- receivedCoordinates.Add(coordinate);
- SaveData();
- }
- }
- while (currentCoordinate > 0 && currentCoordinate > receivedCoordinates.Count - 1) currentCoordinate--;
- StringBuilder builder = new StringBuilder(), gpsBuilder = new StringBuilder();
- builder.AppendLine("Received Coordinates");
- gpsBuilder.AppendLine("Received Coordinates GPS");
- if (receivedCoordinates.Count > 0)
- {
- Echo("Commands: 'down', 'up', 'remove', 'send'");
- Echo("Current Coordinates");
- for (int i = 0; i < receivedCoordinates.Count; i++)
- {
- gpsBuilder.AppendLine(Convert(receivedCoordinates[i]));
- if (currentCoordinate == i) {
- Echo("> " + receivedCoordinates[i].ToString("N0"));
- builder.AppendLine("> " + receivedCoordinates[i].ToString("N0"));
- }
- else {
- Echo(receivedCoordinates[i].ToString("N0"));
- builder.AppendLine(receivedCoordinates[i].ToString("N0"));
- }
- }
- } else Echo("No coordinates");
- OutputBuilder(builder, "received");
- OutputBuilder(gpsBuilder, "gps");
- builder.Clear();
- builder.AppendLine("Sent Coordinates");
- if (sentCoordinates.Count > 0)
- {
- for (int i = 0; i < sentCoordinates.Count; i++)
- {
- builder.AppendLine("Sent: " + sentCoordinates[i].ToString("N0"));
- }
- }
- OutputBuilder(builder, "sent");
- }
- public bool ConvertCommand(string argument, ref Vector3D targetVector)
- {
- string arg = argument.Substring(argument.IndexOf(":") + 1);
- arg = arg.Substring(arg.IndexOf(":") + 1);
- try
- {
- targetVector.X = double.Parse(arg.Substring(0, arg.IndexOf(":")));
- } catch { return false; }
- arg = arg.Substring(arg.IndexOf(":") + 1);
- try
- {
- targetVector.Y = double.Parse(arg.Substring(0, arg.IndexOf(":")));
- } catch { return false; }
- arg = arg.Substring(arg.IndexOf(":") + 1);
- try
- {
- targetVector.Z = double.Parse(arg.Substring(0, arg.IndexOf(":")));
- } catch { return false; }
- return true;
- }
- public string Convert(Vector3D coordinate)
- {
- return "GPS:Target:" + coordinate.X + ":" + coordinate.Y + ":" + coordinate.Z + ":";
- }
- public void OutputBuilder(StringBuilder builder, string panelKeyword)
- {
- List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyTextPanel>(blocks, b => b.CustomName.ToLower().Contains(panelKeyword));
- for (int i = 0; i < blocks.Count; i++)
- {
- IMyTextSurface surface = (IMyTextSurface)blocks[i];
- surface.ContentType = VRage.Game.GUI.TextPanel.ContentType.TEXT_AND_IMAGE;
- surface.WriteText(builder);
- }
- }
- public void Commands(string argument)
- {
- if (receivedCoordinates.Count > 0)
- {
- switch (argument)
- {
- case "down":
- if (currentCoordinate < receivedCoordinates.Count - 1) currentCoordinate++;
- break;
- case "up":
- if (currentCoordinate > 0) currentCoordinate--;
- break;
- case "remove":
- if (currentCoordinate <= receivedCoordinates.Count - 1) {
- receivedCoordinates.RemoveAt(currentCoordinate);
- SaveData();
- }
- break;
- case "send":
- IGC.SendBroadcastMessage<Vector3D>(sendTag, receivedCoordinates[currentCoordinate], TransmissionDistance.AntennaRelay);
- if (!sentCoordinates.Contains(receivedCoordinates[currentCoordinate])) sentCoordinates.Add(receivedCoordinates[currentCoordinate]);
- break;
- }
- }
- Vector3D targetVector = new Vector3D(0, 0, 0);
- if (ConvertCommand(argument, ref targetVector) && !receivedCoordinates.Contains(targetVector)) {
- receivedCoordinates.Add(targetVector);
- SaveData();
- }
- }
- public void SetListeners()
- {
- List<IMyBroadcastListener> listeners = new List<IMyBroadcastListener>();
- IGC.GetBroadcastListeners(listeners);
- for (int i = 0; i < listeners.Count; i++)
- IGC.DisableBroadcastListener(listeners[i]);
- coordinateListener = IGC.RegisterBroadcastListener(receiveTag);
- Echo("Listening on tag: " + receiveTag);
- }
- public void SaveData()
- {
- StringBuilder builder = new StringBuilder();
- builder.AppendLine("receiveTag=" + receiveTag + ";");
- builder.AppendLine("sendTag=" + sendTag + ";");
- for (int i = 0; i < receivedCoordinates.Count; i++)
- {
- builder.AppendLine("coordinate=" + receivedCoordinates[i].ToString() + ";");
- }
- builder.AppendLine("scriptName=" + scriptName + ";");
- builder.AppendLine("settingsVersion=" + settingsVersion.ToString("N2") + ";");
- Me.CustomData = builder.ToString().TrimEnd();
- foundVersion = false;
- foundName = false;
- resave = false;
- Storage = Me.CustomData;
- settingBackup = Me.CustomData;
- }
- public void LoadData()
- {
- if (Me.CustomData == "" && Storage != "") Me.CustomData = Storage;
- string temp = Me.CustomData.Replace("\r\n", String.Empty).Replace("\n", String.Empty).Replace("\r", String.Empty).Replace("\t", String.Empty);
- string[] settingArray = temp.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
- for (int i = 0; i < settingArray.Length; i++)
- ProcessSetting(settingArray[i]);
- SetListeners();
- if (resave || !foundVersion || !foundName) SaveData();
- else settingBackup = Me.CustomData;
- }
- public bool Convert(string settingValue, ref Vector3D vector)
- {
- string temp = settingValue.Substring(settingValue.IndexOf(":") + 1);
- try
- {
- vector.X = double.Parse(temp.Substring(0, temp.IndexOf(" ")));
- } catch { return false; }
- temp = temp.Substring(temp.IndexOf(":") + 1);
- try
- {
- vector.Y = double.Parse(temp.Substring(0, temp.IndexOf(" ")));
- } catch { return false; }
- temp = temp.Substring(temp.IndexOf(":") + 1);
- try
- {
- vector.Z = double.Parse(temp.Substring(0, temp.IndexOf("}")));
- } catch { return false; }
- return true;
- }
- public void ProcessSetting(string settingString)
- {
- string settingKey = settingString.Substring(0, settingString.IndexOf("=")), settingValue = settingString.Substring(settingString.IndexOf("=") + 1);
- bool settingBool = settingValue.ToLower() == "true";
- double settingDouble = 0.0;
- try { settingDouble = double.Parse(settingValue); } catch { }
- switch (settingKey)
- {
- case "receiveTag":
- receiveTag = settingValue;
- break;
- case "sendTag":
- sendTag = settingValue;
- break;
- case "coordinate":
- Vector3D vector = new Vector3D(0, 0, 0);
- if (Convert(settingValue, ref vector) && !receivedCoordinates.Contains(vector))
- receivedCoordinates.Add(vector);
- break;
- case "scriptName":
- foundName = true;
- if (settingValue != scriptName) resave = true;
- break;
- case "settingsVersion":
- foundVersion = true;
- if (settingDouble != settingsVersion) resave = true;
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement