Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public string receiveTag = "controlcoordinate", settingBackup = "",
- scriptName = "NDS Attack Station", missileKeyword = "missile";
- public IMyBroadcastListener coordinateListener;
- public List<Vector3D> receivedCoordinates = new List<Vector3D>(), unlaunchedCoordinates = new List<Vector3D>();
- public SortedList<long, MissileControlUnit> missileControlUnits = new SortedList<long, MissileControlUnit>();
- public double settingsVersion = 1.0, missileBuildTime = 5;
- public int primedMissiles = 0;
- public bool resave = false, foundName = false, foundVersion = false;
- public Program()
- {
- Runtime.UpdateFrequency = UpdateFrequency.Update10;
- if (Me.CustomData != "") LoadData();
- else {
- SaveData();
- SetListeners();
- }
- GetMissileControlUnits();
- Save();
- }
- public void Save()
- {
- }
- public void Main(string argument, UpdateType updateSource)
- {
- if (Me.CustomData != settingBackup) LoadData();
- AgeMissiles();
- if (argument != "")
- {
- Vector3D targetVector = new Vector3D(0, 0, 0);
- if (Convert(argument, ref targetVector))
- TargetCoordinate(targetVector);
- else Echo("Error targeting: " + argument);
- }
- 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);
- TargetCoordinate(coordinate);
- }
- if (unlaunchedCoordinates.Count > 0) TargetCoordinate(unlaunchedCoordinates[0]);
- Echo("Missile Control Units: " + missileControlUnits.Count);
- Echo("Primed Missile Control Units: " + primedMissiles);
- Echo("Unlaunched Coordinates: " + unlaunchedCoordinates.Count);
- Output();
- Save();
- }
- public bool Convert(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; }
- Echo("Targeting: " + targetVector.ToString());
- return true;
- }
- public void AgeMissiles()
- {
- primedMissiles = 0;
- TimeSpan timeSinceLastRun = Runtime.TimeSinceLastRun;
- for (int i = 0; i < missileControlUnits.Values.Count; i++)
- AdvanceTimeSpan(ref missileControlUnits.Values[i].knownSpan, timeSinceLastRun);
- }
- public void AdvanceTimeSpan(ref TimeSpan timespan, TimeSpan timeSinceLastRun)
- {
- if (timespan.TotalSeconds < missileBuildTime)
- timespan += timeSinceLastRun;
- else primedMissiles++;
- }
- public void TargetCoordinate(Vector3D coordinate)
- {
- GetMissileControlUnits();
- bool launched = false;
- if (missileControlUnits.Count > 0)
- {
- IMyProgrammableBlock missileControlUnit = Me;
- long entityId = -12345654321;
- for (int i = 0; i < missileControlUnits.Values.Count; i += 0)
- {
- if (!missileControlUnits.Values[i].missileControlUnit.CubeGrid.CubeExists(missileControlUnits.Values[i].missileControlUnit.Position))
- missileControlUnits.RemoveAt(i);
- else {
- if (missileControlUnits.Values[i].knownSpan.TotalSeconds >= missileBuildTime)
- {
- missileControlUnit = missileControlUnits.Values[i].missileControlUnit;
- entityId = missileControlUnit.EntityId;
- break;
- }
- i++;
- }
- }
- if (entityId != -12345654321 && missileControlUnit != Me)
- {
- if (!missileControlUnit.IsRunning && missileControlUnit.TryRun(Convert(coordinate)))
- {
- launched = true;
- missileControlUnits.RemoveAt(0);
- if (unlaunchedCoordinates.Count > 0 && unlaunchedCoordinates[0] == coordinate) unlaunchedCoordinates.RemoveAt(0);
- }
- }
- }
- if (!launched && !unlaunchedCoordinates.Contains(coordinate)) unlaunchedCoordinates.Add(coordinate);
- }
- public string Convert(Vector3D coordinate)
- {
- return "GPS:Target:" + coordinate.X + ":" + coordinate.Y + ":" + coordinate.Z + ":";
- }
- public void GetMissileControlUnits()
- {
- List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyProgrammableBlock>(blocks, m => ((IMyFunctionalBlock)m).Enabled && m.CustomName.ToLower().Contains(missileKeyword.ToLower()) && m != Me && UsableUnit(m));
- }
- public bool UsableUnit(IMyTerminalBlock block)
- {
- if (!missileControlUnits.ContainsKey(block.EntityId))
- {
- MissileControlUnit unit = new MissileControlUnit();
- unit.missileControlUnit = (IMyProgrammableBlock)block;
- missileControlUnits[block.EntityId] = unit;
- }
- return false;
- }
- public void Output()
- {
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < receivedCoordinates.Count; i++)
- {
- builder.AppendLine(receivedCoordinates[i].ToString("N0"));
- }
- List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyTextPanel>(blocks, b => b.CustomName.ToLower().Contains("coordinate"));
- 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 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("missileKeyword=" + missileKeyword + ";");
- builder.AppendLine("missileBuildTime=" + missileBuildTime.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 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 "missileKeyword":
- missileKeyword = settingValue;
- break;
- case "missileBuildTime":
- missileBuildTime = settingDouble;
- break;
- case "scriptName":
- foundName = true;
- if (settingValue != scriptName) resave = true;
- break;
- case "settingsVersion":
- foundVersion = true;
- if (settingDouble != settingsVersion) resave = true;
- break;
- }
- }
- public class MissileControlUnit
- {
- public TimeSpan knownSpan = new TimeSpan(0, 0, 0);
- public IMyProgrammableBlock missileControlUnit;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement