Advertisement
NickNDS

Spy Plane

Aug 7th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.75 KB | None | 0 0
  1. public string sendTag = "spycoordinate", settingBackup = "", scriptName = "NDS Spy Plane";
  2.  
  3. public double gyroStrengthMultiplier = 0.8, settingsVersion = 1.0;
  4.  
  5. public IMyTerminalBlock genRemote;
  6.  
  7. public List<IMyGyro> gyroList = new List<IMyGyro>();
  8.  
  9. public bool aligning = false, resave = false, foundName = false, foundVersion = false;
  10.  
  11. public Vector3D planetCoordinate = new Vector3D(0, 0, 0);
  12.  
  13. public Program()
  14. {
  15.     Runtime.UpdateFrequency = UpdateFrequency.Update10;
  16.     Echo("Booting");
  17.     SyncRemote();
  18.     if (genRemote == null) throw new Exception("\n No control block found (remote control or cockpit)");
  19.     else Echo("Booted up and found control block: " + genRemote.CustomName);
  20.     GridTerminalSystem.GetBlocksOfType<IMyGyro>(gyroList, g => g.CubeGrid == Me.CubeGrid);
  21.     GroundCoordinate((IMyShipController)genRemote, 0);
  22.     StopOverride();
  23.     if (Me.CustomData == "") SaveData();
  24.     else LoadData();
  25.     Save();
  26. }
  27.  
  28. public void Save()
  29. {
  30.  
  31. }
  32.  
  33. public void Main(string argument, UpdateType updateSource)
  34. {
  35.     if (Me.CustomData != settingBackup) LoadData();
  36.     if (argument.ToLower() == "align") {
  37.         aligning = !aligning;
  38.         if (!aligning) StopOverride();
  39.     }
  40.     else if (updateSource == UpdateType.Terminal || argument != "") {
  41.         double offset = 0;
  42.         try { offset = double.Parse(argument); } catch { }
  43.         try { RelayCoordinate(offset); } catch {
  44.             SyncRemote();
  45.             Echo("Error getting coordinate");
  46.             if (genRemote == null) Echo("Control block lost, no control block found");
  47.         }
  48.     }
  49.     if (aligning) Align();
  50.     else Echo("Not aligning");
  51. }
  52.  
  53. public void RelayCoordinate(double offset)
  54. {
  55.     Vector3D groundCoordinate = GroundCoordinate((IMyShipController)genRemote, offset);
  56.     IGC.SendBroadcastMessage<Vector3D>(sendTag, groundCoordinate, TransmissionDistance.AntennaRelay);
  57.     Echo("Sent location: " + groundCoordinate.ToString("N0"));
  58. }
  59.  
  60. public void SyncRemote()
  61. {
  62.     if (genRemote == null || Me.CubeGrid.CubeExists(genRemote.Position)) {
  63.         genRemote = null;
  64.         List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
  65.         GridTerminalSystem.GetBlocksOfType<IMyRemoteControl>(blocks);
  66.         if (blocks.Count == 0)
  67.             GridTerminalSystem.GetBlocksOfType<IMyCockpit>(blocks);
  68.         if (blocks.Count > 0)
  69.             genRemote = blocks[0];
  70.     }
  71. }
  72.  
  73. public Vector3D GroundCoordinate(IMyShipController reference, double offset)
  74. {
  75.     Vector3D vector, currentPosition = reference.GetPosition();
  76.     if (reference.TryGetPlanetPosition(out planetCoordinate)) {
  77.         vector = Vector3D.Normalize(currentPosition - planetCoordinate);
  78.         double distance = Vector3D.Distance(planetCoordinate, currentPosition), elevation = 0;
  79.         reference.TryGetPlanetElevation(MyPlanetElevation.Surface, out elevation);
  80.         distance -= elevation + offset;
  81.         return planetCoordinate + (vector * distance);
  82.     }
  83.     return new Vector3D(0, 0, 0);
  84. }
  85.  
  86. public void StopOverride()
  87. {
  88.     for (int i = 0; i < gyroList.Count; i++) {
  89.         gyroList[i].Pitch = 0f;
  90.         gyroList[i].Yaw = 0f;
  91.         gyroList[i].Roll = 0f;
  92.         gyroList[i].GyroOverride = false;
  93.     }
  94. }
  95.  
  96. public void Align()
  97. {
  98.     Echo("Aligning");
  99.     Vector2D dir = GetDirectionTo(planetCoordinate, genRemote);
  100.     ApplyGyroOverride(dir.X, 0, -dir.Y, genRemote);
  101. }
  102.  
  103. //Whip's ApplyGyroOverride Method v9 - 8/19/17
  104. void ApplyGyroOverride(double pitch_speed, double yaw_speed, double roll_speed, IMyTerminalBlock reference)
  105. {
  106.     var rotationVec = new Vector3D(-pitch_speed, yaw_speed, roll_speed); //because keen does some weird stuff with signs
  107.     var shipMatrix = reference.WorldMatrix;
  108.     var relativeRotationVec = Vector3D.TransformNormal(rotationVec, shipMatrix);
  109.     foreach (var thisGyro in gyroList)
  110.     {
  111.         var gyroMatrix = thisGyro.WorldMatrix;
  112.         var transformedRotationVec = Vector3D.TransformNormal(relativeRotationVec, Matrix.Transpose(gyroMatrix));
  113.         thisGyro.Pitch = (float)transformedRotationVec.X;
  114.         thisGyro.Yaw = (float)transformedRotationVec.Y;
  115.         thisGyro.Roll = (float)transformedRotationVec.Z;
  116.         thisGyro.GyroOverride = true;
  117.     }
  118. }
  119.  
  120. public Vector2D GetDirectionTo(Vector3D targetVector, IMyTerminalBlock originBlock)
  121. {
  122.     MatrixD matrix = originBlock.WorldMatrix;
  123.     Vector3D originVector = originBlock.GetPosition(), forwardVector = matrix.Down * 5.0 , rightVector = matrix.Right * 5.0, upVector = matrix.Forward * 5.0;
  124.     forwardVector = forwardVector + originVector;
  125.     rightVector = rightVector + originVector;
  126.     upVector = upVector + originVector;
  127.  
  128.     double
  129.     originRange = Vector3D.Distance(originVector, targetVector),
  130.     forwardRange = Vector3D.Distance(forwardVector, targetVector),
  131.     upRange = Vector3D.Distance(upVector, targetVector),
  132.     rightRange = Vector3D.Distance(rightVector, targetVector),
  133.     upLength = Vector3D.Distance(upVector, originVector),
  134.     rightLength = Vector3D.Distance(rightVector, originVector);
  135.  
  136.     double
  137.     ThetaP = Math.Acos((upRange * upRange - upLength * upLength - originRange * originRange) / (-2.0 * upLength * originRange)),
  138.     ThetaY = Math.Acos((rightRange * rightRange - rightLength * rightLength - originRange * originRange) / (-2.0 * rightLength * originRange));
  139.     double outPitch = 90.0 - (ThetaP * 180.0 / Math.PI), outYaw = 90.0 - (ThetaY * 180.0 / Math.PI);
  140.  
  141.     if (originRange < forwardRange) outPitch = 180 - outPitch;
  142.     if (outPitch > 180.0) outPitch = -1 * (360 - outPitch);
  143.     if (originRange < forwardRange) outYaw = 180.0 - outYaw;
  144.     if (outYaw > 180.0) outYaw = -1.0 * (360.0 - outYaw);
  145.  
  146.     outPitch = (outPitch / 180.0) * Math.PI * gyroStrengthMultiplier;
  147.     outYaw = (outYaw / 180.0) * Math.PI * gyroStrengthMultiplier;
  148.  
  149.     return new Vector2D(outPitch, outYaw);
  150. }
  151.  
  152. public void SaveData()
  153. {
  154.     StringBuilder builder = new StringBuilder();
  155.    
  156.     builder.AppendLine("sendTag=" + sendTag + ";");
  157.     builder.AppendLine("gyroStrengthMultiplier=" + gyroStrengthMultiplier.ToString() + ";");
  158.     builder.AppendLine("scriptName=" + scriptName + ";");
  159.     builder.AppendLine("settingsVersion=" + settingsVersion.ToString("N2") + ";");
  160.    
  161.     Me.CustomData = builder.ToString().TrimEnd();
  162.    
  163.     foundVersion = false;
  164.     foundName = false;
  165.     resave = false;
  166.     Storage = Me.CustomData;
  167.     settingBackup = Me.CustomData;
  168. }
  169.  
  170. public void LoadData()
  171. {
  172.     if (Me.CustomData == "" && Storage != "") Me.CustomData = Storage;
  173.    
  174.     string temp = Me.CustomData.Replace("\r\n", String.Empty).Replace("\n", String.Empty).Replace("\r", String.Empty).Replace("\t", String.Empty);
  175.    
  176.     string[] settingArray = temp.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  177.    
  178.     for (int i = 0; i < settingArray.Length; i++)
  179.         ProcessSetting(settingArray[i]);
  180.  
  181.     if (resave || !foundVersion || !foundName) SaveData();
  182.     else settingBackup = Me.CustomData;
  183. }
  184.  
  185. public void ProcessSetting(string settingString)
  186. {
  187.     string settingKey = settingString.Substring(0, settingString.IndexOf("=")), settingValue = settingString.Substring(settingString.IndexOf("=") + 1);
  188.    
  189.     bool settingBool = settingValue.ToLower() == "true";
  190.    
  191.     double settingDouble = 0.0;
  192.     try { settingDouble = double.Parse(settingValue); } catch { }
  193.    
  194.     switch (settingKey)
  195.     {
  196.         case "sendTag":
  197.             sendTag = settingValue;
  198.         break;
  199.         case "gyroStrengthMultiplier":
  200.             gyroStrengthMultiplier = settingDouble;
  201.         break;
  202.         case "scriptName":
  203.             foundName = true;
  204.             if (settingValue != scriptName) resave = true;
  205.         break;
  206.         case "settingsVersion":
  207.             foundVersion = true;
  208.             if (settingDouble != settingsVersion) resave = true;
  209.         break;
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement