Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public string
- panelKeyword = "ores", // Panel keyword for panels displaying ore information
- gpsKeyword = "gps", // Panel keyword for panels displaying gps, opening these panels temporarily saves them to your hud as hidden gps
- broadcastPassword = "default"; // Password to keep transferred ore deposits private, must be the same on all grids
- public int
- idleTicks = 5, // Ticks spent inactive
- sendPerTick = 5; // Max ores sent per tick
- public double oreSize = 50; // Distance between similar ore deposits to save
- public bool stopIfNoDetectorsFound = true; // Break the script if no detectors are found at first run
- //
- // ---------- END OF USER SETTINGS ----------
- //
- public IMyBroadcastListener answeringMachineOres;
- public List<OreDeposit> mainOreList = new List<OreDeposit>(), subOreList = new List<OreDeposit>();
- public List<IMyTerminalBlock> detectorList = new List<IMyTerminalBlock>();
- public int detectorIndex = 0, runs = 0, oreIndex = 0;
- public VRage.Game.GUI.TextPanel.ContentType content = VRage.Game.GUI.TextPanel.ContentType.TEXT_AND_IMAGE;
- bool running = true, sendingAll = true;
- public Program()
- {
- Runtime.UpdateFrequency = UpdateFrequency.Update1;
- answeringMachineOres = IGC.RegisterBroadcastListener(broadcastPassword);
- GridTerminalSystem.GetBlocksOfType<IMyOreDetector>(detectorList);
- if (detectorList.Count == 0)
- {
- if (stopIfNoDetectorsFound) throw new Exception("\n\nNo Ore Detectors Found");
- }
- else Echo("Detectors Found: " + detectorList.Count);
- try { LoadOres(); } catch { Echo("Error caught loading ores"); }
- Save();
- }
- public void Save() {}
- public void Main(string arg, UpdateType updateSource)
- {
- try {
- if (sendingAll || (arg != "" && arg.ToLower().Replace(" ", "") == "sendall"))
- {
- sendingAll = true;
- int reps = 0;
- for (int i = oreIndex; i < mainOreList.Count; i++)
- {
- TransmitOre(mainOreList[i]);
- oreIndex++;
- reps++;
- if (reps >= 5) break;
- }
- if (oreIndex >= mainOreList.Count)
- {
- oreIndex = 0;
- sendingAll = false;
- }
- }
- else if (running)
- {
- running = false;
- string oreDepositString = "";
- if (detectorList.Count > 0) Purge();
- if (subOreList.Count == 0 && detectorList.Count > 0) Scan();
- else if (subOreList.Count > 0) {
- Echo("Saving Ores");
- AddOre(subOreList[0]);
- TransmitOre(subOreList[0]);
- subOreList.RemoveAt(0);
- }
- try {
- if (ReceiveMessageString(answeringMachineOres, ref oreDepositString))
- AddOre(GetOreDeposit(oreDepositString));
- } catch { Echo("Error caught receiving/ saving transmitted ore"); }
- try { OutputOres(); } catch { Echo("Error caught outputting ores"); }
- try { OutputGPS(); } catch { Echo("Error caught outputting ores"); }
- Echo("Ore Detectors: " + detectorList.Count);
- Echo("Known Deposits: " + mainOreList.Count);
- } else runs++;
- if (runs >= idleTicks) {
- runs = 0;
- running = true;
- }
- } catch { Echo("Error caught in main"); }
- Save();
- }
- public void OutputOres()
- {
- List<IMyTerminalBlock> panels = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyTextPanel>(panels, p => p.CustomName.ToLower().Contains(panelKeyword.ToLower()));
- if (panels.Count > 0) {
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < mainOreList.Count; i++) {
- if (builder.Length != 0) builder.AppendLine();
- builder.Append(mainOreList[i].oreType + ": " + mainOreList[i].size + ": " + mainOreList[i].gps.ToString("N1"));
- }
- for (int i = 0; i < panels.Count; i++) {
- IMyTextSurface surface = (IMyTextPanel)panels[i];
- surface.ContentType = content;
- surface.WriteText(builder);
- }
- }
- }
- public void OutputGPS()
- {
- List<IMyTerminalBlock> panels = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyTextPanel>(panels, p => p.CustomName.ToLower().Contains(gpsKeyword.ToLower()));
- if (panels.Count > 0) {
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < mainOreList.Count; i++) {
- if (builder.Length != 0) builder.AppendLine();
- builder.Append("GPS:" + mainOreList[i].oreType + "-" + mainOreList[i].size + ":" + mainOreList[i].gps.X + ":" + mainOreList[i].gps.Y + ":" + mainOreList[i].gps.Z + ":");
- }
- for (int i = 0; i < panels.Count; i++) {
- IMyTextSurface surface = (IMyTextPanel)panels[i];
- surface.ContentType = content;
- surface.WriteText(builder);
- }
- }
- }
- public void TransmitOre(OreDeposit deposit)
- {
- try {
- IGC.SendBroadcastMessage<string>(broadcastPassword, ConvertOre(deposit), TransmissionDistance.AntennaRelay);
- } catch { Echo("Error caught transmitting ore deposit"); }
- }
- public bool ReceiveMessageString(IMyBroadcastListener answeringMachine, ref string oreDepositString)
- {
- if (answeringMachine.HasPendingMessage)
- {
- MyIGCMessage message = answeringMachine.AcceptMessage();
- oreDepositString = (string)(message.Data);
- return true;
- }
- return false;
- }
- public void LoadOres()
- {
- string text = Me.CustomData;
- text = text.Replace("\r\n", String.Empty).Replace("\n", String.Empty).Replace("\r", String.Empty).Replace("\t", String.Empty);
- string[] oreList = text.Split(';');
- for (int i = 0; i < oreList.Length; i++) {
- if (oreList[i] != "") {
- OreDeposit deposit = GetOreDeposit(oreList[i]);
- AddOre(deposit);
- }
- }
- }
- public void SaveOres()
- {
- try {
- if (mainOreList.Count > 0) {
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < mainOreList.Count; i++) {
- if (builder.Length != 0) builder.AppendLine();
- builder.Append(ConvertOre(mainOreList[i]) + ";");
- }
- Me.CustomData = builder.ToString();
- }
- } catch { Echo("Error caught saving ores"); }
- }
- public string ConvertOre(OreDeposit deposit)
- {
- return "GPS:" + deposit.oreType + ":" + deposit.gps.X + ":" + deposit.gps.Y + ":" + deposit.gps.Z + ":" + deposit.size;
- }
- public void Purge()
- {
- bool remove = false;
- try {
- remove = !detectorList[detectorIndex].CubeGrid.CubeExists(detectorList[detectorIndex].Position);
- } catch { remove = true; }
- if (remove) {
- detectorList.RemoveAt(detectorIndex);
- detectorIndex++;
- if (detectorIndex >= detectorList.Count) detectorIndex = 0;
- }
- }
- public void Scan()
- {
- Echo("Scanning");
- SaveOres();
- try {
- subOreList = GetOreDepositList(detectorList[detectorIndex]);
- detectorIndex++;
- if (detectorIndex >= detectorList.Count) detectorIndex = 0;
- } catch { Echo("Error caught scanning for ores"); }
- }
- public void AddOre(OreDeposit ore)
- {
- bool add = true;
- for (int i = 0; i < mainOreList.Count; i++)
- if ((ore.oreType == "None" || ore.size == "None") || (ore.oreType == mainOreList[i].oreType && Vector3D.Distance(mainOreList[i].gps, ore.gps) <= oreSize)) {
- add = false;
- break;
- }
- if (add) mainOreList.Add(ore);
- }
- public List<OreDeposit> GetOreDepositList(IMyTerminalBlock detector)
- {
- List<string> oreStringList = detector.GetValue<List<string>>("Phoenix.Ore.DetectedOre");
- List<OreDeposit> oreList = new List<OreDeposit>();
- for (int i = 0; i < oreStringList.Count; i++)
- oreList.Add(GetOreDeposit(oreStringList[i]));
- return oreList;
- }
- public OreDeposit GetOreDeposit(string text)
- {
- string textB = text.Substring(4);
- OreDeposit deposit = new OreDeposit();
- deposit.oreType = textB.Substring(0, textB.IndexOf(":"));
- textB = textB.Substring(textB.IndexOf(":") + 1);
- deposit.gps.X = double.Parse(textB.Substring(0, textB.IndexOf(":")));
- textB = textB.Substring(textB.IndexOf(":") + 1);
- deposit.gps.Y = double.Parse(textB.Substring(0, textB.IndexOf(":")));
- textB = textB.Substring(textB.IndexOf(":") + 1);
- deposit.gps.Z = double.Parse(textB.Substring(0, textB.IndexOf(":")));
- textB = textB.Substring(textB.IndexOf(":") + 1);
- deposit.size = textB;
- return deposit;
- }
- public class OreDeposit
- {
- public string oreType = "None", size = "None";
- public Vector3D gps = new Vector3D(0, 0, 0);
- public string Output()
- {
- return oreType + ": " + size + ": " + gps.ToString("N1");
- }
- }
Add Comment
Please, Sign In to add comment