Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2015
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.19 KB | None | 0 0
  1. /* http://forums.keenswh.com/post/automated-turrets-how-programmable-block-7307510 */
  2.  
  3. string sensorTag = "Sensor Zone";
  4. void Main()
  5. {
  6.     //fire is origin
  7.     //Loop is front
  8.     //programmable is up
  9.     //RotorY is right
  10.     List<IMyTerminalBlock> turretBlocks = new List<IMyTerminalBlock>();
  11.     GridTerminalSystem.SearchBlocksOfName("Turret1", turretBlocks);
  12.  
  13.     List<IMyTerminalBlock> sensorZones = new List<IMyTerminalBlock>();
  14.     GridTerminalSystem.GetBlocksOfType<IMySensorBlock>(sensorZones);
  15.  
  16.     //Grab the first target from the sensors.
  17.     IMySensorBlock sensorBlock = null;
  18.     for(int i=0;i<sensorZones.Count;i++)
  19.     {
  20.         if(sensorZones[i].CustomName.Contains(sensorTag) && (sensorZones[i] as IMySensorBlock).LastDetectedEntity != null)
  21.         {
  22.             sensorBlock = sensorZones[i] as IMySensorBlock;
  23.             i = sensorZones.Count;
  24.         }
  25.     }
  26.  
  27.     IMyTimerBlock loopTimer = null;
  28.     IMyTimerBlock fireTimer = null;
  29.     IMyProgrammableBlock programmable = null;
  30.     IMyMotorStator rotorx = null;
  31.     IMyMotorStator rotory = null;
  32.  
  33.     for(int i=0;i<turretBlocks.Count;i++)
  34.     {
  35.         if(turretBlocks[i].CustomName.Contains("Loop"))
  36.             loopTimer = turretBlocks[i] as IMyTimerBlock;
  37.         else if(turretBlocks[i].CustomName.Contains("Fire"))
  38.             fireTimer = turretBlocks[i] as IMyTimerBlock;
  39.         else if(turretBlocks[i].CustomName.Contains("Program"))
  40.             programmable = turretBlocks[i] as IMyProgrammableBlock;
  41.         else if(turretBlocks[i].CustomName.Contains("Rotor X"))
  42.             rotorx = turretBlocks[i] as IMyMotorStator;
  43.         else if(turretBlocks[i].CustomName.Contains("Rotor Y"))
  44.             rotory = turretBlocks[i] as IMyMotorStator;
  45.     }
  46.  
  47.     Vector3D sensorTarget = new Vector3D(0,0,0);
  48.     if(sensorBlock != null && sensorBlock.LastDetectedEntity != null)
  49.     {
  50.         sensorTarget = sensorBlock.LastDetectedEntity.GetPosition();
  51.  
  52.             var x = Math.Floor(sensorTarget.GetDim(0)).ToString();
  53.             var y = Math.Floor(sensorTarget.GetDim(1)).ToString();
  54.             var z = Math.Floor(sensorTarget.GetDim(2)).ToString();
  55.  
  56.         float range = (float)((fireTimer.GetPosition() - sensorTarget).Length());
  57.  
  58.         float gyroPitch = 0;
  59.         float gyroYaw = 0;
  60.  
  61.         GetDirectionTo(sensorTarget,
  62.                                  fireTimer as IMyTerminalBlock,
  63.                                  loopTimer as IMyTerminalBlock,
  64.                                  programmable as IMyTerminalBlock,
  65.                                  rotory as IMyTerminalBlock,
  66.                                  ref gyroPitch, ref gyroYaw);
  67.  
  68.         rotorx.SetValueFloat("Velocity", gyroYaw);
  69.         rotory.SetValueFloat("Velocity", gyroPitch);
  70.  
  71.         List<IMyTerminalBlock> thrusters = new List<IMyTerminalBlock>();
  72.         GridTerminalSystem.GetBlocksOfType<IMyThrust>(thrusters);
  73.  
  74.         if((gyroPitch > -5)&&(gyroPitch < 5)&&
  75.            (gyroYaw > -5)&&(gyroYaw < 5))
  76.         {
  77.             fireTimer.GetActionWithName("TriggerNow").Apply(fireTimer);
  78.         }
  79.     }
  80.     else
  81.     {
  82.         //turn off rotors.
  83.         rotorx.SetValueFloat("Velocity", 0.0f);
  84.         rotory.SetValueFloat("Velocity", 0.0f);
  85.     }
  86.     loopTimer.GetActionWithName("TriggerNow").Apply(loopTimer);
  87. }
  88.  
  89. /*
  90. VRageMath.Vector3D TV         This is the target vector.
  91. IMyTerminalBlock Origin       Origin block.
  92. IMyTerminalBlock Forward      Block directly in front of the origin.
  93. IMyTerminalBlock Up           Block directly above the origin.
  94. IMyTerminalBlock Right        Block directly to the right of the origin.
  95. ref double Pitch              Reference to Pitch.
  96. ref double Yaw                Reference to Yaw.
  97. */
  98. void GetDirectionTo(VRageMath.Vector3D TV,
  99.                     IMyTerminalBlock Origin,
  100.                     IMyTerminalBlock Forward,
  101.                     IMyTerminalBlock Up,
  102.                     IMyTerminalBlock Right,
  103.                     ref float Pitch, ref float Yaw)
  104. {
  105.   VRageMath.Vector3D OV = Origin.GetPosition();//Get positions of reference blocks.
  106.   VRageMath.Vector3D FV = Forward.GetPosition();
  107.   VRageMath.Vector3D UV = Up.GetPosition();
  108.   VRageMath.Vector3D RV = Right.GetPosition();
  109.  
  110.   float TVOV = (float)((OV - TV).Length());//Get magnitudes of vectors.
  111.  
  112.   float TVFV = (float)((FV - TV).Length());
  113.   float TVUV = (float)((UV - TV).Length());
  114.   float TVRV = (float)((RV - TV).Length());
  115.  
  116.   float OVFV = (float)((FV - OV).Length());
  117.   float OVUV = (float)((UV - OV).Length());
  118.   float OVRV = (float)((RV - OV).Length());
  119.  
  120.   float ThetaP = (float)(Math.Acos((TVUV * TVUV - OVUV * OVUV - TVOV * TVOV) / (-2 * OVUV * TVOV)));
  121.   //Use law of cosines to determine angles.
  122.   float ThetaY = (float)(Math.Acos((TVRV * TVRV - OVRV * OVRV - TVOV * TVOV) / (-2 * OVRV * TVOV)));
  123.  
  124.   float RPitch = (float)(90 - (ThetaP * 180 / Math.PI));//Convert from radians to degrees.
  125.   float RYaw = (float)(90 - (ThetaY * 180 / Math.PI));
  126.  
  127.   if (TVOV < TVFV) RPitch = 180 - RPitch;//Normalize angles to -180 to 180 degrees.
  128.   if (RPitch > 180) RPitch = -1 * (360 - RPitch);
  129.  
  130.   if (TVOV < TVFV) RYaw = 180 - RYaw;
  131.   if (RYaw > 180) RYaw = -1 * (360 - RYaw);
  132.  
  133.   Pitch = RPitch;//Set Pitch and Yaw outputs.
  134.   Yaw = RYaw;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement