Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- /// Whip's Compass & Bearing Code v8.2 - 3/30/16 ///
- What's Stable?
- - Bearing measured off relelative north direction
- How do I use this?
- 1) Make a program block with this script loaded into it
- 2) Make a remote control pointing forward
- - Add the phrase "Reference" somewhere in its name.
- 3) Make a text panel or LCD screen: Add the phrase "Bearing" in the name.
- Also make sure the screen is set to show public text.
- 4) Make a timer block
- - set to "trigger now" itself
- - set to "start" itself
- - set to "run program with default argument"
- 5) Trigger the timer once and you are good to go! :)
- (Optional): Tinker with the text size until the compass stretches
- the width of the screen. Recommended text size for
- small ship screens is 1.7 size font
- Be sure to drop by my workshop page and leave a comment :D
- http://steamcommunity.com/sharedfiles/filedetails/?id=616627882
- Code by Whiplash141
- */
- //You can change these names if you'd like
- const string strRemoteName = "Reference"; //name of remote to use as reference
- const string strScreenName = "Bearing"; //name of screen to display bearing on
- /*
- _______________________________________________________________________________________
- ======================== Don't edit anything beyond this :) =======================
- _______________________________________________________________________________________
- */
- Vector3D absoluteNorthVec = new Vector3D(0, 0, 1); //by convention +z is north
- double bearingAngle; //measured off relative north
- const double rad2deg = 180 / Math.PI; //constant to convert radians to degrees
- const string strCompass = "-350--355--="
- + "N=--005--010--015--020--025--030--035--040-=N.E=-050--055--060--065--070--075--080--085--=E=--095--100"
- + "--105--110--115--120--125--130-=S.E=-140--145--150--155--160--165--170--175--=S=--185--190--195--200"
- + "--205--210--215--220-=S.W=-230--235--240--245--250--255--260--265--=W=--275--280--285--290--295--300"
- + "--305--310-=N.W=-320--325--330--335--340--345--350--355--="
- + "N=--005--010-";
- string strMessage = "null";
- string strCardinalDirection = "";
- List<IMyTerminalBlock> listRemotes = new List<IMyTerminalBlock>();
- List<IMyTerminalBlock> listScreens = new List<IMyTerminalBlock>();
- bool isRemote(IMyTerminalBlock block)
- {
- var remote = block as IMyRemoteControl;
- return remote != null;
- }
- bool isTextPanel(IMyTerminalBlock block)
- {
- var textPanel = block as IMyTextPanel;
- return textPanel != null;
- }
- Vector3D VectorProjection( Vector3D a, Vector3D b )
- {
- Vector3D projection = a.Dot( b ) / b.Length() / b.Length() * b;
- return projection;
- }
- void Main(string arg)
- {
- Echo( "WMI Planetary Compass Online... " + RunningSymbol() );
- GridTerminalSystem.SearchBlocksOfName( strRemoteName, listRemotes, isRemote );
- if(listRemotes.Count == 0)
- {
- Echo("[CRITICAL] No remote with tag '" + strRemoteName + "' found");
- return;
- }else{
- GetBearing(); //run bearing code
- WriteBearing(strMessage); //write to text screens
- Echo("Bearing: " + Math.Round(bearingAngle, 2).ToString()); //for debugging
- }
- }
- void GetBearing()
- {
- //get forward and right vectors of the ship
- var reference = listRemotes[0] as IMyRemoteControl;
- MatrixD orientation = reference.WorldMatrix;
- Vector3D forwardVec = orientation.Forward;
- //get grav vector
- Vector3D gravityVec = reference.GetNaturalGravity();
- //check if grav vector exists
- double gravMag = gravityVec.Length();
- if(double.IsNaN( gravMag ) || gravMag == 0)
- {
- Echo("[CRITICAL] No gravity detected!");
- strMessage = "Error: No natural gravity";
- return;
- }
- //get east vector
- Vector3D relativeEastVec = gravityVec.Cross(absoluteNorthVec);
- //get relative north vector
- Vector3D relativeNorthVec = relativeEastVec.Cross(gravityVec);
- //project forward vector onto a plane comprised of the north and east vectors
- Vector3D forwardProjNorthVec = VectorProjection(forwardVec, relativeNorthVec);
- Vector3D forwardProjEastVec = VectorProjection(forwardVec, relativeEastVec);
- Vector3D forwardProjPlaneVec = forwardProjEastVec + forwardProjNorthVec;
- //find angle from abs north to projected forward vector measured clockwise
- bearingAngle = Math.Acos(forwardProjPlaneVec.Dot(relativeNorthVec) / forwardProjPlaneVec.Length() / relativeNorthVec.Length()) * rad2deg; //w.h.i.P l a.s h 1. 4 1
- //check direction of angle
- double dotForwardOnEast = forwardVec.Dot(relativeEastVec);
- if(dotForwardOnEast < 0)
- {
- bearingAngle = 360 - bearingAngle; //because of how the angle is measured
- }
- //get cardinal direction
- if(bearingAngle < 22.5 || bearingAngle >= 337.5)
- {
- strCardinalDirection = "N";
- }
- else if(bearingAngle < 67.5)
- {
- strCardinalDirection = "NE";
- }
- else if(bearingAngle < 112.5)
- {
- strCardinalDirection = "E";
- }
- else if(bearingAngle < 157.5)
- {
- strCardinalDirection = "SE";
- }
- else if(bearingAngle < 202.5)
- {
- strCardinalDirection = "S";
- }
- else if(bearingAngle < 247.5)
- {
- strCardinalDirection = "SW";
- }
- else if(bearingAngle < 292.5)
- {
- strCardinalDirection = "W";
- }
- else if(bearingAngle < 337.5)
- {
- strCardinalDirection = "NW";
- }
- //create string of our data
- strMessage = "Bearing: " + string.Format("{0:000}", Math.Round(bearingAngle))
- + " " + strCardinalDirection
- + "\n[" + strCompass.Substring((int)Math.Floor(bearingAngle), 25)
- + "]\n" + "------------------^------------------";
- }
- void WriteBearing(string textToWrite)
- {
- GridTerminalSystem.SearchBlocksOfName(strScreenName, listScreens, isTextPanel);
- if(listScreens.Count == 0)
- {
- Echo("[ALERT]: No text panel with name tag '" + strScreenName + "' was found");
- return;
- }else{
- for(int i = 0; i < listScreens.Count; i++)
- {
- var thisScreen = listScreens[i] as IMyTextPanel;
- if(thisScreen != null)
- {
- thisScreen.WritePublicText(textToWrite);
- //force text update
- thisScreen.ShowTextureOnScreen();
- thisScreen.ShowPublicTextOnScreen();
- }
- }
- }
- }
- //Whip's Running Symbol Method
- int trackSymbol = 0;
- string strRunningSymbol = "";
- string RunningSymbol()
- {
- switch( trackSymbol )
- {
- case 0:
- strRunningSymbol = "|";
- break;
- case 10:
- strRunningSymbol = "/";
- break;
- case 20:
- strRunningSymbol = "--";
- break;
- case 30:
- strRunningSymbol = "\\";
- break;
- default:
- break;
- }
- trackSymbol++;
- if( trackSymbol > 40 )
- trackSymbol = 0;
- return strRunningSymbol;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement