Advertisement
Whiplash141

Whip's Thruster Efficiency Manager v3

Jun 8th, 2016
2,304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.45 KB | None | 0 0
  1.  
  2. /*
  3. /// Whip's Thruster Speed Limiter v3 /// - 9/22/16
  4.  
  5. ________________________________________________________
  6. ///DESCRIPTION///
  7.  
  8.     This code limits your ship's top speed by shutting off any thrusters
  9.     that would speed you up beyond the limit you set. This means that if
  10.     you are traveling backwards at the speed limit, ONLY the thrusters
  11.     pushing you backwards are shut off. This means that you can use this
  12.     on planets without fear of your thrusters shutting off ar the wrong time!
  13.    
  14. ________________________________________________________
  15. ///HOW DO I USE THIS?///
  16.  
  17. ===Required===
  18.  
  19.     1) Put this code in a program block
  20.    
  21.     2) Make a timer and set it to:
  22.         - "Run with no arguments" the program
  23.         - "Start" itself
  24.         - "Trigger Now" itself
  25.        
  26.     3) Start the timer
  27.    
  28.     4) Enjoy!
  29.    
  30. ===Optional===
  31.  
  32.     - To make the code limit only the thrusters that you specify set manageAllThrust
  33.       to false and put the name tag "Limit" in the thrusters that you want speed limited
  34.  
  35. ________________________________________________________
  36. ///AUTHOR'S NOTES///  
  37.  
  38.     I made this code a while back to try and avoid the rotor safety lock bugs and decided
  39.     that I might as well release it to the public. If you have any questions, be sure to
  40.     leave a comment on the workshop page!
  41.    
  42.     - Whiplash141
  43. */
  44.  
  45.  
  46.  
  47.  
  48. //------------------------------------------------------
  49. //============ Configurable Variables ================
  50. //------------------------------------------------------
  51.  
  52.     const double maxSpeed = 98; // this is the max speed that you want your ship to go
  53.    
  54.     bool manageAllThrust = true; //this tells the code whether to limit all thrusters
  55.    
  56.     // If manageAllThrust is set to FALSE, the code will only limit thrusters with the following tag
  57.     //----------------------------------------
  58.         string thrustToManageTag = "Limit";
  59.     //----------------------------------------
  60.  
  61. //------------------------------------------------------
  62. //============ No touch below this line ================
  63. //------------------------------------------------------
  64.  
  65.     List<IMyTerminalBlock> shipControllers = new List<IMyTerminalBlock>();
  66.     List<IMyTerminalBlock> thrusters = new List<IMyTerminalBlock>();
  67.  
  68.     const int updatesPerSecond = 10;
  69.     const double timeMaxCycle = 1 / (double)updatesPerSecond;
  70.  
  71.     double timeCurrentCycle = 0;
  72.  
  73. void Main(string arg)
  74. {
  75.     timeCurrentCycle += Runtime.TimeSinceLastRun.TotalSeconds;
  76.     timeSymbol += Runtime.TimeSinceLastRun.TotalSeconds;
  77.  
  78.     if (timeCurrentCycle >= timeMaxCycle)
  79.     {
  80.         Echo("WMI Thrust Manager Active... " + RunningSymbol());
  81.         ManageThrusters();
  82.         timeCurrentCycle = 0;
  83.     }
  84. }
  85.  
  86. void ManageThrusters()
  87. {
  88.     bool critError = false;
  89.  
  90.     GridTerminalSystem.GetBlocksOfType<IMyShipController>(shipControllers);
  91.     if (shipControllers.Count == 0)
  92.     {
  93.         Echo("Critical Error: No ship controllers were found");
  94.         critError = true;
  95.     }
  96.  
  97.     if (manageAllThrust)
  98.         GridTerminalSystem.GetBlocksOfType<IMyThrust>(thrusters);
  99.     else
  100.         GridTerminalSystem.SearchBlocksOfName(thrustToManageTag, thrusters, block => block is IMyThrust);
  101.    
  102.    
  103.     if (thrusters.Count == 0)
  104.     {
  105.         Echo("Critical Error: No thrusters were found");
  106.         critError = true;
  107.     }
  108.  
  109.     if (critError) return;
  110.  
  111.     var thisController = shipControllers[0] as IMyShipController;
  112.  
  113.     Vector3D velocityVec = thisController.GetShipVelocities().LinearVelocity;
  114.     double speed = velocityVec.Length();
  115.  
  116.     Echo("Current Speed: " + speed.ToString() + "\nMax Speed: " + maxSpeed.ToString());
  117.  
  118.     if (speed >= maxSpeed)
  119.     {
  120.         Echo("Speed is over max...\nManaging thrust");
  121.  
  122.         foreach (IMyThrust thisThrust in thrusters)
  123.         {
  124.             Vector3D thrustVec = thisThrust.WorldMatrix.Backward;
  125.             bool isSameDirection = VectorSameDirection(thrustVec, velocityVec);
  126.  
  127.             if (isSameDirection)
  128.             {
  129.                 ThrusterTurnOff(thisThrust);
  130.             }
  131.             else
  132.             {
  133.                 ThrusterTurnOn(thisThrust);
  134.             }
  135.         }
  136.     }
  137.     else
  138.     {
  139.         Echo("Speed is under max");
  140.        
  141.         for (int i = 0; i < thrusters.Count; i++)
  142.         {
  143.             var thisThrust = thrusters[i] as IMyThrust;
  144.             ThrusterTurnOn(thisThrust);
  145.         }
  146.     }
  147. }
  148.  
  149. bool VectorSameDirection(Vector3D a, Vector3D b)
  150. {
  151.     double check = a.Dot(b);
  152.     if (check <= 0)
  153.         return false;
  154.     else
  155.         return true;
  156. }
  157.  
  158. void ThrusterTurnOn(IMyThrust thruster_block)
  159. {
  160.     bool isOn = thruster_block.GetValue<bool>("OnOff");
  161.     if (!isOn)
  162.     {
  163.         thruster_block.ApplyAction("OnOff_On");
  164.     }
  165. }
  166.  
  167. void ThrusterTurnOff(IMyThrust thruster_block)
  168. {
  169.     bool isOn = thruster_block.GetValue<bool>("OnOff");
  170.     if (isOn)
  171.     {
  172.         thruster_block.ApplyAction("OnOff_Off");
  173.     }
  174. }
  175.  
  176. //Whip's Running Symbol Method v3
  177. double timeSymbol = 0;
  178. string strRunningSymbol = "";
  179.  
  180. string RunningSymbol()
  181. {
  182.     if (timeSymbol < .1d)
  183.         strRunningSymbol = "|";
  184.     else if (timeSymbol < .2d)
  185.         strRunningSymbol = "/";
  186.     else if (timeSymbol < .3d)
  187.         strRunningSymbol = "--";
  188.     else if (timeSymbol < .4d)
  189.         strRunningSymbol = "\\";
  190.     else
  191.     {
  192.         timeSymbol = 0;
  193.         strRunningSymbol = "|";
  194.     }
  195.  
  196.     return strRunningSymbol;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement