NickNDS

Ore Saving Script

Nov 15th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.57 KB | None | 0 0
  1. public string
  2. panelKeyword = "ores", // Panel keyword for panels displaying ore information
  3. gpsKeyword = "gps", // Panel keyword for panels displaying gps, opening these panels temporarily saves them to your hud as hidden gps
  4. broadcastPassword = "default"; // Password to keep transferred ore deposits private, must be the same on all grids
  5.  
  6. public int
  7. idleTicks = 5, // Ticks spent inactive
  8. sendPerTick = 5; // Max ores sent per tick
  9.  
  10. public double oreSize = 50; // Distance between similar ore deposits to save
  11.  
  12. public bool stopIfNoDetectorsFound = true; // Break the script if no detectors are found at first run
  13.  
  14. //
  15. // ---------- END OF USER SETTINGS ----------
  16. //
  17.  
  18. public IMyBroadcastListener answeringMachineOres;
  19.  
  20. public List<OreDeposit> mainOreList = new List<OreDeposit>(), subOreList = new List<OreDeposit>();
  21.  
  22. public List<IMyTerminalBlock> detectorList = new List<IMyTerminalBlock>();
  23.  
  24. public int detectorIndex = 0, runs = 0, oreIndex = 0;
  25.  
  26. public VRage.Game.GUI.TextPanel.ContentType content = VRage.Game.GUI.TextPanel.ContentType.TEXT_AND_IMAGE;
  27.  
  28. bool running = true, sendingAll = true;
  29.  
  30. public Program()
  31. {
  32.     Runtime.UpdateFrequency = UpdateFrequency.Update1;
  33.     answeringMachineOres = IGC.RegisterBroadcastListener(broadcastPassword);
  34.     GridTerminalSystem.GetBlocksOfType<IMyOreDetector>(detectorList);
  35.     if (detectorList.Count == 0)
  36.     {
  37.         if (stopIfNoDetectorsFound) throw new Exception("\n\nNo Ore Detectors Found");
  38.     }
  39.     else Echo("Detectors Found: " + detectorList.Count);
  40.     try { LoadOres(); } catch { Echo("Error caught loading ores"); }
  41.     Save();
  42. }
  43.  
  44. public void Save() {}
  45.  
  46. public void Main(string arg, UpdateType updateSource)
  47. {
  48.     try {
  49.         if (sendingAll || (arg != "" && arg.ToLower().Replace(" ", "") == "sendall"))
  50.         {
  51.             sendingAll = true;
  52.             int reps = 0;
  53.             for (int i = oreIndex; i < mainOreList.Count; i++)
  54.             {
  55.                 TransmitOre(mainOreList[i]);
  56.                 oreIndex++;
  57.                 reps++;
  58.                 if (reps >= 5) break;
  59.             }
  60.             if (oreIndex >= mainOreList.Count)
  61.             {
  62.                 oreIndex = 0;
  63.                 sendingAll = false;
  64.             }
  65.         }
  66.         else if (running)
  67.         {
  68.             running = false;
  69.             string oreDepositString = "";
  70.             if (detectorList.Count > 0) Purge();
  71.             if (subOreList.Count == 0 && detectorList.Count > 0) Scan();
  72.             else if (subOreList.Count > 0) {
  73.                 Echo("Saving Ores");
  74.                 AddOre(subOreList[0]);
  75.                 TransmitOre(subOreList[0]);
  76.                 subOreList.RemoveAt(0);
  77.             }
  78.             try {
  79.                 if (ReceiveMessageString(answeringMachineOres, ref oreDepositString))
  80.                     AddOre(GetOreDeposit(oreDepositString));
  81.             } catch { Echo("Error caught receiving/ saving transmitted ore"); }
  82.             try { OutputOres(); } catch { Echo("Error caught outputting ores"); }
  83.             try { OutputGPS(); } catch { Echo("Error caught outputting ores"); }
  84.             Echo("Ore Detectors: " + detectorList.Count);
  85.             Echo("Known Deposits: " + mainOreList.Count);
  86.         } else runs++;
  87.         if (runs >= idleTicks) {
  88.             runs = 0;
  89.             running = true;
  90.         }
  91.     } catch { Echo("Error caught in main"); }
  92.     Save();
  93. }
  94.  
  95. public void OutputOres()
  96. {
  97.     List<IMyTerminalBlock> panels = new List<IMyTerminalBlock>();
  98.     GridTerminalSystem.GetBlocksOfType<IMyTextPanel>(panels, p => p.CustomName.ToLower().Contains(panelKeyword.ToLower()));
  99.     if (panels.Count > 0) {
  100.         StringBuilder builder = new StringBuilder();
  101.         for (int i = 0; i < mainOreList.Count; i++) {
  102.             if (builder.Length != 0) builder.AppendLine();
  103.             builder.Append(mainOreList[i].oreType + ": " + mainOreList[i].size + ": " + mainOreList[i].gps.ToString("N1"));
  104.         }
  105.         for (int i = 0; i < panels.Count; i++) {
  106.             IMyTextSurface surface = (IMyTextPanel)panels[i];
  107.             surface.ContentType = content;
  108.             surface.WriteText(builder);
  109.         }
  110.     }
  111. }
  112.  
  113. public void OutputGPS()
  114. {
  115.     List<IMyTerminalBlock> panels = new List<IMyTerminalBlock>();
  116.     GridTerminalSystem.GetBlocksOfType<IMyTextPanel>(panels, p => p.CustomName.ToLower().Contains(gpsKeyword.ToLower()));
  117.     if (panels.Count > 0) {
  118.         StringBuilder builder = new StringBuilder();
  119.         for (int i = 0; i < mainOreList.Count; i++) {
  120.             if (builder.Length != 0) builder.AppendLine();
  121.             builder.Append("GPS:" + mainOreList[i].oreType + "-" + mainOreList[i].size + ":" + mainOreList[i].gps.X + ":" + mainOreList[i].gps.Y + ":" + mainOreList[i].gps.Z + ":");
  122.         }
  123.         for (int i = 0; i < panels.Count; i++) {
  124.             IMyTextSurface surface = (IMyTextPanel)panels[i];
  125.             surface.ContentType = content;
  126.             surface.WriteText(builder);
  127.         }
  128.     }
  129. }
  130.  
  131. public void TransmitOre(OreDeposit deposit)
  132. {
  133.     try {
  134.         IGC.SendBroadcastMessage<string>(broadcastPassword, ConvertOre(deposit), TransmissionDistance.AntennaRelay);
  135.     } catch { Echo("Error caught transmitting ore deposit"); }
  136. }
  137.  
  138. public bool ReceiveMessageString(IMyBroadcastListener answeringMachine, ref string oreDepositString)
  139. {
  140.     if (answeringMachine.HasPendingMessage)
  141.     {
  142.         MyIGCMessage message = answeringMachine.AcceptMessage();
  143.         oreDepositString = (string)(message.Data);
  144.         return true;
  145.     }
  146.     return false;
  147. }
  148.  
  149. public void LoadOres()
  150. {
  151.     string text = Me.CustomData;
  152.     text = text.Replace("\r\n", String.Empty).Replace("\n", String.Empty).Replace("\r", String.Empty).Replace("\t", String.Empty);
  153.     string[] oreList = text.Split(';');
  154.     for (int i = 0; i < oreList.Length; i++) {
  155.         if (oreList[i] != "") {
  156.             OreDeposit deposit = GetOreDeposit(oreList[i]);
  157.             AddOre(deposit);
  158.         }
  159.     }
  160. }
  161.  
  162. public void SaveOres()
  163. {
  164.     try {
  165.         if (mainOreList.Count > 0) {
  166.             StringBuilder builder = new StringBuilder();
  167.             for (int i = 0; i < mainOreList.Count; i++) {
  168.                 if (builder.Length != 0) builder.AppendLine();
  169.                 builder.Append(ConvertOre(mainOreList[i]) + ";");
  170.             }
  171.             Me.CustomData = builder.ToString();
  172.         }
  173.     } catch { Echo("Error caught saving ores"); }
  174. }
  175.  
  176. public string ConvertOre(OreDeposit deposit)
  177. {
  178.     return "GPS:" + deposit.oreType + ":" + deposit.gps.X + ":" + deposit.gps.Y + ":" + deposit.gps.Z + ":" + deposit.size;
  179. }
  180.  
  181. public void Purge()
  182. {
  183.     bool remove = false;
  184.     try {
  185.         remove = !detectorList[detectorIndex].CubeGrid.CubeExists(detectorList[detectorIndex].Position);
  186.     } catch { remove = true; }
  187.     if (remove) {
  188.         detectorList.RemoveAt(detectorIndex);
  189.         detectorIndex++;
  190.         if (detectorIndex >= detectorList.Count) detectorIndex = 0;
  191.     }
  192. }
  193.  
  194. public void Scan()
  195. {
  196.     Echo("Scanning");
  197.     SaveOres();
  198.     try {
  199.         subOreList = GetOreDepositList(detectorList[detectorIndex]);
  200.         detectorIndex++;
  201.         if (detectorIndex >= detectorList.Count) detectorIndex = 0;
  202.     } catch { Echo("Error caught scanning for ores"); }
  203. }
  204.  
  205. public void AddOre(OreDeposit ore)
  206. {
  207.     bool add = true;
  208.     for (int i = 0; i < mainOreList.Count; i++)
  209.         if ((ore.oreType == "None" || ore.size == "None") || (ore.oreType == mainOreList[i].oreType && Vector3D.Distance(mainOreList[i].gps, ore.gps) <= oreSize)) {
  210.             add = false;
  211.             break;
  212.         }
  213.     if (add) mainOreList.Add(ore);
  214. }
  215.  
  216. public List<OreDeposit> GetOreDepositList(IMyTerminalBlock detector)
  217. {
  218.     List<string> oreStringList = detector.GetValue<List<string>>("Phoenix.Ore.DetectedOre");
  219.     List<OreDeposit> oreList = new List<OreDeposit>();
  220.     for (int i = 0; i < oreStringList.Count; i++)
  221.         oreList.Add(GetOreDeposit(oreStringList[i]));
  222.     return oreList;
  223. }
  224.  
  225. public OreDeposit GetOreDeposit(string text)
  226. {
  227.     string textB = text.Substring(4);
  228.     OreDeposit deposit = new OreDeposit();
  229.     deposit.oreType = textB.Substring(0, textB.IndexOf(":"));
  230.     textB = textB.Substring(textB.IndexOf(":") + 1);
  231.  
  232.     deposit.gps.X = double.Parse(textB.Substring(0, textB.IndexOf(":")));
  233.     textB = textB.Substring(textB.IndexOf(":") + 1);
  234.  
  235.     deposit.gps.Y = double.Parse(textB.Substring(0, textB.IndexOf(":")));
  236.     textB = textB.Substring(textB.IndexOf(":") + 1);
  237.  
  238.     deposit.gps.Z = double.Parse(textB.Substring(0, textB.IndexOf(":")));
  239.     textB = textB.Substring(textB.IndexOf(":") + 1);
  240.  
  241.     deposit.size = textB;
  242.     return deposit;
  243. }
  244.  
  245. public class OreDeposit
  246. {
  247.     public string oreType = "None", size = "None";
  248.     public Vector3D gps = new Vector3D(0, 0, 0);
  249.     public string Output()
  250.     {
  251.         return oreType + ": " + size + ": " + gps.ToString("N1");
  252.     }
  253. }
Add Comment
Please, Sign In to add comment