Advertisement
NickNDS

Aerodynamic Simulator

Oct 25th, 2019
951
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.99 KB | None | 0 0
  1. public double
  2. requiredForwardSpeedToEnableThrusters = 25,
  3. requiredGravityForAerodynamics = 0.05,
  4. recheckTimeInSeconds = 15,
  5. verticalFrictionPercentage = 10;
  6.  
  7. public bool
  8. allowBackwardsAerodynamics = false,
  9. simulateFrictionWhenFalling = false,
  10. dynamicGravity = true,
  11. noVerticalThrustersInSpace = false;
  12.  
  13. public int updateFrequency = 100; // 100, 10, or 1
  14.  
  15. /**-------------------------
  16.         END OF USER SETTINGS
  17. -------------------------**/
  18.  
  19.  
  20.  
  21. public List<IMyThrust> verticalThrusters = new List<IMyThrust>();
  22.  
  23. public List<int> downThrustIndexes = new List<int>();
  24.  
  25. public IMyShipController shipController;
  26.  
  27. public bool
  28. thrustersEnabled = true,
  29. correctScript = true,
  30. correctVersion = true,
  31. reset = false;
  32.  
  33. public double
  34. scriptVersion = 1.0,
  35. version = 1.0;
  36.  
  37. public string
  38. script = "NDS Aerodynamic Simulator",
  39. settingBackup = "";
  40.  
  41. public TimeSpan recheckTime = new TimeSpan(0, 0, 0);
  42.  
  43. public Program()
  44. {
  45.     Echo("Booting up");
  46.     LoadData();
  47.     if (updateFrequency == 1) Runtime.UpdateFrequency = UpdateFrequency.Update1;
  48.     else if (updateFrequency == 10) Runtime.UpdateFrequency = UpdateFrequency.Update10;
  49.     else Runtime.UpdateFrequency = UpdateFrequency.Update100;
  50.     GetController();
  51.     if (shipController != null) GetThrusters();
  52. }
  53.  
  54. public void Save() { }
  55.  
  56. public void Main(string argument, UpdateType updateSource)
  57. {
  58.     Echo("NDS Aerodynamic Simulator v" + scriptVersion);
  59.     if (argument.ToLower() == "reset") {
  60.         reset = true;
  61.         Me.CustomData = "";
  62.         Storage = "";
  63.     }
  64.     if (!reset)
  65.     {
  66.         if (Me.CustomData != settingBackup) LoadData();
  67.         try {
  68.             if (recheckTime.TotalSeconds >= recheckTimeInSeconds)
  69.             {
  70.                 recheckTime = new TimeSpan(0, 0, 0);
  71.                 GetController();
  72.                 if (shipController != null) GetThrusters();
  73.             }
  74.             else {
  75.                 recheckTime += Runtime.TimeSinceLastRun;
  76.                 if (shipController == null || !shipController.IsWorking || !shipController.CubeGrid.CubeExists(shipController.Position)) GetController();
  77.                 else CheckThrust();
  78.             }
  79.         } catch {
  80.             Echo("Error caught running script, rechecking controller and thrusters");
  81.             GetController();
  82.             if (shipController != null) GetThrusters();
  83.         }
  84.     } else Echo("Please recompile script for reset");
  85. }
  86.  
  87. public void CheckThrust()
  88. {
  89.     Echo("Controller found: " + shipController.CustomName);
  90.     Echo("Thrusters found: " + verticalThrusters.Count);
  91.     double gravity = shipController.GetNaturalGravity().Length() / 9.81;
  92.     bool enableThrusters = true, inGravity = gravity >= requiredGravityForAerodynamics, calculatedDown = false, downIsDown = true;
  93.     double speed = shipController.GetShipSpeed();
  94.     Echo("Speed: " + speed.ToString("N2"));
  95.     Echo("Gravity: " + gravity.ToString("N2"));
  96.     if (inGravity)
  97.     {
  98.         Vector3D velocity = Vector3D.TransformNormal(shipController.GetShipVelocities().LinearVelocity, MatrixD.Transpose(shipController.WorldMatrix));
  99.         speed *= Vector3D.Normalize(velocity).Z * -1.0;
  100.         if (allowBackwardsAerodynamics) speed = Math.Abs(speed);
  101.         Echo("Forward Speed: " + speed.ToString("N2"));
  102.         if (dynamicGravity) speed *= gravity;
  103.     }
  104.     enableThrusters = (!inGravity && !noVerticalThrustersInSpace) || (inGravity && speed >= requiredForwardSpeedToEnableThrusters);
  105.    
  106.     if (enableThrusters != thrustersEnabled)
  107.     {
  108.         Matrix matrix;
  109.         shipController.Orientation.GetMatrix(out matrix);
  110.         int thrusterIndex = 0;
  111.         while (thrusterIndex < verticalThrusters.Count)
  112.         {
  113.             try {
  114.                 if (!enableThrusters && inGravity && simulateFrictionWhenFalling && speed >= 1.0)
  115.                 {
  116.                     if (!calculatedDown)
  117.                     {
  118.                         calculatedDown = true;
  119.                         Vector3D position = new Vector3D(0, 0, 0), planetPosition = new Vector3D(0, 0, 0);
  120.                         if (shipController.TryGetPlanetPosition(out planetPosition))
  121.                         {
  122.                             position = shipController.GetPosition();
  123.                             MatrixD matrixD = shipController.WorldMatrix;
  124.                             downIsDown = Vector3D.Distance(position + (matrixD.Down * 5.0), planetPosition) < Vector3D.Distance(position + (matrixD.Up * 5.0), planetPosition);
  125.                         }
  126.                     }
  127.                     Matrix thrusterMatrix;
  128.                     verticalThrusters[thrusterIndex].Orientation.GetMatrix(out thrusterMatrix);
  129.                     if ((downIsDown && thrusterMatrix.Forward == matrix.Down) || (!downIsDown && thrusterMatrix.Forward == matrix.Up))
  130.                     {
  131.                         verticalThrusters[thrusterIndex].Enabled = true;
  132.                         verticalThrusters[thrusterIndex].ThrustOverridePercentage = (float)(verticalFrictionPercentage / 100.0);
  133.                     } else verticalThrusters[thrusterIndex].Enabled = false;
  134.                 } else {
  135.                     verticalThrusters[thrusterIndex].ThrustOverridePercentage = 0f;
  136.                     verticalThrusters[thrusterIndex].Enabled = enableThrusters;
  137.                 }
  138.                 thrusterIndex++;
  139.             } catch { verticalThrusters.RemoveAt(thrusterIndex); }
  140.         }
  141.         thrustersEnabled = enableThrusters;
  142.     }
  143. }
  144.  
  145. public void GetController()
  146. {
  147.     List<IMyShipController> controllers = new List<IMyShipController>();
  148.     GridTerminalSystem.GetBlocksOfType<IMyShipController>(controllers, b => b.IsWorking && ((IMyShipController)b).IsUnderControl && ((IMyShipController)b).CanControlShip);
  149.     if (controllers.Count == 0) GridTerminalSystem.GetBlocksOfType<IMyShipController>(controllers, b => b.IsWorking && b.CubeGrid == Me.CubeGrid && ((IMyShipController)b).CanControlShip);
  150.     if (controllers.Count == 0) GridTerminalSystem.GetBlocksOfType<IMyShipController>(controllers, b => b.IsWorking && ((IMyShipController)b).CanControlShip);
  151.     if (controllers.Count > 0) shipController = controllers[0];
  152.     else {
  153.         shipController = null;
  154.         Echo("No ship controller found, build a cockpit or remote control");
  155.     }
  156. }
  157.  
  158. public void GetThrusters()
  159. {
  160.     Matrix matrix;
  161.     shipController.Orientation.GetMatrix(out matrix);
  162.     GridTerminalSystem.GetBlocksOfType<IMyThrust>(verticalThrusters, b => VerticalThrust(b, matrix));
  163. }
  164.  
  165. public bool VerticalThrust(IMyThrust block, Matrix matrix)
  166. {
  167.     Matrix thrusterMatrix;
  168.     block.Orientation.GetMatrix(out thrusterMatrix);
  169.     return thrusterMatrix.Forward == matrix.Down || thrusterMatrix.Forward == matrix.Up;
  170. }
  171.  
  172. public void SaveData()
  173. {
  174.     try
  175.     {
  176.         StringBuilder settingBuilder = new StringBuilder();
  177.        
  178.         settingBuilder.AppendLine("requiredForwardSpeedToEnableThrusters=" + requiredForwardSpeedToEnableThrusters);
  179.         settingBuilder.AppendLine("requiredGravityForAerodynamics=" + requiredGravityForAerodynamics);
  180.         settingBuilder.AppendLine("recheckTimeInSeconds=" + recheckTimeInSeconds);
  181.         settingBuilder.AppendLine("verticalFrictionPercentage=" + verticalFrictionPercentage);
  182.         settingBuilder.AppendLine("allowBackwardsAerodynamics=" + allowBackwardsAerodynamics);
  183.         settingBuilder.AppendLine("simulateFrictionWhenFalling=" + simulateFrictionWhenFalling);
  184.         settingBuilder.AppendLine("dynamicGravity=" + dynamicGravity);
  185.         settingBuilder.AppendLine("noVerticalThrustersInSpace=" + noVerticalThrustersInSpace);
  186.         settingBuilder.AppendLine("updateFrequency=" + updateFrequency);
  187.         settingBuilder.AppendLine("version=" + version.ToString("N1"));
  188.         settingBuilder.AppendLine("script=" + script);
  189.        
  190.         Me.CustomData = settingBuilder.ToString().Replace("\r", String.Empty).Trim();
  191.         Storage = Me.CustomData;
  192.         settingBackup = Me.CustomData;
  193.         correctVersion = true;
  194.         correctScript = true;
  195.     }
  196.     catch { Echo("Error Saving Data"); }
  197. }
  198.  
  199. public void LoadData()
  200. {
  201.     correctVersion = false;
  202.     correctScript = false;
  203.     if (Me.CustomData != "" && Storage != Me.CustomData) Storage = Me.CustomData;
  204.     else if (Me.CustomData == "" && Storage != "") {
  205.         reset = true;
  206.         Storage = "";
  207.     }
  208.     if (Storage != "")
  209.     {
  210.         try
  211.         {
  212.             string[] settingArray = Storage.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
  213.             for (int i = 0; i < settingArray.Length; i++)
  214.                 ProcessSetting(settingArray[i]);
  215.         }
  216.         catch { Echo("Error loading data"); }
  217.     }
  218.     settingBackup = Me.CustomData;
  219.     if (!reset && (!correctScript || !correctVersion)) {
  220.         Echo("Updating Settings");
  221.         SaveData();
  222.     }
  223. }
  224.  
  225. public void ProcessSetting(string settingString)
  226. {
  227.     try
  228.     {
  229.         int index = settingString.IndexOf("=");
  230.         string settingKey = settingString.Substring(0, index),
  231.                settingValue = settingString.Substring(index + 1);
  232.         bool settingBool = settingValue.ToLower() == "true";
  233.         double settingDouble = 0;
  234.         try
  235.         {
  236.             settingDouble = double.Parse(settingValue);
  237.         }
  238.         catch { }
  239.         try
  240.         {
  241.             switch (settingKey)
  242.             {
  243.                 case "requiredForwardSpeedToEnableThrusters":
  244.                     requiredForwardSpeedToEnableThrusters = settingDouble;
  245.                 break;
  246.                 case "requiredGravityForAerodynamics":
  247.                     requiredGravityForAerodynamics = settingDouble;
  248.                 break;
  249.                 case "recheckTimeInSeconds":
  250.                     recheckTimeInSeconds = settingDouble;
  251.                 break;
  252.                 case "verticalFrictionPercentage":
  253.                     verticalFrictionPercentage = settingDouble;
  254.                 break;
  255.                 case "allowBackwardsAerodynamics":
  256.                     allowBackwardsAerodynamics = settingBool;
  257.                 break;
  258.                 case "simulateFrictionWhenFalling":
  259.                     simulateFrictionWhenFalling = settingBool;
  260.                 break;
  261.                 case "dynamicGravity":
  262.                     dynamicGravity = settingBool;
  263.                 break;
  264.                 case "noVerticalThrustersInSpace":
  265.                     noVerticalThrustersInSpace = settingBool;
  266.                 break;
  267.                 case "updateFrequency":
  268.                     updateFrequency = (int)settingDouble;
  269.                 break;
  270.                 case "version":
  271.                     if (settingDouble != version) correctVersion = false;
  272.                 break;
  273.                 case "script":
  274.                     if (script != settingValue) correctScript = false;
  275.                 break;
  276.             }
  277.             Echo("Processed Setting: " + settingString);
  278.         }
  279.         catch { Echo("Error Processing Setting: " + settingString); }
  280.     }
  281.     catch { Echo("Error Processing Setting: " + settingString); }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement