Advertisement
plaYer2k

Space Engineers - IMyShipController angular inputs to piston

Mar 26th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.34 KB | None | 0 0
  1. // Cockpit rotational input to piston movement //
  2.  
  3. // This is the name your reference ship controller / cockpit has to end with
  4. // i.e. "Cockpit - Reference", "Remote Control - Reference" etc.
  5. const string controllerName = "- Reference";
  6.  
  7. // The desired movement speed in m/s
  8. const float movementSpeed = 3f;
  9.  
  10. // The desired return speed multiplier
  11. // It gets multiplied with the movementSpeed so that -0.5f return speed and 5 m/s movementSpeed become -2.5 m/s return speed
  12. const float returnMultiplier = -0.5f;
  13.  
  14. // The names that the pistons have to end with
  15. // i.e. "Piston +x", "Piston -z"
  16. string[] pistonNames = new string[]{"+x", "-x", "+y", "-y", "+z", "-z"};
  17.  
  18.  
  19. // Set this value higher than 0f if you want to have a standard velocity for all inputs
  20. // Note that this should match your return multiplier if you want your extract speed to match the retract speed
  21. const float standardVelocity = 0f;
  22.  
  23. // DONT CHANGE BELOW
  24. List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
  25. IMyShipController controller;
  26.  
  27. Program()
  28. {
  29.     Runtime.UpdateFrequency = UpdateFrequency.Update1;
  30. }
  31.  
  32. public void Main(string arg)
  33. {
  34.     if(controller == null || !controller.IsFunctional)
  35.     {
  36.         GridTerminalSystem.GetBlocksOfType<IMyShipController>(blocks, x => x.IsFunctional || x.CustomName.EndsWith(controllerName));
  37.         if(blocks.Count < 1)
  38.         {
  39.             Echo("No ship controller found\nTerminating");
  40.             return;
  41.         }
  42.         controller = blocks[0] as IMyShipController;
  43.     }
  44.     GridTerminalSystem.GetBlocksOfType<IMyPistonBase>(blocks);
  45.     Echo($"{blocks.Count} Pistons found");
  46.     foreach(IMyTerminalBlock block in blocks)
  47.     {
  48.         for(int i = 0; i < pistonNames.Length; i++)
  49.         {
  50.             if(!block.CustomName.EndsWith(pistonNames[i]))
  51.                 continue;
  52.  
  53.             IMyPistonBase piston = block as IMyPistonBase;
  54.             switch(i)
  55.             {
  56.                 case 0:
  57.                     if(controller.RotationIndicator.X > 0)
  58.                         piston.Velocity = movementSpeed * (standardVelocity > 0f ? standardVelocity : controller.RotationIndicator.X);
  59.                     else
  60.                         piston.Velocity = movementSpeed * returnMultiplier;
  61.                     break;
  62.  
  63.                 case 1:
  64.                     if(controller.RotationIndicator.X < 0)
  65.                         piston.Velocity = movementSpeed * (standardVelocity > 0f ? standardVelocity : -controller.RotationIndicator.X);
  66.                     else
  67.                         piston.Velocity = movementSpeed * returnMultiplier;
  68.                     break;
  69.  
  70.                 case 2:
  71.                     if(controller.RotationIndicator.Y > 0)
  72.                         piston.Velocity = movementSpeed * (standardVelocity > 0f ? standardVelocity : controller.RotationIndicator.Y);
  73.                     else
  74.                         piston.Velocity = movementSpeed * returnMultiplier;
  75.                     break;
  76.  
  77.                 case 3:
  78.                     if(controller.RotationIndicator.Y < 0)
  79.                         piston.Velocity = movementSpeed * (standardVelocity > 0f ? standardVelocity : -controller.RotationIndicator.Y);
  80.                     else
  81.                         piston.Velocity = movementSpeed * returnMultiplier;
  82.                     break;
  83.  
  84.                 case 4:
  85.                     if(controller.RollIndicator > 0)
  86.                         piston.Velocity = movementSpeed * (standardVelocity > 0f ? standardVelocity : controller.RollIndicator);
  87.                     else
  88.                         piston.Velocity = movementSpeed * returnMultiplier;
  89.                     break;
  90.  
  91.                 case 5:
  92.                     if(controller.RollIndicator < 0)
  93.                         piston.Velocity = movementSpeed * (standardVelocity > 0f ? standardVelocity : -controller.RollIndicator);
  94.                     else
  95.                         piston.Velocity = movementSpeed * returnMultiplier;
  96.                     break;
  97.  
  98.                 default:
  99.                     break;
  100.             }
  101.             break;
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement