Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public double
- requiredForwardSpeedToEnableThrusters = 25,
- requiredGravityForAerodynamics = 0.05,
- recheckTimeInSeconds = 15,
- verticalFrictionPercentage = 10;
- public bool
- allowBackwardsAerodynamics = false,
- simulateFrictionWhenFalling = false,
- dynamicGravity = true,
- noVerticalThrustersInSpace = false;
- public int updateFrequency = 100; // 100, 10, or 1
- /**-------------------------
- END OF USER SETTINGS
- -------------------------**/
- public List<IMyThrust> verticalThrusters = new List<IMyThrust>();
- public List<int> downThrustIndexes = new List<int>();
- public IMyShipController shipController;
- public bool
- thrustersEnabled = true,
- correctScript = true,
- correctVersion = true,
- reset = false;
- public double
- scriptVersion = 1.0,
- version = 1.0;
- public string
- script = "NDS Aerodynamic Simulator",
- settingBackup = "";
- public TimeSpan recheckTime = new TimeSpan(0, 0, 0);
- public Program()
- {
- Echo("Booting up");
- LoadData();
- if (updateFrequency == 1) Runtime.UpdateFrequency = UpdateFrequency.Update1;
- else if (updateFrequency == 10) Runtime.UpdateFrequency = UpdateFrequency.Update10;
- else Runtime.UpdateFrequency = UpdateFrequency.Update100;
- GetController();
- if (shipController != null) GetThrusters();
- }
- public void Save() { }
- public void Main(string argument, UpdateType updateSource)
- {
- Echo("NDS Aerodynamic Simulator v" + scriptVersion);
- if (argument.ToLower() == "reset") {
- reset = true;
- Me.CustomData = "";
- Storage = "";
- }
- if (!reset)
- {
- if (Me.CustomData != settingBackup) LoadData();
- try {
- if (recheckTime.TotalSeconds >= recheckTimeInSeconds)
- {
- recheckTime = new TimeSpan(0, 0, 0);
- GetController();
- if (shipController != null) GetThrusters();
- }
- else {
- recheckTime += Runtime.TimeSinceLastRun;
- if (shipController == null || !shipController.IsWorking || !shipController.CubeGrid.CubeExists(shipController.Position)) GetController();
- else CheckThrust();
- }
- } catch {
- Echo("Error caught running script, rechecking controller and thrusters");
- GetController();
- if (shipController != null) GetThrusters();
- }
- } else Echo("Please recompile script for reset");
- }
- public void CheckThrust()
- {
- Echo("Controller found: " + shipController.CustomName);
- Echo("Thrusters found: " + verticalThrusters.Count);
- double gravity = shipController.GetNaturalGravity().Length() / 9.81;
- bool enableThrusters = true, inGravity = gravity >= requiredGravityForAerodynamics, calculatedDown = false, downIsDown = true;
- double speed = shipController.GetShipSpeed();
- Echo("Speed: " + speed.ToString("N2"));
- Echo("Gravity: " + gravity.ToString("N2"));
- if (inGravity)
- {
- Vector3D velocity = Vector3D.TransformNormal(shipController.GetShipVelocities().LinearVelocity, MatrixD.Transpose(shipController.WorldMatrix));
- speed *= Vector3D.Normalize(velocity).Z * -1.0;
- if (allowBackwardsAerodynamics) speed = Math.Abs(speed);
- Echo("Forward Speed: " + speed.ToString("N2"));
- if (dynamicGravity) speed *= gravity;
- }
- enableThrusters = (!inGravity && !noVerticalThrustersInSpace) || (inGravity && speed >= requiredForwardSpeedToEnableThrusters);
- if (enableThrusters != thrustersEnabled)
- {
- Matrix matrix;
- shipController.Orientation.GetMatrix(out matrix);
- int thrusterIndex = 0;
- while (thrusterIndex < verticalThrusters.Count)
- {
- try {
- if (!enableThrusters && inGravity && simulateFrictionWhenFalling && speed >= 1.0)
- {
- if (!calculatedDown)
- {
- calculatedDown = true;
- Vector3D position = new Vector3D(0, 0, 0), planetPosition = new Vector3D(0, 0, 0);
- if (shipController.TryGetPlanetPosition(out planetPosition))
- {
- position = shipController.GetPosition();
- MatrixD matrixD = shipController.WorldMatrix;
- downIsDown = Vector3D.Distance(position + (matrixD.Down * 5.0), planetPosition) < Vector3D.Distance(position + (matrixD.Up * 5.0), planetPosition);
- }
- }
- Matrix thrusterMatrix;
- verticalThrusters[thrusterIndex].Orientation.GetMatrix(out thrusterMatrix);
- if ((downIsDown && thrusterMatrix.Forward == matrix.Down) || (!downIsDown && thrusterMatrix.Forward == matrix.Up))
- {
- verticalThrusters[thrusterIndex].Enabled = true;
- verticalThrusters[thrusterIndex].ThrustOverridePercentage = (float)(verticalFrictionPercentage / 100.0);
- } else verticalThrusters[thrusterIndex].Enabled = false;
- } else {
- verticalThrusters[thrusterIndex].ThrustOverridePercentage = 0f;
- verticalThrusters[thrusterIndex].Enabled = enableThrusters;
- }
- thrusterIndex++;
- } catch { verticalThrusters.RemoveAt(thrusterIndex); }
- }
- thrustersEnabled = enableThrusters;
- }
- }
- public void GetController()
- {
- List<IMyShipController> controllers = new List<IMyShipController>();
- GridTerminalSystem.GetBlocksOfType<IMyShipController>(controllers, b => b.IsWorking && ((IMyShipController)b).IsUnderControl && ((IMyShipController)b).CanControlShip);
- if (controllers.Count == 0) GridTerminalSystem.GetBlocksOfType<IMyShipController>(controllers, b => b.IsWorking && b.CubeGrid == Me.CubeGrid && ((IMyShipController)b).CanControlShip);
- if (controllers.Count == 0) GridTerminalSystem.GetBlocksOfType<IMyShipController>(controllers, b => b.IsWorking && ((IMyShipController)b).CanControlShip);
- if (controllers.Count > 0) shipController = controllers[0];
- else {
- shipController = null;
- Echo("No ship controller found, build a cockpit or remote control");
- }
- }
- public void GetThrusters()
- {
- Matrix matrix;
- shipController.Orientation.GetMatrix(out matrix);
- GridTerminalSystem.GetBlocksOfType<IMyThrust>(verticalThrusters, b => VerticalThrust(b, matrix));
- }
- public bool VerticalThrust(IMyThrust block, Matrix matrix)
- {
- Matrix thrusterMatrix;
- block.Orientation.GetMatrix(out thrusterMatrix);
- return thrusterMatrix.Forward == matrix.Down || thrusterMatrix.Forward == matrix.Up;
- }
- public void SaveData()
- {
- try
- {
- StringBuilder settingBuilder = new StringBuilder();
- settingBuilder.AppendLine("requiredForwardSpeedToEnableThrusters=" + requiredForwardSpeedToEnableThrusters);
- settingBuilder.AppendLine("requiredGravityForAerodynamics=" + requiredGravityForAerodynamics);
- settingBuilder.AppendLine("recheckTimeInSeconds=" + recheckTimeInSeconds);
- settingBuilder.AppendLine("verticalFrictionPercentage=" + verticalFrictionPercentage);
- settingBuilder.AppendLine("allowBackwardsAerodynamics=" + allowBackwardsAerodynamics);
- settingBuilder.AppendLine("simulateFrictionWhenFalling=" + simulateFrictionWhenFalling);
- settingBuilder.AppendLine("dynamicGravity=" + dynamicGravity);
- settingBuilder.AppendLine("noVerticalThrustersInSpace=" + noVerticalThrustersInSpace);
- settingBuilder.AppendLine("updateFrequency=" + updateFrequency);
- settingBuilder.AppendLine("version=" + version.ToString("N1"));
- settingBuilder.AppendLine("script=" + script);
- Me.CustomData = settingBuilder.ToString().Replace("\r", String.Empty).Trim();
- Storage = Me.CustomData;
- settingBackup = Me.CustomData;
- correctVersion = true;
- correctScript = true;
- }
- catch { Echo("Error Saving Data"); }
- }
- public void LoadData()
- {
- correctVersion = false;
- correctScript = false;
- if (Me.CustomData != "" && Storage != Me.CustomData) Storage = Me.CustomData;
- else if (Me.CustomData == "" && Storage != "") {
- reset = true;
- Storage = "";
- }
- if (Storage != "")
- {
- try
- {
- string[] settingArray = Storage.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
- for (int i = 0; i < settingArray.Length; i++)
- ProcessSetting(settingArray[i]);
- }
- catch { Echo("Error loading data"); }
- }
- settingBackup = Me.CustomData;
- if (!reset && (!correctScript || !correctVersion)) {
- Echo("Updating Settings");
- SaveData();
- }
- }
- public void ProcessSetting(string settingString)
- {
- try
- {
- int index = settingString.IndexOf("=");
- string settingKey = settingString.Substring(0, index),
- settingValue = settingString.Substring(index + 1);
- bool settingBool = settingValue.ToLower() == "true";
- double settingDouble = 0;
- try
- {
- settingDouble = double.Parse(settingValue);
- }
- catch { }
- try
- {
- switch (settingKey)
- {
- case "requiredForwardSpeedToEnableThrusters":
- requiredForwardSpeedToEnableThrusters = settingDouble;
- break;
- case "requiredGravityForAerodynamics":
- requiredGravityForAerodynamics = settingDouble;
- break;
- case "recheckTimeInSeconds":
- recheckTimeInSeconds = settingDouble;
- break;
- case "verticalFrictionPercentage":
- verticalFrictionPercentage = settingDouble;
- break;
- case "allowBackwardsAerodynamics":
- allowBackwardsAerodynamics = settingBool;
- break;
- case "simulateFrictionWhenFalling":
- simulateFrictionWhenFalling = settingBool;
- break;
- case "dynamicGravity":
- dynamicGravity = settingBool;
- break;
- case "noVerticalThrustersInSpace":
- noVerticalThrustersInSpace = settingBool;
- break;
- case "updateFrequency":
- updateFrequency = (int)settingDouble;
- break;
- case "version":
- if (settingDouble != version) correctVersion = false;
- break;
- case "script":
- if (script != settingValue) correctScript = false;
- break;
- }
- Echo("Processed Setting: " + settingString);
- }
- catch { Echo("Error Processing Setting: " + settingString); }
- }
- catch { Echo("Error Processing Setting: " + settingString); }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement