Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1.  
  2. using MathHelper
  3.  
  4. // Change the names here to whatever your pistons and rotor are named
  5. // Or change the piston and rotor names to whatever the name below is
  6. const string HorizontalPistonName = "Piston Horizontal";
  7. const string VerticalPistonName = "Piston Vertical";
  8. const string RotorName = "Rotor";
  9. const string DrillName = "Drill";
  10.  
  11. float HorizontalTarget = 0.0f;
  12. float VerticalTarget = -2.0f; // Band-aid
  13. double timeElapsed = 0;
  14. bool FirstRun = true;
  15. int sign = 1;
  16.  
  17. void Main (){
  18.    
  19.     IMyPistonBase HorizontalPiston = GridTerminalSystem.GetBlockWithName(HorizontalPistonName);
  20.     IMyPistonBase VerticalPiston = GridTerminalSystem.GetBlockWithName(VerticalPistonName);
  21.     IMyMotorStator Rotor = GridTerminalSystem.GetBlockWithName(RotorName);
  22.     IMyShipDrill Drill = GridTerminalSystem.GetBlockWithName(DrillName);
  23.     IMyInventory DrillInventory = Drill.GetInventory();
  24.    
  25.     // Pause if inventory is full
  26.     if (DrillInventory.IsFull){
  27.         Drill.Enabled = false;
  28.         return;
  29.     }
  30.     else{
  31.         Drill.Enabled = true;
  32.     }
  33.    
  34.     timeElapsed += Runtime.TimeSinceLastRun.TotalSeconds;
  35.    
  36.     if (FirstRun) {
  37.         init(HorizontalPiston, VerticalPiston, Rotor);
  38.         FirstRun = false;
  39.     }
  40.    
  41.     // One full revolution
  42.     if (timeElapsed > (60 / Rotor.TargetVelocityRPM) + 2){
  43.         if (HorizontalTarget == 10){
  44.             sign = -1;
  45.             VerticalTarget += 2;
  46.         }
  47.         if (HorizontalTarget == 0){
  48.             if (VerticalTarget == 10){
  49.                 Drill.Enabled = false;
  50.                 MovePistonTo(HorizontalPiston, 0);
  51.                 MovePistonTo(VerticalPiston, 0);
  52.                 Rotor.TargetVelocityRPM = 0.0f;
  53.                 Me.Enabled = false;
  54.             }
  55.             sign = 1;
  56.             VerticalTarget += 2;
  57.         }
  58.         HorizontalTarget += sign*2;
  59.         MovePistonTo(HorizontalPiston, HorizontalTarget);
  60.         MovePistonTo(VerticalPiston, VerticalTarget);
  61.         timeElapsed = 0;
  62.     }
  63. }
  64.  
  65. void init (IMyPistonBase H, IMyPistonBase V, IMyMotorStator R){
  66.     H.MaxLimit = 0.0f;
  67.     H.MinLimit = 0.0f;
  68.     H.Velocity = 0.5f;
  69.    
  70.     V.MaxLimit = 0.0f;
  71.     V.MinLimit = 0.0f;
  72.     V.Velocity = 0.5f;
  73.    
  74.     R.Torque = 1000.0f;
  75.     R.BrakingTorque = 1000.0f;
  76.     R.TargetVelocityRPM = 0.25f;
  77.    
  78.     return;
  79. }
  80.  
  81. void MovePistonTo (IMyPistonBase Piston, float Target){
  82.     // Set the target
  83.     MathHelper.Clamp(Target, 0.0f, 10.0f);
  84.     Piston.MaxLimit = Target;
  85.     Piston.MinLimit = Target;
  86.     // Set the direction acording to the target
  87.     if ((Piston.CurrentPostition < Target && Piston.Velocity < 0) || (Piston.CurrentPostition > Target && Piston.Velocity > 0))
  88.         Piston.Reverse();
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement