Advertisement
Whiplash141

Whip's GPS Waypoint Distance Script v1

Mar 30th, 2016
2,547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1.  
  2. //Whip's GPS Waypoint Distance Script v1 - revision: 3/30/16
  3.  
  4. /*
  5. Instructions:
  6. 1) Add this script onto your ship
  7.  
  8. 2) Name a block on your ship "Reference"
  9.  
  10. 3) Copy paste your GPS coordinate into the argument and hit run
  11.     If you do it manually, the form is GPS:[Name of waypoint]:[x]:[y]:[z]:
  12.    
  13. 4) Read the program's readout for the distance to waypoint :D
  14.  
  15. Code by Whiplash141
  16. */
  17.  
  18. string strReferenceName = "Reference"; //name of block that we are using to measure distance, can be any terminal block
  19.  
  20. void Main( string arg )
  21. {
  22.     Echo( "WMI Waypoint Distance Script\n" );
  23.     List<IMyTerminalBlock> listReferences = new List<IMyTerminalBlock>();
  24.     GridTerminalSystem.SearchBlocksOfName( strReferenceName, listReferences );
  25.     IMyTerminalBlock referenceBlock = listReferences[0];
  26.    
  27.     Vector3D shipPositionVec;
  28.     if( referenceBlock == null ) //checks for reference block to measure distance from
  29.     {
  30.         Echo("Error: No reference block named " + strReferenceName + " was found");
  31.         return;
  32.     }else{
  33.         shipPositionVec = referenceBlock.GetPosition();
  34.     }
  35.    
  36.     if( !arg.ToLower().Contains( "gps" ) ) //checks if we have the correct naming
  37.     {
  38.         Echo("Error: GPS coordinate " + arg
  39.             + " could not be understod\nPlease input coordinates in the form\nGPS:[Name of waypoint]:[x]:[y]:[z]:");
  40.         return;
  41.     }
  42.    
  43.     string[] split_arg = arg.Split(':'); //splits argument at the colons
  44.     if( split_arg.Length < 5 )
  45.     {
  46.         Echo("Error: GPS coordinate " + arg
  47.         + " could not be understod\nPlease input coordinates in the form\nGPS:[Name of waypoint]:[x]:[y]:[z]:");
  48.         return;
  49.     }else{
  50.         string strWaypointName = split_arg[1];
  51.        
  52.         Vector3D waypointVec;
  53.         waypointVec.X = StringToDouble( split_arg[2] );
  54.         waypointVec.Y = StringToDouble( split_arg[3] );
  55.         waypointVec.Z = StringToDouble( split_arg[4] );
  56.        
  57.         double distance = Math.Round( Vector3D.Distance( waypointVec, shipPositionVec ), 2 );
  58.        
  59.         Echo( "Distance from Waypoint '" + strWaypointName + "': "
  60.             + distance.ToString() + " meters" );
  61.     }
  62.    
  63. }
  64.  
  65. double StringToDouble( string value )
  66. {
  67.     double n;
  68.     bool isDouble = double.TryParse(value, out n);
  69.         if (isDouble)
  70.             return n;
  71.         else
  72.             return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement