Advertisement
Whiplash141

Whip's Planetary Compass and Bearing Script v8.2

Mar 30th, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.48 KB | None | 0 0
  1.  
  2. /*
  3. /// Whip's Compass & Bearing Code v8.2 - 3/30/16 ///
  4.  
  5. What's Stable?
  6. - Bearing measured off relelative north direction
  7.  
  8. How do I use this?
  9.     1) Make a program block with this script loaded into it
  10.      
  11.     2) Make a remote control pointing forward
  12.         - Add the phrase "Reference" somewhere in its name.
  13.      
  14.     3) Make a text panel or LCD screen: Add the phrase "Bearing" in the name.
  15.         Also make sure the screen is set to show public text.
  16.      
  17.     4) Make a timer block
  18.         - set to "trigger now" itself
  19.         - set to "start" itself
  20.         - set to "run program with default argument"
  21.          
  22.     5) Trigger the timer once and you are good to go! :)
  23.  
  24.     (Optional): Tinker with the text size until the compass stretches
  25.                 the width of the screen. Recommended text size for
  26.                 small ship screens is 1.7 size font
  27.      
  28. Be sure to drop by my workshop page and leave a comment :D
  29. http://steamcommunity.com/sharedfiles/filedetails/?id=616627882
  30.  
  31. Code by Whiplash141
  32. */
  33.  
  34.  
  35. //You can change these names if you'd like
  36. const string strRemoteName = "Reference"; //name of remote to use as reference
  37. const string strScreenName = "Bearing"; //name of screen to display bearing on
  38.  
  39.  /*
  40. _______________________________________________________________________________________
  41.  
  42. ======================== Don't edit anything beyond this :) =======================
  43. _______________________________________________________________________________________
  44. */
  45.  
  46. Vector3D absoluteNorthVec = new Vector3D(0, 0, 1); //by convention +z is north
  47. double bearingAngle; //measured off relative north
  48. const double rad2deg = 180 / Math.PI; //constant to convert radians to degrees
  49. const string strCompass = "-350--355--="
  50.     + "N=--005--010--015--020--025--030--035--040-=N.E=-050--055--060--065--070--075--080--085--=E=--095--100"
  51.     + "--105--110--115--120--125--130-=S.E=-140--145--150--155--160--165--170--175--=S=--185--190--195--200"
  52.     + "--205--210--215--220-=S.W=-230--235--240--245--250--255--260--265--=W=--275--280--285--290--295--300"
  53.     + "--305--310-=N.W=-320--325--330--335--340--345--350--355--="
  54.     + "N=--005--010-";
  55. string strMessage = "null";
  56. string strCardinalDirection = "";
  57. List<IMyTerminalBlock> listRemotes = new List<IMyTerminalBlock>();
  58. List<IMyTerminalBlock> listScreens = new List<IMyTerminalBlock>();
  59.  
  60. bool isRemote(IMyTerminalBlock block)
  61. {
  62.     var remote = block as IMyRemoteControl;
  63.     return remote != null;
  64. }
  65.  
  66. bool isTextPanel(IMyTerminalBlock block)
  67. {
  68.     var textPanel = block as IMyTextPanel;
  69.     return textPanel != null;
  70. }
  71.  
  72. Vector3D VectorProjection( Vector3D a, Vector3D b )
  73. {
  74.     Vector3D projection = a.Dot( b ) / b.Length() / b.Length() * b;  
  75.     return projection;
  76. }
  77.  
  78. void Main(string arg)
  79. {
  80.     Echo( "WMI Planetary Compass Online... " + RunningSymbol() );
  81.    
  82.     GridTerminalSystem.SearchBlocksOfName( strRemoteName, listRemotes, isRemote );
  83.     if(listRemotes.Count == 0)
  84.     {
  85.         Echo("[CRITICAL] No remote with tag '" + strRemoteName + "' found");
  86.         return;
  87.     }else{
  88.         GetBearing(); //run bearing code
  89.         WriteBearing(strMessage); //write to text screens
  90.         Echo("Bearing: " + Math.Round(bearingAngle, 2).ToString()); //for debugging
  91.     }
  92. }
  93.  
  94. void GetBearing()
  95. {
  96. //get forward and right vectors of the ship
  97.     var reference = listRemotes[0] as IMyRemoteControl;
  98.     MatrixD orientation = reference.WorldMatrix;
  99.     Vector3D forwardVec = orientation.Forward;
  100.      
  101. //get grav vector
  102.     Vector3D gravityVec = reference.GetNaturalGravity();
  103.      
  104. //check if grav vector exists
  105.     double gravMag = gravityVec.Length();
  106.         if(double.IsNaN( gravMag ) || gravMag == 0)
  107.         {
  108.             Echo("[CRITICAL] No gravity detected!");
  109.             strMessage = "Error: No natural gravity";
  110.             return;
  111.         }
  112.      
  113. //get east vector
  114.     Vector3D relativeEastVec = gravityVec.Cross(absoluteNorthVec);
  115.      
  116. //get relative north vector
  117.     Vector3D relativeNorthVec = relativeEastVec.Cross(gravityVec);
  118.      
  119. //project forward vector onto a plane comprised of the north and east vectors
  120.     Vector3D forwardProjNorthVec = VectorProjection(forwardVec, relativeNorthVec);
  121.     Vector3D forwardProjEastVec = VectorProjection(forwardVec, relativeEastVec);
  122.     Vector3D forwardProjPlaneVec = forwardProjEastVec + forwardProjNorthVec;
  123.      
  124. //find angle from abs north to projected forward vector measured clockwise
  125.     bearingAngle = Math.Acos(forwardProjPlaneVec.Dot(relativeNorthVec) / forwardProjPlaneVec.Length() / relativeNorthVec.Length()) * rad2deg;                                                                                                                                                   //w.h.i.P l a.s h 1. 4 1
  126.      
  127. //check direction of angle
  128.     double dotForwardOnEast = forwardVec.Dot(relativeEastVec);
  129.      
  130.     if(dotForwardOnEast < 0)
  131.     {
  132.         bearingAngle = 360 - bearingAngle; //because of how the angle is measured
  133.     }
  134.      
  135. //get cardinal direction
  136.     if(bearingAngle < 22.5 || bearingAngle >= 337.5)
  137.     {
  138.         strCardinalDirection = "N";
  139.     }
  140.     else if(bearingAngle < 67.5)
  141.     {
  142.         strCardinalDirection = "NE";
  143.     }
  144.     else if(bearingAngle < 112.5)
  145.     {
  146.         strCardinalDirection = "E";
  147.     }
  148.     else if(bearingAngle < 157.5)
  149.     {
  150.         strCardinalDirection = "SE";
  151.     }
  152.     else if(bearingAngle < 202.5)
  153.     {
  154.         strCardinalDirection = "S";
  155.     }
  156.     else if(bearingAngle < 247.5)
  157.     {
  158.         strCardinalDirection = "SW";
  159.     }
  160.     else if(bearingAngle < 292.5)
  161.     {
  162.         strCardinalDirection = "W";
  163.     }
  164.     else if(bearingAngle < 337.5)
  165.     {
  166.         strCardinalDirection = "NW";
  167.     }
  168.      
  169. //create string of our data
  170.     strMessage = "Bearing: " + string.Format("{0:000}", Math.Round(bearingAngle))
  171.         + " " + strCardinalDirection
  172.         + "\n[" + strCompass.Substring((int)Math.Floor(bearingAngle), 25)
  173.         + "]\n" + "------------------^------------------";
  174. }
  175.  
  176. void WriteBearing(string textToWrite)
  177. {
  178.  
  179.     GridTerminalSystem.SearchBlocksOfName(strScreenName, listScreens, isTextPanel);
  180.     if(listScreens.Count == 0)
  181.     {
  182.         Echo("[ALERT]: No text panel with name tag '" + strScreenName + "' was found");
  183.         return;
  184.     }else{
  185.         for(int i = 0; i < listScreens.Count; i++)
  186.         {
  187.             var thisScreen = listScreens[i] as IMyTextPanel;
  188.             if(thisScreen != null)
  189.             {
  190.                 thisScreen.WritePublicText(textToWrite);
  191.                
  192.                 //force text update
  193.                 thisScreen.ShowTextureOnScreen();
  194.                 thisScreen.ShowPublicTextOnScreen();
  195.             }
  196.         }
  197.     }
  198. }
  199.  
  200. //Whip's Running Symbol Method
  201. int trackSymbol = 0;
  202. string strRunningSymbol = "";
  203.  
  204. string RunningSymbol()
  205. {
  206.     switch( trackSymbol )
  207.     {
  208.         case 0:
  209.             strRunningSymbol = "|";
  210.             break;
  211.  
  212.         case 10:
  213.             strRunningSymbol = "/";
  214.             break;
  215.  
  216.         case 20:
  217.             strRunningSymbol = "--";
  218.             break;
  219.            
  220.         case 30:
  221.             strRunningSymbol = "\\";
  222.             break;
  223.        
  224.         default:
  225.             break;
  226.     }
  227.    
  228.     trackSymbol++;
  229.     if( trackSymbol > 40 )
  230.         trackSymbol = 0;
  231.    
  232.     return strRunningSymbol;
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement