Advertisement
NickNDS

Don't Slow Down

Dec 20th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.57 KB | None | 0 0
  1. public double
  2. minimumSpeed = 33;
  3.  
  4. public string timerKeyword = "control"; // Put into Custom Data of a timer you want triggered
  5.  
  6. public bool triggerTimer = true;
  7.  
  8. /**
  9.  
  10. ---------- END OF USER SETTINGS ----------
  11.  
  12. **/
  13.  
  14. public Vector3D currentOverride = new Vector3D(0, 0, 0);
  15.  
  16. public IMyShipController controller;
  17.  
  18. public IMyTimerBlock timer;
  19.  
  20. public List<IMyGyro> gyroList = new List<IMyGyro>();
  21.  
  22. public bool idle = true, appliedOverride = false, triggeredTimer = false;
  23.  
  24. public Program()
  25. {
  26.     Runtime.UpdateFrequency = UpdateFrequency.Update10;
  27.     Change();
  28.     GridTerminalSystem.GetBlocksOfType<IMyGyro>(gyroList);
  29.     List<IMyTimerBlock> timerList = new List<IMyTimerBlock>();
  30.     GridTerminalSystem.GetBlocksOfType<IMyTimerBlock>(timerList, b => b.CustomData.ToLower().Contains(timerKeyword.ToLower()));
  31.     if (timerList.Count > 0) timer = timerList[0];
  32. }
  33.  
  34. public void Save() { }
  35.  
  36. public void Main(string argument, UpdateType updateSource)
  37. {
  38.     try
  39.     {
  40.         if (controller == null) GetController();
  41.         else
  42.         {
  43.             double currentSpeed = controller.GetShipSpeed();
  44.             if (idle && currentSpeed >= minimumSpeed) idle = false;
  45.             if (currentSpeed >= minimumSpeed && appliedOverride) StopOverride();
  46.             else if (!idle && currentSpeed < minimumSpeed && !appliedOverride) Uncontrol();
  47.         }
  48.     }
  49.     catch { Echo("Error caught in main"); }
  50. }
  51.  
  52. public void Uncontrol()
  53. {
  54.     if (!triggeredTimer && triggerTimer)
  55.     {
  56.         triggeredTimer = true;
  57.         try {
  58.             if (timer != null) timer.Trigger();
  59.         } catch { }
  60.     }
  61.     ApplyGyroOverride();
  62.     appliedOverride = true;
  63. }
  64.  
  65. public void Change()
  66. {
  67.     int reps = 0;
  68.     Random rnd = new Random();
  69.     while (reps < 10 && (Math.Abs(currentOverride.X) < Math.PI * 0.05 || Math.Abs(currentOverride.Y) < Math.PI * 0.05 || Math.Abs(currentOverride.Z) < Math.PI * 0.05))
  70.     {
  71.         currentOverride = new Vector3D(Math.PI * ((double)rnd.Next(-1000, 1000) / 1000.0), Math.PI * ((double)rnd.Next(-1000, 1000) / 1000.0), Math.PI * ((double)rnd.Next(-1000, 1000) / 1000.0));
  72.         reps++;
  73.     }
  74. }
  75.  
  76. public void StopOverride()
  77. {
  78.     appliedOverride = false;
  79.     triggeredTimer = false;
  80.     for (int i = 0; i < gyroList.Count; i++)
  81.     {
  82.         try
  83.         {
  84.             gyroList[i].Pitch = 0f;
  85.             gyroList[i].Yaw = 0f;
  86.             gyroList[i].Roll = 0f;
  87.             gyroList[i].GyroOverride = false;
  88.         } catch { }
  89.     }
  90. }
  91.  
  92. //Whip's ApplyGyroOverride Method v9 - 8/19/17
  93. public void ApplyGyroOverride()
  94. {
  95.     MatrixD refMatrix = controller.WorldMatrix;
  96.     var rotationVec = new Vector3D(-currentOverride.X, currentOverride.Y, currentOverride.Z); //because keen does some weird stuff with signs
  97.     var relativeRotationVec = Vector3D.TransformNormal(rotationVec, refMatrix);
  98.     for (int i = 0; i < gyroList.Count; i++)
  99.     {
  100.         try {
  101.             var gyroMatrix = gyroList[i].WorldMatrix;
  102.             var transformedRotationVec = Vector3D.TransformNormal(relativeRotationVec, Matrix.Transpose(gyroMatrix));
  103.             gyroList[i].GyroOverride = true;
  104.             gyroList[i].Pitch = (float)transformedRotationVec.X;
  105.             gyroList[i].Yaw = (float)transformedRotationVec.Y;
  106.             gyroList[i].Roll = (float)transformedRotationVec.Z;
  107.         } catch { }
  108.     }
  109. }
  110.  
  111. public void GetController()
  112. {
  113.     List<IMyShipController> controllerList = new List<IMyShipController>();
  114.     GridTerminalSystem.GetBlocksOfType<IMyShipController>(controllerList);
  115.     if (controllerList.Count > 0) controller = controllerList[0];
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement